:root {
    --bg: #050a12;
    --panel: rgba(9, 20, 32, 0.88);
    --text: #f4fbff;
    --soft: #c9e9f4;
    --muted: #8fb2bf;
    --cyan: #55e8ff;
    --pink: #ff5ec4;
    --amber: #ffd166;
    --green: #5dffb1;
    --violet: #9b72ff;
    --border: rgba(85, 232, 255, 0.28);
    --shadow: rgba(85, 232, 255, 0.2);
}

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

body {
    background:
        radial-gradient(circle at 16% 10%, rgba(85, 232, 255, 0.14), transparent 30%),
        radial-gradient(circle at 84% 18%, rgba(255, 94, 196, 0.13), transparent 28%),
        radial-gradient(circle at 50% 86%, rgba(93, 255, 177, 0.11), transparent 34%),
        linear-gradient(180deg, #030811 0%, #0b1824 56%, #050a12 100%);
    color: var(--text);
    font-family: Inter, "Segoe UI", system-ui, sans-serif;
    margin: 0;
    min-height: 100vh;
    overflow: hidden;
}

.game-shell {
    align-items: center;
    display: flex;
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
    width: 100%;
}

.game-panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 0 42px var(--shadow), inset 0 0 36px rgba(2, 6, 23, 0.42);
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    height: 100%;
    margin: 0 auto;
    max-height: 900px;
    max-width: 1080px;
    min-width: 0;
    overflow: hidden;
    position: relative;
    width: 100%;
}

.game-topbar {
    align-items: center;
    background: linear-gradient(180deg, rgba(9, 20, 32, 0.96), rgba(3, 9, 16, 0.92));
    border-bottom: 1px solid var(--border);
    display: grid;
    gap: 16px;
    grid-template-columns: auto 1fr auto;
    min-width: 0;
    padding: 14px 18px;
}

.back-link,
.controls button,
.board-hud button,
.overlay-card button {
    background: rgba(3, 9, 16, 0.78);
    border: 1px solid var(--border);
    border-radius: 7px;
    color: var(--cyan);
    cursor: pointer;
    font: 800 12px/1.25 Inter, "Segoe UI", system-ui, sans-serif;
    letter-spacing: 0;
    padding: 10px 12px;
    text-decoration: none;
    text-transform: uppercase;
    min-height: 44px;
    min-width: 44px;
    overflow-wrap: anywhere;
    text-align: center;
}

.back-link { display: flex; align-items: center; justify-content: center; }

.back-link:hover,
.controls button:hover,
.board-hud button:hover,
.overlay-card button:hover,
.back-link:focus-visible,
.controls button:focus-visible,
.board-hud button:focus-visible,
.overlay-card button:focus-visible {
    box-shadow: 0 0 16px var(--shadow);
    outline: 1px solid var(--cyan);
}

.game-title {
    min-width: 0;
    text-align: center;
}

.game-title h1 {
    color: var(--text);
    font-size: 24px;
    line-height: 1.05;
    margin: 0;
    text-shadow: 0 0 16px rgba(85, 232, 255, 0.42), 0 0 22px rgba(93, 255, 177, 0.22);
    text-transform: uppercase;
}

.game-title p {
    color: var(--muted);
    font: 700 12px/1.4 "SFMono-Regular", Consolas, monospace;
    margin: 4px 0 0;
    text-transform: uppercase;
}

.scoreboard {
    display: flex;
    gap: 10px;
    min-width: 0;
}

.scoreboard div,
.run-stat {
    border: 1px solid rgba(85, 232, 255, 0.24);
    border-radius: 7px;
    padding: 8px 10px;
}

.scoreboard div {
    min-width: 104px;
    text-align: right;
}

.scoreboard span,
.run-stat span {
    color: var(--muted);
    display: block;
    font: 800 10px/1 "SFMono-Regular", Consolas, monospace;
    text-transform: uppercase;
}

.scoreboard strong,
.run-stat strong {
    color: var(--amber);
    display: block;
    font: 900 22px/1.1 "SFMono-Regular", Consolas, monospace;
    margin-top: 4px;
}

.arena-wrap {
    align-items: center;
    background:
        linear-gradient(rgba(85, 232, 255, 0.07) 1px, transparent 1px),
        linear-gradient(90deg, rgba(93, 255, 177, 0.07) 1px, transparent 1px),
        radial-gradient(circle at 50% 48%, rgba(85, 232, 255, 0.12), transparent 40%),
        #030a12;
    background-size: 48px 48px, 48px 48px, auto, auto;
    display: flex;
    justify-content: center;
    min-height: 0;
    overflow: hidden;
    padding: 8px;
}

.play-layout {
    align-items: stretch;
    display: grid;
    gap: 8px;
    grid-template-columns: minmax(0, 1fr) 116px;
    height: 100%;
    justify-content: center;
    max-width: 880px;
    min-width: 0;
    min-height: 0;
    outline: none;
    width: 100%;
}

.board-column {
    display: grid;
    gap: 6px;
    grid-template-rows: auto minmax(0, 1fr) auto;
    min-height: 0;
    min-width: 0;
}

.board-hud {
    align-items: center;
    display: flex;
    gap: 12px;
    justify-content: space-between;
}

.board-hud span {
    background: rgba(3, 9, 16, 0.72);
    border: 1px solid rgba(85, 232, 255, 0.24);
    border-radius: 999px;
    color: var(--soft);
    font: 900 11px/1.25 "SFMono-Regular", Consolas, monospace;
    padding: 6px 10px;
    text-transform: uppercase;
}

.board {
    --size: 6;
    aspect-ratio: 1;
    background:
        radial-gradient(circle at 50% 50%, rgba(85, 232, 255, 0.08), transparent 58%),
        url("../images/chip-core.svg") center / 54% no-repeat,
        rgba(3, 9, 16, 0.86);
    border: 1px solid rgba(85, 232, 255, 0.38);
    border-radius: 10px;
    box-shadow: 0 0 34px rgba(85, 232, 255, 0.16), inset 0 0 28px rgba(2, 6, 23, 0.62);
    display: grid;
    gap: 2px;
    grid-template-columns: repeat(var(--size), minmax(0, 1fr));
    grid-template-rows: repeat(var(--size), minmax(0, 1fr));
    height: var(--board-edge, 360px);
    width: var(--board-edge, 360px);
    justify-self: center;
    padding: 4px;
    touch-action: none;
    user-select: none;
}

.board-viewport { min-height: 0; min-width: 0; overflow: auto; overscroll-behavior: contain; border: 1px solid var(--border); border-radius: 8px; }
.board-stage { display: grid; width: max(100%, var(--board-edge, 360px)); min-height: max(100%, var(--board-edge, 360px)); place-items: center; }
.board-viewport.is-panning .board { touch-action: pan-x pan-y; }
.board-viewport.is-panning .cell { cursor: grab; }
.board-help { color: var(--muted); font-size: 11px; line-height: 1.35; margin: 0; }
.cell:focus-visible, .board-viewport:focus-visible { outline: 2px solid var(--amber); outline-offset: -3px; }
.cell.route-head { border: 2px solid var(--text); }
.cell .terminal-mark { color: #031018; position: absolute; inset: 0; display: grid; place-items: center; font: 900 12px/1 monospace; z-index: 1; pointer-events: none; }

.cell {
    appearance: none;
    background:
        linear-gradient(135deg, rgba(85, 232, 255, 0.08), rgba(93, 255, 177, 0.04)),
        rgba(3, 9, 16, 0.78);
    border: 1px solid rgba(85, 232, 255, 0.18);
    border-radius: 7px;
    cursor: pointer;
    min-height: 0;
    min-width: 0;
    padding: 0;
    position: relative;
}

.cell::before {
    background: var(--trace-color);
    border-radius: 999px;
    box-shadow: 0 0 14px var(--trace-color);
    content: "";
    inset: 34%;
    opacity: 0;
    position: absolute;
}

.cell.filled::before {
    inset: 28%;
    opacity: 0.88;
}

.cell.terminal::before {
    background: var(--trace-color);
    inset: 15%;
    opacity: 1;
    filter: drop-shadow(0 0 10px var(--trace-color));
    -webkit-mask: url("../images/terminal-node.svg") center / contain no-repeat;
    mask: url("../images/terminal-node.svg") center / contain no-repeat;
}

.color-cyan { --trace-color: #55e8ff; }
.color-pink { --trace-color: #ff5ec4; }
.color-amber { --trace-color: #ffd166; }
.color-green { --trace-color: #5dffb1; }
.color-violet { --trace-color: #9b72ff; }
.color-orange { --trace-color: #ff8f4d; }
.color-blue { --trace-color: #5f8cff; }

.side-panel {
    display: grid;
    gap: 10px;
    grid-auto-rows: max-content;
    min-width: 0;
    min-height: 0;
    overflow: auto;
}

.run-stat {
    background: rgba(3, 9, 16, 0.58);
}

.countdown-overlay {
    align-items: center;
    background: radial-gradient(circle at center, rgba(85, 232, 255, 0.2), transparent 42%), rgba(3, 9, 16, 0.28);
    display: flex;
    flex-direction: column;
    gap: 12px;
    inset: 0;
    justify-content: center;
    pointer-events: none;
    position: absolute;
    z-index: 9;
}

.countdown-overlay.hidden,
.overlay.hidden {
    display: none;
}

.countdown-ring {
    --countdown-accent: var(--green);
    align-items: center;
    aspect-ratio: 1;
    background: linear-gradient(#050a12, #050a12) padding-box, conic-gradient(from -90deg, var(--countdown-accent), var(--cyan), var(--amber), var(--countdown-accent)) border-box;
    border: 4px solid transparent;
    border-radius: 50%;
    box-shadow: 0 0 34px color-mix(in srgb, var(--countdown-accent) 45%, transparent), inset 0 0 24px rgba(2, 6, 23, 0.9);
    display: grid;
    justify-items: center;
    width: clamp(118px, 20vw, 178px);
}

.countdown-ring span {
    color: var(--text);
    font: 950 clamp(48px, 9vw, 86px)/1 "SFMono-Regular", Consolas, monospace;
    text-shadow: 0 0 16px var(--countdown-accent), 0 4px 0 rgba(2, 6, 23, 0.72);
}

.countdown-overlay small {
    background: rgba(3, 9, 16, 0.72);
    border: 1px solid rgba(85, 232, 255, 0.28);
    border-radius: 999px;
    color: var(--soft);
    font: 900 11px/1 "SFMono-Regular", Consolas, monospace;
    padding: 9px 13px;
    text-transform: uppercase;
}

.countdown-overlay.pulse .countdown-ring {
    animation: countdownPop 0.7s ease both;
}

.countdown-overlay.go .countdown-ring {
    --countdown-accent: var(--amber);
}

.controls {
    background: rgba(3, 9, 16, 0.74);
    border-top: 1px solid var(--border);
    display: grid;
    gap: 8px;
    grid-template-columns: repeat(4, minmax(0, 150px));
    justify-content: center;
    padding: 6px;
}

.controls button {
    min-height: 44px;
    padding: 6px;
}

.controls button[aria-pressed="true"] { background: #25453b; border-color: var(--green); color: var(--green); }

.overlay {
    align-items: center;
    background: radial-gradient(circle at 30% 28%, rgba(85, 232, 255, 0.18), transparent 30%), radial-gradient(circle at 72% 64%, rgba(93, 255, 177, 0.15), transparent 28%), linear-gradient(135deg, rgba(3, 9, 16, 0.92), rgba(3, 9, 16, 0.64));
    display: flex;
    inset: 0;
    justify-content: center;
    padding: clamp(6px, 2vw, 18px);
    position: absolute;
    z-index: 10;
}

.overlay-card {
    align-items: stretch;
    background: linear-gradient(135deg, rgba(9, 20, 32, 0.96), rgba(3, 9, 16, 0.88)), radial-gradient(circle at 14% 16%, rgba(85, 232, 255, 0.15), transparent 34%);
    border: 1px solid rgba(85, 232, 255, 0.38);
    border-radius: 10px;
    box-shadow: 0 0 34px rgba(85, 232, 255, 0.2), inset 0 0 28px rgba(2, 6, 23, 0.62);
    display: grid;
    gap: 0 22px;
    grid-template-columns: minmax(0, 0.55fr) minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    max-height: 100%;
    max-width: 820px;
    overflow: hidden;
    padding: 16px;
    position: relative;
    width: 100%;
    min-height: 0;
}

.overlay-content { overflow: auto; overscroll-behavior: contain; min-width: 0; min-height: 0; padding: 3px; }
.overlay-actions { grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 8px; padding-top: 12px; }
.overlay-actions > * { flex: 1 1 120px; }
.overlay-pause .difficulty-select, .overlay-end .difficulty-select { display: none; }
[hidden] { display: none !important; }
.difficulty-select input:focus-visible + span { outline: 2px solid var(--amber); outline-offset: -3px; }

.overlay-preview {
    display: block;
    filter: drop-shadow(0 0 24px rgba(85, 232, 255, 0.28));
    height: 100%;
    object-fit: contain;
    width: 100%;
}

.overlay-visual {
    min-width: 0;
    min-height: 0;
    align-self: center;
}

.overlay-mode {
    color: var(--green);
    display: block;
    font: 900 11px/1.2 "SFMono-Regular", Consolas, monospace;
    margin-bottom: 8px;
    text-transform: uppercase;
}

.overlay-card h2 {
    color: var(--cyan);
    font-size: clamp(30px, 5vw, 52px);
    line-height: 1.05;
    margin: 0 0 10px;
    text-transform: uppercase;
}

.overlay-card p {
    color: var(--soft);
    line-height: 1.55;
    margin: 0 0 18px;
    max-width: 480px;
}

.overlay-stats,
.difficulty-select {
    display: grid;
    gap: 8px;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    margin: 0 0 14px;
}

.overlay-stats div,
.difficulty-select span {
    background: rgba(3, 9, 16, 0.62);
    border: 1px solid rgba(85, 232, 255, 0.24);
    border-radius: 7px;
    padding: 10px;
}

.overlay-stats span,
.difficulty-select legend {
    color: var(--muted);
    display: block;
    font: 900 10px/1 "SFMono-Regular", Consolas, monospace;
    text-transform: uppercase;
}

.overlay-stats strong {
    color: var(--amber);
    display: block;
    font: 900 18px/1.1 "SFMono-Regular", Consolas, monospace;
    margin-top: 6px;
}

.difficulty-select {
    border: 0;
    padding: 0;
}

.difficulty-select legend {
    margin-bottom: 8px;
}

.difficulty-select label {
    cursor: pointer;
    min-width: 0;
}

.difficulty-select span { display: flex; min-height: 48px; flex-direction: column; justify-content: center; gap: 4px; }

.difficulty-select input {
    inline-size: 1px;
    opacity: 0;
    position: absolute;
}

.difficulty-select input:checked + span {
    background: linear-gradient(135deg, rgba(93, 255, 177, 0.2), rgba(3, 9, 16, 0.88));
    border-color: rgba(93, 255, 177, 0.56);
    box-shadow: 0 0 18px rgba(93, 255, 177, 0.18);
}

.difficulty-select strong {
    color: var(--text);
    font-size: 13px;
}

.difficulty-select small {
    color: var(--muted);
    font: 800 10px/1.2 "SFMono-Regular", Consolas, monospace;
}

.rules-panel {
    background: rgba(3, 9, 16, 0.54);
    border: 1px solid rgba(85, 232, 255, 0.24);
    border-radius: 8px;
    margin: 0 0 18px;
    padding: 12px 14px;
}

.rules-panel strong {
    color: var(--cyan);
    display: block;
    font: 900 11px/1 "SFMono-Regular", Consolas, monospace;
    margin-bottom: 8px;
    text-transform: uppercase;
}

.rules-panel ul {
    color: var(--soft);
    display: grid;
    gap: 7px;
    list-style: none;
    margin: 0;
    padding: 0;
}

.rules-panel li {
    font-size: 13px;
    line-height: 1.35;
    padding-left: 16px;
    position: relative;
}

.rules-panel li::before {
    background: var(--green);
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(93, 255, 177, 0.58);
    content: "";
    height: 6px;
    left: 0;
    position: absolute;
    top: 0.48em;
    width: 6px;
}

.overlay-card button {
    background: linear-gradient(135deg, rgba(93, 255, 177, 0.22), rgba(3, 9, 16, 0.88));
    border-color: rgba(93, 255, 177, 0.42);
    min-width: 0;
}

@keyframes countdownPop {
    0% { opacity: 0; transform: scale(0.72) rotate(-5deg); }
    55% { opacity: 1; transform: scale(1.08) rotate(2deg); }
    100% { opacity: 1; transform: scale(1) rotate(0deg); }
}

@media (max-width: 780px) {
    .game-topbar { gap: 6px; grid-template-columns: auto 1fr; padding: 6px; }
    .back-link, .game-title { grid-row: 1; }
    .scoreboard { grid-column: 1 / -1; justify-content: space-between; }
    .scoreboard div { flex: 1; min-width: 0; }
    .scoreboard div { padding: 4px 8px; }
    .scoreboard strong { font-size: 18px; }
    .game-title h1 { font-size: 20px; }
    .game-title p { font-size: 10px; }
    .overlay-card { grid-template-columns: minmax(0, 1fr); max-width: 560px; text-align: center; }
    .overlay-visual { display: none; }
}

@media (max-width: 560px) {
    .game-panel { border-radius: 8px; }
    .game-title h1 { font-size: 18px; }
    .game-title p { display: none; }
    .arena-wrap { padding: 4px; }
    .play-layout { gap: 8px; grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; }
    .side-panel { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); order: 2; }
    .run-stat { padding: 4px 6px; }
    .run-stat strong { font-size: 16px; }
    .overlay-card { padding: 10px; }
    .overlay-card h2 { font-size: 30px; margin-bottom: 8px; }
    .overlay-card p { font-size: 14px; line-height: 1.35; margin-bottom: 12px; }
    .overlay-stats, .difficulty-select { gap: 4px; margin-bottom: 12px; }
    .overlay-stats div, .difficulty-select span { padding: 8px 3px; }
    .overlay-stats strong { font-size: 14px; }
    .rules-panel { margin-bottom: 12px; padding: 10px; text-align: left; }
}

@media (max-height: 500px) and (max-width: 479px) {
    .scoreboard div, .run-stat { display: flex; align-items: center; justify-content: space-between; gap: 4px; padding: 4px; }
    .scoreboard strong, .run-stat strong { margin: 0; font-size: 14px; }
    .side-panel { gap: 4px; }
}

@media (max-width: 400px) {
    .controls button { font-size: 11px; padding: 4px; overflow-wrap: normal; }
}

@media (max-height: 380px) and (max-width: 479px) {
    .board-help { display: none; }
    .board-column { grid-template-rows: auto minmax(0, 1fr); }
}

@media (max-height: 500px) and (min-width: 480px) {
    .game-topbar { grid-template-columns: auto minmax(0, 1fr) auto; padding: 4px 6px; gap: 8px; }
    .scoreboard { grid-column: auto; }
    .scoreboard div { min-width: 64px; padding: 3px 5px; }
    .scoreboard strong { font-size: 16px; }
    .game-title h1 { font-size: 18px; }
    .game-title p { display: none; }
    .play-layout { grid-template-columns: minmax(0, 1fr) 92px; grid-template-rows: minmax(0, 1fr); }
    .side-panel { grid-template-columns: 1fr; gap: 6px; }
    .run-stat { padding: 4px 6px; }
    .run-stat strong { font-size: 16px; }
    .overlay-card { grid-template-columns: minmax(0, 1fr); max-width: 680px; padding: 10px; }
    .overlay-visual { display: none; }
    .overlay-card h2 { font-size: 28px; }
}

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