:root {
  --road: #939bb5;
  --road-dark: #7b82a0;
  --platform: #c9cde4;
  --text-dark: #3d4a6c;
  --text-light: #ffffff;
  --accent-blue: #4a80f0;
  --accent-green: #52c144;
  --accent-yellow: #fbc400;
  --shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 8px 25px rgba(0, 0, 0, 0.15);
  --radius: 16px;
  --radius-lg: 24px;
  --cell: min(60px, calc((100vw - 40px) / 6));
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background-color: #84a0c8;
  font-family: "Arial Rounded MT Bold", "Helvetica Rounded", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  user-select: none;
}

#app {
  min-height: 100%;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 500px;
  margin: 0 auto;
  background: #84a0c8;
}

/* --- Top Bar --- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px;
  background: linear-gradient(180deg, #6c82b4, #5c70a4);
  border-radius: var(--radius-lg);
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: var(--shadow-lg), inset 0 1px 1px rgba(255, 255, 255, 0.2);
}

.btn-icon {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: linear-gradient(180deg, #5ca2f8, #4a80f0);
  border: 2px solid #3a6fd0;
  box-shadow: 0 4px 8px rgba(0,0,0,0.2), inset 0 -2px 2px rgba(0,0,0,0.2);
  color: white;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  padding: 8px;
}
.btn-icon svg { filter: drop-shadow(0 1px 1px rgba(0,0,0,0.3)); }

.level-display {
  background: rgba(0, 0, 0, 0.2);
  color: var(--text-light);
  font-weight: bold;
  font-size: 20px;
  padding: 10px 20px;
  border-radius: 99px;
  border: 2px solid rgba(0,0,0,0.2);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
  text-shadow: 0 1px 2px rgba(0,0,0,0.4);
}

.coins-display {
  display: flex;
  align-items: center;
  background: #4a5c8a;
  border-radius: 99px;
  padding: 4px;
  font-size: 18px;
  font-weight: bold;
  color: var(--text-light);
  border: 2px solid rgba(0,0,0,0.2);
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
}
.coin-icon {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--accent-yellow);
  margin-right: 8px;
  border: 2px solid #fdd835;
  box-shadow: inset 0 -2px 2px rgba(0,0,0,0.2);
}
.coins-display span { margin-right: 8px; }
.btn-icon-sm {
  width: 28px; height: 28px;
  border-radius: 50%;
  background: var(--accent-green);
  border: none;
  color: white;
  font-size: 20px;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

/* --- Game World --- */
.game-world {
  flex: 1;
  display: flex;
  flex-direction: column;
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-lg);
}

.station-area {
  background: var(--platform);
  padding-bottom: 30px;
  position: relative;
}
.station-bg {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 80px;
  background: #b4b9d7;
  border-bottom: 4px solid #a0a5c0;
}
.train {
  position: absolute;
  bottom: 0; left: 10px; right: 10px;
  height: 60px;
  background: linear-gradient(180deg, #e0e2f0, #c8cce0);
  border-radius: 12px 12px 0 0;
  border: 4px solid #a0a5c0;
  border-bottom: none;
}
.train::before { /* Windows */
  content: '';
  position: absolute;
  top: 10px; left: 10px; right: 10px;
  height: 30px;
  background: #84a0c8;
  border-radius: 8px;
}

.platform {
  position: relative;
  padding-top: 20px;
}

.queue-sign {
  position: absolute;
  top: 45px; left: 20px;
  width: 100px;
  height: 60px;
  background: #4a80f0;
  border-radius: 12px;
  border: 4px solid #3a6fd0;
  color: white;
  text-align: center;
  font-weight: bold;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}
.queue-sign::after { /* Pole */
  content: '';
  position: absolute;
  bottom: -24px; left: 50%;
  transform: translateX(-50%);
  width: 12px;
  height: 20px;
  background: #7b82a0;
}
.queue-count { font-size: 24px; line-height: 1.4; }
.queue-label { font-size: 14px; }

.queue {
  display: flex;
  gap: 4px;
  padding: 0 10px;
  margin-top: 80px;
  overflow-x: auto;
  height: 60px;
  align-items: flex-end;
}
.person {
  flex: 0 0 24px;
  width: 24px;
  height: 40px;
  border-radius: 12px 12px 0 0;
  position: relative;
  transform-origin: bottom center;
  transition: transform 0.2s;
}
.person.front {
  transform: scale(1.1);
}
.person::before { /* Head */
  content: '';
  position: absolute;
  top: -10px; left: 50%;
  transform: translateX(-50%);
  width: 20px; height: 20px;
  border-radius: 50%;
  background-color: inherit;
}

/* --- Road & Lot --- */
.road-area {
  background: var(--road);
  flex: 1;
  padding: 20px 10px;
  border-top: 4px solid var(--road-dark);
  display: flex;
  flex-direction: column;
}

.buffer-zone {
  height: calc(var(--cell) * 1.5);
  margin-bottom: 20px;
  display: flex;
  justify-content: center;
  gap: 10px;
  padding: 5px;
  background: rgba(0,0,0,0.1);
  border-radius: 12px;
  border: 2px dashed rgba(255,255,255,0.3);
}

.buffer-slot {
  width: calc(var(--cell) * 1.1);
  height: 100%;
  border-radius: 8px;
  background: rgba(255,255,255,0.1);
  position: relative;
}

.road {
  flex: 1;
  position: relative;
  /* overflow: hidden; Removed to allow cars to slide out visible if needed, though they vanish */
}

.lot {
  --cols: 6;
  --rows: 10;
  width: calc(var(--cols) * var(--cell));
  height: calc(var(--rows) * var(--cell));
  margin: 0 auto;
  position: relative;
  touch-action: manipulation;
}
.lot::before { /* Parking lines */
  content: "";
  position: absolute;
  inset: 0;
  background-image: repeating-linear-gradient(
    to right,
    transparent, transparent 20px,
    rgba(255,255,255,0.2) 20px, rgba(255,255,255,0.2) 22px
  );
  background-size: var(--cell) var(--cell);
  opacity: 0.5;
}

.v { /* Vehicle */
  position: absolute;
  left: calc(var(--x) * var(--cell));
  top: calc(var(--y) * var(--cell));
  width: calc(var(--w) * var(--cell));
  height: calc(var(--h) * var(--cell));
  border-radius: calc(var(--cell) * 0.25);
  border: 2px solid rgba(0,0,0,0.3);
  box-shadow: 
    0 6px 12px rgba(0,0,0,0.3), 
    inset 0 2px 2px rgba(255,255,255,0.4);
  transition: left 0.2s ease, top 0.2s ease, transform 0.15s ease;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

/* In Buffer Styling */
.v.in-buffer {
  position: relative;
  left: auto; top: auto;
  width: 100%; height: 100%;
  transform: none !important;
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);
}

.v.selected {
  transform: scale(1.05);
  box-shadow: 0 0 0 4px var(--accent-blue), 0 8px 20px rgba(0,0,0,0.4);
}
.v::before { /* Windshield */
  content: '';
  position: absolute;
  background: #a2d5f7;
  border: 2px solid rgba(255,255,255,0.5);
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.2);
}
.v[data-dir="up"]::before, .v[data-dir="down"]::before {
  width: 60%; height: 20%;
  left: 20%;
  border-radius: 8px;
}
.v[data-dir="up"]::before { top: 10%; }
.v[data-dir="down"]::before { bottom: 10%; }

.v[data-dir="left"]::before, .v[data-dir="right"]::before {
  width: 20%; height: 60%;
  top: 20%;
  border-radius: 8px;
}
.v[data-dir="left"]::before { left: 10%; }
.v[data-dir="right"]::before { right: 10%; }

.arrow {
  position: absolute;
  width: 28px; height: 28px;
  color: var(--text-dark);
  font-weight: bold;
  font-size: 24px;
  text-shadow: 0 1px 1px rgba(255,255,255,0.8);
}

/* --- Modals & Toasts --- */
.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 20px;
  background: rgba(0,0,0,0.7);
  color: white;
  padding: 12px 20px;
  border-radius: 99px;
  opacity: 0;
  transition: all 0.3s;
  pointer-events: none;
  z-index: 100;
}
.toast.show {
  opacity: 1;
  bottom: 40px;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.modal-backdrop.hidden { display: none; }

.modal {
  background: #eef2f9;
  border-radius: var(--radius-lg);
  padding: 20px;
  width: min(400px, 90vw);
  box-shadow: var(--shadow-lg);
  text-align: center;
}
.modal-title {
  font-size: 24px;
  font-weight: bold;
  color: var(--text-dark);
  margin-bottom: 16px;
}
.modal-body {
  background: white;
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 20px;
}
.stat-row {
  display: flex;
  justify-content: space-between;
  font-size: 18px;
  color: var(--text-dark);
}
.stat-row b { font-weight: bold; }

.btn {
  background: var(--accent-blue);
  color: white;
  border: none;
  padding: 14px 24px;
  border-radius: 99px;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 0 4px 8px rgba(0,0,0,0.2);
  transition: all 0.2s;
}
.btn:hover { transform: translateY(-2px); }

.shake {
  animation: shake 0.3s ease-in-out;
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}