/* General Styles & Fonts */
:root {
    --grid-rows: 5;
    --grid-cols: 5;
    --cell-size: min(16vw, 70px);
    --header-height: 4.6rem;

    --main-bg: #f7f7f7;
    --cell-bg: #fff;
    --black-cell-bg: #222222;
    --active-cell-bg: #ffe082;
    --active-word-bg: #a7d8ff;
    --font-color: #222222;
    --accent-color: #121212;
    --clue-highlight: #1976d2;
    --leaderboard-bg: #fff;
    --leaderboard-accent: #1976d2;
    --border-color: #222222;
}

*, *::before, *::after {
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--main-bg);
    color: var(--font-color);
    margin: 0;
    padding: var(--header-height) 1rem 1rem;
    min-height: 100vh;
}

#game-container {
    display: flex;
    flex-direction: column;
    padding-top: 1rem;
    gap: 2rem;
    max-width: 1000px;
    width: 100%;
    margin: 0 auto;
}

#main-panel {
    display: none;
    flex-wrap: wrap; /* This is key for responsiveness */
    gap: 2.2rem;
    justify-content: center;
    align-items: flex-start; /* ADDED: Align items to the top */
}

#main-panel.active {
    display: flex;
}

#crossword-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    /* ADDED: Flex properties for fluid wrapping */
    flex: 1 1 360px; /* Grow, shrink, with a base size */
}

#controls {
    font-size: 1.2rem;
    font-weight: bold;
    color: var(--font-color);
}

#grid-container {
    display: grid;
    grid-template-columns: repeat(var(--grid-cols), var(--cell-size));
    grid-template-rows: repeat(var(--grid-rows), var(--cell-size));
    border: 2px solid var(--border-color);
    background-color: var(--black-cell-bg);
    gap: 1px;
}

.grid-cell {
    position: relative;
    background-color: var(--cell-bg);
    display: flex;
    justify-content: center;
    align-items: center;
}

.grid-cell.black { background-color: var(--black-cell-bg); }
.grid-cell.active-word { background-color: var(--active-word-bg); }
.grid-cell.active { background-color: var(--active-cell-bg); }

.cell-number {
    position: absolute;
    top: 2px;
    left: 4px;
    font-size: calc(var(--cell-size) / 5);
    font-weight: bold;
}

.cell-input {
    width: 70%;
    height: 70%;
    border: none;
    text-align: center;
    text-transform: uppercase;
    font-size: calc(var(--cell-size) / 2);
    font-weight: bold;
    background-color: transparent;
    color: var(--font-color);
    padding: 0;
    caret-color: transparent;
}

.cell-input:focus {
    outline: none;
}

#clues-container {
    flex: 1 1 300px;
    min-width: 280px;
    max-width: 100%;
}


#clues-container h3 {
    margin-top: 0;
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 0.5rem;
}

#clues-container ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

#clues-container li {
    margin-bottom: 0.5rem;
    cursor: pointer;
}

.active-clue { font-weight: bold; color: var(--clue-highlight); }

#active-clue-bar {
    width: 100%; /* CHANGED: Use 100% to fit its container */
    min-height: 2.2em;
    height: auto;
    display: flex;
    align-items: center;
    margin-bottom: 0.5rem;
    padding: 0.75rem 0.75rem;
    background: var(--active-word-bg);
    border-radius: 6px;
    border: 2px solid var(--leaderboard-accent);
    color: var(--font-color);
    overflow-wrap: break-word;
    word-break: break-word;
}

/* Header Bar Styles */
#header-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    min-height: var(--header-height);
    z-index: 6000;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.7rem 1.5rem;
    background: var(--leaderboard-bg);
    color: var(--font-color);
    box-shadow: 0 2px 8px rgba(0,0,0,0.07);
}

.header-title { display: flex; flex-direction: column; }
#header-bar h1 { margin: 0; font-size: 2rem; font-weight: bold; }
#header-bar #puzzle-date { margin: 0; font-size: 1rem; }

#show-leaderboard-btn {
    margin: 0;
    font-size: 1.5rem;
    padding: 0.5rem 0.5rem;
    border: none;
    cursor: pointer;
    font-weight: bold;
    background: transparent;
}

#show-leaderboard-btn:hover { text-shadow: 0 0 2px var(--accent-color); }

#change-person-btn {
    margin: 0;
    font-size: 1.5rem;
    padding: 0.5rem 0.5rem;
    border: none;
    cursor: pointer;
    font-weight: bold;
    background: transparent;
}
#change-person-btn:hover { text-shadow: 0 0 2px var(--accent-color); }

.leaderboard-window {
    display: none;
    position: fixed;
    top: var(--header-height); /* CHANGED: Use variable */
    left: 0;
    width: 100vw;
    height: calc(100vh - var(--header-height)); /* CHANGED: Use variable */
    background: var(--main-bg);
    justify-content: center;
    align-items: flex-start; /* CHANGED: Align to top */
    padding-top: 2rem;
    overflow-y: auto;
    z-index: 5000;
}

.leaderboard-window.active { display: flex; }

.leaderboard-content {
    background: var(--leaderboard-bg);
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    padding: 2.5rem;
    width: 90%; /* CHANGED: More responsive width */
    max-width: 480px; /* Increased max-width slightly */
    display: flex;
    flex-direction: column;
    gap: 1.2rem;
    position: relative;
}

.leaderboard-title {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    margin-bottom: 0.5rem;
}

.leaderboard-title h2 { text-align: center; margin: 0; }
.leaderboard-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    font-size: 1.7rem;
    background: transparent;
    color: var(--font-color);
    border: none;
    cursor: pointer;
    font-weight: bold;
    padding: 0;
    line-height: 1;
}

.leaderboard-header {
    display: flex;
    font-weight: bold;
    font-size: 1.1rem;
    border-bottom: 2px solid var(--active-word-bg);
    padding-bottom: 0.5rem;
    color: var(--leaderboard-accent);
}

.leaderboard-header .rank-col { width: 60px; }
.leaderboard-header .name-col { flex: 1; }
.leaderboard-header .time-col { width: 80px; text-align: right; }

#leaderboard-list { list-style: none; padding: 0; margin: 0; }
#leaderboard-list li {
    display: flex;
    align-items: center;
    font-size: 1.05rem;
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--active-word-bg);
}

#leaderboard-list li .rank { width: 60px; font-weight: bold; color: #888; }
#leaderboard-list li .name { flex: 1; color: var(--font-color); }
#leaderboard-list li .time { width: 80px; text-align: right; font-family: monospace; color: var(--font-color); }
#leaderboard-list li.current-player {
    font-weight: bold;
    border-bottom: 1px solid var(--font-color);
}
/* Generic Modal Styles */
.modal-overlay {
    position: fixed;
    top: var(--header-height); /* CHANGED: Use variable */
    left: 0;
    width: 100%;
    height: calc(100vh - var(--header-height)); /* CHANGED: Use variable */
    background-color: rgba(247, 247, 247, 0.9);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 4000;
}

.modal-overlay.active { display: flex; }

#name-modal { z-index: 4001; }

.modal-content {
    background: var(--leaderboard-bg);
    padding: 2rem;
    border-radius: 8px;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    text-align: center;
    max-width: 350px;
    width: 90%;
}

.modal-content h2 { margin-top: 0; }
.modal-content p { margin-bottom: 1.5rem; }
.modal-content input {
    width: 100%;
    padding: 0.8rem;
    font-size: 1rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    margin-bottom: 1rem;
}

.modal-content button {
    width: 100%;
    padding: 0.8rem;
    font-size: 1.1rem;
    font-weight: bold;
    border: none;
    border-radius: 4px;
    background-color: var(--font-color);
    color: white;
    cursor: pointer;
    transition: background-color 0.2s;
}

.modal-content button:hover { background-color: var(--accent-color); }
.modal-content button:disabled { background-color: #cccccc; cursor: not-allowed; }
.modal-content button:disabled:hover { background-color: #cccccc; }

.keyboard-container {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 5px 2px;
    background-color: #d1d5db;
    display: none;
    flex-direction: column;
    gap: 5px;
    z-index: 10000;
}

@media (max-width: 600px) {
    :root {
        --cell-size: calc(95vw / var(--grid-cols));
    }

    #header-bar {
        min-height: 5vw;
        padding: 0.25rem 1.5rem;
        justify-content: space-between;
    }

    #header-bar h1 { margin: 0; font-size: 1rem; font-weight: bold; }
    #header-bar #puzzle-date { margin: 0; font-size: 0.625rem; }
    #show-leaderboard-btn {
        font-size: 1rem;
        width: 0.5rem;
        margin: 0;
        padding: 0.125rem;
    }

    #change-person-btn {
        font-size: 1rem;
        width: 0.5rem;
        margin: 0;
        padding: 0.125rem;
    }

    #controls {
        font-size: 1rem;
        font-weight: bold;
        color: var(--font-color);
    }

    html, body {
        height: 100svh;
        overflow: hidden;
    }

    #clues-container {
        display: none;
    }

    #main-panel {
        width: 100%;
        align-items: flex-start;
        justify-content: center;
    }

    #game-container {
        display: flex;
        align-items: flex-start;
        height: calc(100svh - var(--header-height));
        padding-top: 0;
    }

    .cell-input {
        font-size: max(calc(var(--cell-size) / 2), 16px);
    }

    #main-panel.active ~ .keyboard-container {
        display: flex;
    }
    #main-panel.active ~ .keyboard-container {
        display: flex;
    }
    .keyboard-container {
        display: none;
    }

    .keyboard-container.visible {
        display: flex;
        padding-bottom: 1rem;
    }

    .keyboard-row {
        display: flex;
        justify-content: center;
        gap: 4px;
    }

    .keyboard-key {
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
        font-size: 16px;
        font-weight: 600;
        height: 40px;
        flex: 1;
        max-width: 35px;
        border: none;
        border-radius: 4px;
        background-color: #ffffff;
        color: #111827;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    .keyboard-key:active {
        background-color: #a7d8ff;
    }

    .special-key {
        flex: 1.5;
    }
}