/* ===== CSS Variables ===== */
:root {
    --word-length: 6;
    --max-guesses: 10;
    --color-green: #01689b;
    --color-yellow: #c15a28;
    --color-gray: #3a3a3c;
    --color-bg: #121213;
    --color-tile-border: #555;
    --color-tile-active: #888;
    --color-text: #ffffff;
    --color-key-bg: #818384;
    --color-header-bg: #1a1a1b;
}

/* ===== Light Theme ===== */
body.theme-light {
    --color-bg: #f5f5f5;
    --color-text: #1a1a1b;
    --color-gray: #787c7e;
    --color-tile-border: #ccc;
    --color-tile-active: #999;
    --color-key-bg: #d3d6da;
    --color-header-bg: #ffffff;
}

body.theme-light .board {
    border-color: #ccc;
    background: #ffffff;
}

body.theme-light .tile.gray {
    background-color: #787c7e;
    border-color: #787c7e;
    color: #fff;
}

body.theme-light .tile.green,
body.theme-light .tile.yellow {
    color: #fff;
}

body.theme-light .key-quadrant.gray {
    background-color: #adb0b3;
}

body.theme-light .toast {
    background: #1a1a1b;
    color: #fff;
}

body.theme-light #modal {
    background: #ffffff;
    border-color: #ddd;
}

body.theme-light #modal-title {
    color: #1a1a1b;
}

body.theme-light #modal-body {
    color: #444;
}

body.theme-light .modal-answer-def {
    color: rgba(0, 0, 0, 0.65);
}

body.theme-light .score-breakdown {
    background: rgba(0, 0, 0, 0.05);
}

body.theme-light .score-line {
    color: #444;
}

body.theme-light .score-subtotal {
    color: #1a1a1b;
    border-color: #ccc;
}

body.theme-light #info-btn {
    border-color: #999;
    color: #999;
}

body.theme-light #info-btn:hover {
    border-color: #333;
    color: #333;
}

/* ===== High Contrast Theme ===== */
body.theme-highcontrast {
    --color-green: #00b300;
    --color-yellow: #ff8c00;
    --color-gray: #555555;
    --color-bg: #000000;
    --color-tile-border: #888;
    --color-tile-active: #fff;
    --color-text: #ffffff;
    --color-key-bg: #666;
}

body.theme-highcontrast .board {
    border-color: #fff;
    border-width: 3px;
}

body.theme-highcontrast .tile {
    border-width: 2px;
}

body.theme-highcontrast .tile.green {
    border-color: #00ff00;
}

body.theme-highcontrast .tile.yellow {
    border-color: #ffaa00;
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
}

body {
    background-color: var(--color-bg);
    color: var(--color-text);
    font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
    padding-top: env(safe-area-inset-top, 0);
    padding-bottom: env(safe-area-inset-bottom, 0);
}

/* ===== Header ===== */
#header {
    width: 100%;
    max-width: 700px;
    padding: 4px 8px;
    border-bottom: 1px solid var(--color-gray);
    flex-shrink: 0;
}

#header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    gap: 8px;
}

#header-logo {
    flex-shrink: 0;
}

#logo-img {
    height: 40px;
    width: auto;
    display: block;
}

#header-center {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

#header-right {
    flex-shrink: 0;
    min-width: 64px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
}

/* ===== Theme Button ===== */
#theme-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 2px solid #888;
    background: transparent;
    font-size: 0.75rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
    padding: 0;
}

#theme-btn:hover {
    border-color: #fff;
}

body.theme-light #theme-btn {
    border-color: #999;
}

body.theme-light #theme-btn:hover {
    border-color: #333;
}

/* ===== Info Button ===== */
#info-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 2px solid #888;
    background: transparent;
    color: #888;
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

#info-btn:hover {
    border-color: #fff;
    color: #fff;
}

#game-type-selector {
    display: flex;
    justify-content: center;
    gap: 6px;
}

.type-btn {
    background: var(--color-gray);
    color: var(--color-text);
    border: 2px solid transparent;
    border-radius: 6px;
    padding: 3px 14px;
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.type-btn:hover {
    background: #565758;
}

.type-btn.active {
    border-color: #c15a28;
    background: #c15a28;
    color: #fff;
}

#guess-counter {
    font-size: 0.75rem;
    color: #888;
}

/* ===== Give Up Button ===== */
#give-up-btn {
    background: #c0392b;
    color: white;
    border: none;
    border-radius: 6px;
    padding: 5px 12px;
    font-size: 0.7rem;
    font-weight: 700;
    cursor: pointer;
    transition: opacity 0.2s;
    white-space: nowrap;
}

#give-up-btn:hover {
    opacity: 0.85;
}

#give-up-btn.hidden {
    visibility: hidden;
}

/* ===== Game Container ===== */
#game-container {
    flex: 1 1 0;
    display: flex;
    align-items: stretch;
    justify-content: center;
    width: 100%;
    max-width: 700px;
    padding: 4px;
    overflow: hidden;
    min-height: 0;
}

/* ===== Boards Wrapper (2x2 grid) ===== */
#boards-wrapper {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    gap: 6px;
    width: 100%;
    height: 100%;
}

/* ===== Individual Board ===== */
.board {
    display: grid;
    gap: 1px;
    position: relative;
    grid-template-columns: repeat(var(--word-length), 1fr);
    grid-template-rows: repeat(var(--max-guesses), 1fr);
    overflow: hidden;
    min-height: 0;
    min-width: 0;
    border: 2px solid #666;
    border-radius: 4px;
    padding: 2px;
    background: #1a1a1b;
}

.board.solved::after {
    content: '';
    position: absolute;
    inset: 0;
    background: rgba(1, 104, 155, 0.15);
    border-radius: 4px;
    pointer-events: none;
    z-index: 1;
}

/* ===== Tiles ===== */
.tile {
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: clamp(0.45rem, 1.6vw, 0.85rem);
    text-transform: uppercase;
    border: 1px solid var(--color-tile-border);
    background: transparent;
    color: var(--color-text);
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    transition: border-color 0.1s, font-size 0.15s;
}

.tile.filled {
    border-color: var(--color-tile-active);
}

.tile.current-row {
    border-color: var(--color-tile-active);
}

/* Active row: the row you're currently typing in — bigger font + brighter border */
.tile.active-row {
    font-size: clamp(1rem, 3.5vw, 2rem);
    border-color: var(--color-tile-active);
}

.tile.green {
    background-color: var(--color-green);
    border-color: var(--color-green);
}

.tile.yellow {
    background-color: var(--color-yellow);
    border-color: var(--color-yellow);
}

.tile.gray {
    background-color: var(--color-gray);
    border-color: var(--color-gray);
}

/* ===== Stats Row (appears after game end) ===== */
.stats-tile {
    font-weight: 700;
    font-size: clamp(0.55rem, 2.2vw, 0.9rem);
    border: none !important;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 18px;
    border-radius: 2px;
    background: transparent;
}

.stats-fraction {
    background-color: #e25822 !important;
}

.stats-possibles {
    background-color: #d4a017 !important;
}

/* ===== Tile Animations ===== */
.tile.flip {
    animation: flipTile 0.5s ease-in-out;
}

@keyframes flipTile {
    0%   { transform: rotateX(0deg); }
    50%  { transform: rotateX(90deg); }
    100% { transform: rotateX(0deg); }
}

.tile.pop {
    animation: popTile 0.1s ease-in-out;
}

@keyframes popTile {
    0%   { transform: scale(1); }
    50%  { transform: scale(1.12); }
    100% { transform: scale(1); }
}

.board.shake {
    animation: shakeBoard 0.3s ease-in-out;
}

@keyframes shakeBoard {
    0%, 100% { transform: translateX(0); }
    20%      { transform: translateX(-4px); }
    40%      { transform: translateX(4px); }
    60%      { transform: translateX(-4px); }
    80%      { transform: translateX(4px); }
}

.tile.bounce {
    animation: bounceTile 0.5s ease;
}

@keyframes bounceTile {
    0%   { transform: translateY(0); }
    30%  { transform: translateY(-8px); }
    60%  { transform: translateY(2px); }
    100% { transform: translateY(0); }
}

/* ===== Keyboard ===== */
#keyboard-container {
    width: 100%;
    max-width: 500px;
    padding: 4px 4px 2px;
    flex-shrink: 0;
}

.keyboard-row {
    display: flex;
    justify-content: center;
    gap: 3px;
    margin-bottom: 3px;
}

.key {
    position: relative;
    height: 42px;
    min-width: 24px;
    flex: 1;
    max-width: 42px;
    border-radius: 4px;
    overflow: hidden;
    border: none;
    cursor: pointer;
    padding: 0;
    background: var(--color-key-bg);
    touch-action: manipulation;
}

.key.wide {
    min-width: 44px;
    max-width: 64px;
    flex: 1.5;
}

.key-quadrant {
    position: absolute;
    width: 50%;
    height: 50%;
    background: var(--color-key-bg);
    transition: background-color 0.3s;
}

.key-quadrant.q-tl { top: 0; left: 0; }
.key-quadrant.q-tr { top: 0; right: 0; }
.key-quadrant.q-bl { bottom: 0; left: 0; }
.key-quadrant.q-br { bottom: 0; right: 0; }

.key-quadrant.green  { background-color: var(--color-green); }
.key-quadrant.yellow { background-color: var(--color-yellow); }
.key-quadrant.gray   { background-color: #4a4a4c; }

.key-label {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 700;
    color: white;
    pointer-events: none;
    z-index: 1;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.key:active {
    opacity: 0.7;
}

/* ===== Hint Bar ===== */
#hint-bar {
    width: 100%;
    max-width: 500px;
    display: flex;
    justify-content: center;
    padding: 2px 4px;
    flex-shrink: 0;
}

#hint-btn {
    background: linear-gradient(135deg, #2d6a4f, #40916c);
    color: white;
    border: none;
    border-radius: 6px;
    padding: 5px 14px;
    font-size: 0.7rem;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    white-space: nowrap;
}

#hint-btn:hover {
    opacity: 0.9;
    transform: scale(1.02);
}

#hint-btn.hidden {
    display: none;
}

/* ===== Ad Overlay (Rewarded Hint) ===== */
#ad-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.92);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1001;
}

.ad-overlay-content {
    text-align: center;
    color: white;
    padding: 20px;
    width: 100%;
    max-width: 420px;
}

.ad-overlay-content p {
    font-size: 0.95rem;
    margin-bottom: 12px;
    color: #ccc;
}

.ad-overlay-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(255,255,255,0.2);
    border-top-color: #c15a28;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 16px auto;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ===== Toast ===== */
#toast-container {
    position: fixed;
    top: 80px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 1000;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    pointer-events: none;
}

.toast {
    background: var(--color-text);
    color: var(--color-bg);
    padding: 10px 18px;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 600;
    opacity: 1;
    transition: opacity 0.3s;
}

.toast.fade-out {
    opacity: 0;
}

/* ===== Modal ===== */
#modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 999;
}

#modal-overlay.hidden {
    display: none;
}

#modal {
    background: #1e1e1f;
    border: 1px solid #444;
    border-radius: 12px;
    padding: 24px;
    max-width: 360px;
    width: 90%;
    text-align: center;
    max-height: 85vh;
    overflow-y: auto;
}

#modal-title {
    font-size: 1.4rem;
    margin-bottom: 12px;
}

#modal-body {
    font-size: 0.9rem;
    color: #ccc;
    margin-bottom: 16px;
    line-height: 1.5;
}

.modal-answers {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin: 12px 0;
}

.modal-answer {
    padding: 6px 8px;
    border-radius: 4px;
    font-weight: 700;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.modal-answer.solved {
    background: var(--color-green);
    color: white;
}

.modal-answer.unsolved {
    background: #c0392b;
    color: white;
}

.modal-answer-def {
    font-size: 0.6rem;
    font-weight: 400;
    text-transform: none;
    letter-spacing: 0;
    color: rgba(255, 255, 255, 0.85);
    margin-top: 3px;
    line-height: 1.3;
    max-height: 2.6em;
    overflow: hidden;
}

/* ===== Score Breakdown ===== */
.score-breakdown {
    text-align: left;
    margin: 12px 0;
    padding: 10px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
}

.score-breakdown h3 {
    text-align: center;
    font-size: 0.9rem;
    margin-bottom: 8px;
    color: #c15a28;
}

.score-line {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    padding: 3px 0;
    color: #ccc;
}

.score-subtotal {
    border-top: 1px solid #555;
    padding-top: 6px;
    margin-top: 4px;
    font-weight: 600;
    color: #fff;
}

.score-final {
    border-top: 2px solid #c15a28;
    padding-top: 6px;
    margin-top: 4px;
    font-weight: 700;
    font-size: 1rem;
    color: #c15a28;
}

.daily-note {
    font-style: italic;
    color: #c15a28;
    margin-top: 8px;
}

.modal-btn {
    background: var(--color-green);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 10px 28px;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    transition: opacity 0.2s;
}

.modal-btn:hover {
    opacity: 0.85;
}

.modal-btn-cancel {
    background: var(--color-gray);
}

.modal-btn-danger {
    background: #c0392b;
}

.modal-btn-share {
    background: #2d6a4f;
}

#modal-actions {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
}

/* ===== Countdown Timer ===== */
.countdown-timer {
    font-size: 0.85rem;
    color: #c15a28;
    font-weight: 600;
    margin-top: 10px;
    padding: 6px 0;
}

/* ===== How to Play Modal ===== */
.how-to-play {
    text-align: left;
}

.how-to-play p {
    margin-bottom: 8px;
}

.how-to-play ul {
    margin: 8px 0 12px 20px;
}

.how-to-play li {
    margin-bottom: 4px;
    font-size: 0.85rem;
}

.how-to-play h3 {
    font-size: 0.95rem;
    color: #c15a28;
    margin: 12px 0 6px;
    border-bottom: 1px solid #333;
    padding-bottom: 4px;
}

.htp-examples {
    margin: 8px 0;
}

.htp-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 6px;
    font-size: 0.85rem;
}

.htp-tile {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    font-weight: 700;
    font-size: 0.9rem;
    border-radius: 3px;
    color: white;
    flex-shrink: 0;
}

.htp-green { background: var(--color-green); }
.htp-yellow { background: var(--color-yellow); }
.htp-gray { background: var(--color-gray); }

.htp-keyboard-demo {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px;
    margin: 8px 0;
}

.htp-quad-label {
    font-size: 0.75rem;
    color: #aaa;
    padding: 4px 8px;
    background: rgba(255,255,255,0.05);
    border-radius: 4px;
    text-align: center;
}

.htp-stat-fraction {
    display: inline-block;
    background: #e25822;
    color: white;
    padding: 1px 6px;
    border-radius: 3px;
    font-weight: 700;
    font-size: 0.8rem;
}

.htp-stat-possible {
    display: inline-block;
    background: #d4a017;
    color: white;
    padding: 1px 6px;
    border-radius: 3px;
    font-weight: 700;
    font-size: 0.8rem;
}

/* ===== Responsive: Mobile ===== */
@media (max-width: 600px) {
    #header {
        padding: 2px 4px;
    }

    #header-row {
        gap: 4px;
    }

    #logo-img {
        height: 28px;
    }

    #game-type-selector {
        gap: 4px;
    }

    .type-btn {
        padding: 2px 8px;
        font-size: 0.6rem;
        border-width: 1px;
    }

    #guess-counter {
        font-size: 0.6rem;
    }

    #give-up-btn {
        padding: 3px 8px;
        font-size: 0.6rem;
    }

    #theme-btn {
        width: 24px;
        height: 24px;
        font-size: 0.65rem;
        border-width: 1.5px;
    }

    #info-btn {
        width: 24px;
        height: 24px;
        font-size: 0.7rem;
        border-width: 1.5px;
    }

    #header-right {
        min-width: 50px;
        gap: 4px;
    }

    #game-container {
        padding: 2px;
        flex: 1 1 0;
        min-height: 0;
    }

    #boards-wrapper {
        gap: 2px;
    }

    .board {
        padding: 1px;
        border-width: 1px;
        gap: 1px;
        border-radius: 2px;
    }

    .tile {
        font-size: clamp(0.35rem, 1.8vw, 0.65rem);
        border-width: 1px;
    }

    .tile.active-row {
        font-size: clamp(0.75rem, 3.8vw, 1.4rem);
    }

    .stats-tile {
        font-size: clamp(0.5rem, 2.8vw, 0.8rem);
        min-height: 14px;
    }

    #keyboard-container {
        padding: 2px 2px 2px;
    }

    .key {
        height: 36px;
        min-width: 22px;
        border-radius: 3px;
    }

    .key.wide {
        min-width: 38px;
    }

    .key-label {
        font-size: 0.65rem;
    }

    .keyboard-row {
        gap: 2px;
        margin-bottom: 2px;
    }

    #hint-bar {
        padding: 1px 4px;
    }

    #hint-btn {
        padding: 3px 10px;
        font-size: 0.65rem;
    }
}

/* ===== Tall phones (narrow + tall) — maximize board space ===== */
@media (max-width: 600px) and (min-height: 600px) {
    /* On tall phones, the boards should expand to fill available space.
       The game container already uses flex:1, so ensure nothing else wastes space. */

    #game-container {
        padding: 3px;
    }

    #boards-wrapper {
        gap: 3px;
    }

    .board {
        padding: 2px;
    }
}

/* ===== Short phones / small viewport ===== */
@media (max-width: 600px) and (max-height: 600px) {
    #logo-img {
        height: 22px;
    }

    .type-btn {
        padding: 1px 6px;
        font-size: 0.55rem;
    }

    .key {
        height: 30px;
    }

    .tile.active-row {
        font-size: clamp(0.65rem, 3.2vw, 1.2rem);
    }
}

/* ===== Landscape / short screens ===== */
@media (max-height: 500px) {
    #header {
        padding: 1px 8px;
    }

    #logo-img {
        height: 22px;
    }

    #guess-counter {
        font-size: 0.6rem;
    }

    #keyboard-container {
        padding: 1px 4px 2px;
    }

    .key {
        height: 28px;
    }

    #hint-bar {
        padding: 1px 4px;
    }
}
