:root {
    --bg: #0f1115;
    --panel: #171a21;
    --panel2: #1e222b;
    --line: #2a2f3a;
    --text: #e6e8ec;
    --muted: #9aa3b2;
    --accent: #ffcc00;
    --accent2: #38bdf8;
    --good: #34d399;
}

* { box-sizing: border-box; }

/* Hide scrollbar chrome on every page (page body + the overflow-x table wrappers)
   while keeping the content scrollable via wheel/trackpad/drag. */
* { scrollbar-width: none; -ms-overflow-style: none; }
*::-webkit-scrollbar { width: 0; height: 0; display: none; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

a { color: var(--accent2); text-decoration: none; }
a:hover { text-decoration: underline; }

.topbar {
    display: flex;
    align-items: center;
    gap: 20px;
    padding: 10px 18px;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    position: sticky;
    top: 0;
    z-index: 10;
    flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 18px; color: var(--text); }
.topbar nav { display: flex; gap: 6px; flex-wrap: wrap; }
.topbar nav a {
    padding: 6px 12px;
    border-radius: 6px;
    color: var(--muted);
}
.topbar nav a:hover { background: var(--panel2); text-decoration: none; }
.topbar nav a.active { background: var(--panel2); color: var(--accent); }

main { max-width: 1100px; margin: 0 auto; padding: 18px; }

h1 { font-size: 22px; margin: 4px 0 14px; }
h2 { font-size: 17px; margin: 20px 0 10px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 14px;
    margin-bottom: 16px;
}

table { border-collapse: collapse; width: 100%; }
th, td { padding: 6px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
tbody tr:hover { background: var(--panel2); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.kart { display: inline-block; min-width: 22px; padding: 1px 6px; border-radius: 5px; background: #2b2f3a; color: var(--accent); font-weight: 700; text-align: center; }
.rid { margin-left: 5px; color: var(--muted); font-size: 0.82em; font-variant-numeric: tabular-nums; }
/* Race type ("Adult Race", "Junior Race", a league qualifier…) shown beside a heat's time. */
.rtype { display: inline-block; margin-left: 6px; padding: 1px 7px; border-radius: 20px; background: var(--panel2); color: var(--muted); font-size: 0.82em; font-weight: 600; }
.racerlink { color: var(--text); font-weight: 600; }
.racerlink:hover { color: var(--accent2); text-decoration: underline; }
/* "that's me" highlight */
tr.me, tr.me:hover { background: rgba(255, 196, 0, 0.16); }
tr.me td { font-weight: 600; }
.me-tag, td.cell.me, th.me { box-shadow: inset 0 0 0 2px rgba(255, 196, 0, 0.55); border-radius: 4px; }
.name.me, .rn.me { color: #ffce3a; font-weight: 700; }

.table-wrap { overflow-x: auto; }

/* whole-row link (e.g. /history rows -> heat detail) */
tr.rowlink { cursor: pointer; }
tr.rowlink:hover { background: var(--panel2); }

/* click-to-sort column headers */
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th.sortable .arrow { opacity: .45; font-size: 10px; margin-left: 3px; }
th.sortable.sorted { color: var(--accent); }
th.sortable.sorted .arrow { opacity: 1; }

/* server-side sort headers (e.g. /alltimes): the whole header is a link */
th a.sortlink { color: inherit; text-decoration: none; cursor: pointer; user-select: none; display: inline-block; }
th a.sortlink:hover { color: var(--text); }
th a.sortlink .arrow { opacity: .45; font-size: 10px; margin-left: 3px; }

.controls { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg a { padding: 6px 12px; color: var(--muted); }
.seg a.active { background: var(--accent); color: #111; font-weight: 700; }
.seg a:hover { text-decoration: none; }

select, button {
    background: var(--panel2);
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 8px;
    padding: 6px 10px;
    font: inherit;
}

.badge { font-size: 11px; padding: 2px 7px; border-radius: 20px; background: var(--panel2); color: var(--muted); }
.badge.live { background: #3b0d0d; color: #ff6b6b; }
.badge.on { background: #0d3b1f; color: var(--good); }

/* live lap grid */
.grid-wrap { overflow-x: auto; }
.lapgrid td, .lapgrid th { border: 1px solid var(--line); text-align: center; font-size: 12px; padding: 3px 4px; }
/* Narrow cells: name / id / kart / time stack vertically; long names wrap instead of
   forcing the column wide (overrides the global `td { white-space: nowrap }`). */
.lapgrid .cell { min-width: 46px; max-width: 68px; white-space: normal; }
.lapgrid .cell .rn { color: var(--text); font-weight: 600; line-height: 1.12; word-break: break-word; }
.lapgrid .cell .rid { margin-left: 0; display: block; line-height: 1.15; }
.lapgrid .cell .kline { line-height: 1.4; margin: 1px 0; }
.lapgrid .cell .lt { color: var(--muted); font-variant-numeric: tabular-nums; margin-top: 1px; }
.lapgrid .best .lt { color: var(--good); }
/* fastest lap time across all racers on that lap (the lap leader) */
.lapgrid td.leadlap { background: rgba(94, 168, 255, 0.20); }
.lapgrid td.leadlap .lt { color: #cfe2ff; font-weight: 700; }
.lapgrid td.leadlap.best .lt { color: var(--good); }
.leadlap-key { color: #cfe2ff; font-weight: 700; }
.best-key { color: var(--good); font-weight: 700; }
/* highlight the hovered scoreboard racer's cells across the whole grid */
.lapgrid td.cell.hl { background: rgba(255, 196, 0, 0.22); box-shadow: inset 0 0 0 2px rgba(255, 196, 0, 0.55); }
.lapgrid td.cell.hl .rn, .lapgrid td.cell.hl .lt { color: var(--text); }

td.active-col, th.active-col { background: rgba(255,204,0,.08); color: var(--accent); }

/* Hall of Fame record cards */
.records { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.record { display: block; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 14px; color: var(--text); }
.record:hover { text-decoration: none; border-color: var(--accent); background: #232733; }
.record .rec-icon { font-size: 22px; line-height: 1; }
.record .rec-label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-top: 8px; }
.record .rec-value { font-size: 26px; font-weight: 800; margin-top: 2px; font-variant-numeric: tabular-nums; color: var(--accent); }
.record .rec-value .rec-unit { font-size: 14px; font-weight: 600; color: var(--muted); margin-left: 2px; }
.record .rec-who { font-size: 13px; color: var(--accent2); margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* popularity heatmap */
.heat td { text-align: center; font-size: 12px; padding: 0; }
.heat .rowlabel { text-align: right; color: var(--muted); padding: 4px 6px; }
/* clickable slot: the anchor fills the whole cell so the entire box is a target */
.heat td .slotlink,
.heat td .slotempty { display: block; padding: 4px 6px; min-height: 1.2em; text-decoration: none; }
.heat td .slotlink:hover { text-decoration: none; box-shadow: inset 0 0 0 2px var(--accent); }
/* ring marking the track's open hours */
.heat td.open { box-shadow: inset 0 0 0 2px rgba(239,68,68,.70); }

/* ---- Live page enhancements ---- */

/* Scoreboard legend */
.legend { display: flex; flex-wrap: wrap; gap: 8px 18px; margin: 0 0 12px; font-size: 12px; color: var(--muted); }
.legend .lg { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.legend .sw { font-size: 15px; line-height: 1; }

/* F1 hot-lap coloring on the scoreboard: purple = fastest lap of the heat, green = personal best */
td.hotlap { color: #d8b4fe; font-weight: 700; }
td.pb { color: var(--good); font-weight: 600; }

/* Ticking "current lap" column: normal, approaching expected lap length, overdue */
td.curlap { font-variant-numeric: tabular-nums; }
td.curlap.due { color: var(--accent); }
td.curlap.overdue { color: #ff6b6b; font-weight: 700; }

/* Lap-trend sparkline */
.spark-cell { padding: 2px 8px; }
svg.spark { display: inline-block; vertical-align: middle; }
svg.spark .sparkline { fill: none; stroke: var(--accent2); stroke-width: 1.5; }
/* fastest lap of the series: hollow amber ring, so the green/red trend segments stay unambiguous */
svg.spark .sparkdot { fill: none; stroke: var(--accent); stroke-width: 1.4; }

/* Record / hot-lap banner */
#liveAlert { display: none; margin: 0 0 14px; padding: 11px 15px; border-radius: 8px;
    background: #2a1746; border: 1px solid #6d28d9; color: #e9d5ff; font-weight: 600; }
#liveAlert.show { display: block; animation: alertpulse 0.9s ease; }
#liveAlert.record { background: #3a2a06; border-color: var(--accent); color: #ffe58a; }
@keyframes alertpulse { from { transform: scale(1.02); } to { transform: scale(1); } }

/* Combined battle chart + lap-by-lap grid: position lines behind per-lap labels.
   The chart is sized in JS to the container width (one column per lap), so it always fits with
   no horizontal scroll; height grows with the number of positions. */
.battle-wrap { overflow: hidden; }
svg.racesvg { display: block; width: 100%; height: auto; }

/* The scoreboard has 15 columns, too many to read inside the 1100px content column. Break its
   wrapper out wider than <main> (centered, capped at 1500px / 96vw) AND shrink the cell font so
   every cell is legible with no horizontal scroll. Column widths come from the <colgroup>. */
.table-wrap.wide { width: min(1500px, 96vw); margin-left: 50%; transform: translateX(-50%); }
table.scoreboard { table-layout: fixed; width: 100%; font-size: 12px; }
table.scoreboard th { font-size: 10px; }
table.scoreboard th, table.scoreboard td { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-left: 5px; padding-right: 5px; }
table.scoreboard .spark-cell { padding: 2px 4px; }
table.scoreboard svg.spark { width: 100%; height: 20px; }
table.scoreboard .rid { display: none; } /* the racer id doesn't fit the narrowed column; name is enough */

/* Phones: 15 columns can't fit legibly in a ~380px viewport, so instead of crushing every cell to
   a few pixels, let the table size to its content and scroll horizontally (swipe). The colgroup
   percentage widths are dropped so auto layout picks readable per-column widths. */
@media (max-width: 760px) {
    .table-wrap.wide { width: 100vw; margin-left: 50%; transform: translateX(-50%);
        overflow-x: auto; -webkit-overflow-scrolling: touch; }
    table.scoreboard { table-layout: auto; width: max-content; min-width: 100%; font-size: 13px; }
    table.scoreboard th { font-size: 11px; }
    table.scoreboard th, table.scoreboard td { padding-left: 8px; padding-right: 8px; white-space: nowrap; }
    table.scoreboard colgroup col { width: auto !important; }
    table.scoreboard .spark-cell { min-width: 64px; }
    table.scoreboard svg.spark { width: 60px; }
}

svg.racesvg { display: block; }
svg.racesvg .plabel, svg.racesvg .llabel { fill: var(--muted); font-size: 11px; }
svg.racesvg .bline { fill: none; stroke-width: 2; opacity: 0.5; }
svg.racesvg .bline.me { stroke-width: 3.5; opacity: 0.8; }
/* Labels sit on top of the lines; a panel-colored stroke haloes the glyphs so the line reads behind. */
svg.racesvg .rname { font-size: 11px; font-weight: 600; paint-order: stroke; stroke: var(--panel); stroke-width: 3px; stroke-linejoin: round; }
svg.racesvg .rtime { font-size: 10px; fill: var(--muted); font-variant-numeric: tabular-nums; paint-order: stroke; stroke: var(--panel); stroke-width: 3px; stroke-linejoin: round; }
svg.racesvg .rnode.me .rname { font-weight: 800; }
/* Hover spotlight (driven from the scoreboard rows) */
svg.racesvg .bline.dim { opacity: 0.1; }
svg.racesvg .rnode.dim { opacity: 0.28; }
svg.racesvg .bline.hot { opacity: 1; stroke-width: 3.5; }
svg.racesvg .rnode.hot .rname { font-weight: 800; }

/* ---- Track map (live spatial view) + track editor ---- */
.editlink { font-size: 12px; font-weight: 600; margin-left: 10px; }
.tv-wrap, .te-canvas-wrap { padding: 0; overflow: hidden; }
#tvCanvas, #teCanvas { display: block; width: 100%; height: auto; border-radius: 10px; }
#teCanvas { cursor: crosshair; touch-action: none; }

.te-toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin: 10px 0; }
.te-toolbar label { display: inline-flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.te-toolbar input[type=text], .te-toolbar input[type=password], .te-toolbar select {
    background: var(--panel2); color: var(--text); border: 1px solid var(--line);
    border-radius: 6px; padding: 6px 8px; font: inherit; font-size: 13px; text-transform: none;
}
.te-toolbar .te-width { min-width: 150px; }
.te-toolbar input[type=range] { accent-color: var(--accent2); }

.btn { background: var(--panel2); color: var(--text); border: 1px solid var(--line);
    border-radius: 6px; padding: 7px 12px; font: inherit; font-size: 13px; cursor: pointer; }
.btn:hover { border-color: var(--accent2); }
.btn.primary { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 700; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: #ff6b6b; }
.btn.danger:hover { border-color: #ff6b6b; }

.te-status { align-self: center; }
.te-status.dirty { color: var(--accent); }
.te-status.ok { color: var(--good); }
.te-status.err { color: #ff6b6b; }
