:root {
  --bg: #0b1020;
  --fg: #f4f1ea;
  --fg-muted: rgba(244, 241, 234, 0.72);
  --accent: #7dffb3;
  --scrim: rgba(11, 16, 32, 0.72);
  --glass: rgba(255, 255, 255, 0.12);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --motion: 180ms ease;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  overflow: hidden;
}

#renderCanvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* —— Boot —— */
#boot {
  position: fixed;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  background: var(--bg);
  padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}
#boot.hidden { display: none; }
.boot-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 24px;
}
.boot-copy {
  margin: 0;
  color: var(--fg);
  font-size: clamp(1rem, 3.5vw, 1.15rem);
  font-weight: 600;
  letter-spacing: 0.01em;
  animation: boot-pulse 1.4s ease-in-out infinite;
}
.boot-bar {
  width: min(220px, 56vw);
  height: 4px;
  border-radius: 999px;
  background: var(--glass);
  overflow: hidden;
}
.boot-bar span {
  display: block;
  height: 100%;
  width: 40%;
  border-radius: 999px;
  background: var(--accent);
  animation: boot-bar 1.2s ease-in-out infinite;
}
@keyframes boot-pulse {
  0%, 100% { opacity: 0.72; }
  50% { opacity: 1; }
}
@keyframes boot-bar {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(280%); }
}

/* —— HUD —— */
#hud {
  position: fixed;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding:
    calc(12px + var(--safe-top))
    calc(16px + var(--safe-right))
    calc(12px + var(--safe-bottom))
    calc(16px + var(--safe-left));
}
#hud .top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.score-block {
  display: flex;
  align-items: baseline;
  gap: 14px;
  min-width: 0;
}
#hud .score {
  font-weight: 700;
  letter-spacing: 0.02em;
  font-size: clamp(1rem, 3.5vw, 1.35rem);
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}
#hud .coins {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--fg-muted);
  font-weight: 600;
  font-size: clamp(0.9rem, 3vw, 1.05rem);
  font-variant-numeric: tabular-nums;
}
.coin-glyph {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffe9a8, #e6b422 55%, #b8860b);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.25) inset;
  flex-shrink: 0;
}
#hud .controls {
  pointer-events: auto;
  display: flex;
  gap: 10px;
}
#hud button,
.overlay button {
  pointer-events: auto;
  border: 0;
  border-radius: 999px;
  background: var(--glass);
  color: var(--fg);
  padding: 10px 14px;
  min-width: 44px;
  min-height: 44px;
  cursor: pointer;
  font: inherit;
  transition: background var(--motion), transform var(--motion), opacity var(--motion);
}
#hud button img {
  width: 28px;
  height: 28px;
  display: block;
}
#hud button:focus-visible,
.overlay button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}
#hud .hint {
  align-self: end;
  justify-self: center;
  color: var(--fg-muted);
  font-size: 0.9rem;
  text-align: center;
  max-width: 92vw;
  transition: opacity var(--motion);
}
#hud .hint.faded {
  opacity: 0;
  pointer-events: none;
}

/* —— Overlays —— */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: center;
  padding:
    calc(16px + var(--safe-top))
    calc(16px + var(--safe-right))
    calc(16px + var(--safe-bottom))
    calc(16px + var(--safe-left));
  background: var(--scrim);
  pointer-events: auto;
  opacity: 1;
  transition: opacity var(--motion);
}
.overlay[hidden] {
  display: none;
}
.overlay .card {
  width: min(320px, 100%);
  border-radius: 16px;
  background: rgba(11, 16, 32, 0.92);
  border: 1px solid var(--glass);
  padding: 24px 20px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  text-align: center;
  transform: scale(1);
  transition: transform var(--motion), opacity var(--motion);
}
.overlay .card h2 {
  margin: 0 0 4px;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--fg);
}
.overlay .final-stats {
  margin: 0 0 8px;
  color: var(--fg-muted);
  font-size: 0.95rem;
}
.overlay .final-stats strong {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.btn-primary {
  width: 100%;
  background: var(--accent) !important;
  color: #0b1020 !important;
  font-weight: 700;
  font-size: 1rem;
  min-height: 48px;
}
.btn-secondary {
  width: 100%;
  background: transparent !important;
  color: var(--fg-muted) !important;
  text-decoration: underline;
  text-underline-offset: 3px;
  min-height: 44px;
}
.btn-primary:active,
.btn-secondary:active,
#hud button:active {
  transform: scale(0.98);
}

@media (prefers-reduced-motion: reduce) {
  :root { --motion: 0ms; }
  .boot-copy,
  .boot-bar span { animation: none; }
  .boot-copy { opacity: 1; }
  .boot-bar span { width: 100%; transform: none; }
}

@media (orientation: landscape) and (max-height: 480px) {
  #hud { padding-top: calc(8px + var(--safe-top)); padding-bottom: calc(8px + var(--safe-bottom)); }
  #hud .hint { font-size: 0.8rem; }
  .overlay .card { padding: 16px; }
}
