[hidden] { display: none !important; }
:root {
  --bg: #f6f4ef;
  --paper: #ffffff;
  --ink: #1d1b18;
  --muted: #6b665e;
  --line: #e2ddd3;
  --accent: #6a4bd8;
  --accent-ink: #ffffff;
  --pink: #d02ccf;
  --ok: #2e8a4a;
  --warn: #b26b00;
  --bad: #c0362c;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.05);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: inherit; }
button, select, input, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--line); background: var(--paper); border-radius: 8px;
  padding: 6px 12px; cursor: pointer; white-space: nowrap;
}
button:hover { border-color: #c9c2b4; }
a.button-link { border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; text-decoration: none; white-space: nowrap; }
a.button-link.primary,
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: #fff; }
button:disabled { opacity: .5; cursor: default; }
select, input[type=text], input[type=search] {
  border: 1px solid var(--line); background: var(--paper); border-radius: 8px; padding: 6px 10px;
}
.muted { color: var(--muted); }
.chip {
  display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px;
  background: #ece8df; color: var(--muted);
}
.chip.ready { background: #dff0e3; color: var(--ok); }
.chip.needs_review { background: #fbecd2; color: var(--warn); }
.chip.failed { background: #f8dcd9; color: var(--bad); }
.chip.queued, .chip.processing { background: #e7e1fa; color: var(--accent); }

/* ---------- library ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 14px 20px;
  border-bottom: 1px solid var(--line); background: var(--paper); position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 18px; margin: 0; font-weight: 650; letter-spacing: -.01em; }
.topbar .grow { flex: 1; }
.library { max-width: 1200px; margin: 0 auto; padding: 20px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.card {
  background: var(--paper); border-radius: var(--radius); box-shadow: var(--shadow);
  text-decoration: none; display: flex; flex-direction: column; overflow: hidden;
  border: 1px solid transparent;
}
.card:hover { border-color: var(--line); }
.card .thumb { height: 120px; overflow: hidden; background: #fff; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: center; }
.card .thumb > div { width: 100%; }
.card .meta { padding: 10px 12px 12px; display: grid; gap: 4px; }
.card .title { font-weight: 600; }
.card .sub { font-size: 13px; color: var(--muted); display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.empty { text-align: center; padding: 60px 20px; color: var(--muted); }
.drop {
  border: 2px dashed var(--line); border-radius: var(--radius); padding: 18px; text-align: center;
  color: var(--muted); margin-bottom: 18px; background: var(--paper);
}
.drop.over { border-color: var(--accent); color: var(--accent); }
dialog { border: none; border-radius: 14px; padding: 0; box-shadow: 0 20px 60px rgba(0,0,0,.25); width: min(460px, 92vw); }
dialog form { padding: 20px; display: grid; gap: 12px; }
dialog h2 { margin: 0; font-size: 17px; }
dialog label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
dialog .row { display: flex; gap: 8px; justify-content: flex-end; }
.spinner { width: 18px; height: 18px; border: 2px solid #ddd; border-top-color: var(--accent);
  border-radius: 50%; animation: spin 1s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- piece view ---------- */
.piece-head { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.piece-head .title { font-size: 18px; font-weight: 650; border: 1px solid transparent; border-radius: 6px; padding: 2px 6px; background: none; min-width: 6ch; }
.piece-head .title:hover, .piece-head .title:focus { border-color: var(--line); background: var(--paper); outline: none; }
.piece-head .composer { color: var(--muted); }
.toolbar {
  display: flex; flex-wrap: wrap; gap: 8px 14px; align-items: center; padding: 8px 20px;
  border-bottom: 1px solid var(--line); background: #fbfaf7; position: sticky; top: 57px; z-index: 4;
}
.toolbar .group { display: flex; align-items: center; gap: 6px; }
.toolbar .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: none; border-radius: 0; border-right: 1px solid var(--line); }
.seg button:last-child { border-right: none; }
.toolbar .value { min-width: 4ch; text-align: center; font-variant-numeric: tabular-nums; }
.toolbar .grow { flex: 1; }
.menu { position: relative; }
.menu-items { position: absolute; right: 0; top: calc(100% + 4px); background: var(--paper); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: var(--shadow); padding: 6px; display: none; min-width: 200px; z-index: 10; }
.menu.open .menu-items { display: grid; }
.menu-items a, .menu-items button { text-align: left; border: none; padding: 8px 10px; border-radius: 6px; text-decoration: none; font-size: 14px; background: none; }
.menu-items a:hover, .menu-items button:hover { background: var(--bg); }
.menu-items hr { border: none; border-top: 1px solid var(--line); margin: 4px 0; }

.workspace { display: grid; grid-template-columns: 1fr; gap: 0; min-height: calc(100vh - 120px); }
.workspace.scan { grid-template-columns: minmax(300px, 1fr) minmax(0, 1.2fr); }
.workspace.edit { grid-template-columns: minmax(0, 1.2fr) minmax(320px, 1fr); }
.workspace.scan.edit { grid-template-columns: minmax(280px, .9fr) minmax(0, 1.2fr) minmax(300px, .9fr); }
.panel { border-right: 1px solid var(--line); background: #efece5; overflow: auto; max-height: calc(100vh - var(--chrome-h, 120px)); position: sticky; top: var(--chrome-h, 120px); }
.workspace.ai { grid-template-columns: minmax(0, 1fr) minmax(320px, 400px); }
.workspace.ai.scan { grid-template-columns: minmax(260px, .8fr) minmax(0, 1.2fr) minmax(320px, 380px); }
.workspace.ai.edit { grid-template-columns: minmax(0, 1.2fr) minmax(280px, .8fr) minmax(320px, 380px); }
.workspace.ai.scan.edit { grid-template-columns: minmax(220px, .7fr) minmax(0, 1.2fr) minmax(260px, .7fr) minmax(300px, 360px); }
.panel.editor { border-right: none; border-left: 1px solid var(--line); background: var(--paper); display: flex; flex-direction: column; }
.scan-pages { padding: 12px; display: grid; gap: 12px; }
.scan-pages img { width: 100%; background: #fff; box-shadow: var(--shadow); border-radius: 4px; }
.scan-tools { display: flex; gap: 6px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--line); background: #f6f4ef; position: sticky; top: 0; }
.paper-wrap { padding: 20px; overflow: auto; }
.paper { background: var(--paper); box-shadow: var(--shadow); border-radius: 6px; padding: 24px 20px; max-width: 1000px; margin: 0 auto; min-height: 300px; }
.status-box { max-width: 700px; margin: 40px auto; background: var(--paper); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.status-box pre { background: #f4f2ed; padding: 10px; border-radius: 6px; max-height: 360px; overflow: auto; font-size: 12px; white-space: pre-wrap; }
.editor .abc-wrap { flex: 1; min-height: 300px; position: relative; display: flex; background: #fffdf8; }
.editor .abc-wrap textarea, .abc-backdrop {
  border: none; padding: 12px; margin: 0; font: 13px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap; overflow-wrap: break-word; word-break: normal; tab-size: 8; scrollbar-gutter: stable; }
.editor .abc-wrap textarea { flex: 1; min-height: 0; resize: none; outline: none; background: transparent; position: relative; z-index: 1; overflow: auto; }
.abc-backdrop { position: absolute; inset: 0; overflow: hidden; color: transparent; pointer-events: none; }
.abc-backdrop mark { color: transparent; background: rgba(106, 75, 216, .3); border-radius: 3px; box-shadow: 0 0 0 1px rgba(106, 75, 216, .5); }
.editor .bar { display: flex; gap: 6px; padding: 8px 10px; border-bottom: 1px solid var(--line); align-items: center; flex-wrap: wrap; }
.editor .bar input { flex: 1; min-width: 120px; }
.editor .warnings { border-top: 1px solid var(--line); max-height: 140px; overflow: auto; font-size: 12px; padding: 6px 10px; color: var(--warn); }
.editor .warnings:empty { display: none; }
.ai-panel { border-right: none; border-left: 1px solid var(--line); background: #f8f6fc; display: flex; flex-direction: column; height: calc(100vh - var(--chrome-h, 120px)); overflow: hidden; }
.ai-head { display: flex; gap: 6px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line); background: var(--paper); }
.ai-head button { padding: 4px 10px; font-size: 13px; }
.ai-log { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; font-size: 14px; }
.ai-intro p { margin: 0 0 8px; line-height: 1.45; }
.ai-examples { display: grid; gap: 6px; margin-top: 10px; }
.ai-examples button { text-align: left; font-size: 13px; background: var(--paper); white-space: normal; width: 100%; }
.ai-msg { padding: 8px 11px; border-radius: 12px; white-space: pre-wrap; line-height: 1.45; max-width: 100%; }
.ai-msg.me { background: var(--accent); color: var(--accent-ink); align-self: flex-end; max-width: 88%; border-bottom-right-radius: 4px; }
.ai-msg.them { background: var(--paper); border: 1px solid var(--line); align-self: flex-start; border-bottom-left-radius: 4px; }
.ai-msg.note { color: var(--muted); display: flex; gap: 8px; align-items: center; }
.ai-msg.err { background: #f8dcd9; color: var(--bad); }
.ai-tag { font-size: 12px; opacity: .85; margin-bottom: 3px; }
.ai-warn { margin-top: 8px; font-size: 12px; color: var(--warn); white-space: normal; }
.ai-diff { margin-top: 8px; font-size: 12px; white-space: normal; }
.ai-diff summary { cursor: pointer; color: var(--muted); }
.ai-edit { display: grid; gap: 2px; margin-top: 6px; font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap; word-break: break-all; }
.ai-edit del { background: #fbe1de; color: #8a2a20; text-decoration: none; padding: 2px 5px; border-radius: 4px; }
.ai-edit del::before { content: "− "; }
.ai-edit ins { background: #def3e2; color: #1d6b2e; text-decoration: none; padding: 2px 5px; border-radius: 4px; }
.ai-edit ins::before { content: "+ "; }
.ai-actions { display: flex; gap: 6px; align-items: center; margin-top: 8px; font-size: 12px; white-space: normal; }
.ai-compose { border-top: 1px solid var(--line); background: var(--paper); padding: 8px 10px; display: grid; gap: 6px; }
.ai-picked { display: flex; gap: 6px; align-items: center; justify-content: space-between; font-size: 12px; min-height: 26px; }
.ai-picked.on { background: #ece6fb; color: var(--accent); border-radius: 8px; padding: 2px 4px 2px 8px; font-weight: 600; }
.ai-picked button { padding: 1px 7px; font-size: 12px; }
.ai-compose textarea { resize: vertical; font: inherit; font-size: 14px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; min-height: 64px; }
.ai-compose textarea:focus { outline: 2px solid #cfc2f3; border-color: var(--accent); }
.ai-send { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.ai-send label { font-size: 12px; display: flex; align-items: center; gap: 4px; }
.paper.picking { cursor: crosshair; user-select: none; -webkit-user-select: none; }
.notation .ai-sel, .notation .ai-sel * { fill: var(--accent) !important; }
.notation path.ai-sel { stroke: var(--accent); stroke-width: .6px; }
/* With notes picked, everything else fades, so the pick is obvious even in rainbow colours. */
.paper.has-sel .notation .abcjs-note:not(.ai-sel), .paper.has-sel .notation .abcjs-rest:not(.ai-sel),
.paper.has-sel .notation .note-name { opacity: .3; }
.pick-box { position: fixed; z-index: 50; border: 2px solid var(--accent); background: rgba(106, 75, 216, .12); border-radius: 4px; pointer-events: none; }
.editor .banner { background: #fbecd2; color: #6b4100; font-size: 13px; padding: 8px 10px; display: flex; gap: 8px; align-items: center; }
.editor details { border-top: 1px solid var(--line); font-size: 13px; }
.editor details summary { padding: 8px 10px; cursor: pointer; color: var(--muted); }
.versions { list-style: none; margin: 0; padding: 0 10px 10px; max-height: 200px; overflow: auto; }
.versions li { display: flex; gap: 8px; align-items: center; padding: 4px 0; border-bottom: 1px solid #f0ede6; }
.versions li span { flex: 1; }
.dirty { color: var(--warn); font-size: 12px; }

/* notation */
.notation svg { display: block; }
.note-name {
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-weight: 700; fill: #222; text-anchor: middle;
  stroke: #fff; stroke-width: 2.4px; stroke-linejoin: round; paint-order: stroke; pointer-events: none;
}
.abcjs-container > div { break-inside: avoid; page-break-inside: avoid; }
.notation svg { break-inside: avoid; page-break-inside: avoid; }
/* abcjs makes its container an overflow-hidden inline-block. Chromium can't fragment an
   inline-block across pages, so it slices it at the page edge and the per-line break rules
   never apply. As a plain block, each line's wrapper can be kept whole. */
.notation.abcjs-container { display: block !important; overflow: visible !important; }
.notation.abcjs-container > div { overflow: visible !important; }
.notation.abcjs-container svg { display: block !important; }

@media (max-width: 800px) {
  .workspace.scan, .workspace.edit, .workspace.scan.edit, .workspace.ai, .workspace.ai.scan, .workspace.ai.edit, .workspace.ai.scan.edit { grid-template-columns: 1fr; }
  .ai-panel { height: 70vh; }
  .panel { position: static; max-height: 60vh; }
  .toolbar { position: static; }
  .paper-wrap { padding: 8px; }
  .paper { padding: 12px 6px; }
}
/* Playback, shared by Learn and Set up: the notes sounding now coloured by hand (beats the
   inline fill the colour modes set on heads), the playhead, and the bars being repeated. */
:root { --rh: #2f7de1; --lh: #ef7d1a; --kid-accent: #6a4bd8; }
.notation.abcjs-container > div { position: relative; }
/* The playhead and repeat shading are drawn behind the notes and letter names. */
.notation.abcjs-container > div > svg { position: relative; z-index: 1; }
.notation .playing.rh, .notation .playing.rh * { fill: var(--rh) !important; }
.notation .playing.lh, .notation .playing.lh * { fill: var(--lh) !important; }
.notation .tapped, .notation .tapped * { fill: var(--kid-accent) !important; }
.playhead {
  position: absolute; left: 0; top: 6px; bottom: 6px; width: 8px; margin-left: -4px; border-radius: 4px;
  background: var(--kid-accent); opacity: .4; box-shadow: 0 0 12px rgba(106,75,216,.7);
  pointer-events: none; z-index: 0; will-change: transform;
}
.loop-zone {
  position: absolute; top: 4px; bottom: 4px; border-radius: 12px; pointer-events: none; z-index: 0;
  background: rgba(255, 196, 0, .16); border: 2px dashed rgba(220, 160, 0, .55);
}
@media print {
  .notation .playing, .notation .playing * { fill: inherit !important; }
  .playhead, .loop-zone { display: none !important; }
}
