/* 音乐启蒙 · Music — staff on top, keyboard below, one stacked layout for
   portrait (one octave C4–C5) and landscape (three octaves C3–C6 on a longer
   staff). brand.css provides the body baseline.
   Pitch colors follow Boomwhackers tuned toward the site palette; the note
   colors themselves are set from app.js (single source of truth there). */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--paper);
  overflow: hidden;
  /* flex column: the shell takes whatever the brand header leaves, so the
     keyboard never slides under the viewport bottom at any header height */
  display: flex;
  flex-direction: column;
}

/* ---------------------------------------------------------- shell layout */

.piano-shell {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* the staff and, in 弹曲子 mode, the song bar on top of it */
.staff-pane {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.staff-wrap {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: clamp(4px, 1.5vmin, 14px) clamp(8px, 2vw, 24px);
}

#staff-svg {
  width: 100%;
  height: 100%;
  touch-action: none;          /* display-only: no scroll/zoom gestures here */
  user-select: none;
  -webkit-user-select: none;
}

/* ---------------------------------------------------------- toolbar */

.toolbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 2vw, 18px);
  padding: clamp(6px, 1.5vmin, 12px) 12px;
}

/* buttons and the label never squash — on narrow screens the toolbar wraps
   to two rows instead (see the small-screen media query) */
.note-label, .tool-btn { flex-shrink: 0; }

.note-label {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.35em;
  min-width: clamp(110px, 18vmin, 160px);
  padding: 0.15em 0.7em;
  background: var(--paper-card);
  border: 2px solid var(--ink);
  border-radius: 16px;
  box-shadow: 0 3px 0 rgba(42, 53, 84, .18);
  transition: background-color 160ms ease;
}

.note-label-solfege {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(26px, 5vmin, 38px);
  line-height: 1.2;
  color: var(--ink);
}

.note-label-letter {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(14px, 2.4vmin, 20px);
  color: var(--ink-2);
}

@keyframes label-pop {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.12); }
  100% { transform: scale(1); }
}
.note-label.pop { animation: label-pop 220ms ease-out; }

.tool-btn {
  font-family: var(--font-cn-display);
  font-weight: 800;
  font-size: clamp(15px, 2.4vmin, 20px);
  color: var(--ink);
  background: var(--paper-card);
  border: 2px solid var(--ink);
  border-radius: 999px;
  padding: 0.4em 1em;
  box-shadow: 0 3px 0 rgba(42, 53, 84, .18);
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease, opacity 160ms ease;
}
.tool-btn:hover:not(:disabled)  { transform: translateY(-1px); box-shadow: 0 4px 0 rgba(42, 53, 84, .18); }
.tool-btn:active:not(:disabled) { transform: translateY(1px);  box-shadow: 0 2px 0 rgba(42, 53, 84, .18); }
.tool-btn:disabled { opacity: 0.45; cursor: default; }

/* the ☰ in the site header is brand's .header-btn (same as cube/solar's
   panel toggle) — no app-local CSS; .settings-toggle stays only as a JS/
   semantic hook */

/* the pattern chip next to the toggle: mini notation of the current rhythm
   pattern, tap to re-roll */
.pattern-hint {
  display: inline-flex;
  align-items: center;
  padding: 0.25em 0.6em;
}
.pattern-hint[hidden] { display: none; }
.hint-svg { display: block; }
.hint-rest { fill: var(--ink); }

/* which song is on the staff — the same song-bar language as the piano
   app's bar over its chord sheet: ‹ › switch, the dots say how many songs
   there are. Only shown in 弹曲子 mode; picking a song is play, not
   configuration, so it lives on the page, not in the settings list. */
.song-bar {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: var(--tap) 1fr var(--tap);
  align-items: center;
  gap: 8px;
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
  padding: clamp(2px, 1vmin, 8px) clamp(8px, 2vw, 24px) 0;
}
.song-bar[hidden] { display: none; }

.song-nav {
  width: var(--tap);
  height: var(--tap);
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--paper-card);
  font-size: 22px;
  line-height: 1;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(42, 53, 84, .18);
}
.song-nav:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(42, 53, 84, .18); }

.song-mid {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 0;
}
.song-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-cn-display);
  font-weight: 800;
  font-size: clamp(17px, 3.4vmin, 24px);
  line-height: 1.1;
  white-space: nowrap;
}
.song-icon { font-size: .9em; }

.song-dots { display: flex; gap: 6px; }
.song-dots i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(42, 53, 84, .22);
  transition: background-color 160ms ease;
}
.song-dots i.on { background: var(--ink); }

/* ---------------------------------------------------------- settings */

.settings-dialog { position: relative; }
.settings-close {
  position: absolute;
  top: 10px;
  right: 14px;
  border: none;
  background: none;
  font-size: 20px;
  color: var(--ink-2);
  cursor: pointer;
}
.settings-dialog .setting-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 16px;
  font-size: 16px;
  text-align: left;
}
.settings-dialog small { color: var(--ink-2); }
.setting-label { font-weight: 700; }

/* 难度 is rhythm mode's own sub-setting: indented under it, and asleep
   (dimmed, inert) until 节奏 is the mode */
.setting-row.sub { margin-left: 20px; transition: opacity 160ms ease; }
.setting-row.sub.off { opacity: .42; pointer-events: none; }

/* segmented switch: tap a segment or drag the pill sideways — both select.
   --n (segment count) is set from JS. */
.seg {
  --n: 3;
  position: relative;
  display: flex;
  padding: 3px;
  background: var(--paper);
  border: 2px solid var(--ink);
  border-radius: 999px;
  box-shadow: 0 3px 0 rgba(42, 53, 84, .18);
  touch-action: pan-y;            /* vertical scroll stays, horizontal is ours */
  user-select: none;
  -webkit-user-select: none;
}
.seg-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: calc((100% - 6px) / var(--n));
  height: calc(100% - 6px);
  background: var(--butter);
  border: 2px solid var(--ink);
  border-radius: 999px;
  transition: transform 180ms ease;
}
.seg button {
  position: relative;             /* label sits above the sliding pill */
  flex: 1 1 0;
  min-width: 0;
  padding: 0.45em 0.2em;
  border: none;
  background: none;
  font-family: var(--font-cn-display);
  font-weight: 800;
  font-size: 15px;
  white-space: nowrap;
  color: var(--ink);
  cursor: pointer;
}

/* circular icon-buttons (rhythm / replay / clear), same style as
   chess-pieces' reset */
.icon-btn {
  width: clamp(42px, 7vmin, 52px);
  height: clamp(42px, 7vmin, 52px);
  padding: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
}

/* ▶ and ⏹ are the same button in its two states */
.icon-stop, .is-playing .icon-play { display: none; }
.is-playing .icon-stop { display: block; }
.is-playing { background: var(--butter); }

/* ---------------------------------------------------------- keyboard */

.keyboard {
  position: relative;
  flex: 0 0 auto;
  display: flex;
  height: clamp(150px, 30vh, 240px);
  padding-bottom: env(safe-area-inset-bottom);
  border-top: 3px solid var(--ink);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  transition: opacity 180ms ease;
}

/* while the app plays, the keys are for watching, not for pressing — they
   still light up with what is sounding */
.keyboard.locked {
  pointer-events: none;
  opacity: .72;
}

.white-key {
  position: relative;
  flex: 1 1 0;                 /* 8 keys → 12.5% each */
  min-width: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: clamp(8px, 2vmin, 16px);
  background: var(--key-color, var(--paper-card));
  border-right: 3px solid var(--ink);
  box-shadow: inset 0 -8px 0 rgba(42, 53, 84, 0.14);
  cursor: pointer;
  transition: transform 80ms ease, filter 80ms ease;
}
.white-key:first-child { border-left: 3px solid var(--ink); }
/* 弹曲子: the key that plays the note the song owes next. The mark sits low
   on the key — the black keys cover the top half, and low is where a finger
   lands anyway. Gold with an ink rim so it stands out on all seven hues. */
.white-key.cue::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: clamp(30px, 7vmin, 48px);
  width: clamp(13px, 2.8vmin, 20px);
  aspect-ratio: 1;
  transform: translateX(-50%);
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--butter);
}

.white-key.active {
  transform: translateY(5px);
  filter: brightness(1.14) saturate(1.15);
  box-shadow: inset 0 -2px 0 rgba(42, 53, 84, 0.14);
}

.key-name {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(14px, 2.6vmin, 20px);
  line-height: 1;
  color: var(--ink);
  pointer-events: none;
}

.black-key {
  position: absolute;
  top: 0;
  width: 7%;
  height: 58%;
  background: var(--ink);
  border: 2px solid var(--ink);
  border-radius: 0 0 8px 8px;
  box-shadow: 0 4px 0 rgba(42, 53, 84, 0.35);
  z-index: 2;
  cursor: pointer;
}

@keyframes key-shake {
  0%, 100% { transform: translateX(0); }
  25%      { transform: translateX(-4px); }
  75%      { transform: translateX(4px); }
}
.black-key.shake { animation: key-shake 160ms ease-in-out; }

/* ---------------------------------------------------------- staff skin */

.staff-line { stroke: var(--ink); stroke-width: 3; stroke-linecap: round; opacity: 0.66; }
.staff-ledger { stroke: var(--ink); stroke-linecap: round; opacity: 0.66; }  /* width set in app.js */
.staff-clef { fill: var(--ink); stroke: none; pointer-events: none; opacity: 0.78; }
.staff-bar { stroke: var(--ink); stroke-width: 3; stroke-linecap: round; opacity: 0.42; }

/* Structure stays ink; pitch color belongs to the head alone. This keeps a
   multicolor beamed group visually joined and gives every duration the same
   silhouette. */
.note-rest-path { fill: var(--ink); }
.note-stem { stroke: var(--ink); stroke-linecap: round; }
.note-flag, .note-dot, .note-beam { fill: var(--ink); }
.note-head {
  fill: var(--note-color, var(--ink));
  stroke: var(--ink);
  stroke-width: 3.8;
  stroke-linejoin: round;
  paint-order: stroke fill;
  filter: drop-shadow(0 2px 0 rgba(42, 53, 84, 0.16));
}
.note-halo {
  fill: var(--note-color, var(--ink));
  opacity: 0;
  transition: opacity 140ms ease;
}

/* while replaying, per-frame panning drives transforms — no easing */
.playing .staff-note { transition: none; }

.staff-note {
  transition: transform 300ms ease;   /* slides when the FIFO queue shifts */
}
.note-head-pulse {
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 140ms ease;
}

/* the bounce that pulls a kid's eyes to where the note landed */
@keyframes note-pop {
  0%   { transform: scale(0.85); }
  55%  { transform: scale(1.12); }
  82%  { transform: scale(0.98); }
  100% { transform: scale(1); }
}
@keyframes note-halo-pop {
  0%, 100% { opacity: 0; }
  42%      { opacity: 0.22; }
}
.staff-note.new .note-head-pulse { animation: note-pop 280ms cubic-bezier(.34, 1.56, .64, 1); }
.staff-note.new .note-halo { animation: note-halo-pop 280ms ease-out; }

/* 弹曲子: the song is on the staff from the first note. What is still owed
   is faint, the note owed next is full strength and wears its halo — the
   score reads as "here is the song, here is where you are". While listening
   (▶) the whole song lights up: nothing is owed yet. */
.staff-note.todo, .note-beam.todo { opacity: .32; }
.playing .staff-note.todo, .playing .note-beam.todo { opacity: 1; }

/* the note the song is waiting for: a playhead band down the staff, a bigger
   head and a lit halo. Three signals, because this is the one thing on the
   page a child has to find. */
.cue-band {
  fill: var(--butter);
  opacity: 0;
  transition: opacity 160ms ease;
}
.staff-note.cue .cue-band { opacity: .26; }
/* while listening, nothing is owed yet — same reason .todo lights up */
.playing .staff-note.cue .cue-band { opacity: 0; }
.staff-note.cue .note-head-pulse { transform: scale(1.24); }
.staff-note.cue .note-halo { opacity: .42; }
.staff-note.cue .note-head { stroke-width: 4.6; }

.staff-note.sounding .note-head-pulse { transform: scale(1.08); }
.staff-note.sounding .note-halo { opacity: 0.24; }
.staff-note.sounding .note-head {
  filter: drop-shadow(0 2px 0 rgba(42, 53, 84, 0.16))
          drop-shadow(0 0 5px rgba(42, 53, 84, 0.24));
}

/* ---------------------------------------------------------- small screens */

@media (max-width: 480px) {
  .keyboard { height: clamp(130px, 26vh, 200px); }
  /* 5 toolbar items don't fit one row at this width — wrap, don't squash */
  .toolbar { flex-wrap: wrap; row-gap: 6px; }
}

/* landscape: three octaves (22 white keys) need less height per key; the
   toolbar becomes a wrapping right sidebar in normal flow, so it can never
   cover the high notes at the staff's top-right. minmax(0, …) keeps a tall
   sidebar from ever pushing the keyboard off-screen. */
@media (orientation: landscape) {
  .piano-shell {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .staff-pane { grid-column: 1; grid-row: 1; min-height: 0; }
  .toolbar {
    grid-column: 2;
    grid-row: 1;
    align-self: center;
    flex-direction: row;
    flex-wrap: wrap;
    align-content: center;
    max-width: 210px;
    min-height: 0;
    padding: 0 clamp(6px, 1vw, 12px);
  }
  .keyboard {
    grid-column: 1 / -1;
    grid-row: 2;
    height: clamp(100px, 30vh, 200px);
  }
}
