:root {
  --bg: #060c1d;
  --bg2: #0a1430;
  --panel: rgba(9, 18, 42, 0.86);
  --line: rgba(96, 165, 250, 0.22);
  --text: #e6f0ff;
  --muted: #8ea3c9;
  --cyan: #38e1ff;
  --cyan2: #22d3ee;
  --gold: #ffd166;
  --green: #34d399;
  --red: #ff3b6b;
  --hud-h: 56px;
  --touch-h: 0px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: ui-sans-serif, system-ui, -apple-system, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; overscroll-behavior: none; }
body {
  background:
    radial-gradient(1200px 700px at 50% 40%, #0d1d45 0%, rgba(13, 29, 69, 0) 70%),
    linear-gradient(180deg, #050a18, #071027 60%, #050a18);
  touch-action: none;
  user-select: none; -webkit-user-select: none;
}
body::before {
  content: ""; position: fixed; top: 0; right: 0; bottom: 0; left: 0; pointer-events: none; opacity: .5;
  background-image: linear-gradient(rgba(80,140,255,.05) 1px, transparent 1px), linear-gradient(90deg, rgba(80,140,255,.05) 1px, transparent 1px);
  background-size: 32px 32px;
  mask-image: radial-gradient(ellipse at 50% 45%, #000 30%, transparent 80%);
}
button { font: inherit; color: inherit; cursor: pointer; }
svg { display: block; }

#app { position: fixed; top: 0; right: 0; bottom: 0; left: 0; display: flex; flex-direction: column; padding-top: var(--safe-t); }

/* ---------- HUD ---------- */
#hud {
  height: var(--hud-h); flex: none; display: flex; align-items: center; gap: 10px;
  padding: 0 12px; position: relative; z-index: 3;
  background: linear-gradient(180deg, rgba(6,12,29,.92), rgba(6,12,29,.6));
  border-bottom: 1px solid var(--line);
}
.hud-left, .hud-right { display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0; }
.hud-right { justify-content: flex-end; }
.hud-mid { display: flex; align-items: center; gap: 10px; flex: none; }
.lvl { display: flex; align-items: baseline; gap: 8px; min-width: 0; margin-left: 4px; }
.lvl-code { font-weight: 800; color: var(--cyan); font-variant-numeric: tabular-nums; letter-spacing: .04em; }
.lvl-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.loop-label { font-weight: 800; letter-spacing: .04em; font-size: 15px; white-space: nowrap; }
.echo-dots { display: flex; gap: 4px; }
.echo-dots i {
  width: 16px; height: 16px; border-radius: 50%; border: 2px solid currentColor; display: grid; place-items: center;
  font: 800 9px/1 ui-sans-serif, system-ui, sans-serif; font-style: normal; background: rgba(6,12,29,.8);
}
.key-chip { color: #fbbf24; width: 26px; height: 26px; border-radius: 8px; display: grid; place-items: center; background: rgba(251,191,36,.12); border: 1px solid rgba(251,191,36,.5); }
.key-chip[hidden] { display: none; }
.key-chip svg { width: 18px; height: 18px; }
.time-left { font: 800 15px/1 ui-monospace, "SF Mono", Menlo, Consolas, monospace; color: var(--text); min-width: 44px; text-align: right; margin-right: 4px; }
.time-left.low { color: var(--red); }
.icon-btn {
  width: 38px; height: 38px; flex: none; border-radius: 10px; border: 1px solid var(--line);
  background: rgba(15, 30, 66, .6); display: grid; place-items: center; color: #cfe3ff;
  transition: background .15s, transform .1s, border-color .15s;
}
.icon-btn svg { width: 20px; height: 20px; fill: currentColor; }
.icon-btn:hover { background: rgba(40, 80, 160, .55); border-color: rgba(56,225,255,.5); }
.icon-btn:active { transform: scale(.94); }
.icon-btn:disabled { opacity: .35; pointer-events: none; }
#btnSound .mute { display: none; }
#btnSound.off .mute { display: block; }
#btnSound.off .wave { display: none; }

#timer { height: 6px; flex: none; position: relative; background: rgba(20, 40, 90, .55); z-index: 3; }
#timer .fill { position: absolute; top: 0; bottom: 0; left: 0; width: 100%; background: linear-gradient(90deg, var(--cyan2), #7dd3fc); box-shadow: 0 0 12px rgba(56,225,255,.7); transform-origin: left; }
#timer .fill.low { background: linear-gradient(90deg, #ff3b6b, #ff8aa5); box-shadow: 0 0 12px rgba(255,59,107,.8); }
#timer .marks i { position: absolute; top: -3px; width: 3px; height: 12px; border-radius: 2px; transform: translateX(-1px); }

/* ---------- stage ---------- */
#stage { position: relative; flex: 1; min-height: 0; overflow: hidden; }
#cv { position: absolute; top: 0; right: 0; bottom: 0; left: 0; display: block; }
#banner {
  position: absolute; left: 50%; top: 38%; transform: translate(-50%, -50%); text-align: center; pointer-events: none;
  opacity: 0; transition: opacity .35s; white-space: nowrap;
}
#banner.show { opacity: 1; }
#banner .b-code { font: 800 14px/1 ui-monospace, Menlo, monospace; letter-spacing: .4em; color: var(--cyan); margin-bottom: 10px; }
#banner .b-name { font-size: clamp(30px, 7vw, 54px); font-weight: 900; letter-spacing: .08em; text-shadow: 0 0 24px rgba(56,225,255,.6), 0 2px 0 #000; }
#toast {
  position: absolute; left: 50%; top: 56px; z-index: 2; transform: translate(-50%, -8px); padding: 8px 16px; border-radius: 999px;
  background: rgba(6,12,29,.9); border: 1px solid var(--line); font-weight: 700; font-size: 14px;
  opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; white-space: nowrap; max-width: 92%;
  overflow: hidden; text-overflow: ellipsis;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.bad { border-color: rgba(255,59,107,.7); color: #ffd0da; }
#toast.good { border-color: rgba(56,225,255,.6); color: #c9f6ff; }
#readyHint {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%); pointer-events: none; text-align: center;
  font-weight: 700; font-size: 15px; color: #c9f6ff; opacity: 0; transition: opacity .2s; white-space: nowrap;
  text-shadow: 0 0 10px rgba(56,225,255,.7), 0 1px 0 #000;
}
#readyHint.show { opacity: 1; animation: pulse 1.4s ease-in-out infinite; }
#readyHint small { display: block; font-size: 12px; font-weight: 600; color: var(--muted); text-shadow: none; margin-top: 4px; }
@keyframes pulse { 50% { opacity: .55; } }
#hint {
  position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%); width: max-content; max-width: min(640px, calc(100% - 24px));
  display: flex; gap: 8px; align-items: flex-start; padding: 9px 14px; border-radius: 12px;
  background: rgba(8, 18, 44, .88); border: 1px solid rgba(255, 209, 102, .35); color: #ffeab8; font-size: 14px; line-height: 1.45;
  transition: opacity .3s; pointer-events: none;
}
#hint svg { width: 18px; height: 18px; flex: none; margin-top: 1px; color: var(--gold); }
#hint.hide { opacity: 0; }

/* ---------- touch controls ---------- */
#touch { display: none; }
.touch #touch {
  display: flex; flex: none; height: var(--touch-h); align-items: center; justify-content: space-between;
  padding: 6px 22px calc(10px + var(--safe-b)); position: relative; z-index: 3;
}
.touch.landscape #touch { position: absolute; left: 0; right: 0; bottom: 0; height: auto; padding: 0 18px calc(14px + var(--safe-b)); pointer-events: none; }
.touch.landscape #touch > * { pointer-events: auto; }
#pad {
  --s: 148px; width: var(--s); height: var(--s); border-radius: 50%; position: relative; touch-action: none;
  background: radial-gradient(circle at 50% 50%, rgba(30,60,130,.5), rgba(10,22,52,.75)); border: 2px solid rgba(96,165,250,.35);
  box-shadow: inset 0 0 24px rgba(56,225,255,.12), 0 0 20px rgba(0,0,0,.4);
}
#pad .arrow { position: absolute; width: 0; height: 0; border: 13px solid transparent; opacity: .75; }
#pad .up { left: 50%; top: 12px; transform: translateX(-50%); border-bottom: 16px solid #9cc8ff; border-top: 0; }
#pad .down { left: 50%; bottom: 12px; transform: translateX(-50%); border-top: 16px solid #9cc8ff; border-bottom: 0; }
#pad .left { top: 50%; left: 12px; transform: translateY(-50%); border-right: 16px solid #9cc8ff; border-left: 0; }
#pad .right { top: 50%; right: 12px; transform: translateY(-50%); border-left: 16px solid #9cc8ff; border-right: 0; }
#pad .nub { position: absolute; left: 50%; top: 50%; width: 46px; height: 46px; margin: -23px; border-radius: 50%; background: rgba(96,165,250,.18); border: 1px solid rgba(96,165,250,.4); transition: transform .06s; }
#pad[data-dir="1"] .nub { transform: translateY(-30px); } #pad[data-dir="1"] .up { opacity: 1; filter: drop-shadow(0 0 6px #38e1ff); }
#pad[data-dir="2"] .nub { transform: translateX(30px); } #pad[data-dir="2"] .right { opacity: 1; filter: drop-shadow(0 0 6px #38e1ff); }
#pad[data-dir="3"] .nub { transform: translateY(30px); } #pad[data-dir="3"] .down { opacity: 1; filter: drop-shadow(0 0 6px #38e1ff); }
#pad[data-dir="4"] .nub { transform: translateX(-30px); } #pad[data-dir="4"] .left { opacity: 1; filter: drop-shadow(0 0 6px #38e1ff); }
#loopBtn {
  width: 108px; height: 108px; border-radius: 50%; border: 2px solid rgba(56,225,255,.7); touch-action: none;
  background: radial-gradient(circle at 40% 35%, rgba(56,225,255,.35), rgba(8,40,80,.85));
  color: #dffbff; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  box-shadow: 0 0 22px rgba(56,225,255,.35), inset 0 0 18px rgba(56,225,255,.2);
  font-weight: 800; font-size: 13px;
}
#loopBtn svg { width: 40px; height: 40px; }
#loopBtn:active { transform: scale(.95); }

/* ---------- screens & overlays ---------- */
.screen, .overlay { position: absolute; top: 0; right: 0; bottom: 0; left: 0; display: none; z-index: 10; }
.screen.show, .overlay.show { display: flex; }
.overlay { align-items: center; justify-content: center; background: rgba(3, 7, 18, .62); backdrop-filter: blur(3px); padding: 16px; }
.card {
  width: min(380px, 100%); max-height: calc(100% - 8px); overflow: auto; padding: 24px 22px; border-radius: 18px;
  background: var(--panel); border: 1px solid var(--line); box-shadow: 0 20px 60px rgba(0,0,0,.55), 0 0 0 1px rgba(56,225,255,.06) inset;
  display: flex; flex-direction: column; gap: 10px; animation: pop .22s ease-out;
}
@keyframes pop { from { transform: scale(.94); opacity: 0; } }
.card h2 { margin: 0 0 6px; text-align: center; font-size: 22px; letter-spacing: .08em; }
.btn {
  height: 48px; border-radius: 12px; border: 1px solid rgba(96,165,250,.35); background: rgba(18, 36, 80, .7);
  font-weight: 800; font-size: 16px; letter-spacing: .06em; transition: transform .1s, background .15s, border-color .15s, box-shadow .15s;
  padding: 0 18px;
}
.btn:hover { background: rgba(36, 70, 150, .7); border-color: rgba(56,225,255,.6); }
.btn:active { transform: scale(.97); }
.btn.primary { background: linear-gradient(180deg, #2fd6f5, #1597c9); border-color: #7deaff; color: #04121f; box-shadow: 0 0 22px rgba(56,225,255,.35); }
.btn.primary:hover { box-shadow: 0 0 30px rgba(56,225,255,.6); }
.btn.ghost { background: transparent; }
.btn.danger { color: #ff9db2; border-color: rgba(255,59,107,.4); }
.btn:focus-visible, .icon-btn:focus-visible, .toggle:focus-visible, .lv:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
.row { display: flex; gap: 10px; } .row .btn { flex: 1; }
.toggles { display: flex; gap: 10px; } .toggles.col { flex-direction: column; }
.toggle {
  flex: 1; height: 44px; border-radius: 12px; border: 1px solid var(--line); background: rgba(10, 22, 52, .7);
  display: flex; align-items: center; justify-content: space-between; padding: 0 14px; font-weight: 700;
}
.toggle b { font-size: 13px; padding: 3px 10px; border-radius: 999px; background: rgba(56,225,255,.18); color: var(--cyan); }
.toggle.off b { background: rgba(140,160,200,.12); color: var(--muted); }
.pause-goal { text-align: center; color: var(--muted); font-size: 14px; line-height: 1.5; margin: -2px 0 6px; }

/* title */
#title { align-items: center; justify-content: center; flex-direction: column;
  background: radial-gradient(ellipse at 50% 45%, rgba(6,12,29,.35), rgba(6,12,29,.88) 75%); }
.title-wrap { display: flex; flex-direction: column; align-items: center; text-align: center; padding: 20px; animation: rise .6s ease-out; }
@keyframes rise { from { transform: translateY(16px); opacity: 0; } }
.logo { display: flex; align-items: center; gap: 14px; }
.logo-bot { width: clamp(52px, 11vw, 84px); height: auto; filter: drop-shadow(0 0 12px rgba(56,225,255,.7)); animation: hover 3s ease-in-out infinite; }
@keyframes hover { 50% { transform: translateY(-5px); } }
.logo h1 {
  margin: 0; font-size: clamp(46px, 11vw, 92px); font-weight: 900; letter-spacing: .06em; line-height: 1;
  background: linear-gradient(180deg, #ffffff 10%, #8beeff 55%, #2aa9e0); -webkit-background-clip: text; background-clip: text; color: transparent;
  filter: drop-shadow(0 0 18px rgba(56,225,255,.55)) drop-shadow(0 3px 0 #04203a);
}
.subtitle { margin-top: 12px; font: 800 clamp(13px, 2.6vw, 18px)/1 ui-monospace, Menlo, monospace; letter-spacing: .55em; color: var(--cyan); text-shadow: 0 0 12px rgba(56,225,255,.8); padding-left: .55em; }
.tagline { margin: 18px 0 26px; color: #b9cdee; font-size: clamp(14px, 2.4vw, 17px); max-width: 30em; line-height: 1.6; }
.menu { display: flex; flex-direction: column; gap: 12px; width: min(300px, 82vw); }
.menu .btn { height: 52px; font-size: 17px; }
.hub-link { position: absolute; left: 16px; bottom: calc(14px + var(--safe-b)); color: var(--muted); text-decoration: none; font-size: 14px; }
.hub-link:hover { color: var(--text); }
.version { position: absolute; right: 16px; bottom: calc(14px + var(--safe-b)); color: #50648c; font: 12px ui-monospace, monospace; }

/* level select */
#select { justify-content: center; background: rgba(4, 8, 20, .82); overflow: auto; touch-action: pan-y; }
.sheet { width: min(860px, 100%); padding: 14px 16px calc(28px + var(--safe-b)); }
.sheet-head { display: flex; align-items: center; gap: 12px; position: sticky; top: 0; padding: 8px 0 12px; background: linear-gradient(180deg, rgba(4,8,20,.98) 70%, rgba(4,8,20,0)); z-index: 1; }
.sheet-head h2 { margin: 0; flex: 1; font-size: 22px; letter-spacing: .08em; }
.star-total { color: var(--gold); font-weight: 800; }
.chapter { margin: 14px 0 22px; }
.chapter h3 { margin: 0 0 12px; font-size: 15px; color: var(--muted); letter-spacing: .12em; font-weight: 800; display: flex; gap: 10px; align-items: baseline; }
.chapter h3 b { color: var(--text); font-size: 18px; letter-spacing: .06em; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.lv {
  position: relative; min-height: 84px; border-radius: 14px; border: 1px solid var(--line); background: rgba(12, 26, 60, .78);
  padding: 10px 12px; text-align: left; display: flex; flex-direction: column; justify-content: space-between; gap: 6px;
  transition: transform .12s, border-color .15s, background .15s;
}
.lv:hover { transform: translateY(-2px); border-color: rgba(56,225,255,.55); background: rgba(20, 44, 96, .85); }
.lv .n { font: 800 13px/1 ui-monospace, Menlo, monospace; color: var(--cyan); }
.lv .nm { font-weight: 800; font-size: 16px; }
.lv .st { color: var(--gold); letter-spacing: 2px; font-size: 14px; }
.lv .st .e { color: #33476e; }
.lv.locked { opacity: .45; pointer-events: none; }
.lv.locked .nm::after { content: " 🔒"; font-size: 12px; }
.lv.next { border-color: rgba(56,225,255,.7); box-shadow: 0 0 18px rgba(56,225,255,.25); }

/* win */
.win-card { text-align: center; align-items: stretch; }
.win-title { font-size: 28px; font-weight: 900; letter-spacing: .1em; color: #b8ffe4; text-shadow: 0 0 18px rgba(52,211,153,.7); }
.win-sub { color: var(--muted); font-size: 14px; line-height: 1.5; }
.stars { display: flex; justify-content: center; gap: 12px; margin: 6px 0 2px; }
.stars i {
  width: 46px; height: 46px; display: block;
  background: #22345c; clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
  transition: background .2s, transform .3s cubic-bezier(.3, 1.8, .5, 1);
}
.stars i:nth-child(2) { transform: translateY(-8px); }
.stars i.on { background: linear-gradient(180deg, #fff2b8, #ffc94a 60%, #f59e0b); transform: scale(1.12); filter: drop-shadow(0 0 10px rgba(255,201,74,.8)); }
.stars i:nth-child(2).on { transform: translateY(-8px) scale(1.12); }
.stats { display: flex; gap: 10px; margin: 6px 0; }
.stats div { flex: 1; padding: 10px; border-radius: 12px; background: rgba(10, 22, 52, .7); border: 1px solid var(--line); display: flex; flex-direction: column; gap: 4px; }
.stats span { font-size: 12px; color: var(--muted); letter-spacing: .1em; }
.stats b { font-size: 22px; font-variant-numeric: tabular-nums; }
.stats b small { font-size: 12px; color: var(--muted); font-weight: 600; margin-left: 4px; }
.new-best { color: var(--gold); font-weight: 800; letter-spacing: .2em; font-size: 13px; visibility: hidden; }
.new-best.show { visibility: visible; }

/* how to */
.howto-card { width: min(460px, 100%); }
.howto-card ol { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 10px; counter-reset: s; }
.howto-card li { display: flex; gap: 12px; align-items: flex-start; padding: 10px 12px; border-radius: 12px; background: rgba(10,22,52,.6); border: 1px solid rgba(96,165,250,.15); }
.howto-card li::before { counter-increment: s; content: counter(s); flex: none; width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-weight: 900; background: rgba(56,225,255,.18); color: var(--cyan); font-size: 14px; }
.howto-card li b { display: block; margin-bottom: 2px; }
.howto-card li span { color: #b9cdee; font-size: 14px; line-height: 1.5; }
.keys { color: var(--muted); font-size: 12.5px; text-align: center; line-height: 1.6; }

/* states */
#app.on-title #hud, #app.on-title #timer, #app.on-title #touch, #app.on-title #hint, #app.on-title #readyHint { visibility: hidden; }
#app.on-select #hud, #app.on-select #timer, #app.on-select #touch, #app.on-select #hint, #app.on-select #readyHint, #app.on-select #cv { visibility: hidden; }

@media (max-width: 640px) {
  :root { --hud-h: 52px; }
  #hud { padding: 0 8px; gap: 6px; }
  .lvl-name { display: none; }
  .icon-btn { width: 34px; height: 34px; border-radius: 9px; }
  .icon-btn svg { width: 18px; height: 18px; }
  .hud-left, .hud-right { gap: 4px; }
  .time-left { min-width: 36px; font-size: 14px; }
  .loop-label { font-size: 14px; }
  .echo-dots i { width: 14px; height: 14px; font-size: 8px; }
  #hint { font-size: 13px; padding: 8px 12px; }
  .grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 640px) {
  #btnRetry, #btnSound { display: none; }
  .hud-left, .hud-right { flex: 0 0 auto; }
  .hud-mid { flex: 1; justify-content: center; min-width: 0; }
  .echo-dots { max-width: 40vw; overflow: hidden; }
  #readyHint { font-size: 14px; }
}
@media (max-height: 520px) {
  .tagline { margin: 10px 0 14px; }
  .menu { gap: 8px; } .menu .btn { height: 44px; }
  .subtitle { margin-top: 8px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
