:root {
  --fog: #e2eae8;
  --sky0: #9ec6e2;
  --sky1: #e2eae8;
  --ink: #23292d;
  --ink-soft: rgba(35, 41, 45, 0.62);
  --glass: rgba(250, 251, 249, 0.62);
  --glass-strong: rgba(250, 251, 249, 0.82);
  --line: rgba(35, 41, 45, 0.12);
  --accent: #e8473f;
  --purple: #a45ee5;
  --green: #2fb36a;
  --yellow: #e5b62f;
  --font: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
body.night {
  --ink: #eef1f6;
  --ink-soft: rgba(238, 241, 246, 0.62);
  --glass: rgba(14, 19, 34, 0.55);
  --glass-strong: rgba(14, 19, 34, 0.8);
  --line: rgba(238, 241, 246, 0.14);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--fog);
  color: var(--ink);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  user-select: none; -webkit-user-select: none;
  touch-action: none;
}
button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- stage ---------- */
#stage { position: fixed; inset: 0; overflow: hidden; background: var(--fog); }
#view, #gl { position: absolute; left: 0; top: 0; width: 100%; height: 100%; display: block; }
#vignette {
  position: absolute; inset: 0; pointer-events: none;
  background: radial-gradient(ellipse at 50% 55%, transparent 55%, rgba(20, 25, 30, 0.18) 100%);
}
body.night #vignette { background: radial-gradient(ellipse at 50% 60%, transparent 45%, rgba(0, 0, 8, 0.45) 100%); }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; transition: opacity 0.4s; }
body.in-menu #hud, body.hud-hidden #hud { opacity: 0; }

.hud-tl { position: absolute; left: 28px; top: 24px; text-shadow: 0 1px 12px rgba(255, 255, 255, 0.35); }
body.night .hud-tl { text-shadow: 0 1px 12px rgba(0, 0, 0, 0.5); }
#h-track { font-size: 22px; font-weight: 300; letter-spacing: 0.02em; }
#h-gp { font-size: 12px; color: var(--ink-soft); letter-spacing: 0.08em; text-transform: uppercase; margin-top: 4px; }

.hud-tr {
  position: absolute; right: 24px; top: 20px; min-width: 210px;
  padding: 14px 16px; border-radius: 14px;
  background: var(--glass); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  font-variant-numeric: tabular-nums;
}
.hud-tr .row { display: flex; justify-content: space-between; gap: 14px; align-items: baseline; }
.hud-tr .big { font-size: 26px; font-weight: 300; }
#h-lapno { font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
#h-cur.invalid { color: var(--ink-soft); text-decoration: line-through; text-decoration-thickness: 1px; }
#h-delta { font-size: 14px; font-weight: 500; min-height: 18px; margin-left: auto; }
#h-delta.good { color: var(--green); }
#h-delta.bad { color: #e0563f; }
.hud-tr .small { font-size: 13px; color: var(--ink-soft); margin-top: 3px; }
.hud-tr .small span:last-child { color: var(--ink); }
.sectors { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; margin: 8px 0 8px; }
.sec {
  font-size: 11px; text-align: center; padding: 3px 0; border-radius: 4px;
  background: var(--line); color: var(--ink-soft);
}
.sec.purple { background: var(--purple); color: #fff; }
.sec.green { background: var(--green); color: #fff; }
.sec.yellow { background: var(--yellow); color: #2a2200; }

.hud-bc {
  position: absolute; left: 50%; bottom: 26px; transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center;
  text-shadow: 0 1px 14px rgba(255, 255, 255, 0.4);
}
body.night .hud-bc { text-shadow: 0 1px 14px rgba(0, 0, 0, 0.6); }
.speedo { display: flex; align-items: baseline; gap: 6px; font-variant-numeric: tabular-nums; }
#h-speed { font-size: 54px; font-weight: 200; line-height: 1; min-width: 2.2ch; text-align: right; }
.unit { font-size: 12px; letter-spacing: 0.1em; color: var(--ink-soft); }
.rpm { width: 160px; height: 3px; border-radius: 2px; background: var(--line); margin-top: 8px; overflow: hidden; }
.rpm i { display: block; height: 100%; background: var(--ink); transform-origin: 0 50%; opacity: 0.75; }
#h-gear { font-size: 13px; margin-top: 6px; letter-spacing: 0.12em; color: var(--ink-soft); }
#h-status { font-size: 11px; letter-spacing: 0.2em; color: #e0563f; height: 16px; margin-bottom: 4px; }

#minimap {
  position: absolute; left: 22px; bottom: 22px; width: 170px; height: 170px;
  border-radius: 16px; background: rgba(25, 32, 36, 0.26);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#elev {
  position: absolute; left: 204px; bottom: 22px; width: 230px; height: 64px;
  border-radius: 14px; background: rgba(25, 32, 36, 0.26);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#elev.flat { display: none; }
#keys {
  position: absolute; right: 24px; bottom: 22px; max-width: 520px; text-align: right;
  font-size: 11px; color: var(--ink-soft); line-height: 1.7; letter-spacing: 0.02em;
  animation: fadeKeys 1s 14s forwards;
}
#keys b { color: var(--ink); font-weight: 500; }
@keyframes fadeKeys { to { opacity: 0; } }

#toast {
  position: fixed; left: 50%; top: 22px; transform: translate(-50%, -12px);
  padding: 9px 16px; border-radius: 999px; font-size: 13px;
  background: var(--glass-strong); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  opacity: 0; transition: opacity 0.3s, transform 0.3s; pointer-events: none; white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.pb { background: var(--purple); color: #fff; }

/* ---------- menu ---------- */
#menu {
  position: fixed; inset: 0; display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr; gap: 20px; padding: 24px;
  transition: opacity 0.45s;
}
body:not(.in-menu) #menu { opacity: 0; pointer-events: none; }
.panel, .browser {
  background: var(--glass); backdrop-filter: blur(16px) saturate(1.1); -webkit-backdrop-filter: blur(16px) saturate(1.1);
  border-radius: 20px; min-height: 0;
}
.panel { padding: 28px; display: flex; flex-direction: column; gap: 26px; align-self: end; }
.panel h1 { font-size: 40px; font-weight: 200; margin: 0; letter-spacing: -0.02em; }
.panel header p { margin: 6px 0 0; font-size: 14px; color: var(--ink-soft); line-height: 1.5; }
.eyebrow { font-size: 11px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--ink-soft); }
.detail h2 { font-size: 28px; font-weight: 300; margin: 6px 0 10px; }
.meta { display: flex; flex-wrap: wrap; gap: 6px; }
.meta span { font-size: 12px; padding: 4px 9px; border-radius: 999px; background: var(--line); }
.go-row { display: flex; align-items: center; gap: 14px; margin-top: 18px; }
#go {
  border: 0; border-radius: 999px; padding: 12px 22px; font-size: 15px; font-weight: 500;
  background: var(--ink); color: var(--fog); transition: transform 0.15s;
}
body.night #go { color: #0d1222; }
#go:hover { transform: translateY(-1px); }
#go span { opacity: 0.55; margin-left: 6px; }
#d-pb { font-size: 13px; color: var(--ink-soft); }

.opts { display: flex; flex-direction: column; gap: 10px; }
.opt { display: grid; grid-template-columns: 64px 1fr; align-items: center; }
.opt label { font-size: 12px; color: var(--ink-soft); }
.seg { display: flex; gap: 3px; padding: 3px; border-radius: 10px; background: var(--line); }
.seg button { flex: 1; border: 0; background: transparent; border-radius: 7px; padding: 6px 4px; font-size: 12px; }
.seg button.on { background: var(--glass-strong); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1); }
.swatches { display: flex; gap: 7px; }
.swatches button {
  width: 22px; height: 22px; border-radius: 50%; border: 2px solid transparent; padding: 0;
  background: var(--c); box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15);
}
.swatches button.on { border-color: var(--ink); }

.browser { display: flex; flex-direction: column; padding: 18px 18px 12px; overflow: hidden; align-self: stretch; }
.browser-head { display: flex; align-items: center; gap: 16px; }
.chips { display: flex; gap: 5px; overflow-x: auto; scrollbar-width: none; flex: 1; padding-bottom: 2px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  border: 0; border-radius: 999px; padding: 6px 12px; font-size: 12px; white-space: nowrap;
  background: var(--line); font-variant-numeric: tabular-nums;
}
.chip.on { background: var(--ink); color: var(--fog); }
body.night .chip.on { color: #0d1222; }
#count { font-size: 12px; color: var(--ink-soft); white-space: nowrap; }
#cards {
  flex: 1; overflow-y: auto; margin-top: 14px; padding: 2px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 10px; align-content: start;
}
.card {
  position: relative; display: flex; flex-direction: column; text-align: left; gap: 2px;
  border: 1px solid transparent; border-radius: 14px; padding: 12px; background: var(--line);
  transition: background 0.15s, transform 0.15s;
}
.card:hover { background: var(--glass-strong); transform: translateY(-1px); }
.card.sel { border-color: var(--ink); background: var(--glass-strong); }
.card svg { width: 100%; height: 92px; margin-bottom: 8px; }
.card svg path { fill: none; stroke: var(--ink); stroke-width: 2.2; stroke-linejoin: round; opacity: 0.8; }
.card svg circle { fill: var(--accent); }
.c-name { font-size: 14px; font-weight: 500; }
.c-sub { font-size: 11px; color: var(--ink-soft); }
.c-years { font-size: 11px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.c-pb {
  position: absolute; top: 10px; right: 10px; font-size: 10px; padding: 2px 7px; border-radius: 999px;
  background: var(--purple); color: #fff; font-variant-numeric: tabular-nums;
}
.browser footer { font-size: 10.5px; color: var(--ink-soft); padding-top: 10px; line-height: 1.5; }
.browser footer a { color: inherit; }

/* ---------- touch ---------- */
#touch { display: none; }
body.touch:not(.in-menu) #touch { display: block; }
#touch button {
  pointer-events: auto; border: 0; border-radius: 50%;
  background: var(--glass); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  font-size: 30px; width: 76px; height: 76px; touch-action: none;
}
#touch button.on { background: var(--glass-strong); transform: scale(0.95); }
.tc-left, .tc-right { position: fixed; bottom: 26px; display: flex; gap: 14px; }
.tc-left { left: 20px; }
.tc-right { right: 20px; }
.tc-top { position: fixed; top: 90px; right: 20px; display: flex; gap: 8px; }
.tc-top button { width: auto !important; height: 34px !important; border-radius: 999px !important; font-size: 12px !important; padding: 0 14px; }
body.touch #keys, body.touch #minimap, body.touch #elev { display: none; }

@media (max-width: 820px) {
  #menu { grid-template-columns: 1fr; grid-template-rows: auto 1fr; padding: 12px; gap: 12px; }
  .panel { padding: 18px; gap: 14px; align-self: stretch; }
  .panel h1 { font-size: 28px; }
  .panel header p { display: none; }
  .detail h2 { font-size: 22px; }
  .hud-tr { min-width: 170px; padding: 10px 12px; right: 12px; top: 12px; }
  .hud-tl { left: 14px; top: 14px; }
  #cards { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}

/* on-board cameras: speedo sits over dark bodywork → light text */
body.onboard .hud-bc { color: #f4f5f2; text-shadow: 0 1px 10px rgba(0, 0, 0, 0.7); }
body.onboard .hud-bc .unit, body.onboard #h-gear { color: rgba(244, 245, 242, 0.7); }
body.onboard .rpm { background: rgba(255, 255, 255, 0.2); }
body.onboard .rpm i { background: #f4f5f2; }

/* ---------- race ---------- */
.race-row { display: none !important; margin-bottom: 6px; }
body.racing .race-row { display: flex !important; }
#h-pos { font-size: 30px; font-weight: 500; }
#h-racelap { font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; color: var(--ink-soft); }
#tower {
  display: none; list-style: none; margin: 14px 0 0; padding: 6px; width: 190px;
  border-radius: 12px; background: var(--glass); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  font-size: 12px; font-variant-numeric: tabular-nums; text-shadow: none;
}
body.racing #tower { display: block; }
#tower li { display: grid; grid-template-columns: 18px 4px 1fr auto; gap: 7px; align-items: center; padding: 2px 6px; border-radius: 6px; }
#tower li.me { background: var(--glass-strong); font-weight: 600; }
#tower b { font-weight: 500; color: var(--ink-soft); text-align: right; }
#tower i { height: 12px; border-radius: 2px; background: var(--c); }
#tower em { font-style: normal; color: var(--ink-soft); }
#lights {
  position: fixed; left: 50%; top: 18%; transform: translateX(-50%);
  display: none; gap: 12px; padding: 14px 18px; border-radius: 14px; background: rgba(15, 17, 20, 0.85);
}
#lights.show { display: flex; }
#lights i { width: 34px; height: 34px; border-radius: 50%; background: #2b2e33; }
#lights i.on { background: #ff2a1f; box-shadow: 0 0 18px #ff2a1f; }
@media (max-width: 700px) { #tower { width: 160px; font-size: 11px; } }
body.racing #h-lapno { display: none; }
