:root {
  --bg: #171b26;
  --bg2: #1f2432;
  --panel: #262c3c;
  --panel-hi: #39415a;
  --panel-lo: #161a24;
  --text: #e8eaf0;
  --muted: #9aa3b5;
  --accent: #ffc93c;
  --accent-dark: #b8860b;
  --safe: #8fbc65;
  --danger: #ff6b6b;
  --hint: #5dade2;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  background-image:
    linear-gradient(rgba(255,255,255,.015) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,.015) 1px, transparent 1px);
  background-size: 4px 4px;
  color: var(--text);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  overflow-x: hidden;
}

/* ---------- 像素風按鈕：方角 + 硬邊斜面 ---------- */
.ctl, .icon-btn, .tier-tabs button, .level-list button, #dpad button {
  border-radius: 0;
  border: 2px solid var(--panel-lo);
  background: var(--panel);
  color: var(--text);
  box-shadow:
    inset 2px 2px 0 var(--panel-hi),
    inset -2px -2px 0 var(--panel-lo),
    3px 3px 0 rgba(0,0,0,.45);
  cursor: pointer;
  font-family: inherit;
  image-rendering: pixelated;
}
.ctl:active, .icon-btn:active, .tier-tabs button:active,
.level-list button:active, #dpad button:active {
  transform: translate(2px, 2px);
  box-shadow:
    inset 2px 2px 0 var(--panel-lo),
    inset -2px -2px 0 var(--panel-hi),
    1px 1px 0 rgba(0,0,0,.45);
}
.ctl.primary, .tier-tabs button.active {
  background: var(--accent);
  color: #221803;
  font-weight: 700;
  box-shadow:
    inset 2px 2px 0 #ffe08a,
    inset -2px -2px 0 var(--accent-dark),
    3px 3px 0 rgba(0,0,0,.45);
}
.ctl:disabled { opacity: .45; cursor: default; }

header {
  width: 100%;
  max-width: 1080px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 6px;
}
h1 { font-size: 1.3rem; letter-spacing: 2px; text-shadow: 3px 3px 0 rgba(0,0,0,.5); }
.header-btns { display: flex; gap: 8px; }
.icon-btn { font-size: 1rem; padding: 6px 10px; }

/* ---------- 版面：手機直式；桌機雙欄 ---------- */
#layout {
  width: 100%;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  padding: 6px 12px 24px;
  gap: 8px;
}
.pblock { width: 100%; }
#block-tabs { order: 1; }
#block-levels { order: 2; }
#block-hud { order: 3; }
#block-stage { order: 4; }
#block-message { order: 5; }
#block-controls { order: 6; }
#block-dpad { order: 7; }

@media (min-width: 960px) {
  #layout {
    max-width: 1080px;
    display: grid;
    grid-template-columns: 280px 1fr;
    grid-template-areas:
      "tabs   stage"
      "levels stage"
      "hud    stage"
      "ctl    stage"
      "msg    msg"
      "dpad   dpad";
    grid-template-rows: auto auto auto auto auto;
    align-items: start;
    gap: 12px;
  }
  #block-tabs { grid-area: tabs; }
  #block-levels { grid-area: levels; }
  #block-hud { grid-area: hud; }
  #block-controls { grid-area: ctl; }
  #block-stage { grid-area: stage; }
  #block-message { grid-area: msg; }
  #block-dpad { grid-area: dpad; display: none; }
  #controls { flex-direction: column; }
  #controls .ctl { width: 100%; }
}

.tier-tabs { display: flex; gap: 8px; }
.tier-tabs button {
  flex: 1;
  padding: 9px 0;
  color: var(--muted);
  font-size: .95rem;
}
.tier-tabs button.active { color: #221803; }

.level-list { display: flex; gap: 8px; flex-wrap: wrap; }
.level-list button {
  width: 42px; height: 42px;
  font-size: .95rem;
  position: relative;
}
.level-list button.active { outline: 2px solid var(--accent); outline-offset: 1px; }
.level-list button.done::after {
  content: "✓";
  position: absolute;
  top: -7px; right: -7px;
  font-size: .7rem;
  color: #14351f;
  background: var(--safe);
  border: 2px solid var(--panel-lo);
  width: 16px; height: 16px;
  line-height: 12px;
}

#hud {
  display: flex;
  gap: 18px;
  align-items: center;
  color: var(--muted);
  font-size: .95rem;
  padding: 2px 2px;
}
#hud b { color: var(--text); }
#hud-level { font-weight: 700; color: var(--accent); }

#block-stage { display: flex; justify-content: center; }
#board-wrap {
  position: relative;
  border: 3px solid var(--panel-lo);
  box-shadow:
    inset 2px 2px 0 rgba(255,255,255,.05),
    6px 6px 0 rgba(0,0,0,.4);
  background: #10131b;
  padding: 8px;
  line-height: 0;
}
#board { image-rendering: pixelated; display: block; }

#overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 12, 16, .72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
#overlay.hidden { display: none; }
#overlay-card {
  background: var(--panel);
  border: 3px solid var(--panel-lo);
  box-shadow:
    inset 2px 2px 0 var(--panel-hi),
    6px 6px 0 rgba(0,0,0,.5);
  padding: 22px 30px;
  text-align: center;
  line-height: 1.4;
}
#overlay-emoji { font-size: 2.4rem; }
#overlay-text { margin: 10px 0 16px; font-size: 1.05rem; white-space: pre-line; }
#overlay-buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
#overlay-buttons .ctl { padding: 9px 14px; font-size: .95rem; }

#message {
  min-height: 1.5em;
  color: var(--muted);
  font-size: .92rem;
  text-align: center;
}
#controls { display: flex; gap: 10px; justify-content: center; }
.ctl { padding: 10px 16px; font-size: .95rem; }

#dpad { display: none; flex-direction: column; align-items: center; gap: 8px; }
#dpad button {
  width: 58px; height: 48px;
  font-size: 1.1rem;
}
.dpad-mid { display: flex; gap: 8px; }
@media (pointer: coarse) and (max-width: 959px) { #dpad { display: flex; } }

/* ---------- 全螢幕火焰 ---------- */
#fire {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  z-index: 100;
  pointer-events: none;
  image-rendering: pixelated;
}
#fire.hidden { display: none; }

dialog {
  background: var(--panel);
  color: var(--text);
  border: 3px solid var(--panel-lo);
  box-shadow:
    inset 2px 2px 0 var(--panel-hi),
    8px 8px 0 rgba(0,0,0,.5);
  padding: 24px;
  max-width: 440px;
  margin: auto;
}
dialog::backdrop { background: rgba(10, 12, 16, .7); }
dialog h2 { margin-bottom: 12px; letter-spacing: 2px; }
dialog ul { list-style: none; display: flex; flex-direction: column; gap: 10px; margin-bottom: 16px; line-height: 1.5; font-size: .95rem; }
