:root {
  --bg: #e9ebef;
  --fg: #14161a;
  --muted: #5b6270;
  --panel: rgba(255, 255, 255, 0.92);
  --row-alt: rgba(0, 0, 0, 0.04);
  --track: #2b2f36;
  --track-edge: #9aa1ad;
  --accent: #e10600;
  --battle: rgba(225, 6, 0, 0.12);
  --fastest: #8a2be2;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0f13;
    --fg: #eef0f4;
    --muted: #8b93a3;
    --panel: rgba(18, 21, 27, 0.92);
    --row-alt: rgba(255, 255, 255, 0.04);
    --track: #3a3f48;
    --track-edge: #1c1f25;
    --accent: #ff3b30;
    --battle: rgba(255, 59, 48, 0.18);
    --fastest: #b06cff;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 14px/1.2 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, sans-serif;
}

#map { position: fixed; inset: 0; width: 100%; height: 100%; }
#view3d { position: fixed; inset: 0; width: 100%; height: 100%; display: none; }
.has-3d #view3d { display: block; }
/* With a 3D track loaded the 2D map becomes a minimap. */
.has-3d #map {
  inset: auto 16px 32px auto; width: 300px; height: 220px;
  background: var(--panel); border-radius: 6px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}
#loading {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%);
  padding: 10px 16px; border-radius: 6px; background: var(--panel); color: var(--fg);
  font: 600 13px/1.3 inherit; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}

#tower {
  position: fixed; top: 16px; left: 16px;
  width: 248px; max-height: calc(100% - 64px); overflow: hidden;
  background: var(--panel);
  border-radius: 6px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(6px);
}
#tower header { padding: 10px 12px 8px; border-bottom: 3px solid var(--accent); }
#session-name { font-weight: 700; letter-spacing: 0.02em; text-transform: uppercase; font-size: 12px; color: var(--muted); }
#tower .sub { display: flex; justify-content: space-between; align-items: center; margin-top: 4px; }
#lap-counter { font-weight: 800; font-size: 18px; letter-spacing: 0.03em; }
#tower-mode {
  font: 700 11px/1 inherit; letter-spacing: 0.06em;
  background: none; color: var(--muted); border: 1px solid currentColor; border-radius: 3px;
  padding: 3px 6px; cursor: pointer;
}

.row {
  display: grid;
  grid-template-columns: 26px 4px 30px 1fr auto;
  align-items: center; gap: 6px;
  height: 26px; padding: 0 10px 0 6px;
  font-variant-numeric: tabular-nums;
}
.row:nth-child(even) { background: var(--row-alt); }
.row .pos { text-align: center; font-weight: 800; }
.row .bar { width: 4px; height: 16px; border-radius: 1px; }
.row .num { color: var(--muted); font-size: 12px; text-align: right; }
.row .name { font-weight: 700; letter-spacing: 0.02em; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.row .gap { font-size: 12px; color: var(--muted); text-align: right; }
.row.battle { background: var(--battle); }
.row.lap .gap { color: var(--fg); font-weight: 700; }
.row.fastest .gap { color: var(--fastest); }
.row.stale { opacity: 0.4; }
.row { cursor: pointer; }
.row.followed .name::after { content: " ◉"; color: var(--accent); font-size: 10px; vertical-align: 1px; }

/* Broadcast graphics (only meaningful over the 3D view). */
#bug {
  position: fixed; top: 16px; right: 16px; display: none; align-items: center; gap: 8px;
  font: 700 11px/1 inherit; letter-spacing: 0.08em; color: #fff; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}
.has-3d #bug { display: flex; }
#bug .live { background: var(--accent); padding: 4px 7px; border-radius: 3px; text-shadow: none; }
#cam-name { opacity: 0.85; }
#lower-third {
  position: fixed; left: 50%; bottom: 52px; transform: translateX(-50%);
  min-width: 340px; max-width: calc(100% - 32px);
  background: rgba(14, 16, 20, 0.88); color: #fff; border-radius: 4px; overflow: hidden;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.35); animation: lt-in 0.35s ease-out;
}
@keyframes lt-in { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }
.lt-head { background: #e10600; padding: 5px 12px; font: 800 11px/1.2 inherit; letter-spacing: 0.1em; }
.lt-head.fastest { background: #8a2be2; }
.lt-body { display: flex; align-items: center; gap: 8px; padding: 9px 12px; font-size: 15px; white-space: nowrap; }
.lt-body b { font-weight: 800; letter-spacing: 0.02em; }
.lt-body .chip { width: 4px; height: 18px; border-radius: 1px; }
.lt-body .vs { opacity: 0.6; font-size: 12px; margin: 0 4px; }
.lt-sub { opacity: 0.7; font-size: 13px; }
.lt-gap { margin-left: auto; padding-left: 16px; font-variant-numeric: tabular-nums; font-weight: 700; }
.lt-gap.fastest { color: #c99bff; }

/* Camera picker: fades out while the mouse is idle so the picture stays clean. */
#cams {
  position: fixed; top: 16px; left: 50%; transform: translateX(-50%); display: none;
  align-items: center; gap: 10px; padding: 5px; border-radius: 6px;
  background: rgba(14, 16, 20, 0.78); color: #fff; font-size: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3); transition: opacity 0.4s;
}
.has-3d #cams { display: flex; }
#cams.idle { opacity: 0; pointer-events: none; }
#cams .seg { display: flex; gap: 2px; }
#cams button {
  font: 600 12px/1 inherit; color: #d6d9df; background: transparent; border: 0;
  padding: 7px 10px; border-radius: 4px; cursor: pointer;
}
#cams button:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }
#cams button[aria-checked="true"] { background: #e10600; color: #fff; }
#cams .follow { display: flex; align-items: center; gap: 6px; padding-right: 4px; color: #aeb3bc; }
#cams select {
  font: 600 12px/1 inherit; color: #fff; background: rgba(255, 255, 255, 0.1);
  border: 0; border-radius: 4px; padding: 6px; max-width: 160px;
}
#cams select option { color: #000; }
#cams button.icon { display: flex; padding: 6px; color: #8d929b; }
#cams button.icon[aria-expanded="true"] { color: #fff; background: rgba(255, 255, 255, 0.1); }

/* Graphics settings: a small panel under the picker's gear button. */
#gfx {
  position: absolute; top: calc(100% + 6px); right: 0; width: 300px; padding: 10px;
  border-radius: 6px; background: rgba(14, 16, 20, 0.92); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
  display: grid; gap: 8px;
}
#gfx[hidden] { display: none; }
#gfx .row { display: flex; align-items: center; justify-content: space-between; gap: 8px; color: #aeb3bc; }
#gfx .row.preset { padding-bottom: 8px; border-bottom: 1px solid rgba(255, 255, 255, 0.08); }
#gfx .opts { display: flex; gap: 2px; background: rgba(255, 255, 255, 0.05); border-radius: 4px; padding: 2px; }
#cams #gfx button { padding: 5px 7px; font-size: 11px; }
#cams #gfx button[aria-pressed="true"] { background: rgba(255, 255, 255, 0.16); color: #fff; }
#gfx .note { color: #6f747d; font-size: 11px; line-height: 1.35; }

#hud {
  position: fixed; left: 16px; right: 16px; bottom: 10px;
  font: 11px/1.3 ui-monospace, Menlo, Consolas, monospace;
  color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#hud .dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; background: #999; vertical-align: 1px; }
#hud .dot.live { background: #2ecc71; }
#hud .dot.disconnected { background: var(--accent); }

@media (max-width: 600px) {
  #tower { width: calc(100% - 32px); max-height: 45%; }
  .has-3d #map { width: 160px; height: 120px; }
  #cams { top: auto; bottom: 30px; max-width: calc(100% - 32px); flex-wrap: wrap; justify-content: center; }
  #cams button { padding: 7px; }
  #gfx { top: auto; bottom: calc(100% + 6px); right: 50%; transform: translateX(50%); width: min(300px, calc(100vw - 32px)); }
}
