/* ScoreAnalyser design system: tokens, base, components. The score itself always sits on light
   "paper" (dark mode only changes the chrome around it) so notation stays easy to read. */

:root {
  --bg: #f4f2ec;
  --surface: #ffffff;
  --surface-2: #f8f7f3;
  --ink: #1d1f24;
  --muted: #5d6270;
  --faint: #8b909c;
  --line: #dedbd2;
  --accent: #3553c9;
  --accent-ink: #ffffff;
  --accent-soft: #e8ecfb;
  --warn-soft: #fff4d9; --warn-ink: #7a5600; --warn-line: #efd9a0;
  --bad-soft: #fde4e4;  --bad-ink: #9b1c1c;
  --good: #1f8a5b;
  --paper: #fffdf8;
  --paper-line: #e4dfd2;
  --rh: #2b8ce6; --rh-dark: #1f6fb8;
  --lh: #f0a030; --lh-dark: #c97f1e;
  --playing: #d6336c;
  --range: #3553c9;
  --shadow: 0 1px 2px rgba(20, 20, 30, .06), 0 4px 16px rgba(20, 20, 30, .07);
  --shadow-up: 0 -2px 18px rgba(20, 20, 30, .10);
  --radius: 12px;
  --tap: 44px;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-title: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-mono: ui-monospace, "Cascadia Mono", Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171c; --surface: #1f2229; --surface-2: #262a32; --ink: #eceef2; --muted: #a9afbc; --faint: #7d8492;
    --line: #353a45; --accent: #8fa2ff; --accent-ink: #10131c; --accent-soft: #2a3150;
    --warn-soft: #3a3120; --warn-ink: #f3d38a; --warn-line: #5b4b28; --bad-soft: #4a2326; --bad-ink: #ffb4b4;
    --paper: #f7f3ea; --paper-line: #3a3f4a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 18px rgba(0, 0, 0, .35); --shadow-up: 0 -2px 18px rgba(0, 0, 0, .45);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171c; --surface: #1f2229; --surface-2: #262a32; --ink: #eceef2; --muted: #a9afbc; --faint: #7d8492;
  --line: #353a45; --accent: #8fa2ff; --accent-ink: #10131c; --accent-soft: #2a3150;
  --warn-soft: #3a3120; --warn-ink: #f3d38a; --warn-line: #5b4b28; --bad-soft: #4a2326; --bad-ink: #ffb4b4;
  --paper: #f7f3ea; --paper-line: #3a3f4a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 18px rgba(0, 0, 0, .35); --shadow-up: 0 -2px 18px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 15px/1.5 var(--font-ui); background: var(--bg); color: var(--ink); }
a { color: var(--accent); }
h1, h2, h3 { font-family: var(--font-title); font-weight: 600; letter-spacing: .005em; }
[hidden] { display: none !important; }
.wrap { max-width: 1280px; margin: 0 auto; padding: 0 16px; }
body.dev-off .dev-only { display: none !important; }

/* ---- buttons & form controls ---- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: var(--tap); min-width: var(--tap);
  padding: 0 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font: inherit; font-weight: 500; cursor: pointer; text-decoration: none; transition: background .12s, border-color .12s; }
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, .step:focus-visible, .seg button:focus-visible, .chip:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.btn .ico { font-size: 18px; line-height: 1; }
.btn.round { border-radius: 50%; padding: 0; }
input[type=number], input[type=text], select { min-height: 36px; padding: 4px 8px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--ink); font: inherit; }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
input[type=range] { accent-color: var(--accent); }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--surface); }
.seg button { min-height: 38px; padding: 0 12px; border: 0; background: transparent; color: var(--ink); font: inherit; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }

.kbd, kbd { display: inline-block; min-width: 22px; padding: 1px 6px; border: 1px solid var(--line); border-bottom-width: 2px;
  border-radius: 6px; background: var(--surface-2); font: 12px/1.5 var(--font-mono); text-align: center; }

/* ---- top bar ---- */
.topbar { position: sticky; top: 0; z-index: 30; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.topbar .wrap { display: flex; align-items: center; gap: 12px; min-height: 60px; }
.brand { display: inline-flex; align-items: center; gap: 8px; font: 600 18px var(--font-title); color: var(--ink); text-decoration: none; }
.brand .logo { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-size: 17px; }
.song-id { flex: 1; min-width: 0; }
.song-id h1 { margin: 0; font-size: 20px; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.song-id .sub { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; gap: 6px; }

/* ---- popover panels ---- */
.pop-anchor { position: relative; }
.panel { position: absolute; z-index: 60; min-width: 280px; max-width: min(92vw, 380px); padding: 14px 16px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); }
.panel.right { right: 0; top: calc(100% + 6px); }
.panel.up { bottom: calc(100% + 10px); right: 0; }
.panel h4 { margin: 0 0 8px; font: 600 12px var(--font-ui); text-transform: uppercase; letter-spacing: .06em; color: var(--faint); }
.panel .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 42px; }
.panel .row + .row { border-top: 1px solid var(--line); }
.panel .row label { display: flex; align-items: center; gap: 10px; flex: 1; cursor: pointer; }
.panel .row .hint { color: var(--faint); font-size: 12px; }
.panel .group + .group { margin-top: 14px; }

/* ---- level staircase ---- */
.stairs { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; align-items: end; margin: 16px 0 6px; }
.step { position: relative; display: flex; flex-direction: column; justify-content: flex-end; gap: 2px; text-align: left;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--ink);
  font: inherit; cursor: pointer; transition: transform .12s, border-color .12s, background .12s; }
.step:hover { border-color: var(--accent); }
.step .num { position: absolute; top: 8px; right: 10px; width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center;
  font: 600 12px var(--font-ui); background: var(--surface-2); color: var(--muted); }
.step .lbl { font: 600 16px var(--font-title); }
.step .goal { font-size: 12px; color: var(--muted); line-height: 1.3; }
.step:nth-child(1) { min-height: 72px; } .step:nth-child(2) { min-height: 82px; } .step:nth-child(3) { min-height: 92px; }
.step:nth-child(4) { min-height: 102px; } .step:nth-child(5) { min-height: 112px; }
.step.done { background: var(--surface-2); }
.step.active { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.step.active .goal { color: inherit; opacity: .85; }
.step.active .num { background: var(--accent-ink); color: var(--accent); }
.level-desc { margin: 4px 0 12px; color: var(--muted); max-width: 900px; font-size: 14px; }

/* ---- the score ---- */
#paper-wrap { position: relative; }
#paper { background: var(--paper); border: 1px solid var(--paper-line); border-radius: var(--radius); padding: 8px 12px; overflow-x: auto; box-shadow: var(--shadow); color: #111; }
#paper svg .abcjs-note.playing, #paper svg .abcjs-rest.playing { fill: var(--playing); }
#paper svg .playing { fill: var(--playing); stroke: var(--playing); }
#paper svg .in-range.abcjs-note, #paper svg .in-range.abcjs-rest, #paper svg .in-range.abcjs-bar { fill: var(--range); }
#paper svg .cursor.abcjs-note, #paper svg .cursor.abcjs-rest { fill: var(--good); }
#paper svg [class*="abcjs-mm"] { cursor: pointer; }

body.scrolling #paper-wrap { overflow: hidden; background: var(--paper); border: 1px solid var(--paper-line); border-radius: var(--radius); box-shadow: var(--shadow); }
body.scrolling #paper { border: none; margin: 0; overflow: visible; will-change: transform; width: max-content; box-shadow: none; border-radius: 0; }
body.scrolling #paper svg { display: block; }
#playhead { position: absolute; top: 0; bottom: 0; left: 50%; width: 0; border-left: 2px solid color-mix(in srgb, var(--playing) 75%, transparent); pointer-events: none; z-index: 3; }
#playhead::before { content: ''; position: absolute; top: 0; left: -7px; border: 6px solid transparent; border-top: 8px solid var(--playing); }
body.scrolling #paper-wrap::before, body.scrolling #paper-wrap::after { content: ''; position: absolute; top: 0; bottom: 0; width: 12%; z-index: 2; pointer-events: none; }
body.scrolling #paper-wrap::before { left: 0; background: linear-gradient(to right, var(--paper), transparent); }
body.scrolling #paper-wrap::after { right: 0; background: linear-gradient(to left, var(--paper), transparent); }

/* ---- secondary sections ---- */
.card-sec { margin-top: 16px; padding: 14px 16px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.card-sec h3 { margin: 0 0 8px; font-size: 17px; display: flex; align-items: center; gap: 12px; }
.card-sec h3 .spacer { flex: 1; }
pre#chord-sheet { margin: 0; font: 14px/1.55 var(--font-mono); overflow-x: auto; }
#photo img { display: block; max-width: 100%; border-radius: 8px; border: 1px solid var(--line); }
#photo img + img { margin-top: 12px; }
#abc-source { width: 100%; font: 12px/1.4 var(--font-mono); background: var(--surface-2); color: var(--ink); border: 1px solid var(--line); border-radius: 8px; }
#score-notes { margin: 0; padding-left: 20px; color: var(--muted); font-size: 13px; }
#score-notes li { margin-bottom: 4px; }
.compare { margin-bottom: 12px; padding: 10px 14px; background: var(--warn-soft); color: var(--warn-ink); border: 1px solid var(--warn-line); border-radius: var(--radius); font-size: 14px; }
.compare a { color: inherit; font-weight: 600; }

/* ---- bottom dock: transport + keyboard ---- */
body.practice { padding-bottom: calc(var(--dock-h, 90px) + 24px); }
.dock { position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; background: var(--surface); border-top: 1px solid var(--line); box-shadow: var(--shadow-up); }
.transport { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; min-height: 68px; padding: 8px 0; }
.transport > .group { display: flex; align-items: center; gap: 6px; }
.transport .grow { flex: 1; }
#play { width: 56px; height: 56px; font-size: 22px; }
.bar-pos { display: flex; flex-direction: column; align-items: center; min-width: 74px; line-height: 1.15; }
.bar-pos .big { font: 600 20px var(--font-ui); font-variant-numeric: tabular-nums; }
.bar-pos .small { font-size: 11px; color: var(--faint); text-transform: uppercase; letter-spacing: .06em; }
.tempo { display: flex; align-items: center; gap: 6px; }
.tempo input[type=range] { width: 150px; }
.tempo .val { min-width: 64px; font-variant-numeric: tabular-nums; font-weight: 600; }
.tempo .val small { color: var(--faint); font-weight: 400; }
.status { font-size: 13px; color: var(--muted); min-width: 0; }
#loop-btn .range { font-size: 12px; opacity: .85; font-variant-numeric: tabular-nums; }

/* on-screen keyboard */
#piano-bar { border-top: 1px solid var(--line); padding: 6px 0 10px; }
.piano-head { display: flex; align-items: center; gap: 16px; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.legend .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -2px; margin: 0 4px 0 6px; }
.legend .sw.rh { background: var(--rh); } .legend .sw.lh { background: var(--lh); }
#piano-keys svg { display: block; width: 100%; height: 120px; }
#piano-keys .key { stroke: #555; stroke-width: .6; transition: fill .05s; }
#piano-keys .key.white { fill: #fff; }
#piano-keys .key.black { fill: #222; }
#piano-keys .key.on.rh { fill: var(--rh); }
#piano-keys .key.on.lh { fill: var(--lh); }
#piano-keys .key.black.on.rh { fill: var(--rh-dark); }
#piano-keys .key.black.on.lh { fill: var(--lh-dark); }

/* ---- dialogs ---- */
dialog { border: 1px solid var(--line); border-radius: 16px; padding: 0; background: var(--surface); color: var(--ink); box-shadow: var(--shadow); width: min(92vw, 520px); }
dialog::backdrop { background: rgba(10, 12, 18, .45); }
dialog .dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
dialog .dlg-head h2 { margin: 0; font-size: 19px; }
dialog .dlg-body { padding: 14px 18px 18px; }
.shortcuts { width: 100%; border-collapse: collapse; }
.shortcuts td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
.shortcuts td:first-child { width: 40%; white-space: nowrap; }

.toast { position: fixed; left: 50%; top: 76px; transform: translateX(-50%); z-index: 70; padding: 8px 16px; border-radius: 20px;
  background: var(--ink); color: var(--bg); font-size: 14px; box-shadow: var(--shadow); pointer-events: none; transition: opacity .25s; }

/* ---- responsive ---- */
@media (max-width: 820px) {
  .stairs { grid-template-columns: repeat(5, minmax(96px, 1fr)); overflow-x: auto; padding-bottom: 4px; }
  .step .goal { display: none; }
  .step:nth-child(n) { min-height: 0; }
  .step .lbl { font-size: 14px; padding-right: 22px; }
  .tempo input[type=range] { width: 100px; }
  .status { display: none; }
  .song-id h1 { font-size: 17px; }
}
@media (max-width: 560px) {
  .brand .name, .hide-sm { display: none; }
  .transport { gap: 6px 8px; min-height: 0; }
  .transport > .group:last-child { margin-left: auto; }
  .tempo { order: 5; flex: 1 1 100%; }
  .tempo input[type=range] { flex: 1; width: auto; }
  #play { width: 50px; height: 50px; }
  .bar-pos { min-width: 52px; }
  #piano-keys svg { height: 90px; }
}

@media print {
  .topbar, .stairs, .level-desc, .dock, .card-sec, .compare { display: none !important; }
  body { background: #fff; padding: 0 !important; }
  #paper { border: none; box-shadow: none; }
}

/* dialog rows share the panel look */
.dlg-body .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 48px; }
.dlg-body .row + .row { border-top: 1px solid var(--line); }
.dlg-body .row label { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.hint { color: var(--faint); font-size: 13px; }

/* ---- tablet / touch ---- */
button, .btn, .step, .chip, input, select { touch-action: manipulation; }       /* no double-tap zoom delay */
#paper { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
#paper svg .in-range.cursor.abcjs-note { fill: var(--good); }
/* short screens (tablet in landscape, laptop): a compact level bar leaves the height to the score */
@media (max-height: 900px) and (min-width: 821px) {
  .stairs { margin: 10px 0 4px; grid-template-columns: repeat(5, minmax(0, 1fr)); }
  .step .lbl { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: none; max-width: 100%; }
  .level-desc { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .level-desc:hover { white-space: normal; }
  .step:nth-child(n) { min-height: 0; flex-direction: row; align-items: baseline; gap: 8px; padding: 8px 12px; }
  .step .num { position: static; flex: none; width: 22px; height: 22px; }
  .step .goal { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
  .level-desc { margin: 2px 0 8px; font-size: 13px; }
  .topbar .wrap { min-height: 52px; }
  .transport { min-height: 60px; padding: 4px 0; }
  #play { width: 50px; height: 50px; }
  #piano-bar { padding: 2px 0 6px; }
  .piano-head { display: none; }
  #piano-keys svg { height: 84px; }
}
@media (pointer: coarse) {
  .panel .row { min-height: 48px; }
  input[type=number], select { min-height: 40px; font-size: 16px; }   /* 16px: no iOS zoom on focus */
  input[type=checkbox] { width: 22px; height: 22px; }
  .tempo input[type=range] { height: 32px; }
}

/* credits of shipped songs */
.about dl { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; font-size: 14px; }
.about dt { color: var(--faint); }
.about dd { margin: 0; }
.about p.hint { margin: 10px 0 0; font-size: 12px; }

/* ---- dark skin: optional dark score (light notes on dark paper), for evening practice ---- */
body.score-dark { --paper: #1b1d23; --paper-line: #30343d; }
body.score-dark #paper svg,
body.score-dark .song .thumb .mini svg { filter: invert(1) hue-rotate(180deg); }   /* black engraving -> light, hues kept */
body.score-dark .song .thumb img { filter: brightness(.82) saturate(.85); }        /* calmer photos on dark cards */
[data-theme-toggle] { font-size: 18px; }
@media print {
  body.score-dark #paper { background: #fff; }
  body.score-dark #paper svg { filter: none; }
}
