:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f2f5;
  --border: #e3e5ea;
  --text: #16181d;
  --text-2: #5b6170;
  --text-3: #8a90a0;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --accent-text: #3730a3;
  --ok: #16a34a;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --err: #dc2626;
  --err-soft: #fee2e2;
  --hl: #fff7d6;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --radius: 12px;
  --sidebar: 272px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1f232b;
    --border: #2b303a;
    --text: #e8eaf0;
    --text-2: #a7adbb;
    --text-3: #7c8293;
    --accent: #818cf8;
    --accent-soft: #23264a;
    --accent-text: #c7cbff;
    --warn-soft: #3a2c10;
    --warn: #fbbf24;
    --err-soft: #3b1414;
    --err: #f87171;
    --hl: #3a3417;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 "Pretendard Variable", Pretendard, -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- 레이아웃 ---------- */
.shell { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 20px 16px; display: flex; flex-direction: column; gap: 20px;
}
.main { min-width: 0; padding: 28px 32px 64px; outline: none; container-type: inline-size; }

.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; letter-spacing: -0.01em; }
.brand-mark svg { width: 28px; height: 28px; display: block; }
.brand-mark rect { fill: var(--accent); }
.brand-mark path { fill: #fff; }

/* ---------- 입력 ---------- */
.add-form { display: flex; flex-direction: column; gap: 8px; }
.add-row { display: flex; gap: 8px; }
input[type="text"], input[type="search"], input[type="password"], select {
  width: 100%; height: 40px; padding: 0 12px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  outline: none; transition: border-color .15s, background .15s;
}
input:focus, select:focus { border-color: var(--accent); background: var(--surface); }
select { padding-right: 8px; min-width: 0; }

.btn {
  height: 40px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; white-space: nowrap; font-weight: 600;
  display: inline-flex; align-items: center; gap: 6px; transition: background .15s, opacity .15s;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent); opacity: .9; }
.btn-sm { height: 32px; padding: 0 12px; font-size: 13px; border-radius: 8px; }
.btn-danger { color: var(--err); }
.icon-btn {
  width: 28px; height: 28px; border: 0; border-radius: 8px; background: transparent;
  display: inline-grid; place-items: center; cursor: pointer; color: var(--text-2);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg, .ico { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }

/* ---------- 사이드바 내비 ---------- */
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item, .collection-link {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--text-2); font-weight: 500;
}
.nav-item:hover, .collection-link:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active, .collection-link.active { background: var(--accent-soft); color: var(--accent-text); }
.nav-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-top: 14px; padding: 0 4px 0 10px; font-size: 12px; font-weight: 600; color: var(--text-3);
  text-transform: uppercase; letter-spacing: .04em;
}
.collections { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.collection { position: relative; }
.collection-link { padding-right: 64px; }
.collection-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.collection-count { font-size: 12px; color: var(--text-3); }
.collection-actions { position: absolute; right: 4px; top: 50%; transform: translateY(-50%); display: none; gap: 2px; }
.collection:hover .collection-actions, .collection:focus-within .collection-actions { display: flex; }
.collection:hover .collection-count { visibility: hidden; }
.collections .empty { padding: 6px 10px; font-size: 13px; color: var(--text-3); }

/* ---------- 목록 ---------- */
.list-head { display: flex; align-items: center; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.list-head h1 { font-size: 22px; margin: 0; letter-spacing: -0.02em; flex: 1; min-width: 0; }
.search { position: relative; width: min(360px, 100%); }
.search input { padding-left: 36px; }
.search .ico { position: absolute; left: 12px; top: 12px; color: var(--text-3); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 20px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; box-shadow: var(--shadow); display: flex; flex-direction: column;
  transition: transform .15s, box-shadow .15s;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgb(16 24 40 / 12%); }
.thumb { position: relative; aspect-ratio: 16 / 9; background: var(--surface-2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .duration {
  position: absolute; right: 8px; bottom: 8px; padding: 1px 6px; border-radius: 6px;
  background: rgb(0 0 0 / 75%); color: #fff; font-size: 12px; font-weight: 600;
}
.card-body { padding: 12px 14px 14px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.card-title { font-weight: 650; line-height: 1.4; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-meta { font-size: 12.5px; color: var(--text-3); }
.card-lead { font-size: 13.5px; color: var(--text-2); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }

.badge {
  display: inline-flex; align-items: center; gap: 6px; align-self: flex-start;
  font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
}
.badge-queued, .badge-processing { background: var(--warn-soft); color: var(--warn); }
.badge-failed { background: var(--err-soft); color: var(--err); }
.badge-processing::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty-state { text-align: center; padding: 80px 20px; color: var(--text-2); }
.empty-state h2 { font-size: 18px; color: var(--text); margin: 0 0 8px; }
.more { display: flex; justify-content: center; margin-top: 24px; }

/* ---------- 상세 ---------- */
.detail { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); gap: 28px; align-items: start; }
/* 왼쪽: 영상·정보 + 스크립트(남은 높이를 채우며 스크롤) */
.detail-left {
  position: sticky; top: 20px; height: calc(100vh - 40px);
  display: flex; flex-direction: column; gap: 14px; min-height: 0;
}
.detail-top { display: flex; flex-direction: column; gap: 12px; }
.script-panel { flex: 1; min-height: 180px; display: flex; flex-direction: column; overflow: hidden; }
.script-wrap { flex: 1; overflow-y: auto; padding: 6px 8px 10px; position: relative; }
.panel-head {
  display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.panel-head h2 { font-size: 15px; margin: 0; flex: 1; }
.back { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); font-weight: 500; margin-bottom: 16px; }
.back:hover { color: var(--text); }
.player { aspect-ratio: 16 / 9; border-radius: var(--radius); overflow: hidden; background: #000; }
.player iframe, .player > div { width: 100%; height: 100%; display: block; }
.detail-title { font-size: 20px; line-height: 1.4; margin: 0; letter-spacing: -0.02em; }
.detail-meta { font-size: 13px; color: var(--text-3); display: flex; flex-wrap: wrap; gap: 4px 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  height: 30px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-size: 13px; color: var(--text-2);
}
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: transparent; color: var(--accent-text); font-weight: 600; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 0; }
.tabs { display: flex; gap: 4px; padding: 8px 8px 0; border-bottom: 1px solid var(--border); align-items: center; }
.tab {
  border: 0; background: none; padding: 10px 14px; cursor: pointer; font-weight: 600; color: var(--text-3);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.tabs .spacer { flex: 1; }
.tab-body { padding: 24px 28px 32px; }

.summary { line-height: 1.75; word-break: keep-all; overflow-wrap: anywhere; }
.summary > p:first-child { font-size: 16px; background: var(--accent-soft); color: var(--text); padding: 16px 18px; border-radius: 10px; margin-top: 0; }
.summary p { margin: 0 0 12px; }
.summary ol, .summary ul { padding-left: 22px; margin: 4px 0 14px; }
.summary li { margin: 4px 0; }
.summary li > p { margin: 4px 0; }
.summary ul { list-style: disc; }
.summary ul ul { list-style: circle; }
.summary strong { font-weight: 700; }
.summary > p.h2 { font-size: 18px; margin: 32px 0 8px; letter-spacing: -0.01em; }
.summary > p.h3 { font-size: 16px; margin: 22px 0 6px; }
.summary > p.h2 + p { color: var(--text-2); }
.summary blockquote { margin: 24px 0 0; padding: 10px 14px; border-left: 3px solid var(--border); color: var(--text-3); font-size: 13px; }

.script { list-style: none; margin: 0; padding: 0; }
.script li { display: grid; grid-template-columns: 64px 1fr; gap: 12px; padding: 7px 8px; border-radius: 8px; }
.script li.now { background: var(--hl); }
.script li.flash { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); } to { background: transparent; } }
.script p { font-size: 14px; }

/* 요약 속 인용 [n] */
.cite {
  border: 0; background: none; padding: 0 1px; margin-left: 2px; cursor: pointer;
  color: var(--accent); font-size: 12px; font-weight: 600; vertical-align: 1px;
  font-variant-numeric: tabular-nums; border-radius: 4px;
}
.cite:hover, .cite:focus-visible { background: var(--accent-soft); outline: none; }
.summary .h2 .cite, .summary .h3 .cite { font-size: 12px; }
.cite-pop {
  position: fixed; z-index: 50; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px 12px;
  box-shadow: 0 12px 32px rgb(16 24 40 / 22%); display: flex; flex-direction: column; gap: 10px;
}
.cite-pop[hidden] { display: none; }
.cite-pop-title { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-3); min-width: 0; }
.cite-pop-title span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cite-pop-n {
  flex: none; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 5px; display: inline-grid; place-items: center;
  border: 1px solid var(--accent); color: var(--accent); font-weight: 700; font-size: 11px;
}
.cite-pop-body { max-height: 200px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; }
.cite-pop-body p { margin: 0; font-size: 14px; line-height: 1.6; }
.cite-pop-body p.dim { color: var(--text-3); }
.cite-pop .ts { align-self: flex-start; padding: 0 10px; height: 26px; }
.ts {
  border: 0; background: var(--accent-soft); color: var(--accent-text); font-weight: 600; font-size: 12.5px;
  height: 24px; border-radius: 6px; cursor: pointer; font-variant-numeric: tabular-nums; margin-top: 2px;
}
.ts:hover { background: var(--accent); color: #fff; }
.script p { margin: 0; color: var(--text-2); word-break: keep-all; overflow-wrap: anywhere; }

.state { padding: 48px 28px; text-align: center; color: var(--text-2); }
.state .badge { align-self: center; margin-bottom: 12px; }
.state pre { white-space: pre-wrap; text-align: left; background: var(--err-soft); color: var(--err); padding: 12px; border-radius: 8px; font-size: 13px; }

.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-weight: 500;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--err); color: #fff; }

.logout { margin-top: auto; border: 0; background: none; cursor: pointer; text-align: left; width: 100%; font: inherit; }

/* ---------- 채널 ---------- */
.ch-help { color: var(--text-2); margin: -8px 0 16px; font-size: 14px; }
.ch-add { display: grid; grid-template-columns: minmax(0, 1fr) 180px auto; gap: 8px; margin-bottom: 28px; max-width: 760px; }
.ch-group { margin-bottom: 24px; }
.ch-group h2 { font-size: 15px; margin: 0 0 8px; }
.ch-group h2 a:hover { color: var(--accent); }
.ch-list { overflow: hidden; }
.ch-row {
  display: grid; grid-template-columns: minmax(0, 1fr) 170px auto auto auto; gap: 12px; align-items: center;
  padding: 12px 16px; border-top: 1px solid var(--border);
}
.ch-row:first-child { border-top: 0; }
.ch-row.off .ch-name { color: var(--text-3); }
.ch-row select { height: 34px; font-size: 13px; }
.ch-name { font-weight: 650; }
.ch-name:hover { color: var(--accent); }
.ch-sub { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12.5px; color: var(--text-3); margin-top: 2px; align-items: center; }
.ch-error { color: var(--err); max-width: 420px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta-link { color: var(--accent); }
.meta-link:hover { text-decoration: underline; }
.switch { position: relative; width: 38px; height: 22px; display: inline-block; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch span[aria-hidden] { position: absolute; inset: 0; border-radius: 999px; background: var(--border); transition: background .15s; pointer-events: none; }
.switch span[aria-hidden]::after { content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(16px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- 반응형 ---------- */
/* 상세 두 열은 브라우저 폭이 아니라 본문 영역 폭으로 정한다 */
@container (max-width: 760px) {
  /* 한 열: 영상·정보 → 요약 → 스크립트 순서 */
  .detail { grid-template-columns: 1fr; }
  .detail-left { display: contents; }
  .detail-top { order: 1; }
  .summary-panel { order: 2; }
  .script-panel { order: 3; }
  .script-wrap { max-height: 60vh; }
}
@media (max-width: 1280px) {
  :root { --sidebar: 232px; }
  .main { padding: 24px 24px 56px; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--border); padding: 16px; gap: 14px; }
  .nav { flex-direction: row; flex-wrap: wrap; gap: 6px; align-items: center; }
  .nav-head { margin: 0; padding: 0; }
  .nav-head span { display: none; }
  .collections { flex-direction: row; flex-wrap: wrap; gap: 6px; }
  .collection-link { padding-right: 10px; }
  .logout { margin-top: 0; width: auto; }
  .collection-actions, .collection:hover .collection-count { display: none; visibility: visible; }
  .main { padding: 20px 16px 48px; }
  .tab-body { padding: 18px 16px 24px; }
  .grid { grid-template-columns: 1fr; }
  .ch-add { grid-template-columns: 1fr; }
  .ch-row { grid-template-columns: 1fr auto auto; }
  .ch-row .ch-main { grid-column: 1 / -1; }
}
