:root {
  --bg: #14161c;
  --card: #1e2129;
  --line: #2e333e;
  --text: #f4f5f7;
  --muted: #9098a8;
  --accent: #ff7a3d;
  --good: #4cd480;
  --bad: #ff5a5a;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body { overflow-x: hidden; }
button { font: inherit; color: var(--text); background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; cursor: pointer; }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #1a0d05; font-weight: 700; font-size: 18px; width: 100%; padding: 15px; }
button.primary.big { font-size: 22px; padding: 18px; }
button.primary.done { background: var(--good); border-color: var(--good); }
button.ghost { background: transparent; border-style: dashed; width: 100%; }
button.link { background: none; border: none; color: var(--muted); text-decoration: underline; padding: 4px; font-size: 14px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
a { color: inherit; }
h1 { font-size: 34px; margin: 8px 0 4px; letter-spacing: -0.5px; }
h2 { font-size: 24px; margin: 4px 0; }
.label { font-size: 14px; color: var(--muted); margin: 18px 0 8px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

/* Screens: only the one named on <body> shows */
.screen { display: none; min-height: 100dvh; }
body[data-screen="join"] #join,
body[data-screen="lobby"] #lobby,
body[data-screen="race"] #race,
body[data-screen="results"] #results { display: block; }
.wrap { max-width: 480px; margin: 0 auto; padding: 18px 16px 32px; }

.field { display: block; margin-top: 16px; font-size: 14px; color: var(--muted); }
.field input { display: block; width: 100%; margin-top: 6px; font: inherit; font-size: 18px; color: var(--text); background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.field input:focus { outline: 2px solid var(--accent); border-color: transparent; }

.chars { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.chars button { padding: 6px 4px 8px; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 13px; }
.chars canvas { width: 100%; height: 92px; }
.chars button.on { border-color: var(--accent); background: #2a2018; box-shadow: 0 0 0 2px var(--accent) inset; }

.seg { display: flex; gap: 8px; }
.seg button { flex: 1; display: flex; flex-direction: column; align-items: center; font-weight: 600; }
.seg button small { font-weight: 400; color: var(--muted); font-size: 12px; }
.seg button.on { background: var(--accent); border-color: var(--accent); color: #1a0d05; }
.seg button.on small { color: #4a2a14; }
.seg.small { margin-top: 8px; }
.seg.small button { padding: 8px; font-size: 14px; }
#join-btn { margin-top: 22px; }

.pill { font-size: 13px; padding: 4px 10px; border-radius: 999px; background: var(--card); color: var(--muted); }
.pill.ok { color: var(--good); }
.pill.bad { color: var(--bad); }

.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 10px; }
.cam-card { display: flex; gap: 12px; align-items: center; margin-top: 10px; user-select: none; touch-action: manipulation; }
.cam-card canvas { width: 120px; height: 160px; border-radius: 10px; background: #000; flex: none; }
.cam-card.tap canvas { display: none; }
.cam-info { flex: 1; min-width: 0; }
.status { font-weight: 700; }
.status.ok { color: var(--good); }
.status.bad { color: var(--bad); }
.big-num span { font-size: 44px; font-weight: 800; color: var(--accent); font-variant-numeric: tabular-nums; }
.big-num small { margin-left: 6px; color: var(--muted); }

.players { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.players li { display: flex; align-items: center; gap: 10px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 6px 10px; }
.players canvas { width: 40px; height: 48px; flex: none; }
.players .who { flex: 1; min-width: 0; }
.players .who b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.players .tag { font-size: 13px; font-weight: 700; }
.players .tag.ready { color: var(--good); }
.players .tag.wait { color: var(--muted); }
.players .x { padding: 4px 10px; font-size: 14px; }
#ready-btn { margin-top: 14px; }

/* Race */
#race { position: fixed; inset: 0; min-height: 0; touch-action: none; user-select: none; -webkit-user-select: none; overflow: hidden; }
#race-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#hud { position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: flex-start; gap: 12px; align-items: center; padding: max(10px, env(safe-area-inset-top)) 14px 0; pointer-events: none; color: #10233a; }
/* Your place as a medal: gold, silver, bronze, blue */
#place { font-weight: 900; letter-spacing: -2px; }
#place.medal { width: min(24vmin, 150px); height: min(24vmin, 150px); border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: min(14vmin, 88px); line-height: 1; border: min(1.6vmin, 10px) solid rgba(0,0,0,0.18); box-shadow: 0 4px 0 rgba(0,0,0,0.2); }
#place.m1 { background: #FFC928; color: #6b4a00; }
#place.m2 { background: #DDE3EA; color: #46505c; }
#place.m3 { background: #E0A26A; color: #5a3212; }
#place.m4 { background: #8FC1F0; color: #123a63; }
#place sup { font-size: 0.38em; letter-spacing: 0; margin-top: -0.6em; }
.hud-right { text-align: left; }
#togo { font-size: min(7vmin, 48px); font-weight: 900; line-height: 1; }
#myspeed { font-size: min(6vmin, 40px); font-weight: 700; opacity: 0.8; margin-top: 4px; }
#countdown { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: min(45vmin, 320px); font-weight: 900; color: #fff; text-shadow: 0 6px 0 rgba(0,0,0,0.25); pointer-events: none; }
#countdown.go { color: #FFD23F; }
#banner { position: absolute; left: 50%; top: 32%; transform: translateX(-50%); background: rgba(17,17,17,0.85); color: #fff; padding: 12px 22px; border-radius: 16px; font-size: min(6.5vmin, 40px); font-weight: 800; text-align: center; display: none; pointer-events: none; width: max-content; max-width: 92vw; }
#banner small { display: block; font-size: 0.55em; font-weight: 600; opacity: 0.8; }
/* Camera picture top-right, clear of the track and the finish line */
#race-cam-wrap { position: absolute; right: 10px; top: max(10px, env(safe-area-inset-top)); width: min(20vmin, 120px); }
#race-cam-wrap #race-cam { max-height: 28vh; }
#race-cam { width: 100%; aspect-ratio: 3 / 4; display: block; border-radius: 10px; border: 3px solid var(--good); background: #000; }
#race-cam-wrap.bad #race-cam { border-color: var(--bad); }
#race-cam-status { margin-top: 4px; font-size: 12px; font-weight: 700; text-align: center; color: #fff; text-shadow: 0 1px 2px #000; }
body.tap-mode #race-cam-wrap { display: none; }
#tap-hint { display: none; position: absolute; bottom: calc(max(10px, env(safe-area-inset-bottom)) + 44px); left: 0; right: 0; text-align: center; color: #fff; font-weight: 700; font-size: 18px; text-shadow: 0 1px 3px #000; pointer-events: none; }
body.tap-mode #tap-hint { display: block; }
.small-btn { touch-action: manipulation; position: absolute; left: 10px; bottom: max(10px, env(safe-area-inset-bottom)); font-size: 13px; padding: 6px 10px; background: rgba(0,0,0,0.45); border-color: transparent; color: #fff; }
.small-btn.armed { background: var(--bad); }

/* Results */
.podium { list-style: none; padding: 0; margin: 16px 0 22px; display: flex; flex-direction: column; gap: 8px; }
.podium li { display: flex; align-items: center; gap: 12px; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 8px 14px; }
.podium li.me { border-color: var(--accent); }
.podium .pos { font-size: 24px; font-weight: 900; width: 48px; height: 48px; border-radius: 50%; display: flex; align-items: center; justify-content: center; flex: none; background: #8FC1F0; color: #123a63; }
.podium li:nth-child(1) .pos { background: #FFC928; color: #6b4a00; width: 56px; height: 56px; font-size: 30px; }
.podium li:nth-child(2) .pos { background: #DDE3EA; color: #46505c; }
.podium li:nth-child(3) .pos { background: #E0A26A; color: #5a3212; }
.podium canvas { width: 48px; height: 60px; flex: none; }
.podium .who { flex: 1; min-width: 0; }
.podium .who b { display: block; font-size: 18px; }
.podium .time { font-size: 20px; font-weight: 800; font-variant-numeric: tabular-nums; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #111; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; opacity: 0; transition: opacity 0.2s; pointer-events: none; max-width: 90vw; text-align: center; z-index: 10; }
#toast.show { opacity: 1; }

/* Sideways phones are short: a smaller medal so it never covers a runner */
@media (orientation: landscape) {
  #place.medal { width: min(19vmin, 110px); height: min(19vmin, 110px); font-size: min(11vmin, 64px); }
}

/* ---- Several players on one camera (version 2) ---- */
.seg button small { white-space: nowrap; }
.group-rows { display: flex; flex-direction: column; gap: 8px; }
.group-row { display: flex; align-items: center; gap: 10px; }
.group-row .num { width: 22px; text-align: center; font-weight: 800; color: var(--muted); }
.group-row .avatar { width: 54px; height: 62px; padding: 2px; flex: none; }
.group-row .avatar canvas { width: 100%; height: 100%; }
.group-row input { flex: 1; min-width: 0; font: inherit; font-size: 18px; color: var(--text); background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.group-row input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.cam-card.group { flex-direction: column; align-items: stretch; }
.cam-card.group canvas { width: 100%; height: auto; aspect-ratio: 16 / 9; }
.status.prompt { font-size: 22px; color: #FFD23F; }
.big-num i { font-style: normal; font-weight: 800; font-size: 20px; margin-right: 10px; white-space: nowrap; }
#resign { display: none; margin-top: 6px; }
#team { display: flex; flex-wrap: wrap; gap: 8px; max-width: calc(100% - 150px); }
#team .chip { display: flex; align-items: center; gap: 8px; background: rgba(255,255,255,0.85); border: 4px solid; border-radius: 999px; padding: 4px 14px 4px 4px; font-size: min(5vmin, 30px); }
.mini-medal { width: 1.6em; height: 1.6em; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; font-weight: 900; }
.mini-medal.m1 { background: #FFC928; color: #6b4a00; }
.mini-medal.m2 { background: #DDE3EA; color: #46505c; }
.mini-medal.m3 { background: #E0A26A; color: #5a3212; }
.mini-medal.m4 { background: #8FC1F0; color: #123a63; }
body.group-mode #race-cam-wrap { width: min(34vmin, 220px); }
body.group-mode #race-cam { aspect-ratio: 16 / 9; }
