/* Super Kart Rally — UI */
:root {
  --red: #e52521;
  --red-dark: #b81b18;
  --blue: #049cd8;
  --yellow: #fbd000;
  --green: #43b047;
  --purple: #7b2cbf;
  --ink: #1a1a2e;
  --cream: #fff8e7;
  --panel: rgba(255, 248, 231, 0.94);
  --shadow: 0 8px 0 rgba(0, 0, 0, 0.25);
  --radius: 18px;
  --font-display: "Bangers", system-ui, sans-serif;
  --font-body: "Nunito", system-ui, sans-serif;
}

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

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: var(--font-body);
  background: #0b1020;
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
  user-select: none;
}

#app {
  width: 100%;
  height: 100%;
  position: relative;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  overflow: hidden;
}

.screen.active {
  display: flex;
}

/* —— Title —— */
#screen-title {
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #4fc3f7 0%, #81d4fa 40%, #a5d6a7 70%, #66bb6a 100%);
}

.title-bg {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

.clouds {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 120px 50px at 15% 20%, rgba(255, 255, 255, 0.9) 0%, transparent 70%),
    radial-gradient(ellipse 160px 60px at 70% 15%, rgba(255, 255, 255, 0.85) 0%, transparent 70%),
    radial-gradient(ellipse 100px 40px at 45% 30%, rgba(255, 255, 255, 0.7) 0%, transparent 70%),
    radial-gradient(ellipse 140px 55px at 90% 25%, rgba(255, 255, 255, 0.8) 0%, transparent 70%);
  animation: drift-clouds 40s linear infinite;
}

.hills {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 35%;
  background:
    radial-gradient(ellipse 50% 100% at 20% 100%, #43a047 0%, transparent 55%),
    radial-gradient(ellipse 60% 120% at 70% 100%, #2e7d32 0%, transparent 55%),
    linear-gradient(180deg, transparent 0%, #388e3c 100%);
}

@keyframes drift-clouds {
  from { transform: translateX(0); }
  to { transform: translateX(-80px); }
}

.title-content {
  position: relative;
  z-index: 2;
  text-align: center;
  padding: 2rem;
  animation: pop-in 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes pop-in {
  from { opacity: 0; transform: scale(0.85) translateY(20px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.title-badge {
  display: inline-block;
  background: var(--yellow);
  color: var(--ink);
  font-weight: 900;
  font-size: 0.75rem;
  letter-spacing: 0.2em;
  padding: 0.35rem 0.9rem;
  border-radius: 999px;
  border: 3px solid var(--ink);
  margin-bottom: 0.75rem;
  box-shadow: 0 3px 0 var(--ink);
}

.logo {
  font-family: var(--font-display);
  line-height: 0.9;
  margin-bottom: 0.5rem;
  filter: drop-shadow(0 6px 0 rgba(0, 0, 0, 0.35));
}

.logo-super {
  display: block;
  font-size: clamp(1.5rem, 5vw, 2.4rem);
  color: var(--yellow);
  -webkit-text-stroke: 2px var(--ink);
  letter-spacing: 0.15em;
}

.logo-kart {
  display: block;
  font-size: clamp(4rem, 14vw, 7.5rem);
  color: var(--red);
  -webkit-text-stroke: 4px var(--ink);
  letter-spacing: 0.04em;
  text-shadow: 0 0 40px rgba(229, 37, 33, 0.5);
}

.logo-rally {
  display: block;
  font-size: clamp(1.8rem, 6vw, 3rem);
  color: var(--blue);
  -webkit-text-stroke: 2px var(--ink);
  letter-spacing: 0.2em;
}

.tagline {
  font-weight: 800;
  font-size: 1.05rem;
  color: var(--ink);
  opacity: 0.85;
  margin: 1rem 0 1.75rem;
}

.title-hints {
  margin-top: 1.5rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--ink);
  opacity: 0.75;
}

kbd {
  display: inline-block;
  background: var(--cream);
  border: 2px solid var(--ink);
  border-radius: 6px;
  padding: 0.1rem 0.4rem;
  font-family: var(--font-body);
  font-weight: 800;
  font-size: 0.75rem;
  box-shadow: 0 2px 0 var(--ink);
}

/* —— Buttons —— */
.btn {
  font-family: var(--font-body);
  font-weight: 900;
  font-size: 1.05rem;
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 0.75rem 1.75rem;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
  box-shadow: var(--shadow);
  color: var(--ink);
  background: var(--cream);
}

.btn:hover {
  transform: translateY(-2px);
  filter: brightness(1.05);
}

.btn:active {
  transform: translateY(3px);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
}

.btn-primary {
  background: linear-gradient(180deg, #ff5a56 0%, var(--red) 50%, var(--red-dark) 100%);
  color: #fff;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.25);
  font-size: 1.25rem;
  letter-spacing: 0.04em;
  min-width: 200px;
}

.btn-ghost {
  background: rgba(255, 255, 255, 0.7);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.15);
}

.btn-pulse {
  animation: pulse-btn 1.6s ease-in-out infinite;
}

@keyframes pulse-btn {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.04); }
}

/* —— Screen headers —— */
.screen-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  background: linear-gradient(180deg, #fff 0%, #f0e6d0 100%);
  border-bottom: 4px solid var(--ink);
  z-index: 5;
}

.screen-header h2 {
  font-family: var(--font-display);
  font-size: 1.75rem;
  letter-spacing: 0.06em;
  color: var(--red);
  -webkit-text-stroke: 1px var(--ink);
}

.header-spacer {
  width: 90px;
}

/* —— Character select —— */
#screen-character {
  background: linear-gradient(160deg, #1a237e 0%, #3949ab 40%, #5c6bc0 100%);
}

.char-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 1rem;
  padding: 1.25rem;
  overflow-y: auto;
  max-width: 960px;
  margin: 0 auto;
  width: 100%;
  align-content: start;
}

.char-card {
  background: var(--panel);
  border: 4px solid var(--ink);
  border-radius: var(--radius);
  padding: 1rem 0.75rem;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.2);
  position: relative;
  overflow: hidden;
}

.char-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 8px;
  background: var(--char-color, var(--red));
}

.char-card:hover {
  transform: translateY(-4px) scale(1.02);
}

.char-card.selected {
  border-color: var(--yellow);
  box-shadow: 0 0 0 4px var(--yellow), 0 8px 0 rgba(0, 0, 0, 0.25);
  transform: scale(1.04);
}

.char-avatar {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  margin: 0.5rem auto;
  border: 3px solid var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-size: 1.8rem;
  color: #fff;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
  background: var(--char-color, var(--red));
  box-shadow: inset 0 -8px 0 rgba(0, 0, 0, 0.15);
}

.char-card h3 {
  font-family: var(--font-display);
  font-size: 1.2rem;
  letter-spacing: 0.04em;
  margin-top: 0.25rem;
}

.char-card .role {
  font-size: 0.75rem;
  font-weight: 700;
  opacity: 0.65;
  margin-top: 0.15rem;
}

.char-detail {
  background: var(--panel);
  border-top: 4px solid var(--ink);
  padding: 1rem 1.5rem 1.25rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1.25rem;
  justify-content: center;
}

.stat-bars {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem 1.5rem;
  min-width: min(100%, 420px);
  flex: 1;
}

.stat {
  display: grid;
  grid-template-columns: 64px 1fr;
  align-items: center;
  gap: 0.5rem;
  font-weight: 800;
  font-size: 0.8rem;
}

.stat .bar {
  height: 12px;
  background: #ddd;
  border-radius: 999px;
  border: 2px solid var(--ink);
  overflow: hidden;
}

.stat .bar i {
  display: block;
  height: 100%;
  width: 50%;
  background: linear-gradient(90deg, var(--blue), var(--green));
  border-radius: 999px;
  transition: width 0.25s ease;
}

/* —— Track select —— */
#screen-track {
  background: linear-gradient(160deg, #004d40 0%, #00796b 45%, #26a69a 100%);
}

.track-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1.25rem;
  padding: 1.25rem;
  overflow-y: auto;
  max-width: 1100px;
  margin: 0 auto;
  width: 100%;
  align-content: start;
}

.track-card {
  background: var(--panel);
  border: 4px solid var(--ink);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  box-shadow: 0 8px 0 rgba(0, 0, 0, 0.25);
}

.track-card:hover {
  transform: translateY(-4px);
}

.track-card.selected {
  border-color: var(--yellow);
  box-shadow: 0 0 0 4px var(--yellow), 0 8px 0 rgba(0, 0, 0, 0.25);
}

.track-preview {
  height: 120px;
  position: relative;
  overflow: hidden;
}

.track-preview canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.track-info {
  padding: 0.85rem 1rem 1rem;
}

.track-info h3 {
  font-family: var(--font-display);
  font-size: 1.35rem;
  letter-spacing: 0.04em;
  margin-bottom: 0.25rem;
}

.track-meta {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.chip {
  font-size: 0.7rem;
  font-weight: 800;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  border: 2px solid var(--ink);
  background: var(--yellow);
}

.chip.hard { background: #ff8a80; }
.chip.med { background: #ffe082; }
.chip.easy { background: #a5d6a7; }

.track-footer {
  background: var(--panel);
  border-top: 4px solid var(--ink);
  padding: 1rem 1.5rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 1.5rem;
}

.race-options {
  display: flex;
  gap: 1.25rem;
  font-weight: 800;
}

.race-options label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.race-options select {
  font-family: var(--font-body);
  font-weight: 800;
  border: 3px solid var(--ink);
  border-radius: 10px;
  padding: 0.4rem 0.6rem;
  background: #fff;
  cursor: pointer;
}

/* —— Race HUD —— */
#screen-race {
  background: #000;
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: auto;
  outline: none;
}

.hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.hud.hidden,
.hidden {
  display: none !important;
}

.hud-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 1rem 1.25rem;
}

.hud-position {
  font-family: var(--font-display);
  font-size: 3.5rem;
  line-height: 1;
  color: #fff;
  text-shadow: 0 3px 0 #000, 0 0 20px rgba(0, 0, 0, 0.5);
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.45), transparent);
  padding: 0.25rem 0.75rem;
  border-radius: 12px;
}

.hud-position small {
  font-size: 1.4rem;
  margin-left: 0.1rem;
}

.hud-lap {
  font-family: var(--font-display);
  font-size: 1.5rem;
  color: #fff;
  letter-spacing: 0.08em;
  text-shadow: 0 2px 0 #000;
  background: rgba(0, 0, 0, 0.45);
  padding: 0.4rem 0.9rem;
  border-radius: 12px;
  border: 2px solid rgba(255, 255, 255, 0.25);
  margin-top: 0.35rem;
}

.hud-timer {
  font-family: var(--font-body);
  font-weight: 900;
  font-size: 1.25rem;
  color: #fff;
  text-shadow: 0 2px 0 #000;
  background: rgba(0, 0, 0, 0.5);
  padding: 0.45rem 0.85rem;
  border-radius: 10px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  font-variant-numeric: tabular-nums;
}

.hud-mid {
  position: absolute;
  top: 5.5rem;
  right: 1rem;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.75rem;
}

#minimap {
  width: 140px;
  height: 140px;
  border-radius: 14px;
  border: 3px solid rgba(255, 255, 255, 0.7);
  background: rgba(0, 0, 0, 0.55);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.item-slot {
  width: 88px;
  height: 88px;
  border-radius: 16px;
  border: 4px solid var(--ink);
  background: linear-gradient(160deg, #fff 0%, #e0e0e0 100%);
  box-shadow: 0 6px 0 rgba(0, 0, 0, 0.35), inset 0 0 20px rgba(251, 208, 0, 0.3);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.item-slot.has-item {
  animation: item-glow 0.8s ease-in-out infinite;
}

@keyframes item-glow {
  0%, 100% { box-shadow: 0 6px 0 rgba(0, 0, 0, 0.35), 0 0 12px rgba(251, 208, 0, 0.5); }
  50% { box-shadow: 0 6px 0 rgba(0, 0, 0, 0.35), 0 0 28px rgba(251, 208, 0, 0.9); }
}

.item-inner {
  width: 56px;
  height: 56px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-size: 1.6rem;
  color: #fff;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.3);
}

.item-hint {
  position: absolute;
  bottom: 4px;
  font-size: 0.55rem;
  font-weight: 900;
  opacity: 0.5;
  letter-spacing: 0.05em;
}

.hud-bottom {
  position: absolute;
  bottom: 1.25rem;
  left: 1.25rem;
  right: 1.25rem;
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

.speedo {
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
  background: rgba(0, 0, 0, 0.5);
  padding: 0.6rem 0.9rem;
  border-radius: 14px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  color: #fff;
}

.speed-bar {
  width: 120px;
  height: 14px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.25);
  margin-bottom: 0.2rem;
}

.speed-bar i {
  display: block;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--green), var(--yellow), var(--red));
  border-radius: 999px;
  transition: width 0.08s linear;
}

#speed-num {
  font-family: var(--font-display);
  font-size: 2rem;
  line-height: 1;
  min-width: 3ch;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.speedo small {
  font-weight: 800;
  opacity: 0.7;
  margin-bottom: 0.25rem;
}

.boost-meter {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: rgba(0, 0, 0, 0.5);
  padding: 0.55rem 0.85rem;
  border-radius: 12px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  font-weight: 900;
  font-size: 0.8rem;
}

.boost-pips {
  display: flex;
  gap: 4px;
}

.boost-pips i {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.15);
  border: 2px solid rgba(255, 255, 255, 0.3);
  transition: background 0.15s ease, box-shadow 0.15s ease;
}

.boost-pips i.on {
  background: var(--blue);
  box-shadow: 0 0 10px var(--blue);
}

.boost-pips i.on.orange {
  background: #ff9800;
  box-shadow: 0 0 10px #ff9800;
}

.boost-pips i.on.purple {
  background: #e040fb;
  box-shadow: 0 0 12px #e040fb;
}

.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-size: clamp(5rem, 20vw, 10rem);
  color: #fff;
  text-shadow: 0 6px 0 #000, 0 0 40px rgba(251, 208, 0, 0.8);
  z-index: 20;
  pointer-events: none;
}

.race-banner {
  position: absolute;
  top: 35%;
  left: 50%;
  transform: translateX(-50%);
  font-family: var(--font-display);
  font-size: clamp(2.5rem, 8vw, 4.5rem);
  color: var(--yellow);
  -webkit-text-stroke: 2px var(--ink);
  text-shadow: 0 4px 0 #000;
  z-index: 20;
  letter-spacing: 0.08em;
  white-space: nowrap;
  animation: banner-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes banner-in {
  from { opacity: 0; transform: translateX(-50%) scale(0.5); }
  to { opacity: 1; transform: translateX(-50%) scale(1); }
}

.standings {
  position: absolute;
  top: 5.5rem;
  left: 1rem;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 150px;
}

.standings .row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 8px;
  padding: 0.25rem 0.5rem;
  color: #fff;
  font-weight: 800;
  font-size: 0.75rem;
  border-left: 4px solid #888;
}

.standings .row.you {
  background: rgba(251, 208, 0, 0.35);
  border-left-color: var(--yellow);
}

.standings .row .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* —— Overlays / Results —— */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  pointer-events: auto;
  backdrop-filter: blur(4px);
}

.overlay-card {
  background: var(--panel);
  border: 4px solid var(--ink);
  border-radius: 20px;
  padding: 2rem;
  text-align: center;
  box-shadow: 0 12px 0 rgba(0, 0, 0, 0.3);
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  min-width: 280px;
}

.overlay-card h2 {
  font-family: var(--font-display);
  font-size: 2.2rem;
  color: var(--blue);
  -webkit-text-stroke: 1px var(--ink);
  margin-bottom: 0.5rem;
}

#screen-results {
  align-items: center;
  justify-content: center;
  background: #0d1b2a;
}

.results-bg {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(251, 208, 0, 0.25) 0%, transparent 50%),
    linear-gradient(180deg, #1a237e 0%, #0d1b2a 100%);
}

.results-panel {
  position: relative;
  z-index: 2;
  background: var(--panel);
  border: 4px solid var(--ink);
  border-radius: 24px;
  padding: 2rem 2.5rem;
  min-width: min(92vw, 440px);
  box-shadow: 0 14px 0 rgba(0, 0, 0, 0.35);
  animation: pop-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.results-panel h2 {
  font-family: var(--font-display);
  font-size: 2.8rem;
  text-align: center;
  color: var(--yellow);
  -webkit-text-stroke: 2px var(--ink);
  margin-bottom: 1.25rem;
  letter-spacing: 0.06em;
}

.results-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 1.5rem;
}

.results-list li {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.85rem;
  border-radius: 12px;
  border: 3px solid var(--ink);
  background: #fff;
  font-weight: 800;
}

.results-list li.you {
  background: #fff8c4;
  box-shadow: 0 0 0 3px var(--yellow);
}

.results-list .place {
  font-family: var(--font-display);
  font-size: 1.4rem;
  width: 2ch;
  color: var(--red);
}

.results-list .avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-family: var(--font-display);
  font-size: 0.9rem;
}

.results-list .time {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  opacity: 0.7;
  font-size: 0.9rem;
}

.results-actions {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

/* Mobile touch controls hint area */
@media (max-width: 640px) {
  .hud-position { font-size: 2.5rem; }
  #minimap { width: 100px; height: 100px; }
  .item-slot { width: 72px; height: 72px; }
  .standings { display: none; }
  .speed-bar { width: 80px; }
  .title-hints { font-size: 0.75rem; }
  .stat-bars { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  .btn-pulse,
  .clouds,
  .item-slot.has-item {
    animation: none;
  }
}
