:root {
  --ink: #473638;
  --ink-soft: #7b6668;
  --glass: rgba(255, 252, 248, 0.86);
  --border: rgba(255, 255, 255, 0.94);
  --shadow: 0 12px 32px rgba(64, 48, 56, 0.18);
  --pink: #ff80ab;
  --gold: #ffca45;
  --aqua: #4dd0e1;
  --radius: 22px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: 'Zen Maru Gothic', 'Noto Sans TC', 'Microsoft JhengHei', sans-serif;
  color: var(--ink);
  background: #87dcff;
}

.hidden {
  display: none !important;
}

#race-canvas-container {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
}

/* ===== Top Right Utility Bar ===== */
.top-utility-bar {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}

.race-util-drawer {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
}

.util-icon-row {
  display: flex;
  gap: 6px;
}

.race-mobile-menu-btn {
  display: none;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 2.5px solid var(--border);
  background: var(--glass);
  backdrop-filter: blur(12px);
  font-size: 1.15rem;
  font-weight: 900;
  color: var(--ink);
  box-shadow: var(--shadow);
  cursor: pointer;
}

.race-jukebox-pill {
  display: flex;
  align-items: center;
  gap: 5px;
  background: var(--glass);
  backdrop-filter: blur(12px);
  border: 2.5px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  box-shadow: var(--shadow);
}

.race-jukebox-select {
  border: none;
  background: transparent;
  font-family: inherit;
  font-size: 0.8rem;
  font-weight: 800;
  color: var(--ink);
  cursor: pointer;
  outline: none;
}

.util-pill {
  text-decoration: none;
  background: var(--glass);
  backdrop-filter: blur(12px);
  border: 2.5px solid var(--border);
  border-radius: 999px;
  padding: 8px 15px;
  font-weight: 800;
  font-size: 0.88rem;
  color: var(--ink);
  box-shadow: var(--shadow);
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.2s;
}

.util-pill:hover {
  transform: translateY(-2px) scale(1.04);
  background: #ffffff;
}

.util-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 2.5px solid var(--border);
  background: var(--glass);
  backdrop-filter: blur(12px);
  font-size: 1.1rem;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), border-color 0.2s;
}

.util-btn:hover {
  transform: scale(1.1);
}

.util-btn.active {
  background: #ffffff;
  border-color: var(--pink);
}

/* ===== 1. GARAGE / SELECT OVERLAY ===== */
.garage-overlay {
  position: fixed;
  inset: 0;
  z-index: 25;
  pointer-events: none;
}

.garage-header {
  position: fixed;
  top: 16px;
  left: 18px;
  pointer-events: auto;
}

.garage-title-pill {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--glass);
  backdrop-filter: blur(14px);
  border: 2.5px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 20px;
  box-shadow: var(--shadow);
}

.title-flag {
  font-size: 2rem;
}

.garage-title-pill h1 {
  font-size: 1.32rem;
  font-weight: 900;
  color: var(--ink);
  line-height: 1.2;
}

.garage-title-pill p {
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: #e06389;
  margin-top: 2px;
}

.garage-right-panel {
  position: fixed;
  right: 22px;
  top: 50%;
  transform: translateY(-54%);
  pointer-events: auto;
  width: 325px;
}

.kart-spec-card {
  background: var(--glass);
  backdrop-filter: blur(16px);
  border: 3px solid var(--border);
  border-radius: 26px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.spec-char-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.spec-badge {
  width: 52px;
  height: 52px;
  border-radius: 16px;
  background: linear-gradient(135deg, #fff5f8, #ffe0eb);
  border: 2px solid #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.7rem;
  flex-shrink: 0;
}

.spec-char-row h2 {
  font-size: 1.25rem;
  font-weight: 900;
}

.spec-vehicle-name {
  font-size: 0.85rem;
  font-weight: 800;
  color: #df5b86;
  margin-top: 2px;
}

.spec-vehicle-desc {
  font-size: 0.8rem;
  color: var(--ink-soft);
  margin-top: 10px;
  line-height: 1.45;
}

.stat-bars {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.8rem;
  font-weight: 800;
}

.stat-row span {
  width: 36px;
}

.stat-track {
  flex: 1;
  height: 10px;
  background: rgba(71, 54, 56, 0.12);
  border-radius: 999px;
  overflow: hidden;
}

.stat-fill {
  height: 100%;
  border-radius: 999px;
  transition: width 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.fill-speed { background: linear-gradient(90deg, #ff7c98, #ff476f); }
.fill-accel { background: linear-gradient(90deg, #ffd15c, #ff9d2e); }
.fill-handle { background: linear-gradient(90deg, #6dd5fa, #3ba8f5); }

.start-race-btn {
  width: 100%;
  margin-top: 16px;
  padding: 13px 18px;
  border-radius: 999px;
  border: 3px solid #ffffff;
  background: linear-gradient(135deg, #ff6c96, #ff9a52);
  color: #ffffff;
  font-family: inherit;
  font-size: 1.08rem;
  font-weight: 900;
  letter-spacing: 0.03em;
  cursor: pointer;
  box-shadow: 0 8px 22px rgba(255, 108, 150, 0.42);
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s;
}

.start-race-btn:hover {
  transform: translateY(-3px) scale(1.03);
  box-shadow: 0 12px 26px rgba(255, 108, 150, 0.55);
}

.start-race-btn:active {
  transform: scale(0.96);
}

.garage-controls-hint {
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  font-size: 0.72rem;
  color: var(--ink-soft);
}

.garage-char-dock {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  pointer-events: auto;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 7px;
  background: var(--glass);
  backdrop-filter: blur(16px);
  border: 2.5px solid var(--border);
  border-radius: 26px;
  padding: 7px 12px;
  box-shadow: var(--shadow);
  max-width: min(1080px, calc(100vw - 24px));
}

.garage-char-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px 6px 8px;
  border-radius: 999px;
  border: 2.5px solid transparent;
  background: rgba(255, 255, 255, 0.72);
  color: var(--ink);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 800;
  cursor: pointer;
  transition: all 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.garage-char-btn:hover {
  transform: translateY(-3px);
  background: #ffffff;
  border-color: var(--accent);
}

.garage-char-btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 6px 14px rgba(71, 54, 56, 0.16);
}

.garage-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--accent-soft);
  border: 1.5px solid var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95rem;
}

/* ===== 2. IN-RACE HUD ===== */
.race-hud {
  position: fixed;
  inset: 0;
  z-index: 25;
  pointer-events: none;
}

.hud-rank-card {
  position: fixed;
  top: 16px;
  left: 18px;
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--glass);
  backdrop-filter: blur(14px);
  border: 3px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 20px;
  box-shadow: var(--shadow);
}

.rank-big {
  display: flex;
  align-items: baseline;
  line-height: 1;
}

#hud-rank-num {
  font-size: 2.6rem;
  font-weight: 900;
  color: #ff5c8a;
}

.rank-suffix {
  font-size: 1.1rem;
  font-weight: 900;
  color: #ff5c8a;
  margin-left: 1px;
}

.rank-total {
  font-size: 0.95rem;
  font-weight: 800;
  color: var(--ink-soft);
  margin-left: 4px;
}

.lap-time-col {
  border-left: 2px solid rgba(71, 54, 56, 0.12);
  padding-left: 14px;
}

.hud-lap {
  font-size: 1rem;
  font-weight: 900;
  color: var(--ink);
}

.hud-timer {
  font-size: 0.85rem;
  font-weight: 800;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
  margin-top: 2px;
}

/* Item Roulette */
.hud-item-wrap {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.item-slot {
  width: 72px;
  height: 72px;
  border-radius: 22px;
  background: linear-gradient(135deg, #fffdf5, #ffe9b8);
  border: 4px solid #ffffff;
  box-shadow: 0 8px 24px rgba(255, 165, 60, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.3rem;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.item-slot.rolling {
  animation: slotBounce 0.16s infinite alternate;
}

.item-slot.ready {
  background: linear-gradient(135deg, #fff0f6, #ffd4e5);
  border-color: #ff7ba6;
  transform: scale(1.06);
}

@keyframes slotBounce {
  from { transform: scale(0.94); }
  to { transform: scale(1.08); }
}

.item-name-pill {
  background: var(--glass);
  border: 2px solid var(--border);
  border-radius: 999px;
  padding: 3px 12px;
  font-size: 0.78rem;
  font-weight: 800;
  color: var(--ink);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

/* Center Banner */
.center-banner {
  position: fixed;
  top: 38%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: 4.8rem;
  font-weight: 900;
  color: #ffffff;
  text-shadow: 0 6px 0 #ff5c8a, 0 12px 30px rgba(71, 54, 56, 0.35);
  letter-spacing: 0.04em;
  animation: popBanner 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes popBanner {
  from { transform: translate(-50%, -50%) scale(0.4); opacity: 0; }
  to { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}

.event-toast {
  position: fixed;
  top: 122px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(71, 54, 56, 0.88);
  color: #ffffff;
  font-weight: 800;
  font-size: 0.95rem;
  padding: 8px 20px;
  border-radius: 999px;
  border: 2px solid rgba(255, 255, 255, 0.65);
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.2);
}

/* Minimap */
.minimap-card {
  position: fixed;
  bottom: 18px;
  left: 18px;
  background: rgba(255, 252, 248, 0.78);
  backdrop-filter: blur(12px);
  border: 3px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
}

.zone-label {
  margin-top: 4px;
  font-size: 0.78rem;
  font-weight: 800;
  color: var(--ink);
}

/* Speedometer & Drift */
.speed-card {
  position: fixed;
  bottom: 18px;
  right: 18px;
  background: var(--glass);
  backdrop-filter: blur(14px);
  border: 3px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 18px;
  box-shadow: var(--shadow);
  min-width: 205px;
}

.drift-bar-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.drift-label {
  font-size: 0.7rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
}

.drift-track {
  flex: 1;
  height: 9px;
  background: rgba(71, 54, 56, 0.14);
  border-radius: 999px;
  overflow: hidden;
}

.drift-fill {
  height: 100%;
  background: linear-gradient(90deg, #4fc3f7, #ff9800, #d500f9);
  border-radius: 999px;
  transition: width 0.06s linear;
}

.speed-readout {
  display: flex;
  align-items: baseline;
  justify-content: flex-end;
  gap: 5px;
}

.speed-num {
  font-size: 2.4rem;
  font-weight: 900;
  line-height: 1;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.speed-unit {
  font-size: 0.85rem;
  font-weight: 800;
  color: var(--ink-soft);
}

.key-mini-hints {
  margin-top: 8px;
  display: flex;
  justify-content: space-between;
  font-size: 0.7rem;
  color: var(--ink-soft);
  font-weight: 700;
}

kbd {
  background: #ffffff;
  border: 1.5px solid rgba(71, 54, 56, 0.22);
  border-radius: 5px;
  padding: 1px 5px;
  font-family: inherit;
  font-size: 0.68rem;
  font-weight: 800;
}

/* ===== 3. RESULTS OVERLAY ===== */
.results-overlay {
  position: fixed;
  inset: 0;
  z-index: 35;
  background: rgba(40, 28, 45, 0.42);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.results-card {
  width: min(460px, 100%);
  background: rgba(255, 252, 248, 0.95);
  border: 4px solid #ffffff;
  border-radius: 30px;
  padding: 26px 24px;
  box-shadow: 0 20px 50px rgba(45, 30, 50, 0.35);
  text-align: center;
}

.results-trophy {
  font-size: 3.4rem;
  line-height: 1;
}

.results-card h2 {
  font-size: 1.55rem;
  font-weight: 900;
  margin-top: 8px;
  color: #e64d7a;
}

.results-subtitle {
  font-size: 0.9rem;
  font-weight: 800;
  color: var(--ink-soft);
  margin-top: 4px;
}

.leaderboard {
  list-style: none;
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  gap: 7px;
  text-align: left;
}

.leaderboard li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  border-radius: 14px;
  background: #ffffff;
  border: 2px solid rgba(71, 54, 56, 0.08);
  font-weight: 800;
  font-size: 0.9rem;
}

.leaderboard li.is-player {
  background: #fff0f6;
  border-color: #ff80ab;
  color: #c73264;
}

.results-actions {
  margin-top: 18px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.res-btn {
  text-decoration: none;
  border: none;
  border-radius: 999px;
  padding: 10px 18px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 900;
  cursor: pointer;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.res-btn:hover {
  transform: translateY(-2px) scale(1.04);
}

.res-primary {
  background: linear-gradient(135deg, #ff6c96, #ff9a52);
  color: #ffffff;
}

.res-secondary {
  background: #fff1d6;
  color: var(--ink);
  border: 2px solid #f5be4f;
}

.res-ghost {
  background: #ffffff;
  color: var(--ink);
  border: 2px solid rgba(71, 54, 56, 0.16);
}

/* Mobile Touch Controls */
.touch-controls {
  display: none;
  position: fixed;
  bottom: 14px;
  left: 14px;
  right: 14px;
  z-index: 32;
  justify-content: space-between;
  align-items: flex-end;
  pointer-events: none;
}

.touch-pad-left,
.touch-pad-right {
  display: flex;
  gap: 10px;
  pointer-events: auto;
}

.t-btn {
  width: 58px;
  height: 58px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.92);
  background: rgba(255, 252, 248, 0.78);
  backdrop-filter: blur(8px);
  font-size: 1.35rem;
  font-weight: 900;
  color: var(--ink);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.18);
  touch-action: none;
}

.t-btn:active,
.t-btn.pressed {
  transform: scale(0.92);
  background: #ffe0ec;
}

.t-steer {
  width: 66px;
  height: 66px;
  font-size: 1.5rem;
}

.t-brake {
  width: 48px;
  height: 48px;
  font-size: 1.1rem;
  background: rgba(255, 235, 238, 0.85);
}

.t-drift {
  width: 64px;
  height: 64px;
  font-size: 1.45rem;
  background: linear-gradient(135deg, #6cc4f5, #8f9eff);
  color: #ffffff;
}

.t-item {
  width: 64px;
  height: 64px;
  font-size: 1.55rem;
  background: linear-gradient(135deg, #ffe066, #ffae42);
}

/* Ultra-clean Mobile Mode (works on BOTH portrait & landscape iPhone!) */
body.is-mobile .top-utility-bar { top: max(8px, env(safe-area-inset-top)); right: 8px; }
body.is-mobile .race-mobile-menu-btn {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; font-size: 1rem;
}
body.is-mobile .race-util-drawer {
  display: none;
  flex-direction: column;
  align-items: stretch;
  background: rgba(255, 252, 248, 0.96);
  backdrop-filter: blur(16px);
  border: 2px solid #ffffff;
  border-radius: 18px;
  padding: 8px;
  box-shadow: 0 12px 28px rgba(45, 30, 50, 0.25);
}
body.is-mobile .race-util-drawer.open { display: flex; }

/* Mobile Garage: ultra-slim bottom bar so 80% of screen clearly shows the 3D kart! */
body.is-mobile .garage-header { top: max(8px, env(safe-area-inset-top)); left: 8px; }
body.is-mobile .garage-title-pill { padding: 5px 11px; gap: 6px; border-radius: 999px; }
body.is-mobile .title-flag { font-size: 1.05rem; }
body.is-mobile .garage-title-pill h1 { font-size: 0.85rem; }
body.is-mobile .garage-title-pill p { display: none; }

body.is-mobile .garage-right-panel {
  right: 50%;
  top: auto;
  bottom: calc(54px + env(safe-area-inset-bottom));
  transform: translateX(50%);
  width: calc(100vw - 16px);
  max-width: 360px;
}
body.is-mobile .kart-spec-card {
  padding: 7px 10px;
  border-radius: 999px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: rgba(255, 252, 248, 0.86);
}
body.is-mobile .spec-char-row { gap: 7px; min-width: 0; }
body.is-mobile .spec-badge { width: 30px; height: 30px; font-size: 1rem; border-radius: 50%; }
body.is-mobile .spec-char-row h2 { font-size: 0.84rem; line-height: 1.1; }
body.is-mobile .spec-vehicle-name { font-size: 0.66rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 140px; }
body.is-mobile .spec-vehicle-desc,
body.is-mobile .stat-bars,
body.is-mobile .garage-controls-hint { display: none; }
body.is-mobile .start-race-btn {
  width: auto;
  margin-top: 0;
  padding: 8px 14px;
  font-size: 0.84rem;
  white-space: nowrap;
  flex-shrink: 0;
}

body.is-mobile .garage-char-dock {
  bottom: max(8px, env(safe-area-inset-bottom));
  width: calc(100vw - 14px);
  max-width: calc(100vw - 14px);
  flex-wrap: nowrap;
  justify-content: flex-start;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  padding: 4px 7px;
  border-radius: 999px;
  background: rgba(255, 252, 248, 0.8);
}
body.is-mobile .garage-char-dock::-webkit-scrollbar { display: none; }
body.is-mobile .garage-char-btn { flex-shrink: 0; padding: 3px 8px 3px 5px; font-size: 0.74rem; }
body.is-mobile .garage-avatar { width: 22px; height: 22px; font-size: 0.82rem; }

/* Slim In-Race Mobile HUD */
body.is-mobile .hud-rank-card {
  top: max(8px, env(safe-area-inset-top)); left: 8px;
  padding: 4px 10px; gap: 8px; border-radius: 999px;
  background: rgba(255, 252, 248, 0.78);
}
body.is-mobile #hud-rank-num { font-size: 1.35rem; }
body.is-mobile .rank-suffix,
body.is-mobile .rank-total { font-size: 0.74rem; }
body.is-mobile .lap-time-col { padding-left: 8px; }
body.is-mobile .hud-lap { font-size: 0.74rem; }
body.is-mobile .hud-timer { display: none; }
body.is-mobile .hud-item-wrap { top: max(8px, env(safe-area-inset-top)); }
body.is-mobile .item-slot { width: 42px; height: 42px; font-size: 1.35rem; border-radius: 12px; border-width: 2.5px; }
body.is-mobile .item-name-pill,
body.is-mobile .minimap-card,
body.is-mobile .speed-card { display: none; }
body.is-mobile .touch-controls { display: flex; bottom: max(12px, env(safe-area-inset-bottom)); left: 12px; right: 12px; }
body.is-mobile .t-btn { opacity: 0.82; }
body.is-mobile .t-steer { width: 58px; height: 58px; font-size: 1.3rem; }
body.is-mobile .t-drift,
body.is-mobile .t-item { width: 56px; height: 56px; font-size: 1.3rem; }
body.is-mobile .t-brake { width: 44px; height: 44px; font-size: 1rem; }
