/* Base / structural stylesheet. Defines the token vocabulary in :root with
   neutral defaults so the page renders even without a theme file. Every
   color reference below is var(--token) so themes only need to redefine
   tokens and add decoration classes scoped to [data-theme="<id>"]. */

:root {
  /* Surfaces */
  --bg: #111;
  --surface: #1c1c1c;
  --surface-2: #222;
  --surface-hover: #2d2d2d;
  --surface-pressed: #2a3344;
  --overlay-bg: rgba(0, 0, 0, 0.78);

  /* Borders */
  --border: #333;
  --border-strong: #555;
  --border-soft: #2a2a2a;
  --border-row: #262626;

  /* Text */
  --text: #eee;
  --text-muted: #aaa;
  --text-dim: #888;
  --text-accent: #9cf;
  --text-faint: #555;

  /* Brand accents */
  --accent: #9cf;
  --accent-2: #6cf;
  --accent-glow: rgba(156, 204, 255, 0.35);

  /* State colors */
  --success: #a8e8a8;
  --success-bg: #2a3a2a;
  --success-border: #305030;
  --warn: #c9b89a;
  --danger: #c99a9a;
  --danger-bg: #3a2a2a;
  --danger-border: #553030;
  --locked: #c98a6a;

  /* Stat-strip tints */
  --stat-money: #9ac99a;
  --stat-hp-bad: #c99a9a;
  --stat-hp-mid: #c9b89a;
  --stat-hp-ok:  #9ac9a8;
  --stat-wave: #9ab5c9;
  --stat-remaining: #c9b49a;
  --stat-score: #a9aecc;
  --stat-best: #b89ac4;
  --stat-fps: #7a7a7a;
  --stat-fps-mid: #c9b89a;
  --stat-fps-low: #c99a9a;
  --stat-reward: #ffcc66;

  /* Tower panel chevrons */
  --chevron-power: #ffcc44;
  --chevron-speed: #66ddff;
  --chevron-empty: #333;
  --chev-power-text: #ffd980;
  --chev-speed-text: #9bdfff;
  --chev-sell-text: #d8a8a8;

  /* Leaderboard */
  --lb-title: #b89ac4;
  --lb-input: #f0d29a;
  --lb-input-border: #554;
  --lb-caret: #b89ac4;
  --lb-submit-bg: #2a3344;
  --lb-submit-border: #3a4a6a;
  --lb-submit-hover: #3a4a6a;
  --lb-submit-text: #cfe;
  --lb-new-bg: #2a2435;
  --lb-new-fg: #e8d8f0;
  --lb-header-border: #333;
  --lb-empty: #555;
  --scores-btn-fg: #b89ac4;
  --scores-btn-border: #3a2d4a;
  --scores-btn-hover-bg: #2a2030;
  --scores-btn-hover-border: #554060;
  --scores-btn-hover-fg: #d2b4de;

  /* Pause overlay */
  --pause-bg: rgba(10, 14, 10, 0.35);
  --pause-label-bg: rgba(20, 26, 30, 0.6);
  --pause-label-border: rgba(180, 210, 230, 0.4);
  --pause-label-fg: #cfe;

  /* Game controls */
  --ctl-bg: rgba(25, 25, 25, 0.85);
  --ctl-bg-hover: rgba(45, 45, 45, 0.95);
  --ctl-fg: #ccc;
  --ctl-fg-hover: #eee;
  --ctl-pause-on-fg: #a8e8a8;
  --ctl-pause-on-bg: #2a3a2a;
  --ctl-pause-on-border: #305030;

  /* Tower info badge */
  --info-bg: rgba(20, 24, 28, 0.85);
  --info-fg: #9ab;
  --info-border: #555;
  --info-hover-bg: #2a3a4a;
  --info-hover-fg: #cfe;
  --info-hover-border: #789;

  /* Tower-selected outline */
  --tower-selected-outline: var(--accent-2);
  --tower-selected-bg: var(--surface-pressed);

  /* Gameover / new-score titles */
  --title-gameover: #ff8866;
  --title-newscore: #f0d29a;

  /* Radii */
  --r-sm: 4px;
  --r-md: 6px;
  --r-lg: 10px;

  /* Fonts */
  --font-ui: system-ui, sans-serif;
  --font-mono: ui-monospace, 'SF Mono', Menlo, monospace;
  --font-display: system-ui, sans-serif;
  --font-serif: ui-serif, Georgia, serif;

  /* Letter spacing */
  --ls-wide: 0.3px;
  --ls-wider: 1px;
  --ls-widest: 3px;
}

/* ---------- Base document ---------- */

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
}

/* ---------- Top bar (stats + controls, full game width) ---------- */

#top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: var(--game-width, 960px);
  padding: 6px 10px;
  box-sizing: border-box;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  margin-bottom: 6px;
}

#stats {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  justify-content: flex-start;
  gap: 4px 18px;
  font-size: 14px;
  letter-spacing: var(--ls-wide);
  flex: 1 1 auto;
  min-width: 0;
}

#top-controls {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

#stats .stat {
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}

#stats .stat .lbl {
  color: var(--text-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: var(--ls-wider);
  font-family: var(--font-ui);
}

#stats .stat .val {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-size: 18px;
  font-weight: 600;
  color: var(--stat-color, var(--text-muted));
  transition: color 200ms ease;
}

#stats .stat.stat-money { --stat-color: var(--stat-money); }
#stats .stat.stat-hp    { --stat-color: var(--stat-hp-bad); }
#stats .stat.stat-hp.hp-mid { --stat-color: var(--stat-hp-mid); }
#stats .stat.stat-hp.hp-ok  { --stat-color: var(--stat-hp-ok); }
#stats .stat.stat-wave      { --stat-color: var(--stat-wave); }
#stats .stat.stat-remaining { --stat-color: var(--stat-remaining); }
#stats .stat.stat-score     { --stat-color: var(--stat-score); }
#stats .stat.stat-best      { --stat-color: var(--stat-best); }
#stats .stat.stat-fps       { --stat-color: var(--stat-fps); }
#stats .stat.stat-fps .val  { font-size: 14px; font-weight: 500; }
#stats .stat.stat-fps.fps-low { --stat-color: var(--stat-fps-low); }
#stats .stat.stat-fps.fps-mid { --stat-color: var(--stat-fps-mid); }

/* Cheats-active badge in the stats bar */
#cheat-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 7px;
  border-radius: var(--r-sm);
  background: #5c1010;
  border: 1px solid #c03030;
  color: #ff7070;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-family: var(--font-mono);
  user-select: none;
}
#cheat-badge[hidden] { display: none; }

/* Cheat activation toast — created programmatically by cheats.js */
#cheat-toast {
  position: fixed;
  top: 52px;
  left: 50%;
  transform: translateX(-50%) translateY(-6px);
  background: rgba(10, 20, 10, 0.92);
  color: #44ff88;
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.1em;
  padding: 7px 18px;
  border-radius: var(--r-md);
  border: 1px solid #44ff88;
  box-shadow: 0 0 12px rgba(68, 255, 136, 0.35);
  pointer-events: none;
  z-index: 9999;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
}
#cheat-toast.active {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

#stats .stat .val.flash { animation: val-flash 360ms ease-out; }
@keyframes val-flash {
  0%   { filter: brightness(1); }
  20%  { filter: brightness(1.6); }
  100% { filter: brightness(1); }
}

/* ---------- Board / layout ---------- */

#board {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: 12px;
  padding: 8px 8px 12px;
}

#game-column {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0;
}

#game-wrap { position: relative; }

#game-wrap.daily-active {
  box-shadow: 0 0 0 2px #ffcc44, 0 0 18px 5px rgba(255, 204, 68, 0.3);
  animation: daily-glow-pulse 2.6s ease-in-out infinite;
}
@keyframes daily-glow-pulse {
  0%, 100% { box-shadow: 0 0 0 2px #ffcc44, 0 0 14px 4px rgba(255, 204, 68, 0.2); }
  50%       { box-shadow: 0 0 0 3px #ffcc44, 0 0 28px 10px rgba(255, 204, 68, 0.5); }
}

#daily-btn:not(.daily-done) {
  color: #44dd88;
}

#daily-btn.daily-done {
  min-width: unset;
  padding: 4px 8px;
  color: #44dd88;
  background: transparent;
  border-color: rgba(68, 221, 136, 0.35);
  font-size: 14px;
  cursor: default;
}

/* Explicit dimensions on #game give Phaser.Scale.FIT a measurable parent.
   On desktop these match the internal resolution so the canvas renders
   pixel-perfect; touch-mode overrides further down shrink the box to fit
   narrow viewports and let FIT scale the canvas to match. */
#game {
  width: var(--game-width, 960px);
  height: var(--game-height, 640px);
  max-width: 100vw;
}

#right-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 220px;
}

/* ---------- Tower picker ---------- */

#picker {
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 8px;
  max-height: var(--game-height, 640px);
  overflow-y: auto;
  position: relative;
}

#picker .category-header {
  font-size: 11px;
  letter-spacing: var(--ls-wider);
  text-transform: uppercase;
  color: var(--accent);
  padding: 8px 4px 2px;
  border-bottom: 1px solid var(--border-soft);
  margin-bottom: 2px;
  font-family: var(--font-ui);
}
#picker .category-header:first-child { padding-top: 2px; }

.tower-btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  padding: 6px 8px;
  font: inherit;
  cursor: pointer;
  text-align: left;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  width: 100%;
  position: relative;
}
.tower-btn:hover { background: var(--surface-hover); }
.tower-btn.selected {
  outline: 2px solid var(--tower-selected-outline);
  background: var(--tower-selected-bg);
}
.tower-btn.disabled { opacity: 0.4; cursor: not-allowed; }
.tower-btn.locked { opacity: 0.35; cursor: not-allowed; filter: grayscale(0.6); }
.tower-btn.locked:hover { background: var(--surface-2); }

.tower-btn .name {
  font-weight: 600;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tower-btn .cost {
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
  font-family: var(--font-mono);
}
.tower-btn .info {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid var(--info-border);
  color: var(--info-fg);
  background: var(--info-bg);
  font-size: 10px;
  line-height: 1;
  font-family: var(--font-serif);
  font-style: italic;
  font-weight: 700;
  cursor: help;
  flex-shrink: 0;
  margin-left: auto;
}
.tower-btn .info:hover {
  color: var(--info-hover-fg);
  border-color: var(--info-hover-border);
  background: var(--info-hover-bg);
}
.tower-btn .swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

#picker .category-header.locked { color: var(--text-faint); }
#picker .category-header .unlock-tag {
  color: var(--locked);
  font-size: 10px;
  letter-spacing: 0.5px;
  margin-left: 6px;
}

/* ---------- Sliders (standalone + inside settings) ---------- */

#sliders,
#settings-sliders {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#sliders {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 14px;
  border-radius: var(--r-lg);
}

#sliders h3 {
  margin: 0 0 4px;
  font-size: 14px;
  color: var(--accent);
  letter-spacing: 0.5px;
}
.slider-row { display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.slider-row .row-top { display: flex; justify-content: space-between; }
.slider-row input[type=range] { width: 100%; accent-color: var(--accent); }
.slider-row input[type=range]:disabled { opacity: 0.45; cursor: not-allowed; }
.auto-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  user-select: none;
}
.mode-label { font-size: 12px; color: var(--accent); margin-top: -6px; }
.slider-readout { color: var(--accent); font-variant-numeric: tabular-nums; font-family: var(--font-mono); }
#reward-info {
  font-size: 12px;
  color: var(--text);
  border-top: 1px solid var(--border);
  padding-top: 10px;
}
#reward-info .val { color: var(--stat-reward); font-weight: 600; font-family: var(--font-mono); }
.settings-hint { font-size: 11px; color: var(--text-dim); }

/* ---------- Top-bar controls (pause / settings / scores) ---------- */

#top-controls .ctl-btn {
  background: var(--ctl-bg);
  color: var(--ctl-fg);
  border: 1px solid var(--border);
  border-radius: var(--r-sm);
  padding: 4px 10px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  letter-spacing: var(--ls-wide);
}
#top-controls .ctl-btn:hover {
  background: var(--ctl-bg-hover);
  border-color: var(--border-strong);
  color: var(--ctl-fg-hover);
}
#top-controls #pause-btn[aria-pressed="true"] {
  background: var(--ctl-pause-on-bg);
  color: var(--ctl-pause-on-fg);
  border-color: var(--ctl-pause-on-border);
}
#top-controls #scores-btn {
  color: var(--scores-btn-fg);
  border-color: var(--scores-btn-border);
}
#top-controls #scores-btn:hover {
  background: var(--scores-btn-hover-bg);
  border-color: var(--scores-btn-hover-border);
  color: var(--scores-btn-hover-fg);
}

/* ---------- Right panel: "Towers" header above the picker ---------- */

#right-panel .panel-title {
  font-size: 13px;
  letter-spacing: var(--ls-widest);
  text-transform: uppercase;
  color: var(--accent);
  padding: 0 2px 4px;
  font-family: var(--font-display);
  font-weight: 700;
}

/* ---------- Music widget (floats bottom-right of the canvas) ---------- */
/* Left button (♪) opens a popup with Music + SFX sliders.
 * Right button (🔊) toggles master mute. */

#music-strip {
  position: absolute;
  right: 10px;
  bottom: 10px;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 6px;
  background: color-mix(in srgb, var(--surface) 78%, transparent);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  font-family: var(--font-ui);
  color: var(--text);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

#music-strip .ms-btn {
  background: transparent;
  color: var(--text);
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  width: 28px;
  height: 28px;
  padding: 0;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
}
#music-strip .ms-btn:hover {
  background: var(--surface-hover);
  border-color: var(--border-strong);
}
/* Muted / off state: danger tint on the speaker */
#music-strip #sound-mute[aria-pressed="true"] {
  color: var(--danger);
}
/* Music note button highlighted when popup is open */
#music-strip #music-toggle[aria-expanded="true"] {
  background: var(--surface-hover);
  border-color: var(--accent);
}

/* Popup panel: floats above the strip, aligned to the right edge */
#music-strip .ms-popup {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  min-width: 190px;
  background: color-mix(in srgb, var(--surface) 95%, transparent);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(6px);
  transition: opacity 160ms ease, transform 160ms ease;
}
#music-strip[data-popup-open="true"] .ms-popup {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.ms-popup-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.ms-popup-label {
  font-size: 12px;
  color: var(--text-muted, var(--text));
  width: 36px;
  flex-shrink: 0;
  user-select: none;
}
.ms-popup-slider {
  flex: 1;
  height: 6px;
  appearance: none;
  background: var(--border);
  border-radius: 3px;
  outline: none;
  cursor: pointer;
  margin: 0;
}
.ms-popup-slider::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}
.ms-popup-slider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  border: none;
  cursor: pointer;
}

/* ---------- Pause overlay ---------- */

#daily-banner {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 8;
  background: rgba(20, 10, 0, 0.82);
  border-bottom: 1px solid rgba(255, 204, 68, 0.4);
  color: #ffcc44;
  font-size: 11px;
  letter-spacing: 0.04em;
  text-align: center;
  padding: 4px 8px;
  pointer-events: none;
}
#pause-overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  background: var(--pause-bg);
  z-index: 10;
}
#pause-overlay.open { display: flex; }
#pause-overlay .label {
  font-size: 28px;
  letter-spacing: 6px;
  color: var(--pause-label-fg);
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.6);
  padding: 8px 24px;
  border: 1px solid var(--pause-label-border);
  border-radius: var(--r-md);
  background: var(--pause-label-bg);
  font-family: var(--font-display);
}

/* ---------- Settings overlay / panel ---------- */

.settings-section { margin-bottom: 16px; }
.settings-section h3 {
  margin: 0 0 8px;
  font-size: 13px;
  color: var(--accent);
  letter-spacing: var(--ls-wider);
  text-transform: uppercase;
  font-family: var(--font-ui);
}
.settings-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 13px;
}
.settings-row .toggle-btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  padding: 4px 12px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  min-width: 72px;
}
.settings-row .toggle-btn[aria-pressed="true"] {
  background: var(--ctl-pause-on-bg);
  color: var(--ctl-pause-on-fg);
  border-color: var(--ctl-pause-on-border);
}
.settings-row .toggle-btn:hover { background: var(--surface-hover); }
#settings-sliders .slider-row { margin-bottom: 10px; }
#settings-sliders .auto-row { margin-bottom: 8px; }
#settings-sliders .mode-label { margin: -4px 0 8px; }

/* Terrain size group inherits toggle-btn styling */
#terrain-size-group { display: flex; gap: 4px; flex-wrap: wrap; }

/* Cheat code reference table in Settings */
.cheat-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 6px;
  font-size: 12px;
}
.cheat-table td {
  padding: 4px 6px;
  border-bottom: 1px solid var(--border-row);
  color: var(--text-muted);
  vertical-align: middle;
}
.cheat-table tr:last-child td { border-bottom: none; }
.cheat-table .cheat-code {
  font-family: var(--font-mono);
  font-weight: 700;
  color: #44ff88;
  letter-spacing: 0.06em;
  white-space: nowrap;
  padding-right: 14px;
}

/* Audio mixer */
#audio-mixer .mix-row {
  display: grid;
  grid-template-columns: 70px 1fr 36px 42px 44px;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
  font-size: 12px;
}
#audio-mixer .mix-label { color: var(--text); }
#audio-mixer input[type="range"] { width: 100%; accent-color: var(--accent); }
#audio-mixer .mix-readout {
  color: var(--text-muted);
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-family: var(--font-mono);
}
#audio-mixer .mix-toggle,
#audio-mixer .mix-test {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
  padding: 3px 0;
  font: inherit;
  font-size: 11px;
  cursor: pointer;
}
#audio-mixer .mix-toggle[aria-pressed="false"] {
  background: var(--danger-bg);
  color: var(--danger);
  border-color: var(--danger-border);
}
#audio-mixer .mix-toggle:hover,
#audio-mixer .mix-test:hover { background: var(--surface-hover); }

/* Theme picker (settings section) */
#theme-picker {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 4px;
}
#theme-picker .theme-card {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  padding: 10px 12px;
  font: inherit;
  font-size: 12px;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: border-color 140ms ease, background 140ms ease;
}
#theme-picker .theme-card:hover { background: var(--surface-hover); border-color: var(--border-strong); }
#theme-picker .theme-card[aria-pressed="true"] {
  outline: 2px solid var(--tower-selected-outline);
  background: var(--tower-selected-bg);
  border-color: var(--tower-selected-outline);
}
#theme-picker .theme-name {
  font-weight: 700;
  font-size: 13px;
  letter-spacing: var(--ls-wide);
  color: var(--text);
}
#theme-picker .theme-desc {
  color: var(--text-muted);
  font-size: 11px;
  line-height: 1.35;
}
#theme-picker .theme-swatches {
  display: flex;
  gap: 4px;
  margin-top: 2px;
}
#theme-picker .theme-swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid var(--border-strong);
}

/* ---------- Leaderboard / game-over ---------- */

#lb-panel .lb-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin: 0 auto 8px;
}
#lb-panel .lb-skip {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border-strong);
  border-radius: var(--r-md);
  padding: 6px 18px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
#lb-panel .lb-skip:hover { color: var(--text); border-color: var(--text-dim); }

#lb-overlay,
#settings-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}
#lb-overlay.open,
#settings-overlay.open { display: flex; }

#settings-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 22px 26px;
  min-width: 340px;
  max-width: 90vw;
  color: var(--text);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  position: relative;
}
#settings-panel h2 {
  margin: 0 0 16px;
  font-size: 18px;
  color: var(--accent);
  letter-spacing: var(--ls-wider);
  text-align: center;
  font-family: var(--font-display);
}

#lb-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 22px 26px;
  min-width: 420px;
  max-width: 90vw;
  color: var(--text);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  position: relative;
}
#lb-panel h2 {
  margin: 0 0 12px;
  font-size: 20px;
  color: var(--lb-title);
  letter-spacing: var(--ls-wider);
  text-align: center;
  font-family: var(--font-display);
}
#lb-panel h2.lb-gameover-title { color: var(--title-gameover); }
#lb-panel h2.lb-newscore-title { color: var(--title-newscore); }
#lb-panel .lb-stats {
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 14px;
}
#lb-panel .lb-entry {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 10px 0 16px;
}
#lb-panel .lb-entry input {
  width: 36px;
  height: 42px;
  background: var(--bg);
  color: var(--lb-input);
  border: 1px solid var(--lb-input-border);
  border-radius: var(--r-md);
  font-family: var(--font-mono);
  font-size: 22px;
  font-weight: 700;
  text-align: center;
  text-transform: uppercase;
  caret-color: var(--lb-caret);
}
#lb-panel .lb-entry input:focus { outline: 2px solid var(--lb-caret); }
#lb-panel .lb-submit {
  display: block;
  margin: 0 auto 14px;
  background: var(--lb-submit-bg);
  color: var(--lb-submit-text);
  border: 1px solid var(--lb-submit-border);
  border-radius: var(--r-md);
  padding: 6px 18px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
#lb-panel .lb-submit:hover { background: var(--lb-submit-hover); }
#lb-panel table { width: 100%; border-collapse: collapse; font-size: 13px; }
#lb-panel th,
#lb-panel td { padding: 4px 8px; text-align: left; }
#lb-panel th {
  color: var(--text-dim);
  font-weight: 500;
  font-size: 11px;
  letter-spacing: var(--ls-wider);
  text-transform: uppercase;
  border-bottom: 1px solid var(--lb-header-border);
  font-family: var(--font-ui);
}
#lb-panel td {
  border-bottom: 1px solid var(--border-row);
  font-variant-numeric: tabular-nums;
  font-family: var(--font-mono);
}
#lb-panel tr.new td { background: var(--lb-new-bg); color: var(--lb-new-fg); }
#lb-panel tr.empty td { color: var(--lb-empty); }
#lb-panel .lb-footer {
  margin-top: 14px;
  text-align: center;
  font-size: 12px;
  color: var(--text-dim);
}
#lb-panel .lb-close {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 12px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  display: block;
  margin: 10px auto 0;
}
#lb-panel .lb-close:hover { color: var(--text); border-color: var(--border-strong); }

/* ---------- Tower detail panel ---------- */

#tower-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 10px 12px;
  color: var(--text);
  font-size: 12px;
  display: none;
  position: relative;
}
#tower-panel .tp-header {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--lb-submit-text);
  font-family: var(--font-display);
}
#tower-panel .tp-tiers {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 8px;
  font-size: 11px;
  color: var(--text-dim);
}
#tower-panel .tp-tier-label { letter-spacing: var(--ls-wider); }
#tower-panel .tp-chevs {
  display: inline-flex;
  gap: 3px;
  margin-left: 4px;
  vertical-align: middle;
}
#tower-panel .tp-chev {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  display: inline-block;
  background: var(--chevron-empty);
}
#tower-panel .tp-chev.tp-chev-power.filled { background: var(--chevron-power); }
#tower-panel .tp-chev.tp-chev-speed.filled { background: var(--chevron-speed); }
#tower-panel .tp-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 12px;
  margin-bottom: 10px;
}
#tower-panel .tp-stats div {
  display: flex;
  justify-content: space-between;
  font-variant-numeric: tabular-nums;
}
#tower-panel .tp-lbl { color: var(--text-dim); font-size: 11px; }
#tower-panel .tp-val { color: var(--text); font-size: 12px; font-family: var(--font-mono); }
#tower-panel .tp-actions { display: flex; flex-direction: column; gap: 4px; }
#tower-panel .tp-btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  padding: 5px 8px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
}
#tower-panel .tp-btn:hover:not(:disabled) {
  background: var(--surface-hover);
  border-color: var(--border-strong);
}
#tower-panel .tp-btn:disabled { opacity: 0.5; cursor: not-allowed; }
#tower-panel .tp-btn.tp-power  { color: var(--chev-power-text); }
#tower-panel .tp-btn.tp-speed  { color: var(--chev-speed-text); }
#tower-panel .tp-btn.tp-sell   { color: var(--chev-sell-text); }
#tower-panel .tp-btn.tp-evolve {
  color: #00eeff;
  border-color: #00eeff;
  background: rgba(0,238,255,0.08);
  font-weight: 700;
  letter-spacing: 0.04em;
}
#tower-panel .tp-btn.tp-evolve:hover:not(:disabled) {
  background: rgba(0,238,255,0.18);
}
#tower-panel .tp-btn.tp-global { color: #ffcc44; }
#tower-panel .tp-evolved-badge {
  display: inline-block;
  margin-left: 6px;
  font-size: 9px;
  letter-spacing: 0.1em;
  background: rgba(0,238,255,0.15);
  border: 1px solid rgba(0,238,255,0.5);
  border-radius: 3px;
  padding: 1px 5px;
  color: #00eeff;
  vertical-align: middle;
}
#tower-panel .tp-cost {
  font-family: var(--font-mono);
  font-size: 11px;
  opacity: 0.85;
}

/* ---------- Tutorial overlay ---------- */

#tutorial-overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-bg);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2500;
}
#tutorial-overlay.open { display: flex; }

#tutorial-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 26px 30px 20px;
  max-width: 560px;
  width: min(560px, 92vw);
  max-height: 90vh;
  overflow-y: auto;
  color: var(--text);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
  position: relative;
  font-family: var(--font-ui);
}

#tutorial-panel h2 {
  margin: 0 0 10px;
  font-size: 22px;
  color: var(--accent);
  letter-spacing: var(--ls-widest);
  text-align: center;
  font-family: var(--font-display);
}

#tutorial-panel .tut-lede {
  margin: 0 0 18px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  line-height: 1.45;
}

#tutorial-panel .tut-close {
  position: absolute;
  top: 6px;
  right: 10px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--r-sm);
}
#tutorial-panel .tut-close:hover { color: var(--text); background: var(--surface-hover); }

.tut-steps {
  list-style: none;
  padding: 0;
  margin: 0 0 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.tut-steps li {
  display: flex;
  gap: 12px;
  font-size: 13px;
  line-height: 1.45;
  color: var(--text);
}
.tut-steps li strong { color: var(--text); }
.tut-steps li em { color: var(--accent); font-style: normal; }
.tut-step-n {
  flex-shrink: 0;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--surface-pressed);
  color: var(--accent);
  border: 1px solid var(--accent-glow);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-family: var(--font-mono);
  font-size: 12px;
}

.tut-hint {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  margin: 10px 0 16px;
}
.tut-hint kbd {
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 3px;
  padding: 1px 5px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text);
}

.tut-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding-top: 8px;
  border-top: 1px solid var(--border-soft);
}

.tut-dontshow {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}
.tut-dontshow input { accent-color: var(--accent); }

.tut-go {
  background: var(--lb-submit-bg);
  color: var(--lb-submit-text);
  border: 1px solid var(--lb-submit-border);
  border-radius: var(--r-md);
  padding: 8px 18px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  letter-spacing: var(--ls-wide);
}
.tut-go:hover { background: var(--lb-submit-hover); }

/* ---------- Game-over celebration ---------- */

#lb-panel .lb-celebration-rank1 {
  color: var(--title-newscore);
  animation: celebrate-pulse 1.8s ease-in-out infinite;
  text-shadow: 0 0 12px var(--accent-glow);
}
#lb-panel .lb-celebration-top3 {
  color: var(--lb-title);
  animation: celebrate-pulse 2.4s ease-in-out infinite;
}
@keyframes celebrate-pulse {
  0%, 100% { filter: brightness(1); transform: scale(1); }
  50%      { filter: brightness(1.3); transform: scale(1.03); }
}

#lb-panel .lb-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px 12px;
  margin: 0 auto 14px;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--r-md);
  max-width: 420px;
}
#lb-panel .lb-summary .lb-summary-item {
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
#lb-panel .lb-summary .lb-summary-lbl {
  font-size: 10px;
  letter-spacing: var(--ls-wider);
  text-transform: uppercase;
  color: var(--text-dim);
}
#lb-panel .lb-summary .lb-summary-val {
  font-family: var(--font-mono);
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
#lb-panel .lb-summary .lb-summary-val.highlight {
  color: var(--accent);
}

/* --- Side-by-side personal + global tables --- */
#lb-panel .lb-tables-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 16px;
  margin-top: 4px;
}

/* --- Play Again button in game-over footer --- */
#lb-panel .lb-footer {
  display: flex;
  justify-content: center;
}
#lb-panel .lb-play-again {
  background: var(--lb-submit-bg);
  color: var(--lb-submit-text);
  border: 1px solid var(--lb-submit-border);
  border-radius: var(--r-md);
  padding: 7px 28px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: var(--ls-wide);
  cursor: pointer;
}
#lb-panel .lb-play-again:hover { background: var(--lb-submit-hover); }

/* --- Game-over name entry (three character boxes) --- */
#lb-panel .lb-name-section {
  border-top: 1px solid var(--border-soft);
  margin: 4px 0 0;
  padding: 14px 0 8px;
  text-align: center;
}
#lb-panel .lb-name-label {
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: var(--ls-wider);
  text-transform: uppercase;
  color: var(--lb-caret);
  margin-bottom: 3px;
}
#lb-panel .lb-name-sub {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 10px;
}

/* --- Global high scores section --- */
#lb-panel .lb-global-section {
  border-top: 1px solid var(--border-soft);
  margin-top: 4px;
  padding-top: 10px;
}
#lb-panel .lb-global-title {
  font-family: var(--font-display);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: var(--ls-wider);
  text-transform: uppercase;
  color: var(--text-dim);
  text-align: center;
  margin: 0 0 8px;
}
#lb-panel tr.dl-own td {
  background: var(--lb-new-bg);
  color: var(--lb-new-fg);
}

/* Scrollable panel when game-over content is tall */
#lb-panel { max-height: 90vh; overflow-y: auto; }

/* ---------- Floating touch placement Confirm / Cancel ------------------- */
/* Sit over the canvas (inside #game-wrap which is position:relative). The
   buttons are hidden by default — GameScene toggles display:flex on the
   ghost-active branch. On desktop they simply never appear. */

#touch-place {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 30;
}
#touch-place .tp-touch-btn {
  position: absolute;
  display: none;
  width: 44px;
  height: 44px;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  font-weight: 700;
  line-height: 1;
  border-radius: 50%;
  border: 2px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  pointer-events: auto;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
  transition: transform 120ms ease;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
}
#touch-place .tp-touch-btn:active { transform: scale(0.92); }
#touch-place .tp-touch-confirm {
  color: var(--success);
  border-color: var(--success-border);
  background: var(--success-bg);
}
#touch-place .tp-touch-cancel {
  color: var(--danger);
  border-color: var(--danger-border);
  background: var(--danger-bg);
}

/* ---------- Settings touch-mode group ----------------------------------- */
/* Same button styling as terrain-size-group; just a labelled row. */

#touch-mode-group {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
#touch-mode-group .toggle-btn {
  min-width: 56px;
}

/* ---------- Touch mode: scoped overrides -------------------------------- */
/* Two layers here:
   1. `body.touch-mode` (any viewport size) — pointer-behavior tweaks and
      hit-target bumps that are always safe to apply. iPad in landscape keeps
      the desktop side-panel layout, just with tappable buttons.
   2. `body.touch-mode` + `@media (max-width: 900px)` — the layout flip to a
      bottom-sheet picker + column stack. Kicks in only on phones / iPad
      portrait / any narrow window where the side panel would crowd the map.
   Desktop (no touch-mode class) is untouched by both layers. */

body.touch-mode {
  touch-action: manipulation;
  overscroll-behavior: none;
}

/* Prevent iOS rubber-band scroll from the game canvas */
body.touch-mode #game-wrap,
body.touch-mode #game canvas {
  touch-action: none;
}

/* Tap-target bumps that are safe on both side-panel and bottom-sheet layouts. */
body.touch-mode .tower-btn {
  min-height: 42px;
  padding: 8px 10px;
}
body.touch-mode .tower-btn .info {
  width: 22px;
  height: 22px;
  font-size: 12px;
}
body.touch-mode #tower-panel .tp-btn {
  min-height: 38px;
  padding: 8px 10px;
}
body.touch-mode #top-controls .ctl-btn {
  min-height: 34px;
  min-width: 40px;
  padding: 6px 12px;
  font-size: 13px;
}

/* Settings / leaderboard modals: tap-friendly buttons at all sizes. */
body.touch-mode .lb-close,
body.touch-mode .tut-close,
body.touch-mode .tut-go {
  min-height: 40px;
}

/* ---------- Narrow-viewport bottom sheet (phones + iPad portrait) ------- */
/* Below 900px the side panel crowds the canvas, so we flip to a bottom
   sheet. 900px was picked so iPad landscape (1194px) stays on the
   side-panel layout — confirmed best UX per user feedback. */
@media (max-width: 900px) {
  body.touch-mode {
    overflow: hidden;
    height: 100dvh;
    width: 100vw;
  }

  body.touch-mode #board {
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    padding: max(4px, env(safe-area-inset-top)) max(4px, env(safe-area-inset-right)) 4px max(4px, env(safe-area-inset-left));
  }

  body.touch-mode #top-bar {
    width: 100%;
    max-width: 100%;
  }

  body.touch-mode #game-column {
    width: 100%;
    align-items: center;
  }

  body.touch-mode #game {
    width: 100vw;
    height: auto;
    max-width: 100vw;
    aspect-ratio: var(--game-aspect, 3 / 2);
    max-height: calc(100dvh - 150px);
    margin: 0 auto;
  }

  /* Bottom sheet. The .panel-title doubles as the drag handle — tapping it
     toggles `.collapsed` on #right-panel via hud.js. */
  body.touch-mode #right-panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-height: 60vh;
    max-height: 60dvh;
    z-index: 50;
    background: var(--surface);
    border-top: 1px solid var(--border);
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    padding: 0 8px max(10px, env(safe-area-inset-bottom));
    transform: translateY(0);
    transition: transform 220ms ease;
    overflow: hidden;
    box-shadow: 0 -6px 20px rgba(0, 0, 0, 0.45);
    display: flex;
    flex-direction: column;
    gap: 4px;
  }

  body.touch-mode #right-panel.collapsed {
    transform: translateY(calc(100% - 48px));
  }

  body.touch-mode #right-panel .panel-title {
    height: 40px;
    padding: 6px 2px 2px;
    font-size: 12px;
    text-align: center;
    cursor: pointer;
    user-select: none;
    position: relative;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  body.touch-mode #right-panel .panel-title::before {
    content: '';
    position: absolute;
    top: 5px;
    left: 50%;
    transform: translateX(-50%);
    width: 44px;
    height: 4px;
    border-radius: 2px;
    background: var(--border-strong);
  }

  body.touch-mode #picker {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    gap: 6px;
    max-height: unset;
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 6px;
    background: transparent;
    border: none;
  }

  body.touch-mode #picker .category-header {
    grid-column: 1 / -1;
    padding: 6px 4px 2px;
  }

  /* Bottom-sheet-only: tiles become taller cards in a grid. */
  body.touch-mode .tower-btn {
    min-height: 56px;
    padding: 10px 10px;
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
    position: relative;
  }
  body.touch-mode .tower-btn .name {
    font-size: 13px;
    padding-right: 32px;
  }
  body.touch-mode .tower-btn .cost {
    font-size: 13px;
  }
  body.touch-mode .tower-btn .info {
    width: 28px;
    height: 28px;
    font-size: 14px;
    position: absolute;
    top: 6px;
    right: 6px;
    margin-left: 0;
  }

  body.touch-mode #tower-panel {
    flex-shrink: 0;
    margin: 0;
  }
  body.touch-mode #tower-panel .tp-btn {
    min-height: 44px;
    padding: 10px 12px;
    font-size: 13px;
  }

  body.touch-mode #top-controls .ctl-btn {
    min-height: 40px;
    min-width: 44px;
    padding: 6px 10px;
  }

  body.touch-mode #stats {
    font-size: 12px;
    gap: 2px 12px;
  }
  body.touch-mode #stats .stat .val { font-size: 15px; }
  body.touch-mode #stats .stat.stat-fps,
  body.touch-mode #stats .stat.stat-best { display: none; }

  body.touch-mode #settings-panel,
  body.touch-mode #lb-panel {
    max-height: 88dvh;
    overflow-y: auto;
    min-width: 0;
    width: min(92vw, 440px);
    padding: 18px 16px;
  }
  body.touch-mode .lb-close,
  body.touch-mode .tut-close,
  body.touch-mode .tut-go {
    min-height: 44px;
  }
}

/* Sprite Studio embeds QTD at ?preview=sphere to drive the procedural-sphere
   picker. Hide HUD chrome + overlays but keep the #board > ... > #game tree
   rendering, otherwise the canvas disappears along with its parents. The
   `html.preview-mode` variant is set by an inline <head> script so the CSS
   applies from the first paint — no flash of HUD before main.js boots. */
html.preview-mode, html.preview-mode body {
  background: #1a1d24;
  margin: 0;
  overflow: hidden;
}
html.preview-mode #top-bar,
html.preview-mode #right-panel,
html.preview-mode #pause-overlay,
html.preview-mode #music-strip,
html.preview-mode #touch-place,
html.preview-mode #lb-overlay,
html.preview-mode #tutorial-overlay,
html.preview-mode #settings-panel {
  display: none !important;
}
html.preview-mode #board { display: block; grid-template: none; width: 100vw; height: 100vh; margin: 0; }
html.preview-mode #game-column { width: 100%; height: 100%; }
html.preview-mode #game-wrap { width: 100%; height: 100%; }
html.preview-mode #game { width: 100% !important; height: 100% !important; }
html.preview-mode #game canvas {
  width: 100% !important;
  height: 100% !important;
  display: block;
}
