:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e3e2de;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #8a8984;
  --accent: #e34948;
  --accent-ink: #ffffff;
  --fast: #2a78d6;
  --slow: #e34948;
  --neutral: #c9c8c3;
  --s1: #2a78d6;
  --s2: #eb6834;
  --s3: #1baf7a;
  --good: #1a8a4a;
  --warn: #b97800;
  --live: #e34948;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.04);
  --radius: 16px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f1115; --surface: #171a20; --surface-2: #1f232b; --border: #2a2f39;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #8b8f99;
    --fast: #3987e5; --slow: #e66767; --neutral: #4a4f5a;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70;
    --good: #3ccf7f; --warn: #f0b429; --live: #ff5a5a;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1115; --surface: #171a20; --surface-2: #1f232b; --border: #2a2f39;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8b8f99;
  --fast: #3987e5; --slow: #e66767; --neutral: #4a4f5a;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70;
  --good: #3ccf7f; --warn: #f0b429; --live: #ff5a5a;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.45 Inter, system-ui, -apple-system, Segoe UI, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(76px + env(safe-area-inset-bottom));
}
a { color: inherit; text-decoration: none; }
.mono, .t { font-family: "JetBrains Mono", ui-monospace, monospace; font-variant-numeric: tabular-nums; }
.container { max-width: 1180px; margin: 0 auto; padding: 16px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px;
  padding: 10px 16px; background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: blur(12px); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; white-space: nowrap; }
.brand b { color: var(--accent); }
.nav { display: flex; gap: 4px; margin: 0 auto; }
.nav a {
  display: flex; align-items: center; gap: 6px; padding: 8px 12px; border-radius: 10px;
  color: var(--text-2); font-weight: 500;
}
.nav a svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.nav a.active { background: var(--surface-2); color: var(--text); }
.top-actions { display: flex; align-items: center; gap: 8px; }
.icon-btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  width: 34px; height: 34px; border-radius: 10px; cursor: pointer; font-size: 16px;
}

@media (max-width: 760px) {
  /* backdrop-filter would make the fixed bottom nav position relative to the header */
  .topbar { padding: 10px 16px; backdrop-filter: none; background: var(--bg); }
  .nav {
    position: fixed; bottom: 0; left: 0; right: 0; margin: 0; justify-content: space-around;
    background: var(--surface); border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .nav a { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 8px; }
  .nav a svg { width: 22px; height: 22px; }
  .nav a.active { background: none; color: var(--accent); }
  .brand span:last-child { font-size: 14px; }
}
@media (min-width: 761px) { body { padding-bottom: 0; } }

/* building blocks */
h1 { font-size: 24px; margin: 4px 0 4px; letter-spacing: -.02em; }
h2 { font-size: 17px; margin: 0 0 12px; letter-spacing: -.01em; }
.sub { color: var(--text-2); margin: 0 0 16px; }
.muted { color: var(--muted); }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }
@media (max-width: 900px) { .g3, .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .g2 { grid-template-columns: minmax(0, 1fr); } .span2 { grid-column: auto; } }
.stack > * + * { margin-top: 16px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.between { justify-content: space-between; }

.tile .label { color: var(--text-2); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 700; letter-spacing: -.02em; margin-top: 2px; }
.tile .hint { color: var(--muted); font-size: 12px; margin-top: 2px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-2); font-size: 13px; font-weight: 500; white-space: nowrap;
}
.chip.ghost { background: transparent; border: 1px solid var(--border); }
.chip.live { background: var(--live); color: #fff; }
.chip.live::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: #fff; animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .3; } }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: thin; }
.chips a.chip.on { background: var(--text); color: var(--bg); }

.kart {
  display: inline-grid; place-items: center; min-width: 34px; height: 30px; padding: 0 6px; border-radius: 8px;
  font: 700 14px "JetBrains Mono", monospace; background: var(--surface-2); color: var(--text);
  border-bottom: 3px solid var(--neutral);
}
.kart.fast { border-bottom-color: var(--fast); }
.kart.slow { border-bottom-color: var(--slow); }

.eff { font: 600 13px "JetBrains Mono", monospace; white-space: nowrap; }
.eff.fast { color: var(--fast); }
.eff.slow { color: var(--slow); }

/* tables */
.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: 8px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 9px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover td { background: var(--surface-2); }
td.num, th.num { text-align: right; }
.pos { font-weight: 700; width: 28px; color: var(--text-2); }
.p1 { color: #c99a00; } .p2 { color: #8f9aa6; } .p3 { color: #b0703a; }
@media (max-width: 640px) { .g2, .g3 { grid-template-columns: minmax(0, 1fr); } .span2 { grid-column: auto; } .container { padding: 16px; } h1 { font-size: 21px; } .tile .value { font-size: 22px; } }
@media (max-width: 600px) { .hide-sm { display: none; } td, th { padding: 8px 6px; } }

/* kart ranking bars */
.rank-list { display: grid; gap: 2px; }
.rank-item {
  display: grid; grid-template-columns: 26px 46px 1fr 74px; gap: 10px; align-items: center;
  padding: 8px 6px; border-radius: 10px; cursor: pointer;
}
.rank-item:hover { background: var(--surface-2); }
.rank-item .r { color: var(--muted); font-weight: 600; font-size: 13px; text-align: right; }
.bar { position: relative; height: 10px; }
.bar .axis { position: absolute; left: 50%; top: -3px; bottom: -3px; width: 1px; background: var(--border); }
.bar .fill { position: absolute; top: 0; bottom: 0; border-radius: 4px; }
.bar .fill.fast { background: var(--fast); }
.bar .fill.slow { background: var(--slow); }
.bar .ci { position: absolute; top: 4px; height: 2px; background: var(--muted); opacity: .45; }
.rank-item.inactive { opacity: .45; }

.search {
  width: 100%; padding: 11px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; outline: none;
}
.search:focus { border-color: var(--text-2); }
.btn {
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: 500 14px Inter, sans-serif;
  padding: 8px 14px; border-radius: 10px; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.back { color: var(--text-2); font-size: 14px; display: inline-flex; gap: 4px; margin-bottom: 6px; }

.luck { display: inline-flex; align-items: center; gap: 6px; }
.luck .meter { width: 54px; height: 6px; border-radius: 3px; background: var(--surface-2); position: relative; overflow: hidden; }
.luck .meter i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 3px; background: var(--text-2); }
.luck.lucky .meter i { background: var(--fast); }
.luck.unlucky .meter i { background: var(--slow); }

.chart-box { position: relative; height: 280px; }
.chart-box.sm { height: 200px; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--text-2); margin-bottom: 8px; }
.legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }

.spark { width: 120px; height: 26px; display: block; }
.spark path { fill: none; stroke: var(--s1); stroke-width: 1.5; }
.spark circle { fill: var(--s1); }

.wx-now { display: flex; align-items: center; gap: 16px; }
.wx-now .big { font-size: 46px; font-weight: 700; letter-spacing: -.03em; }
.wx-now .emoji { font-size: 48px; }
.forecast { display: grid; grid-template-columns: repeat(7, minmax(84px, 1fr)); gap: 8px; overflow-x: auto; }
.fc { text-align: center; padding: 10px 6px; border-radius: 12px; background: var(--surface-2); }
.fc .d { font-size: 12px; color: var(--text-2); font-weight: 600; }
.fc .e { font-size: 26px; margin: 4px 0; }
.fc .tt { font-weight: 600; }
.fc .p { font-size: 12px; color: var(--fast); }
.fc.closed { opacity: .5; }

.empty { text-align: center; padding: 48px 16px; color: var(--text-2); }
.loading { padding: 48px; text-align: center; color: var(--muted); }
.foot { max-width: 1180px; margin: 0 auto; padding: 8px 16px 24px; color: var(--muted); font-size: 12px; display: flex; gap: 12px; flex-wrap: wrap; justify-content: space-between; }
.note { font-size: 13px; color: var(--text-2); background: var(--surface-2); border-radius: 12px; padding: 10px 12px; }
.lap-best { color: var(--fast); font-weight: 700; }
.laps-cell { display: flex; gap: 6px; font: 500 12px "JetBrains Mono", monospace; color: var(--text-2); }
.err { color: var(--slow); }

/* phone-only helpers */
.show-sm, .show-sm-inline { display: none; }
.sub-val { font-size: 11px; font-weight: 500; }
@media (max-width: 600px) {
  .show-sm { display: block; }
  .show-sm-inline { display: inline; }
  td.ellip { max-width: 130px; overflow: hidden; text-overflow: ellipsis; }
  .card { padding: 14px; border-radius: 14px; }
  .table-wrap { margin: 0 -14px; padding: 0 14px; }
  .grid { gap: 12px; }
  .chart-box { height: 230px; }
  .top-actions .chip { display: none; }
  .row.between > .row { width: 100%; }
}
.laps-table th:first-child, .laps-table td:first-child {
  position: sticky; left: 0; z-index: 1; background: var(--surface);
  box-shadow: 6px 0 6px -6px rgba(0,0,0,.4);
}
.date-in { width: auto; padding: 8px 10px; flex: 1 1 140px; min-width: 0; color-scheme: inherit; }
