* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #ffffff;
    min-height: 100vh;
    padding: 20px;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
}

.background-grid {
    position: absolute;
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(15, 1fr);
    grid-template-rows: repeat(15, 1fr);
    z-index: 1;
    pointer-events: none;
}

.background-grid div {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    font-weight: 600;
    color: rgba(87, 189, 145, 0.3);
    letter-spacing: 2px;
}

.overlay {
    position: relative;
    z-index: 2;
    text-align: center;
    background-color: rgba(87, 189, 145, 0.1);
    backdrop-filter: blur(4px);
    padding: 30px 20px;
    border-radius: 16px;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
    width: 100%;
    max-width: 800px;
    margin: 0 auto;
    height: auto;
    max-height: none;
    min-height: 0;
}

.title {
    background-color: #57BD91;
    color: white;
    padding: 20px;
    border-radius: 12px;
    font-size: 32px;
    margin-bottom: 2.5rem; /* Increased gap below title */
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.content {
    display: flex;
    flex-direction: column;
    gap: 2.5rem; /* Increased gap between grid and word list */
    max-height: none;
    overflow: visible;
}

.grid-container {
    width: 420px;
    height: auto;
    aspect-ratio: 1 / 1;
    max-width: 90vw;
    max-height: 90vw;
    min-width: 220px;
    min-height: 220px;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: visible;
    margin: 0 auto;
}

#word-grid {
    width: 100%;
    height: 100%;
    table-layout: fixed;
    border-spacing: 2px;
    border-collapse: separate;
}

#word-grid td {
    width: calc(100% / var(--grid-size, 10));
    height: calc(100% / var(--grid-size, 10));
    min-width: 18px;
    min-height: 18px;
    max-width: 60px;
    max-height: 60px;
    text-align: center;
    font-size: 1.2rem;
    font-weight: bold;
    background-color: white;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    cursor: pointer;
    user-select: none;
    transition: all 0.2s ease;
    padding: 0;
}

#word-grid td.selected {
    background-color: #57BD91;
    color: white;
    transform: scale(0.95);
}

#word-grid td.correct {
    background-color: #45a678;
    color: white;
}

.word-list-container {
    background-color: rgba(255, 255, 255, 0.9);
    padding: 20px;
    border-radius: 12px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    max-height: none;
    overflow: visible;
}

#word-list {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    list-style: none;
    padding: 0;
}

#word-list li {
    background-color: #f8f9fa;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 1rem;
    transition: all 0.3s ease;
    color: #57BD91;
    font-weight: bold;
}

#word-list li.found {
    background-color: #57BD91;
    color: white;
    text-decoration: none;
    transform: scale(0.95);
}

@media (max-width: 600px) {
    body {
        padding: 0;
        overflow: auto;
        align-items: flex-start;
    }

    .overlay {
        padding: 6px 4px 16px 4px;
        height: auto;
        max-height: none;
        min-height: 0;
        display: flex;
        flex-direction: column;
        border-radius: 8px;
        margin-bottom: 20px;
        margin-top: 0;
        margin: 0 auto;
        overflow-y: visible;
        align-items: center;
        text-align: center;
        width: 100vw;
        min-height: 100vh;
        max-width: 100vw;
        max-height: 100vh;
        border-radius: 0;
        margin: 0;
        padding-top: 0.5rem;
        box-sizing: border-box;
    }

    .title {
        margin-bottom: 1.2rem;
        text-align: center;
        width: 100%;
        margin-top: 30px;
    }

    .content {
        flex: unset;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 1.2rem;
        max-height: none;
        overflow: visible;
        width: 100%;
    }

    .grid-container {
        margin: 0 0 1.2rem 0;
        align-self: center;
    }

    .word-list-container {
        margin: 0;
        align-self: center;
    }

    .grid-container {
        width: 98vw;
        aspect-ratio: 1 / 1;
        height: auto;
        min-width: 120px;
        min-height: 120px;
        max-width: 98vw;
        max-height: none;
        display: flex;
        align-items: stretch;
        justify-content: center;
        overflow: visible;
        margin-top: -0.5rem;
        margin-bottom: 0.5rem;
    }

    #word-grid {
        width: 100%;
        height: 100%;
        table-layout: fixed;
        border-spacing: 2px;
        border-collapse: separate;
    }

    #word-grid tr {
        height: auto;
    }

    #word-grid td {
        aspect-ratio: 1 / 1;
        width: auto;
        height: auto;
        min-width: 12px;
        min-height: 12px;
        max-width: 38px;
        max-height: 38px;
        font-size: 1.1rem;
        box-sizing: border-box;
        touch-action: manipulation;
    }

    .word-list-container {
        padding: 10px;
        margin-top: -0.5rem; /* Add a small margin above word list on mobile */
        margin-bottom: 15px;
        max-height: none;
        overflow: visible;
    }

    #word-list li {
        padding: 8px 12px;
        font-size: 1rem;
    }
}

@media (max-width: 400px) {
    .title {
        font-size: 1.2rem;
        padding: 8px;
    }

    .grid-container {
        width: 96vw;
        height: 96vw;
        min-width: 80px;
        min-height: 80px;
        max-width: 96vw;
        max-height: 96vw;
        margin-top: -0.5rem;
        overflow: visible;
    }

    #word-grid td {
        font-size: 0.9rem;
    }

    #word-list li {
        padding: 6px 10px;
        font-size: 0.9rem;
    }
}

@media (min-width: 768px) {
    .content {
        flex-direction: row;
        align-items: flex-start;
        max-height: 70vh;
        overflow-y: auto;
    }

    .grid-container {
        flex: 2;
    }

    .word-list-container {
        flex: 1;
        max-height: 60vh;
        margin-top: -0.2rem;
        overflow-y: auto;
    }
}
