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

:root {
    /* Semantic colors — light mode */
    --bg: #ffffff;
    --fg: #121212;
    --bg-secondary: #f7f7f7;
    --border: #e8e8e8;
    --border-strong: #d4d4d4;
    --text-muted: #787878;
    --text-secondary: #565656;
    --cell-black: #121212;
    --cell-white: #ffffff;
    --cell-selected: #ffda00;
    --cell-highlighted: #a7d8ff;
    --cell-border: #d4d4d4;
    --correct: #6aaa64;
    --incorrect: #e74c3c;
    --revealed: #adadad;
    --btn-bg: #ffffff;
    --btn-border: #d4d4d4;
    --btn-hover: #f7f7f7;
    --modal-bg: #ffffff;
    --font-sans: 'Libre Franklin', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-serif: 'Noto Serif', Georgia, serif;
    --cell-size: 44px;
}

body.dark-mode {
    --bg: #121212;
    --fg: #e8e8e8;
    --bg-secondary: #1e1e1e;
    --border: #333333;
    --border-strong: #444444;
    --text-muted: #999999;
    --text-secondary: #bbbbbb;
    --cell-black: #000000;
    --cell-white: #2a2a2a;
    --cell-selected: #5c4a00;
    --cell-highlighted: #1a3a5c;
    --cell-border: #444444;
    --correct: #538d4e;
    --incorrect: #c0392b;
    --revealed: #666666;
    --btn-bg: #1e1e1e;
    --btn-border: #444444;
    --btn-hover: #2a2a2a;
    --modal-bg: #1e1e1e;
}

body {
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--fg);
    min-height: 100vh;
    transition: background 0.2s, color 0.2s;
}

/* Header */
header {
    border-bottom: 1px solid var(--border);
    text-align: center;
}

.header-top {
    padding: 8px 20px;
    border-bottom: 1px solid var(--border);
    font-size: 12px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.header-icons {
    display: flex;
    gap: 8px;
}

.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--fg);
    padding: 4px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.15s;
    opacity: 0.7;
}

.icon-btn:hover { opacity: 1; }
.icon-btn.active { opacity: 1; color: var(--cell-selected); }

.header-main {
    padding: 16px 20px 12px;
}

.header-main h1 {
    font-family: var(--font-serif);
    font-size: 36px;
    font-weight: 700;
    letter-spacing: -0.5px;
}

.header-subtitle {
    font-size: 14px;
    color: var(--text-muted);
    margin-top: 4px;
    font-weight: 300;
}

/* Toolbar */
.toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 20px;
    border-top: 1px solid var(--border);
    max-width: 900px;
    margin: 0 auto;
}

.toolbar-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.timer {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    transition: background 0.15s;
}

.timer:hover { background: var(--bg-secondary); }
.timer svg { color: var(--text-muted); }

.pencil-btn {
    background: none;
    border: 1px solid var(--btn-border);
    cursor: pointer;
    color: var(--text-muted);
    padding: 5px 10px;
    border-radius: 20px;
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 12px;
    font-family: var(--font-sans);
    transition: all 0.15s;
}

.pencil-btn:hover { border-color: var(--fg); color: var(--fg); }
.pencil-btn.active {
    background: var(--fg);
    color: var(--bg);
    border-color: var(--fg);
}

.toolbar-buttons {
    display: flex;
    gap: 8px;
    align-items: center;
}

.puzzle-picker {
    display: flex;
    gap: 4px;
}

/* Buttons */
.btn {
    padding: 6px 16px;
    border: 1px solid var(--btn-border);
    border-radius: 20px;
    background: var(--btn-bg);
    font-family: var(--font-sans);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
    color: var(--fg);
}

.btn:hover {
    background: var(--btn-hover);
}

.btn-primary {
    background: var(--fg);
    color: var(--bg);
    border-color: var(--fg);
}

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

/* Dropdown menus */
.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-menu {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    right: 0;
    background: var(--modal-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.12);
    min-width: 160px;
    z-index: 50;
    overflow: hidden;
}

.dropdown.open .dropdown-menu { display: block; }

.dropdown-item {
    display: block;
    width: 100%;
    padding: 10px 16px;
    border: none;
    background: none;
    text-align: left;
    font-family: var(--font-sans);
    font-size: 13px;
    color: var(--fg);
    cursor: pointer;
    transition: background 0.1s;
}

.dropdown-item:hover { background: var(--bg-secondary); }

/* Game Layout */
main {
    max-width: 1100px;
    margin: 0 auto;
    padding: 24px 20px;
}

.game-container {
    display: flex;
    gap: 40px;
    align-items: flex-start;
}

.grid-section { flex-shrink: 0; }
.clues-section { flex: 1; min-width: 0; }

/* Current Clue Bar */
.current-clue-bar {
    background: var(--bg-secondary);
    padding: 10px 14px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 15px;
    line-height: 1.4;
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.current-clue-bar .clue-number { font-weight: 700; white-space: nowrap; }
.current-clue-bar .clue-text { color: var(--text-secondary); }

/* Grid */
.grid-wrapper {
    display: inline-block;
    border: 2px solid var(--fg);
    position: relative;
    transition: filter 0.3s;
}

.grid-wrapper.paused {
    filter: blur(20px);
    pointer-events: none;
}

.pause-overlay {
    display: none;
    position: absolute;
    inset: 0;
    z-index: 20;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 12px;
    cursor: pointer;
}

.pause-overlay.visible { display: flex; }

.pause-overlay svg { color: var(--fg); opacity: 0.8; }
.pause-overlay span {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
}

#hidden-input {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    opacity: 0;
    border: none; outline: none;
    padding: 0; margin: 0;
    z-index: 10;
    font-size: 16px;
    caret-color: transparent;
}

.grid { display: grid; gap: 0; }

.cell {
    width: var(--cell-size);
    height: var(--cell-size);
    border: 1px solid var(--cell-border);
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
    background: var(--cell-white);
    transition: background 0.1s ease;
}

.cell.black { background: var(--cell-black); cursor: default; }

.cell .cell-number {
    position: absolute;
    top: 2px; left: 3px;
    font-size: 10px;
    font-weight: 600;
    line-height: 1;
    color: var(--fg);
}

.cell .cell-letter {
    font-size: 20px;
    font-weight: 500;
    text-transform: uppercase;
    color: var(--fg);
}

.cell.selected { background: var(--cell-selected); }
.cell.highlighted { background: var(--cell-highlighted); }
.cell.correct .cell-letter { color: var(--correct); }
.cell.incorrect .cell-letter { color: var(--incorrect); }
.cell.revealed .cell-letter { color: var(--revealed); }

.cell.pencil .cell-letter {
    color: var(--text-muted);
    font-size: 14px;
    font-weight: 400;
}

/* Clues */
.clues-panel {
    display: flex;
    flex-direction: column;
    gap: 24px;
    max-height: calc(var(--cell-size) * 13 + 60px);
    overflow-y: auto;
    padding-right: 8px;
}

.clues-panel::-webkit-scrollbar { width: 4px; }
.clues-panel::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 2px; }

.clues-column h3 {
    font-size: 14px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--fg);
    margin-bottom: 8px;
}

.clue-list { list-style: none; }

.clue-item {
    padding: 6px 8px;
    font-size: 14px;
    line-height: 1.4;
    cursor: pointer;
    border-radius: 4px;
    display: flex;
    gap: 8px;
    transition: background 0.1s ease;
}

.clue-item:hover { background: var(--bg-secondary); }
.clue-item.active { background: var(--cell-highlighted); }
.clue-item .clue-num { font-weight: 700; flex-shrink: 0; min-width: 20px; }

.clue-item.completed .clue-text-content {
    color: var(--text-muted);
    text-decoration: line-through;
}

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

.modal {
    background: var(--modal-bg);
    border-radius: 16px;
    padding: 40px;
    text-align: center;
    max-width: 400px;
    width: 90%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.modal h2 {
    font-family: var(--font-serif);
    font-size: 28px;
    margin-bottom: 12px;
}

.modal p {
    font-size: 16px;
    color: var(--text-secondary);
    margin-bottom: 24px;
    line-height: 1.5;
}

.modal-buttons {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}

/* Stats Modal */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin: 20px 0;
}

.stat-box {
    text-align: center;
}

.stat-value {
    font-size: 28px;
    font-weight: 700;
}

.stat-label {
    font-size: 11px;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.3px;
    margin-top: 2px;
}

/* Auth modal */
.auth-field {
    margin-bottom: 12px;
}

.auth-field input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border-strong);
    border-radius: 8px;
    font-size: 15px;
    font-family: var(--font-sans);
    background: var(--bg);
    color: var(--fg);
    outline: none;
    transition: border-color 0.15s;
}

.auth-field input:focus {
    border-color: var(--fg);
}

.auth-error {
    color: var(--incorrect);
    font-size: 13px;
    margin-bottom: 12px;
}

.auth-toggle {
    margin-top: 16px;
    font-size: 13px;
    color: var(--text-muted);
}

.auth-link {
    background: none;
    border: none;
    color: var(--fg);
    font-weight: 600;
    cursor: pointer;
    font-family: var(--font-sans);
    font-size: 13px;
    text-decoration: underline;
}

#auth-user-name {
    font-size: 16px;
    margin-bottom: 20px;
}

/* Toast */
.toast {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%) translateY(-100px);
    background: var(--fg);
    color: var(--bg);
    padding: 10px 24px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    z-index: 200;
    transition: transform 0.3s ease;
    pointer-events: none;
}

.toast.visible {
    transform: translateX(-50%) translateY(0);
}

/* Confetti canvas */
#confetti-canvas {
    position: fixed;
    inset: 0;
    z-index: 150;
    pointer-events: none;
}

/* Responsive */
@media (max-width: 800px) {
    :root { --cell-size: 36px; }

    .game-container { flex-direction: column; gap: 24px; }

    .grid-section {
        width: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .current-clue-bar { width: 100%; }

    .clues-panel {
        max-height: none;
        flex-direction: row;
        gap: 32px;
    }

    .clues-column { flex: 1; }
}

@media (max-width: 500px) {
    :root { --cell-size: 30px; }
    .cell .cell-number { font-size: 8px; }
    .cell .cell-letter { font-size: 16px; }
    .cell.pencil .cell-letter { font-size: 12px; }
    .header-main h1 { font-size: 28px; }
    .toolbar { flex-wrap: wrap; gap: 8px; }
    .toolbar-left { width: 100%; justify-content: center; }
    .toolbar-buttons { width: 100%; justify-content: center; flex-wrap: wrap; }
}
