/* LMU Live Timing — pit-wall instrument.
   The three class colours are semantic (they identify the car's category),
   not decoration. Purple/green/amber are the timing-monitor convention for
   session-best / personal-best / caution. */
:root {
  --void: #0a0b0d;
  --panel: #101216;
  --panel-hi: #161a20;
  --rule: #23272e;
  --ink: #edeff2;
  --ink-dim: #7d848f;
  --ink-faint: #4c525b;

  --hy: #e4022b;   /* Hypercar   */
  --p2: #2e7df6;   /* LMP2       */
  --gt3: #14b87a;  /* LMGT3 / GTE */
  --cls-other: #8a6df0;

  --purple: #a970ff; /* session best */
  --best:   #2ed16a; /* personal best */
  --hold:   #f2b01e; /* caution / out lap / pit */
  --gain:   #2ed16a;
  --loss:   #e4022b;
  --mine:   #f5c542; /* your team */

  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "Roboto Mono", Consolas, monospace;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --cols: 3px 46px 54px minmax(130px, 1fr) 88px 76px 76px 138px 104px;
  --pad-x: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--void);
  color: var(--ink);
  font: 13px/1.4 var(--ui);
  -webkit-text-size-adjust: 100%;
  display: flex;
  flex-direction: column;
  min-height: 100%;
}
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }

/* ── start gantry ─────────────────────────────────────────── */
#gantry {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 18px;
  padding: 9px 14px;
  background: linear-gradient(#12151a, #0d0f13);
  border-bottom: 1px solid var(--rule);
  font-family: var(--mono);
  overflow-x: auto;
  scrollbar-width: none;
}
#gantry::-webkit-scrollbar { display: none; }
#gantry > div { display: flex; align-items: center; gap: 7px; white-space: nowrap; }
.g-state { font-weight: 700; letter-spacing: .12em; font-size: 11px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ink-faint); box-shadow: 0 0 0 3px rgba(255,255,255,.04); }
.dot.live { background: var(--best); box-shadow: 0 0 10px var(--best); }
.dot.stale { background: var(--hold); box-shadow: 0 0 10px var(--hold); }
.dot.down { background: var(--loss); }
.g-flag {
  font-weight: 700; letter-spacing: .14em; font-size: 11px;
  padding: 4px 9px; border-radius: 2px; color: var(--void); background: var(--ink-dim);
}
.g-flag.green { background: var(--best); }
.g-flag.yellow { background: var(--hold); }
.g-flag.red { background: var(--loss); color: #fff; }
.g-flag.chequered { background: repeating-linear-gradient(45deg,#fff 0 6px,#000 6px 12px); color: transparent; }
.g-lbl { color: var(--ink-faint); font-size: 10px; letter-spacing: .12em; }
.g-leader #leader, #leader { font-weight: 600; }
.g-count { font-size: 12px; color: var(--ink-dim); }
.g-clock { margin-left: auto; font-size: 15px; font-weight: 700; letter-spacing: .02em; }
.g-weather { color: var(--ink-dim); font-size: 11px; }
@media (max-width: 620px) { .g-weather, .g-lbl { display: none; } #gantry { gap: 12px; } }

/* ── pinned car strip ─────────────────────────────────────── */
#pinbar {
  position: sticky; top: 39px; z-index: 15;
  display: grid; grid-template-columns: var(--cols); align-items: center;
  background: var(--panel-hi);
  border-bottom: 1px solid var(--rule);
  box-shadow: 0 6px 14px -8px rgba(0,0,0,.7);
  font-family: var(--mono);
  min-height: 40px;
}
#pinbar .c-driver { font-family: var(--ui); font-weight: 700; }
#pinbar .pin-tag {
  grid-column: 1 / 2; align-self: stretch; background: var(--purple);
}
#pinbar.mine {
  box-shadow: inset -3px 0 0 var(--mine), 0 6px 14px -8px rgba(0,0,0,.7);
  background: rgba(245,197,66,.1);
}
#pinbar.mine .pin-tag { background: var(--mine); }

/* ── controls ─────────────────────────────────────────────── */
#controls {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 12px; border-bottom: 1px solid var(--rule);
  background: var(--void);
}
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  font: 700 10px/1 var(--ui); letter-spacing: .06em;
  padding: 6px 9px; border-radius: 2px;
  color: var(--ink-dim); background: var(--panel);
  border: 1px solid var(--rule);
  display: flex; align-items: center; gap: 6px;
}
.chip .swatch { width: 8px; height: 8px; border-radius: 1px; }
.chip[aria-pressed="false"] { opacity: .38; }
.chip[aria-pressed="true"] { color: var(--ink); }
#mine {
  margin-left: auto;
  width: 96px; font: 600 11px/1 var(--mono);
  padding: 6px 8px; border-radius: 2px;
  color: var(--mine); background: var(--panel); border: 1px solid var(--rule);
}
#mine::placeholder { color: var(--ink-faint); font-family: var(--ui); font-weight: 500; letter-spacing: .04em; }
#mine:focus { outline: none; border-color: var(--mine); }
.modebtn {
  font: 700 10px/1 var(--ui); letter-spacing: .08em;
  padding: 6px 10px; border-radius: 2px;
  color: var(--ink-dim); background: var(--panel); border: 1px solid var(--rule);
}
.modebtn:hover { color: var(--ink); }
.modebtn.on, .modebtn[aria-pressed="true"] { color: var(--ink); border-color: var(--muted); }

/* ── track map ────────────────────────────────────────────── */
#map {
  height: 32vh; min-height: 170px; flex: none;
  position: relative; background: #0c0e12;
  border-bottom: 1px solid var(--rule);
}
#mapSvg { width: 100%; height: 100%; display: block; }
#mapPath { stroke: #39414c; stroke-width: 4; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }
#mapDots circle { stroke: #0b0d10; stroke-width: 3; transition: cx .35s linear, cy .35s linear; }
.map-empty {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--ink-faint); font-size: 12px; text-align: center; padding: 16px;
}
#map.ready .map-empty { display: none; }
@media (prefers-reduced-motion: reduce) { #mapDots circle { transition: none; } }

/* drawer: recent laps / electronics / damage */
.spark { width: 100%; height: 30px; max-height: 30px; display: block; margin: 2px 0 4px; }
.drawer-note { grid-column: 1 / -1; color: var(--ink-faint); font-size: 11px; line-height: 1.4; }
.spark polyline { fill: none; stroke: var(--ink-dim); stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.laprow { display: flex; justify-content: space-between; font-family: var(--mono); font-size: 11px; padding: 1px 0; }
.laprow b { color: var(--ink-faint); font-weight: 500; }
.laprow.fast span:last-child { color: var(--best); }
.laprow.pit span:last-child { color: var(--hold); }
.elec { display: flex; flex-wrap: wrap; gap: 4px 12px; font-family: var(--mono); font-size: 12px; }
.elec span b { color: var(--ink-faint); font-weight: 500; margin-right: 3px; }
.elec .warn { font-weight: 700; }
.dmg-panels { display: inline-flex; gap: 2px; }
.dmg-panels i { width: 11px; height: 11px; border-radius: 1px; background: #1c2129; }
.dmg-panels i.d1 { background: var(--hold); }
.dmg-panels i.d2 { background: var(--loss); }
.drawer-actions { grid-column: 1 / -1; margin-bottom: 2px; }
.alllaps {
  font: 700 10px/1 var(--ui); letter-spacing: .06em; color: var(--ink-dim);
  background: var(--panel); border: 1px solid var(--rule); border-radius: 2px; padding: 5px 9px;
}
.alllaps:hover { color: var(--ink); border-color: var(--muted); }

/* ── lap-history modal ────────────────────────────────────── */
#lapModal {
  width: min(560px, 94vw); max-height: 84vh; padding: 0;
  border: 1px solid var(--rule); border-radius: 6px;
  background: var(--surface); color: var(--ink);
  box-shadow: 0 30px 80px -20px rgba(0,0,0,.8);
}
#lapModal::backdrop { background: rgba(6,7,9,.72); }
.lm-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; border-bottom: 1px solid var(--rule);
  font-family: var(--mono); font-weight: 700; font-size: 13px;
  position: sticky; top: 0; background: var(--surface);
}
#lmTitle { flex: 1; }
#lmClose {
  color: var(--ink-dim); font-size: 15px; line-height: 1; padding: 4px 6px; border-radius: 2px;
}
#lmClose:hover { color: var(--ink); background: var(--panel); }
.lm-summary {
  display: flex; flex-wrap: wrap; gap: 6px 16px;
  padding: 10px 14px; border-bottom: 1px solid var(--rule);
  font-family: var(--mono); font-size: 12px;
}
.lm-summary b { color: var(--ink-faint); font-weight: 500; margin-right: 5px; }
.lm-body { overflow: auto; max-height: calc(84vh - 108px); }
table.lm {
  width: 100%; border-collapse: collapse;
  font-family: var(--mono); font-variant-numeric: tabular-nums; font-size: 12px;
}
table.lm thead th {
  position: sticky; top: 0; background: var(--surface);
  color: var(--ink-faint); font: 600 10px/1 var(--ui); letter-spacing: .08em;
  text-align: right; padding: 8px 12px; border-bottom: 1px solid var(--rule);
}
table.lm td { text-align: right; padding: 6px 12px; border-bottom: 1px solid var(--line, #20242b); }
table.lm td.l { text-align: left; }
table.lm tr.pit td { color: var(--hold); }
table.lm td.sb { color: var(--purple); }
.lm-empty { padding: 24px; text-align: center; color: var(--ink-faint); font-size: 13px; }

/* ── sheet ────────────────────────────────────────────────── */
#sheetWrap { flex: 1; overflow: auto; position: relative; }
#sheet { min-width: 100%; }
.row {
  display: grid; grid-template-columns: var(--cols);
  align-items: center; min-width: 720px;
}
.row.head {
  position: sticky; top: 0; z-index: 10;
  background: var(--void);
  border-bottom: 1px solid var(--rule);
  font: 600 10px/1 var(--ui); letter-spacing: .09em; color: var(--ink-faint);
  padding: 9px 0;
}
.row.head > span { padding: 0 var(--pad-x); }
.row.head .c-pos, .row.head .c-gap, .row.head .c-last, .row.head .c-best { text-align: right; }
.row.head .c-car { text-align: center; }

.row.car {
  border-bottom: 1px solid var(--rule);
  position: relative;
  font-family: var(--mono);
  min-height: 40px;
  transition: transform .18s cubic-bezier(.2,.7,.3,1), background .12s;
  will-change: transform;
}
.row.car:hover { background: var(--panel); }
.row.car.pinned { background: var(--panel-hi); }
.row.car.mine { background: rgba(245,197,66,.07); box-shadow: inset -3px 0 0 var(--mine); }
.row.car.mine.pinned { background: rgba(245,197,66,.12); }
.row.car.gone { opacity: .4; }
.row.car.pit-in .c-gap { color: var(--hold); }
.c-driver .star { color: var(--mine); font-size: 11px; line-height: 1; }
.c-driver .pen {
  font: 800 9px/1 var(--ui); letter-spacing: .04em;
  padding: 3px 5px; border-radius: 2px; color: var(--void); flex: none;
}
.c-driver .pen:empty { display: none; }
.c-driver .pen.count { background: var(--hold); }
.c-driver .pen.black, .c-driver .pen.dsq { background: var(--loss); color: #fff; }
.row.car.penalised .c-pos { color: var(--loss); }

.c-spine { align-self: stretch; background: var(--ink-faint); }
.class-HYPERCAR .c-spine, .class-HY .c-spine { background: var(--hy); }
.class-LMP2 .c-spine, .class-P2 .c-spine { background: var(--p2); }
.class-LMGT3 .c-spine, .class-GTE .c-spine, .class-GT3 .c-spine { background: var(--gt3); }

.c-pos { text-align: right; padding: 0 var(--pad-x); font-weight: 700; font-size: 14px; }
.c-pos .pic { display: block; font-size: 9px; color: var(--ink-faint); font-weight: 500; margin-top: 1px; }

.c-car { display: flex; justify-content: center; }
.plate {
  font-weight: 700; font-size: 12px; letter-spacing: .02em;
  padding: 3px 6px; border-radius: 2px; min-width: 34px; text-align: center;
  color: var(--ink); background: var(--panel-hi); border-left: 2px solid var(--ink-faint);
}
.class-HYPERCAR .plate { border-color: var(--hy); }
.class-LMP2 .plate { border-color: var(--p2); }
.class-LMGT3 .plate, .class-GTE .plate { border-color: var(--gt3); }

.c-driver { padding: 0 var(--pad-x); display: flex; align-items: center; gap: 8px; min-width: 0; }
.c-driver .name {
  font-family: var(--ui); font-weight: 600; font-size: 13px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.c-driver .sub { font-size: 10px; color: var(--ink-faint); }
.lapsbtn {
  margin-left: auto; flex: none;
  color: var(--ink-faint); font-size: 12px; line-height: 1;
  padding: 4px 6px; border-radius: 3px; opacity: .5;
}
.row.car:hover .lapsbtn, #pinbar .lapsbtn { opacity: 1; }
.lapsbtn:hover { color: var(--ink); background: var(--panel-hi); opacity: 1; }
.delta {
  font-family: var(--mono); font-size: 11px; font-weight: 700;
  width: 1.4em; text-align: center; opacity: 0;
}
.delta.up { color: var(--gain); }
.delta.down { color: var(--loss); }
.delta.show { animation: deltaFlash 4s ease-out forwards; }
@keyframes deltaFlash { 0%,70% { opacity: 1; } 100% { opacity: 0; } }

.c-gap, .c-last, .c-best { text-align: right; padding: 0 var(--pad-x); font-size: 13px; }
.c-gap { font-weight: 600; }
.c-gap.laps { color: var(--ink-dim); }
.c-gap .rate { font-size: 9px; font-weight: 700; margin-left: 3px; }
.c-gap .rate.up { color: var(--gain); }
.c-gap .rate.down { color: var(--loss); }
.rate-win { opacity: .55; font-size: 9px; margin-left: 4px; }
.c-last.sb, .c-best.sb { color: var(--purple); }
.c-last.pb { color: var(--best); }
.c-best .fl { color: var(--purple); font-size: 9px; margin-right: 3px; }
.pulseLine::after {
  content: ""; position: absolute; left: 0; right: 0; height: 1px; background: var(--purple);
  animation: sweep 1s ease-out forwards;
}
@keyframes sweep { from { clip-path: inset(0 100% 0 0); } to { clip-path: inset(0 0 0 0); } }

/* sector tri-block */
.c-sectors { display: flex; gap: 2px; padding: 0 var(--pad-x); }
.seg {
  flex: 1; height: 20px; border-radius: 1px;
  display: flex; align-items: center; justify-content: center;
  font-size: 10px; font-weight: 600; color: var(--ink-dim);
  background: #191d23;
  transition: background .12s, color .12s;
}
.seg.set { color: var(--ink); }
.seg.pb { background: rgba(46,209,106,.16); color: var(--best); }
.seg.sb { background: rgba(169,112,255,.20); color: var(--purple); }
/* sector currently in progress: a calm marker, not a strobe */
.seg.live { background: #21262e; color: var(--ink-dim); box-shadow: inset 0 -2px 0 var(--hold); }

/* stint: energy/fuel bar + tyre */
.c-stint { display: flex; align-items: center; gap: 7px; padding: 0 var(--pad-x); }
.bar { flex: 1; height: 6px; border-radius: 3px; background: #191d23; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--ink-dim); border-radius: 3px; }
.class-HYPERCAR .bar > i { background: var(--hy); }
.class-LMP2 .bar > i { background: var(--p2); }
.class-LMGT3 .bar > i, .class-GTE .bar > i { background: var(--gt3); }
.bar.low > i { background: var(--hold); }
.tyre {
  width: 17px; height: 17px; border-radius: 50%; flex: none;
  display: flex; align-items: center; justify-content: center;
  font-size: 9px; font-weight: 800; color: var(--void);
  background: var(--ink-faint);
}
.tyre.S { background: #ff4d4d; } .tyre.M { background: #f2b01e; }
.tyre.H { background: #e6e6e6; } .tyre.W { background: #3aa0ff; }
.stint-txt { font-size: 10px; color: var(--ink-faint); white-space: nowrap; }
.stint-txt.pit { color: var(--hold); font-weight: 700; }

.pill {
  font: 800 9px/1 var(--ui); letter-spacing: .06em;
  padding: 3px 5px; border-radius: 2px; color: var(--void);
}
.pill.pit { background: var(--hold); }
.pill.out { background: var(--best); }
.pill.pen { background: var(--loss); color: #fff; }

/* class divider */
.clsdiv {
  display: flex; align-items: center; gap: 8px;
  padding: 5px var(--pad-x) 5px 12px;
  background: var(--void);
  border-bottom: 1px solid var(--rule);
  font: 700 10px/1 var(--ui); letter-spacing: .12em; color: var(--ink-dim);
  position: relative;
}
.clsdiv::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--ink-faint); }
.clsdiv.class-HYPERCAR::before { background: var(--hy); }
.clsdiv.class-LMP2::before { background: var(--p2); }
.clsdiv.class-LMGT3::before, .clsdiv.class-GTE::before { background: var(--gt3); }
.clsdiv .cd-count { color: var(--ink-faint); font-weight: 500; }
.clsdiv .cd-best {
  margin-left: auto; color: var(--purple);
  font-family: var(--mono); font-variant-numeric: tabular-nums;
  font-weight: 600; letter-spacing: 0;
}
.clsdiv .cd-best:empty { display: none; }

/* telemetry drawer */
.drawer { border-bottom: 1px solid var(--rule); background: var(--panel); overflow: hidden; }
.drawer-inner {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  align-items: start; align-content: start;   /* don't stretch short columns */
  gap: 12px 22px; padding: 12px 16px 14px;
}
.drawer-inner > div { min-width: 0; }
.drawer h4 { margin: 0 0 6px; font: 700 9px/1 var(--ui); letter-spacing: .1em; color: var(--ink-faint); }
.tyre-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 8px; font-family: var(--mono); }
.tcell { background: #191d23; border-radius: 3px; padding: 5px 7px; }
.tcell .big { font-size: 14px; }
.tcell .sm { font-size: 10px; color: var(--ink-faint); }
.kv { display: flex; justify-content: space-between; font-family: var(--mono); font-size: 12px; padding: 2px 0; }
.kv b { color: var(--ink-dim); font-weight: 500; }
.kv span b { font-weight: 700; }
.kv span b.gain { color: var(--gain); }
.kv span b.loss { color: var(--loss); }
.hot { color: var(--loss); } .cold { color: var(--p2); } .warn { color: var(--hold); }

/* status overlay */
.status {
  position: absolute; inset: 0; display: none;
  align-items: center; justify-content: center; text-align: center;
  padding: 24px; background: rgba(10,11,13,.9);
  color: var(--ink-dim); font-size: 14px;
}
.status.show { display: flex; }
.status.warn { color: var(--hold); }

/* footer */
#foot {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 12px; border-top: 1px solid var(--rule);
  background: var(--void); color: var(--ink-faint); font: 11px/1 var(--mono);
}
#foot .grow { flex: 1; }

/* ── responsive ───────────────────────────────────────────── */
@media (max-width: 680px) {
  :root { --cols: 3px 40px 48px minmax(90px,1fr) 74px 66px; }
  .row { min-width: 0; }
  .c-best, .c-sectors, .row.head .c-best, .row.head .c-sectors { display: none; }
  .c-stint { display: none; }
  .row.head .c-stint { display: none; }
  #pinbar { grid-template-columns: var(--cols); }
  .c-driver .sub { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .row.car { transition: none; }
  .delta.show { animation: none; opacity: 1; }
  .seg.live { animation: none; opacity: 1; }
  .pulseLine::after { animation: none; }
}
