:root {
  --bg: #f4f5f9;
  --surface: #ffffff;
  --surface-2: #eef0f6;
  --surface-3: #e3e6ef;
  --border: #dde1ea;
  --text: #171a23;
  --muted: #646b7d;
  --accent: #6d4dff;
  --accent-soft: rgba(109, 77, 255, 0.12);
  --accent-text: #ffffff;
  --danger: #e5484d;
  --lane: #f8f9fc;
  --lane-alt: #f1f3f8;
  --grid: rgba(20, 25, 40, 0.07);
  --ruler-text: #6b7285;
  --playhead: #ff4d6d;
  --shadow: 0 1px 2px rgba(16, 20, 35, 0.05), 0 4px 16px rgba(16, 20, 35, 0.05);
  --clip-l: 46%;
  --clip-bg-a: 0.22;
  --hw: 190px;
  --lane-h: 84px;
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1016; --surface: #161922; --surface-2: #1d212c; --surface-3: #272c39;
    --border: #2a2f3c; --text: #e8eaf1; --muted: #8d94a7; --accent: #8b70ff;
    --accent-soft: rgba(139, 112, 255, 0.16); --lane: #12151d; --lane-alt: #151821;
    --grid: rgba(255, 255, 255, 0.06); --ruler-text: #8d94a7;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 18px rgba(0, 0, 0, 0.25);
    --clip-l: 62%; --clip-bg-a: 0.2; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1016; --surface: #161922; --surface-2: #1d212c; --surface-3: #272c39;
  --border: #2a2f3c; --text: #e8eaf1; --muted: #8d94a7; --accent: #8b70ff;
  --accent-soft: rgba(139, 112, 255, 0.16); --lane: #12151d; --lane-alt: #151821;
  --grid: rgba(255, 255, 255, 0.06); --ruler-text: #8d94a7;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 18px rgba(0, 0, 0, 0.25);
  --clip-l: 62%; --clip-bg-a: 0.2; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 "Inter", "Kantumruy Pro", system-ui, sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo { width: 36px; height: 36px; }
.logo rect { fill: var(--accent); stroke: none; }
.logo path { stroke: #fff; stroke-width: 2.5; }
.brand h1 { font-size: 17px; margin: 0; letter-spacing: -0.01em; }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 12px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface-2); cursor: pointer; white-space: nowrap; font-size: 13px; font-weight: 500;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--surface-3); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger:hover { color: var(--danger); border-color: var(--danger); }
.btn.icon { width: 34px; padding: 0; }
.btn.wide { width: 100%; height: 40px; margin-top: 4px; }
.btn:focus-visible, .chip:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#btnTheme .i-moon { display: none; }
[data-theme="dark"] #btnTheme .i-sun { display: none; }
[data-theme="dark"] #btnTheme .i-moon { display: block; }

/* ---------- Controls ---------- */
.controls {
  display: grid; gap: 12px; padding: 16px 20px;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
}
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px;
  transition: opacity .2s;
}
.card.disabled { opacity: .5; }
.card.disabled :is(input, select, button) { pointer-events: none; }
.card-title { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.card-title small { color: var(--muted); font-weight: 500; font-size: 12px; margin-left: auto; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: hsl(var(--c) 80% 60%); }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label, .row-between label { font-size: 12px; color: var(--muted); }
.row-between { display: flex; justify-content: space-between; align-items: center; }
output { font-variant-numeric: tabular-nums; font-size: 12px; font-weight: 600; color: var(--accent); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
select, input[type="number"], .name-input {
  height: 34px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2);
  padding: 0 10px; width: 100%; min-width: 0;
}
input[type="range"] { width: 100%; accent-color: var(--accent); margin: 4px 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 999px;
  padding: 3px 10px; font-size: 12px; cursor: pointer;
}
.chip.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.check { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.check.small { font-size: 12px; color: var(--muted); }
.hint { margin: 0; font-size: 12px; color: var(--muted); }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; }
.btn-row .btn { flex: 1; padding: 0 8px; }
.btn-col { display: flex; flex-direction: column; gap: 6px; }
.btn-col .btn { justify-content: flex-start; }

/* ---------- Transport ---------- */
.transport {
  display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 10px;
  padding: 8px 20px; background: var(--surface); border-top: 1px solid var(--border); border-bottom: 1px solid var(--border);
}
.t-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.tools { display: flex; gap: 4px; padding-right: 8px; margin-right: 2px; border-right: 1px solid var(--border); }
.btn.play { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); color: #fff; border-color: var(--accent); }
.btn.play svg { fill: currentColor; stroke: none; }
.btn.play .i-pause { display: none; }
.playing .btn.play .i-play { display: none; }
.playing .btn.play .i-pause { display: block; }
.time { font: 600 15px/1 "Inter", monospace; font-variant-numeric: tabular-nums; padding: 0 6px; }
.time .sep { color: var(--muted); margin: 0 6px; font-weight: 400; }
#timeTotal { color: var(--muted); }
.zoom { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.zoom input { width: 120px; }

/* ---------- Timeline ---------- */
.timeline { position: relative; flex: 1; min-height: 300px; display: flex; background: var(--lane); }
.tl-scroll { position: absolute; inset: 0; overflow: auto; overscroll-behavior: contain; }
.tl-inner { position: relative; min-width: 100%; min-height: 100%; }
.tl-ruler-row { display: flex; position: sticky; top: 0; z-index: 7; height: 30px; background: var(--surface); border-bottom: 1px solid var(--border); }
.tl-corner {
  position: sticky; left: 0; z-index: 2; width: var(--hw); flex-shrink: 0; background: var(--surface);
  border-right: 1px solid var(--border); font-size: 11px; color: var(--muted); padding: 7px 12px;
  text-transform: uppercase; letter-spacing: .06em;
}
#ruler { position: sticky; left: var(--hw); display: block; cursor: pointer; height: 30px; }

.track-row { display: flex; height: var(--lane-h); border-bottom: 1px solid var(--border); }
.track-row:nth-child(even) .lane { background: var(--lane-alt); }
.track-head {
  position: sticky; left: 0; z-index: 6; width: var(--hw); flex-shrink: 0;
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; justify-content: center; gap: 6px; padding: 8px 10px;
}
.track-head::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: hsl(var(--h) 75% 58%); }
.track-name { border: 1px solid transparent; background: transparent; padding: 2px 6px; border-radius: 6px; font-weight: 600; font-size: 13px; width: 100%; }
.track-name:hover, .track-name:focus { border-color: var(--border); background: var(--surface-2); outline: none; }
.track-btns { display: flex; gap: 4px; }
.tbtn {
  width: 26px; height: 22px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface-2);
  font-size: 11px; font-weight: 700; cursor: pointer; display: grid; place-items: center; padding: 0;
}
.tbtn svg { width: 13px; height: 13px; }
.tbtn.on-m { background: #f5a524; border-color: #f5a524; color: #1a1300; }
.tbtn.on-s { background: #2fbf71; border-color: #2fbf71; color: #00170a; }
.tbtn.del { margin-left: auto; }
.tbtn.del:hover { color: var(--danger); border-color: var(--danger); }
.track-row.muted .lane { opacity: .45; }

.lane {
  position: relative; flex: 1; background: var(--lane);
  background-image: linear-gradient(to right, var(--grid) 1px, transparent 1px);
  background-size: var(--grid-step, 60px) 100%;
}
.lane.drop-target { box-shadow: inset 0 0 0 2px var(--accent); }

.clip {
  position: absolute; top: 6px; bottom: 6px; z-index: 1; border-radius: 8px; overflow: hidden;
  background: hsl(var(--h) 75% var(--clip-l) / var(--clip-bg-a));
  border: 1px solid hsl(var(--h) 70% var(--clip-l) / .7);
  --wave: hsl(var(--h) 80% var(--clip-l));
  cursor: grab; touch-action: none; user-select: none;
  display: flex; flex-direction: column;
}
.clip.selected { border-color: var(--text); box-shadow: 0 0 0 1px var(--text); z-index: 2; }
.clip.dragging { cursor: grabbing; opacity: .88; z-index: 3; }
.clip-head {
  height: 18px; flex-shrink: 0; font-size: 11px; font-weight: 600; padding: 1px 8px;
  background: hsl(var(--h) 75% var(--clip-l) / .35); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  display: flex; gap: 6px; align-items: center;
}
.badge { font-size: 10px; padding: 0 5px; border-radius: 4px; background: rgba(0,0,0,.18); }
.clip canvas { flex: 1; width: 100%; min-height: 0; display: block; }
.handle { position: absolute; top: 0; bottom: 0; width: 8px; cursor: ew-resize; z-index: 2; }
.handle.h-l { left: 0; }
.handle.h-r { right: 0; }
.handle::after { content: ""; position: absolute; top: 50%; height: 22px; margin-top: -11px; width: 3px; border-radius: 2px; background: var(--text); opacity: 0; transition: opacity .15s; }
.handle.h-l::after { left: 2px; }
.handle.h-r::after { right: 2px; }
.clip:hover .handle::after, .clip.selected .handle::after { opacity: .55; }

.add-row { display: flex; height: 56px; }
.add-row .track-head { justify-content: center; }
.add-row .lane { display: flex; align-items: center; padding-left: 16px; color: var(--muted); font-size: 12px; background: transparent; }

.playhead { position: absolute; top: 0; bottom: 0; width: 2px; margin-left: -1px; background: var(--playhead); z-index: 5; cursor: ew-resize; touch-action: none; }
/* Wider invisible grab area so the whole line can be dragged. */
.playhead::before { content: ""; position: absolute; top: 0; bottom: 0; left: -6px; width: 14px; }
.playhead:hover, .playhead.dragging { width: 3px; margin-left: -1.5px; box-shadow: 0 0 0 3px color-mix(in srgb, var(--playhead) 22%, transparent); }

.empty {
  position: absolute; inset: 30px 0 0 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; color: var(--muted); text-align: center; padding: 20px; pointer-events: none;
}
.empty svg { width: 44px; height: 44px; margin-bottom: 8px; stroke: var(--accent); }
.empty p { margin: 0; }
.empty b { color: var(--text); }
.drop-hint {
  position: absolute; inset: 8px; border: 2px dashed var(--accent); border-radius: 12px; background: var(--accent-soft);
  display: none; align-items: center; justify-content: center; font-weight: 600; color: var(--accent); pointer-events: none; z-index: 10;
}
.timeline.dragover .drop-hint { display: flex; }
.timeline.dragover.on-lane .drop-hint { display: none; }

.shortcuts {
  display: flex; flex-wrap: wrap; gap: 14px; padding: 8px 20px; font-size: 12px; color: var(--muted);
  background: var(--surface); border-top: 1px solid var(--border);
}
kbd { font: 600 11px "Inter", monospace; background: var(--surface-2); border: 1px solid var(--border); border-bottom-width: 2px; padding: 0 5px; border-radius: 4px; }

/* ---------- Overlays ---------- */
.busy { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: grid; place-items: center; z-index: 100; }
.busy[hidden] { display: none; }
.busy-box { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px 22px; display: flex; gap: 12px; align-items: center; box-shadow: var(--shadow); }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--surface-3); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 13px; z-index: 101;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  :root { --hw: 112px; --lane-h: 76px; }
  .topbar, .controls, .transport, .shortcuts { padding-left: 16px; padding-right: 16px; }
  .btn.primary span { display: none; }
  #btnImport { width: 34px; padding: 0; }
  .zoom input { width: 80px; }
  .track-name { font-size: 12px; padding: 2px 4px; }
  .tbtn { width: 22px; }
  .timeline { min-height: 340px; }
}
