:root {
  color-scheme: dark;
  --bg: #0b1620;
  --panel: #111f2a;
  --line: #284454;
  --text: #e9f0ee;
  --muted: #8fa3a7;
  --amber: #ffb23f;
  --cyan: #2dd9e5;
  --green: #66d17a;
  --red: #ff6d5d;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(180deg, rgba(20, 42, 55, 0.66), rgba(8, 17, 25, 0.94)),
    var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

.shell {
  width: min(920px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 14px 0 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid rgba(143, 163, 167, 0.22);
  padding-bottom: 10px;
}

h1 {
  margin: 0;
  /* vmin, not vw: on a laptop the constraint is height, not width */
  font-size: clamp(20px, 3.2vmin, 30px);
  line-height: 1;
  letter-spacing: 0.01em;
}

p {
  margin: 0;
  color: var(--muted);
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 154px;
  justify-content: flex-end;
  color: var(--muted);
  font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 13px;
  text-transform: uppercase;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--amber);
  box-shadow: 0 0 18px rgba(255, 178, 63, 0.7);
}

.status.locked .status-dot {
  background: var(--green);
  box-shadow: 0 0 18px rgba(102, 209, 122, 0.75);
}

.status.error .status-dot {
  background: var(--red);
  box-shadow: 0 0 18px rgba(255, 109, 93, 0.75);
}

/* Ring and dancer side by side. Both are sized against viewport HEIGHT as well
   as width: on a laptop the page has ~640px of usable height, and sizing off
   vw alone is what made it overflow. */
.stage {
  --piece: min(34vh, 30vw, 260px);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(12px, 4vw, 40px);
}

.ring-wrap {
  position: relative;
  width: var(--piece);
  aspect-ratio: 1;
  display: grid;
  place-items: center;
  filter: drop-shadow(0 0 30px rgba(255, 178, 63, 0.08));
}

.phase-ring {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.ring-track,
.ring-progress,
.ring-pulse {
  fill: none;
  stroke-width: 12;
}

.ring-track {
  stroke: rgba(143, 163, 167, 0.2);
}

.ring-progress {
  stroke: var(--amber);
  stroke-linecap: round;
  stroke-dasharray: 615.75;
  stroke-dashoffset: 615.75;
  /* no transition on stroke-dashoffset: app.js rewrites it every frame, and the
     transition animates the rewind on reset, so the ring never closed */
  transition: opacity 120ms ease, stroke 120ms ease;
}

.ring-progress.downbeat {
  stroke: var(--cyan);
}

.ring-pulse {
  stroke: rgba(255, 178, 63, 0.1);
  stroke-width: 1;
  transform-origin: center;
  transition: transform 160ms ease, opacity 160ms ease;
}

.ring-wrap.hit .ring-pulse {
  opacity: 0.95;
  transform: scale(1.15);
}

.readout {
  position: absolute;
  text-align: center;
  font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
}

.bpm {
  font-size: calc(var(--piece, 240px) * 0.26);
  line-height: 0.92;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.unit,
.countdown,
.datum span,
.confidence-head span {
  color: var(--muted);
  font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
  text-transform: uppercase;
  font-size: 12px;
}

.countdown {
  font-size: 15px;
}

.countdown span {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.sequencer {
  border-top: 1px solid rgba(143, 163, 167, 0.18);
  border-bottom: 1px solid rgba(143, 163, 167, 0.18);
  padding: 12px 0;
}

.steps {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.step {
  min-height: 54px;
  border: 1px solid var(--line);
  border-radius: 6px;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 20px;
  background: rgba(17, 31, 42, 0.72);
}

.step.downbeat {
  border-color: rgba(45, 217, 229, 0.75);
  color: var(--cyan);
}

.step.active {
  border-color: var(--amber);
  color: var(--text);
  background: rgba(255, 178, 63, 0.18);
  box-shadow: inset 0 0 0 1px rgba(255, 178, 63, 0.22), 0 0 24px rgba(255, 178, 63, 0.1);
}

.step.active.downbeat {
  border-color: var(--cyan);
  background: rgba(45, 217, 229, 0.18);
  box-shadow: inset 0 0 0 1px rgba(45, 217, 229, 0.22), 0 0 24px rgba(45, 217, 229, 0.12);
}

/* ------------------------------------------------------- onboarding

   The two sources are the first thing on the page and read as a choice, not as
   a toolbar: same size, side by side, each explaining what it captures. Once one
   is connected the whole block is replaced by the compact source bar. */
.onboarding {
  display: grid;
  gap: 10px;
}

.lead {
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

.lead strong {
  color: var(--text);
}

.choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.choice {
  display: grid;
  gap: 4px;
  padding: 14px 16px;
  text-align: left;
  min-height: 0;
  border-color: rgba(143, 163, 167, 0.35);
  background: rgba(17, 31, 42, 0.7);
}

.choice:hover {
  border-color: var(--cyan);
  background: rgba(45, 217, 229, 0.08);
}

.choice-icon {
  font-size: 20px;
  line-height: 1;
}

.choice-title {
  font-size: 16px;
  font-weight: 600;
}

.choice-desc {
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.4;
}

.sourcebar {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* ------------------------------------------------------------ fields */

.field {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
  text-transform: uppercase;
  font-size: 12px;
}

.field.grow {
  flex: 1;
  min-width: 0;
}

.field select {
  min-width: 0;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font-size: 13px;
  text-transform: none;
}

.field.grow select {
  flex: 1;
}

.field select:disabled {
  opacity: 0.5;
}

/* --------------------------------------------------------- bottom bar */

.bottombar {
  display: flex;
  align-items: center;
  gap: 14px;
}

.confidence {
  flex: 1;
  min-width: 0;
}

.confidence-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 6px;
}

.confidence strong {
  font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.bar {
  height: 8px;
  background: rgba(143, 163, 167, 0.18);
  border-radius: 999px;
  overflow: hidden;
}

.bar div {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, var(--amber), var(--green));
  transition: width 120ms ease;
}

/* ----------------------------------------------------------- details

   Diagnostics, collapsed by default: useful while developing, noise for
   someone who just wants to see the beat. */
.advanced {
  border-top: 1px solid rgba(143, 163, 167, 0.18);
  padding-top: 8px;
}

.advanced summary {
  cursor: pointer;
  color: var(--muted);
  font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
  text-transform: uppercase;
  font-size: 12px;
  list-style: none;
}

.advanced summary::before {
  content: "\25B8  ";
}

.advanced[open] summary::before {
  content: "\25BE  ";
}

.advanced-body {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  padding-top: 10px;
  align-items: start;
}

.datum {
  display: grid;
  gap: 4px;
  border: 1px solid rgba(143, 163, 167, 0.18);
  border-radius: 6px;
  background: rgba(17, 31, 42, 0.6);
  padding: 8px 10px;
}

.datum strong {
  font-family: "IBM Plex Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
}

.nudge {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 12px;
}

.hint {
  color: var(--muted);
  font-size: 12.5px;
}

/* ---------------------------------------------------------- buttons */

button {
  min-height: 38px;
  border: 1px solid rgba(143, 163, 167, 0.28);
  border-radius: 6px;
  background: rgba(233, 240, 238, 0.08);
  color: var(--text);
  padding: 0 14px;
  font: inherit;
  cursor: pointer;
}

button:hover {
  border-color: rgba(233, 240, 238, 0.55);
  background: rgba(233, 240, 238, 0.13);
}

.ghost {
  background: transparent;
  color: var(--muted);
  font-size: 13px;
}

.ghost:hover {
  color: var(--text);
}

/* ------------------------------------------------------------ phones */

@media (max-width: 700px) {
  .shell {
    width: calc(100vw - 20px);
    gap: 12px;
  }

  /* the ring and the dancer stay side by side even here: stacking them is what
     pushed the bar boxes below the fold */
  .stage {
    --piece: min(30vh, 42vw);
    gap: 10px;
  }

  .choices {
    grid-template-columns: 1fr;
  }

  .choice-desc {
    font-size: 12px;
  }

  .bottombar {
    flex-wrap: wrap;
    gap: 10px;
  }

  .confidence {
    order: 3;
    flex-basis: 100%;
  }

  .advanced-body {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .nudge {
    flex-direction: column;
    align-items: stretch;
  }
}

/* very short windows (laptop in landscape, phone in landscape) */
@media (max-height: 620px) {
  .stage {
    --piece: min(30vh, 26vw, 200px);
  }

  .steps {
    gap: 6px;
  }

  .step {
    min-height: 42px;
  }
}

/* Intensity of the active beat according to the detected accent.
   --accent goes from 0 (barely lights up) to 1 (full).
   These rules come after .step.active so they override it. */
.step.active {
  border-color: rgba(255, 178, 63, calc(0.18 + 0.82 * var(--accent, 1)));
  background: rgba(255, 178, 63, calc(0.02 + 0.16 * var(--accent, 1)));
  color: rgba(233, 240, 238, calc(0.45 + 0.55 * var(--accent, 1)));
  box-shadow:
    inset 0 0 0 1px rgba(255, 178, 63, calc(0.04 + 0.18 * var(--accent, 1))),
    0 0 24px rgba(255, 178, 63, calc(0.02 + 0.1 * var(--accent, 1)));
}

.step.active.downbeat {
  border-color: rgba(45, 217, 229, calc(0.18 + 0.82 * var(--accent, 1)));
  background: rgba(45, 217, 229, calc(0.02 + 0.16 * var(--accent, 1)));
  box-shadow:
    inset 0 0 0 1px rgba(45, 217, 229, calc(0.04 + 0.18 * var(--accent, 1))),
    0 0 24px rgba(45, 217, 229, calc(0.02 + 0.12 * var(--accent, 1)));
}

/* a beat we filled in because madmom skipped it */
.step.active.predicted {
  border-style: dashed;
}

/* ---------------------------------------------------------------- dancer

   Two independent motions, driven from app.js:

     data-arms  flips 0/1 at the move's arm subdivision
     data-hips  flips 0/1 at the move's hip subdivision
     data-style picks which set of transforms applies

   Both subdivisions come from src/dance.js and are measured in beats, so a move
   can put the arms on eighths and the hips on the bar. With the hips on the bar
   the meter is readable at a glance: in 4/4 they change every four arm swings.

   --hit  (0..1) is the accent of the current beat: how hard it bounces.
   --beat-ms is set from the measured period, so the motion tracks the tempo
   instead of running at a fixed speed. */
.dancer {
  --hit: 1;
  --flow: 0;
  --beat-ms: 160ms;
  width: var(--piece, 240px);
  filter: drop-shadow(0 12px 26px rgba(0, 0, 0, 0.45));
}

.dancer svg {
  width: 100%;
  height: auto;
  overflow: visible;
}

.dancer rect,
.dancer circle {
  fill: #cfe0e4;
}

.dancer .torso {
  fill: #9fb6bf;
}

.dancer .head rect {
  fill: #b9ced5;
}

.dancer .eye {
  fill: #0b1620;
}

.dancer .chest-light,
.dancer .antenna-tip {
  fill: var(--amber);
  transition: fill var(--beat-ms) ease;
}

.dancer .foot,
.dancer .hand {
  fill: #7e969f;
}

/* every animated group pivots from its joint, not from the svg origin */
.dancer .arm,
.dancer .hips,
.dancer .leg,
.dancer .head,
.dancer .bot {
  transform-box: fill-box;
  transition: transform var(--beat-ms) cubic-bezier(0.22, 1.2, 0.4, 1);
}

.dancer .arm-l {
  transform-origin: 50% 8%;
}

.dancer .arm-r {
  transform-origin: 50% 8%;
}

.dancer .bot {
  transform-origin: 50% 100%;
}

.dancer .head {
  transform-origin: 50% 100%;
}

.dancer .hips {
  transform-origin: 50% 0%;
}

.dancer .leg-l,
.dancer .leg-r {
  transform-origin: 50% 4%;
}

/* --- the moves ---------------------------------------------------------

   data-arms and data-hips flip 0/1 at whatever subdivision the move asks for
   (see src/dance.js), and data-style picks which transforms below apply. The
   look and the timing are separate, which is how "Swing" and "Swing double"
   share every rule here and differ only in tick rate. */

/* SWING: arms cross past each other, hips lean once a bar */
[data-style="swing"][data-arms="0"] .arm-l { transform: rotate(28deg); }
[data-style="swing"][data-arms="0"] .arm-r { transform: rotate(-64deg); }
[data-style="swing"][data-arms="1"] .arm-l { transform: rotate(64deg); }
[data-style="swing"][data-arms="1"] .arm-r { transform: rotate(-28deg); }
[data-style="swing"][data-arms="0"] .head { transform: rotate(-4deg); }
[data-style="swing"][data-arms="1"] .head { transform: rotate(4deg); }
[data-style="swing"][data-hips="0"] .hips { transform: translateX(-7px) rotate(-5deg); }
[data-style="swing"][data-hips="1"] .hips { transform: translateX(7px) rotate(5deg); }
[data-style="swing"][data-hips="0"] .leg-l { transform: rotate(7deg); }
[data-style="swing"][data-hips="0"] .leg-r { transform: rotate(3deg); }
[data-style="swing"][data-hips="1"] .leg-l { transform: rotate(-3deg); }
[data-style="swing"][data-hips="1"] .leg-r { transform: rotate(-7deg); }

/* TWIST: hips are the loud part, arms counter-rotate against them */
[data-style="twist"][data-arms="0"] .arm-l { transform: rotate(52deg); }
[data-style="twist"][data-arms="0"] .arm-r { transform: rotate(-18deg); }
[data-style="twist"][data-arms="1"] .arm-l { transform: rotate(18deg); }
[data-style="twist"][data-arms="1"] .arm-r { transform: rotate(-52deg); }
[data-style="twist"][data-arms="0"] .head { transform: rotate(7deg); }
[data-style="twist"][data-arms="1"] .head { transform: rotate(-7deg); }
[data-style="twist"][data-hips="0"] .hips { transform: rotate(-12deg) translateX(-3px); }
[data-style="twist"][data-hips="1"] .hips { transform: rotate(12deg) translateX(3px); }
[data-style="twist"][data-hips="0"] .leg-l { transform: rotate(12deg); }
[data-style="twist"][data-hips="1"] .leg-r { transform: rotate(-12deg); }

/* ROBOT: right angles and hard snaps, no easing, no squash */
[data-style="robot"] .arm,
[data-style="robot"] .head,
[data-style="robot"] .hips,
[data-style="robot"] .leg {
  transition-duration: 60ms;
  transition-timing-function: steps(2, end);
}
[data-style="robot"][data-arms="0"] .arm-l { transform: rotate(90deg); }
[data-style="robot"][data-arms="0"] .arm-r { transform: rotate(0deg); }
[data-style="robot"][data-arms="1"] .arm-l { transform: rotate(0deg); }
[data-style="robot"][data-arms="1"] .arm-r { transform: rotate(-90deg); }
[data-style="robot"][data-arms="0"] .head { transform: translateX(-5px); }
[data-style="robot"][data-arms="1"] .head { transform: translateX(5px); }
[data-style="robot"][data-hips="0"] .hips { transform: translateX(-6px); }
[data-style="robot"][data-hips="1"] .hips { transform: translateX(6px); }
[data-style="robot"].hit .bot { transform: none; }

/* JUMP: the whole body leaves the ground, legs together, arms up */
[data-style="jump"] .arm-l { transform: rotate(150deg); }
[data-style="jump"] .arm-r { transform: rotate(-150deg); }
[data-style="jump"][data-arms="0"] .arm-l { transform: rotate(158deg); }
[data-style="jump"][data-arms="1"] .arm-r { transform: rotate(-158deg); }
[data-style="jump"][data-hips="0"] .bot { transform: translateY(0); }
[data-style="jump"][data-hips="1"] .bot { transform: translateY(calc(-16px * var(--hit, 1))); }
[data-style="jump"] .leg-l { transform: rotate(4deg); }
[data-style="jump"] .leg-r { transform: rotate(-4deg); }
[data-style="jump"][data-hips="1"] .head { transform: translateY(2px); }

/* --- continuous moves --------------------------------------------------

   These read --flow, which the Dancer sweeps from -1 to 1 over a bar (or two)
   and rewrites every frame. Anything it drives must NOT have a transition: the
   value is already smooth, and a transition would just make it lag behind. */

/* SWAY: the whole lower body drifts across the bar and back, arms drift with it */
[data-style="sway"] .hips,
[data-style="sway"] .head,
[data-style="sway"] .arm {
  transition: none;
}
[data-style="sway"] .hips {
  transform: translateX(calc(var(--flow) * 12px)) rotate(calc(var(--flow) * 4deg));
}
[data-style="sway"] .head {
  transform: rotate(calc(var(--flow) * -6deg));
}
[data-style="sway"] .arm-l { transform: rotate(calc(34deg + var(--flow) * 14deg)); }
[data-style="sway"] .arm-r { transform: rotate(calc(-34deg + var(--flow) * 14deg)); }

/* HIP ROLL: hips describe a full circuit over the bar, shoulders stay put.
   The legs counter-rotate slightly so the weight reads as shifting. */
[data-style="hips"] .hips,
[data-style="hips"] .leg {
  transition: none;
}
[data-style="hips"] .hips {
  transform:
    translateX(calc(var(--flow) * 15px))
    rotate(calc(var(--flow) * 9deg));
}
[data-style="hips"] .leg-l { transform: rotate(calc(var(--flow) * 6deg)); }
[data-style="hips"] .leg-r { transform: rotate(calc(var(--flow) * 6deg)); }
[data-style="hips"][data-arms="0"] .arm-l { transform: rotate(38deg); }
[data-style="hips"][data-arms="0"] .arm-r { transform: rotate(-22deg); }
[data-style="hips"][data-arms="1"] .arm-l { transform: rotate(22deg); }
[data-style="hips"][data-arms="1"] .arm-r { transform: rotate(-38deg); }
[data-style="hips"] .head { transform: rotate(calc(var(--flow) * -3deg)); }

/* WALK: travels one way for a whole bar and back the next. The slight scale
   sells it as depth rather than a slide, and the legs still step on the beat. */
[data-style="walk"] {
  transform: translateX(calc(var(--flow) * 9%)) scale(calc(1 + var(--flow) * 0.04));
  transition: none;
}
[data-style="walk"][data-arms="0"] .arm-l { transform: rotate(34deg); }
[data-style="walk"][data-arms="0"] .arm-r { transform: rotate(-12deg); }
[data-style="walk"][data-arms="1"] .arm-l { transform: rotate(12deg); }
[data-style="walk"][data-arms="1"] .arm-r { transform: rotate(-34deg); }
[data-style="walk"][data-arms="0"] .leg-l { transform: rotate(16deg); }
[data-style="walk"][data-arms="0"] .leg-r { transform: rotate(-6deg); }
[data-style="walk"][data-arms="1"] .leg-l { transform: rotate(-6deg); }
[data-style="walk"][data-arms="1"] .leg-r { transform: rotate(16deg); }
[data-style="walk"] .head { transition: none; transform: rotate(calc(var(--flow) * 5deg)); }

/* LEAN: a single slow tilt of the whole body across two bars */
[data-style="lean"] .bot,
[data-style="lean"] .hips,
[data-style="lean"] .head {
  transition: none;
}
[data-style="lean"] .bot {
  transform: rotate(calc(var(--flow) * 7deg));
}
[data-style="lean"] .hips {
  transform: translateX(calc(var(--flow) * -8px));
}
[data-style="lean"] .head {
  transform: rotate(calc(var(--flow) * -4deg));
}
[data-style="lean"][data-arms="0"] .arm-l { transform: rotate(30deg); }
[data-style="lean"][data-arms="0"] .arm-r { transform: rotate(-46deg); }
[data-style="lean"][data-arms="1"] .arm-l { transform: rotate(46deg); }
[data-style="lean"][data-arms="1"] .arm-r { transform: rotate(-30deg); }
/* the squash would fight the body tilt, so it only scales here */
[data-style="lean"].hit .bot {
  transform: rotate(calc(var(--flow) * 7deg)) scaleY(calc(1 - 0.04 * var(--hit)));
}

/* the squash lands on the tick and releases; amplitude follows the accent */
.dancer.hit .bot {
  transform: scaleY(calc(1 - 0.07 * var(--hit))) scaleX(calc(1 + 0.05 * var(--hit)));
  transition-duration: 70ms;
}

/* the downbeat also lights the chest and the antenna */
.dancer.downbeat .chest-light,
.dancer.downbeat .antenna-tip {
  fill: var(--cyan);
}

/* nothing playing: stand still, lights off */
.dancer.idle,
.dancer.idle .bot,
.dancer.idle .arm,
.dancer.idle .hips,
.dancer.idle .leg,
.dancer.idle .head {
  transform: none !important;
}

.dancer.idle .chest-light,
.dancer.idle .antenna-tip {
  fill: #4d626c;
}

@media (prefers-reduced-motion: reduce) {
  .dancer .arm,
  .dancer .hips,
  .dancer .leg,
  .dancer .head,
  .dancer .bot {
    transition: none;
  }
}
