:root {
  --bg: #fbfaf7;
  --fg: #1f2328;
  --muted: #5c6370;
  --accent: #1d4ed8;
  --accent-fg: #ffffff;
  --border: #d0d4da;
  --card: #ffffff;
  --ok: #15803d;
  --ok-bg: #ecfdf3;
  --bad: #b91c1c;
  --bad-bg: #fef2f2;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.5;
  color: var(--fg);
  background: var(--bg);
}

* { box-sizing: border-box; }
/* Elements with display rules (e.g. inline-flex labels) must still hide. */
[hidden] { display: none !important; }
body { margin: 0; }
main { max-width: 64rem; margin: 0 auto; padding: 1rem 1.25rem 4rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; }
.lead { font-size: 1.15rem; }

.skip-link { position: absolute; left: -999px; }
.skip-link:focus { left: 1rem; top: 1rem; background: var(--card); padding: .5rem; z-index: 10; }

.site-header {
  display: flex; flex-wrap: wrap; gap: 1rem; align-items: center; justify-content: space-between;
  padding: .75rem 1.25rem; border-bottom: 1px solid var(--border); background: var(--card);
}
.site-header nav { display: flex; gap: 1rem; }
.brand { font-weight: 700; text-decoration: none; color: var(--fg); font-size: 1.15rem; }

button {
  font: inherit; cursor: pointer; padding: .45rem .9rem; border-radius: .4rem;
  border: 1px solid var(--accent); background: var(--accent); color: var(--accent-fg);
}
button.secondary { background: var(--card); color: var(--accent); }
button.danger { background: var(--card); color: var(--bad); border-color: var(--bad); }
button:disabled { opacity: .4; cursor: not-allowed; }
button:focus-visible, a:focus-visible, input:focus-visible, textarea:focus-visible {
  outline: 3px solid #f59e0b; outline-offset: 2px;
}
button.playing { box-shadow: 0 0 0 3px #93c5fd; }
button.icon { padding: .25rem .6rem; background: var(--card); color: var(--accent); }

.toolbar, .actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin: 1rem 0; }
.toggle { display: inline-flex; gap: .4rem; align-items: center; margin-left: .5rem; }
.status { min-height: 1.5em; font-weight: 600; }

.jigsaw-list { list-style: none; padding: 0; }
.jigsaw-list li { padding: .75rem 0; border-bottom: 1px solid var(--border); }
.jigsaw-list a { font-size: 1.1rem; font-weight: 600; }
.jigsaw-list p { margin: .25rem 0 0; }

/* ---- Jigsaw page: board (above) and box (docked at the bottom). ---- */
.board { margin-bottom: 1rem; }
.systems {
  list-style: none; margin: 0; padding: .5rem; min-height: 6rem;
  display: flex; flex-direction: column; gap: .75rem;
  border: 2px dashed var(--border); border-radius: .6rem; background: #f6f5f1;
}
/* A block: one or more joined systems, drawn as a single score (see below).
   It hugs the notation width (centred, so the sheet has equal left/right
   margins) but never exceeds the board; wide scores fill it and scroll. */
.block { position: relative; min-width: 0; max-width: 100%; width: fit-content; margin: 0 auto; border-radius: .5rem; }
.system-header { display: flex; align-items: center; justify-content: flex-end; gap: .5rem; }
/* The whole block is one score, so bar lines line up and the clef appears
   only at the start of each line. Over it lie the pieces' focus/drag areas. */
.system-score { position: relative; min-width: 0; width: fit-content; max-width: 100%; }
.system-score .score { overflow-x: auto; overflow-y: hidden; width: fit-content; max-width: 100%; }
.voice-band {
  position: absolute; border-radius: .3rem; cursor: grab; border: 2px solid transparent; z-index: 3;
}
/* Green marks the pieces that fit a neighbour; on every side where it fits,
   the tint runs straight into the neighbour's (no rounded corner), so fitting
   pieces form one connected green area. Loose pieces stay clear. */
.voice-band.joined { background: rgb(15 118 110 / .12); }
.voice-band.fit-left { border-top-left-radius: 0; border-bottom-left-radius: 0; }
.voice-band.fit-right { border-top-right-radius: 0; border-bottom-right-radius: 0; }
.voice-band.fit-top { border-top-left-radius: 0; border-top-right-radius: 0; }
.voice-band.fit-bottom { border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
/* Hover lights up the whole group (every segment); JS adds `.hover` to all
   segments sharing the group key (see setHover). */
.voice-band.hover:not(.empty) { border-color: var(--accent); }
/* The focused piece/group: one consistent highlight on every segment (see
   setFocused), the same for mouse and keyboard. Deselect by clicking away. */
.voice-band.focused {
  background: rgb(245 158 11 / .18);
  box-shadow: 0 0 0 2px #f59e0b inset;
  border-radius: .3rem;
}
.voice-band:focus, .voice-band:focus-visible { outline: none; } /* .focused stands in */
.voice-band.empty { cursor: default; }
.voice-band.continuation { cursor: grab; }
/* An empty voice shows a faint "fill me" hatch over its (blank) staff. The
   staff itself stays at full opacity — dimming the whole score made multi-voice
   boards (where a position often has one empty staff) look washed out. */
.voice-band.empty {
  background: repeating-linear-gradient(135deg, rgb(0 0 0 / .05) 0 6px, transparent 6px 12px);
}
.voice-band.dragging {
  background: rgb(29 78 216 / .18);
  box-shadow: 0 0 0 2px var(--accent) inset;
  border-radius: .3rem;
}
/* Where a dragged piece will be inserted: a bar on that side of the piece. */
.board.solved .voice-band { cursor: default; }
.end-zone { flex: 1 1 10rem; display: grid; gap: .3rem; min-height: 4rem; }
.end-lane {
  display: flex; align-items: center; justify-content: center; padding: .5rem; min-height: 3rem;
  border-radius: .4rem; color: var(--muted); font-size: .9rem; text-align: center;
}
/* While dragging, every place the piece can go is marked; the one it would
   land in (under the pointer) is marked strongly. */
.end-lane.lane-target { background: rgb(29 78 216 / .06); box-shadow: inset 0 0 0 2px rgb(29 78 216 / .35); color: var(--accent); }
.end-lane.drop-zone { background: rgb(29 78 216 / .14); box-shadow: inset 0 0 0 3px var(--accent); color: var(--accent); }
.voice-band.empty.fill-target { box-shadow: inset 0 0 0 2px rgb(29 78 216 / .35); background-color: rgb(29 78 216 / .05); }
.voice-band.empty.drop-fill { box-shadow: inset 0 0 0 3px var(--accent); background-color: rgb(29 78 216 / .12); }

.cell {
  position: relative; border: 2px solid var(--border); border-radius: .4rem; padding: .35rem;
  cursor: grab; background: var(--card); min-width: 0; max-width: 100%;
}
.cell.dragging { opacity: .35; }
/* Notation wider than the page scrolls sideways inside its piece box. */
.cell .score { overflow-x: auto; overflow-y: hidden; }
.voice-label { font-size: .8rem; font-weight: 600; color: var(--muted); }
.chunk-preview li { min-width: 0; max-width: 100%; }
.chunk-preview.one-per-row { flex-direction: column; }

/* The box with the pieces that aren't placed yet: a grid that scrolls, with
   an adjustable height (drag its top edge). */
.tray {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; height: var(--tray-height, 33vh);
  display: flex; flex-direction: column;
  background: #efece4; border-top: 2px solid var(--border); box-shadow: 0 -4px 16px rgb(0 0 0 / .08);
}
.tray-resize {
  position: absolute; left: 0; right: 0; top: -6px; height: 12px; cursor: ns-resize; touch-action: none;
}
.tray-resize::after {
  content: ""; position: absolute; left: 50%; top: 4px; width: 3rem; height: 4px; margin-left: -1.5rem;
  border-radius: 2px; background: var(--border);
}
.tray-resize:hover::after, .tray-resize:focus-visible::after { background: var(--accent); }
.tray-header { display: flex; align-items: center; justify-content: space-between; padding: .3rem 1.25rem 0; }
.tray-header h2 { margin: 0; font-size: 1rem; }
.tray-pieces {
  list-style: none; margin: 0; padding: .5rem 1.25rem .75rem; flex: 1; min-height: 0;
  display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-start; align-content: flex-start;
  overflow-y: auto;
}
.tray-pieces > li { flex: none; max-width: 100%; }
.tray.collapsed { height: auto; }
.tray.collapsed .tray-pieces, .tray.collapsed .tray-resize { display: none; }
.tray.drop-tray { box-shadow: 0 -4px 16px rgb(0 0 0 / .08), inset 0 0 0 3px var(--accent); }
.tray-empty { color: var(--muted); padding: .5rem 0; }

.chunk-preview { display: flex; flex-wrap: wrap; gap: .75rem; align-items: flex-start; }
.chunk-preview li { background: var(--card); border: 1px solid var(--border); border-radius: .5rem; padding: .25rem; }

.submit-form { display: grid; gap: .35rem; max-width: 46rem; }
.submit-form label { font-weight: 600; margin-top: .6rem; }
.submit-form input, .submit-form textarea {
  font: inherit; padding: .45rem; border: 1px solid var(--border); border-radius: .4rem; background: var(--card);
}
.submit-form textarea.mono { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: .95rem; }
.submit-form input[type="number"] { max-width: 6rem; }

.fixes {
  background: #fffbeb; border: 1px solid #d97706; border-radius: .5rem;
  padding: .25rem 1rem .75rem; margin: 1rem 0;
}
.fixes ul { list-style: none; padding: 0; display: grid; gap: .75rem; }
.fixes li { background: var(--card); border: 1px solid var(--border); border-radius: .4rem; padding: .5rem .75rem; }
.fixes h4 { margin: .25rem 0; }
.fixes p { margin: .25rem 0 .5rem; }

.alert { padding: .75rem 1rem; border-radius: .4rem; margin: 1rem 0; }
.alert.error { background: var(--bad-bg); color: var(--bad); border: 1px solid var(--bad); }


.settings-body { display: grid; gap: .4rem; justify-items: start; }
.settings-body select { font: inherit; padding: .25rem; }
.settings-body .toggle { margin-left: 0; }
.settings-body p { margin: 0; font-size: .9rem; }


/* ---- Seams: pieces that follow each other in the song snap together. ---- */
:root { --joined: #0f766e; --joined-bg: #f0fdfa; --joined-line: rgb(15 118 110 / .35); }

/* Joined pieces share one frame, like a joined part of a jigsaw: no gaps,
   no inner boxes, only a faint line where systems meet. Pieces in a block
   that aren't joined yet keep their own box. */
/* Systems joined in any voice sit in one card without gaps, so the green
   areas of fitting pieces connect across them. The card itself is neutral:
   pieces in it that don't fit stay white, with their jagged edges. */
.block { border: 1px solid var(--border); background: var(--card); padding: .3rem; }
.board.solved .block { border: 2px solid var(--ok); background: var(--ok-bg); }
.board.solved .voice-band.joined { background: transparent; }

/* While dragging a group, this copy of its pieces follows the pointer. */
@keyframes snap {
  0% { transform: scale(1.03); box-shadow: 0 0 0 .4rem rgb(15 118 110 / .35); }
  55% { transform: scale(.993); }
  100% { transform: none; }
}
.block.snap { animation: snap 420ms cubic-bezier(.2, .9, .3, 1.2); }
@media (prefers-reduced-motion: reduce) {
  .block.snap { animation: none; }
}

/* Join playback: the two pieces get a soft outline, the playing notes light up. */
.score.join-playing { outline: 2px solid rgb(29 78 216 / .35); outline-offset: 2px; border-radius: .25rem; }
.score .note-playing, .score .note-playing * { fill: var(--accent) !important; stroke: var(--accent); }
@media (prefers-reduced-motion: no-preference) {
  .score .note-playing { transition: fill 60ms; }
}

/* The selected box piece: the same highlight as on the sheet. */
.tray .cell:focus { outline: none; border-color: #f59e0b; background: rgb(245 158 11 / .12); box-shadow: 0 0 0 2px #f59e0b; }
.tray-hint { display: none; font-size: .85rem; margin-right: auto; margin-left: 1rem; }
.tray:has(.cell:focus) .tray-hint { display: inline; }
.tray-hint kbd {
  font: inherit; font-size: .9em; padding: 0 .3em; border: 1px solid var(--border);
  border-bottom-width: 2px; border-radius: .25rem; background: var(--card);
}
.keys { font-size: .95rem; }
.keys kbd {
  font: inherit; font-size: .85em; padding: 0 .3em; border: 1px solid var(--border);
  border-bottom-width: 2px; border-radius: .25rem; background: var(--card);
}

/* A key move that isn't possible: the piece shakes briefly. */
@keyframes refused { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.voice-band.refused { animation: refused 180ms ease-in-out 2; border-color: var(--bad); }
@media (prefers-reduced-motion: reduce) { .voice-band.refused { animation: none; } }

.help-body { display: block; }
.help-body p { margin: .4rem 0; font-size: .95rem; }

.speed { display: flex; flex-wrap: wrap; align-items: center; gap: .2rem .6rem; }
.speed input[type="range"] { width: 12rem; max-width: 100%; accent-color: var(--accent); }
.speed output { font-variant-numeric: tabular-nums; }

/* Drop targets: a gap zone sits in each reachable gap (only those the dragged
   piece can land in are rendered). They appear when a drag starts; the one
   nearest the pointer lights up (see nearestGap). Gaps are the only targets. */
.gap-zone {
  position: absolute; pointer-events: none; border-radius: 3px; z-index: 4; opacity: 0;
  transition: opacity .1s, background .1s;
}
body.dragging-piece .gap-zone {
  opacity: 1;
  background: rgb(29 78 216 / .22);
  box-shadow: 0 0 0 1px rgb(29 78 216 / .35) inset;
}
body.dragging-piece .gap-zone.active {
  background: var(--accent);
  box-shadow: 0 0 8px 3px rgb(29 78 216 / .5);
}
/* A separator between two adjacent pieces that do NOT fit: a dashed vertical
   rule sitting on the bar line between them, so it reads as "these don't join
   yet". Fitting (joined) neighbours have no separator and share the tint. */
.separator {
  position: absolute; width: 0; pointer-events: none; z-index: 2;
  border-left: 3px dashed var(--muted); opacity: .85; transform: translateX(-1.5px);
}
/* The horizontal variant divides two stacked voices that don't sound together:
   a dashed rule on the midline between their staves. */
.separator-h {
  width: auto; height: 0; border-left: 0; border-top: 3px dashed var(--muted);
  transform: translateY(-1.5px);
}

/* ---- Jigsaw page: controls in a sidebar on the left, the sheet beside it. ---- */
main.jigsaw-page { max-width: 88rem; }
.game { display: grid; grid-template-columns: 16rem minmax(0, 1fr); gap: 1.25rem; align-items: start; }
.sidebar {
  position: sticky; top: .75rem; display: grid; gap: .75rem; align-content: start;
  max-height: calc(100vh - var(--box-space, 33vh) - 1.5rem); overflow-y: auto; padding-right: .25rem;
}
.sidebar h1 { font-size: 1.4rem; margin: 0; line-height: 1.25; overflow-wrap: anywhere; }
.sidebar .byline { margin: -.5rem 0 0; }
.sidebar .toolbar { display: grid; gap: .4rem; margin: 0; }
.sidebar .toolbar button { width: 100%; text-align: left; }
.toolbar kbd {
  float: right; margin-left: .5em; font: inherit; font-size: .8em; padding: 0 .35em; border: 1px solid currentColor;
  border-radius: .25rem; opacity: .7;
}
.game .board { margin: 0; }
/* Narrow windows: controls above the sheet again. */
@media (max-width: 52rem) {
  .game { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; max-height: none; overflow: visible; padding-right: 0; }
  .sidebar .toolbar { display: flex; flex-wrap: wrap; }
  .sidebar .toolbar button { width: auto; }
  .toolbar kbd { float: none; }
}
.clef-settings { display: grid; gap: .4rem; justify-items: start; }

/* How to play / Settings: panels floating next to the sidebar, over the sheet
   (positioned by placePanel in jigsaw.js). */
.panel {
  position: fixed; inset: auto; margin: 0; padding: .75rem 1rem 1rem; width: 24rem; max-width: calc(100vw - 2rem);
  overflow-y: auto; color: inherit; background: var(--card); border: 1px solid var(--border);
  border-radius: .5rem; box-shadow: 0 8px 28px rgb(0 0 0 / .18);
}
/* The help text is long: a wider panel, so it fits without scrolling. */
#help-panel { width: 46rem; }
.panel-header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: .5rem; }
.panel-header h2 { margin: 0; font-size: 1.1rem; }

/* Status messages: a toast above the box that fades in and out. */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--box-space, 33vh) + 1rem); z-index: 30; margin: 0;
  max-width: min(36rem, calc(100vw - 2rem)); padding: .6rem 1rem; border-radius: .5rem;
  background: #1f2328; color: #fff; font-weight: 600; font-size: .95rem; text-align: center;
  box-shadow: 0 6px 20px rgb(0 0 0 / .25); pointer-events: none;
  opacity: 0; transform: translate(-50%, .5rem); transition: opacity .3s, transform .3s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (prefers-reduced-motion: reduce) { .toast { transition: opacity .3s; transform: translate(-50%, 0); } }

/* Honeypot field on the submit form: off-screen for people, visible to bots. */
.hp { position: absolute; left: -10000px; width: 1px; height: 1px; overflow: hidden; }
