:root {
    --bg-color: #2C211B;
    --board-bg: #3C2F24;
    --accent-color: #D4A574;
    --text-color: #F5EDE3;
    --tile-size: 56px;
    --gap: 5px;
    --tile-bg: #E8D9C0;
    --tile-border: #8B7355;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -webkit-touch-callout: none;
}

body {
    margin: 0;
    padding: 0;
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: 'Outfit', sans-serif;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    height: 100vh;
    width: 100vw;
    touch-action: none;
}

#game-container {
    width: 100%;
    max-width: 100vw;
    padding: 12px 16px 20px;
    display: grid;
    grid-template-rows: auto 1fr;
    align-items: stretch;
    gap: 10px;
    transform-origin: top center;
    min-height: 100vh;
}

#top-bar {
    width: 100%;
    padding: 0 20px;
    text-align: center;
}

h1 {
    font-weight: 900;
    font-size: 2.1rem;
    background: linear-gradient(45deg, #D4A574, #C17F59);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 4px 12px rgba(56, 189, 248, 0.25);
    letter-spacing: -1px;
}

.brand-name {
    font-weight: 700;
    color: #94a3b8;
    -webkit-text-fill-color: #94a3b8;
}

.game-name {
    font-weight: 900;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 0 20px;
}

.stats {
    display: flex;
    gap: 20px;
}

.stat-box {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.label {
    font-size: 0.7rem;
    color: #64748b;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.value {
    font-size: 1.65rem;
    font-weight: 800;
    line-height: 1;
    color: #F5EDE3;
}

main {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

/* === BOARD === */
.board {
    position: relative;
    display: grid;
    grid-template-columns: repeat(6, var(--tile-size));
    grid-template-rows: repeat(6, var(--tile-size));
    gap: var(--gap);
    background-color: var(--board-bg);
    border-radius: 16px;
    box-shadow: 0 10px 35px rgba(0, 0, 0, 0.55),
                inset 0 1px 0 rgba(255,255,255,0.08);
    padding: var(--gap);
    width: calc(6 * var(--tile-size) + 7 * var(--gap));
    height: calc(6 * var(--tile-size) + 7 * var(--gap));
    max-width: 100%;
    max-height: 100%;
    overflow: visible;
    margin: 0 auto;
}

/* Board wrapper - ensures the playable board area is always a perfect square */
.board-wrapper {
    aspect-ratio: 1 / 1;
    width: 100%;
    height: auto;
    max-height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Letter Tiles */
.letter-tile {
    width: var(--tile-size);
    height: var(--tile-size);
    background: linear-gradient(145deg, #E8D9C0, #D4C2A0);
    border: 1px solid var(--tile-border);
    border-radius: 12px;
    clip-path: polygon(
        25% 0%, 75% 0%,
        100% 25%, 100% 75%,
        75% 100%, 25% 100%,
        0% 75%, 0% 25%
    );
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.05rem;
    font-weight: 800;
    color: #3F2A1D;
    box-shadow: 
        0 3px 6px rgba(0, 0, 0, 0.35),
        inset 0 1px 0 rgba(255, 255, 255, 0.35),
        inset 0 -2px 4px rgba(0, 0, 0, 0.2);
    transition: transform 0.08s ease, box-shadow 0.08s ease, background 0.1s ease;
    cursor: pointer;
    position: relative;
    z-index: 2;
}

.letter-tile:active,
.letter-tile.in-path {
    transform: scale(0.92);
    background: linear-gradient(145deg, #C17F59, #8B5E3C);
    color: #F5EDE3;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3),
                inset 0 1px 0 rgba(255,255,255,0.3);
    border-color: #D4A574;
    z-index: 10;
    clip-path: polygon(
        25% 0%, 75% 0%,
        100% 25%, 100% 75%,
        75% 100%, 25% 100%,
        0% 75%, 0% 25%
    );
}

.letter-tile.found {
    animation: foundPop 650ms ease;
}

@keyframes foundPop {
    0% { transform: scale(1); }
    25% { transform: scale(1.18); }
    50% { transform: scale(0.95); }
    70% { transform: scale(1.08); }
    100% { transform: scale(1); }
}

/* Pleasant removal when a word is submitted */
.letter-tile.removing {
    transition: transform 0.34s cubic-bezier(0.55, 0.06, 0.68, 0.19),
                opacity 0.34s ease-out;
    transform: scale(0.12) rotate(22deg);
    opacity: 0;
    pointer-events: none;
    z-index: 10;
}

/* New tiles dropping in after gravity/refill */
.letter-tile.new-tile {
    animation: tileDropIn 0.52s cubic-bezier(0.23, 1.0, 0.32, 1) backwards;
}

@keyframes tileDropIn {
    0% {
        transform: translateY(-135%);
        opacity: 0;
    }
    70% {
        transform: translateY(4%);
    }
    100% {
        transform: translateY(0);
        opacity: 1;
    }
}

/* SVG Path Overlay */
.path-overlay {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
    z-index: 30;
    overflow: visible;
    /* Ensure it never participates in grid layout */
    grid-area: 1 / 1 / -1 / -1;
}

/* Current Word Display */
#current-word {
    min-height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.45rem;
    font-weight: 700;
    letter-spacing: 2px;
    color: #64748b;
    margin-top: 2px;
}

#current-word-text {
    padding: 4px 18px;
    border-radius: 9999px;
    transition: all 0.15s ease;
}

#current-word-text.valid {
    background: rgba(212, 165, 116, 0.18);
    color: #D4A574;
    box-shadow: 0 0 0 1px rgba(212, 165, 116, 0.35);
}

#current-word-text.invalid {
    background: rgba(248, 113, 113, 0.12);
    color: #fda4af;
}

/* Found Words Panel */
#found-words-panel {
    width: 100%;
    max-width: 520px;
    background: var(--board-bg);
    border-radius: 12px;
    padding: 10px 14px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.35);
    border: 1px solid rgba(255,255,255,0.06);
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 6px;
    padding: 0 4px;
}

.panel-header .label {
    font-size: 0.72rem;
    letter-spacing: 1px;
}

.count {
    font-weight: 800;
    color: #D4A574;
    font-size: 0.95rem;
}

.words-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    max-height: 78px;
    overflow-y: auto;
    padding: 2px;
}

.words-list::-webkit-scrollbar {
    height: 4px;
}

.words-list::-webkit-scrollbar-thumb {
    background: #5C4635;
    border-radius: 999px;
}

.word-item {
    background: #3C2F24;
    border: 1px solid #5C4635;
    padding: 3px 11px;
    border-radius: 9999px;
    font-size: 0.92rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.word-text {
    color: #F5EDE3;
}

.word-points {
    color: #64748b;
    font-size: 0.78rem;
    font-weight: 700;
}

.empty-state {
    color: #475569;
    font-size: 0.85rem;
    padding: 4px 8px;
}

/* Controls */
#controls {
    display: flex;
    gap: 12px;
    margin-top: 4px;
}

.btn-primary,
.btn-secondary {
    border: none;
    padding: 11px 26px;
    border-radius: 9999px;
    font-weight: 700;
    font-size: 0.95rem;
    cursor: pointer;
    transition: all 0.15s ease;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.25);
}

.btn-primary {
    background: linear-gradient(45deg, #D4A574, #C17F59);
    color: white;
}

.btn-primary:hover {
    transform: translateY(-1px);
    box-shadow: 0 5px 16px rgba(56, 189, 248, 0.45);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-secondary {
    background: #5C4635;
    color: #F5EDE3;
    border: 1px solid #8B7355;
}

.btn-secondary:hover {
    background: #6F573F;
}

.btn-primary.disabled,
.btn-secondary.disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Modals (matching Gem Swap) */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(15, 23, 42, 0.92);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.modal.hidden {
    display: none;
}

.menu-content {
    background-color: var(--board-bg);
    padding: 42px 46px;
    border-radius: 22px;
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: 0 25px 60px rgba(0, 0, 0, 0.6);
    width: 340px;
}

.menu-icon {
    width: 104px;
    height: 104px;
    border-radius: 24px;
    object-fit: cover;
    margin-bottom: 16px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45),
                0 0 0 1px rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.06);
}

.menu-title {
    font-size: 2.1rem;
    margin-bottom: 6px;
}

.menu-subtitle {
    color: #64748b;
    margin-bottom: 26px;
    font-size: 1rem;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.btn-gem {
    padding: 14px 32px;
    border-radius: 9999px;
    font-weight: 800;
    font-size: 1.05rem;
    border: none;
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-gem-continue {
    background: linear-gradient(45deg, #8B7355, #5C4635);
    color: #F5EDE3;
}

.btn-gem-new {
    background: linear-gradient(45deg, #D4A574, #C17F59);
    color: white;
}

.menu-footer {
    margin-top: 28px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8rem;
    color: #475569;
}

.version-text {
    font-family: monospace;
}

.btn-text {
    background: none;
    border: none;
    color: #64748b;
    font-size: 0.78rem;
    cursor: pointer;
    padding: 4px 8px;
}

.btn-text:hover {
    color: #94a3b8;
}

/* Toast */
.toast {
    position: fixed;
    bottom: 26px;
    left: 50%;
    transform: translateX(-50%) translateY(30px);
    background: #3C2F24;
    border: 1px solid #5C4635;
    color: #F5EDE3;
    padding: 10px 22px;
    border-radius: 9999px;
    display: flex;
    align-items: center;
    gap: 14px;
    font-weight: 700;
    font-size: 1.05rem;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    opacity: 0;
    transition: all 0.2s cubic-bezier(0.23, 1, 0.32, 1);
    z-index: 200;
    pointer-events: none;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.toast.error {
    background: #3f1f25;
    border-color: #6b2d36;
    color: #fda4af;
}

#toast-points {
    color: #D4A574;
    font-weight: 800;
}

/* ========================================
   RESPONSIVE LAYOUT — 4:3 iPad Landscape Focus
   ======================================== */

.play-area {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

/* ---- Landscape / Horizontal mode (maximizes space on 4:3 iPad & wider) ---- */
@media (min-width: 800px) and (orientation: landscape) {
    #game-container {
        height: 100vh;
        min-height: 0;
        padding: 6px 12px 10px;
        gap: 4px;
        justify-items: stretch;
        overflow: hidden;
    }

    .play-area {
        display: grid;
        grid-template-columns: minmax(0, 1.75fr) minmax(300px, 0.95fr);
        gap: 16px;
        align-items: stretch;
        width: 100%;
        height: 100%;
        min-height: 0;
        overflow: hidden;
    }

    .board-area {
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: stretch;
        min-height: 0;
        overflow: hidden;
    }

    main {
        flex: 1;
        display: flex;
        flex-direction: column;
        min-height: 0;
    }

    .sidebar {
        display: flex;
        flex-direction: column;
        gap: 10px;
        min-height: 0;
        overflow: hidden;
    }

    .board {
        width: 100%;
        height: 100%;
        /* Use fractional columns so tiles grow to fill the square container */
        grid-template-columns: repeat(6, 1fr);
        grid-template-rows: repeat(6, 1fr);
    }

    .letter-tile {
        width: 100%;
        height: 100%;
        font-size: clamp(1.7rem, 5.5vh, 2.6rem);
    }

    #current-word {
        margin-top: 2px;
        font-size: clamp(1.25rem, 2.6vh, 1.7rem);
    }

    /* Tight sidebar */
    .sidebar header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 100%;
        padding: 0 2px;
        flex-shrink: 0;
    }

    #found-words-panel {
        max-width: none;
        width: 100%;
        flex: 1;
        min-height: 0;
        padding: 6px 8px;
        overflow: hidden;
    }

    .words-list {
        max-height: none;
        flex: 1;
        overflow-y: auto;
    }

    #controls {
        margin-top: 0;
        gap: 8px;
        flex-shrink: 0;
    }

    .btn-primary,
    .btn-secondary {
        padding: 9px 18px;
        font-size: 0.85rem;
    }
}

/* ---- Portrait / Narrow screens (phones + portrait tablets) ---- */
@media (max-width: 820px) {
    #game-container {
        width: 100%;
        max-width: 100vw;
        padding: 4px 6px 10px;
        gap: 6px;
    }

    .board-area {
        width: 100%;
    }

    /* Board maximizes width in the play area while staying square */
    .board {
        width: 100%;
        height: auto;
        aspect-ratio: 1 / 1;
        max-width: 100%;
        /* Use fractional columns so the grid fills the full available width */
        grid-template-columns: repeat(6, 1fr);
        grid-template-rows: repeat(6, 1fr);
    }

    .letter-tile {
        width: 100%;
        height: 100%;
        font-size: clamp(2rem, 8vw, 3.2rem);
    }

    #current-word {
        margin-top: 2px;
        font-size: 1.2rem;
    }

    /* Portrait: keep button visual style identical to horizontal.
       Only adjust layout so the three control buttons do not stretch full width,
       and maximize space for the Found Words list. */
    .sidebar header {
        margin-bottom: 14px;
        padding-bottom: 8px;
        border-bottom: 1px solid #334155;
    }

    #found-words-panel {
        padding: 12px 14px;
        margin-bottom: 12px;
        background: var(--board-bg);
        border-radius: 12px;
        flex: 1; /* take as much vertical space as possible */
    }

    .words-list {
        max-height: none;
        flex: 1;
    }

    #controls {
        gap: 10px;
        margin-top: 8px;
        align-items: center; /* center the compact buttons */
    }

    /* Three control buttons: same visual treatment as Main Menu — not full width */
    #controls .btn-primary,
    #controls .btn-secondary {
        width: auto;
        padding: 10px 18px; /* match Main Menu proportions */
    }

    /* Main Menu button stays compact (consistent with horizontal) */
    #main-menu-btn {
        width: auto;
    }
}
