:root {
  color-scheme: dark;
  --bg: #0f1115; --surface: #181b21; --surface-2: #23272f; --line: #323844;
  --text: #f1f3f6; --muted: #a2abb8; --accent: #ffb627; --on-accent: #1a1300;
  --danger: #ff6b6b; --ok: #5ad08a; --focus: #7cc4ff; --dot: #3a404c; --flash: #ffb627;
  --radius: 12px; --tap: 48px;
}
@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f6f7f9; --surface: #ffffff; --surface-2: #eceef2; --line: #d5d9e0;
    --text: #14171c; --muted: #5b6472; --accent: #9a5b00; --on-accent: #ffffff;
    --danger: #c62828; --ok: #1f8a4c; --focus: #1565c0; --dot: #c9ced6;
  }
}
.stage {
  color-scheme: dark;
  --bg: #000; --surface: #111317; --surface-2: #1c1f25; --line: #2a2f38;
  --text: #f5f6f8; --muted: #9aa3ae; --accent: #ffb627; --on-accent: #1a1300; --dot: #2e333c;
  --focus: #7cc4ff; --danger: #ff6b6b;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; background: var(--bg); }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
h1, h2, p { margin: 0; }
h1 { font-size: 1.3rem; }
h2 { font-size: 1.1rem; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
svg { width: 1.2em; height: 1.2em; flex: none; }
.muted { color: var(--muted); }
.small { font-size: .9rem; }

.top, .bar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}
.head {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: 4px 16px;
}
.head h1 { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar .head { padding-left: 8px; }
.tabs {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  padding: 0 16px;
}
.tabs button {
  flex: 1;
  min-height: var(--tap);
  background: none;
  border: 0;
  border-bottom: 3px solid transparent;
  color: var(--muted);
  font-weight: 600;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.page {
  max-width: 640px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px calc(16px + env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}
.page h2 { margin-top: 12px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: var(--tap);
  padding: 0 18px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface-2);
  font-weight: 600;
  text-align: center;
}
.btn.primary, .btn.on { background: var(--accent); color: var(--on-accent); border-color: transparent; }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn.small { min-height: 40px; padding: 0 14px; font-size: .95rem; }
a.btn { text-decoration: none; }
a.btn:not(.primary):not(.on) { color: inherit; }
.page a:not(.btn) { color: var(--focus); }
.prose { display: flex; flex-direction: column; gap: 12px; }
.prose ul, .prose ol { margin: 0; padding-left: 1.3em; display: flex; flex-direction: column; gap: 6px; }
.btn:disabled, .icon:disabled { opacity: .4; cursor: default; }
.wide { width: 100%; }
.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: var(--tap);
  min-height: var(--tap);
  padding: 0 8px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  font-size: 1.2rem;
  font-weight: 600;
}
.back { font-size: 1rem; gap: 4px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }
.actions .btn { flex: 1 1 auto; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span, .field > label { color: var(--muted); font-size: .9rem; font-weight: 600; }
input, select, textarea {
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
}
textarea { resize: vertical; line-height: 1.4; }
input[type="range"] { padding: 0; border: 0; background: none; accent-color: var(--accent); }
input[type="checkbox"] { width: 24px; height: 24px; min-height: 0; margin: 0; flex: none; accent-color: var(--accent); }
.check { display: flex; align-items: center; gap: 12px; min-height: var(--tap); }
.inline { display: flex; gap: 8px; }
.inline input { flex: 1; min-width: 0; }
.tap { min-width: 120px; font-variant-numeric: tabular-nums; }
.err { color: var(--danger); font-size: .9rem; }
.hint { color: var(--muted); font-size: .9rem; }
.clickrow { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.clickrow .btn { min-width: 140px; }

.rows { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.row {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.grip { display: flex; align-items: center; align-self: stretch; padding: 0 8px; color: var(--muted); font-size: 1.4rem; cursor: grab; touch-action: none; user-select: none; -webkit-user-select: none; }
.rows .row { transition: transform .15s ease; }
.rows .row.dragging { transition: none; position: relative; z-index: 2; box-shadow: 0 8px 24px rgb(0 0 0 / .45); border-color: var(--accent); cursor: grabbing; }
.row-main, .pick {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: var(--tap);
  padding: 6px 10px;
  background: none;
  border: 0;
  border-radius: 8px;
  text-align: left;
}
.pick { flex-direction: row; justify-content: flex-start; align-items: center; gap: 12px; cursor: pointer; }
.row-text { display: flex; flex-direction: column; min-width: 0; }
.t { font-weight: 600; overflow-wrap: anywhere; }
.num { color: var(--muted); font-variant-numeric: tabular-nums; }
.meta {
  color: var(--muted);
  font-size: .9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
}
.ctl { display: flex; flex: none; }
.ctl .icon { min-width: 44px; min-height: 44px; width: 44px; height: 44px; padding: 0; }
.ctl .icon[data-act="remove"] { color: var(--danger); }
.stage-btn { flex: none; padding: 0 14px; }
.tools { display: flex; gap: 8px; }
.tools input { flex: 1; min-width: 0; }
.empty { color: var(--muted); text-align: center; padding: 24px 8px; }
.sticky {
  position: sticky;
  bottom: 0;
  padding: 12px 0 calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--bg) 30%);
}
.danger-zone { display: flex; flex-direction: column; gap: 12px; margin-top: 24px; }

.card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
}
.banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  padding: 10px 14px;
  background: var(--surface-2);
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
}
.banner > span { flex: 1 1 200px; }
.banner.error { border-left-color: var(--danger); }
#toast {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  z-index: 50;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgb(0 0 0 / .35);
  opacity: 0;
  transform: translateY(16px);
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: none; pointer-events: auto; }
#toast.error { border-color: var(--danger); border-left-width: 6px; }
#toast p { flex: 1; }
#toast .icon { min-width: 40px; min-height: 40px; }
.stage-on #toast { bottom: calc(96px + env(safe-area-inset-bottom)); }
.stage-on #toast.error { top: calc(56px + env(safe-area-inset-top)); bottom: auto; }

.dots { display: flex; align-items: center; gap: 16px; }
.dot { width: 28px; height: 28px; border-radius: 50%; background: var(--dot); flex: none; }
.dot.one { width: 36px; height: 36px; }
.dot.on { background: var(--accent); }
.clickrow .dots { gap: 10px; }
.clickrow .dot { width: 18px; height: 18px; }
.clickrow .dot.one { width: 24px; height: 24px; }

html.stage-on, html.stage-on body { overflow: hidden; overscroll-behavior: none; background: #000; }
.stage {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.st-head {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 48px;
  padding: 0 12px;
  color: var(--muted);
  font-size: .95rem;
}
.st-head .btn { min-height: 40px; padding: 0 14px; }
.st-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.st-pos { font-variant-numeric: tabular-nums; }
.st-main {
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  padding: 4px 16px 8px;
  overflow: hidden;
  touch-action: pan-y;
}
.st-title {
  font-size: clamp(1.5rem, 7vw, 2.6rem);
  font-weight: 700;
  line-height: 1.15;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.st-nums { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 16px; }
.cell { display: flex; flex-direction: column; min-width: 0; }
.lab {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  color: var(--muted);
  font-size: .85rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.big {
  font-size: min(22vw, 20vh);
  font-weight: 800;
  line-height: 1.05;
  letter-spacing: -.04em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  min-height: 1.05em;
}
.big.long { font-size: min(11vw, 13vh); line-height: calc(min(22vw, 20vh) * 1.05); }
.big.xlong { font-size: min(7vw, 9vh); line-height: calc(min(22vw, 20vh) * 1.05); }
.big.none { font-size: 2rem; letter-spacing: 0; line-height: calc(min(22vw, 20vh) * 1.05); color: var(--muted); }
.st-beat { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 40px; }
.st-count { color: var(--accent); font-weight: 700; font-size: 1.2rem; }
.st-msg { display: flex; flex-direction: column; gap: 6px; }
.st-msg:empty { display: none; }
.st-msg p { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: .95rem; }
.st-msg p span { flex: 1; }
.st-notes {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font-size: 1.25rem;
  line-height: 1.4;
}
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, "DejaVu Sans Mono", monospace; font-size: .9rem; }
.tr { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.tr .btn { min-width: 48px; }
.tr span { color: var(--muted); font-variant-numeric: tabular-nums; }
.st-sheet { margin: 0 0 12px; font-family: ui-monospace, "Cascadia Mono", Consolas, "DejaVu Sans Mono", monospace; line-height: 1.35; white-space: pre; overflow-x: auto; color: var(--text); }
.st-sheet .ch { color: var(--accent); font-weight: 700; }
.st-sheet .sec { color: var(--muted); font-weight: 700; }
.flabel { display: block; margin-bottom: 6px; color: var(--muted); font-size: .9rem; font-weight: 600; }
.charts { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; margin-bottom: 10px; }
.charts figure { position: relative; margin: 0; border-radius: 8px; overflow: hidden; background: #fff; }
.charts img { display: block; width: 100%; height: 140px; object-fit: cover; object-position: top; }
.charts .icon { position: absolute; top: 4px; right: 4px; background: rgb(0 0 0 / .65); color: #fff; }
.st-meta { color: var(--muted); font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.has-charts .st-nums { display: none; }
.st-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.st-tools .st-meta { flex: 1; min-width: 0; }
.capo { display: inline-block; margin-left: 10px; padding: 1px 10px; border-radius: 999px; background: var(--accent); color: #000; font-weight: 800; letter-spacing: .02em; }
.lab .capo { margin-left: 4px; font-size: .8rem; text-transform: none; letter-spacing: 0; }
.capo-ctl { align-items: center; }
.capo-ctl .lbl { color: var(--muted); font-size: .9rem; }
.st-tools .grp { display: flex; gap: 6px; }
.st-tools .btn { min-height: 40px; min-width: 48px; padding: 0 10px; font-size: 1.05rem; }
.st-tools .btn.sel { border: 2px solid var(--accent); color: var(--accent); }
.st-charts img { margin-left: auto; margin-right: auto; }
.has-charts.no-bpm .st-beat { display: none; }
.st-charts { flex: 1; min-height: 0; overflow-y: auto; touch-action: pan-y; overscroll-behavior: contain; margin: 0 -16px; padding: 0 16px; }
.st-charts img { display: block; width: 100%; height: auto; margin-bottom: 10px; border-radius: 6px; -webkit-user-drag: none; pointer-events: none; }
.st-ink-wrap { position: relative; }
.st-ink { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.inking .st-ink { pointer-events: auto; touch-action: none; cursor: crosshair; }
.inking .st-charts { touch-action: none; overflow: hidden; }
.st-inkbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.st-inkbar[hidden] { display: none; }
.st-inkbar .btn { min-height: 40px; padding: 0 12px; }
.st-inkbar .hint { color: var(--muted); font-size: .85rem; }
.swatch { width: 36px; height: 36px; border-radius: 50%; border: 2px solid var(--line); padding: 0; }
.swatch.sel { border: 3px solid var(--text); }
.st-cnotes { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 1.15rem; line-height: 1.4; margin-bottom: 10px; }
.st-next { font-size: 1.1rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.st-next strong { color: var(--text); }
.st-bar {
  display: grid;
  grid-template-columns: 1fr 1.4fr 1fr;
  gap: 10px;
  height: 88px;
  padding: 8px 12px;
}
.st-bar .btn { height: 100%; padding: 0 8px; font-size: 1.2rem; }
.st-bar .start { font-size: 1.5rem; }
.flash {
  position: fixed;
  inset: 0;
  z-index: 5;
  background: var(--flash);
  opacity: 0;
  pointer-events: none;
}
.flash.go { animation: flash 180ms ease-out; }
@keyframes flash { from { opacity: .45; } to { opacity: 0; } }
.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgb(0 0 0 / .75);
}
.overlay .card { max-width: 420px; }

@media (orientation: landscape) and (max-height: 540px) {
  .st-main {
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
    grid-template-rows: auto auto 1fr auto;
    grid-template-areas: "nums title" "nums msg" "beat notes" "beat next";
    column-gap: 24px;
  }
  .st-title { grid-area: title; }
  .st-nums { grid-area: nums; align-self: center; }
  .st-beat { grid-area: beat; align-self: start; }
  .st-msg { grid-area: msg; }
  .st-notes, .st-charts { grid-area: notes; }
  .st-next { grid-area: next; }
  .big { font-size: min(11vw, 30vh); }
  .big.long { font-size: min(6vw, 20vh); line-height: calc(min(11vw, 30vh) * 1.05); }
  .big.xlong { font-size: min(4vw, 14vh); line-height: calc(min(11vw, 30vh) * 1.05); }
  .big.none { line-height: calc(min(11vw, 30vh) * 1.05); }
  .st-bar { height: 68px; padding: 6px 12px; }
  .stage-on #toast:not(.error) { left: 52%; right: calc(12px + env(safe-area-inset-right)); bottom: calc(76px + env(safe-area-inset-bottom)); margin: 0; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
