:root {
  --bg: #0d0f1a;
  --panel: #161a2b;
  --panel2: #1e2338;
  --line: #2a3050;
  --text: #e6e8f2;
  --dim: #8a90ad;
  --accent: #39e1c6;
  --accent2: #ff4fa3;
  --warn: #ff9a3c;
  --bgm: #1f5f63;
  --bgm-dim: #1a4346;
  --game: #6b4a2a;
  --cell: #232843;
  --cell-beat: #2b3152;
  --on: #39e1c6;
  --play: #ffe066;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --name-w: 84px;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.35 -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", sans-serif;
  display: flex;
  flex-direction: column;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}
button { font: inherit; color: inherit; background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; touch-action: manipulation; }
button:active { filter: brightness(1.4); }
button.primary { background: var(--accent); color: #062420; border-color: var(--accent); font-weight: 700; }
button.danger { color: #ff7b8a; border-color: #5a2a36; }
button:disabled { opacity: .35; }
select, input[type=text], input[type=number] {
  font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 8px; min-height: 34px; max-width: 100%;
}
input[type=text], input[type=number] { -webkit-user-select: text; user-select: text; }
/* iOS zooms the page into any focused field under 16px and can leave it
   zoomed/scrolled afterwards, so taps land off from what's drawn. */
select, input[type=text], input[type=number] { font-size: 16px; }
[hidden] { display: none !important; }

/* --- top bar ---------------------------------------------------------- */
.topbar {
  display: flex; align-items: center; gap: 6px;
  padding: calc(6px + var(--safe-t)) 8px 6px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.icon-btn { width: 40px; height: 40px; padding: 0; font-size: 20px; flex: none; }
.title-btn { flex: 1; min-width: 0; text-align: left; background: none; border: none; padding: 2px 4px; display: flex; flex-direction: column; }
.title-btn span { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title-btn small { color: var(--dim); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.play-btn { width: 52px; height: 40px; flex: none; font-size: 20px; background: var(--accent); color: #062420; border: none; font-weight: 800; }
.play-btn.playing { background: var(--accent2); color: #fff; }
.dirty-dot { color: var(--warn); }

/* --- views ---------------------------------------------------------- */
#views { flex: 1; overflow: hidden; position: relative; }
.view { position: absolute; inset: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: 24px; }
.tabbar {
  display: flex; background: var(--panel); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.tabbar button { flex: 1; border: none; background: none; border-radius: 0; padding: 6px 0 8px; color: var(--dim); font-size: 11px; display: flex; flex-direction: column; align-items: center; gap: 1px; }
.tabbar button span { font-size: 18px; }
.tabbar button.active { color: var(--accent); }

/* --- sequencer ---------------------------------------------------------- */
.seq-toolbar {
  display: flex; align-items: center; gap: 6px; padding: 8px; position: sticky; top: 0; z-index: 3;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.pages { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.pages button { min-width: 38px; height: 34px; padding: 0 6px; font-weight: 700; color: var(--dim); }
.pages button.active { background: var(--panel2); color: var(--accent); border-color: var(--accent); }
.pages button.playing { box-shadow: inset 0 -3px 0 var(--play); }
.seq-toolbar .zoom { height: 34px; font-size: 12px; }

.grid { padding: 4px 6px 0; }
.row { display: flex; align-items: stretch; gap: 4px; margin-bottom: 4px; }
.row-head { width: var(--name-w); flex: none; display: flex; flex-direction: column; gap: 2px; }
.row-name {
  height: 24px; padding: 0 6px; font-size: 12px; font-weight: 600; text-align: left;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border: none; border-radius: 6px;
  background: var(--bgm);
}
.row-name.unbound { background: var(--bgm-dim); }
.row-name.game { background: var(--game); }
.row-ctl { display: flex; gap: 2px; }
.row-ctl button { flex: 1; height: 22px; padding: 0; font-size: 11px; border-radius: 6px; }
.row-ctl button.on { background: var(--accent2); color: #fff; border-color: var(--accent2); }
.row-ctl button.open { background: var(--accent); color: #062420; border-color: var(--accent); }
.cells { flex: 1; display: grid; gap: 2px; touch-action: pan-y; }
.cell {
  border-radius: 4px; background: var(--cell); position: relative; min-height: 48px; overflow: hidden;
}
.cell.beat { background: var(--cell-beat); }
.cell.on { background: color-mix(in srgb, var(--on) 30%, var(--cell)); }
.cell .glyph { position: absolute; left: 18%; right: 18%; bottom: 3px; background: var(--on); border-radius: 2px; }
.cell .chord { position: absolute; top: 2px; right: 3px; width: 5px; height: 5px; border-radius: 50%; background: var(--accent2); }
.cell.ph { outline: 2px solid var(--play); outline-offset: -2px; }
.cells { --gap-beat: 2px; --gap-bar: 8px; }
.bar-sep { grid-row: 1; justify-self: center; width: 2px; border-radius: 1px; background: var(--dim); opacity: .45; }
.grid.dense .cells { gap: 1px; --gap-beat: 1px; --gap-bar: 6px; }
.row.dimmed .cell.on { background: color-mix(in srgb, var(--on) 14%, var(--cell)); }
.row.dimmed .cell .glyph { opacity: .45; }

.roll { margin: 0 0 8px; padding: 6px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; }
.roll-head { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; flex-wrap: wrap; }
.roll-head .grow { flex: 1; }
.roll-head button { height: 32px; padding: 0 10px; font-size: 12px; }
.lane { display: flex; gap: 4px; margin-bottom: 2px; }
.lane-label { width: var(--name-w); flex: none; height: 26px; padding: 0 6px; font-size: 11px; text-align: right; border-radius: 5px; color: var(--dim); }
.lane-label.tonic { color: var(--accent); font-weight: 700; }
.lane .cells .cell { min-height: 26px; }
.lane .cell.on { background: var(--on); }
.lane .cell.tonic { background: #262c4a; }
.lane .cell.tonic.on { background: var(--on); }
.lane .cell.ph.on { background: var(--play); }

.add-row { margin: 6px 6px 0; width: calc(100% - 12px); height: 44px; border-style: dashed; color: var(--dim); }
.hint { color: var(--dim); font-size: 12px; padding: 8px 12px; }

/* --- forms (song/roles/patch) ------------------------------------------ */
.form { padding: 10px 12px; }
.group { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 12px; }
.group > h3, details > summary { margin: 0 0 8px; font-size: 13px; color: var(--accent); letter-spacing: .04em; }
details > summary { list-style: none; cursor: pointer; margin: 0; padding: 2px 0; display: flex; align-items: center; gap: 6px; }
details > summary::before { content: "▸"; color: var(--dim); }
details[open] > summary::before { content: "▾"; }
details[open] > summary { margin-bottom: 8px; }
details > summary .badge { margin-left: auto; font-size: 11px; color: var(--warn); }
.field { display: grid; grid-template-columns: 96px 1fr 64px; align-items: center; gap: 8px; min-height: 40px; }
.field label { font-size: 12px; color: var(--dim); }
.field .val { font-size: 12px; text-align: right; font-variant-numeric: tabular-nums; background: none; border: none; padding: 4px 0; }
.field input[type=number] { width: 64px; min-height: 30px; padding: 2px 4px; font-size: 16px; }
.field2 { display: grid; grid-template-columns: 96px 1fr; align-items: center; gap: 8px; min-height: 42px; }
.field2 label { font-size: 12px; color: var(--dim); }
input[type=range] { width: 100%; height: 32px; background: none; -webkit-appearance: none; appearance: none; }
input[type=range]::-webkit-slider-runnable-track { height: 6px; background: var(--line); border-radius: 3px; }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 24px; height: 24px; margin-top: -9px; border-radius: 50%; background: var(--accent); border: 2px solid #0b2d29; }
input[type=range]::-moz-range-track { height: 6px; background: var(--line); border-radius: 3px; }
input[type=range]::-moz-range-thumb { width: 22px; height: 22px; border-radius: 50%; background: var(--accent); border: 2px solid #0b2d29; }
.seg { display: flex; gap: 0; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { flex: 1; border: none; border-radius: 0; padding: 8px 2px; font-size: 12px; background: var(--panel2); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.active { background: var(--accent); color: #062420; font-weight: 700; }
.checks { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.chip { border-radius: 16px; padding: 6px 12px; font-size: 12px; }
.chip.on { background: var(--accent); color: #062420; border-color: var(--accent); font-weight: 700; }
.chords { display: grid; grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); gap: 6px; }
.chords label { font-size: 11px; color: var(--dim); display: flex; flex-direction: column; gap: 2px; }
.readout { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; font-size: 13px; margin-top: 8px; }
.readout b { color: var(--accent); font-variant-numeric: tabular-nums; }

.role-row { display: grid; grid-template-columns: 92px 1fr auto; gap: 8px; align-items: center; min-height: 44px; }
.role-row .role-btn { padding: 6px 4px; font-size: 12px; font-weight: 600; }
.role-row select { width: 100%; }
.inherit { display: flex; align-items: center; gap: 4px; font-size: 11px; color: var(--dim); }
.inherit input { width: 20px; height: 20px; }

/* --- sheets ---------------------------------------------------------- */
.sheet { position: fixed; inset: 0; z-index: 20; background: var(--bg); display: flex; flex-direction: column; }
.sheet-head {
  display: flex; align-items: center; gap: 6px; padding: calc(6px + var(--safe-t)) 8px 6px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.sheet-head input[type=text] { flex: 1; min-width: 0; font-weight: 700; }
.sheet-body { flex: 1; overflow-y: auto; padding: 10px 12px calc(24px + var(--safe-b)); }
.keys { display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px; margin-bottom: 12px; }
.keys button { height: 54px; padding: 0; font-size: 11px; display: flex; flex-direction: column; align-items: center; justify-content: center; }
.keys button b { font-size: 14px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.actions button { flex: 1 1 auto; min-height: 40px; }
.wave { width: 100%; height: 110px; background: #0a0c16; border-radius: 8px; border: 1px solid var(--line); touch-action: none; display: block; }
.meter { height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; margin: 8px 0; }
.meter > div { height: 100%; width: 0; background: #3ddc84; transition: width .05s; }
.rec { background: #c2263d; border-color: #c2263d; color: #fff; font-weight: 700; }

.menu-list { display: flex; flex-direction: column; gap: 8px; }
.menu-list button { text-align: left; min-height: 46px; padding: 8px 14px; }
.menu-list h4 { margin: 14px 2px 2px; font-size: 12px; color: var(--dim); font-weight: 600; }
.song-item { display: flex; gap: 6px; align-items: center; }
.song-item .open { flex: 1; text-align: left; min-height: 48px; display: flex; flex-direction: column; }
.song-item .open small { color: var(--dim); font-size: 11px; }
.song-item .open.current { border-color: var(--accent); }
.song-item button.small { width: 44px; min-height: 48px; padding: 0; }

/* --- modal / toast ---------------------------------------------------------- */
.modal { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal-box { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; width: 100%; max-width: 360px; }
.modal-box p { margin: 0 0 14px; white-space: pre-wrap; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.modal-actions button { min-width: 96px; min-height: 42px; }
.toast {
  position: fixed; left: 50%; bottom: calc(76px + var(--safe-b)); transform: translateX(-50%);
  background: var(--panel2); border: 1px solid var(--accent); padding: 8px 16px; border-radius: 20px; z-index: 50;
  font-size: 13px; max-width: calc(100% - 32px); text-align: center;
}

@media (min-width: 700px) {
  :root { --name-w: 120px; }
  .form, .sheet-body { max-width: 720px; margin: 0 auto; width: 100%; }
}
/* piano roll: all touch input is handled in JS (1 finger paints, 2 fingers pan) */
.roll { touch-action: none; }
.roll .cells { touch-action: none; transition: none; will-change: transform; }
.roll .lane { overflow: hidden; }
.roll .lane-label { position: relative; z-index: 1; background: var(--panel2); }
/* piano-roll fullscreen: only the open row, lanes stretched, no tab bar */
body.roll-full .tabbar { display: none; }
.roll .lane-label { height: var(--lane-h, 26px); }
.roll .lane .cells .cell { min-height: var(--lane-h, 26px); }
.roll-head button.on { background: var(--accent); color: #062420; border-color: var(--accent); }
/* page tab whose bar is looping (double-tap) */
.pages button { position: relative; }
.pages button.loop { background: color-mix(in srgb, var(--accent2) 35%, var(--panel2)); color: #fff; }
.pages button.loop::after { content: "⟳"; position: absolute; top: 0; right: 3px; font-size: 10px; line-height: 1.2; }
.pages button.loop.active { border-color: var(--accent); }
/* patch editor knobs (the game's RotaryKnob) */
.knobs { display: grid; grid-template-columns: repeat(auto-fill, minmax(60px, 1fr)); gap: 8px 2px; margin-top: 8px; }
.group > h3 + .knobs, summary + .knobs { margin-top: 0; }
.knob { display: flex; flex-direction: column; align-items: center; min-width: 0; }
.knob label { font-size: 11px; color: #ccdff2; text-align: center; line-height: 1.2; min-height: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.knob-wrap { position: relative; width: 52px; height: 52px; touch-action: none; cursor: ns-resize; }
.knob-dial { width: 100%; height: 100%; display: block; overflow: visible; }
.knob-track { fill: none; stroke: rgba(255,255,255,.15); stroke-width: 3.5; stroke-linecap: round; }
.knob-fill { fill: none; stroke-width: 3.5; stroke-linecap: round; }
.knob-body { fill: #212430; stroke-width: 1.5; stroke-opacity: .6; }
.knob-ptr { stroke: #fff; stroke-width: 3; stroke-linecap: round; }
.knob.drag .knob-body { fill: #2c3040; }
.knob .val { font-size: 12px; color: #99e6ff; font-variant-numeric: tabular-nums; background: none; border: none; padding: 2px 4px; min-height: 22px; }
.knob input[type=number] { width: 64px; min-height: 26px; padding: 2px 4px; font-size: 16px; text-align: center; }
.knob-pop {
  display: none; position: absolute; left: 50%; bottom: calc(100% + 18px); transform: translateX(-50%);
  background: rgba(13,15,23,.94); border: 1px solid; border-radius: 6px; padding: 3px 8px;
  font-size: 13px; font-variant-numeric: tabular-nums; white-space: nowrap; z-index: 5; pointer-events: none;
}
.knob.drag .knob-pop { display: block; }
.group .seg + .knobs { margin-top: 10px; }

/* --- update banner (new service-worker version waiting) ---------------------- */
.update-bar {
  position: fixed; left: 50%; bottom: calc(76px + var(--safe-b)); transform: translateX(-50%); z-index: 45;
  display: flex; align-items: center; gap: 12px; background: var(--panel2); border: 1px solid var(--accent);
  padding: 6px 6px 6px 16px; border-radius: 22px; font-size: 13px; white-space: nowrap;
}
.update-bar button { min-height: 34px; padding: 0 16px; border-radius: 17px; }
