:root {
  --bg: #140b3d;
  --bg-2: #241160;
  --panel: rgba(255, 255, 255, 0.10);
  --panel-line: rgba(255, 255, 255, 0.22);
  --ink: #ffffff;
  --ink-dim: #cbc0ff;
  --accent: #4ff5ff;
  --accent-2: #ff5ce4;
  --accent-3: #ffe14f;
  --accent-4: #7cff6b;
  --accent-5: #ff8a3d;
  --good: #5dffa8;
  --warn: #ffd93d;
  --radius: 18px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

/* .ctl sets display:flex, which outranks the user-agent [hidden] rule — without
   this the hidden range row stays on screen. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;          /* no rubber-band bounce behind the canvas */
  -webkit-text-size-adjust: 100%;     /* Safari inflates text in landscape without this */
}

body {
  background:
    radial-gradient(900px 600px at 8% -8%, #2b6bff 0%, transparent 58%),
    radial-gradient(800px 520px at 98% 4%, #ff3fb4 0%, transparent 55%),
    radial-gradient(760px 620px at 48% 108%, #17d6b8 0%, transparent 58%),
    radial-gradient(680px 500px at 82% 92%, #ff9d2e 0%, transparent 52%),
    linear-gradient(165deg, var(--bg-2) 0%, var(--bg) 65%);
  background-attachment: fixed;
  color: var(--ink);
  font-family: "Baloo 2", "Nunito", "Segoe UI Variable", "Segoe UI", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  /* Scrollable on purpose. The layout is built to fit, but a device with larger
     text or an unusual viewport can still overflow — and a pinned body makes
     everything past the fold permanently unreachable. Dragging the target does
     not scroll the page because the canvas sets touch-action: none itself. */
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#app {
  /* A *definite* height, so the stage has a percentage basis for the canvas.
     With only min-height the stage height is indefinite, #scope's height:100%
     resolves to the canvas element's own attribute, and each resize feeds the
     previous measurement back in — the canvas grows past the viewport.
     Overflow is still reachable because the body scrolls. */
  height: 100%;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  gap: 12px;
  padding:
    calc(14px + env(safe-area-inset-top)) 
    calc(clamp(12px, 2.2vw, 26px) + env(safe-area-inset-right))
    calc(16px + env(safe-area-inset-bottom))
    calc(clamp(12px, 2.2vw, 26px) + env(safe-area-inset-left));
  max-width: 1600px;
  margin: 0 auto;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-right: auto;
}

.brand h1 {
  font-size: clamp(20px, 2.4vw, 30px);
  letter-spacing: 0.4px;
  margin: 0;
  background: linear-gradient(92deg, var(--accent-3), var(--accent-4), var(--accent), var(--accent-2), var(--accent-5));
  background-size: 300% 100%;
  animation: rainbowSlide 9s linear infinite;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 0 22px rgba(255, 92, 228, 0.35);
}

@keyframes rainbowSlide {
  to { background-position: 300% 0; }
}

.brand-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 rgba(70, 224, 255, 0.55);
  animation: pulse 2.4s ease-out infinite;
}

@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 rgba(70, 224, 255, 0.5); }
  70%  { box-shadow: 0 0 0 14px rgba(70, 224, 255, 0); }
  100% { box-shadow: 0 0 0 0 rgba(70, 224, 255, 0); }
}

.stats {
  display: flex;
  gap: 10px;
}

.stat {
  min-width: 92px;
  padding: 6px 14px 8px;
  border-radius: 16px;
  background: var(--panel);
  border: 2px solid var(--panel-line);
  text-align: center;
}

.stats .stat:nth-child(1) { border-color: rgba(79, 245, 255, .7); box-shadow: 0 0 18px rgba(79,245,255,.25) inset; }
.stats .stat:nth-child(2) { border-color: rgba(255, 225, 79, .7); box-shadow: 0 0 18px rgba(255,225,79,.25) inset; }
.stats .stat:nth-child(3) { border-color: rgba(255, 92, 228, .7); box-shadow: 0 0 18px rgba(255,92,228,.25) inset; }

.stats .stat:nth-child(1) .stat-value { color: var(--accent); }
.stats .stat:nth-child(2) .stat-value { color: var(--accent-3); }
.stats .stat:nth-child(3) .stat-value { color: var(--accent-2); }

.stat-label {
  display: block;
  font-size: 11px;
  letter-spacing: 1.4px;
  text-transform: uppercase;
  color: var(--ink-dim);
}

.stat-value {
  display: block;
  font-size: 26px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

.stat-value.bump { animation: bump 0.35s cubic-bezier(.2,1.6,.4,1); }

@keyframes bump {
  0% { transform: scale(1); }
  40% { transform: scale(1.35); color: var(--good); }
  100% { transform: scale(1); }
}

.topbar-actions { display: flex; gap: 8px; }

/* ---------- buttons ---------- */

.btn {
  font: inherit;
  font-weight: 700;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--panel-line);
  border-radius: 999px;
  padding: 10px 18px;
  cursor: pointer;
  transition: transform .12s ease, background .2s ease, box-shadow .2s ease;
}

.btn:hover { background: rgba(255,255,255,.12); }
.btn:active { transform: scale(.96); }

.btn.is-on {
  background: linear-gradient(92deg, rgba(75,255,165,.25), rgba(70,224,255,.25));
  border-color: rgba(75,255,165,.55);
}

.btn-primary {
  background: linear-gradient(92deg, var(--accent-3), var(--accent-5), var(--accent-2));
  border: none;
  color: #08122b;
  box-shadow: 0 10px 26px rgba(255, 83, 200, .3);
}

.btn-primary:hover { filter: brightness(1.08); background: linear-gradient(92deg, var(--accent), var(--accent-2)); }

.btn-big {
  padding: clamp(9px, 1.5vh, 15px) clamp(16px, 4vw, 30px);
  font-size: clamp(14px, 2.3vh, 19px);
  margin-top: clamp(4px, 1vh, 6px);
}

/* ---------- canvas stage ---------- */

.stage {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: linear-gradient(180deg, rgba(14,8,48,.95), rgba(6,3,26,.97));
  border: 2px solid transparent;
  background-origin: border-box;
  background-clip: padding-box;
  box-shadow: var(--shadow);
  min-height: 220px;
}

/* Focus-mode chrome only. Anchored bottom-right: the target badge always sits at
   the top of the canvas, and buttons there would cover it. */
.stage-tools {
  position: absolute;
  bottom: calc(8px + env(safe-area-inset-bottom));
  right: calc(8px + env(safe-area-inset-right));
  display: flex;
  gap: 6px;
  z-index: 6;
}

.btn-mini {
  padding: 9px 11px;
  font-size: 15px;
  line-height: 1;
  background: rgba(6, 4, 24, .55);
  border-color: rgba(255, 255, 255, .28);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
}

/* Focus mode: the graph takes the screen. Everything the player still needs —
   score, hold, target, hint — is drawn onto the canvas instead. */
body.focus .topbar,
body.focus .hud,
body.focus .controls { display: none; }

body.focus #app {
  padding: 0;
  gap: 0;
  grid-template-rows: 1fr;
  max-width: none;
}

body.focus .stage {
  border-radius: 0;
  border: none;
  min-height: 0;
  box-shadow: none;
}

#scope {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

#scope.dragging { cursor: grabbing; }

.overlay {
  position: absolute;
  inset: 0;
  /* flex + margin:auto on the child, not place-items:center. Centred grid/flex
     content that overflows gets clipped at the *top* with no way to scroll to
     it — which would hide the first line of the permission instructions. */
  display: flex;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px;
  background: rgba(5, 8, 22, .72);
  -webkit-backdrop-filter: blur(7px);
  backdrop-filter: blur(7px);
  text-align: center;
  transition: opacity .35s ease;
}

.overlay[hidden] { display: none; }
.overlay.fade { opacity: 0; pointer-events: none; }

.overlay-card { padding: clamp(10px, 2vh, 26px); max-width: 460px; margin: auto; }

/* Fluid, so the card fits whatever height the stage ends up with — a phone in
   portrait leaves it very little room. */
.overlay-emoji {
  font-size: clamp(26px, 6vh, 60px);
  line-height: 1;
  animation: float 3s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-9px); }
}

.overlay-card h2 { margin: clamp(4px, 1vh, 12px) 0 4px; font-size: clamp(17px, 3.4vh, 30px); }
.overlay-card p { margin: 0; color: var(--ink-dim); line-height: 1.4; font-size: clamp(12px, 1.9vh, 16px); }
.overlay-note { margin-top: clamp(6px, 1.2vh, 14px) !important; font-size: clamp(10px, 1.5vh, 13px); }
.overlay-note.error { color: #ff9d9d; }

.btn-reload {
  margin-top: 10px;
  border-color: rgba(255, 157, 157, .6);
  background: rgba(255, 157, 157, .14);
}

/* Facts to read back when something is refused on a device we cannot inspect. */
.diag {
  display: inline-block;
  margin-top: 8px;
  padding: 5px 9px;
  border-radius: 8px;
  background: rgba(0, 0, 0, .35);
  color: #cbd6ff;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 10.5px;
  line-height: 1.5;
  word-break: break-word;
  -webkit-user-select: all;
  user-select: all;
}

.toast {
  position: absolute;
  left: 50%;
  top: 16%;
  transform: translate(-50%, -8px);
  padding: 12px 26px;
  border-radius: 999px;
  font-size: 26px;
  font-weight: 800;
  background: linear-gradient(92deg, var(--accent-3), var(--accent-4), var(--accent));
  color: #10203a;
  box-shadow: 0 14px 40px rgba(75,255,165,.35);
  animation: toastIn .45s cubic-bezier(.2,1.5,.4,1);
  pointer-events: none;
  white-space: nowrap;
}

@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 16px) scale(.8); }
  to   { opacity: 1; transform: translate(-50%, -8px) scale(1); }
}

/* ---------- HUD ---------- */

.hud {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.hud-block {
  padding: 10px 16px 12px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--panel-line);
}

.hud-label {
  display: block;
  font-size: 11px;
  letter-spacing: 1.4px;
  text-transform: uppercase;
  color: var(--ink-dim);
}

.hud-value {
  display: block;
  font-size: clamp(22px, 3.2vw, 34px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}

.hud-value small { font-size: .45em; color: var(--ink-dim); margin-left: 4px; }
.target-value { color: var(--warn); }
.hud-sub { font-size: 13px; color: var(--ink-dim); }

.hud-hold, .hud-input { display: flex; flex-direction: column; justify-content: center; gap: 8px; }

.hud-input .hud-label b {
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
}

.level-track {
  height: 12px;
  border-radius: 999px;
  background: rgba(255,255,255,.09);
  overflow: hidden;
}

.level-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(92deg, #4ff5ff 0%, #7cff6b 40%, #ffe14f 68%, #ff8a3d 85%, #ff4d6d 100%);
  transition: width .06s linear;
}

.level-fill.is-dead { background: #ff5d5d; opacity: .45; }

select {
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  background: rgba(20, 28, 60, .95);
  border: 1px solid var(--panel-line);
  border-radius: 10px;
  padding: 8px 10px;
  max-width: 100%;
}

.hold-track {
  height: 16px;
  border-radius: 999px;
  background: rgba(255,255,255,.09);
  overflow: hidden;
}

.hold-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(92deg, var(--accent), var(--accent-4), var(--accent-3));
  transition: width .08s linear;
  box-shadow: 0 0 18px rgba(75,255,165,.6);
}

/* ---------- controls ---------- */

.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px 20px;
  align-items: center;
}

.ctl { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--ink-dim); }
.ctl b { color: var(--ink); font-size: 15px; }

.ctl-buttons {
  flex-direction: row;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 8px;
  touch-action: pan-y;      /* let a vertical swipe scroll, drag the thumb sideways */
  border-radius: 999px;
  background: rgba(255,255,255,.14);
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: linear-gradient(180deg, #fff, #cfe6ff);
  border: 3px solid var(--accent);
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(0,0,0,.4);
}

input[type="range"]::-moz-range-thumb {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--accent);
  cursor: pointer;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  color: var(--ink);
  cursor: pointer;
  user-select: none;
}

.switch input { width: 18px; height: 18px; accent-color: var(--accent); }

/* Tablet widths: two sliders side by side, buttons on their own row rather than
   wrapping one per line down the right edge. */
@media (max-width: 980px) {
  .controls { grid-template-columns: 1fr 1fr; }
  .ctl-buttons { grid-column: 1 / -1; justify-content: flex-start; }
}

@media (max-width: 720px) {
  #app { gap: 8px; padding-top: calc(8px + env(safe-area-inset-top)); }
  .topbar { gap: 10px; }
  .brand h1 { font-size: 22px; }
  .stats { gap: 6px; }
  .stats .stat { min-width: 60px; padding: 3px 8px 5px; border-radius: 12px; }
  .stat-label { font-size: 9px; letter-spacing: .8px; }
  .stat-value { font-size: 19px; }
  .btn { padding: 11px 14px; }        /* keep a comfortable tap target */
  .hud { grid-template-columns: 1fr 1fr; gap: 8px; }
  .hud-block { padding: 7px 11px 9px; }
  .hud-value { font-size: 22px; }
  .hud-hold, .hud-input { grid-column: auto; }
  .controls { gap: 8px 14px; }
  .ctl-buttons { justify-content: flex-start; }
  .sheet-inner { padding: 16px 16px 14px; border-radius: 16px; }
}

/* Phone in portrait: the graph is the point, so give it the room. */
@media (max-width: 520px) {
  .brand { width: 100%; margin-right: 0; }
  .topbar-actions { width: 100%; }
  .topbar-actions .btn { flex: 1; padding: 11px 8px; font-size: 14px; }
  .hud { grid-template-columns: 1fr 1fr; }
  .stage { min-height: 180px; }
}

/* vh-based clamps size against the viewport, but the card is constrained by the
   stage, which on a phone is barely a third of it. Explicit small sizes here. */
@media (max-width: 560px) {
  .overlay-card { max-width: 100%; padding: 9px 12px; margin: auto; }
  .overlay-emoji { font-size: 28px; animation: none; }
  .overlay-card h2 { font-size: 18px; margin: 3px 0 2px; }
  .overlay-card p { font-size: 12.5px; line-height: 1.3; }
  .btn-big { padding: 10px 16px; font-size: 14px; margin-top: 4px; }
  .overlay-note { font-size: 10.5px; margin-top: 6px !important; }
  .btn-reload { margin-top: 7px; padding: 8px 14px; font-size: 13px; }
}

/* Phone in landscape: height is the scarce resource. Everything shrinks so the
   graph, the readouts and the controls all still fit without scrolling. */
@media (max-height: 520px) and (orientation: landscape) {
  #app { gap: 5px; padding: calc(5px + env(safe-area-inset-top)) 10px calc(5px + env(safe-area-inset-bottom)); }
  .topbar { gap: 8px; flex-wrap: nowrap; }
  .brand h1 { font-size: 17px; }
  .brand-dot { width: 9px; height: 9px; animation: none; }
  .stats { gap: 5px; }
  .stats .stat { min-width: 46px; padding: 1px 6px 3px; border-width: 1px; }
  .stat-label { font-size: 8px; letter-spacing: .6px; }
  .stat-value { font-size: 15px; }
  .topbar-actions { width: auto; gap: 6px; }
  .topbar-actions .btn { flex: 0 0 auto; padding: 7px 10px; font-size: 12px; }
  .stage { min-height: 90px; }
  .hud { grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .hud-block { padding: 3px 8px 4px; }
  .hud-label { font-size: 8px; letter-spacing: .6px; }
  .hud-value { font-size: 15px; }
  .hud-sub { font-size: 9px; }
  .hold-track, .level-track { height: 8px; }
  .controls { grid-template-columns: 1fr 1fr auto; gap: 3px 12px; }
  .ctl-buttons { grid-column: auto; }
  .ctl { font-size: 11px; gap: 2px; }
  .ctl b { font-size: 13px; }
  .btn { padding: 6px 10px; font-size: 12px; }
  input[type="range"] { height: 6px; }
  input[type="range"]::-webkit-slider-thumb { width: 18px; height: 18px; }
  .overlay-emoji { display: none; }
  .overlay-card { padding: 6px 10px; }
  .overlay-card h2 { font-size: 16px; margin: 0 0 2px; }
  .overlay-card p { font-size: 11px; line-height: 1.3; }
  .btn-big { padding: 8px 16px; font-size: 13px; }
  .overlay-note { font-size: 10px; margin-top: 4px !important; }
}
}

/* ---------- settings sheet ---------- */

.sheet {
  border: none;
  padding: 0;
  background: transparent;
  color: var(--ink);
  max-width: min(560px, 94vw);
  width: 100%;
  max-height: 90vh;
}

.sheet::backdrop {
  background: rgba(4, 7, 20, .68);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}

/* Safari before 15.4 has no showModal(), so the dialog is positioned by hand and
   the huge spread shadow stands in for ::backdrop. */
.sheet:not([open]) { display: none; }

.sheet[open] {
  position: fixed;
  inset: 0;
  margin: auto;
  z-index: 60;
  box-shadow: 0 0 0 100vmax rgba(4, 7, 20, .68);
}

.sheet-inner {
  -webkit-overflow-scrolling: touch;
  background: linear-gradient(180deg, #131c42, #0b1130);
  border: 1px solid var(--panel-line);
  border-radius: 20px;
  box-shadow: var(--shadow);
  padding: 20px 22px 18px;
  max-height: 90vh;
  overflow-y: auto;
}

.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.sheet-head h2 { margin: 0; font-size: 22px; }

.btn-close { padding: 6px 12px; line-height: 1; }

.sheet-group {
  padding: 14px 0;
  border-top: 1px solid var(--panel-line);
  display: grid;
  gap: 14px;
}

.sheet-group h3 {
  margin: 0;
  font-size: 12px;
  letter-spacing: 1.6px;
  text-transform: uppercase;
  color: var(--accent);
}

.sheet .ctl small, .ctl-note {
  font-size: 12px;
  line-height: 1.45;
  color: var(--ink-dim);
}

.sheet-foot {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding-top: 14px;
  border-top: 1px solid var(--panel-line);
}

.sheet .switch { margin-top: 2px; }
