:root {
    --bg: #061018; --panel: rgba(10,22,34,.88); --text: #f4fbff;
    --soft: #c9e9f4; --muted: #8fb2bf; --cyan: #55e8ff;
    --pink: #ff5ec4; --amber: #ffd166; --green: #5dffb1; --violet: #9b72ff;
    --border: rgba(85,232,255,.28); --shadow: rgba(85,232,255,.2);
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
}
*, *::before, *::after { box-sizing: border-box; }
[hidden], .hidden { display: none !important; }
body {
    margin: 0; color: var(--text); font-family: Inter, "Segoe UI", system-ui, sans-serif;
    background: radial-gradient(circle at 18% 10%,rgba(85,232,255,.14),transparent 30%),
        radial-gradient(circle at 82% 16%,rgba(93,255,177,.13),transparent 28%),
        linear-gradient(180deg,#041018,#0d1b28 56%,#061018);
}
button, select, a { touch-action: manipulation; }
button, .back-link, select {
    background: rgba(4,12,20,.86); 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;
    min-width: 44px; min-height: 44px; padding: 9px 10px; overflow-wrap: anywhere;
}
button, .back-link { text-transform: uppercase; }
.back-link { display: grid; align-items: center; text-align: center; text-decoration: none; }
button:disabled { cursor: not-allowed; opacity: .5; }
:focus-visible { outline: 2px solid var(--cyan); outline-offset: -3px; }
button:hover, .back-link:hover { border-color: var(--cyan); }
.game-shell {
    display: flex; align-items: center; height: 100vh; height: 100dvh; width: 100%;
    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));
}
.game-panel {
    position: relative; display: grid; grid-template-rows: auto minmax(0,1fr) auto;
    width: 100%; max-width: 1180px; height: 100%; max-height: 960px; min-width: 0; min-height: 0; margin: auto;
    border: 1px solid var(--border); border-radius: 10px; background: var(--panel);
    box-shadow: 0 0 42px var(--shadow),inset 0 0 36px rgba(2,6,23,.42);
}
.game-topbar {
    display: grid; align-items: center; grid-template-columns: auto minmax(0,1fr) auto;
    gap: 14px; padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.game-title { min-width: 0; text-align: center; }
.game-title h1 { font-size: clamp(18px,2.4vw,24px); line-height: 1.1; margin: 0; text-transform: uppercase; text-shadow: 0 0 16px var(--shadow); }
.game-title p { color: var(--muted); font: 700 12px/1.4 Consolas,monospace; margin: 4px 0 0; }
.scoreboard { display: flex; gap: 8px; min-width: 0; }
.scoreboard div, .run-stat, .wave-progress { border: 1px solid var(--border); border-radius: 7px; padding: 8px 10px; background: rgba(4,12,20,.58); }
.scoreboard div { min-width: 90px; text-align: right; }
.scoreboard span, .run-stat > span, .wave-progress-head span { display: block; font: 800 11px/1.1 Consolas,monospace; color: var(--muted); text-transform: uppercase; }
.scoreboard strong, .run-stat strong, .wave-progress strong { display: block; color: var(--amber); font: 900 22px/1.1 Consolas,monospace; margin-top: 4px; }
.arena-wrap {
    display: flex; min-height: 0; min-width: 0; padding: 12px;
    background: linear-gradient(rgba(85,232,255,.06) 1px,transparent 1px),
        linear-gradient(90deg,rgba(93,255,177,.06) 1px,transparent 1px),#041018;
    background-size: 54px 54px;
}
.play-layout { display: grid; grid-template-columns: minmax(0,1fr) 132px; gap: 12px; width: 100%; min-width: 0; min-height: 0; }
.stage-column { display: grid; grid-template-rows: auto minmax(0,1fr); gap: 8px; min-width: 0; min-height: 0; }
.stage-hud { display: flex; gap: 8px; align-items: center; min-width: 0; }
.stage-hud > span { display: block; min-width: 0; flex: 1; white-space: nowrap; overflow: auto; overscroll-behavior: contain; padding: 8px; color: var(--soft); font: 800 12px/1.3 Consolas,monospace; border: 1px solid var(--border); border-radius: 7px; }
.stage-hud small { color: var(--muted); font-size: 11px; max-width: 115px; }
.stage-viewport { min-width: 0; min-height: 0; overflow: auto; overscroll-behavior: contain; touch-action: pan-x pan-y; border-radius: 8px; }
.stage-field { position: relative; width: 960px; height: 620px; margin: 0 auto; }
.stage {
    display: block; width: 100%; height: 100%; border-radius: 8px; outline: 1px solid rgba(85,232,255,.38); outline-offset: -1px;
    background: radial-gradient(circle at 50% 52%,rgba(85,232,255,.08),transparent 56%),linear-gradient(180deg,#08111b,#040c14);
}
.pad-layer { position: absolute; inset: 0; pointer-events: none; }
.pad-button {
    position: absolute; transform: translate(-50%,-50%); width: var(--pad-size,44px); height: var(--pad-size,44px);
    padding: 0; border-radius: 50%; pointer-events: auto; touch-action: pan-x pan-y;
    background: rgba(85,232,255,.08); border: 2px solid rgba(85,232,255,.5);
    box-shadow: inset 0 0 18px rgba(2,6,23,.72),0 0 12px rgba(85,232,255,.14);
}
.pad-button.hovered, .pad-button:focus-visible { border-color: white; outline: 2px solid white; }
.pad-button.selected { border-color: var(--amber); }
.pad-button.occupied { background: transparent; border-color: transparent; box-shadow: none; }
.pad-button.occupied:focus-visible, .pad-button.occupied.selected { outline: 2px solid var(--amber); }
.side-panel { display: grid; align-content: start; gap: 8px; min-width: 0; min-height: 0; overflow: auto; overscroll-behavior: contain; }
.wave-progress { display: grid; gap: 8px; }
.wave-progress-head { display: flex; gap: 8px; justify-content: space-between; align-items: center; }
.wave-track { height: 8px; border-radius: 8px; overflow: hidden; background: #020617; }
.wave-progress-fill { height: 100%; width: 100%; background: linear-gradient(90deg,var(--green),var(--amber),var(--pink)); transform-origin: left; }
.wave-ticks { display: grid; gap: 3px; }
.wave-ticks span { height: 5px; background: rgba(85,232,255,.16); border-radius: 3px; }
.wave-ticks .active { background: var(--amber); } .wave-ticks .done { background: var(--green); }
.controls { display: flex; justify-content: center; gap: 8px; padding: 8px 12px; border-top: 1px solid var(--border); min-width: 0; }
.controls > button { min-width: 80px; }
.speed-controls { display: grid; grid-template-columns: repeat(4,minmax(44px,1fr)); gap: 4px; min-width: 0; }
button.selected { background: linear-gradient(135deg,rgba(255,209,102,.24),#040c14); color: var(--amber); border-color: var(--amber); }
/* The nonmodal inspector is positioned in panel CSS pixels, never authored map coordinates. */
.build-menu {
    position: absolute; z-index: 6; width: min(360px,calc(100% - 16px)); max-height: calc(100% - 16px);
    display: grid; grid-template-rows: auto minmax(0,1fr); border: 1px solid rgba(255,209,102,.48); border-radius: 12px;
    background: linear-gradient(180deg,#08141e,#030912); box-shadow: 0 12px 28px #000a,0 0 24px rgba(255,209,102,.24);
}
.menu-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 8px; border-bottom: 1px solid var(--border); color: var(--amber); font-size: 12px; }
.menu-scroll { display: grid; align-content: start; grid-auto-rows: max-content; grid-template-columns: repeat(4,minmax(0,1fr)); gap: 8px; padding: 10px; overflow: auto; min-height: 0; min-width: 0; overscroll-behavior: contain; }
.menu-head { grid-column: 1/-1; display: grid; gap: 5px; min-width: 0; }
.menu-head strong { color: var(--amber); font-size: 14px; }
.menu-spec { color: var(--soft); font: 12px/1.4 Consolas,monospace; overflow-wrap: anywhere; }
.build-menu button[data-build] { display: grid; justify-items: center; gap: 3px; padding: 5px 2px; min-width: 44px; font-size: 11px; }
.build-menu button[data-build] img { height: 38px; width: 38px; pointer-events: none; }
.build-menu button[data-build] span { color: var(--amber); }
.build-name { font-size: 11px; }
.upgrade-action, .sell-action { grid-column: span 2; display: grid; gap: 5px; text-align: left; color: var(--amber); }
.sell-action { color: var(--pink); }
.upgrade-action em, .sell-action em { font-style: normal; text-transform: none; color: var(--soft); font-size: 12px; overflow-wrap: anywhere; }
.target-priority { grid-column: 1/-1; display: grid; grid-template-columns: repeat(auto-fit,minmax(44px,1fr)); gap: 4px; }
.target-priority > span { grid-column: 1/-1; color: var(--muted); font-size: 12px; }
.target-priority button { font-size: 11px; padding: 5px 2px; }
.build-menu:not(.tower-menu) :is(.upgrade-action,.sell-action,.target-priority) { display: none; }
.build-menu .unaffordable { border-color: var(--pink); color: var(--pink); background: #1c0814; }
.build-menu .disabled { opacity: .6; }
.overlay, .how-to-overlay, .countdown-overlay {
    position: absolute; inset: 0; display: grid; place-items: center; padding: 10px; z-index: 10;
    grid-template-columns: minmax(0,1fr); grid-template-rows: minmax(0,1fr);
    background: radial-gradient(circle at 30% 28%,rgba(85,232,255,.18),transparent 35%),rgba(4,12,20,.88); border-radius: inherit;
}
.overlay-card, .how-to-card {
    display: grid; grid-template-rows: minmax(0,1fr) auto; gap: 12px; min-width: 0; min-height: 0; max-height: 100%;
    width: min(100%,820px); padding: 16px; border: 1px solid rgba(85,232,255,.38); border-radius: 10px;
    background: linear-gradient(135deg,#0a1622,#040c14); box-shadow: 0 0 34px var(--shadow);
}
.overlay-scroll { display: grid; grid-template-columns: minmax(118px,170px) minmax(0,1fr); gap: 18px; overflow: auto; min-height: 0; overscroll-behavior: contain; }
.overlay-visual { align-self: center; min-width: 0; }
.overlay-preview { display: block; width: 100%; height: auto; max-height: 200px; object-fit: contain; }
.overlay-content { display: grid; gap: 12px; min-width: 0; }
.overlay-mode { display: block; color: var(--green); font: 800 11px/1.3 Consolas,monospace; margin-bottom: 6px; text-transform: uppercase; }
.settings-head h2, .how-to-card h2 { color: var(--cyan); font-size: clamp(24px,3.5vw,40px); line-height: 1.08; margin: 0 0 8px; text-transform: uppercase; overflow-wrap: anywhere; }
.settings-head p { color: var(--soft); font-size: 14px; line-height: 1.45; margin: 0; }
.settings-grid { display: grid; gap: 10px; min-width: 0; }
.settings-card { background: #040c1480; border: 1px solid var(--border); border-radius: 9px; padding: 10px; margin: 0; min-width: 0; }
.overlay-stats { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 6px; }
.overlay-stats span { display: block; color: var(--muted); font: 800 11px/1.2 Consolas,monospace; }
.overlay-stats strong { display: block; color: var(--amber); font: 900 16px/1.3 Consolas,monospace; margin-top: 6px; overflow-wrap: anywhere; }
.difficulty-select { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 6px; }
.difficulty-select legend { color: var(--muted); font-size: 12px; }
.difficulty-select label { min-width: 0; position: relative; cursor: pointer; }
.difficulty-select input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.difficulty-select span { display: grid; gap: 5px; min-height: 60px; padding: 8px; border: 1px solid var(--border); border-radius: 7px; }
.difficulty-select input:checked + span { border-color: var(--green); background: #5dffb122; }
.difficulty-select input:focus-visible + span { outline: 2px solid var(--cyan); }
.difficulty-select small { color: var(--muted); font-size: 11px; }
.level-select { display: grid; grid-template-columns: auto minmax(0,1fr); align-items: center; gap: 8px; font-size: 12px; }
.level-select select { width: 100%; color-scheme: dark; color: var(--text); background-color: #040c14; font-size: 16px; }
.level-select small { grid-column: 1/-1; color: var(--green); }
.settings-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.settings-actions > * { flex: 1 1 95px; min-width: 0; padding-inline: 4px; }
.settings-actions .primary-start { color: var(--green); border-color: var(--green); background: #5dffb122; }
.how-to-overlay { z-index: 20; }
.how-to-card { width: min(100%,860px); grid-template-rows: auto auto minmax(0,1fr); }
.manual-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-width: 0; }
.manual-head > div { min-width: 0; }
.how-to-close { flex: 0 0 auto; }
.manual-tabs { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 6px; }
.manual-slide { overflow: auto; min-height: 0; min-width: 0; overscroll-behavior: contain; }
.how-to-flow, .how-to-grid, .manual-note-grid { display: grid; grid-template-columns: repeat(3,minmax(0,1fr)); gap: 10px; margin-bottom: 10px; }
.how-to-grid { grid-template-columns: repeat(4,minmax(0,1fr)); }
.how-to-flow > div, .how-to-grid article, .manual-note-grid article { border: 1px solid var(--border); border-radius: 8px; background: #040c1499; padding: 12px; min-width: 0; }
.how-to-flow strong, .how-to-grid strong, .manual-note-grid strong { display: block; color: var(--amber); margin-bottom: 6px; font-size: 13px; }
.how-to-flow span, .how-to-grid span, .manual-note-grid span { display: block; color: var(--soft); font-size: 14px; line-height: 1.4; }
.how-to-grid img { display: block; width: 48px; height: 48px; object-fit: contain; margin: 0 auto 10px; }
.manual-section-title { display: grid; gap: 6px; color: var(--soft); font-size: 13px; padding-bottom: 10px; }
.manual-section-title strong { color: var(--amber); }
.countdown-overlay { z-index: 9; display: flex; flex-direction: column; gap: 10px; justify-content: center; pointer-events: none; }
.countdown-ring { width: min(150px,38vh); aspect-ratio: 1; border: 4px solid var(--green); border-radius: 50%; display: grid; place-items: center; background: var(--bg); box-shadow: 0 0 34px var(--shadow); }
.countdown-ring span { font: 900 clamp(36px,8vh,70px)/1 Consolas,monospace; }
.countdown-overlay small { color: var(--soft); }
.countdown-overlay.pulse .countdown-ring { animation: countdownPop .65s ease both; }
.status-boss, .boss-alert .stage { animation: bossPulse .64s ease-in-out infinite alternate; }
.status-shock { color: var(--cyan) !important; } .status-immune { color: #d8ccff !important; }
.game-panel.paused :is(.stage,.status-boss) { animation-play-state: paused; }
@keyframes countdownPop { from { opacity: 0; transform: scale(.8); } to { opacity: 1; transform: scale(1); } }
@keyframes bossPulse { from { box-shadow: 0 0 12px #ff5ec433; } to { box-shadow: 0 0 24px #ff5ec466; } }
@media (max-width: 900px), (max-height: 600px) {
    .game-topbar { gap: 8px; padding: 6px 8px; }
    .game-title p { font-size: 11px; }
    .scoreboard div { min-width: 65px; padding: 5px; }
    .scoreboard strong { font-size: 18px; }
    .play-layout { grid-template-columns: minmax(0,1fr); grid-template-rows: auto minmax(0,1fr); gap: 6px; }
    .side-panel { grid-row: 1; display: flex; gap: 6px; }
    .side-panel > * { flex: 0 0 auto; min-width: 68px; }
    .run-stat { display: flex; gap: 7px; align-items: center; padding: 6px; }
    .run-stat strong { font-size: 16px; margin: 0; }
    .wave-progress { width: 150px; padding: 4px; gap: 2px; }
    .wave-track { height: 4px; }
    .wave-ticks span { height: 3px; }
    .wave-progress strong { font-size: 15px; margin: 0; }
    .arena-wrap { padding: 6px; }
    .stage-column { gap: 5px; }
    .stage-hud small { max-width: none; }
}
@media (max-width: 600px) {
    .game-topbar { grid-template-columns: minmax(0,1fr) auto; }
    .game-title { text-align: left; }
    .game-topbar > .back-link { grid-column: 2; grid-row: 1/3; max-width: 88px; }
    .game-title { grid-column: 1; grid-row: 1; }
    .game-title p { margin-top: 2px; font-size: 11px; }
    .scoreboard { grid-column: 1; grid-row: 2; }
    .scoreboard div { display: flex; gap: 6px; align-items: center; border: 0; padding: 0; min-width: 0; }
    .scoreboard strong { margin: 0; font-size: 15px; }
    .stage-hud { flex-wrap: wrap; gap: 2px; }
    .stage-hud small { width: 100%; font-size: 11px; }
    .controls { padding: 5px; gap: 5px; }
    .controls > button { min-width: 58px; padding: 5px; font-size: 11px; }
    .speed-controls { flex: 1; gap: 3px; overflow-x: auto; overscroll-behavior: contain; }
    .speed-controls button { padding: 5px 2px; font-size: 11px; }
    .overlay-scroll { grid-template-columns: minmax(0,1fr); gap: 10px; }
    .overlay-visual { display: none; }
    .overlay-card, .how-to-card { padding: 10px; gap: 8px; }
    .difficulty-select { grid-template-columns: minmax(0,1fr); }
    .difficulty-select span { min-height: 44px; grid-template-columns: auto minmax(0,1fr); align-items: center; }
    .manual-head { gap: 6px; }
    .manual-head h2 { font-size: 22px; }
    .how-to-flow, .how-to-grid, .manual-note-grid { grid-template-columns: minmax(0,1fr); }
}
@media (max-height: 500px) {
    .game-topbar { padding: 4px 8px; }
    .game-title p { display: none; }
    .game-title h1 { font-size: 18px; }
    .stage-hud { flex-wrap: nowrap; }
    .stage-hud small { width: auto; flex: 0 0 100px; }
    .overlay-card, .how-to-card { padding: 8px; gap: 6px; }
    .manual-head h2 { font-size: 22px; }
    .manual-head .overlay-mode { margin: 0; }
    .settings-actions { gap: 6px; }
}
@media (min-width: 480px) and (max-height: 500px) {
    .game-topbar { grid-template-columns: auto minmax(0,1fr) auto; }
    .game-topbar > .back-link { grid-column: 1; grid-row: 1; max-width: 88px; }
    .game-title { grid-column: 2; grid-row: 1; text-align: center; }
    .scoreboard { grid-column: 3; grid-row: 1; display: grid; gap: 3px; }
    .scoreboard div { display: flex; align-items: center; gap: 5px; border: 0; padding: 0; }
    .scoreboard strong { font-size: 15px; margin: 0; }
}
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
}
.overlay-end .difficulty-select, .overlay-end .level-select { display: none; }
.overlay-end .overlay-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
