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

:root {
  --sky: #aee3f7;
  --grass: #7fb069;
  --road: #5c5f66;
  --ink: #23303b;
  --card: #ffffff;
  --accent: #ff8c42;
  --accent-dark: #d96c22;
  --good: #3fa34d;
  --bad: #e2504a;
  --sign-bg: #fff8e7;
  --sign-border: #8a6d3b;
}

html, body {
  height: 100%;
  font-family: ui-rounded, "Comic Sans MS", "Trebuchet MS", system-ui, sans-serif;
  background: var(--sky);
  color: var(--ink);
  overflow: hidden;
}

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

.screen { display: none; flex: 1; min-height: 0; flex-direction: column; }
.screen.active { display: flex; }

.hidden { display: none !important; }

/* ---------- menus ---------- */

.menu-screen { align-items: center; padding: 24px 16px; overflow-y: auto; }

.logo { font-size: clamp(34px, 8vw, 56px); margin-top: 6vh; text-align: center; }
.tagline { font-size: clamp(15px, 3vw, 20px); margin: 12px 0 32px; text-align: center; }

.menu-buttons { display: flex; flex-direction: column; gap: 14px; width: min(360px, 92vw); }

.big-btn {
  min-height: 56px;
  border: 0;
  border-radius: 16px;
  background: var(--accent);
  color: #fff;
  font-family: inherit;
  font-size: 19px;
  font-weight: 700;
  cursor: pointer;
  padding: 10px 20px;
  box-shadow: 0 4px 0 var(--accent-dark);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
.big-btn small { font-size: 12px; font-weight: 400; opacity: 0.9; }
.big-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--accent-dark); }
.big-btn.ghost { background: #fff; color: var(--ink); box-shadow: 0 4px 0 #c9d6dd; }
.big-btn.danger { background: var(--bad); box-shadow: 0 4px 0 #a53531; }

.build {
  margin-top: 18px;
  font-size: 12px;
  color: #5c6b76;
  opacity: 0.8;
}

.menu-header {
  display: flex; align-items: center; gap: 14px;
  width: min(720px, 100%);
  margin-bottom: 18px;
}
.menu-header h2 { font-size: clamp(18px, 4vw, 26px); }

.back-btn {
  width: 48px; height: 48px;
  border: 0; border-radius: 14px;
  background: #fff; color: var(--ink);
  font-size: 20px; cursor: pointer;
  box-shadow: 0 3px 0 #c9d6dd;
}

.grid {
  display: grid;
  gap: 12px;
  width: min(720px, 100%);
}
.grid.worlds { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.grid.stages { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }

.tile {
  border: 0; border-radius: 16px;
  background: var(--card);
  font-family: inherit; color: var(--ink);
  cursor: pointer;
  padding: 14px 8px;
  min-height: 72px;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  box-shadow: 0 3px 0 #c9d6dd;
}
.tile:active { transform: translateY(2px); box-shadow: 0 1px 0 #c9d6dd; }
.tile.locked { opacity: 0.55; cursor: default; }
.tile-emoji { font-size: 30px; }
.tile-name { font-weight: 700; font-size: 15px; }
.tile-sub { font-size: 12px; color: #5c6b76; }
.tile-num { font-size: 24px; font-weight: 700; }
.tile-stars { font-size: 14px; color: #f0a500; }
.dim { color: #d3d9de; }

/* ---------- game HUD ---------- */

#hud {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px;
  background: rgba(255, 255, 255, 0.85);
  font-weight: 700;
  font-size: clamp(13px, 2.6vw, 17px);
}
#hud-label { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#hud-scores { white-space: pre; }

.hud-btn {
  min-width: 44px; min-height: 44px;
  border: 0; border-radius: 12px;
  background: #fff; box-shadow: 0 2px 0 #c9d6dd;
  font-size: 18px; cursor: pointer;
}

#progress-1p {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 14px;
  background: rgba(255, 255, 255, 0.6);
}
#progress-1p .track {
  position: relative; flex: 1; height: 26px;
  background: #dfe9ef; border-radius: 13px;
}
#progress-1p .marker {
  position: absolute; top: 50%; transform: translate(-50%, -50%);
  font-size: 18px;
  transition: left 0.4s ease;
  left: 0;
}
#marker-you { z-index: 2; }
.finish-flag { font-size: 18px; }

/* ---------- panels / road ---------- */

#panels { flex: 1; min-height: 0; display: flex; gap: 8px; padding: 8px; }
#panels.one .panel { max-width: 640px; margin: 0 auto; }
#panels.two { flex-direction: row; }
#panels .panel { flex: 1; min-width: 0; display: flex; flex-direction: column; }

@media (orientation: portrait) {
  #panels.two { flex-direction: column; }
}

.strip {
  background: #fff;
  border-radius: 12px;
  padding: 8px 12px;
  font-size: clamp(14px, 2.8vw, 19px);
  line-height: 1.5;
  margin-bottom: 6px;
  text-align: center;
}
.strip .w.blank {
  color: var(--accent-dark);
  font-weight: 700;
  letter-spacing: 2px;
}
.strip .w.blank.current {
  background: #ffe9b3;
  border-radius: 6px;
  padding: 1px 6px;
}

.road-wrap { flex: 1; min-height: 0; position: relative; touch-action: none; }

.road {
  position: absolute; inset: 0;
  background: var(--road);
  border-radius: 14px;
  overflow: hidden;
  border: 4px solid var(--grass);
}

/* moving lane dashes */
.road::before {
  content: "";
  position: absolute; inset: 0;
  background-image:
    repeating-linear-gradient(to bottom, transparent 0 26px, rgba(255,255,255,0.75) 26px 46px);
  background-size: 4px 72px;
  background-repeat: repeat-y;
  background-position: 33.3% 0;
  animation: roadmove 0.9s linear infinite;
}
.road.lanes-3::before {
  background-image:
    repeating-linear-gradient(to bottom, transparent 0 26px, rgba(255,255,255,0.75) 26px 46px),
    repeating-linear-gradient(to bottom, transparent 0 26px, rgba(255,255,255,0.75) 26px 46px);
  background-position: 33.3% 0, 66.6% 0;
}
.road.lanes-2::before {
  background-position: 50% 0;
}
.road.stalled::before { animation-play-state: paused; }
.road.zoom::before { animation-duration: 0.3s; }
.road.zoom .car { transform: translateX(-50%) scale(1.15); }
.road.turbo::before { animation-duration: 0.12s; }
.road.turbo .car { transform: translateX(-50%) scale(1.28); }
.road.turbo { border-color: var(--accent); }

@keyframes roadmove {
  from { background-position-y: 0; }
  to { background-position-y: 72px; }
}
.road.lanes-3.stalled::before, .road.lanes-3::before { background-position-x: 33.3%, 66.6%; }
.road.lanes-2.stalled::before, .road.lanes-2::before { background-position-x: 50%; }

.flash-good { animation: flashg 0.5s ease; }
.flash-bad { animation: flashb 0.5s ease; }
@keyframes flashg { 0% { box-shadow: inset 0 0 0 200px rgba(63,163,77,0.4); } 100% { box-shadow: none; } }
@keyframes flashb { 0% { box-shadow: inset 0 0 0 200px rgba(226,80,74,0.4); } 100% { box-shadow: none; } }

.gate {
  position: absolute; left: 0; right: 0; top: 0;
  display: flex;
  z-index: 2;
  will-change: transform;
}
.sign {
  flex: 1;
  display: flex; justify-content: center;
  padding: 0 4px;
}
.sign span {
  background: var(--sign-bg);
  border: 3px solid var(--sign-border);
  border-radius: 10px;
  padding: 8px 10px;
  font-weight: 800;
  font-size: clamp(14px, 3.4vw, 24px);
  letter-spacing: 1px;
  color: #3a3a3a;
  max-width: 100%;
  overflow-wrap: anywhere;
  text-align: center;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.25);
}

.car {
  position: absolute; bottom: 6%;
  transform: translateX(-50%);
  font-size: clamp(30px, 6vw, 44px);
  transition: left 0.18s ease;
  z-index: 3;
  filter: drop-shadow(0 3px 2px rgba(0,0,0,0.35));
}

.tapzones {
  position: absolute; inset: 0;
  display: flex;
  z-index: 4;
}
.tapzone { flex: 1; }

.banner {
  position: absolute; left: 50%; top: 38%;
  transform: translate(-50%, -50%);
  background: #fff;
  border-radius: 16px;
  padding: 14px 20px;
  text-align: center;
  z-index: 5;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);
  max-width: 92%;
}
.banner-title { font-size: 14px; font-weight: 700; color: var(--bad); margin-bottom: 6px; }
.banner-word span {
  display: inline-block;
  font-size: clamp(22px, 5vw, 34px);
  font-weight: 800;
  color: var(--good);
  margin: 0 2px;
  opacity: 0;
  animation: popletter 0.35s ease forwards;
}
@keyframes popletter {
  from { opacity: 0; transform: translateY(10px) scale(0.6); }
  to { opacity: 1; transform: none; }
}

.panel-footer {
  text-align: center;
  font-size: clamp(11px, 2vw, 13px);
  color: #45535e;
  padding: 4px 0 0;
}

#countdown {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: clamp(70px, 18vw, 140px);
  font-weight: 800;
  color: #fff;
  text-shadow: 0 4px 0 rgba(0, 0, 0, 0.35);
  z-index: 20;
  pointer-events: none;
}
#screen-game { position: relative; }

.fly-banner {
  position: absolute; left: 100%; top: 16%;
  white-space: nowrap; z-index: 25;
  background: #fff;
  border: 3px solid var(--accent);
  border-radius: 999px;
  padding: 10px 20px;
  font-weight: 800;
  font-size: clamp(14px, 3vw, 20px);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  animation: flyacross 6s forwards;
  pointer-events: none;
}
.fly-banner.crash { border-color: var(--bad); background: #ffe9e8; }

/* Glide in, hold centered long enough to read and gloat, then exit. */
@keyframes flyacross {
  0% {
    transform: translateX(0) scale(0.85) rotate(3deg); opacity: 0;
    animation-timing-function: cubic-bezier(0.16, 0.8, 0.3, 1);
  }
  6% { opacity: 1; }
  30% {
    transform: translateX(calc(-50vw - 50%)) scale(1) rotate(0deg);
    animation-timing-function: ease-in-out;
  }
  38% { transform: translateX(calc(-50vw - 50%)) scale(1.06) rotate(-1deg); }
  46% { transform: translateX(calc(-50vw - 50%)) scale(1) rotate(0deg); }
  72% {
    transform: translateX(calc(-50vw - 50%)) scale(1) rotate(0deg); opacity: 1;
    animation-timing-function: cubic-bezier(0.7, 0, 0.85, 0.4);
  }
  100% { transform: translateX(calc(-100vw - 110%)) scale(0.9) rotate(-3deg); opacity: 0.9; }
}

.confetti {
  position: absolute;
  z-index: 26;
  pointer-events: none;
  font-size: clamp(20px, 3.6vw, 30px);
  animation: confettifly var(--dur, 2.4s) cubic-bezier(0.15, 0.7, 0.4, 1) forwards;
  will-change: transform, opacity;
}
@keyframes confettifly {
  0% { transform: translate(0, 0) rotate(0deg) scale(0.3); opacity: 0; }
  10% { opacity: 1; }
  75% { opacity: 1; }
  100% {
    transform: translate(var(--dx), var(--dy)) rotate(var(--rot)) scale(1);
    opacity: 0;
  }
}

.stagechip {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 0.8em;
  font-weight: 800;
  margin-right: 6px;
  vertical-align: middle;
}
#countdown.pop { animation: cdpop 0.5s ease; }
@keyframes cdpop { from { transform: scale(1.7); opacity: 0.4; } to { transform: scale(1); opacity: 1; } }

/* ---------- overlays ---------- */

.overlay {
  position: fixed; inset: 0;
  background: rgba(35, 48, 59, 0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 30;
  padding: 16px;
}
.card {
  background: #fff;
  border-radius: 20px;
  padding: 24px;
  width: min(440px, 94vw);
  max-height: 90vh;
  overflow-y: auto;
  text-align: center;
}
.card h2 { font-size: clamp(19px, 4vw, 26px); margin-bottom: 10px; }
.stars { font-size: 40px; color: #f0a500; margin: 6px 0; }
.recap-sentence { font-size: clamp(15px, 3vw, 19px); margin: 10px 0; }
.recap-row { font-size: 14px; margin: 4px 0; color: #45535e; }
.card-buttons { display: flex; flex-direction: column; gap: 10px; margin-top: 18px; }
.setting-row {
  display: flex; align-items: center; gap: 12px;
  font-size: 17px; font-weight: 700;
  padding: 12px 6px;
  cursor: pointer;
}
.setting-row input { width: 22px; height: 22px; }

@media (prefers-reduced-motion: reduce) {
  .road::before { animation: none; }
  .car { transition: none; }
  .fly-banner {
    left: 50%;
    transform: translateX(-50%);
    animation: bannerfade 4s ease forwards;
  }
  .confetti { display: none; }
}
@keyframes bannerfade {
  0% { opacity: 0; }
  12% { opacity: 1; }
  80% { opacity: 1; }
  100% { opacity: 0; }
}
