:root {
  --bg: #07080c;
  --panel: rgba(12, 14, 20, 0.82);
  --panel-solid: #0e1118;
  --line: rgba(255, 255, 255, 0.08);
  --text: #f2f4f8;
  --muted: #8a93a6;
  --accent: #e10600;
  --purple: #b14cff;
  --green: #22d36b;
  --yellow: #ffd400;
  --blue: #3aa0ff;
  --soft: #ff3b3b;
  --medium: #ffd400;
  --hard: #f2f4f8;
  --inter: #2fbf4a;
  --wet: #2f7dff;
  --radius: 10px;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font: 14px/1.3 'Titillium Web', system-ui, sans-serif; }
#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
button { font: inherit; color: inherit; cursor: pointer; }
.num, .mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* ---- top bar ---- */
.topbar { position: fixed; top: 12px; left: 12px; right: 12px; display: flex; align-items: center; gap: 14px; padding: 8px 12px; background: var(--panel); backdrop-filter: blur(10px); border: 1px solid var(--line); border-radius: var(--radius); z-index: 5; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 900; letter-spacing: .04em; text-transform: uppercase; font-size: 16px; }
.brand .mark { width: 6px; height: 22px; background: var(--accent); border-radius: 2px; }
.brand .session { color: var(--muted); font-weight: 600; font-size: 13px; }
.lapbox { display: flex; align-items: baseline; gap: 6px; font-family: var(--mono); font-weight: 700; font-size: 18px; }
.lapbox .label { font-family: 'Titillium Web'; font-size: 11px; color: var(--muted); letter-spacing: .12em; }
.lapbox .of { color: var(--muted); font-size: 14px; }
.flag { padding: 3px 10px; border-radius: 6px; font-weight: 700; font-size: 12px; letter-spacing: .08em; text-transform: uppercase; }
.flag[data-state="none"] { display: none; }
.flag[data-state="green"] { background: var(--green); color: #031; }
.flag[data-state="yellow"], .flag[data-state="vsc"], .flag[data-state="sc"] { background: var(--yellow); color: #221; }
.flag[data-state="red"] { background: var(--accent); }
.flag[data-state="chequered"] { background: repeating-conic-gradient(#fff 0 25%, #111 0 50%) 0 0/12px 12px; color: #000; text-shadow: 0 0 3px #fff, 0 0 3px #fff; }
.lights { display: flex; gap: 6px; }
.lights i { width: 16px; height: 16px; border-radius: 50%; background: #2a0606; border: 2px solid #111; }
.lights i.on { background: var(--accent); box-shadow: 0 0 12px var(--accent); }
.conditions { color: var(--muted); font-size: 13px; margin-left: auto; white-space: nowrap; }
.conn { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); white-space: nowrap; }
.fps { font: 600 11px var(--mono); color: var(--muted); white-space: nowrap; min-width: 6.5ch; text-align: right; font-variant-numeric: tabular-nums; }
.fps[data-level="ok"] { color: var(--yellow); }
.fps[data-level="poor"] { color: var(--accent); }
.conn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--yellow); }
.conn[data-state="live"] .dot { background: var(--green); box-shadow: 0 0 8px var(--green); }
.conn[data-state="stale"] .dot { background: var(--yellow); }
.conn[data-state="error"] .dot { background: var(--accent); }
.icon-btn { background: transparent; border: 1px solid var(--line); border-radius: 8px; width: 32px; height: 32px; font-size: 16px; }
.icon-btn:hover { background: rgba(255,255,255,.06); }

/* ---- tower ---- */
.tower { position: fixed; top: calc(var(--topbar-bottom, 60px) + 12px); left: 12px; width: 268px; max-height: calc(100% - 140px); overflow: auto; background: var(--panel); backdrop-filter: blur(10px); border: 1px solid var(--line); border-radius: var(--radius); z-index: 4; }
.tower-head { display: grid; grid-template-columns: 38px 1fr 84px; padding: 8px 10px; font-size: 11px; color: var(--muted); letter-spacing: .1em; border-bottom: 1px solid var(--line); }
#gapMode { text-align: right; cursor: pointer; text-decoration: underline dotted; }
.tower ol { list-style: none; margin: 0; padding: 4px 0; }
.tower li { display: grid; grid-template-columns: 30px 6px 1fr 14px 84px; align-items: center; gap: 6px; padding: 5px 10px; cursor: pointer; border-left: 3px solid transparent; transition: background .15s; }
.tower li:hover { background: rgba(255,255,255,.04); }
.tower li.focus { background: rgba(225, 6, 0, .14); border-left-color: var(--accent); }
.tower .pos { font-family: var(--mono); font-weight: 700; text-align: center; }
.tower .team { width: 4px; height: 18px; border-radius: 2px; background: var(--blue); }
.tower .name { font-weight: 700; text-transform: uppercase; letter-spacing: .03em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tower .name small { font-weight: 600; color: var(--muted); margin-left: 6px; font-size: 11px; }
.tower .tyre { width: 12px; height: 12px; border-radius: 50%; border: 3px solid var(--muted); }
.tower .gap { font-family: var(--mono); text-align: right; font-size: 13px; }
.tower .gap.fl { color: var(--purple); }
.tower li.out { opacity: .45; }
.tower .tag { font-size: 10px; font-weight: 700; padding: 1px 5px; border-radius: 4px; background: var(--blue); color: #fff; margin-left: 6px; }
.tower .tag.pen { background: var(--accent); }

/* ---- telemetry ---- */
.telemetry { position: fixed; top: calc(var(--topbar-bottom, 60px) + 12px); right: 12px; width: 280px; padding: 12px; background: var(--panel); backdrop-filter: blur(10px); border: 1px solid var(--line); border-radius: var(--radius); z-index: 4; }
.tel-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.tel-pos { font-family: var(--mono); font-weight: 700; background: var(--text); color: #000; border-radius: 4px; padding: 0 6px; }
.tel-name { font-weight: 900; text-transform: uppercase; font-size: 16px; flex: 1; overflow: hidden; text-overflow: ellipsis; }
.tel-lap { color: var(--muted); font-size: 12px; }
.tel-speed { display: flex; justify-content: space-between; align-items: flex-end; }
.tel-speed .big { font-family: var(--mono); font-weight: 700; font-size: 44px; line-height: 1; }
.tel-speed .unit { color: var(--muted); font-size: 11px; margin-left: 4px; letter-spacing: .1em; }
.tel-speed .gear { font-family: var(--mono); font-weight: 700; font-size: 40px; width: 52px; text-align: center; border: 2px solid var(--line); border-radius: 8px; line-height: 1.1; }
.rpm { height: 6px; background: rgba(255,255,255,.07); border-radius: 3px; margin: 10px 0; overflow: hidden; }
.rpm div { height: 100%; width: 0; background: linear-gradient(90deg, var(--green) 0 60%, var(--yellow) 60% 85%, var(--accent) 85%); transition: width .08s linear; }
.pedals { display: grid; gap: 6px; }
.pedal { display: grid; grid-template-columns: 34px 1fr; align-items: center; font-size: 11px; color: var(--muted); letter-spacing: .08em; }
.bar { position: relative; height: 10px; background: rgba(255,255,255,.07); border-radius: 3px; overflow: hidden; }
.fill { height: 100%; width: 0; transition: width .08s linear; }
.fill.thr { background: var(--green); }
.fill.brk { background: var(--accent); }
.bar.steer::after { content: ''; position: absolute; left: 50%; top: 0; bottom: 0; width: 1px; background: var(--muted); }
.needle { position: absolute; top: 0; bottom: 0; width: 4px; margin-left: -2px; left: 50%; background: var(--blue); border-radius: 2px; transition: left .08s linear; }
.tyres { display: flex; gap: 12px; align-items: center; margin: 12px 0 8px; }
.tyre-grid { display: grid; grid-template-columns: repeat(2, 54px); gap: 6px; }
.tyre-cell { border-radius: 8px; padding: 4px 0; text-align: center; font-family: var(--mono); font-size: 12px; font-weight: 700; color: #000; background: #444; }
.tyre-cell small { display: block; font-size: 10px; font-weight: 500; opacity: .75; }
.tyre-meta { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
.compound { display: inline-flex; align-items: center; gap: 6px; color: var(--text); font-weight: 700; }
.compound::before { content: ''; width: 12px; height: 12px; border-radius: 50%; border: 3px solid var(--tc, var(--muted)); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-family: var(--mono); }

/* ---- minimap / cameras ---- */
.minimap { position: fixed; right: 12px; bottom: 44px; width: 220px; height: 220px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); z-index: 4; }
.cams { position: fixed; bottom: 44px; left: 50%; transform: translateX(-50%); display: flex; gap: 2px; padding: 3px; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; z-index: 4; }
.cams button { background: transparent; border: 0; padding: 6px 14px; border-radius: 999px; font-weight: 700; font-size: 12px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.cams button.on { background: var(--accent); color: #fff; }
/* Free view's follow choice, just above the camera buttons. */
.follow-bar { position: fixed; bottom: 86px; left: 50%; transform: translateX(-50%); display: flex; gap: 2px; padding: 2px; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; z-index: 4; }
.follow-bar button { background: transparent; border: 0; padding: 4px 12px; border-radius: 999px; font-weight: 700; font-size: 11px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
body.free-cam .feed { bottom: 124px; }
.follow-bar button.on { background: rgba(255, 255, 255, .14); color: var(--text); }

/* ---- event feed ---- */
.feed { position: fixed; left: 292px; bottom: 96px; width: min(420px, calc(100% - 560px)); display: flex; flex-direction: column-reverse; gap: 6px; z-index: 4; pointer-events: none; }
.toast { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--panel); backdrop-filter: blur(10px); border: 1px solid var(--line); border-left: 4px solid var(--blue); border-radius: 8px; animation: in .3s ease-out; transition: opacity .6s, transform .6s; }
.toast.warn { border-left-color: var(--yellow); }
.toast.critical { border-left-color: var(--accent); }
.toast.fastest { border-left-color: var(--purple); }
.toast .code { font-family: var(--mono); font-size: 11px; color: var(--muted); }
.toast.gone { opacity: 0; transform: translateX(-20px); }
@keyframes in { from { opacity: 0; transform: translateY(10px); } }

/* ---- results ---- */
/* Below the top bar, never over it: on a phone it used to cover LIVE/REPLAY, so the replay list could not be opened. */
.results { position: fixed; top: calc(var(--topbar-bottom, 60px) + 8px); left: 50%; transform: translateX(-50%); width: min(620px, calc(100% - 32px)); max-height: calc(100% - var(--topbar-bottom, 60px) - 24px); display: flex; flex-direction: column; padding: 20px; background: var(--panel-solid); border: 1px solid var(--line); border-radius: 14px; z-index: 8; box-shadow: 0 30px 80px rgba(0,0,0,.6); }
.results-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.results h2 { margin: 0; font-weight: 900; text-transform: uppercase; letter-spacing: .05em; }
.results-x { flex: none; width: 34px; height: 34px; border: 1px solid var(--line); border-radius: 8px; background: rgba(255,255,255,.06); font-size: 15px; }
.results-x:hover { background: rgba(255,255,255,.12); }
.results-scroll { min-height: 0; overflow: auto; overscroll-behavior: contain; }
.results > .ghost { align-self: flex-start; }
.results table { width: 100%; border-collapse: collapse; }
.results th { text-align: left; font-size: 11px; color: var(--muted); letter-spacing: .1em; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.results td { padding: 8px; border-bottom: 1px solid var(--line); font-family: var(--mono); }
.results td:nth-child(2) { font-family: 'Titillium Web'; font-weight: 700; text-transform: uppercase; }
.results tr:first-child td { color: var(--yellow); }
.ghost { background: transparent; border: 1px solid var(--line); border-radius: 8px; padding: 6px 14px; margin-top: 14px; }
.primary { background: var(--accent); border: 0; border-radius: 8px; padding: 7px 16px; font-weight: 700; }

/* ---- loading / dialog ---- */
.loading { position: fixed; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px; background: var(--bg); z-index: 20; transition: opacity .6s; }
.loading.done { opacity: 0; pointer-events: none; }
.spinner { width: 36px; height: 36px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress { width: 220px; height: 3px; background: var(--line); border-radius: 2px; overflow: hidden; }
.progress div { width: 0; height: 100%; background: var(--accent); transition: width .2s; }
dialog { background: var(--panel-solid); color: var(--text); border: 1px solid var(--line); border-radius: 14px; padding: 20px; width: min(440px, calc(100% - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.6); }
dialog h2 { margin: 0 0 12px; }
dialog label { display: grid; gap: 4px; margin-bottom: 12px; font-size: 12px; color: var(--muted); }
dialog input, dialog select { font: 14px var(--mono); padding: 8px 10px; background: #000; color: var(--text); border: 1px solid var(--line); border-radius: 8px; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; padding: 0; margin: 0; }
dialog menu .ghost { margin: 0; }
.hint { font-size: 12px; color: var(--muted); }

.credits { position: fixed; left: 12px; right: 12px; bottom: 10px; font-size: 11px; color: var(--muted); text-align: center; z-index: 3; }
.credits a { color: var(--muted); }

/* car labels (CSS2D) */
.car-label { padding: 2px 7px; border-radius: 4px; background: rgba(0,0,0,.72); border-left: 3px solid var(--blue); font-weight: 700; font-size: 12px; text-transform: uppercase; white-space: nowrap; pointer-events: none; }
.car-label b { font-family: var(--mono); margin-right: 6px; }
.car-label.focus { border-left-color: var(--accent); background: rgba(225,6,0,.85); }

@media (max-width: 900px) {
  .telemetry { display: none; }
  .tower { width: 200px; }
  .tower li { grid-template-columns: 26px 4px 1fr 12px 64px; }
  .conditions { display: none; }
  .feed { left: 12px; width: calc(100% - 24px); bottom: 140px; }
  body.free-cam .feed { bottom: 140px; }
  .minimap { width: 120px; height: 120px; }
}
/* On a phone the tower starts collapsed to positions, so it doesn't cover the track; the arrow shows names. */
.tower-toggle { display: none; }
@media (max-width: 560px) {
  .tower { left: 8px; width: min(300px, calc(100% - 16px)); max-height: calc(100% - 180px); }
  .tower-head { grid-template-columns: 30px 1fr 64px 28px; align-items: center; }
  .tower-toggle { display: grid; place-items: center; width: 28px; height: 28px; margin: -6px -6px -6px 0; padding: 0; background: none; border: 0; font-size: 20px; line-height: 1; color: var(--text); }
  .tower.collapsed { width: auto; }
  .tower.collapsed .tower-head { display: flex; justify-content: center; padding: 4px; }
  .tower.collapsed .tower-head > span, .tower.collapsed .name, .tower.collapsed .tyre, .tower.collapsed .gap { display: none; }
  .tower.collapsed .tower-toggle { margin: 0; }
  .tower.collapsed li { grid-template-columns: 30px 4px; gap: 5px; padding: 4px 8px 4px 5px; }
  .tower.collapsed .pos::before { content: 'P'; }
  .results { padding: 14px; }
  .results th:nth-child(3), .results td:nth-child(3), .results th:nth-child(5), .results td:nth-child(5), .results th:nth-child(6), .results td:nth-child(6) { display: none; }
  .minimap { display: none; }
  .brand .session, .icon-btn { display: none; }
  /* Two rows on a phone: race on the first, controls on the second, so none fall off the screen. */
  .topbar { gap: 8px; flex-wrap: wrap; row-gap: 6px; }
  .poi-pick { flex: 1 1 60px; min-width: 0; }
  .conn { min-width: 0; }
  #connText { overflow: hidden; text-overflow: ellipsis; }
}

/* person of interest */
.poi-pick { display: flex; align-items: center; gap: 6px; color: var(--yellow); font-weight: 700; white-space: nowrap; }
.poi-pick select { max-width: 190px; font: 600 13px 'Titillium Web', sans-serif; color: var(--text); background: rgba(0,0,0,.4); border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; }
.tower li.poi { box-shadow: inset 0 0 0 1px rgba(255, 212, 0, .55); }
.tower .star { color: var(--yellow); margin-right: 5px; }
.car-label.poi { border-left-color: var(--yellow); box-shadow: 0 0 0 2px var(--yellow); }
@media (max-width: 560px) { .poi-pick select { max-width: none; width: 100%; min-width: 0; } }

/* live / replay switcher */
.source-btn { display: flex; align-items: center; gap: 6px; background: rgba(255,255,255,.06); border: 1px solid var(--line); border-radius: 8px; padding: 5px 10px; font-weight: 700; font-size: 12px; letter-spacing: .06em; white-space: nowrap; }
.source-btn:hover { background: rgba(255,255,255,.12); }
.src-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent); }
.source-btn.replay .src-dot { background: var(--blue); box-shadow: none; border-radius: 2px; }
.sessions { width: min(640px, calc(100% - 32px)); max-height: min(80vh, 760px); }
.sess-head { display: flex; align-items: center; gap: 12px; }
.sess-head h2 { margin: 0; flex: 1; }
.sess-head label { display: flex; align-items: center; gap: 6px; margin: 0; font-size: 12px; color: var(--muted); }
.sess-head .ghost { margin: 0; padding: 4px 10px; }
.sessions h3 { font-size: 11px; letter-spacing: .12em; text-transform: uppercase; color: var(--muted); margin: 16px 0 6px; }
.sess-live { display: grid; grid-template-columns: auto auto 1fr; align-items: center; gap: 10px; width: 100%; margin-top: 14px; padding: 12px; text-align: left; background: rgba(225,6,0,.12); border: 1px solid rgba(225,6,0,.4); border-radius: 10px; }
.sess-live span:last-child { color: var(--muted); font-size: 13px; text-align: right; }
.sessions ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.sessions li button { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; width: 100%; padding: 9px 12px; text-align: left; background: rgba(255,255,255,.03); border: 1px solid var(--line); border-radius: 8px; }
.sessions li button:hover, .sessions li button.current { background: rgba(58,160,255,.12); border-color: rgba(58,160,255,.5); }
.sessions li b { font-weight: 700; }
.sessions li small { color: var(--muted); font-size: 12px; }
.sessions li .when { text-align: right; font-family: var(--mono); font-size: 12px; color: var(--muted); }
.sessions .muted { color: var(--muted); padding: 8px 0; }
.sessions[open] { display: flex; flex-direction: column; overflow: hidden; }
.sessions ul { overflow-y: auto; min-height: 0; flex: 1; padding-right: 4px; }

/* access help, when the relay refuses the page */
.hint.access { color: var(--yellow); }
.linkish { background: none; border: 0; padding: 0; color: var(--text); text-decoration: underline; font: inherit; cursor: pointer; }
