/* ── Root Variables ─────────────────────────────────────────── */
:root {
  --bg:          #0b0f0e;
  --surface:     #111816;
  --panel:       #162018;
  --border:      #2a4030;
  --glow:        #39ff6a;
  --glow-dim:    #1f8c3c;
  --glow-soft:   rgba(57,255,106,0.12);
  --danger:      #ff4444;
  --warn:        #ffb500;
  --player-col:  #4fc3f7;
  --text:        #c8ffd4;
  --text-dim:    #4d8a5a;
  --font-mono:   'Share Tech Mono', monospace;
  --font-head:   'Orbitron', sans-serif;
}

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

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-mono);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem 0.75rem calc(0.75rem + env(safe-area-inset-bottom));
  user-select: none;
  touch-action: none;
  overflow: hidden;
  background-image:
    radial-gradient(ellipse 80% 40% at 50% 0%, rgba(57,255,106,0.04) 0%, transparent 70%),
    repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(0,0,0,0.18) 2px, rgba(0,0,0,0.18) 4px);
}

body::after {
  content: '';
  pointer-events: none;
  position: fixed;
  inset: 0;
  background: repeating-linear-gradient(to bottom, transparent 0px, transparent 3px, rgba(0,0,0,0.07) 3px, rgba(0,0,0,0.07) 4px);
  z-index: 9999;
}

/* ── Header ─────────────────────────────────────────────────── */
.game-header {
  text-align: center;
  margin-bottom: 0.9rem;
  flex: 0 0 auto;
}

.game-title {
  font-family: var(--font-head);
  font-size: clamp(1.6rem, 6vw, 2.4rem);
  font-weight: 900;
  letter-spacing: 0.18em;
  color: var(--glow);
  text-shadow: 0 0 8px var(--glow), 0 0 24px rgba(57,255,106,0.4), 0 0 60px rgba(57,255,106,0.15);
  animation: flicker 8s infinite;
}

.game-subtitle {
  font-size: 0.65rem;
  letter-spacing: 0.3em;
  color: var(--glow-dim);
  margin-top: 0.2rem;
}

@keyframes flicker {
  0%, 95%, 100% { opacity: 1; }
  96% { opacity: 0.85; }
  97% { opacity: 1; }
  98% { opacity: 0.7; }
  99% { opacity: 1; }
}

/* ── Level Navigator ────────────────────────────────────────── */
.level-nav {
  display: flex;
  align-items: stretch;
  gap: 0.4rem;
  justify-content: center;
  margin-bottom: 0.8rem;
  flex: 0 0 auto;
}

.nav-btn, .level-current {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 0.4rem 0.8rem;
  cursor: pointer;
  transition: all 0.2s;
}

.nav-btn:hover, .level-current:hover {
  border-color: var(--glow-dim);
  color: var(--text);
}

.level-current {
  font-family: var(--font-head);
  color: var(--glow);
  text-shadow: 0 0 8px rgba(57,255,106,0.4);
  min-width: 150px;
}

/* ── Level Select Grid (Overlay) ────────────────────────────── */
.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 0.4rem;
  margin: 1rem 0;
  max-height: 50vh;
  overflow-y: auto;
  padding-right: 4px;
}

.level-btn {
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.05em;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 0.4rem 0.2rem;
  cursor: pointer;
  transition: all 0.15s;
}

.level-btn:hover { border-color: var(--glow-dim); color: var(--text); }

.level-btn.active {
  border-color: var(--glow);
  color: var(--glow);
  background: var(--glow-soft);
  box-shadow: 0 0 10px rgba(57,255,106,0.25);
}

.level-btn.solved:not(.active) { border-color: var(--glow-dim); color: var(--glow-dim); }

/* ── HUD ────────────────────────────────────────────────────── */
.hud {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 640px;
  background: var(--panel);
  border: 1px solid var(--border);
  padding: 0.55rem 0.9rem;
  margin-bottom: 0.8rem;
  gap: 0.9rem;
  flex: 0 0 auto;
  flex-wrap: wrap;
}

.hud-segment {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 58px;
}

.hud-label {
  font-size: 0.58rem;
  letter-spacing: 0.18em;
  color: var(--text-dim);
  margin-bottom: 0.12rem;
}

.hud-value {
  font-family: var(--font-head);
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--glow);
  text-shadow: 0 0 10px rgba(57,255,106,0.6);
  letter-spacing: 0.05em;
  line-height: 1;
}

/* ── Reset / Undo Buttons ───────────────────────────────────── */
.reset-btn {
  font-family: var(--font-mono);
  font-size: 1.15rem;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
  width: 42px;
  height: 42px;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.reset-btn:hover {
  border-color: var(--glow);
  color: var(--glow);
  background: var(--glow-soft);
  box-shadow: 0 0 12px rgba(57,255,106,0.3);
}
#btnRestart:hover { transform: rotate(45deg); }

/* ── Stage / Board ──────────────────────────────────────────── */
#stage {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 0;
  overflow: hidden;
}

.board-wrapper {
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 0.6rem;
  box-shadow: 0 0 0 1px rgba(57,255,106,0.05), 0 0 40px rgba(0,0,0,0.6), inset 0 0 30px rgba(0,0,0,0.4);
  position: relative;
  touch-action: none;
}

.corner {
  position: absolute;
  width: 8px;
  height: 8px;
  border-color: var(--glow-dim);
  border-style: solid;
}
.corner.tl { top: 4px; left: 4px; border-width: 1px 0 0 1px; }
.corner.tr { top: 4px; right: 4px; border-width: 1px 1px 0 0; }
.corner.bl { bottom: 4px; left: 4px; border-width: 0 0 1px 1px; }
.corner.br { bottom: 4px; right: 4px; border-width: 0 1px 1px 0; }

#board {
  display: grid;
  gap: 2px;
  touch-action: none;
}

/* ── Cells ──────────────────────────────────────────────────── */
.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.cell.wall {
  background: repeating-linear-gradient(45deg, var(--panel), var(--panel) 6px, var(--surface) 6px, var(--surface) 12px);
  border: 1px solid var(--border);
}

.cell.floor, .cell.goal {
  background: #0d1410;
  border: 1px solid #1a2a1e;
}

.cell.goal::after {
  content: "";
  width: 38%;
  height: 38%;
  border-radius: 50%;
  border: 2px solid var(--glow);
  box-shadow: 0 0 8px rgba(57,255,106,0.5);
  box-sizing: border-box;
}

.cell.boxOnGoal::after,
.cell.goal:has(.player)::after {
  display: none;
}

.cell .box {
  position: absolute;
  inset: 10%;
  background: #241a08;
  border: 2px solid var(--warn);
  box-shadow: inset 0 0 8px rgba(255,181,0,0.25), 0 0 6px rgba(255,181,0,0.2);
}

.cell.boxOnGoal .box {
  background: #0a2412;
  border-color: var(--glow);
  box-shadow: inset 0 0 10px rgba(57,255,106,0.35), 0 0 14px rgba(57,255,106,0.6);
  animation: crate-lock 0.3s ease-out;
}

@keyframes crate-lock {
  0% { background: rgba(57,255,106,0.3); }
  100% { background: #0a2412; }
}

.cell .player {
  position: absolute;
  inset: 14%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #e1f7ff, var(--player-col));
  box-shadow: 0 0 10px rgba(79,195,247,0.8);
  z-index: 2;
  transition: transform 0.08s ease;
}

/* ── Status Bar ─────────────────────────────────────────────── */
.status-bar {
  width: 100%;
  max-width: 640px;
  margin-top: 0.6rem;
  font-size: 0.62rem;
  letter-spacing: 0.12em;
  color: var(--text-dim);
  display: flex;
  justify-content: space-between;
  flex: 0 0 auto;
  gap: 8px;
}

/* ── D-Pad ──────────────────────────────────────────────────── */
#dpadWrap {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  padding-top: 10px;
}

#dpad {
  display: grid;
  grid-template-columns: 54px 54px 54px;
  grid-template-rows: 54px 54px;
  gap: 6px;
  grid-template-areas:
    ".    up   ."
    "left down right";
}

#dpad button[data-dir="up"] { grid-area: up; }
#dpad button[data-dir="left"] { grid-area: left; }
#dpad button[data-dir="down"] { grid-area: down; }
#dpad button[data-dir="right"] { grid-area: right; }

.pad {
  background: var(--panel);
  color: var(--glow);
  border: 1px solid var(--border);
  font-size: 1.3rem;
  font-family: var(--font-mono);
  cursor: pointer;
  touch-action: none;
  transition: all 0.15s;
}
.pad:hover { border-color: var(--glow-dim); }
.pad:active { background: var(--glow-soft); box-shadow: 0 0 12px rgba(57,255,106,0.3); }

body.no-touch #dpadWrap { display: none; }

/* ── Overlay / Dialog ───────────────────────────────────────── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 16px;
}
.overlay.hidden { display: none; }

.dialog-panel {
  background: var(--surface);
  border: 1px solid var(--glow-dim);
  box-shadow: 0 0 40px rgba(57,255,106,0.25);
  padding: 1.6rem 1.8rem;
  max-width: 360px;
  width: 100%;
  text-align: center;
  font-family: var(--font-mono);
}

.dialog-panel.wide { max-width: 560px; }

.dialog-icon {
  font-size: 2.6rem;
  margin-bottom: 0.4rem;
  display: block;
}

.dialog-headline {
  font-family: var(--font-head);
  font-size: 1.05rem;
  letter-spacing: 0.18em;
  margin-bottom: 0.4rem;
}
.dialog-headline.win { color: var(--glow); text-shadow: 0 0 12px var(--glow); }

.dialog-sub {
  font-size: 0.75rem;
  color: var(--text-dim);
  letter-spacing: 0.08em;
  margin-bottom: 1.1rem;
}

.dialog-buttons {
  display: flex;
  gap: 0.6rem;
  justify-content: center;
  flex-wrap: wrap;
}

.ui-btn {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  letter-spacing: 0.1em;
  background: transparent;
  border: 1px solid var(--glow-dim);
  color: var(--glow);
  padding: 0.5rem 1.1rem;
  cursor: pointer;
  transition: all 0.15s;
}
.ui-btn:hover { background: var(--glow-soft); box-shadow: 0 0 12px rgba(57,255,106,0.3); }
.ui-btn:disabled { opacity: 0.35; cursor: default; box-shadow: none; background: transparent; }

@media (min-width: 700px) {
  .game-title { font-size: 2.6rem; }
}
