.accordion-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  gap: 6px;
  padding: 4px 12px 0;
}

/* ---------- Register switches ----------
 *
 * Modelled on the physical "stop buttons" found above the keyboard on a
 * piano accordion. Each button is a black plastic pill with a silver
 * (metal) plate inset showing a vertical spine; black dots punched on the
 * spine at top / middle / bottom mark which reed banks (H / M / L) the
 * register engages. Pressing one engages those reeds — exactly the visual
 * convention you'll find on a real instrument.
 *
 * The strip is positioned just above the keyboard / button array — the
 * same place the row of stops is mounted on the body of a real accordion.
 */
.register-strip {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  margin: 0 auto 4px;
  flex-wrap: wrap;
}

.register-strip-meta {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  user-select: none;
}

.register-strip-label {
  font-size: 10px;
  letter-spacing: 0.16em;
  color: var(--fg-dim);
  text-transform: uppercase;
  font-weight: 700;
}

/* Tells the player at a glance which hand the visible stops belong to —
 * because the strip swaps content when you switch between left- and
 * right-hand views. */
.register-strip-hand {
  font-size: 9px;
  letter-spacing: 0.14em;
  color: var(--accent);
  text-transform: uppercase;
  font-weight: 700;
  opacity: 0.85;
}

.register-options {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

.register-button {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* Tighter vertical packing — these buttons sit between the chrome
   * row and the keyboard and were taking ~62px of vertical real
   * estate (5+38+4+12+5). Trimmed gap and padding bring the chip
   * closer to ~48px without losing the silver-stop look. */
  gap: 2px;
  padding: 3px 6px;
  background: linear-gradient(180deg, #2a2a2a 0%, #0d0d0d 100%);
  border: 1px solid #000;
  border-radius: 10px;
  color: rgba(255, 255, 255, 0.6);
  cursor: pointer;
  font-family: inherit;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), inset 0 -2px 4px rgba(0, 0, 0, 0.6),
    0 1px 2px rgba(0, 0, 0, 0.5);
  transition: transform 80ms ease, box-shadow 120ms ease;
}

.register-button:hover .register-stop {
  filter: brightness(1.05);
}

.register-button:active {
  transform: translateY(1px);
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.7), 0 0 0 rgba(0, 0, 0, 0);
}

.register-button.selected {
  background: linear-gradient(180deg, #4a4a4a 0%, #1a1a1a 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.18), inset 0 -2px 4px rgba(0, 0, 0, 0.6),
    0 0 0 1px rgba(168, 85, 247, 0.85), 0 6px 16px -10px rgba(168, 85, 247, 0.7);
  color: rgba(255, 255, 255, 0.92);
}

/* Silver "stop" plate — the rectangular metal insert in the middle of the
 * black button. Engraved with a vertical spine via ::before. */
.register-stop {
  position: relative;
  width: 18px;
  height: 28px;
  background: linear-gradient(180deg, #f3f4f6 0%, #c4c4c4 50%, #9a9a9a 100%);
  border: 1px solid #6b6b6b;
  border-radius: 3px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.7), inset 0 -1px 0 rgba(0, 0, 0, 0.2);
}

.register-stop::before {
  content: '';
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 50%;
  width: 1.5px;
  background: rgba(0, 0, 0, 0.55);
  transform: translateX(-50%);
  border-radius: 1px;
}

/* The dots are only rendered when their reed is active — appended by JS. */
.register-dot {
  position: absolute;
  width: 5px;
  height: 5px;
  background: radial-gradient(circle at 30% 30%, #2a2a2a, #000 70%);
  border-radius: 50%;
  left: 50%;
  box-shadow: 0 0 1px rgba(0, 0, 0, 0.6);
}

.register-dot.h {
  top: 24%;
  transform: translate(-50%, -50%);
}

.register-dot.m {
  top: 50%;
  transform: translate(-50%, -50%);
}

.register-dot.l {
  top: 76%;
  transform: translate(-50%, -50%);
}

.register-label {
  font-size: 9px;
  letter-spacing: 0.12em;
  color: inherit;
  text-transform: uppercase;
  font-weight: 700;
  line-height: 1;
}

/* Compact "current register" pill — only visible when the strip
 * collapses (short viewports). On desktop and portrait phones the
 * full strip of stops is what the player sees; the toggle hides
 * itself out of the way. */
.register-toggle {
  display: none;
  align-items: center;
  gap: 6px;
  padding: 3px 8px 3px 6px;
  background: linear-gradient(180deg, #2a2a2a 0%, #0d0d0d 100%);
  border: 1px solid #000;
  border-radius: 10px;
  color: rgba(255, 255, 255, 0.85);
  cursor: pointer;
  font-family: inherit;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), inset 0 -2px 4px rgba(0, 0, 0, 0.6),
    0 1px 2px rgba(0, 0, 0, 0.5);
}

.register-toggle:active {
  transform: translateY(1px);
}

.register-toggle-stop {
  /* Mirror of the active register's silver plate — JS clones the dot
   * pattern of whichever stop is currently engaged. */
  width: 14px;
  height: 22px;
}

.register-toggle-name {
  line-height: 1;
}

.register-toggle-chevron {
  font-size: 8px;
  opacity: 0.7;
  line-height: 1;
  transition: transform 120ms ease;
}

.register-toggle[aria-expanded='true'] .register-toggle-chevron {
  transform: rotate(180deg);
}

@media (max-width: 720px) {
  .register-strip {
    /* Tighter than desktop because every vertical pixel here pushes
     * the keyboard further down on a phone. */
    gap: 6px;
    padding: 2px 6px;
    margin: 0 auto 2px;
  }
  .register-button {
    padding: 2px 5px 3px;
    border-radius: 8px;
  }
  .register-stop {
    width: 14px;
    height: 22px;
  }
  .register-dot {
    width: 4px;
    height: 4px;
  }
  .register-label {
    font-size: 8px;
  }
}

/* JS moves the strip between `.accordion-stage` (desktop) and
 * `.instrument-controls` (mobile). Until it's placed we keep it
 * invisible so a mobile visitor doesn't briefly see the desktop
 * full-stop strip flash above the keyboard before being relocated
 * up into the chrome row. */
.register-strip:not([data-placed]) {
  visibility: hidden;
}

/* ---------- Collapsed register strip (mobile) ----------
 *
 * On phones (portrait or landscape) we collapse the register strip
 * into a single "current register" pill that sits inline with the
 * other chrome controls; the JS moves the element into the chrome
 * row at this breakpoint. Tapping the pill opens a popover anchored
 * to it that holds the full set of stops.
 */
@media (max-width: 720px), (max-height: 540px) {
  .register-strip {
    /* Anchor the popover to the strip and let the pill take its
     * natural width — no big horizontal gap, no wrap. */
    position: relative;
    gap: 6px;
    padding: 0;
    margin: 0;
    flex-wrap: nowrap;
  }
  /* When the strip lives inline with the chrome controls (mobile)
   * we want the small "REGISTER" tag visible — every other item in
   * that row has a label, and a bare silver-stop pill with no
   * caption is harder to read at a glance. The desktop strip in
   * .accordion-stage uses its own column-stacked meta block, so
   * we only re-style when the strip is in .instrument-controls. */
  .instrument-controls .register-strip-meta {
    display: inline-flex;
    flex-direction: row;
    align-items: center;
    gap: 4px;
  }
  .instrument-controls .register-strip-label {
    text-transform: uppercase;
    font-size: 10px;
    letter-spacing: 0.06em;
    color: var(--fg-dim);
  }
  .instrument-controls .register-strip-hand {
    /* The active hand is implicit from the View dropdown — hide
     * the sub-label inline so the chrome row stays tight. */
    display: none;
  }
  /* If the strip ever ends up back in the stage on a mobile width
   * (transient during JS placement, or no-JS), keep the meta hidden
   * so the inline strip styling above doesn't leak through. */
  .accordion-stage .register-strip-meta {
    display: none;
  }
  .register-toggle {
    display: inline-flex;
  }
  /* Inline strip becomes the popover panel: hidden by default, shown
   * (anchored beneath the toggle) when the toggle is open. We keep
   * the buttons rendered so the radiogroup state survives the toggle
   * collapse, but hide them visually until the player asks. */
  .register-options {
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 50%;
    transform: translateX(-50%);
    z-index: 50;
    flex-wrap: wrap;
    gap: 6px;
    /* Without an explicit sizing hint flexbox shrink-fits to the
     * narrowest child, forcing every stop onto its own line — which
     * with 6 right-hand registers blows past a 360-tall landscape
     * viewport. `max-content` keeps the popover horizontal whenever
     * it can; `max-width: 100vw - 16px` lets it wrap onto a second
     * row on truly narrow phones rather than overflow off-screen. */
    width: max-content;
    max-width: calc(100vw - 16px);
    padding: 6px 8px;
    background: linear-gradient(180deg, #1a1a1a 0%, #0a0a0a 100%);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 10px;
    box-shadow: 0 8px 24px -4px rgba(0, 0, 0, 0.7);
  }
  .register-strip[data-collapsed-open='true'] .register-options {
    display: flex;
  }
  /* Restore the full per-button affordance inside the popover — these
   * are now the primary tap targets, not crammed into a strip, so the
   * label can come back. */
  .register-button {
    padding: 3px 6px;
    border-radius: 8px;
    gap: 2px;
  }
  .register-label {
    display: inline;
    font-size: 9px;
  }
}

/* ---------- Landscape phone: REGISTER tightening ----------
 *
 * On landscape phones the chrome row is already two-deep (the four
 * selects fill row 1; REGISTER often wraps to row 2 on its own and
 * sits centered with a lot of empty whitespace either side). At this
 * height we drop the inline "REGISTER" label so the pill alone is
 * small enough to fit alongside the other controls on row 1, and
 * kill the row-gap so any remaining wrap doesn't add a visible
 * vertical band above the keyboard. The silver-stop pattern +
 * active-register letter inside the pill still identifies it without
 * the textual caption. */
@media (max-height: 540px) {
  .instrument-controls {
    row-gap: 0;
  }
  .instrument-controls .register-strip-meta {
    display: none;
  }
  .instrument-controls .register-strip {
    gap: 0;
  }
}

/* ---------- Bellows mode ----------
 *
 * Phone-as-bellows: a checkbox plus a thin live meter showing how much
 * "air" the player is currently pushing through the instrument. The meter
 * fills based on smoothed accelerometer magnitude (driven from JS via
 * `--bellows-pressure: <0..1>`).
 */
.bellows-control {
  align-items: center;
  gap: 8px;
}

.bellows-meter {
  display: inline-block;
  width: 64px;
  height: 6px;
  border-radius: 3px;
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid var(--border);
  overflow: hidden;
  vertical-align: middle;
  --bellows-pressure: 0;
}

.bellows-meter-fill {
  display: block;
  height: 100%;
  width: calc(var(--bellows-pressure) * 100%);
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border-radius: 3px;
  transition: width 60ms linear;
}

/* When bellows mode is active the meter glows to draw the eye to it. */
.bellows-control.is-active .bellows-meter {
  border-color: rgba(168, 85, 247, 0.65);
  box-shadow: 0 0 8px -2px rgba(168, 85, 247, 0.5);
}

/* The "stage" hosts whichever single view the player has selected. */
.accordion-view {
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 4px;
  display: flex;
  justify-content: center;
}

.accordion-view .piano-keyboard {
  width: 100%;
  margin: 0 auto;
  max-width: 980px;
}

.accordion-view[data-kind='stradella'],
.accordion-view[data-kind='chromatic'] {
  /* Both button systems can be wide; let them breathe horizontally. */
  justify-content: flex-start;
  /* Narrow desktop viewports (720–1024px) can still drive `--btn-size`
   * down to its 26px floor and produce a row wider than the container —
   * particularly the diagonal bottom row of a 120-bass Stradella. The
   * mobile breakpoint below already adds `overflow: auto` for touch
   * devices, but desktop mouse users need an escape too. Allow the
   * .accordion-view to scroll horizontally whenever its button system
   * exceeds the container; the keyboard remains centred while it fits. */
  overflow-x: auto;
}

.accordion-view[data-kind='stradella'] > *,
.accordion-view[data-kind='chromatic'] > * {
  width: 100%;
}

/* ---------- Phone-sized viewports ----------
 *
 * Same strategy as the standalone piano page: enforce a minimum tap
 * target for buttons / keys and let the host scroll horizontally when
 * the layout is wider than the viewport. With `touch-action: pan-x` on
 * the buttons themselves, the browser handles horizontal scroll
 * natively (with momentum); the JS in stradella.js / chromatic.js /
 * shared/keyboard.js just defers the press by ~80ms so a swipe that
 * starts on a button doesn't accidentally fire it (see
 * `play/shared/scroll-gesture.js`). */
@media (max-width: 720px), (hover: none) and (pointer: coarse) {
  /* The shared rule in `play/style.css` makes `.accordion-stage` a
   * flex:1 / overflow:auto playing area — but the register strip lives
   * inside it (right above the buttons, like on a real instrument) and
   * we want it pinned, not scrolling. Override: stage is a flex column
   * with hidden overflow, register strip stays at natural height,
   * `.accordion-view` underneath fills the rest and owns the scroll. */
  .accordion-stage {
    overflow: hidden;
    padding: 4px 0 0;
    gap: 4px;
  }

  .accordion-stage > .register-strip {
    flex: 0 0 auto;
  }

  .accordion-view {
    flex: 1 1 0;
    min-height: 0;
    /* Both axes — horizontal layouts (landscape stradella, wide piano)
     * overflow X; vertical layouts (portrait stradella / chromatic)
     * overflow Y. `auto` shows scrollbars only when actually needed. */
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    /* Centering inside an overflowing container strands the left/top
     * half (scrollLeft/scrollTop can't go negative). Pin to the start
     * so scrolling reaches every column / row. The piano sub-view
     * still re-applies horizontal centering via the piano-keyboard's
     * own width constraint, so a layout that fits the viewport stays
     * centered. */
    justify-content: flex-start;
    /* Edge-to-edge so the swipe-to-scroll affordance reaches the screen
     * edges and there's no dead margin to grab. */
    padding: 0;
  }
}

/* ---------- Stradella bass (120-button, diagonal grid) ----------
 *
 * Real 120-bass accordions don't lay out their buttons on a rectangular
 * grid — each successive row is offset by ~half a button-width, producing
 * the diagonal "columns" the player navigates by feel.
 *
 * We replicate that here by: (1) using fixed-size circular buttons in a
 * flex row, (2) giving each row class a `--row-offset` value (0, 0.5, 1,
 * 1.5, …) and (3) translating the row's button strip horizontally by
 * `row-offset × column-step`. Result: columns slope down-right, just like
 * the real instrument.
 */

.stradella-bass {
  display: flex;
  flex-direction: column;
  /* Center rows horizontally; `safe` falls back to flex-start when content
   * overflows so we don't clip the left edge on small screens. */
  align-items: safe center;
  gap: 4px;
  /* Horizontal padding has to clear the 14px container radius so the
   * diagonal bottom row's last button doesn't kiss the bottom-right curve.
   * 18px gives ~4px of breathing past the radius even at the tightest
   * 1280px desktop fit. */
  padding: 14px 18px;
  background: linear-gradient(180deg, #0b1220, #1f2937);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 8px 24px -16px rgba(0, 0, 0, 0.7);
  user-select: none;
  -webkit-user-select: none;
  /* The container itself stays scrollable — touches that land in the empty
   * gaps between buttons (e.g. the diagonal stagger) should let the user
   * scroll the page. The buttons get `touch-action: pan-x` individually
   * (see `.stradella-button` below) so a swipe that starts on a button
   * still scrolls horizontally; vertical drags are reserved for our
   * drag-play across rows. */
  touch-action: auto;
  /* Sizing knobs the row offset and button widths reference. The button
   * size auto-grows when there are fewer columns (e.g. 12- or 24-bass
   * instruments) so smaller layouts use the available space; clamps stop
   * it from going too tiny for touch on full 120-bass layouts and from
   * ballooning on huge desktops. */
  --btn-gap: 4px;
  /* Per-row diagonal stagger pushes successive rows right by `--row-offset *
   * --col-step`. The widest (last) row therefore needs `col-count + max-row-
   * offset` button-widths of horizontal space to fit, not just `col-count`.
   * `--max-row-offset` is `(row-count − 1) × 0.5` for the layouts here
   * (standard 6 → 2.5, eastern 5 → 2, free-bass 3 → 1). Without this term
   * the bottom row's tail would clip past the container on a fully-laid-out
   * 120-bass on a 1280px desktop. */
  --max-row-offset: calc((max(var(--row-count, 6), 1) - 1) * 0.5);
  /* Available width inside the keyboard's content area:
   *   100vw − stage padding (24) − accordion-view padding (8)
   *        − stradella-bass padding (36) − label column (88) = 100vw − 156
   * BUT `.accordion-view` is capped at `max-width: 1280px`, so on wide
   * monitors the actual content area never grows past `1280 − 8 − 36 − 88
   * = 1148`. Without the `min(...)` cap the formula keeps growing past
   * its real container, the clamp pins btn-size at the 56px maximum, and
   * the diagonal bottom row overflows hundreds of pixels on a 1920px
   * display. The cap pins the math to the real container above 1304px
   * and is a no-op below it. */
  --auto-btn: calc(
    min(100vw - 156px, 1148px) / (max(var(--col-count, 20), 1) + var(--max-row-offset)) -
      var(--btn-gap)
  );
  --btn-size: clamp(26px, var(--auto-btn), 56px);
  --col-step: calc(var(--btn-size) + var(--btn-gap));
  overflow: visible;
}

.stradella-row {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: var(--btn-gap);
  width: max-content; /* let the row grow to fit 20 staggered buttons */
}

/* Horizontal-mode equal-width trick: every row pads its right side by the
 * *complement* of its own diagonal offset, so all rows render at the same
 * total width as the bottom row. Combined with the container's `align-
 * items: safe center`, this keeps every row's left edge aligned — the
 * staircase comes purely from the per-row leading margin on the first
 * button. Without this, shorter upper rows would each centre on their
 * own midpoint and the diagonal would look bent.
 *
 * Scoped to horizontal orientation only: in vertical mode the row's
 * width is fixed to `var(--btn-size)` (one button wide) and adding
 * padding-right would, under the global `box-sizing: border-box`,
 * eat into the content area and squash the buttons. */
.stradella-bass:not([data-orientation='vertical']) .stradella-row {
  padding-right: calc((var(--max-row-offset, 0) - var(--row-offset, 0)) * var(--col-step));
}

.stradella-row-label {
  width: 80px;
  flex: 0 0 80px;
  margin-right: 8px;
  font-size: 10px;
  font-weight: 600;
  color: var(--muted, #94a3b8);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  text-align: right;
  padding-right: 4px;
}

/* Diagonal stagger — push only the first button so the label stays put. */
.stradella-row > .stradella-button:first-of-type {
  margin-left: calc(var(--row-offset, 0) * var(--col-step));
}

/* Per-row diagonal offsets. Each successive row shifts half a column. */
.stradella-row-counter-bass {
  --row-offset: 0;
}
.stradella-row-bass {
  --row-offset: 0.5;
}
.stradella-row-major {
  --row-offset: 1;
}
.stradella-row-minor {
  --row-offset: 1.5;
}
.stradella-row-dom7 {
  --row-offset: 2;
}
.stradella-row-dim7 {
  --row-offset: 2.5;
}

/* Free-bass uses gentler stagger across just three rows. */
.stradella-row-free-low {
  --row-offset: 0;
}
.stradella-row-free-mid {
  --row-offset: 0.5;
}
.stradella-row-free-high {
  --row-offset: 1;
}

.stradella-button {
  -webkit-appearance: none;
  appearance: none;
  border: 1px solid rgba(148, 163, 184, 0.45);
  background: linear-gradient(180deg, #1e293b, #0f172a);
  color: #e2e8f0;
  border-radius: 999px;
  font-family: inherit;
  /* Heavier weight so chord glyphs (M / m / 7 / °) and the small note
   * letters on counter-bass / bass stay legible on the 26px floor. */
  font-weight: 700;
  cursor: pointer;
  /* `pan-x pan-y` lets the browser handle horizontal AND vertical
   * panning natively (with momentum) on the `.accordion-view`
   * scroll container. Whichever axis actually overflows is the one
   * the browser uses for that gesture — landscape stradella overflows
   * X, portrait stradella overflows Y, dense layouts can overflow
   * both. The page chrome above stays pinned by the body-locked rule
   * in `play/style.css`. We trade vertical drag-glissando across rows
   * for native scroll in both axes (the user's QoL ask). */
  touch-action: pan-x pan-y;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--btn-size);
  height: var(--btn-size);
  flex: 0 0 var(--btn-size);
  padding: 0;
  font-size: calc(var(--btn-size) * 0.42);
  line-height: 1;
  transition: transform 80ms ease-out, background 80ms ease-out, box-shadow 80ms ease-out;
}

.stradella-button:focus-visible {
  outline: 2px solid #f472b6;
  outline-offset: 2px;
}

.stradella-button.active {
  transform: translateY(1px) scale(0.94);
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.5);
}

/* Counter-bass and bass: bigger, single-note buttons. */
.stradella-button-counter-bass,
.stradella-button-bass,
.stradella-button-free-low,
.stradella-button-free-mid,
.stradella-button-free-high {
  /* Note names ("A" / "C♯" / "B♭") scale with button size like the
   * chord glyphs do. Slightly larger ratio (0.46 vs 0.42) because the
   * letterforms have less vertical mass than 'M' / 'm' / '7' / '°' and
   * benefit from a touch more presence. Two-character names (e.g. "C♯")
   * still fit inside the circular chip at this ratio because letter
   * advance is roughly 0.55–0.6 of font-size in the inherited UI font. */
  font-size: calc(var(--btn-size) * 0.46);
  background: linear-gradient(180deg, #f8fafc, #cbd5e1);
  color: #0f172a;
  border-color: #475569;
}

.stradella-button-counter-bass {
  background: linear-gradient(180deg, #e0f2fe, #bae6fd);
  color: #0c4a6e;
  border-color: #0284c7;
}

/* Home buttons (F, C, G) — visible reference points the player feels for. */
.stradella-button.is-home {
  background: linear-gradient(180deg, #fecaca, #fca5a5);
  color: #7f1d1d;
  border-color: #dc2626;
  box-shadow: inset 0 -2px 4px rgba(127, 29, 29, 0.3);
}

.stradella-button.is-home.active {
  background: linear-gradient(180deg, #fca5a5, #ef4444);
  color: #fff;
}

/* Chord rows: smaller label, color-coded subtle tints. */
.stradella-button-major {
  background: linear-gradient(180deg, #052e16, #14532d);
  color: #bbf7d0;
  border-color: #166534;
}

.stradella-button-minor {
  background: linear-gradient(180deg, #042f2e, #134e4a);
  color: #99f6e4;
  border-color: #115e59;
}

.stradella-button-dom7 {
  background: linear-gradient(180deg, #422006, #78350f);
  color: #fed7aa;
  border-color: #92400e;
}

.stradella-button-dim7 {
  background: linear-gradient(180deg, #450a0a, #7f1d1d);
  color: #fecaca;
  border-color: #991b1b;
}

/* Free-bass octave coloring. */
.stradella-button-free-low {
  background: linear-gradient(180deg, #cbd5e1, #94a3b8);
  color: #0f172a;
}
.stradella-button-free-high {
  background: linear-gradient(180deg, #ede9fe, #c4b5fd);
  color: #312e81;
  border-color: #6d28d9;
}

/* Phone-sized viewports (and any touch-only device, regardless of width)
 * get noticeably larger Stradella buttons so they're comfortable to press
 * with a fingertip rather than aimed at with a cursor.
 *
 * The min in `clamp()` (38px) is the tap-target floor — when a wide layout
 * (e.g. 120-bass × 20 cols) would force buttons below it, the row instead
 * grows past the viewport and the parent `.accordion-view` scrolls
 * horizontally (its `overflow-x: auto` is set in the
 * `@media (max-width: 720px)` block above). */
@media (max-width: 720px), (hover: none) and (pointer: coarse) {
  .stradella-bass {
    padding: 10px 8px;
    /* Mobile auto-fit: same formula but with bigger floor/cap so picking
     * a 12- or 24-bass layout fills the screen with chunky touch targets.
     * Same `+ max-row-offset` correction as desktop — when the floor
     * (38px) clamps the size, `.accordion-view` scrolls horizontally
     * either way; this just makes the keyboard fit on tablets / phones
     * in landscape that *can* render 22.5 columns at the floor size.
     * The 1192px cap mirrors the desktop cap for big landscape tablets:
     *   1280 (av max-width) − 8 (av padding) − 16 (sb mobile padding)
     *                       − 64 (mobile label) = 1192. */
    --auto-btn: calc(
      min(100vw - 88px, 1192px) / (max(var(--col-count, 20), 1) + var(--max-row-offset)) -
        var(--btn-gap)
    );
    --btn-size: clamp(38px, var(--auto-btn), 64px);
    --btn-gap: 5px;
  }
  .stradella-button {
    /* Slightly larger glyphs to match the bigger buttons. */
    font-size: calc(var(--btn-size) * 0.42);
  }
  .stradella-row-label {
    width: 56px;
    flex-basis: 56px;
    font-size: 9px;
    letter-spacing: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .stradella-button {
    transition: none;
  }
}

/* ---------- Stradella, vertical orientation ----------
 *
 * Same DOM as horizontal — `.stradella-row` elements with a label and a
 * `.stradella-cells` strip of 20 buttons. We just rotate the layout so each
 * "row" becomes a vertical column, and the diagonal stagger now goes
 * downward instead of rightward.
 */
.stradella-bass[data-orientation='vertical'] {
  flex-direction: row;
  align-items: flex-start;
  /* Center the row-columns horizontally on wide viewports. */
  justify-content: safe center;
  gap: 4px;
  overflow: visible;
  /* `.accordion-view` is `display: flex; align-items: stretch` (default),
   * so by default the keyboard gets stretched to the parent's cross-axis
   * height — a fixed pixel box on mobile (where `.accordion-view` is
   * `flex: 1 1 0`). The keyboard's content (20 staggered buttons per
   * column, 6 columns) is much taller than that, so under the existing
   * `overflow: visible` the buttons escape past the dark gradient
   * background and float on the page background as the user scrolls.
   * `align-self: start` opts out of the stretch and lets the keyboard
   * size to its content; `.accordion-view`'s `overflow-y: auto` (mobile
   * media block above) then provides the scroll container. */
  align-self: flex-start;
  /* In vertical mode the row-count drives horizontal fit (each Stradella
   * "row" becomes a vertical column of buttons). Auto-fit on row-count
   * so a 12-bass (2 rows) gets huge buttons, while a 6-row standard
   * still fits comfortably. */
  --btn-gap: 6px;
  --auto-btn: calc((100vw - 32px) / max(var(--row-count, 6), 1) - var(--btn-gap));
  --btn-size: clamp(48px, var(--auto-btn), 80px);
}

/* Mobile vertical: hug-the-viewport layout makes it impossible to start
 * a page scroll because there's no empty space outside the keyboard for
 * a finger to land in. Reserve a side gutter and shrink the auto-fit
 * budget to match. The 96px subtracted from 100vw covers the side
 * margins (48), the kb's own horizontal padding (16), and the page-
 * level padding around `.accordion-stage` and `.accordion-view` (32). */
@media (max-width: 720px), (hover: none) and (pointer: coarse) {
  .stradella-bass[data-orientation='vertical'] {
    margin-left: 24px;
    margin-right: 24px;
    /* Page-level breathing room *below* the keyboard so the diagonal-
     * deepest button (the last dim7 / dom7 in the staggered column,
     * which sits 2.5 col-steps lower than the counter-bass column)
     * clears the system nav bar / home indicator when the user scrolls
     * all the way down. `env(safe-area-inset-bottom)` picks up the iOS
     * home-indicator inset and the Android gesture-bar inset; the
     * +72px is plain visual breathing on top. */
    margin-bottom: calc(env(safe-area-inset-bottom, 0px) + 72px);
    --auto-btn: calc((100vw - 96px) / max(var(--row-count, 6), 1) - var(--btn-gap));
    /* Slightly lower floor than desktop so 6-row layouts can shrink
     * buttons enough to fit without blowing past the side gutters. */
    --btn-size: clamp(40px, var(--auto-btn), 80px);
  }
}

.stradella-bass[data-orientation='vertical'] .stradella-row {
  flex-direction: column;
  align-items: center;
  width: var(--btn-size);
  height: max-content;
  gap: var(--btn-gap);
}

.stradella-bass[data-orientation='vertical'] .stradella-row-label {
  margin: 0 0 6px;
  padding: 0;
  width: var(--btn-size);
  flex: 0 0 auto;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: none;
  text-align: center;
  color: var(--muted, #cbd5e1);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}

/* Vertical stagger lives on the first button instead of margin-left. */
.stradella-bass[data-orientation='vertical'] .stradella-row > .stradella-button:first-of-type {
  margin-left: 0;
  margin-top: calc(var(--row-offset, 0) * var(--col-step));
}

/* ---------- Chromatic right-hand button accordion ---------- */

.chromatic-keyboard {
  display: flex;
  /* Cross-row spacing comes from the regular flex `gap` — the honeycomb
   * feel is provided by the half-button horizontal stagger on alternating
   * rows (`--row-offset` set in JS), not by physically tucking rows into
   * each other. True hex close-packing (rows overlapping by √3/2) felt
   * cramped on a touch UI; this looser layout matches how the reference
   * CBA spelling charts are drawn — clearly separated rows with a
   * half-button shift. */
  gap: var(--btn-gap);
  padding: 14px 12px;
  background: linear-gradient(180deg, #1e1b4b, #0b1220);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05), 0 8px 24px -16px rgba(0, 0, 0, 0.7);
  user-select: none;
  -webkit-user-select: none;
  /* Container scrolls; the buttons themselves capture the gesture. */
  touch-action: auto;
  /* Auto-fit the button size to the chosen layout, same trick as the
   * Stradella side. `--col-count` and `--row-count` are set by
   * chromatic.js when a layout is built. */
  --btn-gap: 8px;
  --auto-btn: calc((100vw - 32px) / max(var(--col-count, 16), 1) - var(--btn-gap));
  --btn-size: clamp(32px, var(--auto-btn), 56px);
  --col-step: calc(var(--btn-size) + var(--btn-gap));
}

.chromatic-keyboard[data-orientation='horizontal'] {
  /* Render row 0 at the BOTTOM. On a real CBA, "row 1" of the
   * traditional spelling charts (the row closest to the player's wrist)
   * sits at the bottom, with helper / duplicate rows further from the
   * body. Source order in the DOM is row 0 → row N, so reverse. */
  flex-direction: column-reverse;
  align-items: safe center;
  overflow: visible;
}

.chromatic-keyboard[data-orientation='vertical'] {
  flex-direction: row;
  align-items: flex-start;
  justify-content: safe center;
  overflow: visible;
  /* Same fix as `.stradella-bass[data-orientation='vertical']`:
   * `.accordion-view` is `display: flex; align-items: stretch` (default),
   * so by default the keyboard gets stretched to the parent's cross-axis
   * height — a fixed pixel box on mobile (where `.accordion-view` is
   * `flex: 1 1 0`). The keyboard's content (15+ buttons per column,
   * 4–5 columns) is much taller than that, so under the existing
   * `overflow: visible` the buttons escape past the dark gradient
   * background as the user scrolls. `align-self: start` opts out of the
   * stretch and lets the keyboard size to its content; `.accordion-view`'s
   * `overflow-y: auto` (mobile media block) provides the scroll
   * container. */
  align-self: flex-start;
  /* In vertical mode the row-count drives horizontal fit (each chromatic
   * "row" becomes a vertical column). 3-row models get huge buttons,
   * 5-row stays comfortable. */
  --btn-gap: 6px;
  --auto-btn: calc((100vw - 32px) / max(var(--row-count, 4), 1) - var(--btn-gap));
  --btn-size: clamp(48px, var(--auto-btn), 88px);
}

.chromatic-row {
  display: flex;
  gap: var(--btn-gap);
  flex-wrap: nowrap;
  padding-left: calc(var(--row-offset, 0) * var(--col-step));
  width: max-content;
}

.chromatic-col {
  display: flex;
  /* Column reads top-to-bottom matching the horizontal layout's
   * left-to-right note order (i.e. the start of each row's pattern at
   * the top). DOM order is c=0..cols-1, so plain `column` matches. */
  flex-direction: column;
  gap: var(--btn-gap);
  padding-top: calc(var(--col-offset, 0) * var(--col-step));
  height: max-content;
}

.chromatic-button {
  -webkit-appearance: none;
  appearance: none;
  border-radius: 999px;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  touch-action: pan-x pan-y; /* see `.stradella-button` for rationale */
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--btn-size);
  height: var(--btn-size);
  flex: 0 0 var(--btn-size);
  padding: 0;
  font-size: calc(var(--btn-size) * 0.34);
  line-height: 1;
  transition: transform 80ms ease-out, background 80ms ease-out, box-shadow 80ms ease-out;
}

.chromatic-button:focus-visible {
  outline: 2px solid #a78bfa;
  outline-offset: 2px;
}

/* Naturals (the white piano keys: C D E F G A B) — light buttons.
 * Accidentals (the black keys: C♯ D♯ F♯ G♯ A♯) — dark buttons. This
 * matches the convention real CBA spelling charts use, so the player
 * can read pitch class at a glance instead of decoding row position. */
.chromatic-button.is-natural {
  background: linear-gradient(180deg, #ede9fe, #c4b5fd);
  color: #1e1b4b;
  border: 1px solid rgba(99, 92, 161, 0.55);
}

.chromatic-button.is-accidental {
  background: linear-gradient(180deg, #1e1b4b, #0a0a25);
  color: #c4b5fd;
  border: 1px solid rgba(124, 58, 237, 0.45);
}

/* C buttons: visual home base for the player. Light like other naturals
 * but with a gold accent ring so they pop against the rest of the
 * keyboard. */
.chromatic-button.is-c {
  background: linear-gradient(180deg, #fef3c7, #fcd34d);
  color: #422006;
  border-color: #b45309;
  box-shadow: 0 0 0 1px rgba(251, 191, 36, 0.35);
}

.chromatic-button.active {
  transform: translateY(1px) scale(0.94);
  box-shadow: inset 0 2px 6px rgba(0, 0, 0, 0.5);
  filter: brightness(1.4);
}

@media (prefers-reduced-motion: reduce) {
  .chromatic-button {
    transition: none;
  }
}

@media (max-width: 720px), (hover: none) and (pointer: coarse) {
  .chromatic-keyboard {
    /* Mobile: same auto-fit math but with chunkier touch-friendly bounds.
     * 40px floor matches the tap-target minimum; wider layouts (e.g.
     * 16-col 5-row CBA) overflow `.accordion-view` and rely on the
     * scroll-gesture heuristic in chromatic.js. */
    --auto-btn: calc((100vw - 24px) / max(var(--col-count, 16), 1) - var(--btn-gap));
    --btn-size: clamp(40px, var(--auto-btn), 64px);
    --btn-gap: 5px;
    padding: 10px 8px;
  }
}

/* Mobile vertical chromatic: same gutter treatment as Stradella so the
 * user has empty space to start a page scroll. Sits after the
 * unconditional vertical rule for source-order priority. */
@media (max-width: 720px), (hover: none) and (pointer: coarse) {
  .chromatic-keyboard[data-orientation='vertical'] {
    margin-left: 24px;
    margin-right: 24px;
    /* Match the Stradella vertical-mobile margin so the deepest
     * staggered button (last row in the bottom column) clears the
     * system nav bar / home indicator when scrolled to the bottom.
     * `env(safe-area-inset-bottom)` picks up the iOS home-indicator
     * inset and Android gesture-bar inset; the +72px is plain visual
     * breathing on top. */
    margin-bottom: calc(env(safe-area-inset-bottom, 0px) + 72px);
    --auto-btn: calc((100vw - 96px) / max(var(--row-count, 4), 1) - var(--btn-gap));
    --btn-size: clamp(44px, var(--auto-btn), 88px);
  }
}

/* Helper rows (4th and 5th rows on real CBAs) are MIDI-identical to
 * rows 1 and 2 respectively. They no longer need their own tint —
 * pitch-class coloring (natural / accidental / C) already tells the
 * player which note is which — but they get a subtle inner shadow so
 * the eye can tell where the helper rows start. */
.chromatic-row-duplicate .chromatic-button,
.chromatic-col-duplicate .chromatic-button {
  box-shadow: inset 0 0 0 1px rgba(196, 181, 253, 0.15);
}

/* ---------- Landscape phone: tighten keyboard against chrome ----------
 *
 * Sits below the mobile-touch keyboard rules so it can override their
 * 10px vertical padding. On landscape phones every pixel of vertical
 * real-estate counts. Three changes:
 *   - Trim the keyboard container's vertical padding so the buttons
 *     don't have a wide breathing band inside the rounded border.
 *   - `align-self: start` on the horizontal keyboard so it takes its
 *     natural content height instead of being stretched by
 *     `.accordion-view`'s default `align-items: stretch`. Without
 *     this the chromatic keyboard's `column-reverse` packing leaves
 *     a visible dark band of empty container above the topmost row;
 *     and the Stradella's diagonal staircase ends with the same
 *     visual gap above row 0. The leftover stage space below the
 *     keyboard is just the page background and reads as natural
 *     breathing room.
 *   - `justify-content: flex-start` on the stage so the keyboard
 *     hugs the chrome instead of being centered vertically. On real
 *     touch phones the mobile-touch override gives `.accordion-view`
 *     `flex: 1 1 0`, which already fills the stage; this rule covers
 *     the narrow-desktop landscape case where that override doesn't
 *     fire but the cramped vertical space still benefits from
 *     pinning content to the top. */
@media (max-height: 540px) {
  .accordion-stage {
    justify-content: flex-start;
  }
  .chromatic-keyboard,
  .stradella-bass {
    padding-top: 4px;
    padding-bottom: 6px;
  }
  .chromatic-keyboard[data-orientation='horizontal'],
  .stradella-bass:not([data-orientation='vertical']) {
    align-self: start;
  }
}
