/* ==========================================================================
   Design tokens. Every color lives here; the dark theme follows the system
   setting unless overridden in settings. Text/background pairs meet WCAG AA (4.5:1) in both themes.
   ========================================================================== */
:root {
    color-scheme: light;

    --font: 'Outfit', ui-rounded, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;

    --bg: #f6f5fb;
    --surface: #ffffff;
    --surface-2: #f0eef8;
    --text: #1c1a29;
    --text-muted: #625f78;
    --border: #e3e0ee;
    --border-strong: #cfcae3;
    --shadow-sm: 0 1px 2px rgba(28, 26, 41, 0.06), 0 1px 3px rgba(28, 26, 41, 0.05);
    --shadow-md: 0 6px 24px rgba(28, 26, 41, 0.08), 0 2px 6px rgba(28, 26, 41, 0.05);
    --shadow-lg: 0 24px 60px rgba(28, 26, 41, 0.22);

    --accent: #6236e8;           /* text, rings, highlights */
    --accent-soft: #eee8ff;
    --accent-line: #f6f3ff;      /* the selected cell's row and column */
    --primary-bg: #6236e8;       /* filled buttons */
    --primary-bg-hover: #5227d4;
    --on-primary: #ffffff;
    --button-disabled-bg: #e3e0ee;
    --button-disabled-text: #625f78;

    --cell-bg: #ffffff;
    --cell-text: #1c1a29;
    --preset-bg: #ebe6fb;
    --preset-text: #4b2bb0;
    --correct-bg: #d9f4e1;
    --correct-text: #11612f;
    --incorrect-bg: #fde3e1;
    --incorrect-text: #a8231b;
    --hint-bg: #fff0bd;
    --hint-text: #6e4a00;
    --brand-green: #11612f;      /* logo squares; fixed, unlike the cell colors */
    --brand-amber: #6e4a00;
    --revealed-bg: #ffe5cf;
    --revealed-text: #8f3700;

    --focus-ring: #6236e8;
    --error-bg: #c62828;
    --error-text: #ffffff;

    --popover-bg: #ffffff;
    --chip-bg: #f0eef8;
    --chip-text: #4b2bb0;

    --toast-bg: #1c1a29;
    --toast-text: #ffffff;
    --backdrop: rgba(18, 16, 30, 0.55);

    --radius-sm: 10px;
    --radius: 14px;
    --radius-lg: 22px;
}

/* Dark theme: follows the system unless the player picked Light or Dark in
   settings (data-theme on <html>). The block is repeated for the manual choice. */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;

        --bg: #0f0e17;
        --surface: #1a1926;
        --surface-2: #24222f;
        --text: #ecebf5;
        --text-muted: #a5a2b9;
        --border: #2e2b3e;
        --border-strong: #423e57;
        --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
        --shadow-md: 0 8px 28px rgba(0, 0, 0, 0.45);
        --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.6);

        --accent: #a995ff;
        --accent-soft: #2b2547;
        --accent-line: #201e30;
        --primary-bg: #6a48ee;
        --primary-bg-hover: #7a5cf5;
        --on-primary: #ffffff;
        --button-disabled-bg: #2e2b3e;
        --button-disabled-text: #a5a2b9;

        --cell-bg: #1f1d2c;
        --cell-text: #ecebf5;
        --preset-bg: #2b2547;
        --preset-text: #cfc4ff;
        --correct-bg: #133823;
        --correct-text: #8fe3aa;
        --incorrect-bg: #461b1c;
        --incorrect-text: #ffaaa4;
        --hint-bg: #43360a;
        --hint-text: #ffdf7a;
        --brand-green: #8fe3aa;
        --brand-amber: #ffdf7a;
        --revealed-bg: #46290c;
        --revealed-text: #ffbf86;

        --focus-ring: #a995ff;

        --popover-bg: #24222f;
        --chip-bg: #2e2b3e;
        --chip-text: #cfc4ff;

        --toast-bg: #ecebf5;
        --toast-text: #0f0e17;
        --backdrop: rgba(0, 0, 0, 0.7);
    }
}

:root[data-theme="dark"] {
    color-scheme: dark;

    --bg: #0f0e17;
    --surface: #1a1926;
    --surface-2: #24222f;
    --text: #ecebf5;
    --text-muted: #a5a2b9;
    --border: #2e2b3e;
    --border-strong: #423e57;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 8px 28px rgba(0, 0, 0, 0.45);
    --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.6);

    --accent: #a995ff;
    --accent-soft: #2b2547;
    --accent-line: #201e30;
    --primary-bg: #6a48ee;
    --primary-bg-hover: #7a5cf5;
    --on-primary: #ffffff;
    --button-disabled-bg: #2e2b3e;
    --button-disabled-text: #a5a2b9;

    --cell-bg: #1f1d2c;
    --cell-text: #ecebf5;
    --preset-bg: #2b2547;
    --preset-text: #cfc4ff;
    --correct-bg: #133823;
    --correct-text: #8fe3aa;
    --incorrect-bg: #461b1c;
    --incorrect-text: #ffaaa4;
    --hint-bg: #43360a;
    --hint-text: #ffdf7a;
    --brand-green: #8fe3aa;
    --brand-amber: #ffdf7a;
    --revealed-bg: #46290c;
    --revealed-text: #ffbf86;

    --focus-ring: #a995ff;

    --popover-bg: #24222f;
    --chip-bg: #2e2b3e;
    --chip-text: #cfc4ff;

    --toast-bg: #ecebf5;
    --toast-text: #0f0e17;
    --backdrop: rgba(0, 0, 0, 0.7);
}

:root[data-theme="light"] {
    color-scheme: light;
}

/* High contrast (settings): blue for correct, orange for close, gray for
   wrong, so no state depends on telling red from green. */
:root[data-contrast="high"] {
    --correct-bg: #1d5fc4;
    --correct-text: #ffffff;
    --incorrect-bg: #e2e1e8;
    --incorrect-text: #3a3946;
    --hint-bg: #f59b23;
    --hint-text: #241400;
    --revealed-bg: #fff3d6;
    --revealed-text: #6b3d00;
}

@media (prefers-color-scheme: dark) {
    :root[data-contrast="high"]:not([data-theme="light"]) {
        --correct-bg: #2c66cc;
        --correct-text: #ffffff;
        --incorrect-bg: #35343f;
        --incorrect-text: #dcdbe6;
        --hint-bg: #f5a33b;
        --hint-text: #1d1000;
        --revealed-bg: #3d2a0c;
        --revealed-text: #ffd08a;
    }
}

:root[data-contrast="high"][data-theme="dark"] {
    --correct-bg: #2c66cc;
    --correct-text: #ffffff;
    --incorrect-bg: #35343f;
    --incorrect-text: #dcdbe6;
    --hint-bg: #f5a33b;
    --hint-text: #1d1000;
    --revealed-bg: #3d2a0c;
    --revealed-text: #ffd08a;
}

/* Hide timer (settings): the clock still runs for stats. */
:root[data-hide-timer] #timer {
    display: none;
}

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

[hidden] {
    display: none !important;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    margin: 0;
    /* The game fills exactly one screen: the layout below stretches to the
       bottom and the grid takes whatever height is left over. Uses the
       small viewport height (svh) so the grid keeps its size when the
       mobile browser bar shows or hides. */
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100svh;
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.45;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}

button {
    font: inherit;
    color: inherit;
}

svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
    flex: none;
}

svg .fill {
    fill: currentColor;
    stroke: none;
}

:focus-visible {
    outline: 3px solid var(--focus-ring);
    outline-offset: 2px;
}

.muted {
    color: var(--text-muted);
}

/* ==========================================================================
   App bar
   ========================================================================== */
.app-bar {
    position: sticky;
    top: 0;
    z-index: 20;
    padding-top: env(safe-area-inset-top);
    background: color-mix(in srgb, var(--bg) 85%, transparent);
    -webkit-backdrop-filter: saturate(1.4) blur(12px);
    backdrop-filter: saturate(1.4) blur(12px);
    border-bottom: 1px solid var(--border);
}

.app-bar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    max-width: 1040px;
    height: 56px;
    margin: 0 auto;
    padding: 0 max(12px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0;
    font-size: 1.35rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.brand-mark {
    display: grid;
    grid-template-columns: repeat(2, 9px);
    gap: 2px;
}

.brand-mark i {
    width: 9px;
    height: 9px;
    border-radius: 3px;
    background: var(--accent);
}

.brand-mark i:nth-child(2) { background: var(--brand-green); }
.brand-mark i:nth-child(3) { background: var(--brand-amber); opacity: 0.85; }
.brand-mark i:nth-child(4) { opacity: 0.45; }

.app-bar-actions {
    display: flex;
    gap: 4px;
}

.icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: none;
    border-radius: 12px;
    background: transparent;
    color: var(--text);
    cursor: pointer;
    transition: background 0.15s;
}

@media (hover: hover) {
    .icon-button:hover {
        background: var(--surface-2);
    }
}

.icon-button:active {
    background: var(--border);
}

/* ==========================================================================
   Layout
   ========================================================================== */
.layout {
    box-sizing: border-box;
    flex: 1 1 0;
    display: flex;
    width: 100%;
    max-width: 1040px;
    margin: 0 auto;
    padding: 12px max(16px, env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.play {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    width: 100%;
    max-width: 440px;
    margin: 0 auto;
}

/* Phones and tablets: the full clue list opens as a sheet from the app bar
   instead of sitting below the game, so nothing needs scrolling. */
.clues-panel {
    display: none;
}

@media (min-width: 860px) {
    .layout {
        display: grid;
        grid-template-columns: minmax(0, 460px) minmax(0, 1fr);
        align-items: stretch;
        gap: 32px;
        padding-top: 24px;
        padding-bottom: 24px;
    }

    .play {
        margin: 0;
    }

    .clues-panel {
        display: block;
        align-self: start;
        box-sizing: border-box;
        /* Never taller than the screen: scrolls inside itself instead */
        max-height: calc(100svh - 56px - 48px - env(safe-area-inset-top));
        overflow: auto;
    }

    #clues-button {
        display: none;
    }
}

/* ==========================================================================
   Status bar: puzzle, guesses left, timer
   ========================================================================== */
.status-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 32px;
}

.puzzle-info {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    font-size: 0.95rem;
    white-space: nowrap;
}

.difficulty {
    padding: 2px 9px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    background: var(--accent-soft);
    color: var(--accent);
}

.status-right {
    display: flex;
    align-items: center;
    gap: 14px;
}

.guesses {
    display: flex;
    align-items: center;
    gap: 5px;
}

.guess-pip {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--accent);
    transition: background 0.3s, transform 0.3s;
}

.guess-pip.used {
    background: var(--border-strong);
    transform: scale(0.8);
}

.timer {
    min-width: 3.2em;
    padding: 3px 10px;
    border-radius: 999px;
    background: var(--surface-2);
    font-size: 0.9rem;
    font-weight: 600;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

/* ==========================================================================
   Clues for the selected cell
   ========================================================================== */
.active-clues {
    /* A fixed height that fits both clues at two lines each, so the grid
       below never moves when a different cell (or no cell) is selected. */
    --clue-size: 0.95rem;
    --clue-pad: 5px;
    --strip-pad: 6px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    flex: none;
    height: calc(4 * 1.3 * var(--clue-size) + 4 * var(--clue-pad) + 2 * var(--strip-pad) + 1px);
    overflow: hidden;
    padding: var(--strip-pad) 14px;
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow-sm);
}

.active-clue {
    display: flex;
    align-items: baseline;
    gap: 10px;
    padding: var(--clue-pad) 0;
    font-size: var(--clue-size);
    line-height: 1.3;
}

.active-clue-text {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden;
}

/* After the game ends: keep the space so the grid doesn't jump */
.active-clues.concealed {
    visibility: hidden;
}

.active-clue + .active-clue {
    border-top: 1px solid var(--border);
}

.active-clue-empty {
    text-align: center;
    color: var(--text-muted);
}

.active-clue-label {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 3px;
    flex: none;
    min-width: 42px;
    padding: 1px 6px;
    border-radius: 6px;
    background: var(--accent-soft);
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--accent);
}

.active-clue-label svg {
    width: 13px;
    height: 13px;
    stroke-width: 2.5;
}

/* ==========================================================================
   Grid
   ========================================================================== */
.board {
    /* Takes the height the rest of the game leaves free; the grid sizes
       itself to fit inside (container query units below). */
    flex: 1 1 0;
    min-height: calc(22px + 4 * 8px + 4 * 40px);
    display: flex;
    align-items: center;
    justify-content: center;
    container-type: size;
}

.grid-cells {
    /* As big as the width allows, but small enough on short phones that
       the grid, number pad and submit button fit on one screen. Uses the
       small viewport height (svh) so the grid keeps its size when the
       mobile browser bar shows or hides. */
    --chrome: 476px;
    --cell: clamp(48px, min(calc((100vw - 86px) / 4), calc((100vh - var(--chrome)) / 4)), 92px);
    --cell: clamp(48px, min(calc((100vw - 86px) / 4), calc((100svh - var(--chrome)) / 4)), 92px);
    /* Where container queries work: as big as the free space allows */
    --cell: clamp(40px, min(calc((100cqw - 22px - 4 * var(--grid-gap)) / 4), calc((100cqh - 22px - 4 * var(--grid-gap)) / 4)), 92px);
    --grid-gap: 8px;
    display: grid;
    grid-template-columns: 22px repeat(4, var(--cell));
    grid-template-rows: 22px repeat(4, var(--cell));
    gap: var(--grid-gap);
}

.row-label,
.column-label {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    border-radius: 6px;
    transition: color 0.15s, background 0.15s;
}

.row-label.label-active,
.column-label.label-active {
    color: var(--accent);
    background: var(--accent-soft);
}

.grid-cell {
    position: relative;
    border-radius: var(--radius);
    background: var(--cell-bg);
    color: var(--cell-text);
    box-shadow: inset 0 0 0 1.5px var(--border-strong);
    transition: background 0.2s, box-shadow 0.15s, transform 0.15s;
}

.grid-cell.in-line {
    background: var(--accent-line);
}

/* Rings drawn above the cell's colored content (selection, edited
   guesses, repeats); a box-shadow on the cell itself would sit underneath. */
.grid-cell::before {
    content: "";
    position: absolute;
    inset: 0;
    border-radius: inherit;
    pointer-events: none;
    z-index: 1;
    transition: box-shadow 0.15s;
}

/* The selected cell: a solid accent ring, a halo and a slight lift so it
   stands out from every other cell, colored or not. */
.grid-cell:focus-within {
    transform: scale(1.05);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent), var(--shadow-md);
    z-index: 2;
}
.grid-cell:focus-within::before {
    box-shadow: inset 0 0 0 3.5px var(--accent);
}

.duplicate-message {
    position: absolute;
    top: calc(100% + 4px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--error-bg);
    color: var(--error-text);
    padding: 3px 8px;
    font-size: 0.75rem;
    font-weight: 600;
    border-radius: 6px;
    white-space: nowrap;
    z-index: 5;
    pointer-events: none;
}

.grid-cell-content {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--cell) * 0.46);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    border-radius: inherit;
}

.grid-cell input {
    width: 100%;
    height: 100%;
    padding: 0;
    font: inherit;
    color: inherit;
    border: none;
    background: transparent;
    text-align: center;
    outline: none; /* the cell's :focus-within ring replaces it */
    caret-color: transparent;
    cursor: pointer;
    border-radius: inherit;
}

.grid-cell input::selection {
    background: transparent;
}

.grid-cell input:disabled {
    opacity: 1;
    -webkit-text-fill-color: currentColor;
    cursor: default;
}

.grid-cell.preset,
.grid-cell.correct,
.grid-cell.incorrect,
.grid-cell.hint,
.grid-cell.revealed {
    box-shadow: none;
}

.grid-cell.preset .grid-cell-content { background: var(--preset-bg); color: var(--preset-text); }
.grid-cell.correct .grid-cell-content { background: var(--correct-bg); color: var(--correct-text); }
.grid-cell.incorrect .grid-cell-content { background: var(--incorrect-bg); color: var(--incorrect-text); }
.grid-cell.hint .grid-cell-content { background: var(--hint-bg); color: var(--hint-text); }
.grid-cell.revealed .grid-cell-content { background: var(--revealed-bg); color: var(--revealed-text); }

/* A wrong or close cell whose number has been changed since the last
   guess: the fill clears so it reads as a fresh entry, and a ring plus a
   corner tag ("≈ 4") in the old color keep the earlier feedback in view. */
.grid-cell.hint.edited { --was-bg: var(--hint-bg); --was-text: var(--hint-text); --was-symbol: "≈"; }
.grid-cell.incorrect.edited { --was-bg: var(--incorrect-bg); --was-text: var(--incorrect-text); --was-symbol: "✗"; }

.grid-cell.edited .grid-cell-content {
    background: var(--cell-bg);
    color: var(--cell-text);
}
.grid-cell.edited::before {
    box-shadow: inset 0 0 0 4px color-mix(in srgb, var(--was-text) 40%, var(--was-bg));
}
.grid-cell.edited:focus-within::before {
    box-shadow: inset 0 0 0 3.5px var(--accent), inset 0 0 0 7.5px color-mix(in srgb, var(--was-text) 40%, var(--was-bg));
}

/* Repeated guess */
.grid-cell.duplicate::before,
.grid-cell.duplicate:focus-within::before {
    box-shadow: inset 0 0 0 3.5px var(--error-bg);
}

/* Status symbols, so feedback doesn't rely on color alone.
   The "/ ''" gives the symbol empty alt text; the cell label says it. */
.grid-cell.correct::after,
.grid-cell.incorrect::after,
.grid-cell.hint::after {
    position: absolute;
    top: 5px;
    right: 7px;
    font-size: 0.8rem;
    font-weight: 700;
    line-height: 1;
    pointer-events: none;
    z-index: 1;
}
.grid-cell.correct::after { content: "✓"; content: "✓" / ""; color: var(--correct-text); }
.grid-cell.incorrect::after { content: "✗"; content: "✗" / ""; color: var(--incorrect-text); }
.grid-cell.hint::after { content: "≈"; content: "≈" / ""; color: var(--hint-text); }
.grid-cell.edited::after {
    content: var(--was-symbol) " " attr(data-last-guess);
    content: var(--was-symbol) " " attr(data-last-guess) / "";
    top: 6px;
    right: 6px;
    padding: 2px 5px;
    border-radius: 6px;
    font-size: 0.7rem;
    background: var(--was-bg);
    color: var(--was-text);
    z-index: 2;
}

/* A guess lands: cells flip in, one after another */
.grid-cell.reveal-anim .grid-cell-content {
    animation: cell-flip 0.45s cubic-bezier(.3,.7,.4,1) both;
    animation-delay: var(--delay, 0s);
}

@keyframes cell-flip {
    0% { transform: rotateX(90deg); }
    100% { transform: rotateX(0); }
}

/* Guess-history popover (pointer devices; touch uses the number pad) */
.guess-history-popover {
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--popover-bg);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
    padding: 8px 10px;
    z-index: 10;
    min-width: 64px;
    font-size: 0.9rem;
    text-align: center;
    pointer-events: none;
    white-space: nowrap;
}

.chips-two-column {
    display: flex;
    gap: 6px;
}

.guess-chip {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.9em;
    height: 1.9em;
    border-radius: 8px;
    background: var(--chip-bg);
    color: var(--chip-text);
    font-weight: 700;
}

/* Shape cues alongside color: close = dashed ring, wrong = struck through. */
.chip-correct { background: var(--correct-bg); color: var(--correct-text); box-shadow: inset 0 0 0 2px var(--correct-text); }
.chip-hint { background: var(--hint-bg); color: var(--hint-text); outline: 2px dashed var(--hint-text); outline-offset: -2px; }
.chip-incorrect { background: var(--incorrect-bg); color: var(--incorrect-text); text-decoration: line-through; }

.guess-history-empty {
    color: var(--text-muted);
}

/* Shake for refused submits */
.shake {
    animation: shake 0.5s cubic-bezier(.36,.07,.19,.97) both;
}

@keyframes shake {
    10%, 90% { transform: translateX(-2px); }
    20%, 80% { transform: translateX(4px); }
    30%, 50%, 70% { transform: translateX(-6px); }
    40%, 60% { transform: translateX(6px); }
    100% { transform: translateX(0); }
}

/* ==========================================================================
   Number pad
   ========================================================================== */
.keypad {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    margin-top: 4px;
}

.key {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 54px;
    padding: 0;
    border: none;
    border-radius: var(--radius-sm);
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-sm), inset 0 -2px 0 var(--border);
    font-size: 1.4rem;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    cursor: pointer;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
    transition: background 0.12s, transform 0.08s;
}

@media (hover: hover) {
    .key:hover {
        background: var(--surface-2);
    }
}

.key:active {
    transform: translateY(1px) scale(0.97);
    box-shadow: var(--shadow-sm);
}

.key-erase {
    color: var(--text-muted);
}

/* Already tried in the selected cell */
.key.key-incorrect {
    background: var(--incorrect-bg);
    color: var(--incorrect-text);
    box-shadow: none;
    text-decoration: line-through;
    text-decoration-thickness: 2px;
    opacity: 0.8;
}

.key.key-hint {
    background: var(--hint-bg);
    color: var(--hint-text);
    box-shadow: inset 0 0 0 2px var(--hint-text);
    opacity: 0.85;
}

.key.key-hint::after {
    content: "≈";
    content: "≈" / "";
    position: absolute;
    top: 3px;
    right: 6px;
    font-size: 0.7rem;
    font-weight: 700;
}

.keypad.inactive .key {
    opacity: 0.45;
    cursor: default;
}

/* ==========================================================================
   Buttons, result and countdown
   ========================================================================== */
.controls {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    margin-top: 4px;
}

.button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 52px;
    padding: 12px 22px;
    border: none;
    border-radius: var(--radius);
    background: var(--surface-2);
    color: var(--text);
    font-size: 1.05rem;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
    transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
}

.button.primary {
    background: var(--primary-bg);
    color: var(--on-primary);
    box-shadow: 0 6px 18px color-mix(in srgb, var(--primary-bg) 32%, transparent);
}

@media (hover: hover) {
    .button.primary:hover {
        background: var(--primary-bg-hover);
    }
}

.button:active {
    transform: scale(0.985);
}

.button:disabled,
.button.primary:disabled {
    background: var(--button-disabled-bg);
    color: var(--button-disabled-text);
    box-shadow: none;
    cursor: not-allowed;
    transform: none;
}

.button svg {
    width: 20px;
    height: 20px;
}

.button.glow {
    animation: glow 1.6s ease-in-out 3;
}

@keyframes glow {
    50% { box-shadow: 0 0 0 6px var(--accent-soft), 0 6px 18px color-mix(in srgb, var(--primary-bg) 40%, transparent); }
}

.result {
    min-height: 1.45em;
    margin: 4px 0 0;
    font-size: 0.98rem;
    text-align: center;
    color: var(--text-muted);
}

.result.result-end {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    margin-top: 10px;
    padding: 16px;
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow-sm);
    color: var(--text);
    font-weight: 600;
    font-size: 1.05rem;
}

.countdown {
    margin: 4px 0 0;
    text-align: center;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.countdown strong {
    color: var(--text);
}

.countdown .button {
    width: 100%;
}

/* ==========================================================================
   All clues
   ========================================================================== */
.clues-panel {
    width: 100%;
    max-width: 440px;
    margin: 0 auto;
    padding: 18px;
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow-sm);
}

@media (min-width: 860px) {
    .clues-panel {
        max-width: none;
        padding: 24px;
    }
}

.panel-title {
    margin: 0 0 12px;
    font-size: 1.1rem;
    letter-spacing: -0.01em;
}

.clue-groups {
    display: grid;
    gap: 18px;
}

.clue-group h3 {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin: 0 0 6px;
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
}

.clue-group h3 span {
    font-weight: 400;
    text-transform: none;
    letter-spacing: 0;
}

.clue-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    gap: 4px;
}

.clue-list li {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 10px;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    line-height: 1.3;
    transition: background 0.15s;
}

.clue-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: var(--surface-2);
    color: var(--text-muted);
    font-size: 0.85rem;
    font-weight: 700;
    transition: background 0.15s, color 0.15s;
}

/* The row and column clues for the cell in focus (or under the pointer) */
.clue-list li.clue-active {
    background: var(--accent-soft);
    font-weight: 600;
}

.clue-list li.clue-active .clue-badge {
    background: var(--primary-bg);
    color: var(--on-primary);
}

/* ==========================================================================
   Dialogs: a bottom sheet on phones, a centered card on larger screens
   ========================================================================== */
.sheet {
    width: min(520px, 100vw);
    max-width: 100vw;
    max-height: min(88vh, 760px);
    margin: auto auto 0;
    padding: 0;
    border: none;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-lg);
    overflow: hidden auto;
    overscroll-behavior: contain;
}

.sheet[open] {
    animation: sheet-up 0.28s cubic-bezier(.2,.8,.3,1);
}

.sheet::backdrop {
    background: var(--backdrop);
    animation: fade-in 0.2s ease;
}

@keyframes sheet-up {
    from { transform: translateY(40px); opacity: 0; }
    to { transform: none; opacity: 1; }
}

@keyframes fade-in {
    from { opacity: 0; }
}

@media (min-width: 600px) {
    .sheet {
        margin: auto;
        border-radius: var(--radius-lg);
        width: min(520px, calc(100vw - 48px));
    }
}

.sheet-header {
    position: sticky;
    top: 0;
    z-index: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 12px 6px 22px;
    background: var(--surface);
}

.sheet-header::before {
    content: "";
    position: absolute;
    top: 7px;
    left: 50%;
    width: 36px;
    height: 4px;
    margin-left: -18px;
    border-radius: 2px;
    background: var(--border-strong);
}

@media (min-width: 600px) {
    .sheet-header::before { display: none; }
}

.sheet-header h2 {
    margin: 0;
    outline: none;
    font-size: 1.3rem;
    letter-spacing: -0.01em;
}

.sheet-body {
    padding: 6px 22px calc(24px + env(safe-area-inset-bottom));
}

.sheet-body h3 {
    margin: 18px 0 8px;
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
}

.rule-steps {
    margin: 4px 0 0;
    padding: 0;
    list-style: none;
    counter-reset: step;
    display: grid;
    gap: 12px;
}

.rule-steps li {
    position: relative;
    padding-left: 40px;
    counter-increment: step;
}

.rule-steps li::before {
    content: counter(step);
    position: absolute;
    left: 0;
    top: -1px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: var(--accent-soft);
    color: var(--accent);
    font-weight: 700;
    font-size: 0.9rem;
}

.legend {
    margin: 0;
    padding: 0;
    list-style: none;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

.legend li {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.92rem;
    line-height: 1.25;
}

.swatch {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 36px;
    height: 36px;
    border-radius: 10px;
    font-weight: 700;
}

.swatch-correct { background: var(--correct-bg); color: var(--correct-text); }
.swatch-hint { background: var(--hint-bg); color: var(--hint-text); }
.swatch-incorrect { background: var(--incorrect-bg); color: var(--incorrect-text); }
.swatch-preset { background: var(--preset-bg); color: var(--preset-text); }

.rules .muted {
    margin: 18px 0;
    font-size: 0.92rem;
}

.rules .button {
    width: 100%;
}

/* Stats */
/* Seven tiles: four streak/result tiles on top, three averages below. */
.stats-summary {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    gap: 8px;
    margin: 6px 0 12px;
}

.stat-tile {
    grid-column: span 4;
    display: flex;
    flex-direction: column-reverse;
    align-items: center;
    justify-content: center;
    padding: 12px 6px;
    border-radius: var(--radius);
    background: var(--surface-2);
    text-align: center;
}

.stat-tile:nth-child(-n+4) {
    grid-column: span 3;
}

.stat-tile dt {
    font-size: 0.75rem;
    color: var(--text-muted);
    line-height: 1.2;
}

.stat-tile dd {
    margin: 0 0 2px;
    font-size: 1.5rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.02em;
}

.stats-share {
    width: 100%;
    margin-bottom: 14px;
}

/* Settings */
.settings .setting {
    margin: 0;
    padding: 14px 0;
    border: none;
    border-bottom: 1px solid var(--border);
}

.settings .setting:last-child {
    border-bottom: none;
}

.setting-name {
    display: block;
    padding: 0;
    font-weight: 600;
}

.setting-help {
    display: block;
    margin-top: 2px;
    font-size: 0.88rem;
    color: var(--text-muted);
}

.segmented {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 4px;
    margin-top: 10px;
    padding: 4px;
    border-radius: var(--radius);
    background: var(--surface-2);
}

.segmented label {
    position: relative;
}

.segmented input {
    position: absolute;
    opacity: 0;
    inset: 0;
    margin: 0;
    cursor: pointer;
}

.segmented span {
    display: block;
    padding: 9px 4px;
    border-radius: var(--radius-sm);
    text-align: center;
    font-weight: 600;
    color: var(--text-muted);
}

.segmented input:checked + span {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-sm);
}

.segmented input:focus-visible + span {
    outline: 2px solid var(--focus-ring);
    outline-offset: 1px;
}

.setting-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    cursor: pointer;
}

/* A switch drawn on the checkbox itself */
.setting-toggle input {
    flex: none;
    appearance: none;
    position: relative;
    width: 46px;
    height: 28px;
    margin: 0;
    border-radius: 999px;
    background: var(--border-strong);
    cursor: pointer;
    transition: background 0.15s;
}

.setting-toggle input::before {
    content: "";
    position: absolute;
    top: 3px;
    left: 3px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #ffffff;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
    transition: transform 0.15s;
}

.setting-toggle input:checked {
    background: var(--primary-bg);
}

.setting-toggle input:checked::before {
    transform: translateX(18px);
}

.setting-toggle input:focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    .setting-toggle input,
    .setting-toggle input::before {
        transition: none;
    }
}

/* Copy-by-hand fallback */
.copy-hint {
    margin: 4px 0 12px;
    font-size: 0.92rem;
}

.copy-text {
    display: block;
    width: 100%;
    box-sizing: border-box;
    padding: 12px;
    border: 1.5px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
    font-size: 0.95rem;
    line-height: 1.35;
    resize: none;
}

.copy-again {
    width: 100%;
    margin-top: 12px;
}

/* Share sheet: the picture's preview, a size switch and ways to share it */
.share-formats {
    display: flex;
    gap: 4px;
    padding: 4px;
    margin: 4px 0 14px;
    border-radius: var(--radius);
    background: var(--surface-2);
}

.share-format {
    flex: 1;
    min-height: 40px;
    border: none;
    border-radius: var(--radius-sm);
    background: transparent;
    color: var(--text-muted);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
}

.share-format span {
    margin-left: 4px;
    font-weight: 500;
    opacity: 0.75;
}

.share-format[aria-pressed="true"] {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow-sm);
}

.share-format:focus-visible {
    outline: 2px solid var(--focus-ring);
    outline-offset: 1px;
}

.share-frame {
    position: relative;
    height: min(40vh, 460px);
    max-width: 100%;
    aspect-ratio: 9 / 16;
    margin: 0 auto;
    border-radius: var(--radius);
    overflow: hidden;
    background: #0b0a12;
    box-shadow: var(--shadow-md);
}

.share-frame[data-format="post"] {
    height: min(33vh, 380px);
    aspect-ratio: 4 / 5;
}

.share-frame img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    transition: opacity 0.2s;
}

.share-frame.loading img {
    opacity: 0.35;
}

.share-frame.loading::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 32px;
    height: 32px;
    margin: -16px 0 0 -16px;
    border: 3px solid rgba(255, 255, 255, 0.25);
    border-top-color: #ffffff;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

@media (prefers-reduced-motion: reduce) {
    .share-frame.loading::after { animation-duration: 2.4s; }
}

.share-tip {
    margin: 14px 0;
    font-size: 0.9rem;
    text-align: center;
}

.share-main {
    width: 100%;
}

.share-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
}

.share-actions .button {
    flex: 1;
    min-width: 0;
    padding: 12px 10px;
    font-size: 0.98rem;
    white-space: nowrap;
}

/* "Save image" leads, on its own row, where pictures can't be shared */
.share-actions .button.primary {
    flex-basis: 100%;
    order: -1;
}

.share-main + .share-actions {
    margin-top: 8px;
}

.stats-chart-status {
    color: var(--text-muted);
    text-align: center;
}

.stats-chart-status:empty {
    display: none;
}

.stats-chart {
    margin-bottom: 8px;
}

.stats-chart-pie {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 8px;
}

.chart-title {
    margin-top: 16px;
}

/* Guess distribution */
.distribution {
    display: grid;
    gap: 5px;
    margin: 0 0 12px;
    padding: 0;
    list-style: none;
}

.distribution-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.distribution-label {
    width: 1.2em;
    font-weight: 600;
    text-align: center;
    color: var(--text-muted);
}

.distribution-bar {
    box-sizing: border-box;
    min-width: 2em;
    width: var(--share);
    padding: 3px 8px;
    border-radius: 6px;
    background: var(--surface-2);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    text-align: right;
    transition: width 0.4s ease;
}

.distribution-row.today .distribution-bar {
    background: var(--primary-bg);
    color: var(--on-primary);
}

.distribution-row.today .distribution-label {
    color: var(--text);
}

/* Moving stats between devices */
.stats-backup {
    margin-top: 18px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
}

.stats-backup summary {
    cursor: pointer;
    font-weight: 600;
    padding: 6px 0;
}

.stats-backup .muted {
    font-size: 0.92rem;
}

.stats-backup label {
    display: block;
    margin: 14px 0 6px;
    font-weight: 600;
    font-size: 0.92rem;
}

.stats-backup .button {
    width: 100%;
}

.stats-backup textarea {
    margin-bottom: 10px;
    word-break: break-all;
}

.stats-import-status {
    margin: 8px 0 0;
    font-size: 0.92rem;
    text-align: center;
}

.stats-import-status:empty {
    display: none;
}

/* ==========================================================================
   End-of-game effects
   ========================================================================== */
/* Result card: slides up, and its badge draws itself in. */
.result.result-enter {
    animation: result-rise 0.55s cubic-bezier(.2,.9,.3,1.15) both;
}

@keyframes result-rise {
    0% { opacity: 0; transform: translateY(14px) scale(0.96); }
    100% { opacity: 1; transform: none; }
}

.result-badge {
    width: 60px;
    height: 60px;
    overflow: visible;
    margin-bottom: 4px;
}

.result-badge .badge-disc { fill: var(--badge-bg); }
.result-badge .badge-ring,
.result-badge .badge-mark,
.result-badge .badge-rays line {
    fill: none;
    stroke: var(--badge-fg);
    stroke-linecap: round;
    stroke-linejoin: round;
}
.result-badge .badge-ring { stroke-width: 3; }
.result-badge .badge-mark { stroke-width: 4.5; stroke-dasharray: 1; stroke-dashoffset: 0; }
.result-badge .badge-rays line { stroke-width: 3; opacity: 0; }

.result-badge-win { --badge-bg: var(--correct-bg); --badge-fg: var(--correct-text); }
.result-badge-lose { --badge-bg: var(--incorrect-bg); --badge-fg: var(--incorrect-text); }

.result-badge-animate .badge-disc {
    transform-origin: 32px 32px;
    animation: badge-disc 0.5s cubic-bezier(.3,1.6,.5,1) 0.15s both;
}
.result-badge-animate .badge-ring {
    stroke-dasharray: 1;
    transform-origin: 32px 32px;
    transform: rotate(-90deg);
    animation: badge-draw 0.5s cubic-bezier(.6,0,.3,1) both;
}
.result-badge-animate .badge-mark {
    animation: badge-draw 0.35s cubic-bezier(.6,0,.4,1) 0.5s both;
}
.result-badge-animate .badge-mark-2 { animation-delay: 0.7s; }
.result-badge-animate .badge-rays {
    transform-origin: 32px 32px;
    animation: badge-rays 0.7s cubic-bezier(.2,.8,.3,1) 0.75s both;
}
.result-badge-animate .badge-rays line {
    animation: badge-rays-fade 0.7s ease-out 0.75s both;
}
.result-badge-lose.result-badge-animate {
    animation: badge-thud 0.5s cubic-bezier(.36,.07,.19,.97) 0.95s both;
}

@keyframes badge-disc {
    0% { transform: scale(0); }
    100% { transform: scale(1); }
}
@keyframes badge-draw {
    0% { stroke-dashoffset: 1; }
    100% { stroke-dashoffset: 0; }
}
@keyframes badge-rays {
    0% { transform: scale(0.7); }
    100% { transform: scale(1.35); }
}
@keyframes badge-rays-fade {
    0%, 100% { opacity: 0; }
    25% { opacity: 1; }
}
@keyframes badge-thud {
    0%, 100% { transform: none; }
    20% { transform: translateX(-5px) rotate(-6deg); }
    45% { transform: translateX(4px) rotate(4deg); }
    70% { transform: translateX(-2px) rotate(-2deg); }
}

/* Win: once the last guess has flipped in, each cell lifts and glows in a
   diagonal wave from the top-left corner. */
.grid-cell.cell-celebrate {
    animation: cell-celebrate 0.75s cubic-bezier(.3,1.5,.5,1) var(--wave-delay, 0s) both;
    z-index: 1;
}
.grid-cell.cell-celebrate .grid-cell-content {
    animation: cell-shine 0.75s ease-out var(--wave-delay, 0s) both;
}

@keyframes cell-celebrate {
    0%, 100% { transform: none; }
    40% { transform: translateY(-8px) scale(1.1); }
    70% { transform: translateY(1px) scale(0.98); }
}
@keyframes cell-shine {
    0%, 100% { box-shadow: none; filter: none; }
    40% {
        box-shadow: 0 0 0 3px color-mix(in srgb, var(--correct-text) 45%, transparent),
                    0 8px 22px color-mix(in srgb, var(--correct-text) 35%, transparent);
        filter: brightness(1.08) saturate(1.2);
    }
}

/* Loss: the grid shudders as the answers flip in, then each revealed answer
   lights up in turn so it is clear which numbers were missed. */
.grid-cells.grid-defeat {
    animation: grid-defeat 0.7s cubic-bezier(.36,.07,.19,.97) 0.2s both;
}
@keyframes grid-defeat {
    0%, 100% { transform: none; }
    15% { transform: translateX(-7px); }
    30% { transform: translateX(6px); }
    45% { transform: translateX(-4px); }
    60% { transform: translateX(3px); }
    75% { transform: translateX(-1px); }
}

.grid-cell.revealed.cell-spotlight {
    animation: cell-spotlight 0.9s ease-out var(--wave-delay, 0s) both;
    z-index: 1;
}
.grid-cell.cell-spotlight:not(.revealed) .grid-cell-content {
    animation: cell-dim 1.6s ease both;
}
@keyframes cell-spotlight {
    0%, 100% { transform: none; box-shadow: none; }
    35% {
        transform: scale(1.08);
        box-shadow: 0 0 0 3px color-mix(in srgb, var(--revealed-text) 50%, transparent),
                    0 6px 18px color-mix(in srgb, var(--revealed-text) 30%, transparent);
    }
}
@keyframes cell-dim {
    0%, 100% { opacity: 1; }
    30%, 70% { opacity: 0.55; }
}

.confetti-canvas {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 9999;
    pointer-events: none;
}

.defeat-vignette {
    position: fixed;
    inset: 0;
    z-index: 9999;
    pointer-events: none;
    background: radial-gradient(ellipse at center, transparent 55%, color-mix(in srgb, var(--incorrect-text) 22%, transparent) 100%);
    animation: vignette 1.8s ease both;
}
@keyframes vignette {
    0%, 100% { opacity: 0; }
    20%, 55% { opacity: 1; }
}

/* ==========================================================================
   Toast
   ========================================================================== */
.toast {
    position: fixed;
    left: 50%;
    top: calc(68px + env(safe-area-inset-top));
    transform: translate(-50%, -12px);
    max-width: calc(100vw - 32px);
    padding: 10px 18px;
    background: var(--toast-bg);
    color: var(--toast-text);
    border-radius: 999px;
    box-shadow: var(--shadow-md);
    font-size: 0.95rem;
    font-weight: 500;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s, transform 0.2s;
    z-index: 10001;
}

.toast.visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

/* ==========================================================================
   Short screens: tighten spacing so grid, pad and button fit
   ========================================================================== */
@media (max-height: 760px) {
    .layout { padding-top: 8px; padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
    .play { gap: 10px; }
    .clues-panel { padding: 16px 18px; }
    .panel-title { margin-bottom: 8px; }
    .clue-groups { gap: 12px; }
    .clue-list li { padding-block: 5px; }
    .grid-cells { --grid-gap: 6px; --chrome: 432px; }
    .key { height: 46px; }
    .keypad { gap: 6px; margin-top: 0; }
    .button { min-height: 48px; }
    .controls { margin-top: 0; gap: 2px; }
    .result, .countdown { margin-top: 2px; font-size: 0.92rem; line-height: 1.35; }
    .active-clues { --clue-size: 0.88rem; --clue-pad: 4px; --strip-pad: 4px; padding-inline: 12px; }
}

@media (max-height: 600px) {
    .grid-cells { --chrome: 380px; }
    .app-bar-inner { height: 48px; }
    .keypad { grid-template-columns: repeat(10, 1fr); gap: 4px; }
    .key { height: 44px; font-size: 1.2rem; }
    .key svg { width: 18px; }
}

/* ==========================================================================
   Solved lines: when a guess completes a row or column, a short animation
   themed on its clue plays over it (see lineEffects.js). Durations here
   are stretched to match PACE there.
   ========================================================================== */
.grid-cells { position: relative; }

.line-fx {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 4;
    animation: line-fx-out 0.55s ease 2.3s forwards;
}
.line-fx-svg {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;   /* the global svg rule sizes icons */
    height: 100%;
    overflow: visible;
}
.line-fx-stroke {
    fill: none;
    stroke: var(--accent);
    stroke-width: 3.5;
    stroke-linecap: round;
    stroke-linejoin: round;
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    filter: drop-shadow(0 0 4px color-mix(in srgb, var(--accent) 55%, transparent));
    animation: line-fx-draw 0.85s cubic-bezier(.45,.05,.3,1) both;
}
.line-fx-head { fill: var(--accent); }
.line-fx-dot {
    fill: var(--accent);
    transform-box: fill-box;
    transform-origin: center;
    animation: line-fx-pop 0.55s cubic-bezier(.3,1.6,.5,1) both;
}
@keyframes line-fx-draw { to { stroke-dashoffset: 0; } }
@keyframes line-fx-out { to { opacity: 0; } }

/* Chips: the label, the plus signs between cells, and arrows */
.line-fx-label,
.line-fx-plus,
.line-fx-arrow {
    position: absolute;
    transform: translate(-50%, -50%);
    animation: line-fx-chip 0.6s cubic-bezier(.3,1.6,.5,1) both;
}
.line-fx-label {
    padding: 4px 11px;
    border-radius: 999px;
    background: var(--primary-bg);
    color: var(--on-primary);
    font-size: 0.85rem;
    font-weight: 700;
    white-space: nowrap;
    box-shadow: var(--shadow-md);
}
.line-fx-plus {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--accent);
    color: var(--surface);
    font-size: 1rem;
    font-weight: 800;
    line-height: 1;
}
.line-fx-arrow {
    color: var(--accent);
    font-size: 0.85rem;
    animation: line-fx-travel 1.4s ease-out both;
}
@keyframes line-fx-chip {
    0% { transform: translate(-50%, -50%) scale(0); opacity: 0; }
    100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}
@keyframes line-fx-travel {
    0% { transform: translate(-50%, -50%); opacity: 0; }
    25% { opacity: 1; }
    100% { transform: translate(-50%, calc(-50% + var(--fx-travel, -20px))); opacity: 0; }
}
@keyframes line-fx-pop {
    0% { transform: scale(0); }
    100% { transform: scale(1); }
}

/* The solved line's row number or column letter */
.row-label.label-solved,
.column-label.label-solved {
    animation: label-solved 2.7s ease both;
}
@keyframes label-solved {
    0%, 100% { color: var(--text-muted); background: transparent; }
    15%, 70% { color: var(--on-primary); background: var(--primary-bg); }
}

/* What the cells do */
.grid-cell.fx-pop,
.grid-cell.fx-rise,
.grid-cell.fx-sink,
.grid-cell.fx-up,
.grid-cell.fx-down,
.grid-cell.fx-ring { z-index: 2; }
.grid-cell.fx-pop { animation: fx-pop 0.8s cubic-bezier(.3,1.5,.5,1) var(--fx-delay, 0s) both; }
.grid-cell.fx-rise { animation: fx-rise 1.1s cubic-bezier(.3,1.4,.5,1) var(--fx-delay, 0s) both; }
.grid-cell.fx-sink { animation: fx-sink 1.1s cubic-bezier(.3,1.4,.5,1) var(--fx-delay, 0s) both; }
.grid-cell.fx-up { animation: fx-up 1s cubic-bezier(.3,1.4,.5,1) var(--fx-delay, 0s) both; }
.grid-cell.fx-down { animation: fx-down 1s cubic-bezier(.3,1.4,.5,1) var(--fx-delay, 0s) both; }
.grid-cell.fx-ring { animation: fx-ring 1.4s ease-out var(--fx-delay, 0s) both; }
.grid-cell.fx-dim .grid-cell-content { animation: fx-dim 2.3s ease both; }
@keyframes fx-pop {
    0%, 100% { transform: none; }
    40% { transform: scale(1.12); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 50%, transparent); }
}
@keyframes fx-rise {
    0%, 100% { transform: none; }
    45% { transform: translateY(-9px); }
}
@keyframes fx-sink {
    0%, 100% { transform: none; }
    45% { transform: translateY(9px); }
}
@keyframes fx-up {
    0%, 100% { transform: none; }
    45% { transform: translateY(-7px) rotate(-4deg); }
}
@keyframes fx-down {
    0%, 100% { transform: none; }
    45% { transform: translateY(7px) rotate(4deg); }
}
@keyframes fx-ring {
    0%, 100% { transform: none; box-shadow: none; }
    35% {
        transform: scale(1.1);
        box-shadow: 0 0 0 3.5px var(--fx-color, var(--accent)),
                    0 0 18px color-mix(in srgb, var(--fx-color, var(--accent)) 50%, transparent);
    }
}
@keyframes fx-dim {
    0%, 100% { opacity: 1; }
    30%, 70% { opacity: 0.45; }
}

/* ==========================================================================
   Load roll: when a puzzle first opens, the numbers on the grid flick
   through random digits and land on their value (see js/rollIn.js). The
   overlay sits over the content, whose own text is hidden until it lands.
   ========================================================================== */
.grid-cell.rolling .grid-cell-content { color: transparent; }

.cell-roll {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--cell) * 0.46);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
    pointer-events: none;
}

/* ==========================================================================
   Admin mode (?admin, see js/admin.js): the panel, its button, answer tags
   ========================================================================== */
.admin-fab {
    position: fixed;
    left: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom));
    z-index: 50;
    padding: 6px 12px;
    border: 1px dashed var(--border-strong);
    border-radius: 999px;
    background: var(--surface);
    color: var(--text-muted);
    font: inherit;
    font-size: 0.8rem;
    font-weight: 600;
    box-shadow: var(--shadow-sm);
    cursor: pointer;
}
.admin-fab:hover { color: var(--accent); border-color: var(--accent); }

/* Let the page scroll clear of the button. */
body.admin-mode { padding-bottom: calc(56px + env(safe-area-inset-bottom)); }

.admin-status {
    margin: 4px 0 0;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    color: var(--text-muted);
    font-size: 0.9rem;
}

.admin-actions {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 8px;
}
.admin-actions + .admin-actions { margin-top: 8px; }
.admin-actions-compact { grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); }

.admin-help {
    margin: 0 0 8px;
    color: var(--text-muted);
    font-size: 0.85rem;
}

.admin-row {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
}
.admin-row .button { flex: 1; }

.admin-number {
    width: 6em;
    min-width: 0;
    padding: 0 12px;
    border: 1.5px solid var(--border-strong);
    border-radius: var(--radius-sm);
    background: var(--cell-bg);
    color: var(--text);
    font: inherit;
    font-variant-numeric: tabular-nums;
}
.admin-number:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 1px; }

.admin-footer {
    margin-top: 22px;
    padding-top: 14px;
    border-top: 1px solid var(--border);
}
.admin-footer .button { width: 100%; }

.admin-answer {
    position: absolute;
    left: 5px;
    bottom: 4px;
    z-index: 3;
    padding: 1px 5px;
    border: 1px dashed currentColor;
    border-radius: 6px;
    background: var(--surface);
    color: var(--accent);
    font-size: 0.7rem;
    font-weight: 700;
    line-height: 1.3;
    pointer-events: none;
}

.practice-chip {
    background: var(--hint-bg);
    color: var(--hint-text);
}

/* ==========================================================================
   Reduced motion: no shakes, bounces, scaling or rain
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        animation-delay: 0s !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    .confetti-canvas,
    .defeat-vignette,
    .line-fx {
        display: none;
    }
}
