:root {
  --bg: #87b8e8;
  --panel: rgba(20, 28, 40, 0.86);
  --text: #f4f6fa;
  --dim: #a9b4c4;
  --accent: #ffb52e;
  --line: rgba(255, 255, 255, 0.14);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); overflow: hidden; color: var(--text); font: 15px/1.35 system-ui, sans-serif; }
#spiel { display: block; width: 100%; height: 100%; }
button { font: inherit; color: var(--text); background: rgba(255, 255, 255, 0.1); border: 1px solid var(--line); border-radius: 8px; padding: 8px 14px; cursor: pointer; }
button:hover { background: rgba(255, 255, 255, 0.18); }
button.big { background: var(--accent); color: #1b1b1b; font-weight: 700; font-size: 18px; padding: 10px 18px; border: 0; width: 100%; margin-top: 12px; }
button.big:hover { filter: brightness(1.08); }
h1 { margin: 0; font-size: 34px; letter-spacing: 0.5px; }
h2 { margin: 12px 0 6px; font-size: 14px; text-transform: uppercase; letter-spacing: 1px; color: var(--dim); }
.dim { color: var(--dim); }

.screen { position: fixed; inset: 0; display: none; place-items: center; padding: 16px; }
.screen.show { display: grid; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px 20px; max-height: calc(100vh - 32px); overflow: auto; }
.panel.small { min-width: 260px; max-width: 420px; text-align: center; }
.panel.small button:not(.big) { width: 100%; margin-top: 8px; }
.panel.menu { width: min(720px, 100%); }
.sub { margin: 2px 0 0; color: var(--dim); }

.avatars { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.avatar { padding: 6px; text-align: center; border: 2px solid transparent; border-radius: 10px; background: rgba(255, 255, 255, 0.06); }
.avatar img { width: 100%; aspect-ratio: 8 / 5; object-fit: contain; display: block; }
.avatar b { display: block; font-size: 15px; }
.avatar small { color: var(--dim); font-size: 12px; }
.avatar.on { border-color: var(--accent); background: rgba(255, 181, 46, 0.14); }
.row { display: flex; gap: 24px; flex-wrap: wrap; align-items: end; }
.seg { display: inline-flex; gap: 4px; }
.seg button { min-width: 44px; }
.seg button.on { background: var(--accent); color: #1b1b1b; border-color: var(--accent); }
input[type=range] { width: 180px; accent-color: var(--accent); }
.keys { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 12px; color: var(--dim); font-size: 13px; }
kbd { display: inline-block; padding: 0 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; color: var(--text); font: 12px/18px system-ui, sans-serif; background: rgba(255, 255, 255, 0.06); }
.hint { margin: 8px 0 0; font-size: 13px; color: var(--dim); }

.huds { display: none; }
.huds.show { display: block; }
.hud { position: fixed; inset: 0; pointer-events: none; font-weight: 700; text-shadow: 0 2px 3px rgba(0, 0, 0, 0.55); }
.hud.half.top { bottom: 50%; }
.hud.half.bottom { top: 50%; border-top: 3px solid rgba(20, 28, 40, 0.9); }
.ptag { position: absolute; left: 50%; top: 8px; transform: translateX(-50%); font-size: 14px; color: var(--dim); }
.hud-tl { position: absolute; left: 16px; top: 12px; font-size: 20px; }
.hud-tl .dim { font-size: 15px; }
.hud-tr { position: absolute; right: 18px; top: 6px; font-size: 52px; }
.hud-tr .dim { font-size: 24px; }
.hud-br { position: absolute; right: 18px; bottom: 12px; font-size: 34px; }
.hud-br .dim { font-size: 16px; }
/* Countdown-Overlay mit drei Lampen */
.cd { position: absolute; left: 50%; top: 22%; transform: translateX(-50%); display: none; text-align: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 12px 26px 10px; min-width: 240px; text-shadow: none; }
.cd.show { display: block; }
.lights { display: flex; justify-content: center; gap: 12px; }
.lights i { width: 34px; height: 34px; border-radius: 50%; background: #3a4352; border: 2px solid rgba(255, 255, 255, 0.15); }
.lights i.red { background: #e5413a; box-shadow: 0 0 14px #e5413a; }
.lights i.green { background: #3fd25a; box-shadow: 0 0 16px #3fd25a; }
.cd-num { font-size: 54px; font-weight: 900; line-height: 1.1; margin-top: 4px; }
.cd-msg { font-size: 17px; min-height: 22px; color: var(--dim); }
.cd-msg.good { color: #6ee07f; }
.cd-msg.bad { color: #ff7a6e; }
.hud.half .cd { top: 14%; padding: 8px 18px 6px; }
.hud.half .cd-num { font-size: 36px; }
.hud.half .lights i { width: 24px; height: 24px; }
.banner { position: absolute; left: 0; right: 0; top: 18%; text-align: center; font-size: 36px; font-weight: 800; pointer-events: none; text-shadow: 0 3px 6px rgba(0, 0, 0, 0.5); opacity: 0; transition: opacity 0.25s; }
.banner.show { opacity: 1; }
table { width: 100%; border-collapse: collapse; margin: 8px 0 4px; }
td { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; }
td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
tr.me td { color: var(--accent); font-weight: 700; }

@media (max-width: 560px) {
  .avatars { grid-template-columns: repeat(2, 1fr); }
  .avatar small, .hint, .keys { display: none; }
  h1 { font-size: 26px; }
  .hud-tr { font-size: 38px; }
}
@media (max-height: 640px) {
  .panel { padding: 12px 16px; }
  .avatar img { aspect-ratio: 2 / 1; }
  h2 { margin: 8px 0 4px; }
}

/* Touch-Steuerung: nur auf Touch-Geraeten (Klasse touch-mode am body) und nur waehrend des Rennens. */
.touch, .touch-hint { display: none; }
body.touch-mode .touch-hint { display: block; }
body.touch-mode .keys { display: none; }
body.touch-mode.racing .touch { display: block; }
body.touch-mode { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.touch { position: fixed; inset: 0; pointer-events: none; }
.touch > * { pointer-events: auto; touch-action: none; }
.steer {
  position: absolute; left: max(16px, env(safe-area-inset-left)); bottom: max(16px, env(safe-area-inset-bottom));
  width: min(42vw, 300px); height: 110px; border-radius: 55px;
  background: rgba(20, 28, 40, 0.38); border: 2px solid rgba(255, 255, 255, 0.35);
  display: flex; align-items: center; justify-content: space-between; padding: 0 22px;
  font-size: 26px; color: rgba(255, 255, 255, 0.75);
}
.steer i { position: absolute; left: 50%; top: 50%; width: 60px; height: 60px; margin: -30px 0 0 -30px; border-radius: 50%; background: rgba(255, 255, 255, 0.55); transition: transform 0.05s; }
.pedals { position: absolute; right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom)); display: grid; grid-template-columns: auto auto; grid-template-rows: auto auto; gap: 12px; }
.tbtn { border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.45); color: #fff; font-weight: 800; font-size: 15px; background: rgba(20, 28, 40, 0.42); padding: 0; }
.tbtn.down { filter: brightness(1.6); transform: scale(0.95); }
.tbtn.gas { width: 104px; height: 104px; background: rgba(60, 170, 80, 0.6); grid-column: 2; grid-row: 2; }
.tbtn.brake { width: 78px; height: 78px; background: rgba(210, 70, 60, 0.55); grid-column: 1; grid-row: 2; align-self: end; }
.tbtn.drift { width: 78px; height: 78px; background: rgba(255, 181, 46, 0.6); grid-column: 2; grid-row: 1; justify-self: center; color: #1b1b1b; }
.tpause { position: absolute; left: 50%; top: max(8px, env(safe-area-inset-top)); transform: translateX(-50%); width: 48px; height: 40px; padding: 0; font-weight: 900; background: rgba(20, 28, 40, 0.5); }
body.touch-mode .hud-br { bottom: auto; top: 70px; font-size: 26px; }
.screen { z-index: 5; }

/* Handy im Querformat: Menue ohne Scrollen. */
@media (max-height: 450px) {
  .panel.menu { padding: 10px 14px; }
  h1 { font-size: 22px; display: inline; }
  .sub { display: inline; margin-left: 10px; font-size: 13px; }
  h2 { margin: 6px 0 3px; font-size: 12px; }
  .avatar { padding: 3px; }
  .avatar img { height: 52px; aspect-ratio: auto; }
  .avatar small, .hint:not(.touch-hint) { display: none; }
  .touch-hint { margin-top: 4px; }
  button.big { margin-top: 8px; padding: 8px 14px; font-size: 16px; }
  .seg button { padding: 5px 12px; }
}

/* Beitreten (2 Spieler) */
.panel.join { width: min(560px, 100%); text-align: center; }
.jslots { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.jslot { border: 2px dashed var(--line); border-radius: 12px; padding: 10px; min-height: 170px; display: flex; flex-direction: column; align-items: center; gap: 2px; }
.jslot.ready { border-style: solid; border-color: var(--accent); background: rgba(255, 181, 46, 0.1); }
.jslot img { width: 140px; height: 88px; object-fit: contain; }
.jname { font-weight: 800; font-size: 17px; }
.jslot small { color: var(--dim); font-size: 12px; }
.panel.join button:not(.big) { margin-top: 8px; }
button:disabled { opacity: 0.45; cursor: default; filter: none; }
.hud.half .hud-tr { font-size: 38px; }
.hud.half .hud-tl { font-size: 17px; }
.hud.half .hud-br { font-size: 26px; }

/* Fahrer-Werte, Streckenwahl, Minikarte, Abstand */
.avatar small { display: none; }
.bars { display: grid; grid-template-columns: auto 1fr; gap: 1px 6px; align-items: center; margin-top: 3px; font-size: 11px; color: var(--dim); text-align: left; }
.bars i { height: 5px; border-radius: 3px; background: linear-gradient(90deg, var(--accent) var(--v), rgba(255,255,255,0.12) var(--v)); }
.trackrow { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
#trackPreview { background: rgba(255,255,255,0.06); border-radius: 6px; }
.mini { position: absolute; left: 12px; top: 92px; width: 130px; height: 130px; background: rgba(20, 28, 40, 0.35); border-radius: 10px; }
.hud.half .mini { width: 92px; height: 92px; top: 78px; }
.gap { position: absolute; right: 18px; top: 66px; font-size: 16px; text-align: right; }
.gap span { display: block; }
.gap .up { color: #ffb0a8; }
.gap .down { color: #a8e6b4; }
.hud.half .gap { top: 52px; font-size: 14px; }
body.touch-mode .gap { top: 104px; }
@media (max-height: 450px) {
  .bars { display: none; }
  #trackPreview { height: 40px; width: 64px; }
  .mini { width: 90px; height: 90px; top: 80px; }
}

h1 { display: inline; }
.sub { display: inline; margin-left: 10px; }
.row { gap: 10px 22px; }
#trackPreview { width: 72px; height: 45px; }
@media (max-width: 560px) { .bars { display: none; } }
@media (max-height: 620px) {
  .avatar img { height: 64px; aspect-ratio: auto; }
  .hint:not(.touch-hint) { display: none; }
}
@media (max-height: 450px) {
  #trackPreview, #trackInfo { display: none; }
}

/* Grafik-Stufe und Bildrate */
.chk { margin-left: 10px; font-size: 13px; color: var(--dim); white-space: nowrap; }
.chk input { accent-color: var(--accent); vertical-align: middle; }
.fps { position: fixed; left: 50%; bottom: 6px; transform: translateX(-50%); font: 600 12px system-ui, sans-serif; color: #fff; background: rgba(0,0,0,0.45); padding: 2px 8px; border-radius: 6px; display: none; pointer-events: none; z-index: 6; }
.fps.show { display: block; }
@media (max-width: 560px) {
  .avatar img { height: 56px; aspect-ratio: auto; }
}
@media (max-height: 450px) {
  .row { gap: 4px 16px; }
  h2 { margin: 4px 0 2px; }
}
@media (max-width: 560px) { #trackPreview { display: none; } }

/* Auswahl per Gamepad */
.padfocus { outline: 3px solid #fff !important; outline-offset: 2px; box-shadow: 0 0 0 6px rgba(255, 181, 46, 0.55); }

/* Aufklappbare Optionen */
.opts { margin-top: 10px; }
.opts summary { cursor: pointer; color: var(--dim); font-size: 13px; text-transform: uppercase; letter-spacing: 1px; }
.opts[open] summary { margin-bottom: 4px; }
@media (max-width: 560px) { .avatar img { height: 44px; } }
@media (max-height: 450px) { .sub { display: none; } .avatar { padding: 2px; } .avatar img { height: 40px; } }
@media (max-width: 560px) { #trackInfo { display: none; } }
