:root {
  --bg: #12141a;
  --panel: #1a1d26;
  --raised: #232733;
  --line: #2c3040;
  --text: #eef0f5;
  --muted: #979eb0;
  --accent: #f5b942;
  --accent-ink: #241a03;
  --good: #4cc38a;
  --bad: #f07272;
  --radius: 14px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f2ec;
    --panel: #ffffff;
    --raised: #f0ece4;
    --line: #e2ddd2;
    --text: #1c1d22;
    --muted: #666b78;
    --accent: #b87400;
    --accent-ink: #ffffff;
    --good: #18875a;
    --bad: #c93c3c;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 1180px; /* fluid: fills the window up to here */
  margin: 0 auto;
  padding: 12px 16px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 100dvh;
}

button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible, select:focus-visible, input:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- header ---------- */

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand svg { fill: var(--accent); }
.brand h1 { font-size: 1.15rem; margin: 0; letter-spacing: 0.01em; line-height: 1.2; }
.tagline { margin: 0; font-size: 0.8rem; color: var(--muted); line-height: 1.2; }
.top-actions { display: flex; gap: 8px; }

.ghost {
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 14px;
  font-size: 0.9rem;
}
.ghost:hover { background: var(--raised); }

/* ---------- drill tabs ---------- */

.tabs { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border-bottom: 1px solid var(--line); }
.tabs button {
  background: none;
  border: 0;
  border-bottom: 3px solid transparent;
  padding: 10px 0 9px;
  font-size: 1.02rem;
  font-weight: 600;
  color: var(--muted);
}
.tabs button[aria-current="true"] { color: var(--text); border-bottom-color: var(--accent); }
.tabs button small { font-size: 0.72rem; font-weight: 500; white-space: nowrap; }

/* ---------- segmented control ---------- */

.seg {
  display: inline-flex;
  background: var(--raised);
  border-radius: 10px;
  padding: 3px;
  gap: 2px;
  max-width: 100%;
}
.seg button {
  background: none;
  border: 0;
  border-radius: 8px;
  padding: 7px 13px;
  font-size: 0.92rem;
  color: var(--muted);
  white-space: nowrap;
}
.seg.small button { padding: 5px 11px; font-size: 0.85rem; }
.seg[hidden] { display: none; }
.seg button[aria-pressed="true"] {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.25);
}

/* ---------- stage ---------- */

main { display: flex; flex-direction: column; gap: 14px; }

.stage {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.stage-head {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: var(--muted);
  font-size: 0.9rem;
}

.readout { text-align: center; padding-top: 6px; }
.digits {
  font-size: clamp(3.4rem, 19vw, 5.6rem);
  line-height: 1.05;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
  min-height: 1.05em;
  white-space: nowrap;
}
.digits.long { font-size: clamp(2.3rem, 12vw, 3.8rem); }
.digits.xlong { font-size: clamp(1.25rem, 6.4vw, 2.3rem); letter-spacing: 0.02em; }
.digits .dim { color: var(--line); }
/* harmony answer: the melody on top, each harmony part under it, note above note */
.digits .stack { display: inline-grid; column-gap: 0.45em; row-gap: 0.1em; justify-items: center; }
.digits .stack .mel { color: var(--text); }
.words { color: var(--muted); font-size: 1.05rem; min-height: 1.5em; }
.theory { color: var(--muted); font-size: 1.15rem; font-weight: 700; min-height: 1.5em; opacity: 0.85; letter-spacing: 0.03em; white-space: pre; }
.readout.answer .digits { color: var(--accent); }
.readout.right .digits { color: var(--good); }
.readout.wrong .digits { color: var(--bad); }

.bar { width: 100%; height: 4px; border-radius: 2px; background: var(--raised); overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); }

.phase { color: var(--muted); min-height: 1.5em; font-size: 0.98rem; }

/* ---------- keyboard: 61 keys, display only ---------- */

/* the instrument's body: dark casing, numbers above the keys, middle C below */
.kb-case {
  width: 100%;
  padding: 20px 9px 22px;
  border-radius: 10px;
  background: linear-gradient(#2b2e3a, #16181e 70%);
  border: 1px solid #3d4150;
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.08), 0 2px 6px rgb(0 0 0 / 0.3);
  --kb-ink: #f1efe8;
}
.keyboard {
  position: relative;
  width: 100%;
  aspect-ratio: 36 / 5.4; /* keys keep their proportions as the window grows */
  min-height: 54px;
  border-top: 3px solid #7d2231; /* felt strip */
  --white: #f3f1ea;
  --black: #23262f;
}
.middle-c {
  position: absolute;
  top: 100%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 5px;
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1;
  color: var(--kb-ink);
}
.middle-c i {
  width: 6px;
  height: 6px;
  margin-bottom: -1px;
  border-top: 2px solid var(--kb-ink);
  border-left: 2px solid var(--kb-ink);
  transform: rotate(45deg);
}
.kb-case[hidden], .fb-case[hidden] { display: none; }
/* Piano or guitar: two picture buttons above the diagram */
.board-pick { width: 100%; display: flex; justify-content: center; margin: 10px 0 8px; }
.board-pics { display: flex; gap: 12px; width: 100%; max-width: 440px; }
.board-pics button {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 10px 6px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--raised);
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 700;
}
.board-pics svg { height: 50px; width: auto; max-width: 100%; opacity: 0.5; filter: saturate(0.35); transition: opacity 0.15s, filter 0.15s; }
.board-pics button:hover svg { opacity: 0.85; filter: none; }
.board-pics button[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}
.board-pics button[aria-pressed="true"] svg { opacity: 1; filter: none; }

/* ---------- fretboard ---------- */

.fb-case {
  width: 100%;
  padding: 3px;
  border-radius: 10px;
  background: linear-gradient(#2b2e3a, #16181e 70%);
  border: 1px solid #3d4150;
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.08), 0 2px 6px rgb(0 0 0 / 0.3);
}
.fretboard { display: block; width: 100%; height: auto; }
.fb-wood { fill: #3a2a22; }
.fb-inlay { fill: #e2d9c5; }
.fb-edge { fill: #241a15; }
.fb-marker { fill: #efe9da; }
.fb-fret { stroke: #a9adb8; stroke-width: 3; }
.fb-nut { stroke: #f1efe8; stroke-width: 9; }
.fb-string { stroke: #cfc7b4; }
/* a place on the neck: hidden unless it is in the key's scale or lit */
.fb-spot { display: none; }
.fb-spot text { text-anchor: middle; font-size: 26px; font-weight: 700; fill: #f1efe8; }
.fb-shine { fill: #fff; opacity: 0.16; }
.fb-spot.scale { display: inline; }
.fb-spot.scale .fb-dot { fill: url(#fb-g-scale); stroke: #7b8298; stroke-width: 1.5; }
.fb-spot.scale text { fill: #d5d9e4; }
.fb-spot.tonic .fb-dot { stroke: #f5b942; stroke-width: 3.5; }
.fb-spot.tonic text { fill: #f5b942; }
.fb-spot.lit { display: inline; filter: drop-shadow(0 3px 3px rgb(0 0 0 / 0.55)); }
.fb-spot.lit .fb-dot { stroke: #fff; stroke-width: 2; stroke-opacity: 0.85; }
.fb-spot.lit .fb-shine { opacity: 0.6; }
.fb-spot.lit text { fill: #16181e; }
.fb-spot.lit.answer .fb-dot { fill: url(#fb-g-answer); }
.fb-spot.lit.good .fb-dot { fill: url(#fb-g-good); }
.fb-spot.lit.given .fb-dot { fill: url(#fb-g-given); }
.fb-mute { display: none; fill: #f07272; font-size: 38px; font-weight: 700; text-anchor: middle; }
.fb-mute.on { display: inline; }
.fb-spot.muted { display: none; }

.key { position: absolute; top: 0; display: block; font-style: normal; }
.key.white {
  height: 100%;
  background: var(--white);
  border: 1px solid #b9b5aa;
  border-top: 0;
  border-radius: 0 0 3px 3px;
}
.key.black {
  height: 60%;
  background: var(--black);
  border-radius: 0 0 2px 2px;
  z-index: 1;
}
.key.tonic::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 4px;
  width: 4px;
  height: 4px;
  margin-left: -2px;
  border-radius: 50%;
  background: #9a958a;
}
.key.black.tonic::after { background: #8d93a3; }
.key b {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  padding-bottom: 5px;
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
  color: var(--kb-ink);
}
.key.lit.answer { background: var(--accent); }
.key.lit.good { background: var(--good); }
.key.lit.given { background: #8fa3c7; }
.key.lit::after { display: none; }

.answers {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(70px, 1fr));
  gap: 8px;
}
.answers[hidden] { display: none; }
.answers button {
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 4px 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.1;
}
.answers button b { font-size: 1.7rem; font-variant-numeric: tabular-nums; }
.answers button small { color: var(--muted); font-size: 0.78rem; }
.answers button:active { transform: scale(0.97); }
.answers button:disabled { opacity: 0.5; cursor: default; }
.answers button.good { border-color: var(--good); background: color-mix(in srgb, var(--good) 22%, var(--raised)); opacity: 1; }
.answers button.bad { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 22%, var(--raised)); opacity: 1; }

.stage-foot { display: flex; gap: 10px; align-items: center; justify-content: center; width: 100%; margin-top: 4px; }
.go {
  background: var(--accent);
  color: var(--accent-ink);
  border: 0;
  border-radius: 999px;
  padding: 13px 44px;
  font-size: 1.1rem;
  font-weight: 700;
  min-width: 170px;
}
.go.running { background: transparent; color: var(--text); border: 2px solid var(--line); padding: 11px 42px; }
.go:disabled { opacity: 0.6; }

.tally { color: var(--muted); font-size: 0.9rem; min-height: 1.4em; display: flex; gap: 10px; align-items: center; }
.link { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; font-size: inherit; }

/* ---------- options ---------- */

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 16px;
}
.options .field { padding: 12px 0; border-bottom: 1px solid var(--line); }
.options .field:last-child { border-bottom: 0; }

.field { display: flex; flex-direction: column; gap: 8px; }
.field .label { font-size: 0.85rem; color: var(--muted); }
.field .hint { font-size: 0.82rem; color: var(--muted); }
.field.row { flex-direction: row; align-items: center; justify-content: space-between; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  min-width: 38px;
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 6px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}
.chips button[aria-pressed="true"] {
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  border-color: var(--accent);
  color: var(--text);
}
.chips button.nudge { animation: nudge 0.25s; }
@keyframes nudge { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }

select {
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  max-width: 100%;
}
input[type="range"] { width: 100%; accent-color: var(--accent); }
input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }

.slider-row { display: flex; align-items: center; gap: 12px; }
.slider-row output { min-width: 3.2em; text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- footer and dialogs ---------- */

.foot { margin-top: auto; display: flex; justify-content: space-between; color: var(--muted); font-size: 0.82rem; }

dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  width: min(480px, calc(100vw - 32px));
}
dialog::backdrop { background: rgb(0 0 0 / 0.55); }
dialog header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
dialog h2 { margin: 0; font-size: 1.1rem; }
dialog .field { padding: 10px 0; }
dialog p { color: var(--muted); font-size: 0.92rem; }
dialog a { color: var(--accent); }
/* the "Ready for sound?" prompt shown before anything plays */
#soundGate h2 { font-size: 1.3rem; margin-bottom: 8px; }
#soundGate p { font-size: 1rem; margin: 0 0 8px; }
.gate-actions { display: flex; justify-content: flex-end; align-items: center; gap: 12px; margin-top: 18px; flex-wrap: wrap; }
.gate-actions .go { padding: 11px 26px; font-size: 1rem; }

.diag {
  margin: 0;
  padding: 10px;
  background: var(--raised);
  border-radius: 10px;
  font: 0.8rem/1.5 ui-monospace, Consolas, monospace;
  white-space: pre-wrap;
}

@media (prefers-reduced-motion: reduce) {
  .chips button.nudge { animation: none; }
}

/* controls keep their natural width inside a stacked field */
.field > .seg, .field > select, .field > .chips { align-self: flex-start; }
.field.row > input { align-self: center; }

/* ---------- wide windows: the diagram gets the room, options sit beside it ---------- */

@media (min-width: 900px) {
  /* the drill tabs sit over the stage; the options run the full height beside both */
  main {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    grid-template-areas: "tabs panel" "stage panel";
    grid-template-rows: auto 1fr; /* a tall options panel must not stretch the tab row */
    align-items: start;
    gap: 14px 16px;
  }
  main > .tabs { grid-area: tabs; }
  .stage { grid-area: stage; }
  .panel { grid-area: panel; }
  .panel {
    position: sticky;
    top: 12px;
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
  }
  .stage { padding: 18px 22px 22px; }
  .key b { font-size: 0.8rem; }
  .middle-c { font-size: 0.8rem; }
}
