:root {
  --bg: #0d1117; --panel: #151b23; --panel2: #1b232d; --line: #2a3441; --text: #e6edf3;
  --muted: #8b98a8; --accent: #4cc2ff; --live: #ff3b47; --purple: #b46cff; --green: #36d47c;
  --yellow: #e8c547; --red: #ff5a5f; --orange: #ff9f43;
  --hdr: 48px;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 14px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.pad { padding: 16px; }

/* header */
#top { height: var(--hdr); display: flex; align-items: center; gap: 10px; padding: 0 10px;
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
#title { font-weight: 600; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 7px 12px;
  cursor: pointer; min-height: 36px; touch-action: manipulation; }
.btn:hover { border-color: #3d4a5c; }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 9px; min-height: 32px; font-size: 13px; }
.btn.accent { background: #12384d; border-color: var(--accent); color: #bfe9ff; }
.btn.on { background: #12384d; border-color: var(--accent); }
.live-badge { background: var(--live); color: #fff; font-weight: 700; font-size: 12px; letter-spacing: .08em;
  padding: 3px 8px; border-radius: 4px; animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { opacity: .55; } }

/* session list */
#viewList { max-width: 980px; margin: 0 auto; padding: 14px; }
.list-h { font-size: 15px; color: var(--muted); font-weight: 600; margin: 18px 4px 8px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 10px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px;
  cursor: pointer; display: flex; flex-direction: column; gap: 4px; text-align: left; }
.card:hover { border-color: var(--accent); }
.card .c-title { font-weight: 600; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.card .c-meta { color: var(--muted); font-size: 13px; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 4px; border: 1px solid var(--line); color: var(--muted); }
.tag.ok { color: var(--green); border-color: #1f5a3a; }
.tag.no { color: var(--orange); border-color: #5a3d1f; }
.tag.live { color: #fff; background: var(--live); border-color: var(--live); }
.card.live-card { border-color: var(--live); background: #1d1418; }

/* race layout: desktop */
#viewRace { display: grid; height: calc(100vh - var(--hdr)); height: calc(100dvh - var(--hdr));
  grid-template-columns: minmax(0, 1fr) minmax(430px, 520px); grid-template-rows: minmax(0, 1fr) auto;
  grid-template-areas: "map side" "tl side"; }
#mapWrap { grid-area: map; position: relative; min-height: 0; overflow: hidden; background: #0a0e13; }
#map { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#map.dragging { cursor: grabbing; }
.map-msg { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 16px; text-align: center; padding: 20px; pointer-events: none; }
.banner { position: absolute; top: 8px; left: 8px; right: 110px; background: #3a1d20; border: 1px solid var(--red);
  border-radius: 8px; padding: 6px 10px; font-size: 13px; }
.map-btns { position: absolute; top: 8px; right: 8px; display: flex; gap: 6px; }
.ev-bar { position: absolute; left: 8px; bottom: 8px; max-width: calc(100% - 16px); display: flex; flex-wrap: wrap; gap: 4px;
  z-index: 2; }
.ev-chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 7px; font-size: 12px; line-height: 16px;
  background: rgba(13,17,23,.82); border: 1px solid var(--line); border-radius: 12px; color: var(--muted); cursor: pointer;
  user-select: none; opacity: .6; }
.ev-chip.on { opacity: 1; color: #e6edf3; }
.ev-chip input { position: absolute; opacity: 0; width: 0; height: 0; pointer-events: none; }
.ev-chip .sw { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.ev-chip.ev-all { border-style: dashed; }
.ev-tip { position: absolute; z-index: 3; pointer-events: none; max-width: 260px; background: rgba(13,17,23,.95);
  border: 1px solid var(--line); border-radius: 6px; padding: 5px 8px; font-size: 12px; color: #e6edf3; }
.ev-tip b { color: #ffd84d; }
#feed li.hl { outline: 1px solid #ffd84d; background: rgba(255,216,77,.12); }
@media (max-width: 820px) { .ev-chip { padding: 6px 9px; font-size: 13px; } }
#timeline { grid-area: tl; background: var(--panel); border-top: 1px solid var(--line); padding: 6px 10px 10px; }
#side { grid-area: side; display: flex; flex-direction: column; min-height: 0; border-left: 1px solid var(--line);
  background: var(--panel); }
#board { flex: 1 1 60%; overflow: auto; min-height: 0; }
#feedBox { flex: 0 1 40%; min-height: 0; overflow: auto; border-top: 1px solid var(--line); }

/* timeline */
.tl-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.btn.play { width: 44px; height: 40px; font-size: 16px; }
.speeds { display: flex; gap: 4px; }
.tl-info { margin-left: auto; font-variant-numeric: tabular-nums; }
.lap-now { margin-left: 10px; font-weight: 600; }
#tlTrack { position: relative; height: 34px; margin-top: 6px; }
#tlCanvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#tlRange { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; background: transparent;
  -webkit-appearance: none; appearance: none; cursor: pointer; touch-action: none; }
#tlRange::-webkit-slider-runnable-track { background: transparent; height: 34px; }
#tlRange::-moz-range-track { background: transparent; height: 34px; }
#tlRange::-webkit-slider-thumb { -webkit-appearance: none; width: 4px; height: 34px; background: var(--accent);
  border-radius: 2px; box-shadow: 0 0 0 3px rgba(76, 194, 255, .25); }
#tlRange::-moz-range-thumb { width: 4px; height: 34px; background: var(--accent); border: 0; border-radius: 2px; }

/* leaderboard */
#lb { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; font-size: 13px; }
#lb th { position: sticky; top: 0; background: var(--panel2); color: var(--muted); font-weight: 600;
  text-align: left; padding: 6px 4px; font-size: 12px; z-index: 1; }
#lb td { padding: 0 4px; height: 30px; border-bottom: 1px solid #1f2730; white-space: nowrap; }
#lb tr { cursor: pointer; }
#lb tbody tr:hover td { background: #1a222c; }
#lb tr.sel td { background: #12384d !important; }
#lb tr.out td { opacity: .45; }
#gapHead { cursor: pointer; text-decoration: underline dotted; }
.c-pos { width: 26px; text-align: right !important; color: var(--muted); }
.c-drv b { display: inline-block; width: 34px; }
.team-bar { display: inline-block; width: 4px; height: 16px; border-radius: 2px; vertical-align: -3px; margin-right: 6px; }
.hum { color: var(--accent); font-size: 10px; margin-left: 2px; }
.st { font-size: 10px; font-weight: 700; padding: 1px 4px; border-radius: 3px; margin-left: 4px; }
.st.pit { background: #1d4f73; color: #cfeaff; }
.st.dnf { background: #5a1f24; color: #ffc9cc; }
.st.fin { background: #2c2c2c; color: #fff; border: 1px solid #666; }
.tyre { display: inline-flex; width: 18px; height: 18px; border-radius: 50%; border: 2px solid; align-items: center;
  justify-content: center; font-size: 10px; font-weight: 700; margin-right: 3px; }
.c-sec { width: 50px; text-align: center !important; }
td.c-sec { font-size: 12px; }
.sec { display: block; border-radius: 3px; padding: 1px 2px; color: #111; }
.sec.p { background: var(--purple); color: #fff; }
.sec.g { background: var(--green); }
.sec.y { background: var(--yellow); }
.sec.old { opacity: .5; }
.purple-t { color: var(--purple); font-weight: 600; }

/* event feed */
#feedBox summary { padding: 8px 10px; font-weight: 600; cursor: pointer; position: sticky; top: 0;
  background: var(--panel2); min-height: 36px; }
#feed { list-style: none; margin: 0; padding: 0; }
#feed li { display: flex; gap: 8px; padding: 6px 10px; border-bottom: 1px solid #1f2730; cursor: pointer; align-items: flex-start; }
#feed li:hover { background: #1a222c; }
#feed .ev-t { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 46px; font-size: 12px; padding-top: 1px; }
.ev-ico { font-size: 10px; font-weight: 700; min-width: 34px; text-align: center; border-radius: 3px; padding: 2px 3px; color: #111; }
.ev-OVTK { background: var(--accent); }
.ev-COLL { background: var(--red); color: #fff; }
.ev-PENA, .ev-DTSV, .ev-SGSV { background: var(--orange); }
.ev-DAMG { background: #ff7a45; }
.ev-FTLP { background: var(--purple); color: #fff; }
.ev-RTMT { background: #6b2c32; color: #fff; }
.ev-other { background: #3b4655; color: #fff; }
.ev-flag { background: #ddd; }

/* detail panel */
#detail { position: absolute; left: 10px; bottom: 10px; width: 330px; max-width: calc(100% - 20px);
  max-height: calc(100% - 20px); overflow: auto; background: rgba(21, 27, 35, .96); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px 12px; box-shadow: 0 8px 30px rgba(0, 0, 0, .5); }
.d-head { display: flex; align-items: center; gap: 8px; }
.d-head .team-bar { height: 34px; width: 5px; }
.d-name { flex: 1; }
.d-name b { font-size: 18px; }
.d-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 10px 0; }
.stat { background: var(--panel2); border-radius: 6px; padding: 4px 6px; }
.stat .k { font-size: 10px; color: var(--muted); text-transform: uppercase; }
.stat .v { font-weight: 600; font-variant-numeric: tabular-nums; }
.chart { width: 100%; height: 64px; display: block; background: #0f141b; border-radius: 6px; }
.chart-l { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin: 6px 0 2px; }
.sw { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin: 0 3px 0 6px; }
.sw.thr { background: var(--green); } .sw.brk { background: var(--red); }
.sub-h { font-size: 11px; color: var(--muted); text-transform: uppercase; margin: 10px 0 4px; }
.dmg-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.dmg { background: var(--panel2); border-radius: 5px; padding: 3px 5px; font-size: 11px; }
.dmg b { display: block; font-size: 13px; }

/* phone / narrow: stack map, timeline, board, feed */
@media (max-width: 820px) {
  #viewRace { display: block; height: auto; }
  #mapWrap { height: 52vh; height: 52svh; min-height: 260px; }
  #timeline { position: sticky; top: var(--hdr); z-index: 10; }
  #side { border-left: 0; }
  #board, #feedBox { overflow: visible; }
  #lb td { height: 38px; }
  #lb .c-best { display: none; }
  .c-sec { width: 42px; }
  #detail { position: fixed; left: 0; right: 0; bottom: 0; width: auto; max-width: none; max-height: 70vh;
    border-radius: 14px 14px 0 0; z-index: 30; }
  #feed li { padding: 10px; }
  .banner { right: 90px; }
}
@media (max-width: 420px) {
  #lb { font-size: 12px; }
  #lb .c-last { display: none; }
  .c-sec { width: 38px; }
  td.c-sec { font-size: 11px; }
  #btnHome { padding: 7px 8px; }
}

/* keyboard help popover, lapped label, damage legend */
.help-wrap { position: relative; }
.help { position: absolute; left: 0; bottom: 40px; z-index: 30; background: var(--panel2); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 12px; font-size: 12px; width: 300px; box-shadow: 0 6px 20px rgba(0,0,0,.5); }
.help[hidden] { display: none; }
.help table { border-collapse: collapse; margin-top: 6px; width: 100%; }
.help td { padding: 2px 6px 2px 0; vertical-align: top; }
.help kbd { background: #0f141b; border: 1px solid var(--line); border-radius: 4px; padding: 0 5px; font-size: 11px; }
.lapped { color: #ffb86b; font-size: 11px; white-space: nowrap; }
.dmg-legend { display: flex; align-items: center; gap: 6px; margin-top: 6px; font-size: 10px; color: var(--muted); }
.dmg-legend i { flex: 1; height: 8px; border-radius: 4px;
  background: linear-gradient(90deg, hsl(130,65%,34%) 0%, hsl(105,65%,34%) 10%, hsl(58,65%,34%) 25%, hsl(30,65%,34%) 45%, hsl(8,65%,34%) 65%, hsl(0,65%,34%) 100%); }
