:root {
  --bg: #f6f6f3; --panel: #ffffff; --text: #1d1d1f; --muted: #6b6b70;
  --border: #e3e3de; --accent: #3b5bdb; --accent-soft: #e7ecfd; --accent-text: #ffffff;
  --bad: #d6336c; --code-bg: #f1f1ee;
  --p1: #e8590c; --p2: #f59f00; --p3: #2f9e44; --p4: #1098ad; --p5: #4263eb; --p6: #ae3ec9;
  --p7: #8d6e4a;
  /* Model colors from the paper */
  --m-pcfg: rgb(21, 102, 68); --m-ag: rgb(114, 182, 62); --m-hag: rgb(255, 203, 31);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131315; --panel: #1c1c1f; --text: #ececee; --muted: #9a9aa2;
    --border: #2e2e33; --accent: #748ffc; --accent-soft: #232a45; --accent-text: #0b0b0d;
    --bad: #f06595; --code-bg: #26262a;
    --p1: #ff8a3d; --p2: #ffc53d; --p3: #51cf66; --p4: #3bc9db; --p5: #748ffc; --p6: #da77f2;
    --p7: #c9a27e;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
a { color: var(--accent); }
code, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
h2 { font-size: 15px; margin: 0 0 10px; }
h3 { font-size: 14px; margin: 18px 0 8px; }

/* ----- Header and tabs ----- */
header { border-bottom: 1px solid var(--border); background: var(--panel); }
.header-inner { max-width: 1480px; margin: 0 auto; padding: 14px 16px 0;
  display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 8px 24px; }
.title h1 { font-size: 20px; margin: 0; letter-spacing: -0.01em; }
.title p { margin: 2px 0 12px; color: var(--muted); font-size: 13px; }
.tabs { display: flex; gap: 4px; }
.tab { font: inherit; font-weight: 600; background: none; border: none; color: var(--muted);
  padding: 10px 14px; border-bottom: 2px solid transparent; cursor: pointer; }
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

main { max-width: 1480px; margin: 0 auto; padding: 16px; }
.panel { background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 16px; margin-bottom: 16px; min-width: 0; }

/* ----- Explore layout: controls | results | library ----- */
.explore { display: grid; gap: 16px; grid-template-columns: 320px minmax(0, 1fr) 360px;
  align-items: start; }
.explore > * { min-width: 0; }
.sticky { position: sticky; top: 16px; max-height: calc(100vh - 32px); overflow-y: auto; }
@media (max-width: 1280px) {
  .explore { grid-template-columns: 320px minmax(0, 1fr); }
  .col-library { grid-column: 2; }
}
@media (max-width: 820px) {
  .explore { grid-template-columns: minmax(0, 1fr); }
  .col-library { grid-column: auto; }
  .sticky { position: static; max-height: none; }
}

/* ----- Controls ----- */
label, .label { display: block; font-weight: 600; font-size: 12px; margin-bottom: 4px; }
.hint { color: var(--muted); font-weight: 400; }
textarea { width: 100%; min-height: 96px; resize: vertical; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text);
  font: 14px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip { border: 1px solid var(--border); background: var(--bg); color: var(--text);
  border-radius: 999px; padding: 3px 10px; font: inherit; font-size: 12px; cursor: pointer; }
.chip:hover { border-color: var(--accent); }
.field { margin-top: 14px; }
.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg label { flex: 1; text-align: center; margin: 0; padding: 6px 0; cursor: pointer;
  font-weight: 600; font-size: 13px; }
.seg input:checked + label { background: var(--accent); color: var(--accent-text); }
.seg.small label { padding: 4px 0; font-size: 12px; font-weight: 500; }
#m-pcfg:checked + label { background: var(--m-pcfg); color: #fff; }
#m-ag:checked + label { background: var(--m-ag); color: #1d1d1f; }
#m-hag:checked + label { background: var(--m-hag); color: #1d1d1f; }
.seg input:focus-visible + label { outline: 2px solid var(--accent); outline-offset: -2px; }
.model-desc { color: var(--muted); font-size: 12px; margin-top: 6px; }
.param { margin-top: 10px; }
.param-head { display: flex; justify-content: space-between; align-items: baseline; }
.param-head output { font-variant-numeric: tabular-nums; font-size: 12px; color: var(--muted); }
input[type="range"] { width: 100%; accent-color: var(--accent); margin: 2px 0 0; }
input[type="range"]:disabled { opacity: 0.4; }
.param-group { border-top: 1px solid var(--border); margin-top: 14px; padding-top: 4px; }
details.param-group > summary { margin-top: 8px; font-size: 13px; font-weight: 600; cursor: pointer; }
.param-sub { margin-top: 10px; color: var(--muted); font-size: 12px; }
.check { display: flex; gap: 6px; align-items: center; font-weight: 400; font-size: 12px; margin-top: 12px;
  cursor: pointer; }
.param-group > .label { margin-top: 8px; color: var(--muted);
  letter-spacing: 0.04em; font-size: 11px; }
.actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
button.primary, button.secondary { font: inherit; font-weight: 600; border-radius: 8px;
  padding: 7px 14px; cursor: pointer; border: 1px solid transparent; }
button.primary { background: var(--accent); color: var(--accent-text); flex: 1; }
button.secondary { background: transparent; color: var(--text); border-color: var(--border); }
button:disabled { opacity: 0.5; cursor: default; }
#status { color: var(--muted); font-size: 12px; margin-top: 10px; min-height: 18px;
  display: flex; align-items: center; gap: 8px; }
#status.error { color: var(--bad); }
.spinner { flex: none; width: 12px; height: 12px; border: 2px solid var(--border);
  border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ----- Runs ----- */
.runs { display: grid; grid-template-columns: minmax(220px, 300px) minmax(0, 1fr); gap: 16px; }
@media (max-width: 1000px) { .runs { grid-template-columns: minmax(0, 1fr); } }
.rd-plot svg { width: 100%; height: auto; display: block; }
.rd-plot text { fill: var(--muted); font-size: 10px; }
.rd-plot .axis { stroke: var(--border); }
.rd-plot circle { cursor: pointer; stroke: var(--panel); stroke-width: 1.5; }
.rd-plot circle.selected { stroke: var(--text); stroke-width: 2; }
.run-list { max-height: 220px; overflow-y: auto; }
.run-row { cursor: pointer; }
.run-row:hover td { background: var(--bg); }
.run-row.selected td { background: var(--accent-soft); }
.run-list td:nth-child(2) { white-space: nowrap; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.empty { color: var(--muted); font-size: 13px; margin: 0; }

/* ----- Melodies and notes ----- */
.melody-head { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px;
  align-items: center; margin-bottom: 10px; }
.melody-head h2 { margin: 0; display: flex; align-items: center; gap: 8px; }
.stats { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.stats b { color: var(--text); font-weight: 600; }
.segments { display: flex; flex-wrap: wrap; gap: 8px; }
.segment { border: 1px solid var(--border); border-radius: 8px; padding: 8px; background: var(--bg);
  max-width: 100%; min-width: 0; }
.notes { display: flex; flex-wrap: wrap; gap: 2px; align-items: flex-start; }
.notes.contour .note { margin-top: calc((6 - var(--p)) * 3px); margin-bottom: calc((var(--p) - 1) * 3px); }
.note { width: 20px; height: 20px; border-radius: 4px; display: inline-grid; place-items: center;
  font: 700 11px/1 system-ui, sans-serif; color: #fff; flex: none; vertical-align: middle; }
.note.out { background: transparent !important; color: var(--muted); border: 1px dashed var(--muted); }
.note.out.wrong { color: var(--bad); border: 1.5px solid var(--bad); }
.n1 { background: var(--p1); } .n2 { background: var(--p2); } .n3 { background: var(--p3); }
.n4 { background: var(--p4); } .n5 { background: var(--p5); } .n6 { background: var(--p6); }
.n7 { background: var(--p7); }
.out-row { margin-top: 4px; }
.prog { font: 12px/1.7 ui-monospace, Menlo, monospace; margin-top: 6px; word-break: break-word; }
.prog .note { width: 16px; height: 16px; font-size: 9px; border-radius: 3px; margin: 0 1px; }
.prog .fn { color: var(--accent); font-weight: 600; }
.raw { display: block; color: var(--muted); font-size: 11px; word-break: break-all; margin-top: 2px; }
.raw[hidden] { display: none; }
.seg-meta { color: var(--muted); font-size: 11px; margin-top: 2px; font-variant-numeric: tabular-nums; }
.play { border: 1px solid var(--border); background: var(--panel); color: var(--text); border-radius: 999px;
  width: 24px; height: 24px; padding: 0; cursor: pointer; font-size: 10px; line-height: 1; flex: none; }
.play:hover { border-color: var(--accent); color: var(--accent); }
.legend { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; color: var(--muted);
  font-size: 11px; }
.legend > span { display: inline-flex; align-items: center; gap: 4px; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted);
  font-weight: 400; margin: 0; cursor: pointer; }
.results-head { display: flex; justify-content: space-between; align-items: center; gap: 8px;
  flex-wrap: wrap; margin-bottom: 12px; }
.results-head h2 { margin: 0; }

/* ----- Tables ----- */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 12px; }
th, td { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; position: sticky; top: 0; background: var(--panel); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.lib-step { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 10px; }
.lib-step button { font: inherit; font-size: 12px; border: 1px solid var(--border); background: var(--bg);
  color: var(--text); border-radius: 6px; padding: 2px 8px; cursor: pointer; }
.lib-step button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text);
  border-color: var(--accent); }
.lib-scroll { max-height: calc(100vh - 160px); overflow-y: auto; }

/* ----- Tutorial ----- */
.tutorial { max-width: 1000px; margin: 0 auto; }
.tutorial p { max-width: 75ch; }
.tutorial .panel { padding: 20px 24px; }
.tutorial h2 { font-size: 17px; }
.prim-table td { padding: 8px; vertical-align: middle; }
.prim-table td:first-child { white-space: nowrap; }
.prim-table td:last-child { min-width: 180px; }
.formula { font-family: ui-monospace, Menlo, monospace; background: var(--code-bg); padding: 8px 12px;
  border-radius: 6px; display: inline-block; }
.tut-notes { display: flex; gap: 10px; flex-wrap: wrap; }
.tut-note { display: flex; flex-direction: column; align-items: center; gap: 4px; color: var(--muted);
  font-size: 11px; }
.tut-note .note { width: 32px; height: 32px; font-size: 14px; cursor: pointer; border: none; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 16px; }
.callout { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; }

/* ----- Bad recorder ----- */
.recorder { max-width: 1100px; margin: 0 auto; }
.recorder p { max-width: 75ch; margin-top: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.rec-custom { margin-top: 12px; }
.rec-custom > summary { font-size: 13px; font-weight: 600; cursor: pointer; }
.rec-custom textarea { margin-top: 8px; }
.rec-custom .actions .primary { flex: none; }
.status-line { color: var(--muted); font-size: 12px; margin-top: 10px; min-height: 18px;
  display: flex; align-items: center; gap: 8px; }
.status-line.error { color: var(--bad); }
.rec-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 480px), 1fr)); gap: 0 16px; }
.rec-desc { color: var(--muted); font-size: 12px; margin: -6px 0 10px; }
.rec-phrases { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: flex-start; }
/* Contour by pitch; --top and --bottom are set on #rec-stage */
.rec-phrase { border: none; background: none; padding: 2px; margin: 0; cursor: pointer; border-radius: 6px;
  align-items: flex-start; }
.rec-phrase:hover { background: var(--bg); }
.rec-phrase:focus-visible { outline: 2px solid var(--accent); }
.rec-phrase .note { margin-top: calc((var(--top) - var(--p)) * 3px); margin-bottom: calc((var(--p) - var(--bottom)) * 3px); }
.note.miss { box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--bad); }
.rec-bars { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 4px 10px; align-items: center;
  font-size: 11px; color: var(--muted); margin-bottom: 12px; max-width: 420px; }
.bar { height: 6px; background: var(--code-bg); border-radius: 3px; overflow: hidden; }
.bar > div { height: 100%; background: var(--accent); border-radius: 3px; }
.bar.bad > div { background: var(--bad); }
.rec-progs { margin-top: 10px; }
.rec-progs > summary { font-size: 12px; color: var(--muted); cursor: pointer; }
.rec-prog-row { display: grid; grid-template-columns: 16px minmax(0, 1fr); gap: 6px; align-items: baseline;
  border-top: 1px solid var(--border); margin-top: 6px; }
