*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Themes. Piece colors (--p0 to --p6) and board colors are read by script.js. */
[data-theme="dusk"] {
    --bg: #1a1625;
    --texture: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24'%3E%3Ccircle cx='2' cy='2' r='1' fill='%232e2840'/%3E%3C/svg%3E");
    --surface: #241f33;
    --surface-2: #2e2840;
    --line: #3d3554;
    --text: #f3ede4;
    --muted: #b9b0c9;
    --accent: #ff7a59;
    --accent-hover: #ff9478;
    --accent-ink: #1a1625;
    --accent-2: #3cc6b4;
    --success: #6fd08c;
    --danger: #ff6b8a;
    --scrim: rgba(18, 15, 27, 0.82);
    --board: #13101c;
    --grid: #221c30;
    --cell-ink: #13101c;
    --font-display: "Space Grotesk", system-ui, sans-serif;
    --p0: #ff7a59;
    --p1: #3cc6b4;
    --p2: #f2b134;
    --p3: #8c97f2;
    --p4: #93c47d;
    --p5: #ef6f9a;
    --p6: #c18ae6;
}

[data-theme="parchment"] {
    --bg: #e9dcc2;
    --texture: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12'%3E%3Cpath d='M0 12L12 0' stroke='%23dccbaa' stroke-width='1'/%3E%3C/svg%3E");
    --surface: #f4ecdb;
    --surface-2: #ece0c6;
    --line: #c2ad86;
    --text: #2f2117;
    --muted: #5e4a37;
    --accent: #8e2b2b;
    --accent-hover: #a63838;
    --accent-ink: #fbf5e8;
    --accent-2: #2f5d45;
    --success: #2f5d45;
    --danger: #8e2b2b;
    --scrim: rgba(233, 220, 194, 0.86);
    --board: #f6efe0;
    --grid: #e6d9bd;
    --cell-ink: #2f2117;
    --font-display: "Cinzel", Georgia, serif;
    --p0: #8e2b2b;
    --p1: #2f5d45;
    --p2: #c08a2d;
    --p3: #3e5c8a;
    --p4: #7a4f8f;
    --p5: #b5552b;
    --p6: #5f7a3a;
}

[data-theme="diner"] {
    --bg: #bfe6dc;
    --texture: none;
    --surface: #fbf8f0;
    --surface-2: #e8f4ef;
    --line: #8ccfc0;
    --text: #1f2d3a;
    --muted: #435564;
    --accent: #c8323f;
    --accent-hover: #dd4250;
    --accent-ink: #ffffff;
    --accent-2: #127c7a;
    --success: #127c7a;
    --danger: #c8323f;
    --scrim: rgba(191, 230, 220, 0.86);
    --board: #fdfaf2;
    --grid: #e3ebe5;
    --cell-ink: #1f2d3a;
    --font-display: "Fredoka", system-ui, sans-serif;
    --p0: #e04b59;
    --p1: #f0a830;
    --p2: #1d9a96;
    --p3: #6b7fd7;
    --p4: #f07f9a;
    --p5: #3f8f4f;
    --p6: #8d5bc1;
}

[data-theme="circuit"] {
    --bg: #0b0e16;
    --texture: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='32' height='32'%3E%3Cpath d='M0 16h10l4-4h18' fill='none' stroke='%23151b2b' stroke-width='1'/%3E%3C/svg%3E");
    --surface: #121725;
    --surface-2: #182036;
    --line: #263152;
    --text: #e8f1ff;
    --muted: #9aa8c7;
    --accent: #ff3d8b;
    --accent-hover: #ff66a5;
    --accent-ink: #0b0e16;
    --accent-2: #c6ff3d;
    --success: #c6ff3d;
    --danger: #ff3d8b;
    --scrim: rgba(7, 10, 17, 0.85);
    --board: #070a11;
    --grid: #131a2b;
    --cell-ink: #070a11;
    --font-display: "Orbitron", system-ui, sans-serif;
    --p0: #ff3d8b;
    --p1: #c6ff3d;
    --p2: #3df0ff;
    --p3: #ffb43d;
    --p4: #9b6bff;
    --p5: #3dffa8;
    --p6: #ff6b3d;
}

[data-theme="zellige"] {
    --bg: #0f2747;
    --texture: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='40'%3E%3Cg fill='none' stroke='%2318375f' stroke-width='1'%3E%3Crect x='12' y='12' width='16' height='16'/%3E%3Crect x='12' y='12' width='16' height='16' transform='rotate(45 20 20)'/%3E%3C/g%3E%3C/svg%3E");
    --surface: #15325a;
    --surface-2: #1b3d6b;
    --line: #2d5486;
    --text: #f6efd9;
    --muted: #c3cde0;
    --accent: #f0b429;
    --accent-hover: #f6c654;
    --accent-ink: #0f2747;
    --accent-2: #22b07d;
    --success: #22b07d;
    --danger: #e2574c;
    --scrim: rgba(11, 31, 58, 0.86);
    --board: #0b1f3a;
    --grid: #163258;
    --cell-ink: #0b1f3a;
    --font-display: "Marcellus", Georgia, serif;
    --p0: #f0b429;
    --p1: #22b07d;
    --p2: #e2574c;
    --p3: #5fb7e8;
    --p4: #f3e3b5;
    --p5: #b06ad9;
    --p6: #ef8a3c;
}

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

body {
    min-height: 100vh;
    min-height: 100svh;
    background-color: var(--bg);
    background-image: var(--texture);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    transition: background-color 0.3s ease, color 0.3s ease;
}

svg {
    fill: none;
    stroke: currentColor;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.stroke-accent { stroke: var(--accent); }
.stroke-accent-2 { stroke: var(--accent-2); }

.hidden {
    display: none !important;
}

.app {
    max-width: 760px;
    margin: 0 auto;
    padding: 16px 16px max(16px, env(safe-area-inset-bottom));
    display: flex;
    flex-direction: column;
    gap: 14px;
}

/* Header */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
}

.logo {
    width: 34px;
    height: 34px;
}

.logo-fall { stroke: var(--accent-2); }
.logo-trail { stroke: var(--accent-2); }
.logo-a,
.logo-c { stroke: var(--accent); }
.logo-b { stroke: var(--p2); }

h1 {
    font-family: var(--font-display);
    font-size: 1.6rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    line-height: 1;
}

.icon-btn {
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 12px;
    color: var(--text);
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.icon-btn svg {
    width: 22px;
    height: 22px;
}

.icon-btn:hover:not(:disabled) {
    background: var(--surface-2);
    border-color: var(--accent);
}

.icon-btn:active:not(:disabled) {
    transform: scale(0.94);
}

.icon-btn:disabled {
    opacity: 0.45;
    cursor: default;
}

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

/* Playfield */
.playfield {
    position: relative;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    grid-template-areas: "left board right";
    gap: 16px;
    align-items: start;
}

.panel {
    display: flex;
    flex-direction: column;
    gap: 14px;
    min-width: 0;
}

.panel-left { grid-area: left; }
.panel-right { grid-area: right; }

.card {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 14px;
    padding: 12px;
}

.card-title {
    display: flex;
    align-items: center;
    gap: 6px;
    font-family: var(--font-display);
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
    margin-bottom: 8px;
}

.card-title svg {
    width: 18px;
    height: 18px;
}

.mini-canvas {
    display: block;
    width: 100%;
    aspect-ratio: 5 / 3;
    background: var(--board);
    border-radius: 10px;
}

.next-canvas {
    aspect-ratio: 5 / 13;
}

.stats {
    display: grid;
    gap: 10px;
}

.stat dt {
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted);
}

.stat dd {
    font-family: var(--font-display);
    font-size: 1.45rem;
    font-weight: 700;
    line-height: 1.15;
    font-variant-numeric: tabular-nums;
}

.stat-best dd {
    color: var(--accent);
}

.keys ul {
    list-style: none;
    display: grid;
    gap: 6px;
    font-size: 0.95rem;
    color: var(--muted);
}

kbd {
    display: inline-block;
    min-width: 26px;
    padding: 1px 6px;
    margin-right: 4px;
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 600;
    text-align: center;
    color: var(--text);
    background: var(--surface-2);
    border: 1px solid var(--line);
    border-bottom-width: 2px;
    border-radius: 6px;
}

/* Board */
.board-wrap {
    grid-area: board;
    position: relative;
    width: min(320px, calc((100svh - 120px) / 2));
    min-width: 200px;
    aspect-ratio: 1 / 2;
}

#board {
    display: block;
    width: 100%;
    height: 100%;
    background: var(--board);
    border: 2px solid var(--line);
    border-radius: 14px;
    touch-action: none;
    transition: border-color 0.2s ease;
}

.board-wrap.bump #board {
    animation: bump 0.14s ease-out;
}

@keyframes bump {
    40% { transform: translateY(3px); }
}

.callout {
    position: absolute;
    left: 0;
    right: 0;
    top: 28%;
    text-align: center;
    font-family: var(--font-display);
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--accent);
    text-shadow: 0 2px 0 var(--board);
    pointer-events: none;
    opacity: 0;
}

.callout.show {
    animation: callout 1s ease-out forwards;
}

@keyframes callout {
    0% { opacity: 0; transform: translateY(8px) scale(0.92); }
    15% { opacity: 1; transform: translateY(0) scale(1); }
    75% { opacity: 1; }
    100% { opacity: 0; transform: translateY(-14px); }
}

/* Overlays */
.overlay {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: grid;
    place-items: center;
    padding: 12px;
    background: var(--scrim);
    border-radius: 14px;
}

.overlay-card {
    width: min(340px, 100%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 14px;
    padding: 24px 20px;
    text-align: center;
    background: var(--surface);
    border: 1px solid var(--line);
    border-top: 4px solid var(--accent);
    border-radius: 18px;
    box-shadow: 0 18px 40px rgba(0, 0, 0, 0.28);
}

.overlay-card h2 {
    font-family: var(--font-display);
    font-size: 1.7rem;
    font-weight: 700;
    line-height: 1.1;
}

.overlay-lead {
    color: var(--muted);
}

.overlay-best strong,
.summary dd {
    font-family: var(--font-display);
    font-variant-numeric: tabular-nums;
    color: var(--accent);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    min-height: 48px;
    padding: 10px 18px;
    font: inherit;
    font-weight: 700;
    border-radius: 12px;
    cursor: pointer;
    transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

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

.btn:active {
    transform: scale(0.97);
}

.btn-primary {
    background: var(--accent);
    color: var(--accent-ink);
    border: 2px solid var(--accent);
}

.btn-primary:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

.btn-ghost {
    background: transparent;
    color: var(--text);
    border: 2px solid var(--line);
}

.btn-ghost:hover {
    border-color: var(--accent-2);
    background: var(--surface-2);
}

.btn-ghost svg {
    stroke: var(--accent-2);
}

.new-best {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    font-weight: 700;
    color: var(--success);
    border: 1px solid var(--success);
    border-radius: 999px;
}

.new-best svg {
    width: 18px;
    height: 18px;
}

.summary {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    width: 100%;
}

.summary div {
    padding: 8px 4px;
    background: var(--surface-2);
    border-radius: 10px;
}

.summary dt {
    font-size: 0.8rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--muted);
}

.summary dd {
    font-size: 1.3rem;
    font-weight: 700;
}

.theme-picker {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 2px;
    width: 100%;
}

.swatch {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    min-width: 0;
    padding: 6px 0;
    font: inherit;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--muted);
    background: transparent;
    border: 2px solid transparent;
    border-radius: 10px;
    cursor: pointer;
    transition: border-color 0.15s ease, background-color 0.15s ease;
}

.swatch:hover {
    background: var(--surface-2);
}

.swatch[aria-checked="true"] {
    border-color: var(--accent);
    color: var(--text);
}

.swatch-chip {
    display: flex;
    width: 34px;
    height: 22px;
    overflow: hidden;
    border-radius: 6px;
    border: 1px solid var(--line);
}

.swatch-chip span {
    flex: 1;
}

.hint {
    font-size: 0.9rem;
    color: var(--muted);
}

.hint-touch {
    display: none;
}

/* Touch controls */
.touchpad {
    display: none;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.pad {
    display: grid;
    place-items: center;
    min-height: 54px;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 14px;
    cursor: pointer;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.1s ease, border-color 0.1s ease, transform 0.08s ease;
}

.pad svg {
    width: 26px;
    height: 26px;
    stroke: var(--accent-2);
}

.pad-alt svg {
    stroke: var(--p2);
}

.pad-drop {
    grid-column: span 2;
    background: var(--accent);
    border-color: var(--accent);
}

.pad-drop svg {
    stroke: var(--accent-ink);
}

.pad.pressed,
.pad:active {
    transform: scale(0.95);
    background: var(--surface-2);
    border-color: var(--accent-2);
}

.pad-drop.pressed,
.pad-drop:active {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

@media (pointer: coarse), (max-width: 640px) {
    .touchpad { display: grid; }
    .keys,
    .hint-keys { display: none; }
    .hint-touch { display: block; }
}

@media (max-width: 640px) {
    .app {
        padding: 10px 16px max(12px, env(safe-area-inset-bottom));
        gap: 10px;
    }

    .logo {
        width: 28px;
        height: 28px;
    }

    h1 {
        font-size: 1.3rem;
    }

    .playfield {
        grid-template-columns: auto minmax(0, 1fr);
        grid-template-areas:
            "board right"
            "board left";
        grid-template-rows: auto 1fr;
        gap: 10px;
    }

    .board-wrap {
        width: min(calc(100vw - 150px), calc((100svh - 220px) / 2));
        min-width: 160px;
    }

    .panel {
        gap: 8px;
    }

    .card {
        padding: 8px;
        border-radius: 12px;
    }

    .card-title {
        font-size: 0.75rem;
        margin-bottom: 4px;
    }

    .card-title svg {
        width: 15px;
        height: 15px;
    }

    .mini-canvas {
        aspect-ratio: 2 / 1;
    }

    .next-canvas {
        aspect-ratio: 5 / 7.5;
    }

    .overlay {
        padding: 8px;
    }

    .overlay-card {
        padding: 20px 14px;
    }

    .stats {
        gap: 4px;
    }

    .stat dt {
        font-size: 0.7rem;
    }

    .stat dd {
        font-size: 1.1rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
