:root {
  --grass: #3a7a4a;
  --grass-dark: #2d5a3d;
  --grass-light: #4a8f5c;
  --asphalt: #5a5f66;
  --asphalt-line: #6b7078;
  --lane: rgba(255, 220, 100, 0.22);
  --board-frame: #4a3b2a;
  --gold: #e8b923;
  --gold-dark: #c4920f;
  --gold-shine: #f5d76e;
  --text: #1a241c;
  --text-light: #f4f7f2;
  --panel: #f7f4ee;
  --panel-border: #d9d0c0;
  --accent: #2a7d9b;
  --accent-hover: #236a84;
  --danger: #c45c3e;
  --shadow: rgba(0, 0, 0, 0.28);
  --cell: min(14vmin, 72px);
  --gap: 3px;
  --radius: 10px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  font-family: "Nunito", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--grass-dark);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  user-select: none;
}

#app {
  height: 100%;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

.top-bar,
.bottom-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.65rem 1rem;
  background: linear-gradient(180deg, #2a5234 0%, var(--grass-dark) 100%);
  color: var(--text-light);
  z-index: 2;
}

.top-left {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
}

.brand {
  font-weight: 800;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
}

.level-label {
  font-size: 0.85rem;
  opacity: 0.85;
  font-weight: 600;
}

.timer {
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  font-size: 1.25rem;
  background: rgba(0, 0, 0, 0.2);
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
}

.bottom-bar {
  gap: 0.75rem;
  justify-content: center;
  padding: 0.75rem 1rem calc(0.75rem + var(--safe-bottom));
}

.btn {
  appearance: none;
  border: none;
  border-radius: 12px;
  font: inherit;
  font-weight: 800;
  font-size: 1rem;
  padding: 0.85rem 1.4rem;
  cursor: pointer;
  min-width: 8.5rem;
  transition: transform 0.12s ease, background 0.15s ease, box-shadow 0.15s ease;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.2);
}

.btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.2);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  flex: 1;
  max-width: 14rem;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: #e8e2d6;
  color: var(--text);
  flex: 1;
  max-width: 14rem;
}

.btn-secondary:hover {
  background: #ddd5c5;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
  flex: 1;
  max-width: 14rem;
}

.btn-danger:hover {
  background: #a84c34;
}

.board-wrap {
  position: relative;
  display: grid;
  place-items: center;
  overflow: hidden;
  background:
    radial-gradient(ellipse at 30% 20%, rgba(255, 255, 255, 0.06), transparent 50%),
    linear-gradient(160deg, var(--grass-light), var(--grass) 40%, var(--grass-dark));
}

.scape {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.trees {
  position: absolute;
  background-repeat: repeat-x;
  opacity: 0.9;
}

.trees-top,
.trees-bottom {
  left: 0;
  right: 0;
  height: 48px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='56' height='48' viewBox='0 0 56 48'%3E%3Cellipse cx='14' cy='28' rx='12' ry='14' fill='%232f6b3d'/%3E%3Cellipse cx='28' cy='22' rx='14' ry='18' fill='%23357a45'/%3E%3Cellipse cx='42' cy='30' rx='11' ry='13' fill='%232a6038'/%3E%3Crect x='25' y='34' width='6' height='14' fill='%235c4030'/%3E%3Crect x='11' y='36' width='5' height='12' fill='%23503828'/%3E%3Crect x='39' y='37' width='5' height='11' fill='%23503828'/%3E%3C/svg%3E");
  background-size: 56px 48px;
}

.trees-top { top: 4px; }
.trees-bottom { bottom: 4px; transform: scaleY(-1); }

.trees-left,
.trees-right {
  top: 0;
  bottom: 0;
  width: 40px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='40' height='64' viewBox='0 0 40 64'%3E%3Cellipse cx='20' cy='28' rx='14' ry='18' fill='%23357a45'/%3E%3Cellipse cx='12' cy='36' rx='10' ry='12' fill='%232f6b3d'/%3E%3Crect x='17' y='40' width='6' height='16' fill='%235c4030'/%3E%3C/svg%3E");
  background-size: 40px 64px;
  background-repeat: repeat-y;
}

.trees-left { left: 2px; }
.trees-right { right: 2px; }

.board {
  position: relative;
  width: calc(var(--cell) * 6 + var(--gap) * 7);
  height: calc(var(--cell) * 6 + var(--gap) * 7);
  background: linear-gradient(145deg, #5c4632, var(--board-frame));
  border-radius: 16px;
  box-shadow:
    0 12px 28px var(--shadow),
    inset 0 1px 0 rgba(255, 255, 255, 0.15);
  padding: var(--gap);
}

.grid {
  display: grid;
  grid-template-columns: repeat(6, var(--cell));
  grid-template-rows: repeat(6, var(--cell));
  gap: var(--gap);
  width: 100%;
  height: 100%;
}

.cell {
  background:
    linear-gradient(90deg, transparent 46%, var(--lane) 46%, var(--lane) 54%, transparent 54%),
    linear-gradient(0deg, transparent 46%, var(--lane) 46%, var(--lane) 54%, transparent 54%),
    linear-gradient(160deg, #676d75, var(--asphalt));
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}

.cell.exit-row {
  background:
    linear-gradient(90deg, transparent 0%, rgba(232, 185, 35, 0.12) 100%),
    linear-gradient(160deg, #676d75, var(--asphalt));
}

.exit {
  position: absolute;
  right: -10px;
  top: calc(var(--gap) + (var(--cell) + var(--gap)) * 2);
  width: 14px;
  height: var(--cell);
  background: linear-gradient(90deg, #c9a227, var(--gold));
  border-radius: 0 8px 8px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.25);
  z-index: 1;
}

.exit span {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-size: 0.55rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: #3a2a05;
}

.vehicles {
  position: absolute;
  inset: var(--gap);
  pointer-events: none;
}

.vehicle {
  --paint: #4a90d9;
  --paint-light: #7eb6f0;
  --paint-mid: #5a9ee0;
  --paint-dark: #3570b0;
  --paint-deep: #2a5a8f;
  --glass: #3a3f46;
  --glass-shine: rgba(200, 210, 220, 0.35);
  position: absolute;
  pointer-events: auto;
  cursor: grab;
  touch-action: none;
  transition: filter 0.15s ease, box-shadow 0.15s ease;
  z-index: 2;
  padding: 0;
}

.vehicle .v-body {
  position: relative;
  width: 100%;
  height: 100%;
  background:
    radial-gradient(ellipse 80% 55% at 50% 35%, var(--paint-light), transparent 55%),
    linear-gradient(160deg, var(--paint-mid), var(--paint) 40%, var(--paint-dark));
  box-shadow:
    0 4px 10px rgba(0, 0, 0, 0.4),
    inset 0 2px 0 rgba(255, 255, 255, 0.28),
    inset 0 -3px 0 rgba(0, 0, 0, 0.12);
  overflow: visible;
}

/* Soft rounded corners — still fills the cell footprint */
.vehicle.car.H .v-body,
.vehicle.car.V .v-body {
  border-radius: 14px;
}

.vehicle.truck.H .v-body,
.vehicle.truck.V .v-body {
  border-radius: 12px;
}

/* Hood / roof highlights */
.vehicle .v-hood {
  position: absolute;
  border-radius: 40%;
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.22),
    transparent 70%
  );
  pointer-events: none;
}

.vehicle.H .v-hood {
  top: 12%;
  bottom: 12%;
  right: 8%;
  width: 22%;
}

.vehicle.V .v-hood {
  left: 12%;
  right: 12%;
  bottom: 8%;
  height: 22%;
}

.vehicle .v-trunk {
  position: absolute;
  border-radius: 30%;
  background: linear-gradient(
    180deg,
    rgba(255, 255, 255, 0.12),
    transparent
  );
  pointer-events: none;
}

.vehicle.car.H .v-trunk {
  top: 18%;
  bottom: 18%;
  left: 8%;
  width: 16%;
}

.vehicle.car.V .v-trunk {
  left: 18%;
  right: 18%;
  top: 8%;
  height: 16%;
}

/* Windshield — dark trapezoid with shine */
.vehicle .v-windshield {
  position: absolute;
  background:
    linear-gradient(
      135deg,
      var(--glass-shine) 0 18%,
      transparent 19% 40%,
      var(--glass-shine) 41% 48%,
      transparent 49%
    ),
    linear-gradient(160deg, #5a616a, var(--glass));
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.15);
  pointer-events: none;
}

.vehicle.H .v-windshield {
  top: 16%;
  bottom: 16%;
  right: 26%;
  width: 16%;
  border-radius: 4px 10px 10px 4px;
  clip-path: polygon(18% 0, 100% 8%, 100% 92%, 18% 100%, 0 78%, 0 22%);
}

.vehicle.V .v-windshield {
  left: 16%;
  right: 16%;
  bottom: 26%;
  height: 16%;
  border-radius: 4px 4px 10px 10px;
  clip-path: polygon(8% 0, 92% 0, 100% 18%, 78% 100%, 22% 100%, 0 18%);
}

.vehicle .v-rear-glass {
  position: absolute;
  background:
    linear-gradient(
      135deg,
      var(--glass-shine) 0 20%,
      transparent 21%
    ),
    linear-gradient(160deg, #4e555c, var(--glass));
  pointer-events: none;
}

.vehicle.car.H .v-rear-glass {
  top: 20%;
  bottom: 20%;
  left: 22%;
  width: 11%;
  border-radius: 3px;
  clip-path: polygon(0 10%, 100% 0, 100% 100%, 0 90%);
}

.vehicle.car.V .v-rear-glass {
  left: 20%;
  right: 20%;
  top: 22%;
  height: 11%;
  border-radius: 3px;
  clip-path: polygon(0 0, 100% 0, 90% 100%, 10% 100%);
}

/* Side windows */
.vehicle .v-side {
  position: absolute;
  background: var(--glass);
  opacity: 0.92;
  border-radius: 2px;
  pointer-events: none;
}

.vehicle.H .v-side {
  height: 9%;
  width: 11%;
}

.vehicle.H .v-side.s1 { top: 7%; right: 40%; }
.vehicle.H .v-side.s2 { top: 7%; right: 54%; }
.vehicle.H .v-side.s3 { bottom: 7%; right: 40%; }
.vehicle.H .v-side.s4 { bottom: 7%; right: 54%; }

.vehicle.V .v-side {
  width: 9%;
  height: 11%;
}

.vehicle.V .v-side.s1 { left: 7%; bottom: 40%; }
.vehicle.V .v-side.s2 { left: 7%; bottom: 54%; }
.vehicle.V .v-side.s3 { right: 7%; bottom: 40%; }
.vehicle.V .v-side.s4 { right: 7%; bottom: 54%; }

.vehicle.truck.H .v-side.s2,
.vehicle.truck.H .v-side.s4,
.vehicle.truck.V .v-side.s2,
.vehicle.truck.V .v-side.s4 {
  display: none;
}

.vehicle.truck.H .v-side.s1 { right: 34%; }
.vehicle.truck.H .v-side.s3 { right: 34%; }
.vehicle.truck.V .v-side.s1 { bottom: 34%; }
.vehicle.truck.V .v-side.s3 { bottom: 34%; }

/* Truck bed */
.vehicle .v-bed {
  position: absolute;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.08), transparent 40%),
    rgba(0, 0, 0, 0.16);
  border: 1.5px solid rgba(0, 0, 0, 0.18);
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06);
  pointer-events: none;
}

.vehicle.truck.H .v-bed {
  top: 14%;
  bottom: 14%;
  left: 10%;
  right: 48%;
}

.vehicle.truck.V .v-bed {
  left: 14%;
  right: 14%;
  top: 10%;
  bottom: 48%;
}

.vehicle .v-cab-split {
  position: absolute;
  background: rgba(0, 0, 0, 0.2);
  pointer-events: none;
}

.vehicle.truck.H .v-cab-split {
  top: 12%;
  bottom: 12%;
  right: 44%;
  width: 2px;
}

.vehicle.truck.V .v-cab-split {
  left: 12%;
  right: 12%;
  bottom: 44%;
  height: 2px;
}

/* Side mirrors */
.vehicle .v-mirror {
  position: absolute;
  background: var(--paint-dark);
  border-radius: 40%;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  z-index: 2;
}

.vehicle.H .v-mirror {
  width: 6%;
  height: 10%;
  right: 38%;
}

.vehicle.H .v-mirror.m1 { top: 1%; }
.vehicle.H .v-mirror.m2 { bottom: 1%; }

.vehicle.V .v-mirror {
  width: 10%;
  height: 6%;
  bottom: 38%;
}

.vehicle.V .v-mirror.m1 { left: 1%; }
.vehicle.V .v-mirror.m2 { right: 1%; }

/* Headlights — pale with orange signal tip */
.vehicle .v-hl {
  position: absolute;
  background: linear-gradient(145deg, #f4f1ea, #d8d4cc);
  pointer-events: none;
  z-index: 2;
  overflow: hidden;
}

.vehicle .v-hl i {
  position: absolute;
  background: #e89030;
  border-radius: 40%;
}

.vehicle.H .v-hl {
  width: 11%;
  height: 22%;
  right: 2%;
  border-radius: 55% 40% 40% 55%;
}

.vehicle.H .v-hl.a { top: 10%; }
.vehicle.H .v-hl.b { bottom: 10%; }

.vehicle.H .v-hl i {
  width: 38%;
  height: 55%;
  right: 4%;
  top: 22%;
}

.vehicle.V .v-hl {
  width: 22%;
  height: 11%;
  bottom: 2%;
  border-radius: 40% 40% 55% 55%;
}

.vehicle.V .v-hl.a { left: 10%; }
.vehicle.V .v-hl.b { right: 10%; }

.vehicle.V .v-hl i {
  width: 55%;
  height: 38%;
  bottom: 4%;
  left: 22%;
}

/* Taillights — bold red, curved to body */
.vehicle .v-tl {
  position: absolute;
  background: linear-gradient(145deg, #e85555, #c62828 60%, #9a1c1c);
  box-shadow: 0 0 4px rgba(180, 40, 40, 0.35);
  pointer-events: none;
  z-index: 2;
}

.vehicle.H .v-tl {
  width: 10%;
  height: 24%;
  left: 2%;
  border-radius: 40% 55% 55% 40%;
}

.vehicle.H .v-tl.a { top: 10%; }
.vehicle.H .v-tl.b { bottom: 10%; }

.vehicle.V .v-tl {
  width: 24%;
  height: 10%;
  top: 2%;
  border-radius: 55% 55% 40% 40%;
}

.vehicle.V .v-tl.a { left: 10%; }
.vehicle.V .v-tl.b { right: 10%; }

/* Gold hero */
.vehicle.gold {
  z-index: 3;
}

.vehicle.gold .v-body {
  box-shadow:
    0 4px 14px rgba(200, 150, 20, 0.55),
    inset 0 2px 0 rgba(255, 255, 255, 0.4),
    inset 0 -3px 0 rgba(0, 0, 0, 0.12);
}

.vehicle.gold .v-hl {
  background: linear-gradient(145deg, #fffdf5, #f0e6b8);
  box-shadow: 0 0 6px rgba(255, 220, 100, 0.55);
}

.vehicle.gold .v-mirror {
  background: var(--paint-deep);
}

.vehicle.selected {
  filter: brightness(1.06);
  z-index: 4;
}

.vehicle.selected .v-body {
  outline: 3px solid #fff;
  outline-offset: 2px;
}

.vehicle.dragging {
  cursor: grabbing;
  z-index: 5;
  transition: none;
}

.vehicle.escaped {
  z-index: 4;
  animation: escaped-glow 0.9s ease infinite;
}

.vehicle.escaped .v-body {
  outline: 3px solid #fff8dc;
  outline-offset: 2px;
}

@keyframes escaped-glow {
  50% {
    filter: brightness(1.15);
  }
}

.vehicle.hint-target {
  animation: pulse 0.9s ease infinite;
}

@keyframes pulse {
  50% { filter: brightness(1.15); }
}

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 1rem;
  padding-bottom: calc(1rem + var(--safe-bottom));
}

.modal[hidden] {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(12, 24, 16, 0.55);
  backdrop-filter: blur(3px);
}

.modal-panel {
  position: relative;
  width: min(100%, 420px);
  max-height: min(88dvh, 640px);
  overflow: auto;
  background: var(--panel);
  border-radius: 18px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.35);
  border: 1px solid var(--panel-border);
  padding: 1rem 1.1rem 1.25rem;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.modal-header h2,
.win-panel h2 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
}

.icon-btn {
  appearance: none;
  border: none;
  background: transparent;
  font-size: 1.75rem;
  line-height: 1;
  cursor: pointer;
  color: #666;
  padding: 0.2rem 0.45rem;
  border-radius: 8px;
}

.icon-btn:hover {
  background: rgba(0, 0, 0, 0.06);
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.55rem;
}

.level-btn {
  appearance: none;
  border: 1.5px solid var(--panel-border);
  background: #fff;
  border-radius: 12px;
  padding: 0.65rem 0.35rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  min-height: 3.4rem;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.level-btn:hover {
  border-color: var(--accent);
  background: #f0f8fb;
}

.level-btn.current {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(42, 125, 155, 0.25);
}

.level-btn .num {
  font-size: 1.05rem;
  font-weight: 800;
}

.level-btn .meta {
  font-size: 0.65rem;
  font-weight: 700;
  color: #6a7368;
  min-height: 0.9rem;
}

.level-btn .tick {
  color: #2e8b57;
  font-weight: 800;
}

.procedural-section {
  margin-top: 1.1rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--panel-border);
}

.procedural-section h3 {
  margin: 0 0 0.6rem;
  font-size: 0.95rem;
  font-weight: 800;
  color: #4a554c;
}

.procedural-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.55rem;
}

.proc-btn {
  appearance: none;
  border: 1.5px solid var(--panel-border);
  background: #fff;
  border-radius: 12px;
  padding: 0.7rem 0.4rem;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
}

.proc-btn:hover {
  border-color: var(--accent);
}

.proc-btn.easy { border-bottom: 3px solid #5aaa6a; }
.proc-btn.medium { border-bottom: 3px solid #d4a017; }
.proc-btn.hard { border-bottom: 3px solid var(--danger); }

.proc-btn .meta {
  font-size: 0.65rem;
  font-weight: 700;
  color: #6a7368;
}

.win-panel {
  text-align: center;
  overflow: hidden;
  width: min(100%, 340px);
}

.confirm-panel {
  text-align: center;
  width: min(100%, 340px);
}

.confirm-panel h2 {
  margin: 0 0 0.4rem;
  font-size: 1.35rem;
  font-weight: 800;
}

.confirm-copy {
  margin: 0 0 1rem;
  color: #4a554c;
  font-weight: 600;
  font-size: 0.95rem;
  line-height: 1.4;
}

.win-panel h2 {
  font-size: 1.75rem;
  color: var(--gold-dark);
  margin-bottom: 0.35rem;
}

.win-time {
  margin: 0.4rem 0;
  font-size: 1.1rem;
}

.win-best {
  margin: 0.25rem 0 0.85rem;
  color: #2e8b57;
  font-weight: 800;
}

.win-actions {
  display: flex;
  gap: 0.6rem;
  justify-content: center;
  margin-top: 0.9rem;
}

.win-actions .btn {
  min-width: 0;
  flex: 1;
}

.confetti {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 20% 30%, #e8b923 0 4px, transparent 5px),
    radial-gradient(circle at 70% 20%, #2a7d9b 0 5px, transparent 6px),
    radial-gradient(circle at 40% 70%, #c45c3e 0 4px, transparent 5px),
    radial-gradient(circle at 85% 55%, #5aaa6a 0 5px, transparent 6px),
    radial-gradient(circle at 15% 80%, #f5d76e 0 3px, transparent 4px);
  opacity: 0.85;
  animation: confetti-fall 1.2s ease-out;
}

@keyframes confetti-fall {
  from { transform: translateY(-12px); opacity: 0; }
  to { transform: translateY(0); opacity: 0.85; }
}

@media (min-width: 720px) {
  :root {
    --cell: min(12vmin, 84px);
  }

  .level-grid {
    grid-template-columns: repeat(5, 1fr);
  }
}

@media (hover: hover) and (pointer: fine) {
  .vehicle {
    cursor: grab;
  }

  .vehicle.dragging {
    cursor: grabbing;
  }
}
