* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button,
.ctrl,
.cta,
.units-toggle button {
  touch-action: manipulation;
}

.screen {
  position: absolute;
  inset: 0;
}

.hidden {
  display: none !important;
}

/* ---------- Home screen ---------- */

#home {
  display: flex;
  overflow-y: auto;
  background: linear-gradient(160deg, #0f172a 0%, #1e3a8a 60%, #2563eb 100%);
  color: #fff;
}

.home-inner {
  width: 100%;
  max-width: 720px;
  margin: auto;
  padding: 32px;
  padding-top: calc(32px + env(safe-area-inset-top));
  padding-bottom: calc(32px + env(safe-area-inset-bottom));
  text-align: center;
}

.home-inner h1 {
  margin: 0 0 8px;
  font-size: 42px;
  letter-spacing: -0.5px;
}

.tagline {
  margin: 0 0 30px;
  color: #cbd5e1;
}

.mode-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
}

.mode-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 20px;
  text-align: left;
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.08);
  cursor: pointer;
  transition: transform 0.12s ease, background 0.12s ease;
}

.mode-card:hover {
  background: rgba(255, 255, 255, 0.16);
  transform: translateY(-2px);
}

.mode-card.disabled {
  opacity: 0.55;
  cursor: default;
}

.mode-card.disabled:hover {
  background: rgba(255, 255, 255, 0.08);
  transform: none;
}

.mode-title {
  font-size: 18px;
  font-weight: 700;
}

.mode-desc {
  font-size: 13px;
  line-height: 1.4;
  color: #cbd5e1;
}

.settings {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 26px;
  font-size: 13px;
  color: #cbd5e1;
}

/* ---------- Garage (car customization) ---------- */

.garage {
  margin-top: 24px;
  text-align: left;
}

.garage-head {
  margin-bottom: 8px;
  font-size: 12px;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: #93c5fd;
}

.garage-body {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.06);
}

.car-preview {
  flex: 0 0 auto;
  width: 200px;
  height: 120px;
  border-radius: 10px;
  background: linear-gradient(160deg, #1e293b, #0f172a);
}

.garage-controls {
  flex: 1;
  min-width: 0;
}

.garage .field {
  margin: 0 0 14px;
  color: #cbd5e1;
}

.garage .field:last-child {
  margin-bottom: 0;
}

.pill-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.pill-group button {
  padding: 6px 12px;
  font-size: 12px;
  color: #e2e8f0;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  cursor: pointer;
}

.pill-group button.active {
  color: #fff;
  border-color: #2563eb;
  background: #2563eb;
}

.swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 6px;
}

.swatch {
  width: 26px;
  height: 26px;
  padding: 0;
  border: 2px solid rgba(255, 255, 255, 0.35);
  border-radius: 50%;
  cursor: pointer;
}

.swatch.active {
  border-color: #fff;
  box-shadow: 0 0 0 2px #2563eb;
}

/* ---------- Free draw screen ---------- */

#map {
  position: absolute;
  inset: 0;
}

.panel {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 2;
  width: 250px;
  padding: 18px;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
}

.panel h1 {
  margin: 0 0 10px;
  font-size: 17px;
}

.hint {
  margin: 0 0 12px;
  font-size: 12.5px;
  line-height: 1.4;
  color: #555;
}

.field {
  display: block;
  margin: 10px 0;
  font-size: 13px;
  color: #333;
}

.search {
  position: relative;
  margin-top: 6px;
}

.search input {
  width: 100%;
  box-sizing: border-box;
  padding: 8px 10px;
  font-size: 13px;
  color: #111;
  border: 1px solid #ccc;
  border-radius: 7px;
  background: #fff;
}

.search input:focus {
  outline: none;
  border-color: #2563eb;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.25);
}

.search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 5;
  max-height: 220px;
  overflow-y: auto;
  border: 1px solid #ccc;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.18);
}

.search-results.hidden { display: none; }

.search-results button {
  display: block;
  width: 100%;
  margin: 0;
  padding: 8px 10px;
  text-align: left;
  font-size: 12.5px;
  line-height: 1.35;
  color: #222;
  border: 0;
  border-bottom: 1px solid #eee;
  border-radius: 0;
  background: #fff;
  cursor: pointer;
}

.search-results button:last-child { border-bottom: 0; }
.search-results button:hover { background: #eef2ff; }

.search-results .empty {
  padding: 8px 10px;
  font-size: 12px;
  color: #888;
}

input[type="color"] {
  height: 38px;
  width: 66px;
  padding: 2px;
  margin-left: 6px;
  vertical-align: middle;
  border: 1px solid #ccc;
  border-radius: 7px;
  background: none;
  cursor: pointer;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 10px 0;
}

.panel button {
  padding: 8px 10px;
  font-size: 13px;
  border: 1px solid #ccc;
  border-radius: 7px;
  background: #f7f7f7;
  cursor: pointer;
}

.panel button:hover {
  background: #eee;
}

.actions button {
  flex: 1 1 auto;
}

.panel button.back {
  margin-bottom: 12px;
  padding: 6px 10px;
}

.status {
  margin: 12px 0 6px;
  font-size: 12px;
  color: #555;
  min-height: 15px;
}

.credit {
  display: block;
  font-size: 11px;
  line-height: 1.4;
  color: #999;
}

button.back {
  padding: 6px 10px;
  font-size: 12px;
  border: 1px solid #ccc;
  border-radius: 7px;
  background: #f7f7f7;
  cursor: pointer;
}

button.back:hover { background: #eee; }

.cta {
  display: block;
  width: 100%;
  margin-top: 10px;
  padding: 10px;
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  border: 0;
  border-radius: 8px;
  background: #2563eb;
  cursor: pointer;
}

.cta:hover { background: #1d4ed8; }

.cta:disabled {
  background: #cbd5e1;
  cursor: not-allowed;
}

.panel button.cta {
  color: #fff;
  border: 0;
  background: #2563eb;
}

.panel button.cta:hover { background: #1d4ed8; }

.panel button.cta:disabled { background: #cbd5e1; }

.panel button.cta.ghost {
  color: #0f172a;
  background: #e2e8f0;
}

.panel button.cta.ghost:hover { background: #cbd5e1; }

.panel button.cta.ghost:disabled {
  color: #94a3b8;
  background: #e2e8f0;
}

/* ---------- Free drive entry ---------- */

#free-drive {
  display: flex;
  overflow-y: auto;
  background: linear-gradient(160deg, #0f172a 0%, #1e3a8a 60%, #2563eb 100%);
}

#free-drive .panel {
  position: static;
  width: 340px;
  max-width: calc(100% - 32px);
  margin: auto;
  padding-top: calc(18px + env(safe-area-inset-top));
  padding-bottom: calc(18px + env(safe-area-inset-bottom));
}

/* Free roam has no route, so hide the progress UI. */
#drive3d.free .bar,
#drive3d.free .stat.small {
  display: none;
}

/* ---------- Drive screen ---------- */

#drive-map {
  position: absolute;
  inset: 0;
}

#drive3d {
  background: #9fc4e8;
}

#drive3d-canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
}

.drive-hud {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 2;
  width: 200px;
  padding: 14px;
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.2);
}

.drive-hud .stat {
  font-size: 26px;
  font-weight: 700;
  line-height: 1.1;
}

.drive-hud .stat small {
  margin-left: 4px;
  font-size: 12px;
  font-weight: 400;
  color: #888;
}

.drive-hud .stat.small {
  margin-top: 4px;
  font-size: 12px;
  color: #555;
  font-weight: 400;
}

.bar {
  height: 8px;
  margin: 8px 0;
  border-radius: 4px;
  background: #eee;
  overflow: hidden;
}

.bar > div {
  width: 0;
  height: 100%;
  background: #2563eb;
  transition: width 0.1s linear;
}

.warning,
.done {
  margin-top: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
}

.warning {
  background: #fee2e2;
  color: #b91c1c;
}

.warning.water {
  background: #dbeafe;
  color: #1d4ed8;
}

.done {
  background: #dcfce7;
  color: #15803d;
}

.drive-overlay {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
  color: #fff;
  background: rgba(15, 23, 42, 0.72);
}

.drive-overlay p {
  margin: 0;
  font-size: 16px;
}

.drive-overlay .cta {
  width: auto;
  padding: 10px 18px;
}

.drive-controls {
  position: absolute;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  padding: 16px;
  pointer-events: none;
}

.pad {
  display: flex;
  gap: 12px;
}

.ctrl {
  width: 68px;
  height: 68px;
  font-size: 20px;
  color: #1e293b;
  border: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.25);
  cursor: pointer;
  pointer-events: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.ctrl:active {
  color: #fff;
  background: #2563eb;
}

.car {
  width: 26px;
  height: 52px;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.45));
}

.car svg {
  display: block;
}

.end-card {
  width: min(90vw, 320px);
  padding: 26px 28px;
  border-radius: 16px;
  text-align: center;
  color: #0f172a;
  background: #fff;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

.end-card h2 {
  margin: 0 0 8px;
  font-size: 24px;
}

.end-card p {
  margin: 0 0 18px;
  font-size: 15px;
  color: #475569;
}

.cta.ghost {
  color: #0f172a;
  background: #e2e8f0;
}

.cta.ghost:hover {
  background: #cbd5e1;
}

.stop {
  width: 16px;
  height: 16px;
  border: 3px solid #fff;
  border-radius: 50%;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}

.stop.start { background: #16a34a; }
.stop.end { background: #dc2626; }

/* ---------- Units toggle ---------- */

.units-toggle {
  display: inline-flex;
  overflow: hidden;
  border: 1px solid #ccc;
  border-radius: 8px;
}

.units-toggle button {
  padding: 6px 10px;
  font-size: 12px;
  color: #334155;
  background: #f1f5f9;
  border: 0;
  border-radius: 0;
  cursor: pointer;
}

.units-toggle button + button {
  border-left: 1px solid #ccc;
}

.units-toggle button.active {
  color: #fff;
  background: #2563eb;
}

.units-toggle.compact {
  margin: 10px 0;
}

.units-toggle.compact button {
  flex: 1;
}

#home .units-toggle {
  border-color: rgba(255, 255, 255, 0.3);
}

#home .units-toggle button {
  color: #e2e8f0;
  background: rgba(255, 255, 255, 0.1);
}

#home .units-toggle button + button {
  border-left-color: rgba(255, 255, 255, 0.3);
}

#home .units-toggle button.active {
  color: #fff;
  background: #2563eb;
}

/* ---------- Small screens ---------- */

@media (max-width: 640px) {
  .home-inner {
    padding: 22px 16px calc(22px + env(safe-area-inset-bottom));
  }

  .home-inner h1 { font-size: 30px; }
  .tagline { margin-bottom: 18px; }
  .mode-grid { grid-template-columns: 1fr; gap: 12px; }
  .mode-card { padding: 16px; }
  .settings { margin-top: 18px; }

  .garage-body { flex-direction: column; align-items: stretch; gap: 12px; }
  .car-preview { width: 100%; height: 140px; }

  /* Free draw: dock the panel to the bottom as a scrollable sheet so the map
     fills the screen above it (one-handed, thumb reachable). */
  #free-draw .panel {
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    max-height: 56vh;
    padding: 14px 16px calc(14px + env(safe-area-inset-bottom));
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -8px 26px rgba(0, 0, 0, 0.28);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #free-draw .panel h1 { font-size: 15px; }
  #free-draw .hint { display: none; }
  /* Results push the sheet instead of overlapping and getting clipped. */
  #free-draw .search-results { position: static; margin-top: 4px; max-height: 38vh; }

  /* Drive HUD stays top-left but compact; clear of the notch. */
  .drive-hud {
    top: calc(12px + env(safe-area-inset-top));
    left: calc(12px + env(safe-area-inset-left));
    width: 168px;
    padding: 12px;
    border-radius: 12px;
  }

  .drive-hud .stat { font-size: 22px; }
  .drive-hud button.back { margin-bottom: 8px; }

  /* Touch controls clear of the home indicator and side edges. */
  .drive-controls {
    padding:
      12px
      calc(12px + env(safe-area-inset-right))
      calc(14px + env(safe-area-inset-bottom))
      calc(12px + env(safe-area-inset-left));
  }

  .ctrl { width: 60px; height: 60px; font-size: 18px; }
  .pad { gap: 10px; }

  .drive-overlay {
    padding: calc(24px + env(safe-area-inset-top)) 20px calc(24px + env(safe-area-inset-bottom));
  }

  .end-card { padding: 22px 20px; }
}

/* Short screens (landscape phones) */
@media (max-height: 480px) {
  .home-inner { padding-top: 16px; padding-bottom: 16px; }
  .home-inner h1 { font-size: 26px; }
  .tagline { margin-bottom: 12px; }
  .mode-grid { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; }
  .mode-card { padding: 14px; }
  #free-draw .panel { max-height: 68vh; }
  #free-draw .hint { display: none; }
}
