:root {
  --ink-1: rgba(10, 24, 44, 0.94);
  --ink-2: rgba(8, 20, 38, 0.86);
  --panel-border: rgba(120, 170, 220, 0.22);
  --panel-hi: rgba(255, 255, 255, 0.08);
  --gold: #ffd34d;
  --gold-deep: #f5a623;
  --silver: #d7e0ea;
  --bronze: #e8934a;
  --cyan: #4fd6ff;
  --good: #37e08a;
  --pink: #ff6fd8;
  --danger: #ff4d6d;
  --text: #eaf4ff;
  --muted: #9fb4cc;
  --shadow: 0 14px 38px rgba(0, 0, 0, 0.45);
  --font: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: #04101c;
  font-family: var(--font);
  color: var(--text);
  overflow: hidden;
}

/* Letterboxed 16:9 stage */
.stage-wrap {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at 50% -10%, #123049, #04101c 72%);
}
.stage {
  position: relative;
  width: min(100vw, calc(100vh * 16 / 9));
  height: min(100vh, calc(100vw * 9 / 16));
  aspect-ratio: 16 / 9;
  overflow: hidden;
  background: #0a2a44;
  box-shadow: 0 0 80px rgba(0, 0, 0, 0.6);
}
#race { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.stage > *:not(#race) { position: absolute; z-index: 2; }

/* Shared panel look */
.panel-look {
  background: linear-gradient(180deg, var(--ink-1), var(--ink-2));
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  box-shadow: var(--shadow), inset 0 1px 0 var(--panel-hi);
}

/* LIVE badge */
.live-badge {
  top: 3%; left: 1.6%;
  background: linear-gradient(180deg, #ff466f, #d0184e);
  color: #fff; font-weight: 900; font-size: clamp(10px, 1.4vw, 18px);
  padding: 0.34em 0.85em; border-radius: 999px;
  display: flex; align-items: center; gap: 0.45em; letter-spacing: 1px;
  box-shadow: 0 6px 18px rgba(208, 24, 78, 0.45), inset 0 1px 0 rgba(255,255,255,.35);
  border: 1px solid rgba(255, 255, 255, 0.4);
}
.live-badge .dot { width: 0.55em; height: 0.55em; background: #fff; border-radius: 50%; animation: blink 1s infinite; box-shadow: 0 0 8px #fff; }
@keyframes blink { 50% { opacity: 0.25; } }

/* Phase + timer */
.phase-hud {
  top: 0; left: 50%; transform: translateX(-50%);
  text-align: center;
  background: linear-gradient(180deg, var(--ink-1), var(--ink-2));
  padding: 0.5em 1.5em 0.6em; border-radius: 0 0 20px 20px;
  border: 1px solid var(--panel-border); border-top: none;
  box-shadow: 0 10px 26px rgba(0,0,0,.4);
  min-width: 168px;
  z-index: 5;
}
.phase-hud .phase-name {
  font-size: clamp(10px, 1.4vw, 17px); letter-spacing: 4px; font-weight: 800;
  color: var(--gold); text-shadow: 0 1px 6px rgba(245,166,35,.4);
}
.phase-hud .timer {
  font-size: clamp(24px, 3.8vw, 50px); font-weight: 900; line-height: 1;
  font-variant-numeric: tabular-nums; letter-spacing: 1px;
  text-shadow: 0 2px 10px rgba(0,0,0,.55);
}
.phase-hud .round { font-size: clamp(9px, 1.1vw, 13px); color: var(--muted); font-weight: 700; letter-spacing: 0.5px; }
/* Results already have their own winner card. Hide the large timer/phase
   banner so the broadcast stays focused on the result, not the UI chrome. */
.stage.results-phase .phase-hud { display: none; }

.top-right {
  top: 2.6%;
  right: 1.6%;
  display: flex;
  gap: 0.5em;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  z-index: 3;
}
.pill {
  background: linear-gradient(180deg, var(--ink-1), var(--ink-2));
  border: 1px solid var(--panel-border);
  padding: 0.42em 0.9em; border-radius: 999px;
  font-size: clamp(9px, 1.1vw, 14px); font-weight: 800; letter-spacing: 0.5px;
  color: var(--text); text-decoration: none; box-shadow: 0 4px 12px rgba(0,0,0,.35);
}
.leaderboard-toggle {
  font-family: inherit;
  cursor: pointer;
}
.leaderboard-toggle:hover { border-color: var(--cyan); color: var(--cyan); }
.pill.link:hover { border-color: var(--cyan); color: var(--cyan); }
.pill.link:focus-visible, .leaderboard-toggle:focus-visible, .results-toggle:focus-visible, .results-actions button:focus-visible {
  outline: 3px solid var(--cyan);
  outline-offset: 3px;
}
/* Right leaderboard */
.leaderboard {
  top: 8.6%; right: 1.4%; width: 24%; bottom: 6.5%;
  background: linear-gradient(180deg, rgba(12,30,54,0.52), rgba(6,18,36,0.42));
  border: 1px solid rgba(210, 235, 255, 0.22);
  border-radius: 14px; padding: 0.7em 0.7em 0.4em;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: var(--shadow), inset 0 1px 0 var(--panel-hi);
}
.leaderboard h3 {
  font-size: clamp(12px, 1.45vw, 20px); margin-bottom: 0.55em; text-align: center;
  color: var(--gold); letter-spacing: 0.5px; font-weight: 900;
  padding-bottom: 0.35em; border-bottom: 1px solid rgba(255,255,255,.1);
  text-shadow: 0 1px 6px rgba(245,166,35,.35);
}
.leaderboard ol { list-style: none; overflow-y: auto; flex: 1; padding-right: 3px; }
.leaderboard ol:empty::before {
  content: "Изчакваме участници";
  display: block;
  margin: 1.4em .25em;
  padding: 1em .5em;
  border: 1px dashed rgba(159,180,204,.35);
  border-radius: 10px;
  color: var(--muted);
  font-size: clamp(10px, 1.1vw, 14px);
  font-weight: 700;
  text-align: center;
}
.leaderboard ol::-webkit-scrollbar { width: 6px; }
.leaderboard ol::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); border-radius: 3px; }
.leaderboard li {
  display: grid; grid-template-columns: 1.7em 1fr auto; align-items: center; gap: 0.5em;
  padding: 0.32em 0.4em; margin-bottom: 4px; border-radius: 10px;
  font-size: clamp(10px, 1.15vw, 15px);
  background: rgba(255,255,255,0.04); border: 1px solid transparent;
}
.leaderboard li .pos {
  font-weight: 900; text-align: center; color: #06263f; height: 1.6em; line-height: 1.6em;
  background: linear-gradient(180deg, #e7eef6, #b7c4d3); border-radius: 8px; font-size: 0.82em;
}
.leaderboard li:nth-child(1) { background: rgba(255,211,77,0.14); border-color: rgba(255,211,77,0.4); }
.leaderboard li:nth-child(1) .pos { background: linear-gradient(180deg, #ffe27a, #f5a623); }
.leaderboard li:nth-child(2) .pos { background: linear-gradient(180deg, #f2f6fb, #c3cedb); }
.leaderboard li:nth-child(3) .pos { background: linear-gradient(180deg, #f0a869, #d07b34); }
.leaderboard li .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; align-items: center; gap: 7px; font-weight: 700; }
.leaderboard li .swatch { width: 0.9em; height: 0.9em; border-radius: 50%; flex: none; box-shadow: 0 0 0 2px rgba(255,255,255,.35), 0 1px 2px rgba(0,0,0,.4); }
.leaderboard li .dist { color: var(--cyan); font-weight: 800; font-variant-numeric: tabular-nums; }
.leaderboard li.boost { border-color: var(--pink); box-shadow: 0 0 12px rgba(255,111,216,.35); animation: boostRow 1s ease-in-out infinite; }
@keyframes boostRow { 50% { box-shadow: 0 0 18px rgba(255,111,216,.6); } }

/* Center overlay (countdown / lobby status) */
.center-overlay { inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; text-align: center; }
.center-overlay .cd {
  width: clamp(120px, 22vw, 300px); height: clamp(120px, 22vw, 300px);
  display: flex; align-items: center; justify-content: center; border-radius: 50%;
  background: radial-gradient(circle, rgba(13,32,56,0.6), rgba(13,32,56,0));
  border: 4px solid rgba(79,214,255,0.5); box-shadow: 0 0 50px rgba(79,214,255,0.35);
  animation: pop 0.5s ease;
}
.center-overlay .cd span {
  font-size: clamp(64px, 15vw, 210px); font-weight: 900; color: #fff;
  text-shadow: 0 0 30px rgba(79,214,255,.8), 0 8px 24px rgba(0,0,0,.7);
}
.center-overlay .cd small {
  position: absolute; margin-top: clamp(105px, 20vw, 270px); color: var(--cyan);
  font-size: clamp(9px, 1.1vw, 14px); font-weight: 900; letter-spacing: 1.5px;
  text-shadow: 0 2px 8px rgba(0,0,0,.8);
}
@keyframes pop { from { transform: scale(1.6); opacity: 0; } }
.center-overlay .cta {
  display: flex; flex-direction: column; align-items: center; gap: 0.15em;
  background: linear-gradient(180deg, rgba(13,32,56,0.94), rgba(8,20,40,0.94));
  border: 3px solid var(--gold); border-radius: 24px; padding: 0.7em 1.8em;
  box-shadow: 0 0 46px rgba(255,211,77,.35), var(--shadow);
  animation: breathe 2s ease-in-out infinite;
}
@keyframes breathe { 50% { transform: scale(1.035); } }
.center-overlay .cta-title { font-size: clamp(22px, 4.4vw, 58px); font-weight: 900; color: var(--gold); letter-spacing: 2px; text-shadow: 0 3px 12px rgba(0,0,0,.5); }
.center-overlay .cta-sub { font-size: clamp(11px, 1.7vw, 22px); color: var(--text); font-weight: 700; }
.center-overlay .cta-sub b { color: var(--cyan); }
.center-overlay .cta-hint { font-size: clamp(8px, 1.1vw, 13px); color: var(--muted); text-transform: uppercase; letter-spacing: 1px; margin-top: 0.2em; }
@keyframes bannerPop { from { transform: translate(-50%, -8px) scale(0.9); opacity: 0; } to { transform: translateX(-50%) scale(1); opacity: 1; } }

/* Bottom bar */
.bottom-bar { bottom: 1.6%; left: 1.6%; display: flex; gap: 0.6em; font-size: clamp(9px, 1.1vw, 15px); font-weight: 800; }
.bottom-bar span {
  background: linear-gradient(180deg, var(--ink-1), var(--ink-2));
  padding: 0.34em 0.8em; border-radius: 999px; border: 1px solid var(--panel-border);
  box-shadow: 0 4px 12px rgba(0,0,0,.35);
}

/* Results card — compact, draggable, never a full-screen dimmer */
.results-modal {
  inset: 0;
  background: transparent;
  pointer-events: none;
  z-index: 5;
}
.results-modal.hidden { display: none; }
.results-card {
  pointer-events: auto;
  position: absolute;
  top: 10%;
  left: 50%;
  transform: translateX(-50%);
  width: min(380px, 52%);
  max-height: min(52%, 460px);
  background: linear-gradient(180deg, #14315a, #0a1c34);
  border: 3px solid var(--gold);
  border-radius: 18px;
  padding: 0.65em 0.8em 0.7em;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 18px 48px rgba(0,0,0,.55), inset 0 1px 0 var(--panel-hi);
}
.results-card.moved { transform: none; }
.results-head {
  display: flex; align-items: center; justify-content: space-between; gap: 0.6em;
  margin-bottom: 0.45em;
  cursor: grab;
  user-select: none;
  touch-action: none;
}
.results-head:active { cursor: grabbing; }
.results-card h2 { text-align: left; font-size: clamp(13px, 1.7vw, 20px); margin: 0; flex: 1; color: var(--gold); font-weight: 900; letter-spacing: 0.4px; text-shadow: 0 2px 10px rgba(0,0,0,.4); }
.results-toggle {
  font-family: inherit; cursor: pointer; white-space: nowrap;
  background: rgba(255,255,255,.14); color: #fff; border: 1px solid rgba(255,255,255,.28);
  border-radius: 999px; padding: 0.35em 0.75em; font-weight: 800;
  font-size: clamp(10px, 1.1vw, 13px);
}
.results-toggle:hover { border-color: var(--cyan); color: var(--cyan); }
.results-modal.collapsed .results-card {
  width: min(340px, 48%);
  max-height: none;
  padding: 0.5em 0.7em 0.55em;
}
.results-modal.collapsed .results-head { margin-bottom: 0.2em; }
.results-modal.collapsed h2 { font-size: clamp(12px, 1.5vw, 16px); }
.results-modal.collapsed .podium,
.results-modal.collapsed .results-scroll,
.results-modal.collapsed .confetti,
.results-modal.collapsed .winner-card .wc-trophy,
.results-modal.collapsed .winner-card .wc-dist { display: none; }
.results-modal.collapsed .winner-card {
  margin-bottom: 0.35em; padding: 0.2em 0.35em 0.25em; border-width: 2px;
}
.podium { display: flex; justify-content: center; align-items: flex-end; gap: 0.9em; margin-bottom: 0.7em; }
.podium .p {
  text-align: center; border-radius: 16px; padding: 0.8em 1em; min-width: 24%;
  background: linear-gradient(180deg, rgba(255,255,255,.1), rgba(255,255,255,.03));
  border: 1px solid rgba(255,255,255,.14);
}
.podium .p.gold { border-color: var(--gold); box-shadow: 0 0 34px rgba(255,211,77,.5); transform: translateY(-10px) scale(1.05); }
.podium .medal { font-size: clamp(26px, 3.6vw, 46px); filter: drop-shadow(0 3px 4px rgba(0,0,0,.4)); }
.podium .p .nm { font-weight: 900; font-size: clamp(12px,1.6vw,19px); margin-top: 4px; }
.podium .p.gold .nm { color: var(--gold); }
.podium .p .meta { font-size: clamp(9px,1.15vw,13px); color: var(--cyan); font-weight: 700; }
.results-scroll { overflow-y: auto; flex: 1; min-height: 0; border: 1px solid rgba(255,255,255,.14); border-radius: 14px; background: rgba(0,0,0,.18); }
.results-scroll table { width: 100%; border-collapse: collapse; font-size: clamp(10px,1.2vw,15px); }
.results-scroll th, .results-scroll td { padding: 0.45em 0.8em; text-align: left; border-bottom: 1px solid rgba(255,255,255,.07); }
.results-scroll th { position: sticky; top: 0; background: #10233f; color: var(--gold); font-weight: 800; }
.results-scroll tr:nth-child(1) td { color: var(--gold); font-weight: 800; }
.center-overlay .cta.full { border-color: var(--danger); box-shadow: 0 0 46px rgba(255,77,109,.4), var(--shadow); }
.center-overlay .cta.full .cta-title { color: var(--danger); }

/* Join toast */
.join-toast {
  bottom: 12%; left: 50%; transform: translateX(-50%); z-index: 6;
  background: linear-gradient(180deg, rgba(16,40,26,0.95), rgba(10,28,18,0.95));
  border: 2px solid var(--good); color: #eafff2; font-weight: 800;
  padding: 0.4em 1em; border-radius: 999px; font-size: clamp(11px,1.5vw,18px);
  box-shadow: 0 0 26px rgba(55,224,138,0.45), var(--shadow); animation: bannerPop 0.3s ease;
}

/* Finish-line confetti — over the river, under the winner banner */
.finish-confetti {
  inset: 0; z-index: 4; overflow: hidden; pointer-events: none;
}
.finish-confetti i {
  position: absolute;
  width: 8px;
  height: 13px;
  border-radius: 2px;
  opacity: 0.95;
  animation-name: finishConfettiBurst;
  animation-timing-function: cubic-bezier(0.16, 0.7, 0.3, 1);
  animation-fill-mode: forwards;
  animation-iteration-count: 1;
}
@keyframes finishConfettiBurst {
  0% { transform: translate3d(0, 0, 0) rotate(0deg); opacity: 1; }
  80% { opacity: 1; }
  100% { transform: translate3d(var(--dx), var(--dy), 0) rotate(var(--rot)); opacity: 0; }
}

/* Winner flash (finish moment) */
.winner-flash {
  top: 34%; left: 50%; transform: translateX(-50%); z-index: 6;
  display: flex; flex-direction: column; align-items: center; gap: 0.1em;
  padding: 0.5em 1.4em; border-radius: 18px;
  background: linear-gradient(180deg, rgba(20,26,54,0.95), rgba(12,16,40,0.95));
  border: 3px solid var(--gold); box-shadow: 0 0 50px rgba(255,211,77,0.55), var(--shadow);
  animation: bannerPop 0.3s ease;
}
.winner-flash .wf-tag { color: var(--gold); font-weight: 900; letter-spacing: 3px; font-size: clamp(14px,2vw,26px); }
.winner-flash .wf-name { color: #fff; font-weight: 900; font-size: clamp(20px,3.4vw,42px); text-shadow: 0 3px 12px rgba(0,0,0,.5); }

/* Winner card in results */
.winner-card {
  position: relative; text-align: center; margin-bottom: 0.45em; padding: 0.35em 0.6em 0.4em;
  border-radius: 12px; border: 2px solid var(--gold);
  background: radial-gradient(circle at 50% 0%, rgba(255,211,77,0.22), rgba(255,211,77,0.04));
  box-shadow: 0 0 24px rgba(255,211,77,0.28), inset 0 1px 0 var(--panel-hi);
}
.winner-card .wc-trophy { font-size: clamp(22px, 3.4vw, 40px); filter: drop-shadow(0 4px 6px rgba(0,0,0,.4)); animation: trophyPop 0.5s ease; }
@keyframes trophyPop { from { transform: scale(0.5) rotate(-12deg); opacity: 0; } }
.winner-card .wc-tag { color: var(--gold); font-weight: 900; letter-spacing: 3px; font-size: clamp(11px,1.4vw,16px); }
.winner-card .wc-name { color: #fff; font-weight: 900; font-size: clamp(16px,2.2vw,26px); text-shadow: 0 3px 12px rgba(0,0,0,.5); }
.winner-card .wc-dist { color: var(--cyan); font-weight: 800; font-size: clamp(11px,1.4vw,17px); }

/* Confetti */
.confetti { position: absolute; inset: 0; overflow: hidden; pointer-events: none; border-radius: 24px; }
.confetti i {
  position: absolute; top: -12px; width: 9px; height: 14px; border-radius: 2px; opacity: 0.95;
  animation-name: confettiFall; animation-timing-function: linear; animation-iteration-count: 1;
}
@keyframes confettiFall {
  0% { transform: translateY(-20px) rotate(0deg); opacity: 1; }
  100% { transform: translateY(560px) rotate(540deg); opacity: 0; }
}

/* Debug overlay */
.debug-panel {
  left: 1.4%; top: 22%; z-index: 7; max-width: 42%;
  background: rgba(2, 10, 20, 0.86); border: 1px solid rgba(120,170,220,.3); border-radius: 10px;
  padding: 8px 10px; color: #b8ffd0; font-family: "Consolas", monospace; white-space: pre;
  font-size: clamp(8px, 1vw, 12px); line-height: 1.35;
}

.hidden { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
  }
}
