:root {
  color-scheme: light;
  --bg: #f8fafc;
  --page: #ffffff;
  --surface: #ffffff;
  --col: #f1f5f9;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --ink: #0f172a;
  --ink-2: #334155;
  --muted: #64748b;
  --faint: #94a3b8;
  --inv: #ffffff;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --accent-line: #bfdbfe;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
  --chev: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748b' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d1117;
  --page: #11161f;
  --surface: #161c26;
  --col: #1f2735;
  --line: #263042;
  --line-strong: #364359;
  --ink: #eef2f7;
  --ink-2: #cbd5e1;
  --muted: #94a3b8;
  --faint: #64748b;
  --inv: #0d1117;
  --accent: #60a5fa;
  --accent-soft: rgba(59, 130, 246, 0.16);
  --accent-line: rgba(96, 165, 250, 0.35);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  --chev: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d1117;
    --page: #11161f;
    --surface: #161c26;
    --col: #1f2735;
    --line: #263042;
    --line-strong: #364359;
    --ink: #eef2f7;
    --ink-2: #cbd5e1;
    --muted: #94a3b8;
    --faint: #64748b;
    --inv: #0d1117;
    --accent: #60a5fa;
    --accent-soft: rgba(59, 130, 246, 0.16);
    --accent-line: rgba(96, 165, 250, 0.35);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
    --chev: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

svg {
  flex: none;
}

[hidden] {
  display: none !important;
}

/* Header */
header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  padding-top: max(12px, env(safe-area-inset-top));
  padding-left: max(20px, env(safe-area-inset-left));
  padding-right: max(20px, env(safe-area-inset-right));
  background: var(--page);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 650;
  font-size: 15px;
  letter-spacing: -0.01em;
  text-decoration: none;
  color: var(--ink);
}

.brand i {
  width: 23px;
  height: 23px;
  border-radius: 6px;
  background: #0f172a;
  border: 1px solid rgba(255, 255, 255, 0.12);
  display: grid;
  place-items: center;
  color: #faf8f3;
  font: 500 15px/1 "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  font-style: normal;
}

.spacer {
  flex: 1;
}

/* Core Controls & Buttons */
.btn {
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  transition: background 0.14s, border-color 0.14s, color 0.14s;
}

.btn:hover {
  background: var(--col);
  border-color: var(--line-strong);
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
  pointer-events: none;
}

.btn.sm {
  height: 28px;
  padding: 0 10px;
  font-size: 12.5px;
  border-radius: 7px;
}

.btn.icon-only {
  width: 34px;
  padding: 0;
}

.btn.sm.icon-only {
  width: 28px;
  height: 28px;
  padding: 0;
}

.btn.primary {
  background: #2563eb;
  border-color: #2563eb;
  color: #ffffff;
}

.btn.primary:hover {
  background: #1d4ed8;
  border-color: #1d4ed8;
}

.btn.active-bookmark,
.btn[aria-pressed="true"] {
  color: var(--accent);
  border-color: var(--accent-line);
  background: var(--accent-soft);
}

.field {
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  padding: 0 10px;
  font-size: 13px;
  font-weight: 500;
  transition: border-color 0.14s;
}

.field:hover {
  border-color: var(--line-strong);
}

.field:focus {
  outline: none;
  border-color: var(--accent);
}

.select,
select.field,
.props select {
  appearance: none;
  -webkit-appearance: none;
  background-image: var(--chev);
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding: 0 28px 0 10px;
  vertical-align: middle;
}

select.field {
  height: 34px;
  line-height: 32px;
}

select::-ms-expand {
  display: none;
}

select::picker-icon {
  display: none;
}

.seg {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 2px;
  background: var(--col);
  height: 34px;
}

.seg.sm {
  height: 28px;
  border-radius: 7px;
}

.seg button {
  border: 0;
  background: none;
  border-radius: 6px;
  padding: 0 9px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.seg.sm button {
  padding: 0 8px;
  font-size: 12.5px;
  border-radius: 5px;
}

.seg button:hover {
  color: var(--ink);
}

.seg button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow);
}

/* Main Layout */
main {
  max-width: 780px;
  width: 100%;
  margin: 0 auto;
  padding: 18px 20px max(48px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 18px;
}

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

.stage {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.meta-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 28px;
}

.meta-info {
  border: 0;
  background: none;
  padding: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  text-align: left;
}

.meta-info.peekable:hover {
  color: var(--accent);
}

.meta-acts {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* Bar Strip */
.bar-strip {
  display: grid;
  grid-template-columns: repeat(var(--bar-cols, 4), minmax(0, 1fr));
  gap: 8px;
}

@keyframes barPulse {
  from { opacity: 0.55; }
  to { opacity: 1; }
}

.bar-strip.pulse {
  animation: barPulse 0.16s ease-out;
}

.bar-cell {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  padding: 9px 11px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
  overflow: hidden;
  transition: border-color 0.14s, background 0.14s;
}

.bar-cell:hover {
  border-color: var(--line-strong);
  background: var(--bg);
}

.bar-cell.iso {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.bar-cell.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.bar-cell.play-phase {
  border-style: dashed;
}

.bar-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px;
  min-height: 18px;
}

.bar-num {
  font: 500 11.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--faint);
  font-variant-numeric: tabular-nums;
}

.bar-cell.active .bar-num {
  color: var(--accent);
}

.bar-phase {
  font-size: 11px;
  font-weight: 600;
  color: var(--accent);
}

.bar-chord {
  font-size: 13.5px;
  font-weight: 650;
  color: var(--ink);
  margin-left: auto;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bar-chord.split {
  font-size: 11.5px;
  letter-spacing: -0.015em;
}

.ans-idiom {
  font-size: 12.5px;
  color: var(--muted);
  font-weight: 500;
}

.bar-beats {
  display: flex;
  align-items: center;
  gap: 5px;
}

.bdot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--line-strong);
  transition: background 0.08s, transform 0.08s;
}

.bdot.on {
  background: var(--accent);
  transform: scale(1.25);
}

.bar-prog {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2.5px;
  background: transparent;
}

.bar-prog > span {
  display: block;
  width: 100%;
  height: 100%;
  background: var(--accent);
  transform-origin: left center;
  transform: scaleX(0);
}

/* Answer Reveal Section */
.answer {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  padding: 12px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ans-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.tab-wrap,
.neck-wrap {
  width: 100%;
  overflow: hidden;
}

/* Tab SVG */
.tab-svg {
  display: block;
  user-select: none;
}

.tab-line {
  stroke: var(--line-strong);
  stroke-width: 1;
}

.tab-barline {
  stroke: var(--muted);
  stroke-width: 1.25;
}

.tab-barline.end {
  stroke: var(--ink);
  stroke-width: 2;
}

.tab-str-lbl {
  font: 600 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  fill: var(--faint);
}

.tab-chord {
  font: 650 12.5px/1 -apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif;
  fill: var(--ink);
}

.tab-barnum {
  font: 500 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  fill: var(--faint);
}

.tab-note {
  cursor: pointer;
}

.tab-note:focus {
  outline: none;
}

.tab-pill {
  fill: var(--surface);
  stroke: transparent;
  transition: fill 0.1s;
}

.tab-fret {
  font: 650 11.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  fill: var(--ink);
  text-anchor: middle;
  font-variant-numeric: tabular-nums;
}

.tab-note:hover .tab-pill {
  fill: var(--col);
}

.tab-note.active .tab-pill {
  fill: var(--accent);
}

.tab-note.active .tab-fret {
  fill: var(--inv);
}

.tab-dur {
  stroke: var(--line-strong);
  stroke-width: 1.5;
  stroke-linecap: round;
}

.tab-dur.short {
  stroke: var(--muted);
}

.tab-dur.long {
  stroke: var(--ink-2);
  stroke-width: 2;
}

.tab-deg {
  font: 500 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  fill: var(--muted);
  text-anchor: middle;
}

.tab-deg.root {
  fill: var(--ink);
  font-weight: 700;
}

.tab-stem {
  stroke: var(--muted);
  stroke-width: 1.2;
}

.tab-beam {
  stroke: var(--ink-2);
  stroke-width: 2.2;
  stroke-linecap: butt;
}

.tab-augdot {
  fill: var(--ink-2);
}

.tab-trip {
  font: 600 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  fill: var(--muted);
  text-anchor: middle;
}

.tab-slur {
  fill: none;
  stroke: var(--ink-2);
  stroke-width: 1.2;
}

.tab-slur-lbl {
  font: 600 9.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  fill: var(--muted);
  text-anchor: middle;
}

.tab-slide {
  stroke: var(--ink-2);
  stroke-width: 1.4;
  stroke-linecap: round;
}

/* Neck SVG */
.neck-svg {
  display: block;
  user-select: none;
}

.neck-wood {
  fill: var(--bg);
  stroke: var(--line);
  stroke-width: 1;
}

.neck-nut {
  stroke: var(--ink);
  stroke-width: 3.5;
  stroke-linecap: round;
}

.neck-wire {
  stroke: var(--line-strong);
  stroke-width: 1.2;
}

.neck-inlay {
  fill: var(--line-strong);
  opacity: 0.7;
}

.neck-string {
  stroke: var(--muted);
  opacity: 0.7;
}

.neck-str-lbl {
  font: 600 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  fill: var(--faint);
}

.neck-fret-lbl {
  font: 500 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  fill: var(--muted);
  text-anchor: middle;
  font-variant-numeric: tabular-nums;
}

.neck-hit {
  fill: transparent;
  cursor: pointer;
}

.neck-ghost {
  fill: none;
  stroke: var(--line-strong);
  stroke-width: 1.2;
  pointer-events: none;
}

.neck-hit:hover {
  fill: var(--accent-soft);
}

.neck-dot {
  cursor: pointer;
}

.neck-dot:focus {
  outline: none;
}

.neck-dot-bg {
  fill: var(--ink);
  transition: fill 0.1s;
}

.neck-dot-ring {
  fill: none;
  stroke: var(--ink);
  stroke-width: 1.5;
}

.neck-dot-txt {
  font: 650 9.5px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  fill: var(--inv);
  text-anchor: middle;
}

.neck-dot.active .neck-dot-bg {
  fill: var(--accent);
}

.neck-dot.active .neck-dot-ring {
  stroke: var(--accent);
}

/* Transport */
.transport {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 4px;
}

.tr-row {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.play-btn {
  min-width: 82px;
  font-weight: 600;
}

.tr-row.sec {
  justify-content: flex-start;
}

/* Dialogs */
dialog {
  border: 1px solid var(--line);
  border-radius: 14px;
  width: min(440px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  padding: 0;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.32);
  background: var(--surface);
  color: var(--ink);
}

dialog:focus {
  outline: none;
}

dialog::backdrop {
  background: rgba(15, 23, 42, 0.35);
  backdrop-filter: blur(2px);
}

.dlg-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px 10px;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
  font-weight: 650;
}

.icon-btn {
  border: 0;
  background: none;
  padding: 5px;
  border-radius: 6px;
  line-height: 0;
  color: var(--faint);
}

.icon-btn:hover {
  background: var(--col);
  color: var(--ink);
}

.dlg-body {
  padding: 12px 18px 18px;
}

.props {
  display: grid;
  grid-template-columns: 104px 1fr;
  gap: 6px 10px;
  align-items: center;
}

.props label {
  font-size: 13px;
  color: var(--muted);
}

.props input[type="number"],
.props select {
  width: 100%;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 9px;
  background-color: var(--surface);
  font-size: 13px;
}

.props select {
  height: 32px;
  line-height: 30px;
  padding: 0 28px 0 9px;
}

.props input:focus,
.props select:focus {
  outline: none;
  border-color: var(--accent);
}

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

.bpm-wrap input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}

.bpm-val {
  min-width: 30px;
  text-align: right;
  font: 600 13px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  font-variant-numeric: tabular-nums;
}

.saved-body {
  padding: 8px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.saved-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
  border-bottom: 1px solid var(--line);
}

.saved-row:last-child {
  border-bottom: 0;
}

.saved-load {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  padding: 4px 6px;
  border-radius: 6px;
  text-align: left;
  font-size: 13.5px;
  font-weight: 500;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.saved-load:hover {
  background: var(--col);
}

.saved-empty {
  padding: 20px 6px;
  text-align: center;
  font-size: 13px;
  color: var(--faint);
}

/* Toast */
.toast {
  z-index: 30;
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translate(-50%, 8px);
  background: var(--ink);
  color: var(--inv);
  padding: 8px 14px;
  border-radius: 9px;
  font-size: 13px;
  opacity: 0;
  transition: opacity 0.18s, transform 0.18s;
  pointer-events: none;
}

.toast.on {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 640px) {
  header {
    padding: 10px 16px;
    padding-top: max(10px, env(safe-area-inset-top));
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
  }

  main {
    padding: 14px 16px max(40px, env(safe-area-inset-bottom));
    gap: 14px;
  }

  .controls {
    gap: 6px;
  }

  input,
  select {
    font-size: 16px !important;
  }

  .bar-cell {
    padding: 8px 9px 9px;
  }

  .bar-chord.split {
    font-size: 10px;
  }

  .seg button {
    padding: 0 7.5px;
  }

  .answer {
    padding: 10px 10px 12px;
  }

  .tr-row {
    gap: 6px;
  }

  .play-btn {
    flex: 1 1 auto;
  }
}
