:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface-1: #fcfcfb;
  --surface-2: #efefec;
  --border: #e0dfdb;
  --grid: #e9e8e4;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --accent: #2a78d6;
  --danger: #c43b3a;
  --good: #1b7f3b;
  --status-good: #1b9a3e;
  --status-warn: #b89400;
  --status-crit: #c0302f;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-8: #e34948;
  --series-other: #9a9994;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110;
    --surface-1: #1a1a19;
    --surface-2: #242422;
    --border: #2f2f2c;
    --grid: #2a2a28;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #8e8d86;
    --accent: #3987e5;
    --danger: #e66767;
    --good: #4cbf6b;
    --status-good: #2fa24a;
    --status-warn: #a8920a;
    --status-crit: #b3261e;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
    --series-8: #e66767;
    --series-other: #6d6c67;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110;
  --surface-1: #1a1a19;
  --surface-2: #242422;
  --border: #2f2f2c;
  --grid: #2a2a28;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #8e8d86;
  --accent: #3987e5;
  --danger: #e66767;
  --good: #4cbf6b;
  --status-good: #2fa24a;
  --status-warn: #a8920a;
  --status-crit: #b3261e;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-7: #9085e9;
  --series-8: #e66767;
  --series-other: #6d6c67;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font: 15px/1.45 ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 26px; letter-spacing: -0.02em; }
h2 { font-size: 17px; }
.num, td.num, th.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  max-width: 1200px; margin: 0 auto; padding: 14px 16px;
}
.brand { display: flex; gap: 10px; align-items: center; font-weight: 700; color: var(--text-primary); letter-spacing: -0.01em; }
.brand:hover { text-decoration: none; }
.flag {
  width: 22px; height: 22px; border-radius: 5px;
  background: conic-gradient(var(--text-primary) 25%, var(--surface-1) 0 50%, var(--text-primary) 0 75%, var(--surface-1) 0) 0 0 / 11px 11px;
  border: 1px solid var(--text-primary);
}
.theme-toggle { background: none; border: 1px solid var(--border); color: var(--text-secondary); border-radius: 8px; width: 34px; height: 34px; cursor: pointer; font-size: 16px; }

.wrap { max-width: 1200px; margin: 0 auto; padding: 0 16px 64px; }
.page-head { display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: flex-end; justify-content: space-between; margin: 8px 0 20px; }
.page-head .meta { color: var(--text-secondary); margin-top: 4px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card + .card, .section + .section { margin-top: 16px; }
.section-head { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: baseline; justify-content: space-between; margin-bottom: 12px; }
.section-head p { margin: 0; color: var(--text-muted); font-size: 13px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  font: inherit; font-size: 14px; font-weight: 550;
  padding: 7px 12px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--text-primary); color: var(--surface-1); border-color: var(--text-primary); }
.btn.primary:hover { opacity: 0.9; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 8px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: default; }

/* Race list */
.race-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.race-card { display: block; color: inherit; }
.race-card:hover { text-decoration: none; border-color: var(--text-muted); }
.race-card h2 { margin-bottom: 4px; }
.race-card .drivers { margin-top: 10px; font-size: 13px; color: var(--text-secondary); }

/* Teams */
.teams { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); margin-bottom: 12px; }
.team { display: flex; gap: 12px; align-items: stretch; background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.team-bar { width: 6px; border-radius: 3px; flex: none; }
.team-name { font-weight: 700; font-size: 17px; }
.team .muted { font-size: 13px; }

/* Stat tiles */
.tiles { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin-bottom: 16px; }
.tiles .card, .race-grid .card { margin-top: 0; }
.tile .label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); }
.tile .value { font-size: 28px; font-weight: 650; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile .sub { color: var(--text-secondary); font-size: 13px; }

/* Driver chips (filter row) */
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 7px;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary);
  border-radius: 999px; padding: 4px 11px 4px 8px; font: inherit; font-size: 13px; cursor: pointer;
}
.chip[aria-pressed="false"] { color: var(--text-muted); background: transparent; }
.chip[aria-pressed="false"] .swatch { opacity: 0.25; }
.chip.tab[aria-pressed="false"] { color: var(--text-secondary); }
.chip.tab[aria-pressed="false"] .swatch { opacity: 1; }
.chip.tab[aria-pressed="true"] { background: var(--surface-2); border-color: var(--text-muted); font-weight: 600; }
.show-sm { display: none; }
.team-sub { white-space: normal; font-size: 12px; font-weight: 400; color: var(--text-muted); }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; display: inline-block; }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-weight: 550; font-size: 12px; color: var(--text-muted); padding: 6px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 8px 10px; border-bottom: 1px solid var(--grid); }
tr:last-child td { border-bottom: none; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover, tbody tr.focused { background: var(--surface-2); }
.driver-cell { display: flex; align-items: center; gap: 8px; font-weight: 600; white-space: nowrap; }
.pill { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 4px; background: var(--surface-2); color: var(--text-secondary); }
.pill.best { background: color-mix(in srgb, var(--good) 18%, transparent); color: var(--good); }
.best-time { color: var(--good); font-weight: 650; }
tr.flagged td { color: var(--text-muted); }

/* Charts */
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart text { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .crosshair { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.tooltip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px rgb(0 0 0 / 0.12); padding: 8px 10px; font-size: 13px; min-width: 170px;
}
.tooltip .tt-head { font-weight: 650; margin-bottom: 4px; }
.tooltip .tt-row { display: flex; align-items: center; gap: 8px; }
.tooltip .tt-row .num { margin-left: auto; padding-left: 12px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; color: var(--text-secondary); margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.spread svg { display: block; }
.bar-cell { width: 34%; min-width: 120px; }
.delta-bar { height: 6px; border-radius: 0 3px 3px 0; background: var(--text-muted); opacity: 0.45; }

/* Driver focus */
.focus-tabs { display: flex; gap: 6px; flex-wrap: wrap; }

/* Race Monitor-style lap list */
.rm { margin: 0 -16px -16px; border-top: 1px solid var(--border); }
.rm-head { background: var(--surface-2); padding: 12px 16px 14px; }
.rm-total { font-size: 17px; color: var(--text-secondary); padding-left: 4px; }
.rm-total span { margin-right: 28px; }
.rm-big { font-size: clamp(40px, 9vw, 64px); font-weight: 500; line-height: 1.05; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }
.rm-sub { display: flex; flex-wrap: wrap; gap: 4px 40px; margin-top: 6px; }
.rm-sub span { display: block; font-size: 14px; color: var(--text-secondary); }
.rm-sub b { font-size: 26px; font-weight: 500; font-variant-numeric: tabular-nums; }
.rm-cols, .rm-row {
  display: grid; align-items: center; column-gap: 12px; padding: 0 16px;
  grid-template-columns: 44px 36px var(--dw, 90px) minmax(130px, 260px) 72px 82px 1fr;
}
.rm-cols { font-weight: 650; font-size: 15px; padding-top: 10px; padding-bottom: 6px; }
.rm-row { border-bottom: 1px solid var(--grid); padding-top: 6px; padding-bottom: 0; font-variant-numeric: tabular-nums; }
.rm-lap, .rm-pos { font-size: 19px; }
.rm-pos { color: var(--text-secondary); }
.rm-time { position: relative; font-size: 22px; padding-bottom: 8px; white-space: nowrap; }
.rm-bar { position: absolute; left: 0; bottom: 3px; height: 3px; border-radius: 0 2px 2px 0; }
.rm-bar.good, .rm-key.good { background: var(--status-good); }
.rm-bar.warn, .rm-key.warn { background: var(--status-warn); }
.rm-bar.crit, .rm-key.crit { background: var(--status-crit); }
.rm-delta { font-size: 14px; color: var(--text-secondary); }
.rm-delta.faster { color: var(--good); }
.rm-note { justify-self: end; }
.rm-legend { display: flex; flex-wrap: wrap; gap: 6px 16px; padding: 12px 16px; font-size: 12px; color: var(--text-muted); }
.rm-legend span { display: inline-flex; align-items: center; gap: 6px; }
.rm-key { width: 16px; height: 3px; border-radius: 2px; display: inline-block; }

/* Kart lap list: driver stints */
.kart-tabs { display: grid; gap: 10px; }
.kart-tab-group { display: flex; flex-wrap: wrap; gap: 6px 10px; align-items: center; }
.kart-tab-label { display: inline-flex; align-items: center; gap: 6px; font-weight: 650; font-size: 14px; min-width: 150px; }
.rm-team { display: flex; align-items: center; gap: 8px; font-weight: 650; font-size: 15px; margin-bottom: 4px; }
.rm-toolbar { display: flex; gap: 8px; justify-content: flex-end; padding: 10px 16px 4px; }
.stint-summary { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.stint-sum {
  display: inline-flex; align-items: center; gap: 6px; font-size: 13px;
  background: var(--surface-1); color: var(--text-secondary); border: 1px solid var(--border); border-radius: 8px; padding: 5px 9px;
}
.stint-sum b { color: var(--text-primary); }
.stint-sum.focused { border-color: var(--stint-color); box-shadow: inset 3px 0 0 var(--stint-color); }
.rm-row { border-left: 4px solid transparent; }
.rm-row.focused { border-left-color: var(--stint-color); background: color-mix(in srgb, var(--stint-color) 7%, transparent); }
.rm-row.handover { border-top: 2px solid var(--stint-color); }
.rm-driver { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; min-width: 0; }
.rm-driver > span:not(.avatar) { overflow: hidden; text-overflow: ellipsis; }
.rm-driver .dn-short, .rm-time .t-short { display: none; }
.rm-row.focused .rm-driver { color: var(--text-primary); font-weight: 600; }
.chart .change { stroke: var(--text-muted); stroke-width: 1; opacity: 0.45; }
.chart .bridge { stroke: var(--text-muted); stroke-width: 1.5; stroke-dasharray: 5 4; opacity: 0.75; }
.chart .change-label { fill: var(--text-secondary); font-size: 11px; }

/* Profile pictures */
/* The ring is a border filled by --ring, which can be a color or a gradient (split team/personal color). */
.avatar {
  border-radius: 50%; object-fit: cover; flex: none; box-sizing: border-box;
  border: 2px solid transparent;
  background: linear-gradient(var(--surface-2), var(--surface-2)) padding-box, var(--ring, var(--border)) border-box;
}
.avatar.placeholder { width: 48px; height: 48px; display: inline-flex; align-items: center; justify-content: center; font-size: 15px; font-weight: 650; color: var(--text-secondary); letter-spacing: 0.02em; line-height: 1; }
.photo-picker { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.photo-picker .btn, .row-actions .btn { white-space: nowrap; }
.row-actions { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; align-items: center; }
.driver-name { align-items: center !important; }
.driver-name .avatar { margin: 3px; }
.photo-picker .avatar { margin: 3px; }
.btn.disabled { opacity: 0.5; pointer-events: none; }
.team .avatar { align-self: center; margin: 0 2px; }
.rm-team .avatar { margin-right: 4px; }

/* Teams and rosters */
.members { display: flex; flex-wrap: wrap; gap: 6px; }
.member { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; padding: 3px 8px 3px 4px; border: 1px solid var(--border); border-radius: 999px; background: var(--surface-1); color: var(--text-primary); white-space: nowrap; }
a.member:hover { text-decoration: none; border-color: var(--text-muted); }
.member .avatar { margin: 2px; }
.member.editable { padding: 2px 3px 2px 4px; gap: 4px; }
.member.editable select { font-size: 12px; padding: 2px 4px; }
.members select.add-team { font-size: 13px; padding: 4px 6px; }
.role { font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; padding: 1px 5px; border-radius: 4px; background: var(--surface-2); color: var(--text-secondary); }
.role-owner, .role-core { color: var(--text-primary); border: 1px solid var(--text-muted); }
.role-rotating { border: 1px dashed var(--border); }
.roster-row td { background: var(--surface-2); }
.pill.warn { color: var(--danger); border: 1px solid currentColor; background: transparent; }
.roster-member .driver-cell { flex-wrap: wrap; }
.roster-editor p { margin: 0 0 10px; font-size: 13px; }
.roster-member, .roster-add { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 8px; }
.roster-member .driver-cell { min-width: 160px; }
.roster-add input { max-width: 220px; }
select { font: inherit; font-size: 14px; padding: 6px 8px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary); }

/* Autosave status */
.save-status { font-size: 13px; color: var(--text-muted); }
.save-status.saved { color: var(--good); }
.save-status.saved::before { content: '✓ '; }
.save-status.error { color: var(--danger); }

/* Editor */
.driver-color { display: inline-flex; align-items: center; gap: 4px; }
.driver-color input[type="color"] { width: 40px; height: 34px; padding: 2px; }
.driver-color.unset input[type="color"] { opacity: 0.45; border-style: dashed; }
.driver-color input[type="color"]:disabled { opacity: 0.2; cursor: default; }
.clear-color { padding: 2px 6px; }
.add-driver { display: flex; gap: 8px; margin-top: 12px; max-width: 360px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
label.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-secondary); }
input[type="text"], input[type="number"], input[type="date"], input[type="password"] {
  font: inherit; font-size: 14px; padding: 7px 9px; border-radius: 7px;
  border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary); width: 100%;
}
input[type="color"] { width: 100%; height: 36px; padding: 2px 4px; border: 1px solid var(--border); border-radius: 7px; background: var(--surface-1); cursor: pointer; }
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.dropzone {
  display: block; border: 1.5px dashed var(--border); border-radius: var(--radius); padding: 22px; text-align: center;
  color: var(--text-secondary); cursor: pointer;
}
.dropzone.over { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.kart-card h3 { font-size: 18px; }
.kart-head { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.stint-row { display: grid; grid-template-columns: minmax(0, 1fr) auto 120px 90px auto; gap: 8px; align-items: center; margin-bottom: 6px; }
.stint-row .range { font-size: 13px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.suggest { font-size: 13px; color: var(--text-secondary); margin: 8px 0; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.notice { padding: 10px 12px; border-radius: 8px; background: var(--surface-2); color: var(--text-secondary); font-size: 13px; margin: 8px 0; }
.notice.error { color: var(--danger); }
.sticky-save { position: sticky; bottom: 0; padding: 12px 0; background: linear-gradient(transparent, var(--bg) 30%); display: flex; gap: 8px; justify-content: flex-end; align-items: center; }

@media (max-width: 640px) {
  h1 { font-size: 22px; }
  .tile .value { font-size: 24px; }
  .stint-row { grid-template-columns: minmax(0, 1fr) auto 80px auto; }
  .stint-row .range { display: none; }
  .hide-sm { display: none; }
  /* Tables that turn each row into a wrapped card on phones. */
  .stack-sm thead { display: none; }
  .stack-sm tr { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 10px 0; border-bottom: 1px solid var(--grid); }
  .stack-sm td { border: 0; padding: 0; }
  .stack-sm td:first-child { flex: 1; min-width: 0; }
  .stack-sm .roster-cell, .stack-sm .team-cell, .stack-sm .actions-cell, .stack-sm .roster-row td { flex-basis: 100%; }
  .stack-sm .actions-cell .row-actions { justify-content: flex-start; }
  .stack-sm tr.roster-row { padding: 0; border: 0; }
  .stack-sm .roster-row td { padding: 12px; border-radius: 8px; }
  .roster-add input { max-width: none; flex: 1 1 100%; }
  .roster-member { flex-wrap: nowrap; }
  .roster-member .driver-cell { min-width: 0; flex: 1; }
  .show-sm { display: block; }
  .driver-cell { align-items: flex-start; }
  .driver-cell .swatch { margin-top: 5px; }
  .tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .tile { padding: 10px; }
  .tile .label { font-size: 10px; letter-spacing: 0.04em; }
  .tile .value { font-size: 19px; }
  .tile .sub { font-size: 11px; line-height: 1.3; }
  td, th { padding-left: 6px; padding-right: 6px; }
  .rm-cols, .rm-row { grid-template-columns: 24px var(--dw-sm, 62px) minmax(0, 1fr) 64px 58px; column-gap: 5px; padding-left: 8px; padding-right: 8px; }
  .rm-driver { font-size: 12px; gap: 4px; }
  .rm-driver .avatar { width: 18px !important; height: 18px !important; font-size: 7px !important; border-width: 1.5px; }
  .rm-driver .dn-full, .rm-time .t-full { display: none; }
  .rm-driver .dn-short, .rm-time .t-short { display: inline; }
  .driver-cell { white-space: normal; }
  .driver-cell b, .driver-cell > span:not(.avatar):not(.swatch) { overflow-wrap: anywhere; }
  .rm-note { display: none; }
  .kart-tab-label { min-width: 0; width: 100%; }
  .rm-lap, .rm-pos { font-size: 16px; }
  .rm-time { font-size: 16px; }
  .rm-delta { font-size: 12px; }
}
