:root {
  --tcg-blue: #2D9CDB;
  --tcg-blue-dark: #2185BF;
  --tcg-red: #EB5757;
  --tcg-red-dark: #D94343;
  --tcg-ice: #EAF5FB;
  --tcg-white: #FFFFFF;
  --tcg-charcoal: #333333;
  --tcg-muted: #7A869A;
  --tcg-border: #DCEBF5;
  --tcg-gray-bg: #EEEEEE;
  --tcg-gold: #F2C94C;
  --tcg-gold-dark: #C99A12;
  --tcg-green: #6FCF97;
  --radius: 12px;
  --pill: 100px;
  --shadow: 0 4px 16px rgba(17, 38, 60, 0.14);
}

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

html, body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--tcg-ice);
  color: var(--tcg-charcoal);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;
  height: 100dvh;
}

/* Top HUD Bar — TCG Company crisp white header + brand blue bar */
.hud-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 20px;
  background: var(--tcg-white);
  border-bottom: 3px solid var(--tcg-blue);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
  z-index: 10;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-logo-wrap {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

.brand-logo-img {
  height: 36px;
  width: auto;
  display: block;
}

.brand-logo-fallback {
  align-items: center;
  gap: 7px;
}

.brand-wordmark {
  display: flex;
  flex-direction: column;
  line-height: 1;
  color: var(--tcg-charcoal);
}

.brand-wordmark strong {
  font-size: 1.15rem;
  font-weight: 900;
  letter-spacing: 0.04em;
}

.brand-wordmark small {
  font-size: 0.56rem;
  font-weight: 700;
  letter-spacing: 0.24em;
  color: var(--tcg-muted);
}

.live-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--tcg-red);
  color: var(--tcg-white);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  padding: 4px 10px;
  border-radius: var(--pill);
}

.live-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #ffffff;
  animation: pulseDot 1.3s ease-in-out infinite;
}

@keyframes pulseDot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(0.75); }
}

.brand-sub {
  font-size: 0.86rem;
  font-weight: 700;
  color: var(--tcg-charcoal);
  letter-spacing: 0.01em;
}

.hud-center {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stat-pill {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 96px;
  padding: 5px 14px;
  background: var(--tcg-ice);
  border: 1px solid var(--tcg-border);
  border-radius: 12px;
}

.stat-label {
  font-size: 0.62rem;
  font-weight: 800;
  letter-spacing: 0.09em;
  color: var(--tcg-blue);
}

.stat-value {
  font-size: 0.94rem;
  font-weight: 800;
  color: var(--tcg-charcoal);
  font-variant-numeric: tabular-nums;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hud-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* TCG Company Pill Buttons */
.btn {
  border: none;
  border-radius: var(--pill);
  font-weight: 700;
  font-size: 0.84rem;
  font-family: inherit;
  padding: 9px 18px;
  cursor: pointer;
  transition: transform 0.12s ease, background-color 0.2s ease, box-shadow 0.2s ease;
  color: var(--tcg-white);
}

.btn:hover {
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background: var(--tcg-red);
  box-shadow: 0 3px 10px rgba(235, 87, 87, 0.32);
}

.btn-primary:hover {
  background: var(--tcg-red-dark);
}

.btn-primary.racing {
  background: var(--tcg-charcoal);
  box-shadow: 0 3px 10px rgba(51, 51, 51, 0.28);
}

.btn-primary.racing:hover {
  background: #1f1f1f;
}

.btn-secondary {
  background: var(--tcg-ice);
  color: var(--tcg-charcoal);
  border: 1px solid var(--tcg-border);
}

.btn-secondary:hover {
  background: #d8edf9;
}

.btn-brand {
  background: var(--tcg-blue);
  color: var(--tcg-white);
}

.btn-brand:hover {
  background: var(--tcg-blue-dark);
}

.btn-ghost {
  background: transparent;
  color: var(--tcg-muted);
}

.btn-ghost:hover {
  color: var(--tcg-charcoal);
}

.btn-tiny {
  font-size: 0.74rem;
  font-weight: 700;
  padding: 5px 12px;
  background: var(--tcg-ice);
  color: var(--tcg-charcoal);
  border: 1px solid var(--tcg-border);
}

.btn-tiny:hover {
  background: var(--tcg-blue);
  color: var(--tcg-white);
  border-color: var(--tcg-blue);
}

/* Main Stage */
.stage-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  background: var(--tcg-blue);
}

#race-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Legend Bar */
.item-legend {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: flex;
  gap: 10px;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(6px);
  border: 1px solid var(--tcg-border);
  border-radius: var(--pill);
  padding: 6px 14px;
  font-size: 0.74rem;
  font-weight: 700;
  color: var(--tcg-charcoal);
  box-shadow: var(--shadow);
  pointer-events: none;
}

.legend-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

/* Countdown Overlay */
.countdown-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(45, 156, 219, 0.22);
  pointer-events: none;
  z-index: 8;
}

.countdown-overlay.hidden {
  display: none;
}

.countdown-text {
  font-size: clamp(4.5rem, 12vw, 9rem);
  font-weight: 900;
  font-style: italic;
  color: var(--tcg-white);
  text-shadow:
    0 6px 0 var(--tcg-red),
    0 12px 30px rgba(17, 38, 60, 0.45);
  animation: popScale 0.45s cubic-bezier(0.22, 1.4, 0.36, 1);
}

@keyframes popScale {
  0% { transform: scale(0.35) rotate(-8deg); opacity: 0; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

/* Minimap Bar — TCG Company Charcoal "Join the legacy" strip */
.minimap-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 20px calc(9px + env(safe-area-inset-bottom, 0px));
  background: var(--tcg-charcoal);
  color: var(--tcg-white);
  border-top: 2px solid var(--tcg-blue);
}

.minimap-brand {
  font-size: 0.78rem;
  font-weight: 800;
  font-style: italic;
  color: var(--tcg-gold);
  letter-spacing: 0.03em;
  white-space: nowrap;
  padding-right: 8px;
  border-right: 1px solid rgba(255, 255, 255, 0.2);
}

.minimap-label {
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: #cbd5e1;
}

.minimap-track {
  position: relative;
  flex: 1;
  height: 22px;
  background: linear-gradient(90deg, #1b6ea1 0%, var(--tcg-blue) 100%);
  border-radius: var(--pill);
  border: 1px solid rgba(255, 255, 255, 0.22);
  overflow: visible;
}

.minimap-finish-flag {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 0.85rem;
}

.minimap-dot {
  position: absolute;
  top: 50%;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #ffffff;
  transform: translate(-50%, -50%);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
  transition: left 0.08s linear;
}

/* Modals — Crisp TCG Company Light Cards */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(24, 39, 58, 0.6);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  padding: 16px;
}

.modal-backdrop.hidden {
  display: none;
}

.modal-card {
  width: 100%;
  max-width: 520px;
  max-height: 92vh;
  display: flex;
  flex-direction: column;
  background: var(--tcg-white);
  border: 2px solid var(--tcg-blue);
  border-radius: 16px;
  box-shadow: 0 20px 50px rgba(17, 38, 60, 0.35);
  overflow: hidden;
  color: var(--tcg-charcoal);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  background: var(--tcg-blue);
  color: var(--tcg-white);
}

.modal-header h2 {
  font-size: 1.1rem;
  font-weight: 800;
  color: var(--tcg-white);
}

.icon-btn {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.9);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.icon-btn:hover {
  color: #ffffff;
}

.modal-body {
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
}

.form-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-row label {
  font-size: 0.84rem;
  font-weight: 700;
  color: var(--tcg-charcoal);
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.count-badge {
  display: inline-block;
  padding: 2px 9px;
  border-radius: var(--pill);
  background: var(--tcg-blue);
  color: var(--tcg-white);
  font-size: 0.7rem;
  font-weight: 800;
}

.hint {
  font-weight: 500;
  color: var(--tcg-muted);
  font-size: 0.76rem;
}

textarea, select, input[type="number"], input[type="text"] {
  width: 100%;
  background: var(--tcg-ice);
  color: var(--tcg-charcoal);
  border: 1.5px solid var(--tcg-border);
  border-radius: 10px;
  padding: 9px 12px;
  font-size: 0.9rem;
  font-family: inherit;
}

textarea:focus, select:focus, input[type="number"]:focus, input[type="text"]:focus {
  outline: none;
  border-color: var(--tcg-blue);
  background: var(--tcg-white);
}

.time-inputs {
  display: flex;
  align-items: center;
  gap: 6px;
}

.time-field {
  position: relative;
  flex: 1;
  display: flex;
  align-items: center;
}

.time-field input {
  padding-right: 36px;
  text-align: center;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.time-field span {
  position: absolute;
  right: 10px;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--tcg-muted);
  pointer-events: none;
}

.time-sep {
  font-weight: 900;
  font-size: 1.1rem;
  color: var(--tcg-blue);
}

.add-name-row {
  display: flex;
  gap: 8px;
}

.add-name-row input {
  flex: 1;
}

.btn-add {
  white-space: nowrap;
  padding: 8px 16px;
  background: var(--tcg-blue);
  color: var(--tcg-white);
  border: none;
}

.btn-add:hover {
  background: var(--tcg-blue-dark);
}

.btn-danger-tiny {
  color: var(--tcg-red);
  border-color: rgba(235, 87, 87, 0.35);
  background: #FFF5F5;
}

.btn-danger-tiny:hover {
  background: var(--tcg-red);
  color: #ffffff;
  border-color: var(--tcg-red);
}

.quick-fill-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.modal-footer {
  padding: 14px 20px;
  background: var(--tcg-ice);
  border-top: 1px solid var(--tcg-border);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* Winner Modal */
.winner-card {
  text-align: center;
  padding: 22px 22px 20px;
  border-top: 6px solid var(--tcg-gold);
}

.winner-announcer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-bottom: 12px;
}

.announcer-svg {
  width: 104px;
  height: 112px;
  flex-shrink: 0;
  overflow: visible;
  filter: drop-shadow(0 4px 6px rgba(17, 38, 60, 0.14));
  animation: mimiHop 1.6s ease-in-out infinite;
}

.mimi-head {
  transform-origin: 63px 94px;
  animation: mimiHeadBob 1.6s ease-in-out infinite;
}

.mimi-ear-right {
  transform-origin: 84px 74px;
  animation: mimiEarFlop 1.6s ease-in-out infinite;
}

.mimi-tail {
  transform-origin: 88px 112px;
  animation: mimiTailWag 0.75s ease-in-out infinite alternate;
}

.mimi-peep-eyes {
  transform-origin: 60px 113px;
  animation: mimiBlink 3.4s infinite;
}

@keyframes mimiHop {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@keyframes mimiHeadBob {
  0%, 100% { transform: rotate(-4deg); }
  50% { transform: rotate(5deg); }
}

@keyframes mimiEarFlop {
  0%, 100% { transform: rotate(0deg); }
  50% { transform: rotate(10deg); }
}

@keyframes mimiTailWag {
  0% { transform: rotate(-8deg); }
  100% { transform: rotate(10deg); }
}

@keyframes mimiBlink {
  0%, 92%, 100% { transform: scaleY(1); }
  95% { transform: scaleY(0.1); }
}

.announcer-bubble {
  position: relative;
  flex: 1;
  text-align: left;
  background: var(--tcg-ice);
  border: 2px solid var(--tcg-blue);
  border-radius: 16px;
  padding: 12px 15px;
  box-shadow: 0 4px 12px rgba(45, 156, 219, 0.14);
}

.announcer-bubble::before {
  content: "";
  position: absolute;
  left: -10px;
  top: 50%;
  transform: translateY(-50%);
  border-width: 8px 10px 8px 0;
  border-style: solid;
  border-color: transparent var(--tcg-blue) transparent transparent;
}

.announcer-bubble::after {
  content: "";
  position: absolute;
  left: -7px;
  top: 50%;
  transform: translateY(-50%);
  border-width: 6px 8px 6px 0;
  border-style: solid;
  border-color: transparent var(--tcg-ice) transparent transparent;
}

.announcer-line {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--tcg-muted);
  margin-top: 4px;
}

.winner-kicker {
  display: inline-block;
  padding: 3px 10px;
  border-radius: var(--pill);
  background: var(--tcg-gold);
  color: var(--tcg-charcoal);
  font-size: 0.68rem;
  font-weight: 900;
  letter-spacing: 0.12em;
}

.winner-name {
  font-size: 1.65rem;
  font-weight: 900;
  margin: 2px 0 0;
  color: var(--tcg-charcoal);
  line-height: 1.15;
  word-break: break-word;
}

.winner-stats {
  font-size: 0.84rem;
  color: var(--tcg-muted);
  margin-bottom: 12px;
}

.photo-finish-wrap {
  margin: 0 auto 14px;
  padding: 7px 7px 6px;
  max-width: 310px;
  background: #ffffff;
  border: 1.5px solid var(--tcg-border);
  border-radius: 10px;
  box-shadow: 0 6px 18px rgba(17, 38, 60, 0.16);
  transform: rotate(-1.2deg);
}

.photo-finish-wrap.hidden {
  display: none;
}

.photo-finish-img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 6px;
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.photo-finish-caption {
  margin-top: 5px;
  font-size: 0.68rem;
  font-weight: 900;
  letter-spacing: 0.1em;
  color: var(--tcg-blue);
}

.podium-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 210px;
  overflow-y: auto;
  margin-bottom: 18px;
  text-align: left;
}

.podium-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--tcg-ice);
  font-size: 0.86rem;
  font-weight: 700;
  color: var(--tcg-charcoal);
}

.podium-row.rank-1 { background: #FFF6D6; border: 1.5px solid var(--tcg-gold); }
.podium-row.rank-2 { background: #EEF2F6; }
.podium-row.rank-3 { background: #FCEFE3; }

.winner-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

@media (max-width: 720px) {
  .hud-bar {
    padding: 6px 10px;
    gap: 6px;
  }
  .brand-logo-img {
    height: 26px;
  }
  .brand-sub {
    display: none;
  }
  .hud-center {
    gap: 5px;
    flex: 1;
    justify-content: flex-end;
  }
  .stat-pill {
    min-width: 62px;
    padding: 3px 7px;
  }
  .stat-label {
    font-size: 0.54rem;
  }
  .stat-value {
    font-size: 0.78rem;
    max-width: 76px;
  }
  .hud-actions {
    width: 100%;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 5px;
  }
  .hud-actions .btn {
    flex: 1;
    padding: 7px 6px;
    font-size: 0.72rem;
    text-align: center;
    white-space: nowrap;
  }
  .item-legend {
    display: none;
  }
  .minimap-bar {
    padding: 7px 12px calc(7px + env(safe-area-inset-bottom, 0px));
    gap: 8px;
  }
  .minimap-brand {
    display: none;
  }
  .form-grid {
    grid-template-columns: 1fr;
  }
}
