:root {
  --bg: #050510;
  --neon-cyan: #00f0ff;
  --neon-magenta: #ff00aa;
  --neon-purple: #a855f7;
  --neon-green: #39ff14;
  --neon-yellow: #ffe600;
  --neon-orange: #ff6b00;
  --panel: rgba(10, 12, 28, 0.92);
  --border: rgba(0, 240, 255, 0.35);
  --text: #e8f4ff;
  --muted: #7a8aaa;
  --rarity-common: #9ca3af;
  --rarity-uncommon: #22c55e;
  --rarity-rare: #3b82f6;
  --rarity-legendary: #f59e0b;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}

#game-root {
  position: relative;
  width: 100vw;
  height: 100vh;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: radial-gradient(ellipse at center, #0a0a20 0%, #030308 70%);
  cursor: default;
}

.hidden { display: none !important; }

/* HUD */
#hud {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.hud-top {
  position: absolute;
  top: 16px;
  left: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 220px;
  max-width: min(280px, 32vw);
}

.hud-center {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  font-size: 14px;
  text-shadow: 0 0 8px rgba(0, 240, 255, 0.5);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  z-index: 11;
}

#score-display {
  font-weight: 600;
  letter-spacing: 0.02em;
}

#time-display {
  color: var(--muted);
  font-size: 13px;
}

#credits-display {
  margin-top: 2px;
  color: #ffe600;
  font-weight: 700;
  font-size: 13px;
  text-shadow: 0 0 8px rgba(255, 230, 0, 0.45);
}

.bar-group {
  display: grid;
  grid-template-columns: 28px 1fr 48px;
  align-items: center;
  gap: 8px;
  font-size: 12px;
}

.bar-label {
  color: var(--muted);
  font-weight: 700;
  letter-spacing: 0.05em;
}

.bar {
  height: 10px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 4px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  width: 100%;
  transform-origin: left center;
  transition: transform 0.1s linear;
}

.health-bar .bar-fill {
  background: linear-gradient(90deg, #ff2d55, #ff6b8a);
  box-shadow: 0 0 8px #ff2d55;
}

.energy-bar .bar-fill {
  background: linear-gradient(90deg, #00a2ff, var(--neon-cyan));
  box-shadow: 0 0 8px var(--neon-cyan);
}

.xp-bar .bar-fill {
  background: linear-gradient(90deg, var(--neon-purple), #e879f9);
  box-shadow: 0 0 8px var(--neon-purple);
}

#ability-cooldowns {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-start;
}

.ability-cd {
  font-size: 11px;
  color: var(--muted);
  padding: 2px 8px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 4px;
  min-width: 100px;
}

.ability-cd.ready {
  color: var(--neon-cyan);
  border-color: rgba(0, 240, 255, 0.5);
}

/* Overlays */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(2, 4, 16, 0.78);
  backdrop-filter: blur(6px);
  z-index: 20;
  touch-action: manipulation;
  overflow-y: auto;
}

.menu-panel, .levelup-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px 40px;
  max-width: 720px;
  width: 92%;
  box-shadow: 0 0 40px rgba(0, 240, 255, 0.15), inset 0 0 60px rgba(0, 100, 255, 0.05);
  text-align: center;
}

.title {
  font-size: 3rem;
  font-weight: 800;
  letter-spacing: 0.2em;
  background: linear-gradient(90deg, var(--neon-cyan), var(--neon-magenta));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: none;
  filter: drop-shadow(0 0 20px rgba(0, 240, 255, 0.4));
}

.subtitle {
  color: var(--muted);
  margin: 8px 0 20px;
  letter-spacing: 0.08em;
}

.meta-row {
  display: flex;
  justify-content: center;
  gap: 32px;
  margin-bottom: 24px;
  color: var(--muted);
  font-size: 14px;
}

.meta-row strong { color: var(--neon-yellow); }

.boot-status {
  margin-top: 10px;
  font-size: 12px;
  color: var(--muted);
  min-height: 1.2em;
}

.boot-status.error { color: #ff6b8a; }
.boot-status.ready { color: var(--neon-green); }

#menu-screen,
#pause-screen,
#levelup-screen,
#gameover-screen,
#shop-screen {
  pointer-events: auto;
}

#menu-screen.hidden,
#pause-screen.hidden,
#levelup-screen.hidden,
#gameover-screen.hidden,
#shop-screen.hidden {
  pointer-events: none;
}

.btn {
  display: inline-block;
  margin: 6px;
  padding: 12px 28px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
  pointer-events: auto;
}

.btn:hover {
  border-color: var(--neon-cyan);
  box-shadow: 0 0 16px rgba(0, 240, 255, 0.3);
  transform: translateY(-1px);
}

.btn.primary {
  background: linear-gradient(135deg, rgba(0, 240, 255, 0.25), rgba(255, 0, 170, 0.2));
  border-color: var(--neon-cyan);
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.controls-hint {
  margin-top: 24px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.7;
}

.controls-hint kbd {
  display: inline-block;
  padding: 1px 6px;
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.08);
  font-family: inherit;
  font-size: 12px;
  color: var(--neon-cyan);
}

.meta-shop {
  margin-top: 28px;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  text-align: left;
}

.meta-shop h3 {
  text-align: center;
  font-size: 14px;
  color: var(--muted);
  margin-bottom: 12px;
  letter-spacing: 0.1em;
}

.meta-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  margin: 4px 0;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  font-size: 13px;
}

.meta-item.owned { border-color: rgba(57, 255, 20, 0.4); color: var(--neon-green); }
.meta-item button { pointer-events: auto; padding: 6px 12px; font-size: 12px; margin: 0; }

/* Level up */
.levelup-panel h2 {
  font-size: 2rem;
  letter-spacing: 0.15em;
  color: var(--neon-purple);
  text-shadow: 0 0 20px rgba(168, 85, 247, 0.6);
}

#levelup-level {
  color: var(--muted);
  margin: 8px 0 20px;
}

.stat-buttons {
  display: flex;
  justify-content: center;
  gap: 8px;
  flex-wrap: wrap;
  margin: 12px 0;
}

.stat-btn { min-width: 110px; }

.stat-preview {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin: 12px 0 20px;
  font-size: 13px;
  color: var(--muted);
}

.stat-preview strong { color: var(--text); }

.upgrade-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-top: 12px;
}

.upgrade-card {
  pointer-events: auto;
  cursor: pointer;
  text-align: left;
  padding: 16px;
  border-radius: 12px;
  background: rgba(20, 24, 48, 0.9);
  border: 2px solid var(--rarity-common);
  transition: transform 0.15s, box-shadow 0.15s;
}

.upgrade-card:hover {
  transform: translateY(-4px) scale(1.02);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.upgrade-card.kb-selected {
  transform: translateY(-4px) scale(1.03);
  box-shadow: 0 0 0 2px #00f0ff, 0 10px 28px rgba(0, 240, 255, 0.25);
}

.stat-btn.kb-selected,
.btn.kb-selected,
.shop-buy.kb-selected,
.meta-item .btn.kb-selected {
  border-color: var(--neon-cyan);
  box-shadow: 0 0 0 2px rgba(0, 240, 255, 0.55), 0 0 18px rgba(0, 240, 255, 0.3);
  background: rgba(0, 240, 255, 0.12);
  outline: none;
}

.upgrade-card.common { border-color: var(--rarity-common); }
.upgrade-card.uncommon { border-color: var(--rarity-uncommon); box-shadow: 0 0 12px rgba(34, 197, 94, 0.2); }
.upgrade-card.rare { border-color: var(--rarity-rare); box-shadow: 0 0 12px rgba(59, 130, 246, 0.25); }
.upgrade-card.legendary {
  border-color: var(--rarity-legendary);
  box-shadow: 0 0 16px rgba(245, 158, 11, 0.35);
  background: linear-gradient(160deg, rgba(40, 30, 10, 0.95), rgba(20, 24, 48, 0.95));
}

.upgrade-card .rarity {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  margin-bottom: 6px;
  opacity: 0.85;
}

.upgrade-card.common .rarity { color: var(--rarity-common); }
.upgrade-card.uncommon .rarity { color: var(--rarity-uncommon); }
.upgrade-card.rare .rarity { color: var(--rarity-rare); }
.upgrade-card.legendary .rarity { color: var(--rarity-legendary); }

.upgrade-card h3 {
  font-size: 15px;
  margin-bottom: 8px;
}

.upgrade-card p {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.4;
}

.upgrade-card .tags {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.upgrade-card .tag {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--neon-cyan);
}

.run-summary {
  text-align: left;
  margin: 20px auto;
  max-width: 320px;
  font-size: 14px;
  line-height: 1.8;
  color: var(--muted);
}

.run-summary strong { color: var(--text); }

/* In-run trade shop */
.shop-panel {
  max-width: 560px;
  max-height: 88vh;
  overflow-y: auto;
}

.shop-panel h2 {
  color: #fbbf24;
  letter-spacing: 0.12em;
  text-shadow: 0 0 18px rgba(251, 191, 36, 0.45);
}

.shop-sub {
  color: var(--muted);
  margin: 8px 0 16px;
}

.shop-sub strong {
  color: #ffe600;
}

.shop-items {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
  text-align: left;
}

.shop-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid rgba(251, 191, 36, 0.28);
  border-radius: 10px;
  background: rgba(20, 24, 48, 0.75);
}

.shop-item-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.shop-item-info strong {
  color: var(--text);
  font-size: 14px;
}

.shop-item-desc {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.shop-buy {
  flex-shrink: 0;
  min-width: 88px;
  margin: 0;
  border-color: rgba(251, 191, 36, 0.55);
  color: #ffe600;
}

.shop-buy.disabled,
.shop-buy:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.shop-empty {
  text-align: center;
  color: var(--muted);
  padding: 16px;
}

.touch-dock {
  width: 52px;
  height: 52px;
  font-size: 11px;
  letter-spacing: 0.04em;
  opacity: 0.35;
  border-color: rgba(251, 191, 36, 0.35);
  background: rgba(251, 191, 36, 0.08);
  pointer-events: none;
}

.touch-dock.dock-ready {
  opacity: 1;
  pointer-events: auto;
  border-color: rgba(251, 191, 36, 0.85);
  background: rgba(251, 191, 36, 0.22);
  box-shadow: 0 0 16px rgba(251, 191, 36, 0.4);
}

.hint-touch { display: none; }
.hint-desktop { display: block; }

body[data-input-platform="touch"] .hint-touch { display: block; }
body[data-input-platform="touch"] .hint-desktop { display: none; }

body[data-input-platform="desktop"] .hint-desktop { display: block; }
body[data-input-platform="desktop"] .hint-touch { display: none; }

/* On-screen touch controls (tablets / phones) */
.touch-controls {
  display: none;
  position: absolute;
  inset: 0;
  z-index: 18;
  pointer-events: none;
}

.touch-controls.touch-available.visible {
  display: block !important;
}

.touch-btn {
  pointer-events: auto;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  border: 2px solid rgba(0, 240, 255, 0.65);
  border-radius: 50%;
  background: rgba(6, 10, 28, 0.78);
  color: #e8f4ff;
  font-weight: 700;
  font-size: 16px;
  box-shadow: 0 0 18px rgba(0, 240, 255, 0.28), inset 0 0 18px rgba(0, 100, 255, 0.12);
  backdrop-filter: blur(6px);
  opacity: 0.92;
  transition: opacity 0.1s, transform 0.1s, border-color 0.1s, background 0.1s;
}

.touch-btn:active,
.touch-btn.active {
  opacity: 1;
  transform: scale(0.96);
  border-color: var(--neon-cyan);
  background: rgba(0, 240, 255, 0.35);
  box-shadow: 0 0 24px rgba(0, 240, 255, 0.55);
}

.touch-cluster {
  position: absolute;
  /* Raised so stick / fire stay fully on-screen above browser chrome */
  bottom: max(120px, calc(env(safe-area-inset-bottom, 0px) + 100px));
  pointer-events: none;
}

.touch-left {
  left: max(20px, env(safe-area-inset-left, 0px));
}

.touch-right {
  right: max(20px, env(safe-area-inset-right, 0px));
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
}

/* Virtual joystick */
.touch-stick {
  position: relative;
  width: 150px;
  height: 150px;
  pointer-events: auto;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

.touch-stick-base {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid rgba(0, 240, 255, 0.55);
  background: rgba(6, 10, 28, 0.55);
  box-shadow: 0 0 20px rgba(0, 240, 255, 0.18), inset 0 0 24px rgba(0, 80, 160, 0.15);
}

.touch-stick-base::after {
  content: '';
  position: absolute;
  inset: 28%;
  border-radius: 50%;
  border: 1px dashed rgba(0, 240, 255, 0.25);
}

.touch-stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 58px;
  height: 58px;
  margin: -29px 0 0 -29px;
  border-radius: 50%;
  border: 2px solid rgba(0, 240, 255, 0.85);
  background: radial-gradient(circle at 35% 30%, rgba(0, 240, 255, 0.55), rgba(0, 80, 140, 0.85));
  box-shadow: 0 0 16px rgba(0, 240, 255, 0.45);
  pointer-events: none;
  will-change: transform;
}

.touch-stick.active .touch-stick-base {
  border-color: var(--neon-cyan);
  background: rgba(0, 240, 255, 0.12);
}

.touch-actions {
  display: flex;
  align-items: center;
  gap: 14px;
  pointer-events: none;
}

.touch-fire {
  width: 96px;
  height: 96px;
  font-size: 15px;
  letter-spacing: 0.08em;
  border-color: rgba(255, 0, 170, 0.45);
  background: rgba(255, 0, 170, 0.12);
}

.touch-fire:active,
.touch-fire.active {
  border-color: var(--neon-magenta);
  background: rgba(255, 0, 170, 0.28);
  box-shadow: 0 0 20px rgba(255, 0, 170, 0.4);
}

.touch-dash {
  width: 72px;
  height: 72px;
  font-size: 12px;
  letter-spacing: 0.06em;
  border-color: rgba(57, 255, 20, 0.4);
  background: rgba(57, 255, 20, 0.1);
}

.touch-dash:active,
.touch-dash.active {
  border-color: var(--neon-green);
  background: rgba(57, 255, 20, 0.25);
  box-shadow: 0 0 16px rgba(57, 255, 20, 0.35);
}

.touch-abilities {
  display: flex;
  gap: 10px;
  pointer-events: none;
}

.touch-ability {
  width: 52px;
  height: 52px;
  font-size: 15px;
  border-color: rgba(168, 85, 247, 0.45);
  background: rgba(168, 85, 247, 0.12);
}

.touch-ability:active,
.touch-ability.active {
  border-color: var(--neon-purple);
  background: rgba(168, 85, 247, 0.28);
  box-shadow: 0 0 14px rgba(168, 85, 247, 0.4);
}

/* Pause top-center, under score/timer */
.touch-pause {
  position: absolute;
  top: max(64px, calc(env(safe-area-inset-top, 0px) + 56px));
  left: 50%;
  transform: translateX(-50%);
  width: 48px;
  height: 48px;
  font-size: 14px;
  border-radius: 12px;
  opacity: 0.9;
}

.touch-pause:active,
.touch-pause.active {
  transform: translateX(-50%) scale(0.96);
}

/* Hard-hide touch chrome on desktop */
body[data-input-platform="desktop"] #touch-controls {
  display: none !important;
}

@media (max-width: 900px), (pointer: coarse) {
  .menu-panel, .levelup-panel {
    padding: 24px 18px;
    max-height: 92vh;
    overflow-y: auto;
  }

  .title { font-size: 2.2rem; letter-spacing: 0.12em; }

  .hud-top { min-width: 160px; top: 10px; left: 10px; max-width: min(240px, 38vw); }
  .hud-center { top: 10px; font-size: 13px; }
  .bar-group { font-size: 11px; grid-template-columns: 24px 1fr 40px; }

  .upgrade-cards {
    grid-template-columns: 1fr;
    max-height: 50vh;
    overflow-y: auto;
  }

  .stat-btn { min-width: 96px; padding: 14px 18px; }
  .btn { padding: 14px 24px; min-height: 48px; }
}

/* ---- Phone layout (narrow devices only; tablet/PC unchanged) ---- */
body[data-form-factor="phone"] .hud-top {
  top: 8px;
  left: 8px;
  min-width: 0;
  max-width: 42vw;
  gap: 4px;
}

body[data-form-factor="phone"] .bar-group {
  font-size: 10px;
  grid-template-columns: 20px 1fr 32px;
  gap: 4px;
}

body[data-form-factor="phone"] .bar { height: 8px; }

body[data-form-factor="phone"] .hud-center {
  top: 6px;
  font-size: 11px;
  gap: 0;
}

body[data-form-factor="phone"] #credits-display { font-size: 11px; }

body[data-form-factor="phone"] #ability-cooldowns {
  margin-top: 4px;
  font-size: 10px;
  max-width: 42vw;
}

/* Lift controls well above home indicator / browser chrome */
body[data-form-factor="phone"] .touch-cluster {
  bottom: max(96px, calc(env(safe-area-inset-bottom, 0px) + 72px));
}

body[data-form-factor="phone"] .touch-left {
  left: max(10px, env(safe-area-inset-left, 0px));
}

body[data-form-factor="phone"] .touch-right {
  right: max(10px, env(safe-area-inset-right, 0px));
  gap: 8px;
  max-width: 48vw;
}

body[data-form-factor="phone"] .touch-stick {
  width: 112px;
  height: 112px;
}

body[data-form-factor="phone"] .touch-stick-knob {
  width: 44px;
  height: 44px;
  margin: -22px 0 0 -22px;
}

body[data-form-factor="phone"] .touch-abilities {
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: 160px;
}

body[data-form-factor="phone"] .touch-ability,
body[data-form-factor="phone"] .touch-dock {
  width: 40px;
  height: 40px;
  font-size: 11px;
}

body[data-form-factor="phone"] .touch-actions {
  gap: 10px;
}

body[data-form-factor="phone"] .touch-fire {
  width: 68px;
  height: 68px;
  font-size: 12px;
  letter-spacing: 0.04em;
}

body[data-form-factor="phone"] .touch-dash {
  width: 52px;
  height: 52px;
  font-size: 10px;
}

/* Pause under score/timer, a bit lower for thumb reach */
body[data-form-factor="phone"] .touch-pause {
  top: max(72px, calc(env(safe-area-inset-top, 0px) + 64px));
  width: 40px;
  height: 40px;
  font-size: 12px;
  border-radius: 10px;
}

@media (max-width: 900px) and (orientation: landscape) and (max-height: 480px) {
  body[data-form-factor="phone"] .touch-cluster {
    bottom: max(56px, calc(env(safe-area-inset-bottom, 0px) + 40px));
  }
  body[data-form-factor="phone"] .touch-stick { width: 96px; height: 96px; }
  body[data-form-factor="phone"] .touch-stick-knob { width: 36px; height: 36px; margin: -18px 0 0 -18px; }
  body[data-form-factor="phone"] .touch-fire { width: 58px; height: 58px; font-size: 11px; }
  body[data-form-factor="phone"] .touch-dash { width: 46px; height: 46px; font-size: 9px; }
  body[data-form-factor="phone"] .touch-ability,
  body[data-form-factor="phone"] .touch-dock { width: 36px; height: 36px; font-size: 10px; }
  body[data-form-factor="phone"] .touch-pause {
    top: max(44px, calc(env(safe-area-inset-top, 0px) + 36px));
    width: 36px;
    height: 36px;
  }
  body[data-form-factor="phone"] .hud-top { max-width: 34vw; }
  body[data-form-factor="phone"] .hud-center { font-size: 10px; }
}

/* Tablet keeps raised controls */
@media (min-width: 601px) and (pointer: coarse) {
  body[data-form-factor="tablet"] .touch-cluster {
    bottom: max(100px, calc(env(safe-area-inset-bottom, 0px) + 80px));
  }
}

@media (orientation: landscape) and (max-height: 520px) {
  body:not([data-form-factor="phone"]) .touch-cluster {
    bottom: max(88px, calc(env(safe-area-inset-bottom, 0px) + 72px));
  }
  body:not([data-form-factor="phone"]) .touch-stick { width: 120px; height: 120px; }
  body:not([data-form-factor="phone"]) .touch-stick-knob { width: 46px; height: 46px; margin: -23px 0 0 -23px; }
  body:not([data-form-factor="phone"]) .touch-fire { width: 80px; height: 80px; }
  body:not([data-form-factor="phone"]) .touch-dash { width: 60px; height: 60px; }
}
