:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-muted: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #475569;
  --text-soft: #94a3b8;

  --sharp: #e67e22;
  --sharp-bg: #fff7ed;
  --flat: #2563eb;
  --flat-bg: #eff6ff;
  --natural: #111827;

  --accent: #0f172a;
  --accent-hover: #1e293b;
  --play-active: #0d9488;
  --play-ring: rgba(13, 148, 136, 0.28);
  --tonic-fill: #fef9c3;
  --tonic-stroke: #eab308;
  --mark-color: #e11d48;
  --mark-bg: #fff1f2;

  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
  cursor: pointer;
}

.app-shell {
  max-width: 1040px;
  margin: 0 auto;
  padding: 18px 16px 36px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  display: grid;
  place-items: center;
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.brand-text h1 {
  margin: 0;
  font-size: 21px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.subtitle {
  margin: 2px 0 0;
  font-size: 14px;
  color: var(--text-muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 38px;
  padding: 0 15px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  transition: all 0.15s ease;
  white-space: nowrap;
}

.btn:hover {
  background: var(--surface-muted);
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-primary.playing {
  background: var(--play-active);
  border-color: var(--play-active);
}

.btn-outline {
  background: var(--surface);
}

.btn-toggle[aria-pressed="true"] {
  background: var(--mark-bg);
  border-color: var(--mark-color);
  color: var(--mark-color);
}

.btn-ghost {
  border-color: transparent;
  background: transparent;
  color: var(--text-muted);
  padding: 0 10px;
}

.btn-ghost:hover {
  background: #e2e8f0;
  color: var(--text);
}

.badge-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--mark-color);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
}

/* Controls Card */
.controls-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  box-shadow: var(--shadow-sm);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.control-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.keys-row {
  justify-content: flex-start;
  align-items: flex-start;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.control-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  min-width: 74px;
  padding-top: 2px;
}

.segmented {
  display: inline-flex;
  background: #f1f5f9;
  padding: 3px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  gap: 2px;
}

.seg-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 600;
  padding: 6px 13px;
  border-radius: 6px;
  transition: all 0.14s ease;
}

.segmented-sm .seg-btn {
  font-size: 13px;
  padding: 5px 11px;
}

.seg-btn:hover {
  color: var(--text);
}

.seg-btn.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
}

.toggle-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 36px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-muted);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}

.toggle-chip.is-disabled {
  opacity: 0.48;
  cursor: not-allowed;
}

.toggle-chip input {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
  margin: 0;
}

.key-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  flex: 1;
}

.key-pill {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 600;
  padding: 6px 12px;
  border-radius: 999px;
  transition: all 0.14s ease;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.key-pill:hover {
  border-color: var(--border-strong);
  background: var(--surface-muted);
}

.key-pill.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.key-pill .acc-tag {
  font-size: 11px;
  opacity: 0.78;
}

.key-pill.sharp-key:not(.active) .acc-tag {
  color: var(--sharp);
  opacity: 1;
}

.key-pill.flat-key:not(.active) .acc-tag {
  color: var(--flat);
  opacity: 1;
}

.key-pill:not(.active) .flat-num {
  color: var(--flat);
}

.key-pill:not(.active) .sharp-num {
  color: var(--sharp);
}

.key-pill:not(.active) .dual-acc-tag {
  opacity: 1;
}

/* Main White Sheet Stage */
.sheet-stage {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 22px 18px;
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.chart-header-banner {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  border-bottom: 1px solid #f1f5f9;
  padding-bottom: 12px;
}

.chart-title-block h2 {
  margin: 0;
  font-size: 23px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.stage-key-row {
  margin-top: 5px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  color: var(--text-muted);
  font-weight: 500;
}

.key-sig-badge {
  font-size: 13px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  background: #f1f5f9;
  color: var(--text-muted);
}

.active-note-pill {
  font-size: 14px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  color: var(--text-muted);
  min-height: 34px;
  display: inline-flex;
  align-items: center;
}

.active-note-pill.has-note {
  background: #f0fdfa;
  border-color: #99f6e4;
  color: #0f766e;
}

.dual-subbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px 12px;
}

.dual-hint {
  font-size: 12.5px;
  color: var(--text-muted);
  font-weight: 500;
}

/* Staff */
.staff-section {
  width: 100%;
  overflow-x: hidden;
}

.staff-container {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

.staff-svg {
  width: 100%;
  max-width: 680px;
  height: auto;
  display: block;
  overflow: visible;
}

.staff-note-hit {
  cursor: pointer;
}

.staff-note-hit .note-bg {
  fill: transparent;
  transition: fill 0.12s ease;
}

.staff-note-hit:hover .note-bg {
  fill: rgba(15, 23, 42, 0.06);
}

.staff-note-hit.is-active .note-bg {
  fill: rgba(13, 148, 136, 0.16);
}

.staff-note-hit.is-active .note-head,
.staff-note-hit.is-active .note-stem,
.staff-note-hit.is-active .note-acc {
  fill: var(--play-active);
  stroke: var(--play-active);
}

.staff-note-hit.is-marked .note-mark-ring {
  stroke: var(--mark-color);
  stroke-width: 2;
  fill: none;
}

/* Fingerboard */
.fingerboard-section {
  width: 100%;
  display: flex;
  justify-content: center;
}

.fingerboard-container {
  width: 100%;
  display: flex;
  justify-content: center;
  gap: 24px;
  flex-wrap: wrap;
}

.fb-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 410px;
}

.fb-panel-all {
  max-width: 450px;
}

.fb-panel-dual {
  max-width: 390px;
}

.fb-panel-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.fb-svg {
  width: 100%;
  height: auto;
  display: block;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.fb-node {
  cursor: pointer;
}

.fb-node .node-circle {
  transition: transform 0.12s ease, stroke-width 0.12s ease, fill 0.12s ease;
  transform-box: fill-box;
  transform-origin: center;
}

.fb-node.is-tonic .node-circle {
  fill: var(--tonic-fill);
}

.fb-open.is-tonic .open-pill {
  fill: var(--tonic-fill);
}

.fb-node:hover .node-circle {
  fill: #f8fafc;
  stroke-width: 2.3;
}

.fb-node.is-tonic:hover .node-circle {
  fill: #fef08a;
}

.fb-node.is-active .node-circle {
  fill: #ccfbf1;
  stroke: var(--play-active);
  stroke-width: 2.8;
  transform: scale(1.07);
}

.fb-node.is-marked .node-mark-halo {
  stroke: var(--mark-color);
  stroke-width: 2.6;
  fill: none;
  stroke-dasharray: none;
}

.fb-node.is-marked .node-circle {
  fill: var(--mark-bg);
}

.fb-open {
  cursor: pointer;
}

.fb-open:hover .open-pill {
  fill: #f1f5f9;
  stroke: var(--border-strong);
}

.fb-open.is-active .open-pill {
  fill: #ccfbf1;
  stroke: var(--play-active);
}

.fb-open.is-marked .open-pill {
  fill: var(--mark-bg);
  stroke: var(--mark-color);
  stroke-width: 2;
}

.finger-badge-bg {
  fill: #0f172a;
}

.fb-node.is-active .finger-badge-bg,
.fb-open.is-active .finger-badge-bg {
  fill: var(--play-active);
}

.fb-node.is-marked .finger-badge-bg,
.fb-open.is-marked .finger-badge-bg {
  fill: var(--mark-color);
}

.finger-badge-text {
  fill: #ffffff;
  font-size: 10.5px;
  font-weight: 700;
}

/* Legend */
.legend-bar {
  border-top: 1px solid #f1f5f9;
  padding-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.legend-items {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  font-size: 13px;
  color: var(--text-muted);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend-dot {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 1.5px solid #111827;
  display: inline-grid;
  place-items: center;
  font-size: 10.5px;
  font-weight: 700;
  line-height: 1;
}

.legend-dot.natural {
  background: #fff;
}

.legend-dot.sharp {
  color: var(--sharp);
  border-color: var(--sharp);
}

.legend-dot.flat {
  color: var(--flat);
  border-color: var(--flat);
}

.legend-tonic {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 1.5px solid #ca8a04;
  background: var(--tonic-fill);
  display: inline-block;
}

.legend-tape {
  width: 22px;
  height: 4px;
  border-radius: 999px;
  background: rgba(74, 222, 128, 0.34);
  box-shadow: 0 0 0 2px rgba(134, 239, 172, 0.18);
  display: inline-block;
}

.legend-dashed {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 1.5px dashed #cbd5e1;
  display: inline-block;
}

.legend-pin {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  border: 2px solid var(--mark-color);
  background: var(--mark-bg);
  display: inline-block;
}

.legend-tip {
  font-size: 13px;
  color: var(--text-soft);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #0f172a;
  color: #ffffff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 10px 30px rgba(15, 23, 42, 0.22);
  z-index: 100;
}

@media (max-width: 640px) {
  .hide-sm {
    display: none;
  }
  .app-shell {
    padding: 10px 10px 26px;
    gap: 10px;
  }
  .topbar {
    gap: 10px;
  }
  .header-actions {
    width: 100%;
    gap: 6px;
  }
  .header-actions .btn {
    height: 36px;
    padding: 0 11px;
    font-size: 13px;
  }
  .controls-card {
    padding: 11px 10px;
    gap: 9px;
  }
  .control-row {
    gap: 8px;
  }
  .pos-group {
    width: 100%;
    justify-content: space-between;
    flex-wrap: nowrap;
  }
  .control-label {
    min-width: auto;
    font-size: 12.5px;
    padding-top: 0;
  }
  .pos-group .segmented {
    flex: 1;
    justify-content: space-between;
  }
  .pos-group .seg-btn {
    flex: 1;
    padding: 6px 8px;
    font-size: 13.5px;
  }
  .control-toggles {
    width: 100%;
    justify-content: space-between;
    flex-wrap: nowrap;
    gap: 6px;
  }
  .toggle-chip {
    height: 35px;
    padding: 0 10px;
    font-size: 13px;
    flex-shrink: 0;
  }
  .control-toggles .segmented {
    flex: 1;
    justify-content: space-between;
  }
  .control-toggles .seg-btn {
    flex: 1;
    padding: 5px 6px;
    font-size: 13px;
  }
  .keys-row {
    flex-direction: column;
    gap: 6px;
  }
  .key-pills {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 6px;
  }
  .key-pill {
    justify-content: center;
    padding: 0 4px;
    height: 35px;
    font-size: 12.5px;
    border-radius: 9px;
    gap: 2px;
    white-space: nowrap;
  }
  .key-pill-wide {
    grid-column: span 2;
  }
  .sheet-stage {
    padding: 14px 12px;
  }
  .brand-text h1 {
    font-size: 17px;
  }
  .chart-title-block h2 {
    font-size: 18px;
  }
}
