:root {
  --sky: #bfe8ff;
  --primary: #ff8a3d;
  --primary-dark: #e5701f;
  --secondary: #4a90c2;
  --panel: #fffaf0;
  --ink: #3a3a3a;
  --accent: #43c97a;
  --danger: #e0645a;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0; width: 100%; height: 100%;
  overflow: hidden;
  background: var(--sky);
  font-family: "Rounded Mplus 1c", "Hiragino Maru Gothic ProN", "Yu Gothic", sans-serif;
  color: var(--ink);
  user-select: none;
  touch-action: none;
}

#app { position: relative; width: 100vw; height: 100dvh; overflow: hidden; }

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

.hidden { display: none !important; }

/* ---------------- generic buttons ---------------- */
.btn {
  font-family: inherit;
  font-weight: 900;
  border: none;
  border-radius: 999px;
  padding: 14px 28px;
  font-size: 1rem;
  cursor: pointer;
  color: #fff;
  background: var(--primary);
  box-shadow: 0 5px 0 var(--primary-dark), 0 8px 16px rgba(0,0,0,0.15);
  transition: transform 0.08s ease;
}
.btn:active { transform: translateY(3px); box-shadow: 0 2px 0 var(--primary-dark); }
.btn-big { font-size: 1.3rem; padding: 18px 44px; }
.btn-secondary { background: #fff; color: var(--secondary); box-shadow: 0 5px 0 #c9dbe8, 0 8px 16px rgba(0,0,0,0.1); }
.btn-secondary:active { box-shadow: 0 2px 0 #c9dbe8; }
.btn-danger { background: var(--danger); box-shadow: 0 5px 0 #b3453d; }
.btn-danger:active { box-shadow: 0 2px 0 #b3453d; }
.btn-toggle { background: var(--accent); box-shadow: 0 5px 0 #2f9d5c; }
.btn-back { margin-top: 18px; }
.btn-round {
  width: 46px; height: 46px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.2rem; padding: 0; background: #fff; color: var(--secondary);
  box-shadow: 0 3px 10px rgba(0,0,0,0.15);
}

/* ---------------- screens ---------------- */
.screen {
  position: absolute; inset: 0; z-index: 5;
  display: flex; align-items: center; justify-content: center;
}
#hud { display: block; pointer-events: none; }
#hud > * { pointer-events: auto; }

#screen-title, #screen-garage, #screen-collection, #screen-settings {
  background: radial-gradient(circle at 50% 20%, rgba(255,255,255,0.35), rgba(191,232,255,0.05) 65%);
  padding: 20px;
}

.title-card { text-align: center; }
.game-title {
  font-size: clamp(2rem, 7vw, 3.4rem);
  line-height: 1.15;
  margin: 0 0 4px;
  color: var(--primary-dark);
  text-shadow: 3px 3px 0 #fff, 6px 6px 0 rgba(0,0,0,0.12);
}
.game-subtitle {
  margin: 0 0 26px;
  font-weight: 900;
  letter-spacing: 0.35em;
  color: var(--secondary);
  font-size: 0.8rem;
}
.title-buttons { display: flex; flex-direction: column; align-items: center; gap: 12px; }
.title-buttons .btn { width: min(260px, 70vw); }

/* ---------------- panel cards ---------------- */
.panel-card {
  background: var(--panel);
  border-radius: 26px;
  padding: 24px 26px 28px;
  max-width: 920px;
  width: min(92vw, 920px);
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 18px 0 rgba(0,0,0,0.06), 0 24px 50px rgba(0,0,0,0.22);
  text-align: center;
}
.panel-heading {
  margin: 0 0 14px;
  font-size: 1.4rem;
  font-weight: 900;
  color: var(--primary-dark);
}

/* ---------------- garage ---------------- */
.garage-layout { display: flex; flex-wrap: wrap; gap: 20px; text-align: left; }
.garage-viewport {
  flex: 1 1 320px;
  min-height: 280px;
  background: linear-gradient(180deg, #cdeeff 0%, #eaf8ff 100%);
  border-radius: 20px;
  position: relative;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
.garage-rotate-hint {
  position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%);
  font-size: 0.75rem; color: #6a8fa8; font-weight: bold;
  background: rgba(255,255,255,0.7); padding: 4px 12px; border-radius: 10px;
}
.garage-controls { flex: 1 1 320px; display: flex; flex-direction: column; gap: 16px; }
.garage-section h3 { margin: 0 0 8px; font-size: 0.85rem; color: #8a8a8a; font-weight: 900; }
.chip-row { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  border: 2px solid #eee; background: #fff; border-radius: 14px;
  padding: 8px 14px; font-weight: 900; font-size: 0.85rem; cursor: pointer; color: #777;
}
.chip.active { border-color: var(--primary); background: #fff3e6; color: var(--primary-dark); }
.chip.locked { opacity: 0.45; cursor: default; }
.swatch-row { display: flex; flex-wrap: wrap; gap: 8px; }
.swatch {
  width: 34px; height: 34px; border-radius: 50%; cursor: pointer;
  border: 3px solid #fff; box-shadow: 0 0 0 2px #e2e2e2;
}
.swatch.active { box-shadow: 0 0 0 3px var(--primary); }
.swatch.locked { opacity: 0.3; cursor: default; }
.garage-stats { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.stat-bar { display: flex; align-items: center; gap: 8px; font-size: 0.78rem; font-weight: 900; color: #888; }
.stat-bar span { width: 84px; flex: none; }
.stat-bar .bar { flex: 1; height: 8px; background: #eee; border-radius: 6px; overflow: hidden; }
.stat-bar .bar i { display: block; height: 100%; background: var(--accent); border-radius: 6px; }

/* ---------------- collection ---------------- */
.collection-explore { font-weight: 900; color: var(--secondary); margin-bottom: 14px; }
.collection-lists { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; text-align: left; }
.collection-group h4 { margin: 0 0 8px; color: var(--primary-dark); font-size: 0.9rem; }
.collection-item {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 4px; font-size: 0.85rem; font-weight: bold;
  color: #666; border-bottom: 1px dashed #eee;
}
.collection-item.found { color: var(--ink); }
.collection-item .mark { width: 18px; text-align: center; }

/* ---------------- settings ---------------- */
.settings-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 4px; border-bottom: 1px solid #f0f0f0; font-weight: 900;
}
.settings-row:last-child { border-bottom: none; justify-content: center; }

/* ---------------- confirm / overlays ---------------- */
.overlay {
  position: absolute; inset: 0; z-index: 20;
  background: rgba(20,30,45,0.45);
  display: flex; align-items: center; justify-content: center;
}
.confirm-card {
  background: var(--panel);
  border-radius: 24px;
  padding: 28px 30px;
  min-width: 260px;
  max-width: 90vw;
  text-align: center;
  box-shadow: 0 16px 0 rgba(0,0,0,0.08), 0 20px 40px rgba(0,0,0,0.25);
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  animation: pop-in 0.25s cubic-bezier(.34,1.56,.64,1);
}
@keyframes pop-in { from { transform: scale(0.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.confirm-card .btn { width: 100%; }
.confirm-buttons { display: flex; gap: 10px; width: 100%; }
.confirm-buttons .btn { flex: 1; }

.result-title { font-size: 2.2rem; font-weight: 900; color: var(--accent); text-shadow: 2px 2px 0 rgba(67,201,122,0.25); }
.result-stars { font-size: 2rem; letter-spacing: 6px; color: #f4c542; }
.result-rewards { font-weight: 900; color: #888; font-size: 0.9rem; }

/* ---------------- HUD ---------------- */
.hud-card {
  background: rgba(255,255,255,0.92);
  border-radius: 16px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.14);
}
/* z-index:5 keeps every HUD corner tappable above .touch-controls (z-index:4,
   see its comment) no matter which one is later in the DOM. */
.hud-top-left { position: absolute; top: 14px; left: 14px; max-width: 62vw; z-index: 5; }
.hud-top-right { position: absolute; top: 14px; right: 14px; display: flex; align-items: center; gap: 8px; z-index: 5; }
.hud-bottom-left { position: absolute; left: 14px; bottom: calc(14px + var(--safe-bottom)); z-index: 5; }
.hud-bottom-right { position: absolute; right: 14px; bottom: calc(14px + var(--safe-bottom)); z-index: 5; }

.mission-panel { padding: 10px 16px; }
.mission-title { font-weight: 900; color: var(--primary-dark); font-size: 0.85rem; }
.mission-desc { font-weight: bold; font-size: 0.95rem; margin-top: 2px; }
.mission-sub { display: flex; gap: 12px; font-size: 0.75rem; color: var(--secondary); font-weight: 900; margin-top: 4px; }

.hud-stats { display: flex; gap: 10px; padding: 8px 14px; font-weight: 900; }
.stat-chip { font-size: 0.9rem; white-space: nowrap; }

.guide-arrow {
  position: absolute; top: 40%; left: 50%;
  font-size: 2.2rem; color: var(--primary);
  filter: drop-shadow(0 3px 4px rgba(0,0,0,0.25));
  transform: translate(-50%, -50%) rotate(0deg);
  transition: transform 0.15s linear;
  pointer-events: none;
}

.speed-card { padding: 10px 16px; min-width: 130px; }
.speed-value { font-weight: 900; font-size: 1.3rem; }
.speed-value small { font-size: 0.7rem; color: #999; }
.nitro-bar { margin-top: 6px; height: 10px; background: #eee; border-radius: 6px; overflow: hidden; }
.nitro-bar i { display: block; height: 100%; width: 0%; background: linear-gradient(90deg, #ffdd55, #ff8a3d); border-radius: 6px; transition: width 0.1s linear; }

.minimap { border-radius: 14px; display: block; }

/* ---------------- popups ---------------- */
.popup-layer { position: absolute; inset: 0; pointer-events: none !important; overflow: hidden; }
.discover-popup {
  position: absolute; top: 18%; left: 50%; transform: translate(-50%, -10px);
  background: rgba(255,255,255,0.95);
  border-radius: 18px; padding: 12px 22px;
  font-weight: 900; color: var(--primary-dark);
  box-shadow: 0 8px 20px rgba(0,0,0,0.2);
  opacity: 0; animation: discover-pop 2.6s ease forwards;
  text-align: center; white-space: nowrap;
}
@keyframes discover-pop {
  0% { opacity: 0; transform: translate(-50%, 10px) scale(0.8); }
  12% { opacity: 1; transform: translate(-50%, -10px) scale(1); }
  82% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -26px) scale(0.95); }
}
.get-popup {
  position: absolute; font-weight: 900; color: #ffb400;
  text-shadow: 1px 1px 0 #fff, -1px -1px 0 #fff;
  font-size: 1.1rem;
  animation: get-pop 1s ease forwards;
}
@keyframes get-pop {
  0% { opacity: 0; transform: translateY(0) scale(0.6); }
  20% { opacity: 1; transform: translateY(-10px) scale(1.15); }
  100% { opacity: 0; transform: translateY(-46px) scale(1); }
}

/* ---------------- touch controls ---------------- */
.touch-controls {
  position: absolute; inset: 0; z-index: 4;
  /* Same bug as .popup-layer: #hud > * { pointer-events: auto } outranks
     a plain class rule on specificity, so without !important this whole
     full-screen (inset:0) div became tappable everywhere - not just its
     button clusters - and being z-index:4 (above the other HUD corners,
     which have no explicit z-index) it silently ate every tap meant for
     the pause button, minimap and speed card on any touch device. */
  pointer-events: none !important;
}
/* Tap+hold anywhere on this (invisible, full-screen) surface to
   accelerate; drag sideways from the touch-down point to steer - see
   input.js's _setupDriveSurface(). It sits UNDER the brake/nitro buttons
   (and every HUD corner - see their explicit z-index below) so those stay
   tappable even though they're visually inside this surface's box. */
.touch-drive-surface { position: absolute; inset: 0; pointer-events: auto; }

/* Stacked well above the minimap (bottom-right, ~110px tall on phones)
   instead of sharing its corner - that overlap was the "map and
   speedometer overlap the driving buttons" report. */
.touch-right {
  position: absolute; right: 14px; bottom: calc(140px + var(--safe-bottom)); z-index: 2;
  display: flex; flex-direction: column; gap: 12px; align-items: flex-end; pointer-events: auto;
}
.touch-btn {
  width: 66px; height: 66px; border-radius: 50%;
  border: none; font-size: 1.5rem;
  background: rgba(255,255,255,0.85); color: var(--secondary);
  box-shadow: 0 5px 0 rgba(0,0,0,0.12);
  display: flex; align-items: center; justify-content: center;
}
.touch-btn:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,0.12); }
.touch-pedal { width: 78px; height: 78px; font-size: 1.7rem; background: rgba(255,255,255,0.92); }
.touch-nitro { background: rgba(255,178,61,0.92); color: #fff; }

.photo-hint {
  position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,0.55); color: #fff; font-weight: bold;
  padding: 8px 18px; border-radius: 14px; font-size: 0.8rem; z-index: 10;
}

@media (max-width: 720px) {
  .hud-top-left { max-width: 55vw; }
  .mission-desc { font-size: 0.82rem; }
  .minimap { width: 110px; height: 110px; }
}

/* Real phones in landscape are wide but SHORT (e.g. ~830x400) - the
   max-width query above doesn't catch those, and with the full-height
   top/bottom HUD corners plus a portrait-sized mission panel there wasn't
   enough vertical room, so the mission panel and coin/star/explore chips
   ended up overlapping. Shrink everything that stacks vertically instead. */
@media (max-height: 480px) {
  .hud-top-left { max-width: 46vw; }
  .mission-panel { padding: 6px 12px; }
  .mission-title { font-size: 0.7rem; }
  .mission-desc { font-size: 0.72rem; margin-top: 0; }
  .mission-sub { font-size: 0.62rem; gap: 8px; margin-top: 2px; }
  .hud-stats { padding: 5px 10px; gap: 7px; }
  .stat-chip { font-size: 0.75rem; }
  .btn-round { width: 38px; height: 38px; font-size: 1rem; }
  .speed-card { padding: 6px 12px; min-width: 100px; }
  .speed-value { font-size: 1.05rem; }
  .minimap { width: 86px; height: 86px; }
  .touch-right { bottom: calc(108px + var(--safe-bottom)); }
  .touch-btn { width: 54px; height: 54px; font-size: 1.2rem; }
  .touch-pedal { width: 62px; height: 62px; font-size: 1.35rem; }
}
