:root {
  --bg: #f4f4f6;
  --surface: #ffffff;
  --surface-2: #f0f0f3;
  --text: #17171c;
  --muted: #62626e;
  --border: #dedee4;
  --accent: #d6001c;
  --accent-text: #ffffff;
  --live: #0a8f3c;
  --planned: #2a62c9;
  --gold: #b8860b;
  --silver: #7b8794;
  --bronze: #a0522d;
  --radius: 10px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f0f13;
    --surface: #18181f;
    --surface-2: #22222b;
    --text: #ececf1;
    --muted: #9a9aa8;
    --border: #2e2e39;
    --accent: #ff2b3f;
    --live: #2fcf6d;
    --planned: #6d9cff;
    --gold: #f2c14e;
    --silver: #c4ccd6;
    --bronze: #d9895b;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--accent); }
h1 { font-size: 1.6rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 1rem 0 .25rem; }
p { margin: .5rem 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.pre { white-space: pre-wrap; }
.num { font-variant-numeric: tabular-nums; text-align: right; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: .5rem 1.5rem;
  padding: .75rem 1.25rem; background: var(--surface); border-bottom: 3px solid var(--accent);
}
.brand { font-weight: 800; font-size: 1.1rem; color: var(--text); text-decoration: none; display: flex; align-items: center; gap: .5rem; letter-spacing: .02em; }
.flag {
  width: 22px; height: 16px; border-radius: 2px; display: inline-block;
  background: conic-gradient(var(--text) 25%, var(--surface) 0 50%, var(--text) 0 75%, var(--surface) 0) 0 0 / 8px 8px;
  border: 1px solid var(--text);
}
.topbar nav { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.topbar nav a { color: var(--text); text-decoration: none; font-weight: 500; }
.topbar nav a:hover { color: var(--accent); }

main { max-width: 1200px; margin: 0 auto; padding: 1.25rem; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 1.1rem 1.25rem; margin-bottom: 1rem; }
.card.narrow { max-width: 460px; margin: 2rem auto; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; flex-wrap: wrap; }
.card-head h2 { white-space: nowrap; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 1rem; align-items: start; }
.grid-2.tight { grid-template-columns: 1fr 1fr; gap: .75rem; }
.grid-4 { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .75rem; }
.sticky-col { position: sticky; top: 1rem; }
@media (max-width: 860px) {
  .grid-2, .grid-2.tight { grid-template-columns: minmax(0, 1fr); }
  .grid-4 { grid-template-columns: 1fr 1fr; }
  /* On phones, keep voice and chat near the top of a race page instead of below everything else. */
  .sticky-col { position: static; order: -1; }
  .chat .messages { height: 240px; }
  main { padding: 1rem; }
}
.row { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.row-between { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; }
.stack { display: flex; flex-direction: column; gap: .8rem; }
.inline { display: inline; }

label { display: flex; flex-direction: column; gap: .25rem; font-weight: 500; }
label.row, label.check { flex-direction: row; align-items: center; font-weight: 400; }
input, select, textarea, button, .button {
  font: inherit; color: inherit; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); padding: .5rem .7rem;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type="checkbox"], input[type="radio"] { width: auto; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem 1rem; margin: 0; display: flex; flex-direction: column; gap: .75rem; }
legend { font-weight: 600; padding: 0 .3rem; }
button, .button { cursor: pointer; text-decoration: none; display: inline-block; font-weight: 500; background: var(--surface); }
button:hover, .button:hover { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: default; }
.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.primary:hover { filter: brightness(1.08); border-color: var(--accent); }
button.link { background: none; border: none; padding: 0 .25rem; color: var(--accent); font-weight: 400; }
.danger { color: var(--accent); }
.danger-zone { margin: 1.5rem 0; text-align: right; }

.flash { padding: .7rem 1rem; border-radius: var(--radius); margin-bottom: 1rem; border: 1px solid; }
.flash.success { border-color: var(--live); color: var(--live); }
.flash.error { border-color: var(--accent); color: var(--accent); }
.note { background: var(--surface-2); padding: .6rem .8rem; border-radius: 8px; }

.badge { font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; font-weight: 700; padding: .15rem .5rem; border-radius: 99px; border: 1px solid currentColor; vertical-align: middle; }
.badge.live { color: var(--live); }
.badge.planned { color: var(--planned); }
.badge.finished, .badge.cancelled { color: var(--muted); }

.race-list { display: flex; flex-direction: column; gap: .6rem; }
.race-card { display: flex; gap: .8rem; text-decoration: none; color: inherit; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface-2); }
.race-card:hover { border-color: var(--accent); }
.race-card .thumb { width: 120px; flex-shrink: 0; }
.race-card-body { padding: .6rem .8rem .6rem 0; flex: 1; min-width: 0; }
.thumb { aspect-ratio: 4 / 3; background: var(--surface-2); display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: .8rem; overflow: hidden; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

.connect .ip-box { display: flex; align-items: center; gap: .6rem; background: var(--surface-2); border: 2px dashed var(--accent); border-radius: var(--radius); padding: .7rem 1rem; margin: .5rem 0; }
.ip-box code { font-size: 1.6rem; font-weight: 700; letter-spacing: .03em; flex: 1; word-break: break-all; }
.steps { margin: 0; padding-left: 1.2rem; }
.conn-list { list-style: none; margin: .3rem 0 0; padding: 0; }
.conn-row { display: flex; justify-content: space-between; gap: .6rem; padding: .3rem 0; border-bottom: 1px solid var(--border); }
.conn-row:last-child { border-bottom: 0; }
.conn-status { color: var(--muted); font-size: .9rem; text-align: right; }
.conn-status.connected { color: var(--live); font-weight: 600; }
.conn-status.disconnected { color: var(--accent); }
.race-pass { margin-top: .8rem; padding-top: .8rem; border-top: 1px solid var(--border); }
.race-pass h3 { margin: 0 0 .3rem; }
/* QR codes stay black on white in dark mode too, so phones can scan them. */
.race-pass .qr { width: min(240px, 100%); margin: .5rem 0; background: #fff; border-radius: var(--radius); padding: .4rem; }
.race-pass .qr svg { display: block; width: 100%; height: auto; }
.status-form { margin-top: .8rem; padding-top: .8rem; border-top: 1px solid var(--border); }
.status-form button { text-transform: capitalize; padding: .3rem .7rem; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: .6rem; margin-bottom: .75rem; }
.gallery.large { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
.gallery figure { margin: 0; }
.gallery img { width: 100%; aspect-ratio: 4 / 3; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); display: block; }
.upload-form { border-top: 1px solid var(--border); padding-top: .75rem; }
.upload-form input[type="file"] { flex: 1; min-width: 200px; }

.track-picker { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: .6rem; }
.track-option { position: relative; border: 2px solid var(--border); border-radius: var(--radius); overflow: hidden; cursor: pointer; gap: 0; font-weight: 400; }
.track-option input { position: absolute; opacity: 0; }
.track-option .name { padding: .35rem .5rem; font-size: .85rem; }
.track-option:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.track-option:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.thumb.add span { font-size: 2rem; }
.new-track { padding-top: .5rem; }

.track-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 1rem; }
.track-tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; color: inherit; text-decoration: none; display: flex; flex-direction: column; }
.track-tile:hover { border-color: var(--accent); }
.track-tile strong, .track-tile > span { padding: 0 .8rem; }
.track-tile strong { padding-top: .6rem; }
.track-tile > span:last-child { padding-bottom: .7rem; }

.chips { list-style: none; padding: 0; margin: 0; display: flex; gap: .4rem; flex-wrap: wrap; }
.chips li { background: var(--surface-2); border: 1px solid var(--border); padding: .2rem .7rem; border-radius: 99px; }
ul.plain { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: .4rem; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .5rem .6rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
tr.podium td:first-child { font-weight: 700; }
tr.p1 td:first-child { color: var(--gold); }
tr.p2 td:first-child { color: var(--silver); }
tr.p3 td:first-child { color: var(--bronze); }
tr.dim { opacity: .55; }
details { margin-top: 1rem; }
summary { cursor: pointer; font-weight: 600; margin-bottom: .75rem; }

.chat { display: flex; flex-direction: column; }
.chat .messages { list-style: none; margin: 0 0 .75rem; padding: 0; height: 380px; overflow-y: auto; display: flex; flex-direction: column; gap: .5rem; }
.chat .messages li { background: var(--surface-2); border-radius: 8px; padding: .4rem .65rem; max-width: 90%; overflow-wrap: anywhere; }
.chat .messages li.mine { align-self: flex-end; border: 1px solid var(--accent); }
.chat .meta { font-size: .78rem; color: var(--muted); }
.chat .meta strong { color: var(--text); }
.chat-form { display: flex; gap: .5rem; }
.chat-form input { flex: 1; min-width: 0; }
.presence { font-size: .8rem; text-align: right; }

.voice-peers { list-style: none; padding: 0; margin: 0 0 .75rem; display: flex; flex-direction: column; gap: .3rem; }
.voice-peers li::before { content: "●"; margin-right: .4rem; color: var(--muted); }
.voice-peers li[data-state="connected"]::before { color: var(--live); }
.voice-peers li[data-state="failed"]::before { color: var(--accent); }
[hidden] { display: none !important; }

/* Host / admin flair */
.role-tag {
  display: inline-block; white-space: nowrap; font-size: .64rem; font-weight: 800; letter-spacing: .07em; text-transform: uppercase;
  padding: .05rem .4rem; border-radius: 4px; vertical-align: middle; line-height: 1.5; color: #fff;
}
.role-tag.host { background: #1f5fd1; }
.role-tag.admin { background: #c8102e; }
.chat .messages li.from-admin { border-left: 3px solid var(--accent); }
.chat .messages li.from-host:not(.from-admin) { border-left: 3px solid var(--planned); }
.presence .who { white-space: nowrap; }
details.reset { margin-top: .3rem; }
details.reset summary { margin-bottom: .4rem; font-weight: 500; color: var(--accent); }
details.reset input { width: 12rem; }

/* Class and achievement badges */
.role-tag.class-1 { background: #2e7d32; }
.role-tag.class-2 { background: #6d4c41; }
.role-tag.class-3 { background: #37474f; }
.role-tag.class-4 { background: linear-gradient(135deg, #b8860b, #e0b100); color: #1a1300; }
.role-tag.record { background: #7b1fa2; }
.role-tag.mostlaps { background: #00796b; }
.role-tag.fastest { background: #9c27b0; }
.role-tag { text-transform: none; letter-spacing: .02em; }
.role-tag.host, .role-tag.admin { text-transform: uppercase; letter-spacing: .07em; }
.ok { color: var(--live); font-weight: 600; }
.progress-card { background: var(--surface-2); border-radius: var(--radius); padding: .75rem 1rem; margin: .75rem 0; }
.meter-row { display: grid; grid-template-columns: 9rem 1fr 4rem; align-items: center; gap: .6rem; margin-top: .3rem; }
.meter-row meter { width: 100%; height: 1rem; }
.ladder-step { border-left: 3px solid var(--border); padding: .2rem 0 .2rem .8rem; margin-bottom: .8rem; }
.class-form select { padding: .25rem .4rem; font-size: .85rem; }

.congrats { border-color: var(--gold); background: linear-gradient(135deg, color-mix(in srgb, var(--gold) 12%, var(--surface)), var(--surface)); }
.congrats li { padding: .2rem 0; }
.export-row { margin: -.25rem 0 1rem; }
.button.small { padding: .3rem .7rem; font-size: .85rem; }

.role-tag.open-class { background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); }
.class-picker .check { gap: .4rem; }
.class-options { padding-left: 1.5rem; gap: .9rem; }
