:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #f0ede6;
  --border: #e2ddd3;
  --text: #1d1b18;
  --muted: #6d675d;
  --ink: #1d1b18;
  --staff: #8b8478;
  --accent: #d9480f;
  --accent-soft: #fde6da;
  --accent-text: #ffffff;
  --perfect: #1f9d55;
  --good: #3fae6a;
  --ok: #d18b00;
  --bad: #d63333;
  --playhead: rgba(217, 72, 15, 0.55);
  --now: #d9480f;
  --shadow: 0 1px 2px rgba(30, 25, 15, 0.06), 0 4px 16px rgba(30, 25, 15, 0.05);
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15140f;
    --surface: #1f1d18;
    --surface-2: #29261f;
    --border: #36322a;
    --text: #efebe3;
    --muted: #a39c8e;
    --ink: #efebe3;
    --staff: #8a8375;
    --accent: #ff7a3d;
    --accent-soft: #3d2416;
    --accent-text: #1a0d05;
    --perfect: #3ccf7a;
    --good: #5bd08a;
    --ok: #f0b429;
    --bad: #ff5c5c;
    --playhead: rgba(255, 122, 61, 0.6);
    --now: #ff7a3d;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { line-height: 1.2; margin: 0; }
button, input, select { font: inherit; color: inherit; }
kbd {
  display: inline-block;
  min-width: 1.7em;
  padding: 0 0.4em;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--surface);
  font: 600 0.85em/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: center;
  margin: 0 2px;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* Buttons */
button { cursor: pointer; }
.primary {
  background: var(--accent);
  color: var(--accent-text);
  border: 0;
  border-radius: 10px;
  padding: 9px 18px;
  font-weight: 650;
}
.primary:hover { filter: brightness(1.06); }
.ghost, .ghost-link {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 7px 12px;
  color: var(--text);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.ghost:hover, .ghost-link:hover { background: var(--surface-2); }
.ghost.danger { color: var(--bad); }
.ghost.wide { width: 100%; justify-content: center; margin-top: 8px; }
.small-btn { padding: 3px 9px; font-size: 13px; align-self: flex-start; }
.round {
  width: 32px; height: 32px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 18px;
  line-height: 1;
}
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 750; font-size: 17px; color: var(--accent); }
.brand span { color: var(--text); }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.session-timer { display: flex; align-items: baseline; gap: 6px; padding: 0 8px; font-size: 14px; }
.session-timer output { font-weight: 700; font-variant-numeric: tabular-nums; min-width: 3.2em; }
.only-mobile { display: none; }

/* Layout */
.layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  min-height: calc(100vh - 57px);
}
.sidebar {
  border-right: 1px solid var(--border);
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  background: var(--surface);
}
.sidebar h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 6px 10px;
}
.lesson { margin-bottom: 2px; }
.lesson summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 9px;
}
.lesson summary::-webkit-details-marker { display: none; }
.lesson summary::before { content: '▸'; color: var(--muted); font-size: 12px; transition: transform 0.15s; }
.lesson[open] summary::before { transform: rotate(90deg); }
.lesson summary:hover { background: var(--surface-2); }
.lesson-title { font-weight: 650; flex: 1; }
.lesson.complete .lesson-meta { color: var(--perfect); font-weight: 650; }
.lesson .steps { margin: 2px 0 8px 10px; }
.steps { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.step {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 7px 8px;
  border-radius: 9px;
}
.step:hover { background: var(--surface-2); }
.step.active { background: var(--accent-soft); font-weight: 600; }
.step .status {
  flex: none;
  width: 20px; height: 20px;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 800;
}
.step.done .status { background: var(--perfect); border-color: var(--perfect); color: #fff; }

main {
  padding: 22px 28px 40px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
  max-width: 1200px;
}

.pattern-head { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; }
.eyebrow { margin: 0 0 4px; color: var(--muted); font-size: 13px; }
.pattern-head h1 { font-size: 26px; }
.desc { margin: 6px 0 0; max-width: 70ch; }
.tips { margin: 6px 0 0; padding-left: 18px; color: var(--muted); max-width: 70ch; }
.head-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; flex: none; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
}
.card h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); margin-bottom: 10px; }

/* Transport */
.transport { display: flex; flex-wrap: wrap; align-items: center; gap: 14px 24px; }
.play { font-size: 17px; padding: 12px 22px; min-width: 120px; border-radius: 12px; }
.play.playing { background: var(--text); color: var(--bg); }
.tempo { display: flex; align-items: center; gap: 8px; }
.tempo .label { color: var(--muted); font-size: 13px; }
.tempo input[type="range"] { width: 180px; accent-color: var(--accent); }
.tempo-value { font-variant-numeric: tabular-nums; min-width: 78px; }
.tempo-value output { font-weight: 750; font-size: 20px; }
.toggles { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; flex-basis: 100%; }
.switch { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; font-size: 14px; }
.switch input { accent-color: var(--accent); width: 16px; height: 16px; }
.trainer-cfg { font-size: 13px; color: var(--muted); }
.trainer-cfg input {
  width: 52px;
  padding: 2px 4px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
}

/* Notation */
.staff-card { position: relative; padding: 8px 14px 12px; }
#notation { width: 100%; }
.notation { display: block; width: 100%; height: auto; }
.notation .line { stroke: var(--staff); stroke-width: 1.1; }
.notation .ink { fill: var(--ink); }
.notation .stem { stroke: var(--ink); stroke-width: 1.4; }
.notation .rest { stroke: var(--ink); stroke-width: 1.7; fill: none; stroke-linecap: round; }
.notation .qrest { stroke-width: 3.2; stroke-linejoin: round; }
.notation .open { fill: none; stroke: var(--ink); stroke-width: 1.4; }
.notation .timesig { font: 800 26px/1 Georgia, "Times New Roman", serif; fill: var(--ink); }
.notation .barnum { font-size: 11px; fill: var(--muted); }
.notation .count { font-size: 14px; fill: var(--muted); }
.notation .count.beat { font-weight: 700; fill: var(--text); }
.notation .count.now { fill: var(--now); font-weight: 800; }
.notation .head { color: var(--ink); transition: color 0.12s; }
.notation .nhead { fill: currentColor; }
.notation .xhead { stroke: currentColor; stroke-width: 2.1; stroke-linecap: round; fill: none; }
.notation .head.n-perfect, .notation .head.n-good { color: var(--perfect); }
.notation .head.n-ok { color: var(--ok); }
.notation .head.n-miss { color: var(--bad); }
.notation .head.n-perfect .nhead, .notation .head.n-good .nhead,
.notation .head.n-ok .nhead, .notation .head.n-miss .nhead { stroke: currentColor; stroke-width: 3; }
.notation .head.n-perfect .xhead, .notation .head.n-good .xhead,
.notation .head.n-ok .xhead, .notation .head.n-miss .xhead { stroke-width: 3.2; }
.notation .playhead { fill: var(--playhead); }
.notation .mark circle { fill: none; stroke: var(--bad); stroke-width: 2; }
.notation .mark path { stroke: var(--bad); stroke-width: 2; }
.notation .mark.extra circle { stroke-dasharray: 3 3; }

.countin {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 96px;
  font-weight: 800;
  color: var(--accent);
  pointer-events: none;
  opacity: 0;
  background: color-mix(in srgb, var(--surface) 70%, transparent);
  border-radius: var(--radius);
  transition: opacity 0.1s;
}
.countin.show { opacity: 1; }
.keyhint { margin: 4px 4px 0; display: flex; flex-wrap: wrap; gap: 6px 18px; font-size: 14px; align-items: center; }

/* Feedback */
.feedback-grid { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr) minmax(0, 1fr); gap: 16px; }
.judge { display: flex; flex-direction: column; gap: 12px; }
.judge-text { font-size: 20px; font-weight: 700; line-height: 1.3; height: 2.6em; overflow: hidden; }
.judge-text.perfect { color: var(--perfect); }
.judge-text.good { color: var(--good); }
.judge-text.ok { color: var(--ok); }
.judge-text.bad { color: var(--bad); }

.meter {
  position: relative;
  height: 26px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--bad) 14%, var(--surface-2));
  overflow: hidden;
}
.meter .zone { position: absolute; top: 0; bottom: 0; left: 50%; transform: translateX(-50%); }
.meter .zone.ok { width: 100%; background: color-mix(in srgb, var(--ok) 22%, var(--surface-2)); }
.meter .zone.good { background: color-mix(in srgb, var(--good) 28%, var(--surface-2)); }
.meter .zone.perfect { background: color-mix(in srgb, var(--perfect) 45%, var(--surface-2)); }
.meter .center { position: absolute; left: 50%; top: 0; bottom: 0; width: 2px; background: var(--text); opacity: 0.5; }
.meter .tick { position: absolute; top: 3px; bottom: 3px; width: 4px; margin-left: -2px; border-radius: 2px; background: var(--text); }
.meter .tick.perfect, .meter .tick.good { background: var(--perfect); }
.meter .tick.ok { background: var(--ok); }
.meter-labels { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }

.goal-dots { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.goal-dots .dot { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--border); transition: background 0.15s, border-color 0.15s; }
.goal-dots .dot.on { background: var(--perfect); border-color: var(--perfect); }
.goal-dots .dot.practice { background: var(--muted); border-color: var(--muted); opacity: 0.55; }
.goal-text { margin: 0 0 6px; font-size: 14px; }
.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.chip.success { background: var(--perfect); color: #fff; }

.stats dl { margin: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; }
.stats dt { font-size: 12px; color: var(--muted); }
.stats dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }

/* Pads */
.pads { display: flex; flex-wrap: wrap; gap: 12px; }
.pad {
  flex: 1 1 140px;
  max-width: 220px;
  min-height: 84px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  touch-action: manipulation;
  user-select: none;
}
.pad-name { font-weight: 650; }
.pad-keys { font: 600 13px ui-monospace, Menlo, monospace; color: var(--muted); }
.pad.flash { animation: flash 0.16s ease-out; }
@keyframes flash {
  from { background: var(--accent); color: var(--accent-text); }
  to { background: var(--surface); }
}

/* Dialogs */
.modal {
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100vh - 40px);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.wide-modal { width: min(1100px, calc(100vw - 32px)); }
.modal::backdrop { background: rgba(10, 8, 5, 0.45); }
.modal-body { padding: 18px 22px 22px; display: flex; flex-direction: column; gap: 10px; }
.modal-body h3 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); margin-top: 12px; }
.modal-body p { margin: 0; }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-foot { display: flex; gap: 8px; align-items: center; margin-top: 8px; }
.spacer { flex: 1; }

.key-table { display: flex; flex-direction: column; gap: 4px; }
.key-row { display: grid; grid-template-columns: 120px 1fr; align-items: center; gap: 10px; }
.key-row-action { padding-bottom: 8px; margin-bottom: 4px; border-bottom: 1px solid var(--border); }
.key-name { text-align: left; border: 0; background: none; padding: 4px 0; font-weight: 600; }
.key-name:hover { color: var(--accent); }
.key-chips { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.key-chip { display: inline-flex; align-items: center; }
.key-chip button { border: 0; background: none; color: var(--muted); padding: 0 4px; font-size: 16px; }
.key-chip button:hover { color: var(--bad); }
.listening { border-color: var(--accent); color: var(--accent); }

.field { display: grid; grid-template-columns: 170px 1fr auto; align-items: center; gap: 10px; }
.field input[type="range"] { accent-color: var(--accent); width: 100%; }
.seg { display: flex; gap: 6px; grid-column: span 2; }
.seg-btn {
  flex: 1;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.2;
}
.seg-btn small { color: var(--muted); font-size: 11px; }
.seg-btn.active { border-color: var(--accent); background: var(--accent-soft); font-weight: 650; }
.row-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.file-btn { cursor: pointer; }
.calibrate button { align-self: center; }
.calibrate { display: flex; align-items: center; gap: 12px; }
.calib-body { text-align: center; align-items: center; }
.calib-beat { font-size: 80px; font-weight: 800; color: var(--accent); line-height: 1.1; min-height: 1.1em; }

/* Editor */
.editor-fields { display: flex; flex-wrap: wrap; gap: 10px 16px; }
.editor-fields label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 3px; }
.editor-fields input, .editor-fields select {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  background: var(--surface);
  font-size: 14px;
}
.editor-fields input[type="number"] { width: 90px; }
.grid-wrap { overflow-x: auto; padding-bottom: 4px; }
.ed-grid {
  display: grid;
  grid-template-columns: 100px repeat(var(--cols), minmax(26px, 1fr));
  gap: 3px;
  min-width: min-content;
}
.ed-label { font-size: 12px; text-align: right; padding-right: 8px; border: 0; background: none; align-self: center; white-space: nowrap; }
button.ed-label:hover { color: var(--accent); }
.ed-count { font-size: 11px; text-align: center; color: var(--muted); }
.ed-count.bar-start, .ed-count.beat-start { color: var(--text); font-weight: 700; }
.ed-cell { height: 26px; border-radius: 5px; border: 1px solid var(--border); background: var(--surface-2); padding: 0; }
.ed-cell.beat-start { margin-left: 4px; }
.ed-cell.bar-start { margin-left: 12px; }
.ed-cell:hover { border-color: var(--accent); }
.ed-cell.on { background: var(--accent); border-color: var(--accent); }
.ed-count.beat-start { margin-left: 4px; }
.ed-count.bar-start { margin-left: 12px; }
.ed-preview { border: 1px solid var(--border); border-radius: 12px; padding: 4px 8px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 30px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 12px;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.success { background: var(--perfect); color: #fff; }
.noscript { padding: 20px; text-align: center; }

/* Narrow screens */
@media (max-width: 1000px) {
  .feedback-grid { grid-template-columns: 1fr 1fr; }
  .feedback-grid .judge { grid-column: span 2; }
}
@media (max-width: 800px) {
  .only-mobile { display: inline-flex; }
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: 57px;
    left: 0;
    bottom: 0;
    width: min(320px, 85vw);
    z-index: 20;
    transform: translateX(-105%);
    transition: transform 0.2s;
    overflow-y: auto;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.25);
  }
  .nav-open .sidebar { transform: none; }
  main { padding: 16px; }
  .pattern-head { flex-direction: column; }
  .head-actions { justify-content: flex-start; }
  .feedback-grid { grid-template-columns: 1fr; }
  .feedback-grid .judge { grid-column: auto; }
  .tempo input[type="range"] { width: 120px; }
  .field { grid-template-columns: 1fr; }
  .topbar { padding: 10px 16px; }
}
