:root {
  --hud-cyan: #6df3ff;
  --hud-amber: #ffc857;
  --hud-magenta: #ff5ecf;
  --bg: #020611;
  /* Single source of truth for the small corner buttons (PAUSE, PAD, AUTO).
     Shared so the PAD offset can stay exactly one button + gap to the right
     of PAUSE at every viewport width — no math drift, no overlap. */
  --tbtn-small-size: clamp(36px, 4.5vw, 48px);
  --tbtn-small-gap:  8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: #cfe6ff;
  font-family: "SF Pro Display", "Helvetica Neue", "Arial", sans-serif;
  overflow: hidden;
  height: 100%;
  /* Use dynamic viewport so mobile URL bars don't clip the canvas */
  height: 100dvh;
  width: 100%;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: none;
}

#stage-wrap {
  position: fixed;
  inset: 0;
  /* Use the small viewport so the URL bar can't eat into the play area */
  height: 100svh;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at 30% 0%, #1c2748 0%, transparent 60%),
    radial-gradient(ellipse at 70% 100%, #2a1a3a 0%, transparent 60%),
    var(--bg);
}

canvas#game {
  display: block;
  /* Actual size is set by JS via window.visualViewport so it stays
     inside the truly-visible area on Android Chrome (URL bar + nav bar)
     and iOS Safari (URL bar + home indicator) alike. The CSS values
     below are just an initial fallback before JS runs. */
  width: 100vw;
  height: auto;
  aspect-ratio: 16/9;
  background: #000;
  image-rendering: auto;
  box-shadow: 0 0 60px rgba(109, 243, 255, 0.15), 0 0 200px rgba(0,0,0,0.6) inset;
}

/* ===== Loader ===== */
#loader {
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at center, #061425 0%, #000 70%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 50;
  transition: opacity 0.6s ease;
}
#loader.hidden { opacity: 0; pointer-events: none; }
.loader-title {
  font-size: clamp(28px, 6vw, 72px);
  font-weight: 900;
  letter-spacing: 0.25em;
  color: #fff;
  text-shadow: 0 0 24px var(--hud-cyan), 0 0 48px rgba(109,243,255,0.5);
}
.loader-sub {
  margin-top: 6px;
  font-size: clamp(12px, 1.8vw, 18px);
  letter-spacing: 0.6em;
  color: var(--hud-cyan);
  text-transform: uppercase;
  opacity: 0.85;
}
.loader-bar {
  margin-top: 36px;
  width: min(60vw, 480px);
  height: 6px;
  border: 1px solid rgba(109,243,255,0.4);
  background: rgba(109,243,255,0.05);
}
#loader-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #6df3ff, #ffc857, #ff5ecf);
  box-shadow: 0 0 12px #6df3ff;
  transition: width 0.2s ease;
}
.loader-status {
  margin-top: 12px;
  font-size: 11px;
  letter-spacing: 0.4em;
  color: rgba(207, 230, 255, 0.7);
}

/* ===== Touch UI ===== */
#touch-ui {
  position: fixed;
  inset: 0;
  pointer-events: none;
}
#touch-pad {
  position: absolute;
  left: 0;
  top: 0;
  width: 55%;
  height: 100%;
  pointer-events: auto;
}
#touch-buttons {
  position: absolute;
  right: max(2vw, env(safe-area-inset-right));
  bottom: max(8px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  pointer-events: auto;
}
.tbtn {
  width:  clamp(64px, 9vw, 96px);
  height: clamp(64px, 9vw, 96px);
  border-radius: 50%;
  background:
    radial-gradient(circle at 30% 30%, rgba(255,255,255,0.25), rgba(255,255,255,0.02) 40%),
    radial-gradient(circle at 50% 60%, rgba(109,243,255,0.35), rgba(8,30,50,0.85) 70%);
  border: 2px solid rgba(109,243,255,0.55);
  color: #fff;
  font-weight: 800;
  letter-spacing: 0.15em;
  font-size: clamp(13px, 1.6vw, 18px);
  cursor: pointer;
  box-shadow:
    0 0 20px rgba(109,243,255,0.25),
    inset 0 0 12px rgba(109,243,255,0.4);
  text-shadow: 0 0 8px rgba(109,243,255,0.8);
  transition: transform 0.06s ease, box-shadow 0.1s ease;
}
.tbtn:active {
  transform: scale(0.92);
  box-shadow:
    0 0 32px rgba(109,243,255,0.6),
    inset 0 0 20px rgba(255,255,255,0.4);
}
#btn-power {
  border-color: rgba(255,200,87,0.7);
  box-shadow:
    0 0 20px rgba(255,200,87,0.3),
    inset 0 0 12px rgba(255,200,87,0.5);
  background:
    radial-gradient(circle at 30% 30%, rgba(255,255,255,0.25), rgba(255,255,255,0.02) 40%),
    radial-gradient(circle at 50% 60%, rgba(255,200,87,0.35), rgba(45,30,8,0.85) 70%);
  text-shadow: 0 0 8px rgba(255,200,87,0.9);
}

/* virtual D-pad (mobile) — bottom-left, sits over the drag pad. Visuals are
   informational only; the whole pad is the touch target and direction is
   resolved from finger position relative to the pad center. */
#touch-dpad {
  position: absolute;
  left: max(2vw, env(safe-area-inset-left));
  bottom: max(8px, env(safe-area-inset-bottom));
  /* Slightly smaller than the original so it covers less of the canvas. */
  width:  clamp(120px, 17vw, 170px);
  height: clamp(120px, 17vw, 170px);
  pointer-events: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
/* Disabled state — D-pad is removed from layout entirely so it doesn't
   cover the playfield when the player has the toggle off. */
#touch-dpad[hidden] { display: none; }
#touch-dpad.disabled { display: none; }
.dpad-rim {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid rgba(109,243,255,0.45);
  background:
    radial-gradient(circle at 30% 30%, rgba(255,255,255,0.18), rgba(255,255,255,0.02) 45%),
    radial-gradient(circle at 50% 60%, rgba(109,243,255,0.18), rgba(8,30,50,0.55) 70%);
  box-shadow:
    0 0 18px rgba(109,243,255,0.20),
    inset 0 0 14px rgba(109,243,255,0.30);
  pointer-events: none;
}
.dpad-rim::before {
  /* faint cross to hint at 4-direction layout */
  content: "";
  position: absolute;
  inset: 18%;
  background:
    linear-gradient(to right, transparent 47%, rgba(109,243,255,0.25) 47% 53%, transparent 53%),
    linear-gradient(to bottom, transparent 47%, rgba(109,243,255,0.25) 47% 53%, transparent 53%);
}
.dpad-glyph {
  position: absolute;
  color: #fff;
  font-size: clamp(18px, 2.4vw, 26px);
  font-weight: 800;
  text-shadow: 0 0 8px rgba(109,243,255,0.9);
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24%;
  height: 24%;
}
.dpad-up    { top:  4%; left: 50%; transform: translateX(-50%); }
.dpad-down  { bottom: 4%; left: 50%; transform: translateX(-50%); }
.dpad-left  { left:  4%; top:  50%; transform: translateY(-50%); }
.dpad-right { right: 4%; top:  50%; transform: translateY(-50%); }
.dpad-knob {
  /* small floating knob that follows the active touch — purely cosmetic */
  position: absolute;
  width: 28%;
  height: 28%;
  left: 50%;
  top:  50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background:
    radial-gradient(circle at 30% 30%, rgba(255,255,255,0.45), rgba(255,255,255,0.06) 50%),
    radial-gradient(circle at 50% 60%, rgba(109,243,255,0.55), rgba(8,30,50,0.85) 70%);
  border: 2px solid rgba(109,243,255,0.7);
  box-shadow:
    0 0 18px rgba(109,243,255,0.45),
    inset 0 0 10px rgba(255,255,255,0.25);
  pointer-events: none;
  transition: transform 0.05s ease;
}
#touch-dpad.held .dpad-knob {
  box-shadow:
    0 0 26px rgba(109,243,255,0.75),
    inset 0 0 14px rgba(255,255,255,0.5);
}

/* small top-left pause button (mobile). Size is driven by --tbtn-small-size
   so the PAD button can compute its offset reliably. */
.tbtn-small {
  position: absolute;
  top: max(10px, env(safe-area-inset-top));
  left: max(10px, env(safe-area-inset-left));
  width:  var(--tbtn-small-size);
  height: var(--tbtn-small-size);
  border-radius: 50%;
  background:
    radial-gradient(circle at 30% 30%, rgba(255,255,255,0.22), rgba(255,255,255,0.02) 40%),
    radial-gradient(circle at 50% 60%, rgba(255,255,255,0.12), rgba(8,18,40,0.85) 70%);
  border: 2px solid rgba(255,255,255,0.55);
  color: #fff;
  font-weight: 800;
  letter-spacing: 0.14em;
  font-size: clamp(11px, 1.3vw, 16px);
  cursor: pointer;
  pointer-events: auto;
  box-shadow:
    0 0 14px rgba(255,255,255,0.18),
    inset 0 0 8px rgba(255,255,255,0.25);
  text-shadow: 0 0 6px rgba(255,255,255,0.8);
}
.tbtn-small:active {
  transform: scale(0.92);
  box-shadow:
    0 0 24px rgba(255,255,255,0.4),
    inset 0 0 16px rgba(255,255,255,0.4);
}

/* Auto-fire toggle — mirrors the pause button to the top-right corner.
   Idle: dim cyan outline. ON: bright amber, pulsing inner glow so the
   player can see at a glance that auto-fire is engaged. */
/* PAD toggle — stacked under the AUTO button on the top-right. */
.tbtn-dpad {
  left: auto;
  right: max(10px, env(safe-area-inset-right));
  top: calc(max(10px, env(safe-area-inset-top)) + var(--tbtn-small-size) + var(--tbtn-small-gap));
  font-size: clamp(9px, 1.1vw, 12px);
  letter-spacing: 0.10em;
}
.tbtn-dpad[aria-pressed="false"] {
  border-color: rgba(255,255,255,0.30);
  color: rgba(207,230,255,0.55);
  background:
    radial-gradient(circle at 30% 30%, rgba(255,255,255,0.10), rgba(255,255,255,0.02) 40%),
    radial-gradient(circle at 50% 60%, rgba(255,255,255,0.04), rgba(8,18,40,0.85) 70%);
  box-shadow:
    0 0 8px rgba(255,255,255,0.10),
    inset 0 0 6px rgba(255,255,255,0.12);
  text-shadow: none;
}

.tbtn-auto {
  left: auto;
  right: max(10px, env(safe-area-inset-right));
  font-size: clamp(9px, 1.1vw, 12px);
  letter-spacing: 0.10em;
  border-color: rgba(109,243,255,0.55);
  color: rgba(207,230,255,0.85);
  background:
    radial-gradient(circle at 30% 30%, rgba(255,255,255,0.18), rgba(255,255,255,0.02) 40%),
    radial-gradient(circle at 50% 60%, rgba(109,243,255,0.18), rgba(8,18,40,0.85) 70%);
  box-shadow:
    0 0 14px rgba(109,243,255,0.18),
    inset 0 0 10px rgba(109,243,255,0.28);
  text-shadow: 0 0 6px rgba(109,243,255,0.7);
}
.tbtn-auto[aria-pressed="true"] {
  border-color: rgba(255,200,87,0.85);
  color: #fff;
  background:
    radial-gradient(circle at 30% 30%, rgba(255,255,255,0.28), rgba(255,255,255,0.05) 40%),
    radial-gradient(circle at 50% 60%, rgba(255,200,87,0.45), rgba(45,30,8,0.85) 70%);
  box-shadow:
    0 0 24px rgba(255,200,87,0.55),
    inset 0 0 14px rgba(255,200,87,0.55);
  text-shadow: 0 0 10px rgba(255,200,87,0.95);
  animation: autofire-pulse 1.1s ease-in-out infinite;
}
@keyframes autofire-pulse {
  0%, 100% { box-shadow: 0 0 20px rgba(255,200,87,0.45), inset 0 0 12px rgba(255,200,87,0.5); }
  50%      { box-shadow: 0 0 32px rgba(255,200,87,0.75), inset 0 0 18px rgba(255,200,87,0.75); }
}

/* Orientation lock for portrait phones */
#orient-lock {
  display: none;
  position: fixed;
  inset: 0;
  background: #000;
  color: #fff;
  z-index: 100;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 20px;
}
.orient-card { font-size: 18px; letter-spacing: 0.15em; }
.orient-icon {
  font-size: 64px;
  margin-bottom: 14px;
  animation: rot 1.5s ease-in-out infinite;
  display: inline-block;
}
.orient-sub {
  margin-top: 6px;
  color: rgba(255,255,255,0.6);
  font-size: 12px;
  letter-spacing: 0.3em;
}
@keyframes rot {
  0%,100% { transform: rotate(-15deg); }
  50% { transform: rotate(15deg); }
}
@media (orientation: portrait) and (max-width: 900px) {
  #orient-lock { display: flex; }
}
