:root {
  --bg: #0b0e16;
  --bg-2: #131829;
  --panel: rgba(20, 26, 44, 0.72);
  --panel-solid: #161c30;
  --text: #eef2ff;
  --muted: #93a0c4;
  --accent: #ff3b6b;
  --accent-2: #36d1ff;
  --good: #36e29b;
  --line: rgba(255, 255, 255, 0.08);
  --radius: 16px;
  font-synthesis-weight: none;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.screen { position: fixed; inset: 0; }
.hidden { display: none !important; }

/* ============ SETUP ============ */
.setup {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  overflow-y: auto;
  background:
    radial-gradient(900px 500px at 80% -10%, rgba(54, 209, 255, 0.16), transparent 60%),
    radial-gradient(800px 600px at 0% 110%, rgba(255, 59, 107, 0.16), transparent 55%),
    var(--bg);
}

.setup-card {
  width: 100%;
  max-width: 540px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 22px 20px 24px;
  backdrop-filter: blur(12px);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.45);
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0 0 4px;
  font-size: clamp(28px, 7vw, 40px);
  font-weight: 800;
  letter-spacing: -0.02em;
}
.marble-dot {
  width: 30px; height: 30px; border-radius: 50%;
  background: radial-gradient(circle at 32% 30%, #fff 0 14%, var(--accent) 55%, #b81d44 100%);
  box-shadow: 0 4px 14px rgba(255, 59, 107, 0.5);
}
.tagline { margin: 0 0 18px; color: var(--muted); font-size: 15px; }

.field-label, .control-label {
  display: block;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 700;
}

textarea#names {
  width: 100%;
  min-height: 150px;
  resize: vertical;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 16px;
  line-height: 1.5;
  padding: 12px 14px;
  font-family: inherit;
}
textarea#names:focus { outline: 2px solid var(--accent-2); border-color: transparent; }

.hint { font-size: 13px; color: var(--muted); margin: 8px 2px 16px; }
.hint code, .mode-note code {
  background: rgba(255, 255, 255, 0.1);
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 12px;
}
#count-hint { color: var(--accent-2); font-weight: 700; }

.controls { display: flex; flex-direction: column; gap: 14px; margin-bottom: 18px; }
.control { display: flex; flex-direction: column; gap: 0; }

.seg {
  display: flex;
  gap: 6px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 5px;
}
.seg-btn {
  flex: 1;
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 700;
  font-size: 14px;
  padding: 9px 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.seg-btn:hover { color: var(--text); }
.seg-btn.active { background: linear-gradient(180deg, #2b3556, #222a45); color: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.3); }

.start-btn {
  width: 100%;
  appearance: none;
  border: 0;
  border-radius: 14px;
  padding: 15px;
  font: inherit;
  font-weight: 800;
  font-size: 18px;
  color: #fff;
  cursor: pointer;
  background: linear-gradient(135deg, var(--accent) 0%, #ff7a4d 100%);
  box-shadow: 0 12px 30px rgba(255, 59, 107, 0.35);
  transition: transform 0.08s, filter 0.15s;
}
.start-btn:hover { filter: brightness(1.07); }
.start-btn:active { transform: translateY(1px); }

.mode-note { color: var(--muted); font-size: 13px; margin: 12px 2px 0; min-height: 1.4em; }
.setup-footer { margin-top: 16px; color: var(--muted); font-size: 12px; opacity: 0.7; text-align: center; }

/* ============ STAGE ============ */
.stage { background: #05070d; }
#canvas { display: block; width: 100%; height: 100%; touch-action: none; }

.hud {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.hud-lap {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px 12px;
  font-size: 15px;
  font-weight: 700;
  color: var(--muted);
  backdrop-filter: blur(8px);
  width: fit-content;
}
.hud-lap b { color: #fff; font-size: 20px; }
.hud-leader {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 7px 12px;
  font-weight: 800;
  font-size: 16px;
  backdrop-filter: blur(8px);
  width: fit-content;
  max-width: 60vw;
}
.hud-leader .dot, .lb-row .dot, .leader-dot {
  width: 14px; height: 14px; border-radius: 50%; flex: none;
  box-shadow: inset 0 0 0 2px rgba(255,255,255,.25);
}
.hud-leader span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.phase-banner {
  margin-top: 2px;
  background: linear-gradient(135deg, var(--accent), #ff7a4d);
  color: #fff;
  font-weight: 900;
  letter-spacing: 0.06em;
  font-size: 14px;
  padding: 6px 12px;
  border-radius: 10px;
  width: fit-content;
  animation: pulse 0.9s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { transform: scale(1); opacity: 1; } 50% { transform: scale(1.06); opacity: .85; } }

/* big race-event announcements (lead changes, fire, items, lightning) */
.event-banner {
  position: absolute;
  top: 11%;
  left: 50%;
  transform: translateX(-50%);
  max-width: 92vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 10px 28px;
  border-radius: 16px;
  font-size: clamp(20px, 3.6vw, 42px);
  font-weight: 900;
  letter-spacing: 0.04em;
  color: #fff;
  background: rgba(10, 14, 24, 0.78);
  border: 2px solid var(--bc, #fff);
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5), 0 0 26px var(--bc, #fff);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  z-index: 4;
  animation: bannerpop 1.9s cubic-bezier(0.22, 1, 0.36, 1) both;
}
@keyframes bannerpop {
  0% { transform: translateX(-50%) scale(0.4); opacity: 0; }
  10% { transform: translateX(-50%) scale(1.08); opacity: 1; }
  16% { transform: translateX(-50%) scale(1); }
  82% { transform: translateX(-50%) scale(1); opacity: 1; }
  100% { transform: translateX(-50%) scale(0.96); opacity: 0; }
}

.leaderboard {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: 12px;
  width: min(240px, 42vw);
  max-height: 78vh;
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 8px;
  backdrop-filter: blur(10px);
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.lb-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 600;
  transition: transform 0.35s cubic-bezier(.22,1,.36,1), background 0.3s;
  will-change: transform;
}
.lb-row .pos { width: 22px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; font-weight: 800; }
.lb-row .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lb-row .meta { color: var(--muted); font-size: 12px; font-weight: 700; }
.lb-row.leader { background: linear-gradient(90deg, rgba(255,59,107,.22), transparent); }
.lb-row.leader .pos { color: var(--accent); }
.lb-row.done .meta { color: var(--good); }

/* long participant lists: tighter rows so everyone stays visible */
.leaderboard.compact { gap: 1px; }
.leaderboard.compact .lb-row { padding: 3px 6px; font-size: 12px; }
.leaderboard.compact .lb-row .pos { width: 18px; }
.leaderboard.compact .lb-row .dot { width: 10px; height: 10px; }

.quit-btn {
  position: absolute;
  bottom: max(14px, env(safe-area-inset-bottom));
  left: 14px;
  width: 40px; height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  backdrop-filter: blur(8px);
}
.quit-btn:hover { color: #fff; }

/* countdown */
.countdown {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
}
.countdown span {
  font-size: clamp(120px, 34vw, 320px);
  font-weight: 900;
  color: #fff;
  text-shadow: 0 0 60px rgba(54, 209, 255, 0.6);
  animation: countpop 1s ease-out forwards;
}
@keyframes countpop {
  0% { transform: scale(2.4); opacity: 0; }
  20% { opacity: 1; }
  100% { transform: scale(0.6); opacity: 0; }
}

/* winner */
.winner {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 35%, rgba(20,26,44,.55), rgba(5,7,13,.92));
  backdrop-filter: blur(3px);
  z-index: 5;
}
.confetti { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.winner-card {
  position: relative;
  text-align: center;
  padding: 24px;
  animation: winrise 0.6s cubic-bezier(.22,1,.36,1) both;
}
@keyframes winrise { from { transform: translateY(24px) scale(.9); opacity: 0; } to { transform: none; opacity: 1; } }
.winner-kicker { font-size: 16px; font-weight: 800; letter-spacing: 0.18em; text-transform: uppercase; color: var(--accent-2); }
.winner-marble {
  width: 120px; height: 120px; border-radius: 50%;
  margin: 18px auto;
  display: flex; align-items: center; justify-content: center;
  font-size: 46px; font-weight: 900; color: #fff;
  text-shadow: 0 2px 8px rgba(0,0,0,.4);
  box-shadow: 0 0 0 6px rgba(255,255,255,.1), 0 20px 60px rgba(0,0,0,.5);
  animation: trophyspin 0.9s ease-out both;
}
@keyframes trophyspin { from { transform: scale(0) rotate(-90deg); } to { transform: none; } }
.winner-name {
  font-size: clamp(40px, 11vw, 88px);
  font-weight: 900;
  letter-spacing: -0.02em;
  line-height: 1.05;
  margin-bottom: 26px;
  word-break: break-word;
}
.winner-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.winner-actions .start-btn { width: auto; padding: 13px 26px; }
.ghost-btn {
  appearance: none;
  background: rgba(255,255,255,.08);
  border: 1px solid var(--line);
  color: var(--text);
  font: inherit; font-weight: 700; font-size: 16px;
  padding: 13px 22px; border-radius: 14px; cursor: pointer;
}
.ghost-btn:hover { background: rgba(255,255,255,.14); }

/* desktop / projector */
@media (min-width: 760px) {
  .leaderboard { width: 280px; }
  .hud-lap { font-size: 17px; }
}
@media (prefers-reduced-motion: reduce) {
  .phase-banner { animation: none; }
}
