:root {
  --bg: #f6f5f2; --panel: #ffffff; --text: #1d1d1f; --muted: #6b6b70; --line: #dedcd6;
  --accent: #c2410c; --accent-ink: #ffffff; --cell: #ebe9e3; --cell-beat: #dedbd3;
  --now: #fde68a; --danger: #b91c1c;
  --drum: #c2410c; --bass: #1d4ed8; --chord: #15803d; --melody: #7e22ce;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141416; --panel: #1e1e22; --text: #ececef; --muted: #9a9aa3; --line: #33333a;
    --accent: #fb923c; --accent-ink: #1a1a1a; --cell: #2a2a30; --cell-beat: #35353c;
    --now: #5c5226; --danger: #f87171;
    --drum: #fb923c; --bass: #60a5fa; --chord: #4ade80; --melody: #c084fc;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; padding-bottom: 80px; }
h2 { font-size: 1.1rem; margin: 0 0 10px; }
h3 { font-size: 1rem; margin: 0 0 8px; }
p { margin: 6px 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

.top { position: sticky; top: 0; z-index: 10; background: var(--bg); border-bottom: 1px solid var(--line); padding: 10px 16px 0; }
.topline { display: flex; justify-content: space-between; align-items: center; }
.brand { font-weight: 700; font-size: 1.15rem; }
.net { font-size: .8rem; padding: 2px 10px; border-radius: 999px; border: 1px solid currentColor; }
.net.on { color: var(--chord); } .net.off { color: var(--danger); }
.transport { display: flex; gap: 8px; margin: 10px 0; flex-wrap: wrap; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs button { border: 0; border-bottom: 3px solid transparent; border-radius: 0; background: none; padding: 8px 10px; white-space: nowrap; color: var(--muted); }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

main { padding: 12px 16px; max-width: 1100px; margin: 0 auto; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin-bottom: 12px; }
.card.editor { border-color: var(--accent); }

button, .btn {
  font: inherit; color: var(--text); background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 12px; cursor: pointer; min-height: 40px; display: inline-flex; align-items: center;
}
button:active, .btn:active { transform: translateY(1px); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.danger { color: var(--danger); }
button.toggle.on { background: var(--text); color: var(--bg); }
button:disabled { opacity: .5; cursor: default; }
input[type=text], input[type=number], input[type=url], select {
  font: inherit; color: var(--text); background: var(--bg); border: 1px solid var(--line);
  border-radius: 8px; padding: 7px 9px; min-height: 40px; width: 100%;
}
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 8px; }
.form { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px 12px; }
.form.compact { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.field { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
.field > :not(span:first-child) { color: var(--text); }
.range { display: flex; align-items: center; gap: 8px; min-height: 40px; }
.range input { flex: 1; min-width: 0; accent-color: var(--accent); }
.range output { min-width: 3.5em; text-align: right; font-variant-numeric: tabular-nums; color: var(--text); }
.chip { display: inline-block; padding: 2px 8px; margin: 2px 4px 2px 0; border: 1px solid var(--line); border-radius: 999px; font-size: .85rem; }

.track { border-left: 4px solid var(--c); }
.role-drum { --c: var(--drum); } .role-bass { --c: var(--bass); }
.role-chord { --c: var(--chord); } .role-melody { --c: var(--melody); }
.trackhead { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.trackhead .tname { font-weight: 600; flex: 1; }
.tag { font-size: .8rem; color: var(--c); border: 1px solid var(--c); border-radius: 999px; padding: 2px 10px; white-space: nowrap; }

.gridscroll { overflow-x: auto; margin-top: 10px; padding-bottom: 4px; }
.grid { display: grid; grid-template-columns: 3.4rem repeat(var(--steps), minmax(26px, 1fr)); gap: 3px; min-width: max-content; }
.rowlabel { font-size: .75rem; color: var(--muted); display: flex; align-items: center; position: sticky; left: 0; background: var(--panel); z-index: 1; }
.barlabel { font-size: .75rem; color: var(--muted); border-left: 2px solid var(--line); padding-left: 4px; }
.cell { min-height: 28px; height: 28px; padding: 0; border: 0; border-radius: 4px; background: var(--cell); }
.cell.beat { background: var(--cell-beat); }
.cell.bar { box-shadow: inset 2px 0 0 var(--line); }
.cell.now { outline: 2px solid var(--now); outline-offset: -2px; }
.cell.on { background: var(--c); }

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); }
.list li:first-child { border-top: 0; }
.grow { flex: 1; min-width: 180px; }
.spec { border-left: 4px solid var(--c); padding: 10px 0 10px 12px; margin: 10px 0; }

#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(120px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  max-width: calc(100% - 32px); transition: transform .2s; z-index: 20;
}
#toast.show { transform: translateX(-50%) translateY(0); }
pre#selftest { white-space: pre-wrap; font-size: .85rem; }

/* Hộp thoại: hộp giữa màn hình trên máy lớn, bottom sheet trên điện thoại */
dialog.sheet { border: 0; padding: 0; background: transparent; color: var(--text); width: calc(100% - 32px); max-width: 440px; overflow: visible; }
dialog.sheet::backdrop { background: rgb(0 0 0 / .5); backdrop-filter: blur(3px); animation: sheet-fade .18s ease-out; }
.sheet-body {
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 22px;
  box-shadow: 0 24px 60px rgb(0 0 0 / .35); animation: sheet-pop .22s cubic-bezier(.2, .9, .3, 1.15);
}
.sheet-body h2 { font-size: 1.2rem; margin: 0 0 6px; }
.sheet-body { max-height: 88vh; overflow-y: auto; }
.sheet-body p { margin: 0; line-height: 1.55; }
.sheet-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 22px; }
.sheet-actions button { border-radius: 10px; padding: 10px 16px; }
.sheet-actions button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.danger-fill { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
dialog.sheet.closing .sheet-body { animation: sheet-pop-out .16s ease-in forwards; }
dialog.sheet.closing::backdrop { animation: sheet-fade-out .16s ease-in forwards; }
@media (max-width: 560px) {
  dialog.sheet { width: 100%; max-width: none; margin: auto 0 0; }
  .sheet-body {
    border-radius: 22px 22px 0 0; border-bottom: 0; padding: 22px 20px calc(18px + env(safe-area-inset-bottom));
    animation: sheet-up .26s cubic-bezier(.2, .9, .3, 1);
  }
  .sheet-actions { flex-direction: column-reverse; }
  .sheet-actions button { width: 100%; justify-content: center; min-height: 50px; font-size: 1rem; }
  dialog.sheet.closing .sheet-body { animation: sheet-down .18s ease-in forwards; }
}
@keyframes sheet-fade { from { opacity: 0; } }
@keyframes sheet-fade-out { to { opacity: 0; } }
@keyframes sheet-pop { from { opacity: 0; transform: translateY(10px) scale(.95); } }
@keyframes sheet-pop-out { to { opacity: 0; transform: scale(.96); } }
@keyframes sheet-up { from { transform: translateY(100%); } }
@keyframes sheet-down { to { transform: translateY(100%); } }
@media (prefers-reduced-motion: reduce) {
  .sheet-body, dialog.sheet::backdrop, dialog.sheet.closing .sheet-body { animation: none !important; }
}

/* Khung nghe bài đang soạn */
.player { position: relative; }
.player-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.player-head h2 { margin: 0; }
#pl-time { font-variant-numeric: tabular-nums; }
.progress {
  position: relative; display: grid; grid-template-columns: repeat(var(--bars), 1fr);
  margin: 12px 0 4px; border-radius: 10px; overflow: hidden; background: var(--cell);
}
.progress .seg {
  position: relative; z-index: 1; min-height: 44px; border: 0; border-radius: 0; background: transparent;
  border-left: 1px solid var(--line); justify-content: center; font-size: .8rem; color: var(--muted); padding: 0 2px;
}
.progress .seg:first-child { border-left: 0; }
.progress .seg.cur { color: var(--text); font-weight: 600; }
.progress .fill {
  position: absolute; inset: 0 auto 0 0; width: 0; pointer-events: none;
  background: color-mix(in srgb, var(--accent) 35%, transparent); transition: width .08s linear;
}
#pl-pos { font-variant-numeric: tabular-nums; margin-left: auto; }
button.big { min-width: 110px; justify-content: center; font-size: 1.05rem; min-height: 46px; }

/* Nút icon Nghe / Dừng */
.icon-btn { width: 44px; height: 44px; min-height: 0; padding: 0; justify-content: center; border-radius: 999px; flex: none; }
.icon-btn svg { width: 20px; height: 20px; fill: currentColor; }
.icon-btn.big { width: 56px; height: 56px; min-width: 0; }
.icon-btn.big svg { width: 26px; height: 26px; }
.icon-btn.is-playing:not(.primary) { background: var(--text); color: var(--bg); border-color: var(--text); }

/* Tách nhạc */
.linkrow { display: flex; gap: 8px; }
.linkrow input { flex: 1; min-width: 0; }
.linkrow button { flex: none; }
label.btn.disabled { opacity: .5; pointer-events: none; }
.meter { height: 10px; border-radius: 999px; background: var(--cell); overflow: hidden; margin: 10px 0 4px; }
.meter > div { height: 100%; background: var(--accent); border-radius: 999px; transition: width .2s; }
.sep-row { flex-wrap: nowrap; }
.sep-row .grow { min-width: 0; }
.sep-time { min-width: 6.5em; }

/* Lưu & xuất */
.save-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
.choices { display: grid; gap: 8px; margin-top: 14px; }
.choice { display: flex; gap: 12px; align-items: flex-start; padding: 12px; border: 1px solid var(--line); border-radius: 12px; cursor: pointer; }
.choice:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }
.choice input { margin-top: 3px; accent-color: var(--accent); width: 18px; height: 18px; flex: none; }
.choice > span { display: flex; flex-direction: column; gap: 2px; }
.export-body .field { color: var(--muted); }
