@font-face {
  font-family: Fredoka;
  src: url("/fonts/fredoka-600.woff2") format("woff2");
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: Fredoka;
  src: url("/fonts/fredoka-700.woff2") format("woff2");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}

:root {
  --ink: #241c33;
  --cream: #fff6e4;
  --pink: #ff5d8f;
  --sun: #ffe14a;
  --leaf: #7dce63;
  --sky: #79d4ff;
  --card: #fff6e4;
  color: var(--ink);
  font-family: Fredoka, "Trebuchet MS", Verdana, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #79d4ff;
  overflow: hidden;
}

button, input { font: inherit; color: inherit; }

[hidden] { display: none !important; }

.btn {
  border: 4px solid var(--ink);
  background: var(--sun);
  border-radius: 16px;
  box-shadow: 5px 5px 0 var(--ink);
  padding: 12px 18px;
  font-weight: 700;
  cursor: pointer;
  min-height: 52px;
}

.btn:active:not(:disabled) {
  transform: translate(3px, 3px);
  box-shadow: 2px 2px 0 var(--ink);
}

.btn:disabled {
  background: #efe6d4;
  color: #8d8496;
  cursor: not-allowed;
  box-shadow: 3px 3px 0 #8d8496;
  border-color: #8d8496;
}

.btn.leaf { background: var(--leaf); }
.btn.pink { background: var(--pink); color: #fff; }
.btn.ghost { background: transparent; box-shadow: none; }

.card {
  background: var(--card);
  border: 4px solid var(--ink);
  border-radius: 24px;
  box-shadow: 8px 8px 0 var(--ink);
}

.kicker {
  letter-spacing: 0.14em;
  font-size: 12px;
  font-weight: 700;
  margin: 0;
}

h1, h2, p { margin: 0; }

.stage-wrap { position: relative; width: 100%; height: 100dvh; }

canvas#stage, canvas#view {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.banner {
  position: absolute;
  left: 16px;
  top: 16px;
  z-index: 6;
  max-width: min(420px, calc(100% - 460px));
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 10px 12px;
  font-weight: 700;
}

/* TV */
.lobby-card, .race-panel, .results-card, .timer-card {
  position: absolute;
  z-index: 3;
}

.lobby-card {
  top: 22px;
  right: 22px;
  bottom: 22px;
  width: min(400px, calc(100vw - 32px));
  padding: 18px 18px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: auto;
}

.lobby-card h1 { font-size: clamp(40px, 4vw, 58px); line-height: 0.9; }
.sub { font-size: 16px; line-height: 1.3; }

.qr-row { display: flex; gap: 12px; align-items: center; }
.qr-row img {
  width: 132px;
  height: 132px;
  background: var(--cream);
  border: 3px solid var(--ink);
  border-radius: 12px;
}
.code-block { flex: 1; }
.code-label { font-size: 12px; letter-spacing: 0.12em; font-weight: 700; }
#room-code { font-size: 48px; line-height: 0.95; letter-spacing: 0.08em; }
#join-url { font-size: 13px; word-break: break-all; }

.roster { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; min-height: 48px; }
.roster li, .board li {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 3px solid var(--ink);
  border-radius: 12px;
  padding: 6px 8px;
  background: #fff;
  font-weight: 700;
}
.swatch { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--ink); flex: none; }
.roster .empty, .wait-note { font-weight: 600; color: #5c5468; }

.howto { display: flex; flex-wrap: wrap; gap: 6px; }
.howto span, .chip {
  border: 3px solid var(--ink);
  border-radius: 999px;
  padding: 4px 8px;
  background: #fff;
  font-size: 13px;
  font-weight: 700;
}

.linkish {
  background: none;
  border: 0;
  padding: 0;
  text-decoration: underline;
  font-weight: 700;
  cursor: pointer;
}

.timer-card {
  top: 18px;
  left: 18px;
  padding: 12px 16px;
  min-width: 180px;
}
.banner:not([hidden]) ~ .timer-card { top: 96px; }
.timer-card strong { display: block; font-size: 42px; line-height: 1; }
.track-name { font-size: 14px; }
#track-section { display: block; font-size: 12px; opacity: 0.8; margin-top: 3px; }

/* Mid-race the stage is the director's feed and the leaderboard owns the right column. */
:root { --board-w: clamp(260px, 26vw, 420px); --row: 58px; }
body[data-phase="countdown"] canvas#stage,
body[data-phase="racing"] canvas#stage { width: calc(100% - var(--board-w)); }
body[data-phase="countdown"] #countdown,
body[data-phase="racing"] #countdown,
body[data-phase="countdown"] .cut,
body[data-phase="racing"] .cut { right: var(--board-w); width: auto; }

.race-panel {
  top: 0;
  right: 0;
  bottom: 0;
  width: var(--board-w);
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--cream);
  border-left: 4px solid var(--ink);
  overflow: hidden;
}
.board-head { display: flex; align-items: center; justify-content: space-between; }
.race-panel h2 { font-size: clamp(20px, 1.8vw, 30px); }
.live {
  background: #e8344e;
  color: #fff;
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.1em;
  animation: blink 1.6s steps(2, start) infinite;
}
@keyframes blink { 50% { opacity: 0.55; } }

/* Rows are absolutely placed by index so a pass slides instead of jumping. */
.board { list-style: none; margin: 0; padding: 0; position: relative; flex: none; }
.board li {
  position: absolute;
  left: 0;
  right: 0;
  height: calc(var(--row) - 8px);
  transition: top 0.45s cubic-bezier(0.2, 0.8, 0.2, 1), background 0.3s;
  font-size: clamp(15px, 1.25vw, 20px);
}
.board li.featured { background: var(--sun); box-shadow: 4px 4px 0 var(--ink); }
.board li.lost { opacity: 0.55; }
.board .pos { width: 1.6em; text-align: center; font-size: 1.15em; }
.board .who { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board .stat { margin-left: auto; text-align: right; line-height: 1.05; font-variant-numeric: tabular-nums; }
.board .stat small { display: block; font-size: 0.68em; color: #5c5468; }
.events { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; overflow: hidden; }

/* Lower third for the battle the director is showing. */
.battle {
  position: absolute;
  z-index: 3;
  left: 18px;
  bottom: 18px;
  max-width: calc(100% - var(--board-w) - 36px);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--ink);
  color: var(--cream);
  border-radius: 14px;
  box-shadow: 5px 5px 0 rgba(36, 28, 51, 0.35);
  font-size: clamp(16px, 1.5vw, 24px);
  font-weight: 700;
}
.battle strong { color: var(--sun); text-transform: uppercase; letter-spacing: 0.04em; }
.battle-gap { color: #b8ecff; font-variant-numeric: tabular-nums; }

/* Quick flash when the director cuts to a different battle. */
.cut { position: absolute; inset: 0; z-index: 2; pointer-events: none; background: #fff; opacity: 0; }
.cut.go { animation: cut 0.35s ease-out; }
@keyframes cut { from { opacity: 0.85; } to { opacity: 0; } }
.events li { font-size: 14px; line-height: 1.25; }
.events .good { color: #1f8a32; }
.events .bad { color: #d23b62; }

#countdown {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: center;
  font-size: min(28vw, 220px);
  font-weight: 700;
  color: #fff;
  -webkit-text-stroke: 8px var(--ink);
  paint-order: stroke fill;
  pointer-events: none;
  text-shadow: 8px 8px 0 var(--ink);
}

.results-card {
  inset: 4vh 5vw;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: auto;
  z-index: 5;
}
.results-card h2 { font-size: clamp(32px, 5vw, 56px); }
.podium {
  display: grid;
  grid-template-columns: 1fr 1.15fr 1fr;
  gap: 12px;
  align-items: end;
}
.spot {
  background: #fff;
  border: 4px solid var(--ink);
  border-radius: 18px;
  text-align: center;
  padding: 8px;
}
.spot.first { min-height: 230px; background: #ffe14a; }
.spot.second { min-height: 190px; }
.spot.third { min-height: 170px; }
.spot canvas { width: 120px; height: 120px; }
.spot .place { font-size: 22px; }
.rest { display: flex; flex-wrap: wrap; gap: 8px; }
.rest span {
  border: 3px solid var(--ink);
  border-radius: 999px;
  padding: 6px 10px;
  background: #fff;
  font-weight: 700;
}

/* Phone */
.phone-screen {
  min-height: 100dvh;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
  overflow: auto;
  background:
    radial-gradient(circle at 10% 0%, #fff6c8 0, transparent 40%),
    linear-gradient(#9adfff, #fff0c2);
}

.phone-screen h1 { font-size: 42px; line-height: 0.9; }
.field { display: flex; flex-direction: column; gap: 4px; font-weight: 700; }
.field input {
  border: 4px solid var(--ink);
  border-radius: 14px;
  padding: 12px;
  background: #fff;
  font-size: 18px;
  min-height: 52px;
}
.racers {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.racers button {
  border: 4px solid var(--ink);
  border-radius: 16px;
  background: #fff;
  padding: 4px;
  min-height: 84px;
  box-shadow: 3px 3px 0 var(--ink);
}
.racers button[aria-pressed="true"] {
  background: var(--sun);
  transform: translate(2px, 2px);
  box-shadow: 1px 1px 0 var(--ink);
}
.racers canvas { width: 64px; height: 56px; display: block; margin: 0 auto; }
.racers small { display: block; font-weight: 700; }
.error { color: #b4234a; font-weight: 700; min-height: 1.2em; }
.you-are { display: flex; align-items: center; gap: 12px; }
#lobby-preview { width: 92px; height: 92px; background: #fff; border-radius: 18px; border: 4px solid var(--ink); }

#game-screen { position: relative; height: 100dvh; background: #79d4ff; }
.hud {
  position: absolute;
  z-index: 2;
  left: 12px;
  right: 12px;
  top: 10px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
}
.pill {
  background: var(--cream);
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 6px 10px;
  font-weight: 700;
  box-shadow: 3px 3px 0 var(--ink);
}
.pill strong { display: block; font-size: 22px; line-height: 1; }
.pill small { display: block; font-size: 11px; margin-top: 3px; }
#active-effects {
  position: absolute;
  top: 90px;
  left: 12px;
  margin: 0;
  padding: 5px 9px;
  background: var(--cream);
  border: 2px solid var(--ink);
  border-radius: 10px;
  font-size: 12px;
  font-weight: 700;
  pointer-events: none;
}
#item-help {
  position: absolute;
  right: 14px;
  bottom: 116px;
  max-width: min(210px, 36vw);
  margin: 0;
  padding: 5px 9px;
  background: var(--cream);
  border: 2px solid var(--ink);
  border-radius: 10px;
  font-size: 12px;
  font-weight: 600;
  text-align: right;
  pointer-events: none;
}
.hint {
  position: absolute;
  z-index: 2;
  left: 12px;
  bottom: 12px;
  margin: 0;
  max-width: 46%;
  font-weight: 700;
  color: var(--ink);
  text-shadow: 0 1px 0 #fff;
}
#item-btn {
  position: absolute;
  z-index: 3;
  right: 14px;
  bottom: 14px;
  width: 92px;
  height: 92px;
  border-radius: 50%;
  border: 4px solid var(--ink);
  background: #d9d0c2;
  box-shadow: 5px 5px 0 var(--ink);
  font-weight: 700;
  font-size: 16px;
}
#item-btn.ready { background: var(--sun); animation: pulse 0.8s ease-in-out infinite; }
#item-btn:active { transform: translate(2px, 2px); }
#callout {
  position: absolute;
  z-index: 3;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%);
  font-size: 42px;
  font-weight: 700;
  color: #fff;
  -webkit-text-stroke: 4px var(--ink);
  pointer-events: none;
}
.tiny-btn {
  position: absolute;
  z-index: 3;
  left: 12px;
  bottom: 48px;
  min-height: 36px;
  padding: 4px 8px;
  font-size: 13px;
}

.rotate-gate {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 8;
  background: #fff0c2;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px;
  flex-direction: column;
  gap: 8px;
}
.phone-icon {
  width: 54px;
  height: 86px;
  border: 4px solid var(--ink);
  border-radius: 10px;
  margin-bottom: 8px;
}

@media (orientation: portrait) {
  #game-screen .rotate-gate { display: flex; }
}

.big-place { font-size: clamp(64px, 18vw, 120px); line-height: 0.85; }
.confetti { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.confetti i {
  position: absolute;
  top: -16px;
  width: 10px;
  height: 16px;
  border: 2px solid var(--ink);
  animation: fall 2.8s linear infinite;
}

@keyframes fall {
  to { transform: translateY(110vh) rotate(180deg); }
}
@keyframes pulse {
  50% { transform: scale(1.05); }
}

@media (max-width: 820px) {
  .banner { right: 16px; max-width: none; }
  .lobby-card { top: auto; bottom: 10px; left: 10px; right: 10px; width: auto; max-height: 58dvh; }
  :root { --board-w: min(220px, 40vw); --row: 46px; }
}

@media (prefers-reduced-motion: reduce) {
  .confetti i, #item-btn.ready, .live, .cut.go { animation: none; }
  .board li { transition: none; }
}
