:root {
  --bg: #0b1020;
  --panel: rgba(15, 23, 42, 0.94);
  --border: rgba(127, 211, 255, 0.22);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #6366f1;
}

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

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

.app-shell {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.topbar {
  height: 46px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(11, 16, 32, 0.9);
}

.top-link {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 600;
  padding: 6px 10px;
  border-radius: 8px;
}

.top-link:hover { color: var(--text); background: rgba(255, 255, 255, 0.06); }

.top-title {
  font-weight: 800;
  font-size: 0.96rem;
  letter-spacing: -0.01em;
}

.top-actions { display: flex; gap: 6px; }

.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.icon-btn.hidden { display: none; }

.stage-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 8px 10px 12px;
}

.canvas-frame {
  position: relative;
  width: min(100%, 512px, calc((100dvh - 68px) * 512 / 576));
  aspect-ratio: 512 / 576;
  border-radius: 14px;
  overflow: hidden;
  border: 2px solid var(--border);
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.55);
  background: #0f172a;
}

#game-canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  background: rgba(8, 12, 24, 0.84);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  overflow-y: auto;
}

.overlay.hidden { display: none; }

.panel {
  width: 100%;
  max-width: 420px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 20px 18px;
  text-align: center;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.45);
}

.logo-emoji { font-size: 2.6rem; margin-bottom: 6px; }

.game-title {
  font-size: 1.7rem;
  font-weight: 900;
  letter-spacing: -0.02em;
  background: linear-gradient(120deg, #7fd3ff, #f472b6);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.panel h2 { font-size: 1.25rem; font-weight: 800; margin-bottom: 12px; }
.subtitle { color: var(--muted); font-size: 0.9rem; margin-bottom: 16px; }
.hint { color: var(--muted); font-size: 0.76rem; margin-top: 14px; line-height: 1.4; }

.stack { display: flex; flex-direction: column; gap: 10px; }

.btn {
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 800;
  font-size: 0.95rem;
  cursor: pointer;
  color: #fff;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.btn:active { transform: scale(0.97); }
.btn.primary { background: linear-gradient(135deg, #38bdf8, #6366f1); }
.btn.secondary { background: #1e293b; border: 1px solid var(--border); }
.btn.ghost { background: transparent; color: var(--muted); border: 1px solid rgba(255,255,255,0.12); }
.btn.lg { padding: 14px 18px; font-size: 1.04rem; }

.row-actions {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 14px;
}

.slot-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 12px;
}

.tab {
  padding: 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.04);
  color: var(--muted);
  font-weight: 700;
  cursor: pointer;
}

.tab.active { background: rgba(56, 189, 248, 0.2); color: #fff; border-color: var(--accent); }

.flavor-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.flavor-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px 4px;
  border-radius: 12px;
  border: 2px solid rgba(255,255,255,0.1);
  background: rgba(255,255,255,0.04);
  color: var(--text);
  cursor: pointer;
}

.flavor-card.sel-p1 { border-color: #38bdf8; background: rgba(56, 189, 248, 0.15); }
.flavor-card.sel-p2 { border-color: #fb923c; background: rgba(251, 146, 60, 0.15); }
.flavor-card:disabled { opacity: 0.4; cursor: not-allowed; }

.flavor-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 3px solid;
}

.flavor-name { font-size: 0.72rem; font-weight: 700; }

.badge {
  position: absolute;
  top: 4px;
  right: 4px;
  font-size: 0.62rem;
  font-weight: 800;
  padding: 1px 5px;
  border-radius: 6px;
}
.badge.p1 { background: #38bdf8; color: #0f172a; }
.badge.p2 { background: #fb923c; color: #0f172a; }

.world-list { display: flex; flex-direction: column; gap: 10px; }

.world-card {
  text-align: left;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid var(--border);
  border-left: 5px solid var(--accent, #38bdf8);
  background: rgba(255,255,255,0.04);
  color: var(--text);
  cursor: pointer;
}
.world-card.locked { opacity: 0.45; cursor: not-allowed; }
.world-head { display: flex; justify-content: space-between; font-size: 0.78rem; color: var(--muted); font-weight: 700; }
.world-title { font-size: 1.05rem; font-weight: 800; margin-top: 2px; }
.world-sub { font-size: 0.82rem; color: var(--muted); }

.level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}

.level-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 4px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.05);
  color: var(--text);
  cursor: pointer;
}
.level-btn.done { border-color: #22c55e; background: rgba(34, 197, 94, 0.14); }
.level-btn.locked { opacity: 0.4; cursor: not-allowed; }
.lvl-num { font-weight: 800; font-size: 0.92rem; }
.lvl-meta { font-size: 0.65rem; color: var(--muted); margin-top: 2px; }

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 12px 0 14px;
}
.stats > div {
  background: rgba(255,255,255,0.05);
  border-radius: 10px;
  padding: 10px;
}
.stats span { display: block; font-size: 0.74rem; color: var(--muted); }
.stats strong { font-size: 1.15rem; color: #7fd3ff; }

.unlock-banner {
  background: rgba(250, 204, 21, 0.15);
  border: 1px solid rgba(250, 204, 21, 0.45);
  color: #fde047;
  font-weight: 800;
  padding: 8px;
  border-radius: 10px;
  margin-bottom: 12px;
}

/* Mobile Touch Controls */
.touch-controls {
  display: none;
  width: 100%;
  max-width: 512px;
  align-items: center;
  justify-content: space-between;
  padding: 4px 12px 6px;
  touch-action: none;
}

/* Show touch controls on touch devices or narrow mobile viewports during gameplay */
@media (pointer: coarse), (max-width: 768px) {
  .touch-controls.playing {
    display: flex;
  }

  .canvas-frame {
    width: min(100%, 460px, calc((100dvh - 216px) * 512 / 576));
  }
}

.dpad {
  position: relative;
  width: 138px;
  height: 138px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 4px;
  touch-action: none;
}

.dpad-btn {
  border-radius: 12px;
  border: 1px solid rgba(127, 211, 255, 0.28);
  background: rgba(30, 41, 59, 0.92);
  color: #e2e8f0;
  font-size: 1.1rem;
  display: grid;
  place-items: center;
  pointer-events: none; /* Parent dpad handles unified slide pointer events */
}

.dpad-btn.active {
  background: #38bdf8;
  color: #0f172a;
  transform: scale(0.95);
}

.dpad-btn.up { grid-column: 2; grid-row: 1; }
.dpad-btn.left { grid-column: 1; grid-row: 2; }
.dpad-center { grid-column: 2; grid-row: 2; border-radius: 10px; background: rgba(15, 23, 42, 0.8); }
.dpad-btn.right { grid-column: 3; grid-row: 2; }
.dpad-btn.down { grid-column: 2; grid-row: 3; }

.ice-btn {
  width: 106px;
  height: 106px;
  border-radius: 50%;
  border: 3px solid #bae6fd;
  background: radial-gradient(circle at 30% 30%, #7dd3fc, #0284c7);
  color: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  box-shadow: 0 10px 26px rgba(2, 132, 199, 0.45);
  touch-action: none;
  cursor: pointer;
}

.ice-btn.active {
  transform: scale(0.93);
  filter: brightness(1.15);
}

.ice-icon { font-size: 1.9rem; line-height: 1; }
.ice-label { font-size: 0.78rem; font-weight: 900; letter-spacing: 0.06em; }

/* Mobile Landscape layout: put controls on sides */
@media (max-height: 540px) and (orientation: landscape) {
  .stage-wrap {
    flex-direction: row;
    justify-content: center;
    padding: 4px 12px;
  }
  .canvas-frame {
    width: auto;
    height: calc(100dvh - 58px);
  }
  .touch-controls.playing {
    position: fixed;
    inset: 46px 10px 8px 10px;
    max-width: none;
    pointer-events: none;
    align-items: flex-end;
  }
  .dpad, .ice-btn { pointer-events: auto; }
}
