:root {
  --bg: #14171c;
  --bg2: #1c2027;
  --card: #222731;
  --card-hi: #2a303b;
  --text: #e8eaed;
  --muted: #9aa3ad;
  --accent: #4fc3f7;
  --accent2: #1565c0;
  --ok: #66bb6a;
  --warn: #ffb74d;
  --bad: #f44336;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
kbd {
  display: inline-block; min-width: 1.8em; padding: 1px 6px; margin-right: 4px;
  border: 1px solid #3a414d; border-bottom-width: 3px; border-radius: 6px;
  background: var(--card); font: 600 13px/1.4 ui-monospace, monospace; text-align: center;
}

/* ============================== MENU ============================== */
#menu-view { max-width: 1200px; margin: 0 auto; padding: 28px 16px 40px; }
.resume-btn {
  display: inline-flex; align-items: center; gap: 6px; margin: -12px 0 16px; padding: 7px 14px 7px 10px;
  border-radius: 99px; border: 1px solid #343b47; background: var(--card); color: var(--text);
  text-decoration: none; font-size: 14px;
}
.resume-btn:hover { background: var(--card-hi); border-color: var(--accent); }
.resume-btn b { font-weight: 700; }
.menu-header { display: flex; align-items: center; gap: 16px; margin-bottom: 28px; }
.logo {
  width: 56px; height: 56px; border-radius: 14px; flex: none;
  display: grid; place-items: center; background: var(--accent2);
  font: 800 34px/1 system-ui, sans-serif; box-shadow: 0 6px 20px rgba(21,101,192,.4);
}
h1 { margin: 0; font-size: 28px; letter-spacing: -.01em; }
.tagline { margin: 2px 0 0; color: var(--muted); }
.site-footer {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  margin-top: 8px; padding-top: 20px; border-top: 1px solid #262c36;
}
.site-footer a { color: var(--muted); text-decoration: none; }
.site-footer a:hover { color: var(--text); }
.site-footer .author { font-size: 15px; line-height: 1; color: var(--muted); }
.site-footer .author:hover { color: var(--muted); text-decoration: underline; }
.site-footer .gh {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  border: 1px solid #343b47; background: var(--card);
}
.site-footer .gh:hover { background: var(--card-hi); }
.menu-section { margin-bottom: 32px; }
.section-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
h2 { margin: 0; font-size: 19px; }
.summary { color: var(--muted); font-size: 14px; }
.scroll-btns { margin-left: auto; display: flex; gap: 8px; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; border: 1px solid #343b47;
  background: var(--card); font-size: 22px; line-height: 1;
}
.icon-btn:hover { background: var(--card-hi); }

.card-track {
  display: flex; gap: 16px; overflow-x: auto; scroll-snap-type: x mandatory;
  padding: 4px 2px 16px; scrollbar-width: thin; scrollbar-color: #3a414d transparent;
  overscroll-behavior-x: contain;
}
.card {
  flex: 0 0 300px; scroll-snap-align: start; text-decoration: none; color: inherit;
  background: var(--card); border-radius: var(--radius); overflow: hidden;
  border: 1px solid #2d3440; transition: transform .15s, border-color .15s, box-shadow .15s;
}
.card:hover, .card:focus-visible {
  transform: translateY(-3px); border-color: var(--accent);
  box-shadow: 0 10px 28px rgba(0,0,0,.45); outline: none;
}
.card.done { border-color: rgba(102,187,106,.55); }
.card.perfect { border-color: rgba(255,213,79,.7); }

/* Group = a stack of cards: extra card edges peek out behind the top card. */
.stack {
  flex: 0 0 300px; scroll-snap-align: start; position: relative; isolation: isolate;
  text-decoration: none; color: inherit; margin: 0 12px 12px 0; display: block;
}
.stack .card { display: block; height: 100%; position: relative; z-index: 2; }
.stack::before, .stack::after {
  content: ''; position: absolute; inset: 0; border-radius: var(--radius);
  background: #1d222b; border: 1px solid #2d3440; z-index: 1; transition: transform .15s;
}
.stack::before { transform: translate(6px, 6px); }
.stack::after { transform: translate(12px, 12px); z-index: 0; background: #191d24; display: none; }
.stack.layers-3::after { display: block; }
.stack:hover .card, .stack:focus-visible .card {
  transform: translateY(-3px); border-color: var(--accent); box-shadow: 0 10px 28px rgba(0,0,0,.45);
}
.stack:hover::before { transform: translate(8px, 7px); }
.stack:hover::after { transform: translate(15px, 13px); }
.stack:focus-visible { outline: none; }
.levels-count { margin-left: auto; font-size: 12px; color: var(--muted); white-space: nowrap; }
.progress-dots { display: flex; gap: 8px; }
.dot {
  width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center;
  border: 2px solid #4a525f; font-size: 14px; font-weight: 800; line-height: 1;
}
.dot.done { border-color: var(--ok); background: rgba(102,187,106,.15); color: var(--ok); }
.dot.star { border-color: #ffd54f; background: rgba(255,213,79,.15); color: #ffd54f; font-size: 15px; }
.card .par { font-size: 12.5px; color: var(--muted); margin-bottom: 4px; }
.card.perfect .best { color: #ffd54f; font-weight: 600; }
.badge.star { background: #ffd54f; color: #3d2f00; }

/* Group pop-up with a 2-wide grid of level cards */
body.modal-open { overflow: hidden; }
.group-overlay {
  position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; padding: 16px;
  background: rgba(5,7,10,.7); backdrop-filter: blur(4px); animation: fade .2s;
}
.group-dialog {
  width: min(760px, 100%); max-height: calc(100dvh - 32px); display: flex; flex-direction: column;
  background: var(--bg2); border: 1px solid #3a4250; border-radius: 20px;
  box-shadow: 0 24px 60px rgba(0,0,0,.6); animation: pop .25s cubic-bezier(.2,1.3,.4,1); overflow: hidden;
}
.group-head {
  display: flex; align-items: flex-start; gap: 14px; padding: 18px 18px 14px 22px;
  border-bottom: 1px solid #2d3440; background: linear-gradient(#232833, var(--bg2));
}
.group-head > div { flex: 1; min-width: 0; }
.group-head h2 { font-size: 22px; margin: 4px 0 2px; }
.group-head p { margin: 0; color: var(--muted); font-size: 14px; }
.group-head .progress-dots { align-self: center; }
.close-btn {
  flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  border: 1px solid #3a414d; background: var(--card); color: var(--text); text-decoration: none;
  font-size: 28px; line-height: 1; padding-bottom: 3px;
}
.close-btn:hover { background: var(--card-hi); }
.level-grid {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); grid-auto-rows: max-content; gap: 14px;
  padding: 16px 18px 20px; overflow-y: auto; overscroll-behavior: contain; min-height: 0;
}
.level-grid .card { flex: none; }
.thumb { position: relative; aspect-ratio: 16 / 10; background: #2a2f37; }
.thumb canvas { width: 100%; height: 100%; display: block; }
.badge {
  position: absolute; top: 10px; right: 10px; width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center; background: var(--ok); color: #0d2b10; font-weight: 800;
  box-shadow: 0 2px 8px rgba(0,0,0,.4);
}
.card-body { padding: 12px 14px 14px; }
.card-head { display: flex; align-items: center; gap: 8px; }
.card h3 { margin: 0; font-size: 16.5px; }
.diff {
  margin-left: auto; font-size: 11.5px; font-weight: 700; text-transform: uppercase;
  padding: 2px 8px; border-radius: 99px; background: #2f3a48; color: var(--accent);
}
.diff[data-level="easy"] { color: var(--ok); background: rgba(102,187,106,.13); }
.diff[data-level="medium"] { color: var(--warn); background: rgba(255,183,77,.13); }
.diff[data-level="hard"] { color: #ff8a80; background: rgba(244,67,54,.15); }
.card p { margin: 6px 0 10px; color: var(--muted); font-size: 14px; min-height: 2.9em; }
.best { font-size: 13px; color: var(--muted); }
.card.done .best { color: var(--ok); font-weight: 600; }

.help-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px 24px; margin: 12px 0; color: #cfd4da; }
.hint { color: var(--muted); max-width: 70ch; }
@media (pointer: fine) { .touch-note { display: none; } }

/* ============================== GAME ============================== */
#game-view {
  position: fixed; inset: 0; overflow: hidden; background: #000;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
#world { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

#topbar {
  position: absolute; top: 0; left: 0; right: 0; height: 56px; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: 8px max(10px, env(safe-area-inset-right)) 8px max(10px, env(safe-area-inset-left));
  background: linear-gradient(rgba(14,16,20,.92), rgba(14,16,20,.78));
  backdrop-filter: blur(6px); border-bottom: 1px solid rgba(255,255,255,.06);
}
.bar-btn {
  height: 40px; min-width: 40px; padding: 0 12px; border-radius: 10px;
  border: 1px solid rgba(255,255,255,.1); background: rgba(255,255,255,.06); font-size: 17px;
  white-space: nowrap;
}
.bar-btn:hover { background: rgba(255,255,255,.12); }
.bar-btn.off { opacity: .55; }
#btn-fullscreen { display: grid; place-items: center; padding: 0; width: 40px; }
#btn-fullscreen[hidden] { display: none; }
#btn-fullscreen .fs-exit, #btn-fullscreen.is-fs .fs-enter { display: none; }
#btn-fullscreen.is-fs .fs-exit { display: block; }
.bar-btn .lbl { font-size: 14px; }
#scen-title { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 1; }
.stats { display: flex; gap: 14px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.stats b { color: var(--text); }
.crash-stat b { color: #ff8a80; }
.stats small { color: var(--muted); font-size: 12px; }
.stats .star { font-style: normal; color: #ffd54f; margin-left: 3px; }
.stats .star[hidden] { display: none; }

.panel {
  position: absolute; top: 62px; right: 10px; z-index: 6; width: 270px;
  background: rgba(28,32,39,.97); border: 1px solid #343b47; border-radius: 12px;
  padding: 12px 14px; box-shadow: 0 12px 30px rgba(0,0,0,.5);
}
.panel label { display: flex; align-items: center; gap: 9px; padding: 5px 0; cursor: pointer; font-size: 14.5px; }
.panel input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }
.opt-title { margin: 10px 0 2px; font-size: 11.5px; font-weight: 700; text-transform: uppercase; color: var(--muted); letter-spacing: .06em; }

#hud .readout {
  background: rgba(14,16,20,.72); border: 1px solid rgba(255,255,255,.08); border-radius: 12px;
  backdrop-filter: blur(4px);
}
#hud {
  position: absolute; top: 66px; right: 10px; z-index: 3;
  display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none;
}
#hud .readout { padding: 3px 10px; font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
#hud-fps { display: inline-block; min-width: 2ch; text-align: right; color: var(--text); font-weight: 600; }

/* touch controls */
#touch-controls { position: absolute; inset: auto 0 0 0; z-index: 4; pointer-events: none; }
.cluster {
  position: absolute; bottom: max(14px, env(safe-area-inset-bottom)); display: flex; gap: 10px;
  pointer-events: auto; align-items: flex-end;
}
.cluster.left { left: max(12px, env(safe-area-inset-left)); }
.cluster.right { right: max(12px, env(safe-area-inset-right)); flex-direction: column; }
.ctl {
  width: 72px; height: 72px; border-radius: 18px; font-size: 26px;
  border: 1px solid rgba(255,255,255,.18); background: rgba(20,23,28,.62); color: #fff;
  backdrop-filter: blur(4px); touch-action: none; display: grid; place-items: center;
}
.ctl.pedal { width: 78px; height: 66px; line-height: 1; }
.ctl.pedal small { display: block; font-size: 11px; font-weight: 700; opacity: .7; }
.ctl.fwd { border-color: rgba(79,195,247,.5); }
.ctl.rev { border-color: rgba(255,183,77,.5); }
.ctl.active { background: rgba(79,195,247,.45); transform: scale(.96); }
.ctl.rev.active { background: rgba(255,183,77,.45); }

.toast {
  position: absolute; top: 68px; left: 50%; transform: translateX(-50%); z-index: 4;
  max-width: min(640px, calc(100% - 480px)); padding: 9px 16px; border-radius: 10px;
  background: rgba(14,16,20,.85); border: 1px solid rgba(255,255,255,.1); text-align: center;
  font-size: 14px; cursor: pointer; transition: opacity .8s;
}
.toast.fade { opacity: 0; pointer-events: none; }

/* overlays */
.overlay {
  position: absolute; inset: 0; z-index: 10; display: grid; place-items: center; padding: 16px;
  background: rgba(5,7,10,.55); backdrop-filter: blur(3px); animation: fade .25s;
}
.dialog {
  width: min(380px, 100%); background: var(--bg2); border: 1px solid #343b47; border-radius: 18px;
  padding: 22px 22px 20px; text-align: center; box-shadow: 0 20px 50px rgba(0,0,0,.6);
  animation: pop .3s cubic-bezier(.2,1.4,.4,1);
}
.dialog h2 { font-size: 24px; margin-bottom: 14px; }
.big-check {
  width: 64px; height: 64px; margin: 0 auto 8px; border-radius: 50%; background: var(--ok);
  color: #0d2b10; display: grid; place-items: center; font-size: 36px; font-weight: 900;
  box-shadow: 0 0 0 8px rgba(102,187,106,.2);
}
.result-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.result-grid div { background: var(--card); border-radius: 10px; padding: 8px; }
.result-grid span { display: block; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.result-grid b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.result-grid small { display: block; font-size: 11.5px; color: var(--muted); }
.big-check.star { background: #ffd54f; color: #3d2f00; box-shadow: 0 0 0 8px rgba(255,213,79,.2); }
.best-line { color: var(--muted); font-size: 14px; margin-bottom: 16px; min-height: 1.4em; }
.tag { display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 99px; background: rgba(255,213,79,.18); color: #ffd54f; font-size: 12px; font-weight: 700; }
.dlg-btns { display: flex; flex-direction: column; gap: 8px; }
.btn {
  height: 44px; border-radius: 11px; border: 1px solid #3a414d; background: var(--card); font-weight: 600;
}
.btn:hover:not(:disabled) { background: var(--card-hi); }
.btn.primary { background: var(--accent2); border-color: #1e88e5; }
.btn.primary:hover:not(:disabled) { background: #1976d2; }
.btn:disabled { opacity: .5; cursor: default; }

@keyframes fade { from { opacity: 0; } }
@keyframes pop { from { transform: scale(.9); opacity: 0; } }

@media (max-width: 640px) {
  .group-overlay { padding: 8px; }
  .group-dialog { max-height: calc(100dvh - 16px); }
  .group-head { padding: 12px 14px 12px 12px; flex-wrap: wrap; }
  .group-head .progress-dots { width: 100%; padding-left: 54px; }
  .level-grid { gap: 10px; padding: 12px; }
  .level-grid .card-body { padding: 9px 10px 11px; }
  .level-grid .card h3 { font-size: 14.5px; }
  .level-grid .card p { font-size: 12.5px; min-height: 0; }
  .level-grid .diff { font-size: 10px; padding: 1px 6px; }
  .bar-btn .lbl { display: none; }
  .stats { gap: 8px; font-size: 13.5px; }
  #scen-title { font-size: 14px; }
  .card, .stack { flex-basis: 74vw; }
  .stack .card { flex-basis: auto; }
  h1 { font-size: 23px; }
  .toast { left: 12px; transform: none; max-width: calc(100% - 24px); top: 112px; font-size: 13px; }
  #hud { top: 64px; right: 12px; }
}
@media (max-width: 430px) {
  #scen-title { visibility: hidden; }
}
@media (max-height: 500px) {
  #hud { top: 62px; }
  .toast { top: 62px; }
}
