:root {
  color-scheme: dark;
  --bg: #090d1d;
  --panel: #1a2030;
  --text: #f4f1e8;
  --muted: #b8b3a6;
  --accent: #e6bd3e;
  --danger: #111;
  --sky-top: #090d1d;
  --sky-bottom: #2b2a35;
  /* "Nachtlinie"-Identitaet: Farben aus der Spielwelt (Canvas-Himmel, Muenz-Gold,
     Wiener-Linien-Rot der Bim, Amber der Zielanzeigen) */
  --bim-rot: #cf2428;
  --led-amber: #ffb63d;
  --panel-line: #2b3352;
  --panel-deep: #0b101f;
  --font-display: "Staatliches", "Arial Narrow", Arial, sans-serif;
  --font-body: "Atkinson Hyperlegible", Arial, Helvetica, sans-serif;
  --font-led: "VT323", "Courier New", monospace;
}

* {
  box-sizing: border-box;
}

html {
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  min-height: 100vh;
  /* dezenter Nachthimmel hinter dem Spielfeld statt flacher Flaeche */
  background: radial-gradient(120% 90% at 50% 0%, #111838 0%, var(--bg) 58%) fixed;
  color: var(--text);
  font-family: var(--font-body);
  /* Mobile: kein Doppeltipp-Zoom (iOS ignoriert user-scalable), kein Pull-to-Refresh */
  touch-action: manipulation;
  overscroll-behavior: none;
  /* Spiel-Seite: nichts ist markierbar — verhindert, dass hektisches Tippen
     neben den Bedienfeldern die ganze Seite blau selektiert (Doppeltipp/Long-Press) */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.game-shell {
  width: min(1180px, 100%);
  margin: 0 auto;
  padding: 12px;
}

.topbar {
  display: flex;
  align-items: end;
  justify-content: flex-end;
  gap: 16px;
  margin-bottom: 8px;
}

h1 {
  margin: 0;
  font-size: clamp(1.45rem, 3vw, 2.2rem);
  letter-spacing: 0;
}

p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.stats {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

/* HUD als Bim-Zielanzeige: dunkle Tafel, Versalien-Label, LED-Ziffern in Amber */
.stats span {
  min-width: 96px;
  padding: 6px 10px 9px;
  background: linear-gradient(180deg, #10162b, var(--panel-deep));
  border: 1px solid var(--panel-line);
  border-radius: 6px;
  text-align: center;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 2px 10px rgba(0, 0, 0, 0.35);
  position: relative;
  font-family: var(--font-display);
  font-size: 0.86rem;
  letter-spacing: 0.09em;
  color: var(--muted);
}

.stats strong {
  color: var(--led-amber);
  font-family: var(--font-led);
  font-weight: 400;
  font-size: 1.34em;
  line-height: 1;
  text-shadow: 0 0 7px rgba(255, 182, 61, 0.5);
}

.stats span::before {
  content: "";
  width: 11px;
  height: 11px;
  border: 1px solid rgba(244, 241, 232, 0.72);
  box-shadow: 0 0 10px currentColor;
}

.stat-score::before {
  border-radius: 50%;
  background: #e6bd3e;
  color: #e6bd3e;
}

.stat-combo::before {
  background: #ff5fb7;
  color: #ff5fb7;
  clip-path: polygon(50% 0, 62% 34%, 98% 34%, 69% 55%, 80% 92%, 50% 70%, 20% 92%, 31% 55%, 2% 34%, 38% 34%);
}

.stat-best::before {
  background: #f4f1e8;
  color: #f4f1e8;
  clip-path: polygon(50% 0, 62% 28%, 92% 28%, 68% 47%, 78% 78%, 50% 60%, 22% 78%, 32% 47%, 8% 28%, 38% 28%);
}

.stat-life::before {
  background: #d84235;
  color: #d84235;
  clip-path: polygon(50% 86%, 8% 42%, 8% 18%, 28% 8%, 50% 24%, 72% 8%, 92% 18%, 92% 42%);
}

.stat-time::before {
  border-radius: 50%;
  background: #9fc7df;
  color: #9fc7df;
}

.stat-spray::before {
  background: #7fdfff;
  color: #7fdfff;
  clip-path: polygon(22% 8%, 78% 8%, 78% 28%, 66% 28%, 66% 92%, 34% 92%, 34% 28%, 22% 28%);
}

.stat-blue::before {
  border-radius: 50%;
  background: #245fba;
  color: #4aa3ff;
}

.stat-jump::before {
  background: #9ff5b4;
  color: #9ff5b4;
  clip-path: polygon(50% 0, 98% 82%, 64% 82%, 64% 100%, 36% 100%, 36% 82%, 2% 82%);
}

.stat-level::before {
  background: #e53935;
  color: #e53935;
}

.stat-distance::before {
  background: #cf2428;
  color: #cf2428;
  clip-path: polygon(0 38%, 58% 38%, 58% 14%, 100% 50%, 58% 86%, 58% 62%, 0 62%);
}

.stat-mode::before {
  background: #111;
  color: #ff685f;
  clip-path: polygon(50% 0, 92% 26%, 82% 88%, 50% 100%, 18% 88%, 8% 26%);
}

.stat-audio::before {
  background: #f4f1e8;
  color: #f4f1e8;
  clip-path: polygon(0 34%, 32% 34%, 66% 8%, 66% 92%, 32% 66%, 0 66%);
}

.meter {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 4px;
  height: 3px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(244, 241, 232, 0.14);
}

.meter i {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: inherit;
  background: currentColor;
  transition: width 120ms linear;
}

.stat-spray .meter i {
  color: #7fdfff;
}

.stat-blue .meter i {
  color: #4aa3ff;
}

.stage-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  border: 2px solid #343b52;
  border-radius: 8px;
  background: linear-gradient(var(--sky-top), var(--sky-bottom));
  box-shadow: 0 18px 45px rgba(0, 0, 0, 0.42), inset 0 0 0 1px rgba(255, 255, 255, 0.04);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: crisp-edges;
}

body.recording-mode {
  overflow: hidden;
}

body.recording-mode .game-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 0;
}

body.recording-mode .topbar,
body.recording-mode .progress,
body.recording-mode .controls {
  display: none;
}

body.recording-mode .stage-wrap {
  width: min(100vw, calc(100vh * 9 / 16));
  max-width: 1080px;
  height: min(100vh, calc(100vw * 16 / 9));
  max-height: 1920px;
  aspect-ratio: 9 / 16;
  border-radius: 0;
  border: 0;
  box-shadow: none;
}

/* Fortschritt als Linien-Route: die Fahrt zur Bim, mit Haltestellen-Punkten */
.progress {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 10px;
  height: 9px;
  border: 1px solid rgba(244, 241, 232, 0.4);
  border-radius: 8px;
  background: rgba(8, 10, 18, 0.55);
  overflow: hidden;
  pointer-events: none;
}

#progressBar {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, var(--bim-rot), var(--led-amber));
}

.progress::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: radial-gradient(circle, rgba(244, 241, 232, 0.55) 1.3px, transparent 1.6px);
  background-size: 12.5% 100%;
  background-position: left 6px center;
  background-repeat: repeat-x;
}

.message {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 12px;
  padding: 24px;
  text-align: center;
  background: rgba(8, 11, 24, 0.74);
}

.message.hidden {
  display: none;
}

#restartButton.hidden {
  display: none;
}

/* Nachtlinien-Kopf: rotes Linien-Badge wie am Bim-Zielschild */
.stop-eyebrow {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  font-family: var(--font-display);
  font-size: 0.8rem;
  letter-spacing: 0.24em;
  text-transform: uppercase;
  color: var(--muted);
}

.stop-eyebrow .line-badge {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: 0.86rem;
  letter-spacing: 0.06em;
  background: var(--bim-rot);
  color: #fff;
  padding: 2px 9px 3px;
  border-radius: 4px;
}

.message strong {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(2.4rem, 8vw, 5.2rem);
  letter-spacing: 0.04em;
  line-height: 0.95;
  color: var(--accent);
  text-shadow: 0 0 22px rgba(230, 189, 62, 0.28);
}

.message span {
  color: var(--text);
  max-width: 520px;
}

.message .disclaimer {
  color: var(--text);
  opacity: 0.55;
  font-size: 11px;
  max-width: 520px;
  line-height: 1.4;
}

.message-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #17130b;
  font-family: var(--font-display);
  font-size: 1.02rem;
  font-weight: 400;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
  touch-action: manipulation;
  white-space: nowrap;
}

button:active {
  transform: translateY(1px);
}

button:focus-visible {
  outline: 2px solid var(--led-amber);
  outline-offset: 2px;
}

/* Primaeraktion faehrt im Wiener-Linien-Rot */
#startButton {
  background: var(--bim-rot);
  color: #fff;
  box-shadow: 0 3px 14px rgba(207, 36, 40, 0.35);
}

button:disabled {
  opacity: 0.58;
  cursor: wait;
  transform: none;
}

.message-actions button {
  min-width: 132px;
  padding: 0 16px;
}

.audio-mix {
  display: grid;
  grid-template-columns: repeat(2, minmax(130px, 1fr));
  gap: 10px;
  width: min(380px, 84vw);
  margin: 2px auto 0;
  padding: 10px;
  border: 1px solid rgba(244, 241, 232, 0.28);
  border-radius: 8px;
  background: rgba(10, 14, 28, 0.64);
}

.audio-mix label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.86rem;
  text-align: left;
}

.audio-mix input {
  width: 100%;
  accent-color: var(--accent);
}

.level-select {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 4px;
}

.level-select::before {
  content: "Test-Level:";
  color: var(--muted);
  font-size: 0.82rem;
  white-space: nowrap;
}

.level-select button {
  min-width: 48px;
  min-height: 36px;
  padding: 0 10px;
  font-size: 0.82rem;
  background: var(--panel);
  color: var(--text);
  border: 1px solid #3c4238;
}

.level-select button:hover {
  background: #2a3248;
}

.level-select.hidden {
  display: none;
}

.controls {
  display: none;
  grid-template-columns: 1fr 1.35fr;
  gap: 10px;
  margin-top: 12px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* Hochformat-Sperre: auf Touch-Geraeten ist das 16:9-Spielfeld im Hochformat zu
   klein — Overlay fordert zum Drehen auf (Menue UND Spiel), game.js pausiert dazu. */
#rotateHint {
  display: none;
}

@media (pointer: coarse) and (orientation: portrait) {
  #rotateHint {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: grid;
    place-content: center;
    gap: 20px;
    padding: 24px;
    text-align: center;
    background: rgba(9, 13, 29, 0.97);
  }
}

#rotateHint p {
  margin: 0;
  color: var(--text);
  font-size: 1.02rem;
  line-height: 1.55;
}

.rotate-phone {
  width: 44px;
  height: 76px;
  margin: 0 auto;
  border: 3px solid var(--accent);
  border-radius: 10px;
  animation: rotate-phone 1.8s ease-in-out infinite;
}

@keyframes rotate-phone {
  0%, 22% { transform: rotate(0deg); }
  58%, 100% { transform: rotate(90deg); }
}

/* Pause/Vollbild direkt am Spielfeld (nur Touch-Geraete, s. Media Query) */
.stage-touch {
  display: none;
  position: absolute;
  top: 8px;
  right: 8px;
  gap: 8px;
  z-index: 15;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.stage-touch button {
  min-width: 42px;
  min-height: 42px;
  padding: 0;
  border-radius: 10px;
  font-size: 1rem;
  background: rgba(26, 32, 48, 0.62);
  color: rgba(244, 241, 232, 0.9);
  border: 1px solid rgba(244, 241, 232, 0.32);
}

.move-controls,
.action-controls {
  display: grid;
  gap: 10px;
}

.move-controls {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.action-controls {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.controls button {
  font-size: 1.1rem;
  min-height: 58px;
  position: relative;
}

/* Unsichtbare Extra-Trefferflaeche (Hit-Slop): knapp daneben zaehlt trotzdem. */
.controls button::after {
  content: "";
  position: absolute;
  inset: -12px;
}

@media (max-width: 720px), (pointer: coarse) {
  .game-shell {
    padding: 6px;
  }

  .topbar {
    align-items: start;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 8px;
  }

  h1 {
    font-size: 1.18rem;
  }

  p {
    display: none;
  }

  .stats {
    justify-content: start;
    gap: 4px;
  }

  .stats span {
    min-width: calc(50% - 2px);
    padding: 6px 8px 9px;
    font-size: 0.82rem;
  }

  .stats {
    width: 100%;
  }

  .stage-wrap {
    border-width: 1px;
  }

  .controls {
    display: grid;
    margin-top: 8px;
  }

  .message {
    gap: 10px;
    padding: 16px;
  }

  .message strong {
    font-size: 2rem;
  }

  .message span {
    max-width: 320px;
    font-size: 0.86rem;
  }

  .message-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: min(340px, 88vw);
    margin: 0 auto;
  }

  .message-actions button {
    min-width: 0;
    min-height: 52px;
    padding: 0 10px;
    font-size: 0.86rem;
  }

  .audio-mix {
    grid-template-columns: 1fr;
    width: min(300px, 84vw);
    padding: 8px;
  }

  .stage-touch {
    display: flex;
  }
}

/* Smartphone quer: Spielfeld auf Viewporthoehe begrenzen, Steuerung als
   halbtransparentes Overlay in die unteren Ecken — sonst laege sie unterhalb
   des sichtbaren Bereichs. */
@media (pointer: coarse) and (orientation: landscape) {
  body {
    overflow: hidden;
  }

  .game-shell {
    height: 100dvh;
    padding: 4px;
    display: flex;
    flex-direction: column;
  }

  .topbar {
    margin-bottom: 4px;
    align-items: center;
    flex-direction: row;
    justify-content: center;
  }

  .stats {
    width: auto;
    justify-content: center;
    flex-wrap: nowrap;
    gap: 4px;
  }

  .stats span {
    min-width: 0;
    padding: 3px 8px 7px;
    font-size: 0.72rem;
  }

  .stage-wrap {
    flex: 0 1 auto;
    width: min(100%, calc((100dvh - 44px) * 16 / 9));
    margin: 0 auto;
  }

  .controls {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 0;
    margin: 0;
    padding: 0 12px max(10px, env(safe-area-inset-bottom));
    pointer-events: none;
  }

  .move-controls {
    display: flex;
    gap: 24px;
    pointer-events: auto;
  }

  /* Jump am groessten unten rechts aussen, Spray/Blau kleiner schraeg darueber —
     Groessenstaffelung nach Nutzungsfrequenz, damit im Stress nichts verwechselt wird. */
  .action-controls {
    display: grid;
    grid-template-areas:
      ". spray"
      "blau jump";
    grid-template-columns: auto auto;
    gap: 14px 16px;
    align-items: end;
    justify-items: center;
    pointer-events: auto;
  }

  .controls button {
    min-width: 64px;
    min-height: 64px;
    border-radius: 50%;
    font-size: 1.05rem;
    background: rgba(230, 189, 62, 0.32);
    color: rgba(244, 241, 232, 0.94);
    border: 1px solid rgba(244, 241, 232, 0.38);
    backdrop-filter: blur(2px);
  }

  .move-controls button {
    min-width: 76px;
    min-height: 76px;
    font-size: 1.35rem;
  }

  #sprayButton {
    grid-area: spray;
  }

  #blueButton {
    grid-area: blau;
  }

  #jumpButton {
    grid-area: jump;
    min-width: 88px;
    min-height: 88px;
    font-size: 1.5rem;
  }

  #sprayButton,
  #blueButton {
    min-width: 56px;
    min-height: 56px;
    font-size: 0.78rem;
  }

  .controls button:active {
    background: rgba(230, 189, 62, 0.62);
    transform: none;
  }

  /* Menue-Overlay: kompakt und scrollbar — der volle Menue-Inhalt ist hoeher als
     das quer liegende Spielfeld, place-content:center wuerde oben abschneiden. */
  .message {
    gap: 8px;
    padding: 10px 14px 96px;
    place-content: start center;
    overflow-y: auto;
  }

  .message strong {
    font-size: 1.35rem;
  }

  .message span {
    font-size: 0.8rem;
    max-width: 460px;
  }

  .message-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: min(420px, 78vw);
    margin: 0 auto;
    gap: 8px;
  }

  .message-actions button {
    min-width: 0;
    min-height: 44px;
    padding: 0 10px;
    font-size: 0.84rem;
  }

  .audio-mix {
    width: min(380px, 72vw);
    padding: 8px;
    grid-template-columns: repeat(2, minmax(110px, 1fr));
  }

  .audio-mix label {
    font-size: 0.78rem;
  }

  .level-select {
    gap: 6px;
  }

  .level-select button {
    min-height: 34px;
    min-width: 40px;
    padding: 0 8px;
    font-size: 0.74rem;
  }

  .message .disclaimer {
    font-size: 9px;
    max-width: 460px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .rotate-phone {
    animation: none;
    transform: rotate(90deg);
  }

  button:active {
    transform: none;
  }

  .meter i {
    transition: none;
  }
}
