:root {
  --bg: #f4f4f1;
  --surface: #ffffff;
  --text: #17181a;
  --muted: #62656b;
  --line: #dddcd6;
  --accent: #d6281b;      /* race red */
  --best: #7a2bd6;        /* purple = fastest, as in timing screens */
  --good: #1f8a4c;
  --chip: #ecebe6;
  --row-hover: #f6f5f0;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111214;
    --surface: #1a1b1e;
    --text: #ecebe6;
    --muted: #9a9ca2;
    --line: #2c2d31;
    --accent: #ff4a3d;
    --best: #b884ff;
    --good: #4cc27f;
    --chip: #25262a;
    --row-hover: #222327;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}
a { color: inherit; }
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }

header {
  display: flex;
  align-items: center;
  gap: 16px 24px;
  flex-wrap: wrap;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand {
  display: flex; align-items: center; gap: 8px;
  font-weight: 700; font-size: 16px; text-decoration: none; color: var(--accent);
}
.filters { display: flex; gap: 12px; flex-wrap: wrap; }
.filters label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
select {
  font: inherit; color: var(--text); background: var(--chip);
  border: 1px solid var(--line); border-radius: 6px; padding: 4px 8px;
}
.totals { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
.chip { background: var(--chip); border-radius: 999px; padding: 3px 10px; font-size: 12px; color: var(--muted); }
.chip b { color: var(--text); font-weight: 600; }

main { flex: 1; display: flex; min-height: 0; }
#map-wrap { flex: 1 1 58%; position: relative; display: flex; min-height: 300px; }
#map { flex: 1; background: #20242a; }
.map-size { display: none; }
.updated { font-size: 12px; color: var(--muted); }
#panel {
  flex: 0 0 42%; max-width: 620px; overflow-y: auto;
  background: var(--surface); border-left: 1px solid var(--line); padding: 16px 18px 32px;
}
@media (max-width: 820px) {
  /* Phone: page scrolls as a whole; map on top, results below */
  html, body { height: auto; }
  body { display: block; }
  header { position: sticky; top: 0; z-index: 1000; gap: 8px 12px; padding: 8px 12px;
           padding-top: max(8px, env(safe-area-inset-top)); }
  .brand { font-size: 15px; }
  .filters { flex: 1 1 100%; gap: 8px; }
  .filters label { flex: 1; font-size: 0; gap: 0; }       /* labels hidden, selects say enough */
  .filters select { flex: 1; width: 100%; font-size: 15px; min-height: 40px; }
  .totals { display: none; }
  main { display: block; }
  #map-wrap { height: 34vh; min-height: 220px; transition: height .2s; }
  #map-wrap.big { height: 68vh; }
  .map-size {
    display: block; position: absolute; right: 10px; bottom: 26px; z-index: 500;
    width: 40px; height: 40px; border-radius: 8px; border: 0; font-size: 20px;
    background: rgba(17, 18, 20, .8); color: #fff; cursor: pointer;
  }
  #panel { max-width: none; border-left: 0; border-top: 1px solid var(--line); overflow: visible;
           padding: 14px 14px calc(28px + env(safe-area-inset-bottom)); }
  h1 { font-size: 22px; }
  .back { padding: 10px 0; font-size: 15px; }
  th, td { padding: 10px 6px; }
  .card { padding: 14px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  /* Wide tables: first column stays put while the rest scrolls */
  .table-wrap { margin: 0 -14px; padding: 0 14px; -webkit-overflow-scrolling: touch; }
  table.sectors th:first-child, table.sectors td:first-child {
    position: sticky; left: -14px; background: var(--surface); z-index: 1; padding-left: 14px;
  }
  .leaflet-control-layers-toggle { width: 40px !important; height: 40px !important; }
}

h1 { font-size: 20px; margin: 0 0 2px; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 22px 0 8px; }
.sub { color: var(--muted); margin: 0 0 12px; }
.back { background: none; border: 0; color: var(--muted); font: inherit; padding: 0; margin-bottom: 8px; cursor: pointer; }
.back:hover { color: var(--text); }
.empty { color: var(--muted); padding: 24px 0; }

.cards { display: grid; gap: 10px; }
.card {
  display: block; width: 100%; text-align: left; font: inherit; color: inherit; cursor: pointer;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px;
}
.card:hover, .card:focus-visible { border-color: var(--accent); outline: none; }
.card .name { font-weight: 650; font-size: 15px; }
.card .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.card .rec { margin-top: 6px; }
.card .rec .time { color: var(--best); font-weight: 650; }

.table-wrap { overflow-x: auto; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 7px 6px; text-align: right; white-space: nowrap; }
th { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); border-bottom: 1px solid var(--line); }
td { border-bottom: 1px solid var(--line); }
th:first-child, td:first-child, .left { text-align: left; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--row-hover); }
tr.sel { background: var(--row-hover); box-shadow: inset 3px 0 0 var(--accent); }
.pos { color: var(--muted); width: 1%; }
.best { color: var(--best); font-weight: 650; }
.gap { color: var(--muted); }
.spark { vertical-align: middle; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; margin: 10px 0 4px; }
.stat { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
.stat .k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat .v { font-size: 18px; font-weight: 650; }
.stat .v.best { color: var(--best); }

.laps svg { width: 100%; height: auto; display: block; }
.legend { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; margin-top: 8px; }
.legend .bar { height: 8px; width: 120px; border-radius: 4px; background: linear-gradient(90deg, #2b6cff, #18c08f, #f2c94c, #ff4a3d); }

.leaflet-tooltip.track-label {
  background: rgba(17, 18, 20, .82); color: #fff; border: 0; border-radius: 4px;
  font: 600 12px system-ui, sans-serif; padding: 2px 6px; box-shadow: none;
}
.leaflet-tooltip.track-label::before { display: none; }

/* Sectors / apex tables */
table.sectors th, table.sectors td { padding: 6px 5px; font-size: 13px; }
tr.static { cursor: default; }
tr.static:hover { background: none; }
.s-purple { color: var(--best); font-weight: 650; }
.s-green { color: var(--good); font-weight: 600; }
.muted { color: var(--muted); }
.series { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; color: var(--text); margin-top: 6px; }
.series i, .dot { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.dot { border-radius: 50%; }
.corner-label {
  background: rgba(17, 18, 20, .85); color: #fff; border-radius: 3px;
  font: 700 11px/18px system-ui, sans-serif; text-align: center;
}

/* Layout editor */
.edit-btn { font: inherit; font-weight: 600; background: var(--chip); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 12px; min-height: 40px; cursor: pointer; }
.seg2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 8px 0 4px; }
.seg2 button { font: inherit; font-weight: 600; min-height: 44px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--bg); color: var(--text); cursor: pointer; }
.seg2 button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.hint { min-height: 1.4em; }
.edit-list { list-style: none; padding: 0; margin: 0; }
.edit-list li { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); transition: background .3s; }
.edit-list li.flash { background: var(--row-hover); }
.edit-list input { flex: 1; min-width: 0; font: inherit; font-size: 16px; color: var(--text); background: var(--bg);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; min-height: 38px; }
.edit-list .muted { flex: 1; }
.tag-pill { font: 700 12px system-ui, sans-serif; border-radius: 4px; padding: 3px 6px; color: #111; white-space: nowrap; }
.tag-pill.pc { background: #f2c94c; }
.tag-pill.ps { background: #fff; border: 1px solid var(--line); }
.edit-actions { display: flex; gap: 8px; align-items: center; margin-top: 16px; flex-wrap: wrap; }
.edit-actions .primary { font: inherit; font-weight: 600; background: var(--accent); color: #fff; border: 0; border-radius: 8px;
  min-height: 44px; padding: 0 22px; cursor: pointer; }
.edit-actions .small, .edit-list .small { font: inherit; font-size: 13px; font-weight: 600; background: var(--chip); color: var(--text);
  border: 0; border-radius: 8px; min-height: 36px; padding: 0 12px; cursor: pointer; }
.edit-list .small.danger { color: var(--accent); }
.edit-corner { background: #f2c94c; color: #111; border-radius: 4px; font: 700 12px/22px system-ui, sans-serif;
  text-align: center; box-shadow: 0 1px 3px rgba(0,0,0,.5); }
.edit-split { background: #fff; color: #111; border-radius: 3px; font: 700 11px/18px system-ui, sans-serif;
  text-align: center; box-shadow: 0 1px 3px rgba(0,0,0,.5); }
