:root {
  --bg: #0b0d11;
  --surface: #14171d;
  --surface-2: #1b1f27;
  --surface-3: #232833;
  --border: #2a303b;
  --text: #eef1f5;
  --muted: #8b94a3;
  --accent: #e10600;
  --accent-2: #ff3b30;
  --good: #3ccf7a;
  --bad: #ff5a52;
  --warn: #f5a524;
  --gold: #f5c542;
  --silver: #c9ced6;
  --bronze: #d08a4f;
  --purple: #b469ff;
  --radius: 10px;
  --font: 'Titillium Web', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --mono: ui-monospace, 'SF Mono', Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  background-image: radial-gradient(1200px 400px at 10% -10%, rgba(225, 6, 0, 0.12), transparent 60%);
  background-attachment: fixed;
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
}

a { color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.15; }
h1 { font-size: 30px; font-weight: 900; letter-spacing: -0.01em; }
h2 {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 14px;
}
code { font-family: var(--mono); font-size: 0.9em; background: var(--surface-3); padding: 1px 5px; border-radius: 4px; }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: var(--mono); font-size: 13px; }
.n { text-align: right; font-variant-numeric: tabular-nums; }
.c { text-align: center; }

/* ---------- Top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 10px max(16px, calc((100vw - 1320px) / 2));
  background: rgba(11, 13, 17, 0.88);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; text-decoration: none; min-width: 0; }
.brand-mark {
  width: 34px;
  height: 34px;
  flex: none;
  border-radius: 8px;
  background:
    conic-gradient(#fff 25%, #111 0 50%, #fff 0 75%, #111 0) 0 0 / 10px 10px;
  box-shadow: inset 0 0 0 3px var(--accent);
}
.brand-name { display: block; font-weight: 900; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-sub { display: block; font-size: 12px; color: var(--muted); }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a {
  padding: 8px 12px;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 600;
  color: var(--muted);
}
.nav a:hover { color: var(--text); background: var(--surface-2); }
.nav a.active { color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); border-radius: 0; }

/* ---------- Layout ---------- */

.page { max-width: 1320px; margin: 0 auto; padding: 28px 16px 120px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { margin: 4px 0 0; }
.back { display: inline-block; color: var(--muted); text-decoration: none; font-size: 13px; margin-bottom: 6px; }
.back:hover { color: var(--text); }
.round-lg { color: var(--accent-2); }
.row-gap { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  min-width: 0;
}
.card.narrow { max-width: 420px; margin: 40px auto; }
.card.narrow h1 { margin-bottom: 6px; }

.grid-2 { display: grid; grid-template-columns: 3fr 2fr; gap: 20px; align-items: start; }
.grid-2 > .card { margin-bottom: 0; }
.grid-2 + .card { margin-top: 20px; }

.empty { text-align: center; padding: 60px 16px; }
.empty h3 { font-size: 22px; margin-bottom: 8px; }
.empty p { color: var(--muted); }
.empty a:not(.btn) { color: var(--text); }

/* ---------- Tables ---------- */

.table-wrap { overflow-x: auto; margin: 0 -4px; padding: 0 4px; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: rgba(255, 255, 255, 0.015); }
tr.dim td { opacity: 0.6; }
td.pos { font-weight: 900; font-size: 16px; width: 1%; white-space: nowrap; text-align: center; }
td.pts { font-weight: 900; font-size: 16px; }

.who { display: flex; align-items: center; gap: 10px; }
.stripe {
  display: inline-block;
  width: 4px;
  height: 30px;
  border-radius: 2px;
  background: var(--team);
  flex: none;
  vertical-align: middle;
}
.chip .stripe, .preview-table .stripe, .who-sm .stripe { height: 14px; width: 3px; margin-right: 6px; }
.dname { font-weight: 700; }
.car-no { font-family: var(--mono); font-size: 12px; color: var(--muted); margin-right: 6px; }
.sub { font-size: 12px; color: var(--muted); }
.note { font-style: italic; }

.standings tbody tr:nth-child(1) td.pos { color: var(--gold); }
.standings tbody tr:nth-child(2) td.pos { color: var(--silver); }
.standings tbody tr:nth-child(3) td.pos { color: var(--bronze); }

.badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 1px 6px;
  margin-left: 6px;
  border-radius: 4px;
  background: var(--surface-3);
  color: var(--muted);
  vertical-align: 2px;
}
.chip {
  display: inline-flex;
  align-items: center;
  font-size: 12px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-3);
  white-space: nowrap;
}
.chip.warn { background: rgba(245, 165, 36, 0.15); color: var(--warn); }
.chip.bad { background: rgba(255, 90, 82, 0.15); color: var(--bad); }
.chips-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }

.flag { display: inline-block; font-size: 10px; font-weight: 900; padding: 1px 5px; border-radius: 4px; margin-left: 4px; vertical-align: 2px; }
.flag.fl { background: var(--purple); color: #fff; }
.flag.pole { background: var(--text); color: var(--bg); }

.delta { font-size: 12px; font-weight: 700; white-space: nowrap; }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }

.alert {
  background: rgba(245, 165, 36, 0.1);
  border: 1px solid rgba(245, 165, 36, 0.35);
  color: var(--warn);
  padding: 8px 12px;
  border-radius: 8px;
  margin-bottom: 10px;
  font-size: 13px;
}

.alert.info {
  background: var(--surface-2);
  border-color: var(--border);
  color: var(--text, inherit);
  line-height: 1.5;
}

.file-btn { cursor: pointer; }

.warn-head { margin-top: 22px; }
.warn-table td { vertical-align: top; }
.warn-list { display: flex; flex-wrap: wrap; gap: 6px; }
.warn-chip {
  display: inline-flex;
  flex-direction: row;
  align-items: center;
  font-weight: 500;
  color: var(--text);
  gap: 5px;
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 12px;
  white-space: nowrap;
  cursor: pointer;
}
.warn-chip input { width: auto; height: auto; margin: 0; padding: 0; flex: none; }
.warn-chip.struck { opacity: 0.55; text-decoration: line-through; }
.warn-table td:nth-child(3) { white-space: nowrap; }
.chip.good { background: rgba(46, 204, 113, 0.15); color: var(--good); }

/* ---------- Season grid ---------- */

.grid { font-size: 13px; }
.grid th, .grid td { padding: 5px 4px; }
.grid th.rd { text-align: center; padding: 6px 2px; }
.grid th.rd a { text-decoration: none; display: block; line-height: 1.1; }
.grid th.rd span { display: block; font-size: 10px; opacity: 0.7; }
.grid th.rd a:hover { color: var(--text); }
.who-sm { white-space: nowrap; font-weight: 700; font-size: 13px; text-transform: none; letter-spacing: 0; color: var(--text); padding-right: 12px !important; }
.cell {
  position: relative;
  text-align: center;
  min-width: 38px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  border-left: 2px solid var(--bg);
}
.cell sup { font-size: 9px; margin-left: 1px; opacity: 0.85; }
.cell .dot, .legend .dot {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--team);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
}
.c-p1 { background: var(--gold); color: #1a1400; }
.c-p2 { background: var(--silver); color: #111; }
.c-p3 { background: var(--bronze); color: #1a0d00; }
.c-pts { background: rgba(60, 207, 122, 0.22); color: #bff3d4; }
.c-nopts { background: rgba(255, 255, 255, 0.05); }
.c-ret { background: rgba(180, 105, 255, 0.2); color: #e2c8ff; }
.c-dsq { background: #000; color: var(--bad); }
.c-dns { background: transparent; color: var(--muted); }
.legend { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 14px; font-size: 12px; }
.legend span { padding: 2px 8px; border-radius: 4px; position: relative; }
.legend .dot { position: static; display: inline-block; vertical-align: middle; }

/* ---------- Results / calendar ---------- */

.event-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 12px; }
.event-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  transition: border-color 0.15s, transform 0.15s;
}
.event-card:hover { border-color: #3d4554; transform: translateY(-1px); }
.event-card.done { border-left: 3px solid var(--accent); }
.round {
  font-weight: 900;
  font-size: 13px;
  color: var(--muted);
  background: var(--surface-2);
  border-radius: 6px;
  padding: 6px 8px;
  min-width: 42px;
  text-align: center;
}
.event-main { flex: 1; min-width: 0; }
.event-name { display: block; font-weight: 700; font-size: 17px; }
.event-status { text-align: right; font-size: 14px; font-weight: 600; }
.event-status .who { gap: 8px; text-align: left; }

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  box-shadow: inset 0 -1px 0 var(--border);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
}
.tabs a {
  padding: 10px 14px;
  text-decoration: none;
  color: var(--muted);
  font-weight: 700;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.tabs a:hover { color: var(--text); }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }

/* ---------- Penalties ---------- */

.meter-cell { width: 40%; }
.meter { height: 8px; background: var(--surface-3); border-radius: 4px; overflow: hidden; }
.meter span { display: block; height: 100%; background: var(--muted); border-radius: 4px; }
.meter.warning span { background: var(--warn); }
.meter.ban span { background: var(--bad); }

/* ---------- Forms ---------- */

input, select, textarea {
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 7px 9px;
  min-width: 0;
  width: 100%;
}
input:focus, select:focus { outline: none; border-color: #5b6475; box-shadow: 0 0 0 3px rgba(225, 6, 0, 0.18); }
input[type='checkbox'] { width: 17px; height: 17px; accent-color: var(--accent); padding: 0; }
input[type='color'] { width: 44px; height: 34px; padding: 2px; cursor: pointer; }
input[type='file'] { padding: 10px; }
select.invalid { border-color: var(--bad); color: var(--bad); }
select.changed { border-color: var(--warn); }
.w-xs { width: 70px; }
.w-sm { width: 130px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); padding-bottom: 8px; }
label.grow { flex: 1; min-width: 180px; }
.stack { display: flex; flex-direction: column; gap: 14px; margin-top: 18px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.settings-form { display: flex; flex-direction: column; gap: 14px; }
.settings-form h2 { margin: 10px 0 0; }
.settings-form p { margin: 0; }

table.edit td { padding: 6px 6px; }
table.edit th { padding: 8px 6px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-weight: 700;
  font-size: 14px;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  color: var(--text);
  background: var(--surface-3);
}
.btn:hover { filter: brightness(1.15); }
.btn:disabled { opacity: 0.35; cursor: default; filter: none; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 5px 11px; font-size: 13px; }
.btn.icon { padding: 4px 9px; font-size: 13px; background: transparent; border-color: var(--border); }
.btn.danger { color: var(--bad); }
.btn.icon.danger:hover { background: rgba(255, 90, 82, 0.12); }

/* ---------- Results editor ---------- */

.editor { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 20px; align-items: start; }
.editor > .card { margin-bottom: 0; }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 6px; }
.toolbar select { width: auto; min-width: 260px; font-weight: 600; }
.page:has(.editor) { max-width: 1600px; }

/* Result rows: a grid per row, sized to the card instead of a table that scrolls sideways.
   Narrow cards (default): penalties, FL/Pole and the note wrap onto a second line.
   Wide cards: everything on one line. Phones: three lines. */
.erows-wrap { container: erows / inline-size; overflow-x: auto; }
.erow {
  display: grid;
  gap: 6px 8px;
  align-items: center;
  padding: 7px 4px;
  border-bottom: 1px solid var(--border);
  grid-template-columns: 22px 28px minmax(0, 1.3fr) minmax(0, 1fr) 92px 116px 34px;
  grid-template-areas:
    'grip pos drv team status gap del'
    'grip .   x   x    x      x   x';
}
.erow:last-child { border-bottom: 0; }
.erow > :nth-child(1) { grid-area: grip; }
.erow > :nth-child(2) { grid-area: pos; }
.erow > :nth-child(3) { grid-area: drv; }
.erow > :nth-child(4) { grid-area: team; }
.erow > :nth-child(5) { grid-area: status; }
.erow > :nth-child(6) { grid-area: gap; }
.erow > :nth-child(7) { grid-area: x; }
.erow > :nth-child(8) { grid-area: del; }
.erow-head {
  padding-top: 0;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  white-space: nowrap;
  grid-template-areas: 'grip pos drv team status gap del';
}
.erow-head .extras { display: none; }
.erow .pos { font-weight: 900; font-size: 16px; text-align: center; }
.erow-head .pos { font-size: 11px; font-weight: 700; }
.erow .drv { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.erow-empty { padding: 14px 4px; margin: 0; }

.extras { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; min-width: 0; }
.xf { flex-direction: row; align-items: center; gap: 6px; font-size: 12px; white-space: nowrap; }
.xf input[type='number'] { width: 58px; }
.xl { font-weight: 600; }
.xnote { flex: 1 1 160px; }
.erow input[type='number'] { -moz-appearance: textfield; }
.erow input[type='number']::-webkit-inner-spin-button { display: none; }

.grip {
  align-self: stretch;
  border: 0;
  border-radius: 6px;
  padding: 0;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: grab;
  touch-action: none;
}
.grip:hover, .grip:focus-visible { color: var(--text); background: var(--surface-2); }
.grip:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.erow .drv[data-drag] { cursor: grab; user-select: none; }
.erow.dragging {
  position: relative;
  z-index: 2;
  background: var(--surface-3);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5), inset 0 0 0 1px var(--accent);
}
body.is-dragging, body.is-dragging * { cursor: grabbing !important; user-select: none; }

@container erows (min-width: 920px) {
  .erow {
    grid-template-columns: 20px 26px minmax(130px, 1.3fr) minmax(120px, 1fr) 88px 104px 52px 52px 52px 30px 34px minmax(100px, 1.2fr) 32px;
    grid-template-areas: none;
  }
  .erows.no-pole .erow { grid-template-columns: 20px 26px minmax(130px, 1.3fr) minmax(120px, 1fr) 88px 104px 52px 52px 52px 30px minmax(100px, 1.2fr) 32px; }
  .erow > :nth-child(n) { grid-area: auto; }
  .extras, .erow-head .extras { display: contents; }
  .xl { display: none; }
  .xf { justify-content: center; }
  .xf input[type='number'] { width: 100%; }
}

@container erows (max-width: 560px) {
  .erow {
    grid-template-columns: 22px 24px minmax(0, 1fr) minmax(0, 1fr) 34px;
    grid-template-areas:
      'grip pos drv    drv  del'
      'grip .   team   team team'
      'grip .   status gap  gap'
      'grip .   x      x    x';
  }
  .erow-head { display: none; }
}

dialog.modal {
  width: min(680px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  overflow: auto;
  color: var(--text);
  padding: 22px;
  margin: auto;
}
dialog.modal::backdrop { background: rgba(0, 0, 0, 0.65); }
dialog.modal p { margin: 0 0 14px; }
.unmatched td { vertical-align: top; }
.unmatched td:last-child { width: 55%; }
.unmatched .sub { margin-top: 3px; }
.modal-actions { justify-content: flex-end; margin-top: 16px; }
input.aliases { min-width: 170px; }
.preview { position: sticky; top: 76px; }
.preview-table td { padding: 5px 6px; font-size: 13px; }
.preview-table td.pos, .preview-table td.pts { font-size: 14px; }

.savebar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 15;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  padding: 12px max(16px, calc((100vw - 1320px) / 2));
  background: rgba(20, 23, 29, 0.95);
  backdrop-filter: blur(8px);
  border-top: 1px solid var(--border);
}
.savebar #dirty-flag { margin-right: auto; color: var(--warn); font-weight: 600; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 80px;
  transform: translate(-50%, 20px);
  opacity: 0;
  pointer-events: none;
  z-index: 50;
  padding: 10px 18px;
  border-radius: 8px;
  font-weight: 700;
  background: var(--surface-3);
  border: 1px solid var(--border);
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.ok { border-color: rgba(60, 207, 122, 0.5); }
.toast.error { border-color: var(--bad); color: var(--bad); }

/* ---------- Responsive ---------- */

@media (max-width: 1080px) {
  .grid-2, .editor { grid-template-columns: 1fr; }
  .grid-2 { gap: 20px; }
  .preview { position: static; }
}

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 8px 12px; }
  .nav { order: 3; flex-basis: 100%; overflow-x: auto; }
  .auth { margin-left: auto; }
  h1 { font-size: 24px; }
  .card { padding: 14px; }
  .hide-sm { display: none; }
  .event-list { grid-template-columns: 1fr; }
  .meter-cell { width: 30%; }
  .savebar { flex-wrap: wrap; }
}
