:root {
  --bg: #0b0d17;
  --bg2: #11142200;
  --panel: #141829;
  --panel2: #1a1f35;
  --panel3: #232946;
  --line: #2a3050;
  --line2: #363d63;
  --text: #e8ebff;
  --muted: #9aa1c7;
  --dim: #6a7199;
  --accent: #39d0ff;
  --accent2: #8b6cff;
  --accent3: #ff5fa8;
  --good: #3ee6a0;
  --warn: #ffc24b;
  --bad: #ff5a6e;
  --grad: linear-gradient(135deg, #39d0ff 0%, #8b6cff 55%, #ff5fa8 100%);
  --hdrW: 210px;
  --rowH: 72px;
  --rulerH: 34px;
  --radius: 10px;
  --font: "Noto Sans JP", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 13px; overflow: hidden; }
body { -webkit-tap-highlight-color: transparent; }
button, input, select { font: inherit; color: inherit; }
#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

/* ---------- 上のバー ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px; flex-wrap: wrap;
  background: linear-gradient(180deg, #151a2e, #10131f); border-bottom: 1px solid var(--line);
  position: relative; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; user-select: none; }
.logo { width: 34px; height: 34px; }
.brand-text { display: flex; flex-direction: column; line-height: 1; }
.brand-text b { font-size: 15px; letter-spacing: .5px; }
.brand-text span { font-size: 11px; letter-spacing: 2px; background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; font-weight: 700; text-transform: uppercase; margin-top: 3px; }
.menus { display: flex; gap: 2px; }
.proj-name { background: transparent; border: 1px solid transparent; border-radius: 8px; padding: 6px 8px; width: 190px; font-weight: 600; }
.proj-name:hover, .proj-name:focus { border-color: var(--line2); background: var(--panel); outline: none; }
.transport { display: flex; gap: 4px; padding: 3px; background: #0a0c15; border-radius: 12px; border: 1px solid var(--line); }
.tbtn { width: 34px; height: 34px; border-radius: 9px; border: 0; background: transparent; display: grid; place-items: center; cursor: pointer; color: var(--muted); }
.tbtn svg { width: 18px; height: 18px; }
.tbtn:hover { background: var(--panel3); color: var(--text); }
.tbtn.play { color: var(--good); }
.tbtn.play.on { background: rgba(62, 230, 160, .15); }
.tbtn.rec { color: var(--bad); }
.tbtn.rec.on { background: var(--bad); color: #fff; animation: blink 1s infinite; }
.tbtn.toggle.on { color: var(--accent); background: rgba(57, 208, 255, .14); }
@keyframes blink { 50% { opacity: .55; } }
.lcd { background: #070910; border: 1px solid var(--line); border-radius: 10px; padding: 4px 12px; font-family: "Consolas", "SF Mono", monospace; min-width: 120px; text-align: center; }
.lcd-pos { font-size: 16px; color: var(--accent); letter-spacing: 1px; }
.lcd-time { font-size: 11px; color: var(--muted); }
.songprops { display: flex; gap: 8px; align-items: center; }
.field { display: flex; flex-direction: column; font-size: 10px; color: var(--dim); gap: 2px; }
.field input, .field select, select.sel, input.inp {
  background: var(--panel); border: 1px solid var(--line); border-radius: 7px; padding: 4px 6px; color: var(--text); font-size: 12px;
}
.field input[type=number] { width: 62px; }
.undo { display: flex; gap: 2px; }
.save-status { font-size: 11px; color: var(--dim); margin-left: auto; white-space: nowrap; }
.save-status.dirty { color: var(--warn); }

.btn { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 6px 11px; cursor: pointer; white-space: nowrap; font-size: 12px; }
.btn:hover { background: var(--panel3); border-color: var(--line2); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--panel2); }
.btn.primary { background: var(--grad); border: 0; color: #fff; font-weight: 700; text-shadow: 0 1px 2px rgba(0,0,0,.25); }
.btn.primary:hover { filter: brightness(1.1); }
.btn.icon { padding: 6px 9px; min-width: 32px; }
.btn.on { border-color: var(--accent); color: var(--accent); background: rgba(57, 208, 255, .1); }
.btn.danger { color: var(--bad); }
.sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.grow { flex: 1; }

/* ---------- 作業エリア ---------- */
.workspace { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.arranger { flex: 1 1 50%; min-height: 120px; display: flex; flex-direction: column; background: var(--bg); }
.arr-toolbar { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
.arr-scroll { flex: 1; overflow: auto; position: relative; overscroll-behavior: contain; }
.arr-inner { position: relative; min-height: 100%; }
.ruler-row { position: sticky; top: 0; z-index: 6; height: var(--rulerH); display: flex; background: #0e1120; border-bottom: 1px solid var(--line); }
.corner { position: sticky; left: 0; z-index: 7; width: var(--hdrW); min-width: var(--hdrW); background: #0e1120; border-right: 1px solid var(--line); display: flex; align-items: center; padding: 0 10px; color: var(--dim); font-size: 11px; }
.ruler { position: sticky; left: var(--hdrW); display: block; height: var(--rulerH); cursor: pointer; touch-action: none; }
.rows { position: relative; }
.row { display: flex; height: var(--rowH); border-bottom: 1px solid #1c2038; }
.th {
  position: sticky; left: 0; z-index: 5; width: var(--hdrW); min-width: var(--hdrW); background: var(--panel);
  border-right: 1px solid var(--line); display: flex; gap: 6px; padding: 6px 8px 6px 0; cursor: pointer;
}
.row.sel .th { background: var(--panel2); }
.th .color { width: 5px; border-radius: 0 4px 4px 0; flex: none; }
.th .body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.th .body > * { flex: none; }
.playhead { left: 0; }
.th .name { line-height: 17px; min-height: 18px; font-weight: 600; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 1px 2px; border-radius: 4px; }
.th .name[contenteditable=true] { background: var(--bg); outline: 1px solid var(--accent); }
.th .line { display: flex; gap: 4px; align-items: center; }
.th select { background: var(--bg); border: 1px solid var(--line); border-radius: 6px; font-size: 11px; padding: 1px 3px; max-width: 120px; color: var(--muted); }
.ms { width: 22px; height: 20px; border-radius: 5px; border: 1px solid var(--line); background: var(--bg); font-size: 10px; font-weight: 700; cursor: pointer; color: var(--muted); padding: 0; }
.ms.m.on { background: var(--warn); color: #111; border-color: var(--warn); }
.ms.s.on { background: var(--accent); color: #111; border-color: var(--accent); }
.ms.x:hover { color: var(--bad); border-color: var(--bad); }
.th input[type=range] { width: 100%; height: 14px; accent-color: var(--accent); }
.th .meter { width: 4px; background: #0a0c15; border-radius: 2px; position: relative; overflow: hidden; flex: none; }
.th .meter i { position: absolute; bottom: 0; left: 0; right: 0; background: linear-gradient(0deg, var(--good), var(--warn) 80%, var(--bad)); height: 0; }

.lane { position: relative; flex: 1; touch-action: pan-x pan-y; }
.lane.drop { background-color: rgba(57, 208, 255, .06); }
.clip {
  position: absolute; top: 4px; bottom: 4px; border-radius: 7px; overflow: hidden; cursor: grab;
  background: color-mix(in srgb, var(--c) 22%, #12152a); border: 1px solid color-mix(in srgb, var(--c) 70%, transparent);
  touch-action: none; user-select: none;
}
.clip.sel { box-shadow: 0 0 0 2px #fff, 0 4px 14px rgba(0,0,0,.4); z-index: 2; }
.clip .cname { position: absolute; left: 6px; top: 2px; font-size: 10px; font-weight: 700; color: color-mix(in srgb, var(--c) 40%, #fff); pointer-events: none; white-space: nowrap; text-shadow: 0 1px 2px #000; z-index: 1; }
.clip canvas { position: absolute; left: 0; top: 0; width: 100%; height: 100%; pointer-events: none; }
.clip .edge { position: absolute; top: 0; bottom: 0; width: 8px; cursor: ew-resize; z-index: 2; }
.clip .edge.l { left: 0; } .clip .edge.r { right: 0; }
.clip .edge:hover { background: rgba(255,255,255,.18); }
.playhead { position: absolute; top: 0; bottom: 0; width: 2px; background: #fff; pointer-events: none; z-index: 8; box-shadow: 0 0 8px rgba(255,255,255,.6); }
.empty-hint { position: absolute; left: calc(var(--hdrW) + 30px); top: 70px; color: var(--dim); font-size: 13px; pointer-events: none; max-width: 520px; line-height: 1.7; }

.splitter { height: 7px; background: var(--panel); border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); cursor: ns-resize; touch-action: none; position: relative; }
.splitter::after { content: ""; position: absolute; left: 50%; top: 2px; width: 40px; height: 2px; margin-left: -20px; background: var(--line2); border-radius: 2px; }

.bottom { flex: 0 0 46%; min-height: 160px; display: flex; flex-direction: column; background: var(--panel); }
.bottom.max { flex-basis: 85%; }
.tabs { display: flex; gap: 2px; padding: 4px 8px 0; border-bottom: 1px solid var(--line); background: #0f1221; align-items: flex-end; }
.tab { border: 0; background: transparent; padding: 8px 14px; border-radius: 8px 8px 0 0; cursor: pointer; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--panel); color: var(--text); box-shadow: inset 0 2px 0 var(--accent); }
.panel { display: none; flex: 1; min-height: 0; flex-direction: column; }
.panel.active { display: flex; }
.ptool { display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--line); flex-wrap: wrap; background: var(--panel); }
.ptool label { color: var(--dim); font-size: 11px; display: flex; align-items: center; gap: 4px; }
.ptool .title { font-weight: 700; margin-right: 6px; display: flex; align-items: center; gap: 6px; }
.ptool .title i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.placeholder { margin: auto; color: var(--dim); text-align: center; line-height: 1.8; padding: 20px; }

/* ピアノロール */
.pr-wrap { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.pr-scroll { flex: 1; overflow: auto; position: relative; overscroll-behavior: contain; }
.pr-scroll canvas { position: sticky; left: 0; top: 0; display: block; touch-action: none; }
.pr-vel { height: 78px; border-top: 1px solid var(--line); position: relative; flex: none; }
.pr-vel canvas { display: block; touch-action: none; }

/* ドラム */
.drum-wrap { flex: 1; overflow: auto; padding: 8px 10px; }
.drum-grid { display: grid; gap: 3px; align-items: center; }
.drum-grid .dl { position: sticky; left: 0; background: var(--panel); z-index: 1; font-size: 11px; color: var(--muted); padding-right: 8px; cursor: pointer; white-space: nowrap; text-align: right; }
.drum-grid .dl:hover { color: var(--text); }
.step { height: 26px; min-width: 22px; border-radius: 5px; background: #1c2140; border: 1px solid #262c50; cursor: pointer; padding: 0; }
.step.beat { background: #232a50; }
.step.on { background: var(--c, var(--accent3)); border-color: transparent; box-shadow: 0 0 8px color-mix(in srgb, var(--c, var(--accent3)) 60%, transparent); }
.step.now { outline: 2px solid #fff; }
.drum-head { display: contents; }
.drum-head span { font-size: 10px; color: var(--dim); text-align: center; }

/* ミキサー */
.mixer { flex: 1; display: flex; min-height: 0; }
.strips { flex: 1; display: flex; gap: 6px; overflow-x: auto; padding: 10px; }
.strip { width: 92px; min-width: 92px; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; display: flex; flex-direction: column; align-items: center; padding: 8px 6px; gap: 6px; cursor: pointer; }
.strip.sel { border-color: var(--accent); }
.strip.master { background: linear-gradient(180deg, #232046, #1a1f35); border-color: var(--accent2); }
.strip .sname { font-size: 11px; font-weight: 700; width: 100%; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; border-bottom: 3px solid var(--c, var(--accent)); padding-bottom: 4px; }
.strip .fader-wrap { flex: 1; display: flex; gap: 6px; align-items: stretch; min-height: 90px; }
.strip input.vfader { writing-mode: vertical-lr; direction: rtl; width: 22px; height: 100%; accent-color: var(--accent); }
.strip .vmeter { width: 8px; background: #0a0c15; border-radius: 3px; position: relative; overflow: hidden; }
.strip .vmeter i { position: absolute; bottom: 0; left: 0; right: 0; height: 0; background: linear-gradient(0deg, var(--good), var(--warn) 78%, var(--bad)); }
.strip .db { font-size: 10px; color: var(--muted); font-family: Consolas, monospace; }
.strip input.pan { width: 74px; accent-color: var(--accent2); }
.strip .row2 { display: flex; gap: 4px; }
.fxpanel { width: 320px; border-left: 1px solid var(--line); overflow-y: auto; padding: 12px; background: var(--panel); }
.fxpanel h3 { margin: 0 0 8px; font-size: 13px; }
.fxbox { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 10px; }
.fxbox h4 { margin: 0 0 8px; font-size: 12px; color: var(--accent); display: flex; justify-content: space-between; align-items: center; }
.knob { display: grid; grid-template-columns: 78px 1fr 48px; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); margin: 4px 0; }
.knob input { width: 100%; accent-color: var(--accent); }
.knob output { font-family: Consolas, monospace; text-align: right; color: var(--text); }

/* 楽譜 */
.score-wrap { flex: 1; overflow: auto; background: #e9eaf2; position: relative; }
.score-wrap .sheet { background: #fff; color: #111; margin: 14px auto; box-shadow: 0 4px 24px rgba(0,0,0,.35); border-radius: 3px; width: min(1100px, calc(100% - 20px)); padding: 10px 10px 24px; }
.score-wrap svg text { font-family: var(--font); }
.sheet .ev { cursor: pointer; }
.sheet .ev:hover rect.hit { fill: rgba(57, 140, 255, .14); }
.sheet .ev.selected rect.hit { fill: rgba(255, 95, 168, .2); stroke: #ff5fa8; }
.sheet .ev.edited text.lbl { fill: #c2185b; }
.score-osmd { min-height: 200px; }
.score-osmd svg { max-width: 100%; }
.popover {
  position: fixed; z-index: 60; background: var(--panel2); border: 1px solid var(--line2); border-radius: 12px; padding: 10px; box-shadow: 0 10px 40px rgba(0,0,0,.55); width: 290px; color: var(--text);
}
.popover h4 { margin: 0 0 8px; font-size: 13px; display: flex; justify-content: space-between; }
.popover .grid12 { display: grid; grid-template-columns: repeat(6, 1fr); gap: 3px; margin-bottom: 8px; }
.popover .grid12 button { padding: 5px 0; font-size: 11px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; }
.popover .grid12 button.on { background: var(--accent); color: #111; border-color: var(--accent); }
.popover .prow { display: flex; gap: 4px; align-items: center; margin: 6px 0; flex-wrap: wrap; }
.popover .prow > span { font-size: 11px; color: var(--muted); width: 56px; }
.popover .chordsel { display: flex; gap: 3px; flex-wrap: wrap; margin-bottom: 6px; }
.popover .chordsel button { font-size: 11px; padding: 3px 7px; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); cursor: pointer; }
.popover .chordsel button.on { border-color: var(--accent3); color: var(--accent3); }

/* メニュー・モーダル */
.menu { position: fixed; z-index: 50; background: var(--panel2); border: 1px solid var(--line2); border-radius: 10px; padding: 5px; min-width: 230px; box-shadow: 0 12px 40px rgba(0,0,0,.5); }
.menu button { display: flex; width: 100%; justify-content: space-between; gap: 16px; text-align: left; background: transparent; border: 0; padding: 7px 10px; border-radius: 7px; cursor: pointer; font-size: 12px; }
.menu button:hover { background: var(--panel3); }
.menu button kbd { color: var(--dim); font-family: inherit; font-size: 11px; }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.menu .mgroup { font-size: 10px; color: var(--dim); padding: 6px 10px 2px; }
.modal-bg { position: fixed; inset: 0; background: rgba(4, 6, 14, .7); z-index: 70; display: grid; place-items: center; padding: 16px; backdrop-filter: blur(3px); }
.modal { background: var(--panel); border: 1px solid var(--line2); border-radius: 16px; width: min(640px, 100%); max-height: 88vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.6); }
.modal header { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal header h2 { margin: 0; font-size: 16px; }
.modal .mbody { padding: 16px 18px; line-height: 1.7; }
.modal .mfoot { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.plist { display: flex; flex-direction: column; gap: 6px; }
.pitem { display: flex; align-items: center; gap: 10px; background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; }
.pitem.cur { border-color: var(--accent); }
.pitem .pinfo { flex: 1; min-width: 0; cursor: pointer; }
.pitem .pinfo b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pitem .pinfo small { color: var(--dim); }
.tpl { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; margin-bottom: 14px; }
.tpl button { background: var(--panel2); border: 1px solid var(--line); border-radius: 12px; padding: 12px; text-align: left; cursor: pointer; }
.tpl button:hover { border-color: var(--accent); }
.tpl button b { display: block; margin-bottom: 3px; }
.tpl button small { color: var(--dim); }
.help h3 { margin: 14px 0 4px; font-size: 13px; color: var(--accent); }
.help p, .help li { color: var(--muted); font-size: 12px; }
.help kbd { background: var(--panel3); border-radius: 4px; padding: 1px 5px; font-size: 11px; color: var(--text); }

.toast { position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%) translateY(20px); background: var(--panel3); border: 1px solid var(--line2); padding: 10px 16px; border-radius: 12px; opacity: 0; pointer-events: none; transition: .25s; z-index: 90; max-width: 90vw; box-shadow: 0 10px 30px rgba(0,0,0,.4); }
.toast.show { opacity: 1; transform: translateX(-50%); }
.busy { position: fixed; inset: 0; background: rgba(4,6,14,.65); display: none; place-items: center; z-index: 95; }
.busy.show { display: grid; }
.busy-box { background: var(--panel); padding: 22px 30px; border-radius: 14px; border: 1px solid var(--line2); display: flex; gap: 14px; align-items: center; }
.spinner { width: 24px; height: 24px; border-radius: 50%; border: 3px solid var(--line2); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#printArea { display: none; }
@media print {
  @page { size: A4; margin: 10mm; }
  html, body { overflow: visible !important; height: auto !important; background: #fff !important; }
  #app, #menuLayer, #modalLayer, #toast, #busy { display: none !important; }
  #printArea { display: block !important; }
  #printArea .page { page-break-after: always; }
  #printArea svg, #printArea img { width: 100%; height: auto; }
}

/* ---------- タブレット・スマホ ---------- */
@media (max-width: 1100px) {
  .songprops .field select { max-width: 84px; }
  .save-status { display: none; }
}
@media (max-width: 820px) {
  :root { --hdrW: 132px; --rowH: 64px; }
  .topbar { gap: 6px; padding: 6px 8px; }
  .brand-text { display: none; }
  .proj-name { width: 120px; order: 2; }
  .menus { order: 1; }
  .transport { order: 3; }
  .lcd { order: 4; min-width: 92px; padding: 3px 8px; }
  .lcd-pos { font-size: 13px; }
  .songprops { order: 5; }
  .undo { order: 6; }
  .th select { max-width: 70px; }
  .th input[type=range] { display: none; }
  .fxpanel { width: 100%; border-left: 0; border-top: 1px solid var(--line); }
  .mixer { flex-direction: column; overflow-y: auto; }
  .strips { flex: none; }
  .strip { height: 260px; }
  .tab { padding: 8px 9px; font-size: 12px; }
  .arr-toolbar .btn { padding: 5px 8px; }
  .ptool, .arr-toolbar { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: thin; }
  .ptool > *, .arr-toolbar > * { flex: none; }
  .ptool .grow, .arr-toolbar .grow { flex: 1 0 8px; }
}
@media (max-width: 480px) {
  .tbtn { width: 30px; height: 30px; }
  .songprops .field { font-size: 9px; }
  .field input[type=number] { width: 52px; }
  .bottom { flex-basis: 52%; }
}
