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

:root {
    --white:      #ffffff;
    --gray-50:    #f9fafb;
    --gray-100:   #f3f4f6;
    --gray-200:   #e5e7eb;
    --gray-300:   #d1d5db;
    --gray-400:   #9ca3af;
    --gray-500:   #6b7280;
    --gray-700:   #374151;
    --gray-900:   #111827;
    --blue:       #4f46e5;
    --blue-light: #6366f1;
    --blue-bg:    #eef2ff;
    --green:      #16a34a;
    --green-bg:   #dcfce7;
    --red:        #dc2626;
    --red-bg:     #fee2e2;
    --amber:      #d97706;
    --amber-bg:   #fef3c7;
    --font:       'Inter', system-ui, sans-serif;
    --radius:     8px;
    --radius-lg:  12px;
    --shadow:     0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
    --shadow-md:  0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.05);
    --shadow-lg:  0 10px 15px rgba(0,0,0,.1), 0 4px 6px rgba(0,0,0,.05);
}

html, body {
    height: 100%;
    font-family: var(--font);
    background: var(--gray-50);
    color: var(--gray-900);
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

/* ── Utilities ───────────────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Header ──────────────────────────────────────────────────────────────── */
.header {
    position: sticky;
    top: 0;
    z-index: 50;
    background: var(--white);
    border-bottom: 1px solid var(--gray-200);
}
.header-inner {
    max-width: 960px;
    margin: 0 auto;
    padding: 0 16px;
    height: 52px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.logo {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--gray-900);
    text-decoration: none;
    letter-spacing: -0.3px;
}
.header-nav { display: flex; gap: 6px; align-items: center; }

.nav-btn {
    padding: 5px 12px;
    border: 1px solid var(--gray-200);
    border-radius: 20px;
    background: transparent;
    color: var(--gray-500);
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    font-family: var(--font);
}
.nav-btn:hover { background: var(--gray-100); color: var(--gray-700); }
.nav-btn.active {
    background: var(--gray-900);
    color: var(--white);
    border-color: var(--gray-900);
}
.nav-btn.icon-btn { padding: 5px 10px; font-size: 1rem; }

/* ── Sections ─────────────────────────────────────────────────────────────── */
.section {
    max-width: 960px;
    margin: 0 auto;
    padding: 24px 16px;
}

/* ── Topic screen ─────────────────────────────────────────────────────────── */
.topic-container {
    max-width: 560px;
    margin: 0 auto;
    padding-top: 24px;
}
.topic-header { margin-bottom: 24px; }
.topic-header h2 {
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: -0.5px;
    margin-bottom: 6px;
}
.topic-header p { color: var(--gray-500); font-size: 0.95rem; }

.input-row {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}
.topic-input {
    flex: 1;
    padding: 10px 14px;
    border: 1.5px solid var(--gray-200);
    border-radius: var(--radius);
    font-size: 0.95rem;
    font-family: var(--font);
    color: var(--gray-900);
    background: var(--white);
    outline: none;
    transition: border-color 0.15s;
}
.topic-input:focus { border-color: var(--blue); }
.topic-input::placeholder { color: var(--gray-400); }

.difficulty-row {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
}
.diff-label { font-size: 0.85rem; font-weight: 500; color: var(--gray-500); }
.diff-group { display: flex; gap: 4px; }
.diff-btn {
    padding: 5px 14px;
    border: 1.5px solid var(--gray-200);
    border-radius: 20px;
    background: var(--white);
    color: var(--gray-500);
    font-size: 0.82rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s;
    font-family: var(--font);
}
.diff-btn:hover { border-color: var(--gray-300); color: var(--gray-700); }
.diff-btn.active {
    background: var(--gray-900);
    color: var(--white);
    border-color: var(--gray-900);
}

.chip-row {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}
.chip {
    padding: 6px 14px;
    background: var(--white);
    border: 1.5px solid var(--gray-200);
    border-radius: 20px;
    font-size: 0.82rem;
    font-weight: 500;
    color: var(--gray-700);
    cursor: pointer;
    transition: all 0.15s;
    font-family: var(--font);
}
.chip:hover {
    border-color: var(--blue);
    color: var(--blue);
    background: var(--blue-bg);
}

/* ── Buttons ──────────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 9px 18px;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-weight: 600;
    font-family: var(--font);
    border: none;
    cursor: pointer;
    transition: all 0.15s;
    white-space: nowrap;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary  { background: var(--blue); color: var(--white); }
.btn-primary:hover:not(:disabled) { background: var(--blue-light); }
.btn-secondary {
    background: var(--white);
    color: var(--gray-700);
    border: 1.5px solid var(--gray-200);
}
.btn-secondary:hover:not(:disabled) { background: var(--gray-100); }
.btn-ghost {
    background: transparent;
    color: var(--gray-500);
    border: 1.5px solid transparent;
}
.btn-ghost:hover:not(:disabled) { background: var(--gray-100); color: var(--gray-700); }
.btn-hint {
    background: var(--amber-bg);
    color: var(--amber);
    border: 1.5px solid #fde68a;
}
.btn-hint:hover:not(:disabled) { background: #fde68a; }
.btn-sm { padding: 6px 12px; font-size: 0.82rem; }
.btn-full { width: 100%; margin-top: 10px; }

/* ── Loading ──────────────────────────────────────────────────────────────── */
.loading-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 40vh;
    text-align: center;
    gap: 16px;
}
.spinner {
    width: 44px; height: 44px;
    border: 3px solid var(--gray-200);
    border-top-color: var(--blue);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.loading-text {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--gray-700);
}
.loading-dots { color: var(--gray-400); }
.loading-sub {
    font-size: 0.85rem;
    color: var(--gray-400);
}

/* ── Puzzle bar ───────────────────────────────────────────────────────────── */
.puzzle-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 0 14px;
    border-bottom: 1px solid var(--gray-200);
    margin-bottom: 16px;
    gap: 12px;
}
.puzzle-info { display: flex; align-items: center; gap: 8px; min-width: 0; }
.puzzle-topic {
    font-weight: 600;
    font-size: 1rem;
    color: var(--gray-900);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.puzzle-badge {
    padding: 2px 8px;
    border-radius: 20px;
    font-size: 0.72rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    flex-shrink: 0;
}
.puzzle-badge.easy   { background: var(--green-bg);  color: var(--green); }
.puzzle-badge.medium { background: var(--blue-bg);   color: var(--blue); }
.puzzle-badge.hard   { background: var(--red-bg);    color: var(--red); }

.puzzle-controls { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.timer {
    font-size: 0.95rem;
    font-weight: 700;
    color: var(--gray-500);
    font-variant-numeric: tabular-nums;
    min-width: 38px;
    text-align: right;
}

/* ── Game layout ──────────────────────────────────────────────────────────── */
.game-layout {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 24px;
    align-items: start;
}

/* ── Grid ─────────────────────────────────────────────────────────────────── */
.grid-area { display: flex; flex-direction: column; gap: 12px; }

.crossword-grid {
    display: grid;
    gap: 1px;
    border: 2px solid #111;
    background: #555; /* gap color — gridlines visible everywhere */
    width: fit-content;
    user-select: none;
}

.cell {
    width: var(--cell-size, 40px);
    height: var(--cell-size, 40px);
    background: var(--white);
    border: none; /* gap handles gridlines */
    position: relative;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.08s;
}
.cell.blocked {
    background: #111;
    cursor: default;
    pointer-events: none;
}

.cell-num {
    position: absolute;
    top: 2px;
    left: 2px;
    font-size: var(--num-size, 10px);
    font-weight: 600;
    color: var(--gray-600, #4b5563);
    line-height: 1;
    pointer-events: none;
}
.cell-letter {
    font-size: var(--letter-size, 20px);
    font-weight: 700;
    color: var(--gray-900);
    text-transform: uppercase;
    pointer-events: none;
}

/* Cell states */
.cell.highlighted  { background: #c4dff6; } /* NYT light blue word */
.cell.active       { background: #f9da00; } /* NYT yellow selected cell */
.cell.correct      { background: #b8f0b8; }
.cell.incorrect    { background: #f5b8b8; }
.cell.revealed     { background: #fde68a; }
.cell.revealed .cell-letter { color: var(--amber); }

/* ── Action row ───────────────────────────────────────────────────────────── */
.action-row {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

/* ── Clue panels ──────────────────────────────────────────────────────────── */
.clue-panels {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    min-width: 0;
}
.clue-panel {
    min-width: 0;
}
.clue-panel-title {
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--gray-400);
    margin-bottom: 8px;
}
.clue-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 2px;
    max-height: 480px;
    overflow-y: auto;
    padding-right: 4px;
}
.clue-list::-webkit-scrollbar { width: 3px; }
.clue-list::-webkit-scrollbar-track { background: transparent; }
.clue-list::-webkit-scrollbar-thumb { background: var(--gray-200); border-radius: 2px; }

.clue-item {
    display: flex;
    align-items: baseline;
    gap: 4px;
    padding: 4px 6px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 0.82rem;
    color: var(--gray-600, #4b5563);
    line-height: 1.45;
    transition: background 0.1s;
}
.clue-item:hover { background: var(--gray-100); }
.clue-item.active {
    background: var(--blue-bg);
    color: var(--blue);
    font-weight: 500;
}
.clue-item.correct { color: var(--green); }
.clue-item.correct .clue-num { color: var(--green); }
.clue-item.incorrect { color: var(--red); }

.clue-num {
    font-weight: 700;
    color: var(--gray-400);
    flex-shrink: 0;
    min-width: 20px;
    font-size: 0.78rem;
}
.clue-text { flex: 1; }
.clue-status {
    font-weight: 700;
    flex-shrink: 0;
    font-size: 0.9rem;
    margin-left: 4px;
}

/* ── Mobile keyboard ──────────────────────────────────────────────────────── */
.mobile-keyboard {
    margin-top: 12px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 10px 6px;
    background: var(--gray-100);
    border-radius: var(--radius-lg);
}
.kb-row {
    display: flex;
    justify-content: center;
    gap: 5px;
}
.key {
    flex: 1;
    max-width: 36px;
    height: 42px;
    border: none;
    border-radius: 6px;
    background: var(--white);
    color: var(--gray-900);
    font-size: 0.9rem;
    font-weight: 600;
    font-family: var(--font);
    cursor: pointer;
    box-shadow: 0 1px 0 var(--gray-300);
    transition: background 0.1s, transform 0.05s;
    display: flex;
    align-items: center;
    justify-content: center;
}
.key:active { transform: translateY(1px); box-shadow: none; }
.key-action {
    max-width: 52px;
    background: var(--gray-200);
    color: var(--gray-700);
    font-size: 0.85rem;
}

/* ── Modals ───────────────────────────────────────────────────────────────── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 16px;
}
.modal {
    background: var(--white);
    border-radius: var(--radius-lg);
    padding: 28px 24px;
    max-width: 340px;
    width: 100%;
    box-shadow: var(--shadow-lg);
    text-align: center;
}
.modal-icon { font-size: 2.4rem; margin-bottom: 10px; }
.modal-title {
    font-size: 1.3rem;
    font-weight: 700;
    margin-bottom: 20px;
    letter-spacing: -0.3px;
}

/* Win stats */
.win-stats {
    display: flex;
    justify-content: center;
    gap: 20px;
    margin-bottom: 6px;
}
.win-stat { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.win-val { font-size: 1.6rem; font-weight: 700; color: var(--gray-900); }
.win-lbl { font-size: 0.75rem; color: var(--gray-400); text-transform: uppercase; letter-spacing: 0.5px; }

/* Stats grid */
.stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 6px;
}
.stat-box {
    background: var(--gray-50);
    border-radius: var(--radius);
    padding: 14px 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
}
.stat-val { font-size: 1.5rem; font-weight: 700; color: var(--gray-900); }
.stat-lbl { font-size: 0.72rem; color: var(--gray-400); text-transform: uppercase; letter-spacing: 0.5px; }

/* ── Toast ────────────────────────────────────────────────────────────────── */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--gray-900);
    color: var(--white);
    padding: 10px 20px;
    border-radius: 20px;
    font-size: 0.88rem;
    font-weight: 500;
    z-index: 200;
    box-shadow: var(--shadow-md);
    white-space: nowrap;
    animation: slideUp 0.2s ease;
}
.toast.error { background: var(--red); }
.toast.success { background: var(--green); }

@keyframes slideUp {
    from { transform: translateX(-50%) translateY(8px); opacity: 0; }
    to   { transform: translateX(-50%) translateY(0);   opacity: 1; }
}

/* ── Capture input ────────────────────────────────────────────────────────── */
.capture-input {
    position: fixed;
    top: -200px;
    left: -200px;
    width: 1px;
    height: 1px;
    opacity: 0;
    pointer-events: none;
}

/* ── Responsive ───────────────────────────────────────────────────────────── */
@media (max-width: 700px) {
    .game-layout {
        grid-template-columns: 1fr;
    }
    .grid-area {
        align-items: center;
    }
    .clue-panels {
        grid-template-columns: 1fr 1fr;
    }
    .clue-list {
        max-height: 200px;
    }
    .puzzle-bar {
        flex-wrap: wrap;
        gap: 8px;
    }
    .action-row {
        justify-content: center;
    }
}

@media (max-width: 420px) {
    .clue-panels {
        grid-template-columns: 1fr;
    }
    .section {
        padding: 16px 12px;
    }
    .header-inner {
        padding: 0 12px;
    }
    .chip { font-size: 0.78rem; padding: 5px 10px; }
}
