:root {
  --bg: #070d18;
  --panel: #0f1a2c;
  --panel-2: #16243b;
  --border: #24385a;
  --text: #e8eef7;
  --muted: #9db0c9;
  --gold: #ffc93c;
  --gold-deep: #7a4b00;
  --accent: #3fb8ff;
  --brick: #b5532a;
  --danger: #ff5a4f;
  --pixel: 'Press Start 2P', ui-monospace, monospace;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, 'Segoe UI', sans-serif;
  overscroll-behavior: none;
}

body {
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

#app {
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  height: 100dvh;
}

/* ---- HUD ---- */
#hud {
  display: flex;
  align-items: center;
  gap: clamp(10px, 3vw, 28px);
  padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
  padding-top: max(8px, env(safe-area-inset-top));
  background: linear-gradient(#0f1a2c, #0b1424);
  border-bottom: 1px solid var(--border);
  z-index: 2;
}

.hud-item { display: flex; flex-direction: column; line-height: 1.1; min-width: 0; }
.hud-label { font-size: 10px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); }
.hud-value { font-family: var(--pixel); font-size: 14px; margin-top: 5px; white-space: nowrap; }
.hud-gold .hud-value { color: var(--gold); }
.hud-lives .hud-value { color: var(--accent); }
.hud-actions { margin-left: auto; display: flex; gap: 8px; }

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  display: grid;
  place-items: center;
  cursor: pointer;
  flex: none;
}
.icon-btn:hover { background: #1d2f4c; }
#btn-mute .icon-muted { display: none; }
#btn-mute[aria-pressed='true'] .icon-muted { display: block; }
#btn-mute[aria-pressed='true'] .icon-sound { display: none; }

button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---- Stage ---- */
#stage { position: relative; min-height: 0; overflow: hidden; }
#canvas-host { position: absolute; inset: 0; }
#canvas-host canvas { display: block; width: 100%; height: 100%; touch-action: none; }

#banner {
  position: absolute;
  left: 50%;
  top: 16%;
  transform: translate(-50%, 8px);
  width: min(92vw, 640px);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s;
}
#banner.show { opacity: 1; transform: translate(-50%, 0); }
.banner-title {
  font-family: var(--pixel);
  font-size: clamp(18px, 4.2vw, 36px);
  color: var(--gold);
  text-shadow: 0 3px 0 var(--gold-deep), 0 0 24px rgba(255, 190, 60, 0.45);
}
.banner-sub {
  margin-top: 12px;
  font-weight: 600;
  font-size: clamp(13px, 2vw, 16px);
  text-shadow: 0 2px 8px #000;
}

/* ---- Touch controls ---- */
#touch-controls { display: none; }
body.touch #touch-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 10px max(16px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  background: #0b1424;
  border-top: 1px solid var(--border);
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.dpad { position: relative; width: 156px; height: 156px; flex: none; touch-action: none; }
.dpad-btn {
  position: absolute;
  width: 54px;
  height: 54px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  display: grid;
  place-items: center;
  pointer-events: none;
}
.dpad-btn svg { width: 26px; height: 26px; }
.dpad-btn.up { left: 51px; top: 0; }
.dpad-btn.down { left: 51px; bottom: 0; }
.dpad-btn.left { left: 0; top: 51px; }
.dpad-btn.right { right: 0; top: 51px; }
.dpad-btn.active { background: var(--accent); color: #04121f; border-color: var(--accent); }

.dig-buttons { display: flex; gap: 14px; }
.dig-btn {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: 2px solid var(--brick);
  background: #3a2414;
  color: #ffd9b8;
  font: 800 11px Inter, sans-serif;
  letter-spacing: 0.1em;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  touch-action: none;
}
.dig-btn svg { width: 28px; height: 28px; }
.dig-btn.active { background: var(--brick); color: #fff; }

@media (orientation: landscape) and (max-height: 540px) {
  #hud { padding-top: 4px; padding-bottom: 4px; }
  .hud-value { font-size: 12px; margin-top: 3px; }
  .icon-btn { width: 34px; height: 34px; }
  body.touch #touch-controls {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: transparent;
    border: 0;
    pointer-events: none;
    z-index: 3;
  }
  body.touch #touch-controls > * { pointer-events: auto; opacity: 0.8; }
  .dpad { width: 138px; height: 138px; }
  .dpad-btn { width: 48px; height: 48px; }
  .dpad-btn.up, .dpad-btn.down { left: 45px; }
  .dpad-btn.left, .dpad-btn.right { top: 45px; }
  .dig-btn { width: 66px; height: 66px; }
}

@media (max-width: 420px) {
  #hud { gap: 12px; }
  .hud-value { font-size: 11px; }
  .hud-label { font-size: 9px; }
}

/* ---- Modals ---- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(3, 7, 14, 0.7);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 50;
  animation: fade-in 0.2s ease-out;
}
.modal-backdrop[hidden] { display: none; }

.modal {
  width: min(500px, 100%);
  max-height: calc(100dvh - 32px);
  overflow: auto;
  padding: 24px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: linear-gradient(180deg, #13213a, #0d1729);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  animation: pop-in 0.22s ease-out;
}
.modal-wide { width: min(760px, 100%); }
.modal h2 { font-family: var(--pixel); font-size: 17px; line-height: 1.5; color: var(--gold); margin: 0 0 14px; }
.modal p { line-height: 1.55; margin: 0 0 14px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.modal-head h2 { margin: 0; }
.muted { color: var(--muted); }
.center { text-align: center; }
.danger { color: var(--danger) !important; }

.title-logo {
  font-family: var(--pixel);
  font-size: clamp(22px, 6.5vw, 38px);
  line-height: 1.3;
  text-align: center;
  color: var(--gold);
  margin: 6px 0 14px;
  text-shadow: 0 4px 0 var(--gold-deep), 0 0 30px rgba(255, 190, 60, 0.35);
}
.title-3d { color: var(--accent); text-shadow: 0 4px 0 #0b4a75, 0 0 30px rgba(63, 184, 255, 0.4); }
.title-sub { text-align: center; color: var(--muted); }

.btn {
  display: block;
  width: 100%;
  min-height: 48px;
  margin-top: 10px;
  padding: 12px 18px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font: 600 16px Inter, system-ui, sans-serif;
  cursor: pointer;
}
.btn:hover { background: #1d2f4c; }
.btn-primary {
  background: linear-gradient(180deg, #ffd25a, #ffb000);
  border-color: #ffcf4a;
  color: #1d1300;
  font-weight: 800;
}
.btn-primary:hover { background: linear-gradient(180deg, #ffdc7a, #ffbb20); }
.btn-ghost { background: transparent; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }
@media (max-width: 380px) { .btn-row { flex-direction: column; gap: 0; } }

.stats { display: flex; justify-content: space-around; gap: 12px; margin: 10px 0 16px; text-align: center; }
.stats:empty { display: none; }
.stat .k { display: block; font-size: 10px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--muted); }
.stat .v { display: block; margin-top: 6px; font-family: var(--pixel); font-size: 14px; color: var(--gold); }

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(52px, 1fr));
  gap: 8px;
  max-height: min(60dvh, 520px);
  overflow: auto;
  padding: 4px;
}
.level-btn {
  position: relative;
  aspect-ratio: 1;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font: 700 14px Inter, sans-serif;
  cursor: pointer;
}
.level-btn:hover { background: #1d2f4c; }
.level-btn.done { border-color: #b88a1a; background: #2a2210; color: var(--gold); }
.level-btn.done::after {
  content: '';
  position: absolute;
  top: 5px;
  right: 5px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--gold);
}
.level-btn.current { outline: 2px solid var(--accent); outline-offset: 1px; }

.help-list {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px 16px;
  align-items: center;
  margin: 0 0 16px;
  font-size: 14px;
}
kbd {
  display: inline-block;
  min-width: 26px;
  padding: 3px 6px;
  margin: 0 2px;
  border-radius: 6px;
  border: 1px solid var(--border);
  border-bottom-width: 3px;
  background: var(--panel);
  font: 600 12px Inter, sans-serif;
  text-align: center;
}

.noscript { padding: 24px; text-align: center; }

@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: translateY(10px) scale(0.98); } }

@media (prefers-reduced-motion: reduce) {
  .modal-backdrop, .modal { animation: none; }
  #banner { transition: none; }
}
