:root {
  --bg: #15171c;
  --panel: #1d2027;
  --panel2: #262a33;
  --line: #2f3440;
  --text: #e6e8ee;
  --muted: #8a91a0;
  --accent: #4f9dff;
  --rec: #ff4d5a;
  --lane-h: 84px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  font-size: 13px;
  user-select: none;
}

button {
  background: var(--panel2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
  font: inherit;
  touch-action: manipulation;
}
button:hover:not(:disabled) { border-color: #556070; }
button:disabled { opacity: .4; cursor: default; }

input:not([type]), input[type=number] {
  background: #111318;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 4px 6px;
  font: inherit;
  user-select: text;
}

/* ===== トップバー ===== */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 16px; letter-spacing: .5px; }
.transport { display: flex; align-items: center; gap: 6px; }
.transport button { font-size: 15px; width: 38px; height: 32px; padding: 0; }
#btn-play.playing { background: var(--accent); border-color: var(--accent); }
.time {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 18px;
  background: #0e1014;
  padding: 4px 10px;
  border-radius: 6px;
  min-width: 120px;
  text-align: center;
}
.rec-indicator { color: var(--rec); font-weight: 700; animation: blink 1s infinite; }
.tools { display: flex; align-items: center; gap: 10px; margin-left: auto; flex-wrap: wrap; }
.tools label { display: flex; align-items: center; gap: 6px; color: var(--muted); }
#latency { width: 64px; }

/* ===== パネル ===== */
.panel {
  display: flex;
  gap: 16px;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.yt-box {
  position: relative;
  width: 288px;
  height: 162px;
  flex: none;
  background: #000;
  border-radius: 6px;
  overflow: hidden;
}
#yt-player { position: absolute; inset: 0; width: 100%; height: 100%; }
.yt-empty {
  position: absolute;
  background: #000;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}
.help-wrap { color: var(--muted); font-size: 12px; }
.help-wrap summary { cursor: pointer; margin-bottom: 4px; }
.help { margin: 0; padding-left: 18px; line-height: 1.8; }
.help b { color: var(--text); }
.latency-group { display: flex; align-items: center; gap: 6px; }
.undo-group { display: flex; gap: 4px; }
.undo-group button { font-size: 15px; padding: 3px 9px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 100;
  max-width: calc(100% - 32px);
  padding: 10px 14px;
  background: #2b303b;
  border: 1px solid #4a5161;
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0,0,0,.5);
  line-height: 1.5;
}

/* ===== タイムライン ===== */
#scroll { flex: 1; overflow: auto; position: relative; }
#inner {
  position: relative;
  width: calc(var(--header-w) + var(--lane-w, 3600px));
  min-height: 100%;
}
.row { display: flex; height: var(--lane-h); border-bottom: 1px solid var(--line); }
.row.yt-row { height: 104px; }

.track-header {
  position: sticky;
  left: 0;
  z-index: 3;
  width: var(--header-w);
  flex: none;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
  padding: 6px 8px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  border-left: 4px solid var(--c, transparent);
}

.lane {
  position: relative;
  width: var(--lane-w);
  flex: none;
  overflow: hidden;
  background-color: #181a20;
  background-image: linear-gradient(to right, rgba(255,255,255,.06) 1px, transparent 1px);
  background-size: var(--grid-step, 60px) 100%;
}
.lane.drop { box-shadow: inset 0 0 0 2px var(--accent); }
.lane.muted .clip, .lane.muted .yt-clip { opacity: .35; }

.ruler-row { position: sticky; top: 0; z-index: 4; height: 28px; background: var(--panel); }
.ruler-row .track-header { padding: 0; }
.ruler { position: relative; width: var(--lane-w); flex: none; overflow: hidden; cursor: pointer; touch-action: none; }
.tick {
  position: absolute;
  top: 0;
  bottom: 0;
  padding: 7px 0 0 4px;
  border-left: 1px solid #3a404d;
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
  pointer-events: none;
}
#ruler-head {
  position: absolute;
  top: 0;
  bottom: 0;
  border-left: 2px solid #fff;
  pointer-events: none;
}
#ruler-head::before {
  content: "";
  position: absolute;
  top: 0;
  left: -7px;
  border: 6px solid transparent;
  border-top: 8px solid #fff;
}
body.recording #ruler-head { border-color: var(--rec); }
body.recording #ruler-head::before { border-top-color: var(--rec); }

#playhead {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  z-index: 2;
  background: #fff;
  box-shadow: 0 0 4px rgba(255,255,255,.5);
  pointer-events: none;
}
body.recording #playhead { background: var(--rec); }

/* ===== トラックヘッダー ===== */
.th-top { display: flex; align-items: center; gap: 6px; }
.th-top .name {
  flex: 1;
  min-width: 0;
  background: transparent;
  border-color: transparent;
  font-weight: 600;
}
.th-top .name:hover, .th-top .name:focus { border-color: var(--line); background: #111318; }
.th-controls { display: flex; align-items: center; gap: 5px; }
.th-controls .vol { flex: 1; min-width: 0; width: 60px; }
button.icon { padding: 3px 6px; background: transparent; border-color: transparent; }

.rec { color: var(--rec); font-weight: 700; padding: 4px 8px; white-space: nowrap; }
.rec.on { background: var(--rec); color: #fff; border-color: var(--rec); animation: pulse 1s infinite; }
.toggle { width: 28px; padding: 4px 0; font-weight: 700; }
.mute.on { background: #e0a100; color: #111; border-color: #e0a100; }
.solo.on { background: #3ecf8e; color: #111; border-color: #3ecf8e; }

.yt-label { font-weight: 600; color: #ff6b6b; }
.yt-load { display: flex; gap: 4px; }
.yt-url { flex: 1; min-width: 0; }
.yt-offset { color: var(--muted); font-size: 11px; white-space: nowrap; }

/* ===== クリップ ===== */
.clip {
  position: absolute;
  top: 5px;
  bottom: 5px;
  overflow: hidden;
  background: color-mix(in srgb, var(--c) 40%, #1b1e25);
  border: 1px solid var(--c);
  border-radius: 5px;
  cursor: grab;
  touch-action: none;
}
.clip.dragging { cursor: grabbing; z-index: 1; box-shadow: 0 4px 14px rgba(0,0,0,.5); }
.clip.selected { outline: 2px solid #fff; outline-offset: -1px; }
.clip canvas { position: absolute; top: 0; height: 100%; pointer-events: none; }
.clip-name {
  position: absolute;
  left: 8px;
  top: 2px;
  font-size: 11px;
  white-space: nowrap;
  text-shadow: 0 1px 2px #000;
  pointer-events: none;
}
.clip-del {
  display: none;
  position: absolute;
  right: 9px;
  top: 2px;
  padding: 0 5px;
  font-size: 12px;
  line-height: 16px;
  background: rgba(0,0,0,.5);
  border: none;
}
.clip:hover .clip-del, .clip.selected .clip-del { display: block; }
.h-l, .h-r { position: absolute; top: 0; bottom: 0; width: 7px; cursor: ew-resize; }
.h-l { left: 0; }
.h-r { right: 0; }
.h-l:hover, .h-r:hover { background: rgba(255,255,255,.25); }

.rec-region {
  position: absolute;
  top: 5px;
  bottom: 5px;
  background: rgba(255,77,90,.3);
  border: 1px solid var(--rec);
  border-radius: 5px;
}

.yt-clip {
  position: absolute;
  top: 5px;
  bottom: 5px;
  padding: 4px 8px;
  overflow: hidden;
  background: linear-gradient(#5a2328, #3d1a1e);
  border: 1px solid #ff5a5f;
  border-radius: 5px;
  font-size: 12px;
  white-space: nowrap;
  cursor: grab;
  touch-action: none;
}
.lane-hint {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 12px;
  pointer-events: none;
}

/* ===== プロジェクト ===== */
.project-bar { display: flex; align-items: center; gap: 6px; min-width: 0; }
.project-name {
  width: 200px;
  min-width: 0;
  background: transparent !important;
  border-color: transparent !important;
  font-weight: 600;
}
.project-name:hover, .project-name:focus { background: #111318 !important; border-color: var(--line) !important; }
.save-status { color: var(--muted); font-size: 11px; white-space: nowrap; min-width: 48px; }

.proj-dialog {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 48px);
  padding: 14px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
}
.proj-dialog::backdrop { background: rgba(0,0,0,.55); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; font-size: 15px; }
.dlg-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.proj-list { list-style: none; margin: 0; padding: 0; overflow: auto; max-height: 50dvh; }
.proj-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-bottom: 1px solid var(--line);
}
.proj-list li.current { background: rgba(79,157,255,.1); }
.pl-info { flex: 1; min-width: 0; }
.pl-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pl-meta { color: var(--muted); font-size: 11px; margin-top: 2px; }
button.danger { color: #ff8088; }
.dlg-note { color: var(--muted); font-size: 11px; margin: 10px 0 0; }

/* ===== タッチ端末 ===== */
@media (pointer: coarse) {
  .h-l, .h-r { width: 16px; background: rgba(255,255,255,.08); }
  .clip-del { font-size: 16px; line-height: 22px; padding: 0 8px; right: 18px; }
  .clip.selected .clip-del { display: block; }
}

/* ===== スマホ（狭い画面） ===== */
@media (max-width: 700px) {
  body { font-size: 12px; }
  /* iOS は 16px 未満の入力欄にフォーカスすると勝手にズームするため */
  input:not([type]), input[type=number] { font-size: 16px; }

  .topbar { gap: 8px; padding: 6px 8px; padding-top: calc(6px + env(safe-area-inset-top)); }
  .brand { display: none; }
  .project-bar { width: 100%; }
  .project-name { flex: 1; width: auto; }
  .transport { width: 100%; }
  .transport button { width: 44px; height: 38px; font-size: 17px; }
  .time { flex: 1; font-size: 17px; min-width: 0; }
  .tools { margin-left: 0; width: 100%; gap: 6px; }
  .tools label { gap: 4px; }
  #zoom { width: 80px; }
  #latency { width: 56px; }

  .panel { flex-direction: column; gap: 6px; padding: 6px 8px; }
  .yt-box { width: 100%; max-width: 360px; height: auto; aspect-ratio: 16 / 9; align-self: center; max-height: 28dvh; }

  .track-header { padding: 4px 5px; gap: 4px; }
  .th-top { gap: 2px; }
  .th-top .name { font-size: 14px; padding: 2px 4px; }
  button.icon { padding: 2px 3px; }
  .th-controls { gap: 3px; flex-wrap: wrap; }
  .th-controls .vol { flex-basis: 100%; height: 16px; }
  .rec { padding: 4px 6px; }
  .toggle { width: 26px; }
  :root { --lane-h: 96px; }
  .row.yt-row { height: 112px; }
  .yt-url { font-size: 14px !important; }
  .yt-offset { display: none; }
  .yt-load button { padding: 4px 6px; }
}

@keyframes blink { 50% { opacity: .3; } }
@keyframes pulse { 50% { opacity: .65; } }
