:root {
  --accent: #ffb020;
  --accent-ink: #241a02;
  --good: #35e07a;
  --bad: #ff4d5e;
  --purple: #b26bff;
  --panel: rgba(8, 11, 10, 0.66);
  --panel-solid: #121614;
  --line: rgba(255, 255, 255, 0.10);
  --text: #eef2ee;
  --dim: #9aa89e;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden;
  background: #0d0f0e; color: var(--text); -webkit-user-select: none; user-select: none;
}
#gl { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.hidden { display: none !important; }

/* ------------------------------------------------------------------ HUD */
.hud { position: absolute; inset: 0; pointer-events: none; z-index: 5; font-variant-numeric: tabular-nums; }
.hud > div { position: absolute; }

.clock {
  top: 14px; left: 50%; transform: translateX(-50%); text-align: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 6px 24px 8px; backdrop-filter: blur(5px); min-width: 200px;
}
.clock .lapcount { font-size: 11px; letter-spacing: .18em; color: var(--accent); font-weight: 800; }
.clock .time { font-size: 34px; font-weight: 800; line-height: 1.05; }
.clock .delta { font-size: 15px; font-weight: 800; min-height: 18px; color: var(--dim); }
.delta.good { color: var(--good); }
.delta.bad { color: var(--bad); }

.stats {
  top: 14px; right: 14px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px; padding: 8px 14px 10px; min-width: 176px; backdrop-filter: blur(5px);
}
.stats .pos { font-size: 40px; font-weight: 900; line-height: 1; text-align: right; margin-bottom: 4px; }
.stats .pos small { font-size: 18px; color: var(--dim); font-weight: 700; }
.stats .row { display: flex; justify-content: space-between; gap: 14px; font-size: 13px; padding: 2px 0; color: var(--dim); }
.stats .v { color: var(--text); font-weight: 700; }
.stats .best .v { color: var(--purple); }

.tower {
  top: 14px; left: 14px; display: flex; flex-direction: column; gap: 2px; min-width: 210px;
}
.tower .r {
  display: grid; grid-template-columns: 26px 6px 1fr auto; align-items: center; gap: 8px;
  background: var(--panel); border-radius: 6px; padding: 3px 9px; font-size: 13px;
  border: 1px solid transparent; transition: transform .25s;
}
.tower .r b { text-align: right; font-weight: 900; }
.tower .r i { width: 6px; height: 16px; border-radius: 2px; }
.tower .r span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 120px; }
.tower .r em { font-style: normal; color: var(--dim); font-size: 12px; }
.tower .r.me { border-color: var(--accent); background: rgba(40, 30, 6, .78); }
.tower .r.fin em { color: var(--good); }

.map-wrap {
  left: 14px; bottom: 14px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px; padding: 8px; backdrop-filter: blur(5px);
}
#minimap { display: block; }

.speed {
  right: 14px; bottom: 14px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 8px 14px 10px; text-align: center; backdrop-filter: blur(5px);
  width: 188px;
}
#speedo { width: 160px; height: 96px; display: block; margin: 0 auto -38px; }
.speed .spd { font-size: 34px; font-weight: 900; line-height: 1; position: relative; }
.speed .spd small { font-size: 10px; color: var(--dim); letter-spacing: .12em; margin-left: 4px; font-weight: 700; }
.surf { margin-top: 6px; font-size: 10px; font-weight: 800; letter-spacing: .12em; padding: 3px 8px; border-radius: 6px; display: inline-block; }
.surf.ok { background: rgba(53, 224, 122, .16); color: var(--good); }
.surf.kerb { background: rgba(255, 176, 32, .18); color: var(--accent); }
.surf.bad { background: rgba(255, 120, 60, .22); color: #ff9a3c; }

.lights { top: 92px; left: 50%; transform: translateX(-50%); display: flex; gap: 10px; opacity: 0; transition: opacity .3s;
  background: rgba(0,0,0,.7); padding: 10px 14px; border-radius: 12px; }
.lights.show { opacity: 1; }
.lights i { width: 30px; height: 30px; border-radius: 50%; background: #2a0b0b; box-shadow: inset 0 0 6px #000; }
.lights i.on { background: radial-gradient(circle at 40% 35%, #ff8a7a, #ff1f10 55%, #a00800); box-shadow: 0 0 18px #ff2a10; }
.lights.go i { background: radial-gradient(circle at 40% 35%, #b9ffcb, #20e060 55%, #0a8030); box-shadow: 0 0 18px #20e060; }

.banner {
  top: 30%; left: 50%; transform: translate(-50%, -50%); font-size: clamp(30px, 6vw, 72px); font-weight: 900;
  letter-spacing: .04em; color: #fff; text-shadow: 0 4px 30px rgba(0,0,0,.6); opacity: 0;
  transition: opacity .25s, transform .25s; white-space: nowrap; text-align: center;
}
.banner.show { opacity: 1; transform: translate(-50%, -50%) scale(1.04); }
.banner.warn { color: var(--bad); }
.banner.gold { color: var(--accent); }
.banner small { display: block; font-size: .32em; font-weight: 700; letter-spacing: .1em; color: var(--text); }

.toast {
  top: 112px; left: 50%; transform: translateX(-50%); background: rgba(20, 24, 22, .92);
  border: 1px solid var(--line); font-weight: 800; padding: 7px 18px; border-radius: 10px;
  font-size: 14px; opacity: 0; transition: opacity .25s; white-space: nowrap;
}
.toast.show { opacity: 1; }
.toast.good { background: rgba(53, 224, 122, .92); color: #04260f; }
.toast.purple { background: rgba(160, 90, 240, .94); color: #fff; }
.toast.bad { background: rgba(255, 77, 94, .92); color: #2a0407; }

.fps { right: 14px; top: 190px; font-size: 11px; color: var(--dim); background: var(--panel); padding: 2px 6px; border-radius: 4px; }
.camname { bottom: 140px; right: 14px; font-size: 11px; color: var(--dim); letter-spacing: .1em; opacity: 0; transition: opacity .4s; }
.camname.show { opacity: 1; }

.touch { inset: 0; display: none; }
.touch.on { display: block; }
.tbtn {
  position: absolute; width: 84px; height: 84px; border-radius: 50%; background: rgba(255,255,255,.10);
  border: 2px solid rgba(255,255,255,.28); display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 900; letter-spacing: .08em; color: #fff; pointer-events: all; touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
.tbtn.active { background: rgba(255,176,32,.38); border-color: var(--accent); }
.tbtn.small { width: 46px; height: 46px; font-size: 11px; }
#t-gas { right: max(16px, env(safe-area-inset-right)); bottom: 20px; width: 104px; height: 104px; background: rgba(53,224,122,.16); border-color: rgba(53,224,122,.5); }
#t-brake { right: calc(max(16px, env(safe-area-inset-right)) + 116px); bottom: 20px; background: rgba(255,77,94,.14); border-color: rgba(255,77,94,.45); }
#t-pause { right: max(14px, env(safe-area-inset-right)); top: 162px; }
#t-cam { right: calc(max(14px, env(safe-area-inset-right)) + 56px); top: 162px; }
.steer {
  position: absolute; left: max(16px, env(safe-area-inset-left)); bottom: 20px; width: min(42vw, 300px); height: 92px;
  border-radius: 46px; background: rgba(255,255,255,.08); border: 2px solid rgba(255,255,255,.22);
  display: flex; align-items: center; justify-content: space-between; padding: 0 18px; color: rgba(255,255,255,.55);
  font-size: 22px; pointer-events: all; touch-action: none; -webkit-tap-highlight-color: transparent;
}
.steer.active { border-color: var(--accent); }
.steer .knob {
  position: absolute; left: 50%; top: 50%; width: 62px; height: 62px; margin: -31px 0 0 -31px; border-radius: 50%;
  background: rgba(255,255,255,.22); border: 2px solid rgba(255,255,255,.5); transition: transform .08s;
}
.steer.active .knob { background: rgba(255,176,32,.5); border-color: var(--accent); transition: none; }

/* touch play: move the HUD out from under the thumbs */
.hud.touchmode .map-wrap { display: none; }
.hud.touchmode .speed { bottom: auto; top: 96px; right: max(14px, env(safe-area-inset-right)); width: 120px; padding: 6px 10px; }
.hud.touchmode #speedo { display: none; }
.hud.touchmode .speed .spd { font-size: 26px; }
.hud.touchmode .camname { display: none; }
.hud.tiltmode #t-steer { display: none; }
body:not(.is-touch) .touch-only { display: none !important; }

.rotate {
  position: absolute; inset: 0; z-index: 30; display: none; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; background: rgba(6,8,7,.94); color: var(--text); font-weight: 800; font-size: 18px; text-align: center; padding: 24px;
}
.rotate div { font-size: 64px; color: var(--accent); animation: spin 2.4s ease-in-out infinite; }
@keyframes spin { 0%, 30% { transform: rotate(0); } 60%, 100% { transform: rotate(90deg); } }
@media (orientation: portrait) and (pointer: coarse) {
  #hud:not(.hidden) ~ .rotate { display: flex; }
}

/* ------------------------------------------------------------------ MENUS */
.menus { position: absolute; inset: 0; z-index: 20; pointer-events: none; }
.screen {
  position: absolute; inset: 0; display: none; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 24px; pointer-events: all; overflow-y: auto;
  background: radial-gradient(ellipse at 50% 40%, rgba(10, 16, 13, .55), rgba(3, 5, 4, .88));
}
.screen.on { display: flex; animation: fadein .25s ease; }
#scr-title.on, #scr-lobby.on { background: linear-gradient(90deg, rgba(3,5,4,.88) 0%, rgba(3,5,4,.55) 45%, rgba(3,5,4,0) 75%); align-items: flex-start; padding-left: max(5vw, 24px); text-align: left; }
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

.logo { margin: 0; line-height: .92; font-style: italic; letter-spacing: -.01em; }
.logo span { display: block; font-size: clamp(22px, 3.4vw, 40px); font-weight: 800; color: var(--text); }
.logo b { display: block; font-size: clamp(48px, 8vw, 104px); font-weight: 900; color: var(--accent);
  text-shadow: 0 6px 40px rgba(255, 176, 32, .35); }
.sub { color: var(--dim); font-size: 14px; letter-spacing: .3em; text-transform: uppercase; margin: 10px 0 28px; }
.screen h2 { margin: 0 0 18px; font-size: 32px; font-weight: 900; font-style: italic; }
.screen h2 small { font-size: 18px; color: var(--accent); font-style: normal; letter-spacing: .2em; margin-left: 8px; }

.menu { display: flex; flex-direction: column; gap: 8px; width: min(360px, 90vw); }
.menu button, .field input {
  font: inherit; font-size: 17px; font-weight: 700; color: var(--text); background: rgba(255,255,255,.06);
  border: 1px solid rgba(255,255,255,.12); border-radius: 10px; padding: 12px 18px; cursor: pointer;
  text-align: left; transition: background .12s, border-color .12s, transform .12s; outline: none;
}
.menu button:hover, .menu button:focus-visible, .menu button.focus {
  background: rgba(255, 176, 32, .16); border-color: var(--accent); transform: translateX(4px);
}
.menu button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.menu button.primary:hover, .menu button.primary.focus, .menu button.primary:focus-visible { background: #ffc552; }
.menu button:disabled { opacity: .45; cursor: default; transform: none; }
.menu .opt { display: flex; justify-content: space-between; align-items: center; }
.menu .opt .val { color: var(--accent); }
.menu .opt.primary .val { color: inherit; }
.menu .opt .val::before { content: "◀ "; opacity: .5; font-size: 12px; }
.menu .opt .val::after { content: " ▶"; opacity: .5; font-size: 12px; }
.swatch { display: inline-block; width: 26px; height: 16px; border-radius: 4px; vertical-align: middle; border: 1px solid rgba(255,255,255,.4); }

.field { display: flex; flex-direction: column; gap: 6px; width: min(360px, 90vw); margin-bottom: 8px; text-align: left; }
.field label { font-size: 12px; color: var(--dim); letter-spacing: .1em; text-transform: uppercase; }
.field input { cursor: text; }
.field input:focus { border-color: var(--accent); }
.field.inline { flex-direction: row; margin: 0; }
.field.inline input { width: 110px; text-transform: uppercase; letter-spacing: .2em; text-align: center; }
.field.inline button { flex: 1; }

.note { margin-top: 18px; font-size: 12px; color: var(--dim); max-width: 520px; line-height: 1.6; }
.padhint { margin-top: 22px; font-size: 12px; color: var(--dim); min-height: 16px; }

.players { list-style: none; padding: 0; margin: 0 0 16px; width: min(360px, 90vw); display: flex; flex-direction: column; gap: 4px; }
.players li { display: grid; grid-template-columns: 8px 1fr auto; gap: 10px; align-items: center; background: rgba(255,255,255,.05);
  border-radius: 8px; padding: 8px 12px; font-weight: 700; }
.players li i { width: 8px; height: 22px; border-radius: 3px; }
.players li em { font-style: normal; font-size: 12px; color: var(--dim); }
.players li em.rdy { color: var(--good); }
.lobby-status { color: var(--accent); font-weight: 700; margin-bottom: 12px; min-height: 20px; }

table.keys, table.results { border-collapse: collapse; margin-bottom: 14px; font-size: 14px; }
table.keys th, table.keys td, table.results th, table.results td { padding: 6px 14px; text-align: left; border-bottom: 1px solid var(--line); }
table.keys th, table.results th { color: var(--dim); font-size: 11px; letter-spacing: .1em; text-transform: uppercase; }
table.keys td:first-child { color: var(--dim); }
table.results { min-width: min(520px, 92vw); font-variant-numeric: tabular-nums; }
table.results tr.me td { color: var(--accent); font-weight: 800; }
table.results td.p { font-weight: 900; width: 36px; }
table.results i { display: inline-block; width: 6px; height: 14px; border-radius: 2px; margin-right: 8px; vertical-align: middle; }

.loadbar { width: 260px; height: 4px; border-radius: 3px; background: rgba(255,255,255,.12); margin-top: 6px; overflow: hidden; }
.loadbar-fill { height: 100%; width: 0%; background: var(--accent); transition: width .25s; }
.loadnote { margin-top: 10px; font-size: 12px; color: var(--dim); letter-spacing: .04em; }

.fatal {
  position: absolute; inset: 0; z-index: 40; display: flex; flex-direction: column; align-items: center;
  justify-content: center; text-align: center; padding: 28px; background: rgba(6,8,7,.97);
}
.fatal[hidden] { display: none; }
.fatal h2 { margin: 0 0 10px; font-size: 22px; }
.fatal p { max-width: 520px; font-size: 13px; color: var(--dim); line-height: 1.7; margin: 4px 0; }
.fatal code { background: rgba(255,255,255,.08); padding: 2px 6px; border-radius: 4px; color: var(--text); font-size: 12px; }

@media (max-width: 760px) {
  .tower { display: none; }
  .clock .time { font-size: 24px; }
  .clock { min-width: 150px; padding: 4px 14px; }
  .stats { min-width: 120px; padding: 6px 10px; }
  .stats .pos { font-size: 28px; }
  .speed { width: 130px; }
  #speedo { width: 110px; height: 66px; margin-bottom: -26px; }
  .speed .spd { font-size: 24px; }
  #scr-title.on, #scr-lobby.on { align-items: center; text-align: center; padding-left: 24px; }
}
@media (max-height: 520px) {
  .map-wrap { display: none; }
  .clock { top: 6px; }
  .stats { top: 6px; padding: 4px 10px 6px; }
  .stats .row { font-size: 11px; padding: 0; }
  .hud.touchmode .speed { top: 84px; }
  .lights { top: 70px; }
  .screen { justify-content: flex-start; }
  .sub { margin-bottom: 12px; }
  .menu button { padding: 8px 14px; font-size: 15px; }
}
