:root {
  --bg: #050816;
  --panel: #0e1530;
  --panel-2: #141d3f;
  --line: #27345f;
  --text: #eef2ff;
  --muted: #9aa6cf;
  --cyan: #22d3ee;
  --blue: #38bdf8;
  --green: #22c55e;
  --amber: #f59e0b;
  --red: #ef4444;
  --violet: #c4b5fd;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(circle at 50% -10%, #1b2560 0%, var(--bg) 55%) fixed, var(--bg);
  color: var(--text);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  line-height: 1.55;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }

.screen { min-height: 100dvh; padding: calc(16px + var(--safe-t)) 16px calc(16px + var(--safe-b)); }

.btn {
  min-height: 44px; min-width: 44px; padding: 10px 18px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--panel-2);
  cursor: pointer; font-weight: 600; transition: transform .08s, background .15s, border-color .15s;
}
.btn:hover { border-color: #4a5ca0; background: #1b2652; }
.btn:active { transform: scale(.97); }
.btn:focus-visible, .level-card:focus-visible, .mode-switch button:focus-visible, .dash-btn:focus-visible, .toggle input:focus-visible + span {
  outline: 3px solid #fde047; outline-offset: 2px;
}
.btn.primary { background: linear-gradient(135deg, #0ea5e9, #6366f1); border-color: transparent; color: #fff; box-shadow: 0 6px 24px rgba(56, 189, 248, .25); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.big { font-size: 1.25rem; padding: 14px 36px; min-height: 56px; }
.btn.ghost { background: transparent; }
.btn.danger { background: #3a0f17; border-color: #7f1d1d; color: #fecaca; }
.btn.icon { padding: 0; width: 44px; display: inline-grid; place-items: center; font-size: 1.05rem; }

.home { display: grid; place-items: center; }
.home-card { text-align: center; max-width: 520px; width: 100%; }
.eyebrow { color: var(--cyan); letter-spacing: .14em; font-size: .85rem; font-weight: 700; margin: 0 0 .4em; }
.home h1 { font-size: clamp(2.6rem, 11vw, 4.2rem); letter-spacing: .08em; text-shadow: 0 0 24px rgba(56, 189, 248, .55); }
.tagline { color: var(--muted); margin: 0 0 1.6em; }
.home-progress { color: var(--muted); font-size: .92rem; min-height: 1.4em; }
.home-links { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.storage-warning { color: #fcd34d; font-size: .9rem; margin-top: 1em; }

.logo-grid { display: grid; grid-template-columns: repeat(4, 34px); gap: 6px; justify-content: center; margin: 0 auto 18px; }
.logo-grid i { width: 34px; height: 34px; border-radius: 7px; background: #0c1430; border: 1px solid #1f2b55; }
.logo-grid .g { border-color: var(--green); background: rgba(34, 197, 94, .18); box-shadow: 0 0 12px rgba(34, 197, 94, .5); }
.logo-grid .b { border-color: var(--blue); background: rgba(56, 189, 248, .2); box-shadow: 0 0 12px rgba(56, 189, 248, .5); }
.logo-grid .r { border-color: var(--red); background: rgba(239, 68, 68, .3); box-shadow: 0 0 12px rgba(239, 68, 68, .5); }
.logo-grid .a { border-color: var(--amber); background: repeating-linear-gradient(135deg, rgba(245, 158, 11, .45) 0 5px, transparent 5px 10px); }
.logo-grid .x { border-color: #f5f3ff; background: rgba(196, 181, 253, .35); box-shadow: 0 0 12px rgba(196, 181, 253, .6); }

.top-bar { display: flex; align-items: center; gap: 12px; max-width: 960px; margin: 0 auto 12px; }
.top-bar h2 { margin: 0; font-size: 1.4rem; }
.spacer { flex: 1; }

.mode-switch { display: flex; max-width: 960px; margin: 0 auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 4px; gap: 4px; width: fit-content; }
.mode-switch button { min-height: 44px; padding: 8px 16px; border: 0; border-radius: 9px; background: transparent; cursor: pointer; font-weight: 600; color: var(--muted); }
.mode-switch button[aria-checked="true"] { background: #23306a; color: #fff; }
.mode-note { max-width: 960px; margin: 10px auto; color: var(--muted); font-size: .9rem; }

.level-grid { max-width: 960px; margin: 0 auto; display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 12px; }
.level-card {
  text-align: left; padding: 14px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--panel);
  cursor: pointer; display: grid; gap: 4px; min-height: 120px; position: relative;
}
.level-card:hover:not([disabled]) { border-color: var(--blue); }
.level-card[disabled] { opacity: .5; cursor: not-allowed; }
.level-card .num { font-size: .8rem; color: var(--cyan); font-weight: 700; }
.level-card .name { font-size: 1.1rem; font-weight: 700; }
.level-card .zone { font-size: .8rem; color: var(--muted); }
.level-card .stars { font-size: 1.15rem; letter-spacing: 2px; color: #fde047; }
.level-card .stars .off { color: #3a4470; }
.level-card .sub { font-size: .8rem; color: var(--muted); }
.level-card .lock { position: absolute; right: 12px; top: 12px; font-size: .8rem; color: var(--muted); }

.game { padding: 0; height: 100dvh; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }
.hud {
  display: flex; align-items: center; gap: 10px; padding: calc(8px + var(--safe-t)) 12px 8px;
  background: rgba(8, 12, 30, .85); border-bottom: 1px solid var(--line);
}
.hud-level { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.hud-zone { font-size: .72rem; color: var(--cyan); }
.hud-level strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-stats { display: flex; align-items: center; gap: 12px; }
.hud-time { font-variant-numeric: tabular-nums; font-size: 1.35rem; font-weight: 800; min-width: 3ch; text-align: right; }
.hud-time.low { color: #fca5a5; }
.hud-lives { display: flex; gap: 3px; font-size: 1.2rem; }
.hud-lives .on { color: #fb7185; }
.hud-lives .off { color: #3a4470; }
.hud-buttons { display: flex; gap: 6px; }

.game-main {
  flex: 1; min-height: 0; display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-rows: minmax(0, 1fr) auto auto;
  grid-template-areas: "board board board" "status status status" "dpad . dash";
  padding: 8px 12px calc(10px + var(--safe-b)); gap: 8px;
}
.board-wrap { grid-area: board; position: relative; min-height: 0; min-width: 0; touch-action: none; }
#board { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.status-row { grid-area: status; display: flex; gap: 8px; align-items: center; justify-content: center; flex-wrap: wrap; }
.chip { padding: 4px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); font-weight: 700; font-size: .95rem; font-variant-numeric: tabular-nums; }
.energy-chip { border-color: #1e6a8f; color: #bae6fd; }
.mode-chip { color: var(--violet); }
.dash-chip.ready { color: #a5f3fc; border-color: #0e7490; }
.kbd-hint { color: var(--muted); font-size: .82rem; }

.dpad, .dash-btn { display: none; }
body.touch .kbd-hint { display: none; }
body.touch .dpad {
  grid-area: dpad; display: grid; position: relative; width: 168px; height: 168px; border-radius: 50%;
  background: radial-gradient(circle, #141d3f 0 30%, #0e1530 31%); border: 1px solid var(--line);
  touch-action: none; user-select: none; -webkit-user-select: none; justify-self: start;
}
.dpad .arrow { position: absolute; width: 58px; height: 58px; display: grid; place-items: center; font-size: 1.3rem; color: #93a3d8; border-radius: 14px; pointer-events: none; }
.dpad .arrow.on { color: #fff; background: rgba(56, 189, 248, .3); }
.dpad .up { left: 55px; top: 2px; }
.dpad .down { left: 55px; bottom: 2px; }
.dpad .left { left: 2px; top: 55px; }
.dpad .right { right: 2px; top: 55px; }
body.touch .dash-btn {
  grid-area: dash; display: grid; place-items: center; position: relative; width: 96px; height: 96px; border-radius: 50%;
  border: 2px solid #0e7490; background: #0b2a3a; color: #cffafe; font-weight: 800; font-size: 1.05rem;
  justify-self: end; align-self: center; touch-action: none; user-select: none; -webkit-user-select: none; cursor: pointer;
}
.dash-btn[disabled] { opacity: .35; }
.dash-ring { position: absolute; inset: -2px; border-radius: 50%; background: conic-gradient(rgba(5, 8, 22, .75) calc(var(--cd, 0) * 1turn), transparent 0); pointer-events: none; }
.dash-label { position: relative; }
body.left-handed .game-main { grid-template-areas: "board board board" "status status status" "dash . dpad"; }
body.left-handed .dpad { justify-self: end; }
body.left-handed .dash-btn { justify-self: start; }

@media (orientation: landscape) and (max-height: 560px) {
  body.touch .game-main {
    grid-template-columns: 180px minmax(0, 1fr) 120px;
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: "dpad board dash" "dpad status dash";
  }
  body.touch.left-handed .game-main { grid-template-columns: 120px minmax(0, 1fr) 180px; grid-template-areas: "dash board dpad" "dash status dpad"; }
  body.touch .dpad { align-self: center; }
  .hud { padding-top: 4px; padding-bottom: 4px; }
}

.banner {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%); max-width: 92%;
  padding: 6px 16px; border-radius: 999px; background: rgba(8, 12, 30, .88); border: 1px solid var(--line);
  font-weight: 700; text-align: center; pointer-events: none; opacity: 0; transition: opacity .2s;
}
.banner.show { opacity: 1; }
.banner.good { border-color: var(--violet); color: #ede9fe; }
.banner.bad { border-color: var(--red); color: #fecaca; }
.countdown { position: absolute; inset: 0; display: grid; place-items: center; font-size: clamp(3rem, 18vw, 7rem); font-weight: 900; pointer-events: none; text-shadow: 0 0 30px rgba(56, 189, 248, .8); }

.overlay { position: fixed; inset: 0; z-index: 20; background: rgba(2, 4, 12, .72); display: grid; place-items: center; padding: 16px; backdrop-filter: blur(4px); }
.panel { width: min(560px, 100%); max-height: calc(100dvh - 32px); overflow: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 22px; box-shadow: 0 20px 60px rgba(0, 0, 0, .5); }
.panel.small { width: min(400px, 100%); }
.panel h2 { font-size: 1.5rem; }
.panel h3 { margin-top: 1.1em; font-size: 1.05rem; color: var(--cyan); }
.lead { font-size: 1.05rem; }
.muted { color: var(--muted); }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 18px; }
.actions.column { flex-direction: column; }
.actions.column .btn { width: 100%; }
.badges { display: flex; gap: 6px; flex-wrap: wrap; margin: 0 0 8px; }
.badge { font-size: .8rem; padding: 2px 10px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel-2); }
.badge.new { border-color: var(--amber); color: #fde68a; }
.badge.assist { border-color: var(--violet); color: #ddd6fe; }
.badge.practice { border-color: var(--green); color: #bbf7d0; }
.facts { padding-left: 1.2em; margin: .6em 0; }
.facts li { margin: .25em 0; }

.legend { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; margin: 12px 0; }
.legend-item { display: flex; align-items: center; gap: 8px; font-size: .9rem; }
.legend-item b { display: block; }
.legend-item small { color: var(--muted); }
.swatch { flex: none; width: 34px; height: 34px; border-radius: 7px; border: 2px solid; display: grid; place-items: center; font-size: .95rem; font-weight: 800; }
.swatch.floor { border-color: #1f2b55; background: #0c1430; }
.swatch.safe { border-color: var(--green); background: rgba(34, 197, 94, .16); color: var(--green); }
.swatch.energy { border-color: var(--blue); background: rgba(56, 189, 248, .16); color: #bae6fd; }
.swatch.warn { border-color: var(--amber); background: repeating-linear-gradient(135deg, rgba(245, 158, 11, .5) 0 5px, transparent 5px 10px); }
.swatch.danger { border-color: var(--red); background: rgba(239, 68, 68, .42); color: #fee2e2; }
.swatch.exit { border-color: #f5f3ff; background: rgba(196, 181, 253, .32); color: #fff; }
.swatch.wall { border-color: #3b4a85; background: #222d55; }

.stars-big { font-size: 2.6rem; letter-spacing: 6px; color: #fde047; text-align: center; margin: 4px 0 8px; }
.stars-big .off { color: #3a4470; }
.result-table { width: 100%; border-collapse: collapse; margin: 8px 0; font-variant-numeric: tabular-nums; }
.result-table td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
.result-table td:last-child { text-align: right; }
.result-table tr.total td { font-weight: 800; font-size: 1.1rem; border-bottom: 0; }
.cond { list-style: none; padding: 0; margin: 8px 0; }
.cond li { padding: 4px 0; }
.cond .ok { color: #86efac; }
.cond .no { color: #94a3b8; }
.new-best { display: inline-block; padding: 2px 10px; border-radius: 999px; background: #3b2a05; color: #fde68a; border: 1px solid #a16207; font-weight: 700; font-size: .85rem; }
.fail-reason { font-size: 1.1rem; }

.toggle { display: flex; gap: 12px; align-items: flex-start; padding: 12px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.toggle input { width: 24px; height: 24px; margin: 2px 0 0; accent-color: var(--blue); flex: none; }
.toggle small { display: block; color: var(--muted); }
.danger-zone { margin-top: 16px; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
body.reduced * { transition: none !important; }
