@font-face {
  font-family: 'Bravura';
  src: url('../fonts/Bravura.woff2') format('woff2');
  font-display: block;
}
@font-face {
  font-family: 'Patrick Hand SC';
  src: url('../fonts/PatrickHandSC.woff2') format('woff2');
  font-display: swap;
}

:root {
  --bg: #e9e7e2;
  --panel: #fbfaf7;
  --ink: #1d1d1f;
  --muted: #6b6a66;
  --line: #d6d3cc;
  --accent: #2463d6;
  --accent-soft: #e3ecfc;
  --bad: #c62828;
  --toolbar-w: 220px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 14px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
  display: flex;
}
button, select, input { font: inherit; color: inherit; }

/* ---------- Toolbar ---------- */
.toolbar {
  width: var(--toolbar-w);
  flex: none;
  height: 100vh;
  position: sticky;
  top: 0;
  overflow-y: auto;
  background: var(--panel);
  border-left: 1px solid var(--line);
  order: 2;                       /* toolbar sits on the right-hand side */
  padding: 14px 12px 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.brand { font: 26px/1 'Patrick Hand SC', cursive; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; margin: 0; }

.tool {
  display: grid;
  grid-template-columns: 60px minmax(0, 1fr);
  gap: 4px 10px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}
.tool label { min-width: 0; display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 2px; }
.tool select { width: 100%; min-width: 0; max-width: 100%; box-sizing: border-box; padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; background: #fff; color: var(--ink); }
.tool .check { grid-column: 2; flex-direction: row; align-items: center; gap: 6px; color: var(--ink); }
.tool-icon {
  grid-row: span 2;
  width: 60px; height: 60px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--ink);
  cursor: grab;
  touch-action: none;
  user-select: none;
}
.tool-icon:hover { outline: 2px solid var(--accent); }
.tool-icon svg { display: block; pointer-events: none; }

.panel { border-top: 1px solid var(--line); padding-top: 10px; }
.panel h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 6px; }
#selection-panel p { margin: 0 0 8px; }
.seg { display: flex; margin-bottom: 6px; }
.seg button { flex: 1; padding: 4px 0; border: 1px solid var(--line); background: #fff; cursor: pointer; }
.seg button + button { border-left: 0; }
.seg button:first-child { border-radius: 6px 0 0 6px; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.on { background: var(--accent); color: #fff; border-color: var(--accent); }
.seg .seg-label { align-self: center; font-size: 12px; color: var(--muted); margin-right: 6px; }
.seg .seg-label + button { border-radius: 6px 0 0 6px; }
.field { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); margin: 6px 0 8px; }
.field input { padding: 5px 7px; border: 1px solid var(--line); border-radius: 6px; color: var(--ink); }
.help summary { cursor: pointer; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.help ul { padding-left: 18px; margin: 8px 0 0; font-size: 12.5px; }
.help li { margin-bottom: 6px; }
kbd { font: 11px ui-monospace, monospace; border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; background: #fff; }

button { cursor: pointer; padding: 6px 12px; border: 1px solid var(--line); border-radius: 7px; background: #fff; }
button:hover { border-color: #b9b5ac; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: var(--bad); }

/* ---------- Workspace & page ---------- */
.workspace { flex: 1; min-width: 0; height: 100vh; overflow: auto; padding: 0 24px 40px; }
.actions {
  position: sticky; top: 0; z-index: 5;
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  padding: 12px 0; background: var(--bg);
  max-width: 1060px; margin: 0 auto;
}
.actions .spacer { flex: 1; }
.actions .paper { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.actions select { padding: 5px 6px; border: 1px solid var(--line); border-radius: 7px; background: #fff; }

.page {
  --page-w: 816px; --page-h: 1056px;           /* US Letter at 96 dpi */
  width: var(--page-w);
  height: var(--page-h);
  margin: 0 auto;
  background: #fff;
  color: #000;
  box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 8px 30px rgba(0,0,0,.08);
  padding: 44px calc((var(--page-w) - 720px) / 2) 40px;
  display: flex;
  flex-direction: column;
  position: relative;
}
/* Student name line: sits in the top margin, so it never pushes the worksheet down. */
.name-line { position: absolute; top: 12px; left: calc((var(--page-w) - 720px) / 2); line-height: 0; }
.name-line[hidden] { display: none; }
.name-line svg { display: block; }
.page[data-paper="a4"] { --page-w: 794px; --page-h: 1123px; }

.sheet-head { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; align-items: center; gap: 12px; }
.titles { min-width: 0; grid-column: 2; }       /* stays in the middle with or without theme drawings */
.sheet-head > .theme-art:last-child { grid-column: 3; }   /* right-hand drawing (also in title copies, which have no ids) */
.theme-art { width: 96px; height: 96px; }
.theme-art:empty { display: none; }
.theme-art svg { display: block; width: 100%; height: 100%; }
.theme-select {
  padding: 6px 10px; border: 1px solid var(--line); border-radius: 7px; background: #fff; cursor: pointer;
}
.option-check {
  display: flex; align-items: center; gap: 8px; padding: 9px 10px;
  border: 1px solid var(--line); border-radius: 10px; background: #fff; cursor: pointer;
}
.option-check input { width: 16px; height: 16px; margin: 0; }

.title, .subtitle {
  border: 0; background: transparent; text-align: center; width: 100%; padding: 0; outline: none;
  white-space: pre; overflow: hidden;     /* one line; fitTitles() shrinks long titles via --fit */
  font-family: 'Patrick Hand SC', 'Comic Sans MS', cursive;
  -webkit-text-stroke: 1.4px currentColor;   /* chunkier, like a marker */
  color: #000;
}
.title { font-size: calc(52px * var(--fit, 1)); line-height: 1.15; min-height: 60px; }
.subtitle { font-size: calc(32px * var(--fit, 1)); line-height: 1.2; min-height: 38px; margin-top: 2px; -webkit-text-stroke-width: 0.9px; }
.title:hover, .subtitle:hover, .title:focus, .subtitle:focus { background: #f6f8fd; border-radius: 6px; }
.title:empty::before, .subtitle:empty::before { color: #c9c6bf; -webkit-text-stroke-color: #c9c6bf; }

.systems { flex: 1; display: flex; flex-direction: column; justify-content: space-evenly; }
.system { position: relative; }
.staff { width: 720px; position: relative; border-radius: 6px; touch-action: none; }

/* Copy / Paste buttons sit just outside the page's right edge, level with each staff. */
.staff-row { position: relative; }
.line-tools {
  position: absolute; top: 50%; transform: translateY(-50%);
  left: calc(100% + (var(--page-w) - 720px) / 2 + 14px);
  display: flex; flex-direction: column; gap: 6px;
}
.line-tools button { width: 64px; padding: 5px 0; font-size: 13px; }
.line-tools .btn-copy.on { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.line-tools button:disabled { opacity: .45; cursor: not-allowed; }

/* Instructions above each staff: takes no space (and prints nothing) while empty. */
.instructions-wrap { position: relative; }
.instructions {
  display: none;
  width: 100%; overflow: hidden; white-space: pre-wrap; overflow-wrap: break-word;
  border: 0; padding: 4px 6px; margin: 0 0 2px; outline: none; border-radius: 6px;
  background: transparent; color: #000;
  font: 16px/1.35 Georgia, 'Times New Roman', serif;
}
.instructions-wrap.has-text .instructions, .instructions-wrap.editing .instructions { display: block; }
.instructions:hover, .instructions:focus { background: #f6f8fd; }
.instructions:empty::before { color: #a9a59d; font-style: italic; }
.add-instr {
  position: absolute; left: 0; top: -2px; z-index: 2;
  font-size: 12px; padding: 2px 9px; border-radius: 99px;
  color: var(--accent); border: 1px dashed #b9c7e4; background: #fff;
  opacity: 0; transition: opacity .15s;
}
.system:hover .add-instr, .add-instr:focus-visible { opacity: 1; }
.instructions-wrap.has-text .add-instr, .instructions-wrap.editing .add-instr { display: none; }
body.dragging .add-instr { display: none; }
.overflow-warning {
  flex-basis: 100%; margin: 0; padding: 7px 10px; border-radius: 7px;
  background: #fdecea; color: var(--bad); font-size: 13px;
}
.staff svg { display: block; overflow: visible; color: #000; }
.staff.drop-target { background: var(--accent-soft); }
body.dragging, body.dragging * { cursor: grabbing !important; }
body.dragging-note, body.dragging-note * { cursor: ns-resize !important; }

/* SVG content */
.mus { font-family: 'Bravura'; fill: currentColor; user-select: none; }
.hint { font: italic 15px system-ui, sans-serif; fill: #8d8a83; }
.note-name { font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; font-weight: 700; fill: #000; pointer-events: none; }
.note-name.on-black { fill: #fff; }
.finger { font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif; font-weight: 700; font-variant-numeric: lining-nums; fill: #000; pointer-events: none; }
.pitch-label { font: 600 13px system-ui, sans-serif; fill: var(--accent); }
.hit { fill: transparent; }
.item { cursor: pointer; }
.item:hover { color: #3c4b66; }
.item.sel, .item.hot { color: var(--accent); }
.item.hot .hit { fill: var(--accent-soft); }
.ghost { color: var(--accent); opacity: .65; pointer-events: none; }
.ghost.bad { color: var(--bad); }
.ghost.bad .pitch-label { fill: var(--bad); }
.keyzone .zone { fill: transparent; stroke: #b9c7e4; stroke-dasharray: 3 3; }
.keyzone.hot .zone { fill: var(--accent-soft); stroke: var(--accent); }
.keyzone .hit { display: none; }

/* Drag avatar & toast */
.drag-avatar {
  position: fixed; left: 0; top: 0; z-index: 50;
  width: 60px; height: 60px;
  pointer-events: none; display: none;
  color: var(--accent);
}
.drag-avatar.show { display: block; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: #26262a; color: #fff; padding: 9px 16px; border-radius: 8px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 60;
  max-width: min(520px, calc(100vw - 32px));
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Print: only the page, at exact paper size ---------- */
@media print {
  body { display: block; background: #fff; }
  .toolbar, .actions, .toast, .drag-avatar { display: none !important; }
  .workspace { padding: 0; height: auto; overflow: visible; }
  .page { box-shadow: none; margin: 0; }
  .rt-edit:empty::before { content: none !important; }   /* empty title / subtitle / instructions print blank */
  .hint, .keyzone .zone, .add-instr, .line-tools { display: none !important; }
  .instructions { background: none !important; }
  .item, .item.sel, .item.hot { color: #000 !important; }
  .staff.drop-target { background: none; }
}

/* ---------- Added tools: quantity, double bar, bow markers, save menu ---------- */
.tool[data-kind="rest"] .tool-icon { grid-row: span 3; }
.tool[data-kind="note"] .tool-icon { grid-row: span 6; }   /* list, Quantity, Dotted, Staccato, Show note name, Note colors */
.tool .qty-row { justify-content: space-between; }
.tool .check.disabled { opacity: .45; }
.tool .qty-row select { width: auto; flex: 0 1 50px; min-width: 0; padding: 4px 2px 4px 6px; }
.tool-note { font-size: 11.5px; color: var(--muted); }
.tool-heading { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 6px 0 -4px; }

.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 20; min-width: 210px;
  display: flex; flex-direction: column; padding: 4px;
  background: #fff; border: 1px solid var(--line); border-radius: 9px; box-shadow: 0 8px 24px rgba(0,0,0,.14);
}
.menu[hidden] { display: none; }
.menu button { border: 0; text-align: left; padding: 8px 10px; border-radius: 6px; display: flex; flex-direction: column; }
.menu button:hover, .menu button:focus-visible { background: var(--accent-soft); }
.menu button span { font-size: 12px; color: var(--muted); }

/* ---------- Page bar: back-to-home link and the page's name ---------- */
.page-bar {
  margin: 0 -12px; padding: 12px 0 0;          /* full workspace width: back link in the top-left corner */
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
}
.back-home {
  justify-self: start; text-decoration: none; color: var(--ink);
  padding: 6px 12px; border: 1px solid var(--line); border-radius: 7px; background: #fff;
}
.back-home:hover { border-color: #b9b5ac; }
.page-name { margin: 0; font: 30px/1.1 'Patrick Hand SC', cursive; }
@media print { .page-bar { display: none !important; } }

/* Staffs after a final double bar (Music Major): hidden but still taking their space. */
.system.cut { visibility: hidden; }
/* Lines not used with a wider Staff spacing (Music Major): left out, so the other staffs spread over the page. */
.system.slot-off { display: none; }

/* Note clipboard (Music Major): drag-select box and paste target */
.band { fill: rgba(36, 99, 214, .08); stroke: var(--accent); stroke-dasharray: 4 3; pointer-events: none; }
.paste-mark { fill: rgba(36, 99, 214, .07); stroke: var(--accent); stroke-width: 1.5; pointer-events: none; }
.paste-label { font: 600 11px system-ui, sans-serif; fill: var(--accent); pointer-events: none; }
@media print { .band, .paste-mark, .paste-label { display: none !important; } }

/* ---------- Formatted text boxes (title, subtitle, instructions) and the Format bar ---------- */
.rt-edit { cursor: text; }
.rt-edit:empty::before { content: attr(data-placeholder); pointer-events: none; }
.rt-h {
  background: #fff176;                         /* highlighter yellow */
  -webkit-print-color-adjust: exact; print-color-adjust: exact;
}
.format-bar {
  flex-basis: 100%; display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding-top: 8px; border-top: 1px solid var(--line);
}
.format-label { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-right: 2px; }
.format-bar .fmt-font { width: 150px; }
.format-bar .fmt-size { width: 70px; }
.fmt-btn { width: 34px; padding: 5px 0; text-align: center; line-height: 1.2; }
.fmt-btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.fmt-highlight span { background: #fff176; padding: 0 3px; border-radius: 2px; color: var(--ink); }
.format-hint { font-size: 12px; color: var(--muted); margin-left: 6px; }

/* ---------- Getting-started steps (above the worksheet until a staff has a clef and time signature) ---------- */
.guide {
  width: 816px; max-width: 100%; margin: 0 auto 12px; padding: 10px 14px;
  background: var(--accent-soft); border: 1px solid #b9cdf3; border-radius: 10px; color: var(--ink);
}
.guide[hidden] { display: none; }
.guide-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.guide-hide { border: 1px solid var(--line); background: #fff; border-radius: 6px; padding: 2px 10px; cursor: pointer; font-size: 12px; }
.guide ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.guide li { display: flex; align-items: center; gap: 10px; }
.guide-mark {
  flex: none; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 13px;
}
.guide li.done { color: var(--muted); }
.guide li.done strong, .guide li.done span:last-child { text-decoration: line-through; }
.guide li.done .guide-mark { background: #2e7d32; }
.tool.guide-pulse { border-color: var(--accent); animation: guide-pulse 1.6s ease-in-out infinite; }
@keyframes guide-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(36, 99, 214, .45); }
  50% { box-shadow: 0 0 0 6px rgba(36, 99, 214, 0); }
}
@media (prefers-reduced-motion: reduce) { .tool.guide-pulse { animation: none; box-shadow: 0 0 0 3px rgba(36, 99, 214, .35); } }
@media print { .guide { display: none !important; } }

/* ---------- Several pages: Add next page, Remove page, page numbers ---------- */
.page[hidden] { display: none; }
.page-extra { margin-top: 28px; }
.add-page-bar { width: 816px; max-width: 100%; margin: 18px auto 0; text-align: center; }
.add-page {
  padding: 10px 22px; font-size: 15px; border: 2px dashed #9db6e6; border-radius: 10px;
  background: #fff; color: var(--accent); cursor: pointer;
}
.add-page:hover, .add-page:focus-visible { border-style: solid; border-color: var(--accent); background: var(--accent-soft); outline: none; }
.add-page:disabled { opacity: .45; cursor: not-allowed; }
/* Remove page sits just outside the page's top-right corner, like each line's Copy / Paste. */
.page-tools { position: absolute; top: 10px; left: calc(100% + 14px); }
.page-tools button { padding: 5px 10px; font-size: 13px; white-space: nowrap; }
.page-tools button:hover { border-color: #d9534f; color: #b52b27; }
.page-num { position: absolute; left: 0; right: 0; bottom: 14px; display: flex; justify-content: center; line-height: 0; pointer-events: none; }
.page-num[hidden] { display: none; }
/* A page after the double bar that ends the piece (Music Major): its staffs are hidden and it doesn't print. */
.cut-note { display: none; margin: 0; color: var(--muted); font-style: italic; text-align: center; }
.page-cut .cut-note { display: block; position: absolute; top: 45%; left: 0; right: 0; }
/* Instrument parts (Music Major): the instrument's name in the top-right margin of each part's first page,
   a copy of the title on later parts' first pages, and the box for adding parts below the last page. */
.part-name { position: absolute; top: 12px; right: calc((var(--page-w) - 720px) / 2); line-height: 0; pointer-events: none; }
.part-name[hidden], .part-head[hidden] { display: none; }
.part-head .rt-edit { cursor: default; }
.parts-box {
  width: 816px; max-width: 100%; margin: 18px auto 40px; padding: 14px 18px; box-sizing: border-box;
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.parts-box h2 { margin: 0 0 6px; font-size: 16px; }
.parts-box p { margin: 6px 0; }
.parts-first { display: inline-flex; align-items: center; gap: 8px; margin: 6px 0; }
.parts-choices { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 16px; margin: 8px 0; }
.parts-choices .check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.parts-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.parts-actions button { padding: 7px 14px; }
.parts-actions button:disabled { opacity: .5; cursor: not-allowed; }
.parts-hint { color: var(--muted); font-style: italic; }
@media print {
  .add-page-bar, .page-tools, .cut-note, .parts-box { display: none !important; }
  .page-extra { margin-top: 0; break-before: page; }
  .page-cut { display: none !important; }
}
