/* 테마: 기본 어둡게. <html data-theme="light"> 면 밝게 (packages/ui/theme.js 가 붙인다) */
:root {
  color-scheme: dark;
  --bg: #111318;
  --panel: #1a1d24;
  --panel-2: #22262f;
  --line: #2a2f3a;
  --text: #e8eaf0;
  --muted: #9aa3b2;
  --accent: #4f8cff;
  --accent-text: #ffffff;
  --accent-soft: #4f8cff22;
  --danger: #ff5f6d;
  --warn: #ffb547;
  --ok: #4fd18b;
  --overlay: #000c;
}
:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-2: #eef0f4;
  --line: #d9dde5;
  --text: #1b1f27;
  --muted: #5c6675;
  --accent: #2f6fe4;
  --accent-text: #ffffff;
  --accent-soft: #2f6fe41f;
  --danger: #d6333f;
  --warn: #a35f00;
  --ok: #1f8f5a;
  --overlay: #1b1f27e6;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  -webkit-text-size-adjust: 100%;
}

.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px 16px calc(24px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- 공통 ---------- */

h1 { font-size: 18px; margin: 0; letter-spacing: 0.02em; }
h2 { font-size: 15px; margin: 0; color: var(--muted); }
.muted { color: var(--muted); }
.row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }

input[type="text"], input[type="password"], select {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 16px;
  min-width: 0;
}

button {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 16px;
  cursor: pointer;
  touch-action: manipulation;
  white-space: nowrap;
}
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.45; cursor: default; transform: none; }

button.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
  font-weight: 600;
}
button.primary.on { background: var(--danger); border-color: var(--danger); }

button.quiet { padding: 7px 10px; font-size: 14px; color: var(--muted); }
button.quiet.danger:hover { color: var(--danger); }
button.toggle.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; } /* 켜짐은 채워서, 한눈에 */

button.icon {
  width: 40px; height: 40px; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 18px;
}

.btn-link { color: var(--muted); text-decoration: none; font-size: 14px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; }
.btn-link:hover { color: var(--text); }

.pill {
  font-size: 12px; padding: 3px 8px; border-radius: 999px;
  border: 1px solid var(--line); color: var(--muted);
}
.pill.wifi { color: var(--ok); border-color: var(--ok); }
.pill.cellular { color: var(--warn); border-color: var(--warn); }

.badge {
  font-size: 12px; padding: 3px 8px; border-radius: 6px; white-space: nowrap;
  background: var(--panel-2); color: var(--muted); border: 0; font: inherit; font-size: 12px; cursor: default;
}
button.badge { cursor: pointer; }
.badge.local { color: var(--ok); }
.badge.stream { color: var(--warn); }

.banner {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  color: var(--muted);
}
.banner.warn { border-color: var(--warn); color: var(--warn); }
.banner[hidden] { display: none; }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: var(--overlay); color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: 14px; opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none;
  max-width: calc(100vw - 32px); text-align: center; z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- 상단 ---------- */

.top { display: flex; flex-direction: column; gap: 10px; }
.top-row { display: flex; align-items: center; justify-content: space-between; }
.top-right { display: flex; align-items: center; gap: 8px; }

.url-form { display: flex; gap: 8px; }
.url-form input { flex: 1; }

.player-top { flex-direction: row; align-items: center; gap: 10px; }
.player-title {
  flex: 1; min-width: 0; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

/* ---------- 라이브러리 ---------- */

.item-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.item-list .empty { color: var(--muted); padding: 24px 0; text-align: center; }

.item {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex; flex-direction: column; gap: 8px;
}
.item-main {
  background: none; border: none; padding: 0; text-align: left; width: 100%;
  display: flex; flex-direction: column; gap: 3px; white-space: normal;
}
.item-title { font-weight: 600; line-height: 1.3; }
.item-meta { color: var(--muted); font-size: 13px; }
.item-actions { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.item-actions .status { font-size: 13px; color: var(--muted); padding: 0 4px; }
.item-actions .status.error { color: var(--danger); }
.item-error { font-size: 12px; color: var(--danger); word-break: break-all; }

progress {
  width: 100%; height: 4px; appearance: none; border: none;
  background: var(--line); border-radius: 2px; overflow: hidden;
}
progress::-webkit-progress-bar { background: var(--line); }
progress::-webkit-progress-value { background: var(--accent); }
progress::-moz-progress-bar { background: var(--accent); }

/* ---------- 로그인 / 계정 ---------- */

.login { display: flex; flex-direction: column; gap: 16px; padding: 24px 0; align-items: stretch; max-width: 420px; }
.login p { margin: 0; color: var(--muted); }
.login-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 22px 20px; display: flex; flex-direction: column; gap: 14px; }
.login-card h2 { color: var(--text); font-size: 18px; }
.login-card a.button { display: block; }
.login-or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; }
.login-or::before, .login-or::after { content: ''; flex: 1; height: 1px; background: var(--line); }
.login-form { display: flex; flex-direction: column; gap: 10px; }
.login-form label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.login-form input { width: 100%; box-sizing: border-box; font-size: 16px; padding: 11px 12px; }
.login-form button { margin-top: 4px; padding: 12px; font-size: 16px; }
.login-error { color: var(--warn); font-size: 13px; }
.login > button.quiet { align-self: flex-start; }
a.button {
  display: inline-block; text-decoration: none; text-align: center;
  background: var(--panel); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 12px 18px; font-size: 16px;
}
a.button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
a.button.small { padding: 7px 12px; font-size: 14px; }
.account { display: flex; align-items: center; gap: 10px; }
.account > div { flex: 1; min-width: 0; }
.avatar { width: 36px; height: 36px; border-radius: 50%; }
.small-text { font-size: 13px; }

/* ---------- 설정 ---------- */

dialog {
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px; padding: 0;
  width: min(420px, calc(100vw - 32px));
}
dialog::backdrop { background: #0009; }
.settings { display: flex; flex-direction: column; gap: 12px; padding: 18px; }
.settings h2 { color: var(--text); font-size: 17px; }
.settings label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
/* 설정의 켜기/끄기 줄: 글은 왼쪽, 스위치는 오른쪽 (기본 체크박스는 테마와 안 맞는다) */
.settings label.check { flex-direction: row; align-items: center; justify-content: space-between; gap: 12px; color: var(--text); }
.settings .check input[type="checkbox"] {
  appearance: none; -webkit-appearance: none; width: 44px; height: 26px; margin: 0; flex: none;
  border-radius: 999px; background: var(--line); position: relative; cursor: pointer; transition: background .15s;
}
.settings .check input[type="checkbox"]::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px #0004; transition: transform .15s;
}
.settings .check input[type="checkbox"]:checked { background: var(--accent); }
.settings .check input[type="checkbox"]:checked::after { transform: translateX(18px); }
.settings .check input[type="checkbox"]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.settings-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- 플레이어 ---------- */

/* 영상 + 현재 문장: 상단 고정 */
.stage {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding-top: 4px; margin: -4px -2px 0; padding-left: 2px; padding-right: 2px;
}
/* 세 줄 고정 높이: 문장 길이에 따라 줄 수가 바뀌어도 아래 버튼·목록이 흔들리지 않게. 더 길면 칸 안에서만 스크롤 */
.caption {
  height: calc(3 * 1.4em + 18px); padding: 8px 12px; margin-top: 6px; /* 안쪽 여백 16 + 테두리 2 */
  display: flex; flex-direction: column; justify-content: safe center;
  overflow-y: auto; scrollbar-width: thin;
  font-size: calc(18px * var(--cap-scale, 1)); line-height: 1.4; text-align: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  word-break: keep-all; overflow-wrap: anywhere;
}
.caption.empty::before { content: '재생하면 문장이 여기에 보입니다'; color: var(--muted); font-size: 14px; }
/* 구(쉼표 등으로 나눈 조각)는 한 덩어리로 줄을 바꾼다. balance 는 두세 줄일 때 줄 길이를 고르게 */
.caption, .caption-ko { text-wrap: balance; }
.caption .phrase, .caption-ko .phrase { display: inline-block; }
.caption-ko { font-size: calc(14px * var(--cap-scale, 1)); color: var(--muted); text-align: center; padding: 4px 12px 0; min-height: 1.5em; word-break: keep-all; }
.caption-ko[hidden] { display: none; }
.cue-ko { display: block; font-size: 13px; color: var(--muted); word-break: keep-all; }
.edit-row input.text.ko { font-size: 13px; color: var(--muted); }
/* 영상 정보 창 (머리의 스트리밍/저장됨 배지를 누르면) */
.info-title { font-size: 15px; line-height: 1.35; }
.info-row { display: flex; gap: 12px; justify-content: space-between; font-size: 14px; }
.info-row span:first-child { flex: none; }
.info-row span:last-child { text-align: right; min-width: 0; word-break: keep-all; overflow-wrap: anywhere; }

/* 문장 편집기 */
.cue-editor { display: flex; flex-direction: column; gap: 8px; }
.edit-bar { display: flex; flex-direction: column; gap: 6px; padding: 8px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; }
.edit-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 50vh; overflow-y: auto; }
.edit-row {
  display: grid; grid-template-columns: 24px auto 1fr auto; gap: 6px; align-items: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px;
}
.edit-row .num { color: var(--muted); font-size: 12px; text-align: right; }
.edit-row input.time { padding: 6px 8px; font-size: 14px; width: 76px; font-variant-numeric: tabular-nums; }
.edit-row input.text { grid-column: 1 / -1; padding: 6px 8px; font-size: 14px; }
.edit-row .row-actions { grid-column: 1 / -1; display: flex; gap: 4px; justify-content: flex-end; }
button.tiny { padding: 5px 8px; font-size: 12px; }
.catalog { display: flex; flex-direction: column; gap: 8px; }

.player-wrap {
  position: relative; width: 100%; aspect-ratio: 16 / 9;
  max-height: 38vh; /* 가로 화면이나 짧은 화면에서 고정 영역이 전부를 덮지 않게 */
  background: #000; border-radius: 10px; overflow: hidden;
}
.player-wrap iframe, .player-wrap video, #player {
  position: absolute; inset: 0; width: 100%; height: 100%;
}
.player-wrap video { object-fit: contain; }
/* 영상 위 자막: 영상 아래 절반을 불투명한 띠로 덮는다 — 영상에 박힌 자막과 YouTube 의 시간·전체화면 줄(아래에서 약 80px 위에 뜬다)까지 가린다.
   띠는 터치를 받아 둔다: 띠 밑에 숨은 YouTube 재생 막대가 보이지 않는 채로 눌려 탐색되는 일이 없게. 영상 위쪽을 두드리면 전처럼 재생/정지 */
.player-wrap .caption-box {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 2; box-sizing: border-box;
  min-height: max(50%, 110px); max-height: 100%; /* 문장이 길면 내용만큼 자란다 (고정 높이면 두 줄째가 잘린다) */
  padding: 14px 12px 8px; display: flex; flex-direction: column; justify-content: flex-end; gap: 2px;
  background: linear-gradient(to bottom, #0000 0, #000 10px, #000 100%);
}
.player-wrap .caption {
  display: block; height: auto; max-height: none; padding: 0; margin: 0; overflow: visible;
  background: none; border: 0; color: #fff; font-size: calc(16px * var(--cap-scale, 1)); line-height: 1.35; text-shadow: none;
}
.player-wrap .caption.empty::before { color: #9aa3b2; }
.player-wrap .caption-ko { color: #c9d2de; font-size: calc(13px * var(--cap-scale, 1)); padding: 0; min-height: 0; }

/* 고정 조작 줄: 이전 문장 · 반복 · 다음 문장 · 재생/정지 */
.stage-controls { display: grid; grid-template-columns: auto 1fr auto auto; gap: 8px; margin-top: 8px; align-items: stretch; }
.stage-controls button { min-height: 46px; }
.stage-controls .step { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; padding: 4px 10px; line-height: 1; }
.stage-controls .step .lbl { font-size: 10px; }
.stage-controls .ico { width: 18px; height: 18px; fill: currentColor; display: block; }
.play-pause { width: 50px; padding: 0; display: inline-flex; align-items: center; justify-content: center; color: var(--text); }
.play-pause .ico { width: 20px; height: 20px; }
.play-pause .ico-pause, .play-pause.playing .ico-play { display: none; }
.play-pause.playing .ico-pause { display: block; }
.now { display: flex; justify-content: space-between; align-items: baseline; font-variant-numeric: tabular-nums; }

/* ---------- 전체화면 (.stage 를 올린다: 영상 + 자막 띠 + 네 버튼) ---------- */
.stage:not(.fs) .fs-exit { display: none; } /* 전체화면일 때만 (button.icon 규칙보다 우선) */
.stage.fs {
  position: fixed; inset: 0; z-index: 50; margin: 0; padding: 0; width: 100vw; height: 100vh; height: 100dvh;
  background: #000; display: flex; flex-direction: column; overflow: hidden;
}
.stage.fs .player-wrap { flex: 1; min-height: 0; max-height: none; aspect-ratio: auto; border-radius: 0; }
.stage.fs .player-wrap .caption-box { bottom: var(--fs-ctl, 0); transition: bottom .2s; min-height: max(32%, 110px); }
.stage.fs:not(.idle) { --fs-ctl: 70px; }
/* 전체화면: 글자를 화면 너비에 비례시키고(가로 폰 약 20px, 태블릿 약 25px, 데스크톱 30px) 설정 배율을 곱한다 */
.stage.fs .player-wrap .caption { font-size: calc(clamp(16px, 2.4vw, 30px) * var(--cap-scale, 1)); }
.stage.fs .player-wrap .caption-ko { font-size: calc(clamp(13px, 1.9vw, 24px) * var(--cap-scale, 1)); }
.stage.fs .player-wrap .caption-box { touch-action: none; } /* 두 손가락 벌리기/오므리기로 글자 크기 */
.stage.fs .stage-controls {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 3; margin: 0;
  padding: 8px max(12px, env(safe-area-inset-right, 0px)) max(8px, env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px));
  background: #000d; transition: opacity .2s;
}
.stage.fs .now {
  position: absolute; top: max(8px, env(safe-area-inset-top, 0px)); left: max(12px, env(safe-area-inset-left, 0px)); z-index: 3;
  gap: 12px; color: #fff; background: #0008; padding: 2px 10px; border-radius: 8px; transition: opacity .2s;
}
.stage.fs .now-time { font-size: 16px; }
.stage.fs .loop-state { color: #cfd6e0; } .stage.fs .loop-state.on { color: #8ab4ff; }
.stage.fs .fs-exit {
  display: flex; position: absolute; top: max(8px, env(safe-area-inset-top, 0px)); right: max(8px, env(safe-area-inset-right, 0px)); z-index: 3;
  background: #0008; color: #fff; border-color: #fff3; transition: opacity .2s;
}
.stage.fs.idle .stage-controls, .stage.fs.idle .now, .stage.fs.idle .fs-exit { opacity: 0; pointer-events: none; }
html.has-fs { overflow: hidden; } /* iPhone 대체 레이아웃에서 뒤 화면이 스크롤되지 않게 */
.now-time { font-size: 28px; font-weight: 600; }
.loop-state { color: var(--muted); }
.loop-state.on { color: var(--accent); }

.ab { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.ab-row { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 8px; }
.ab-row label { font-weight: 700; width: 1.2em; text-align: center; }
.ab-row input { width: 100%; }

.options { display: flex; gap: 12px 16px; flex-wrap: wrap; align-items: center; color: var(--muted); }
.options label { display: flex; align-items: center; gap: 8px; }

.sections-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
/* 문장 머리줄: 버튼이 많아도 한 줄. 넘치면 옆으로 밀어 본다 (두 줄이면 화면을 너무 먹는다) */
.cue-head { flex-wrap: nowrap; }
.cue-head .row { flex: 1; min-width: 0; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; padding: 2px 0; }
.cue-head .row::-webkit-scrollbar { display: none; }
.cue-head .row > * { flex: none; white-space: nowrap; }

.script { display: flex; flex-direction: column; gap: 8px; }
.cue-list {
  list-style: none; margin: 0; padding: 0;
  position: relative;
  max-height: 40vh; overflow-y: auto; overscroll-behavior: contain;
  border: 1px solid var(--line); border-radius: 10px; background: var(--panel);
}
.cue-list[hidden] { display: none; }
.cue {
  display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: start;
  padding: 0 0 0 10px; border-bottom: 1px solid var(--line); line-height: 1.45;
}
.cue:last-child { border-bottom: none; }
.cue.current { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
/* 왼쪽 열: 체크박스 위, 시각 아래. 열 전체가 체크 라벨이라 어디를 눌러도 체크만 된다 (줄 재생은 오른쪽 문장 영역) */
.cue-check {
  display: flex; flex-direction: column; align-items: center; gap: 4px; min-width: 52px;
  padding: 8px 4px 8px 0; cursor: pointer; align-self: stretch; touch-action: manipulation; user-select: none;
}
.cue input[type="checkbox"] {
  width: 22px; height: 22px; margin: 2px 0 0; accent-color: var(--accent); flex: none;
  touch-action: manipulation;
}
.cue-num { color: var(--muted); font-size: 12px; padding-top: 3px; min-width: 22px; text-align: center; }
.cue-body {
  display: grid; grid-template-columns: 1fr; gap: 6px; align-items: start;
  padding: 8px 12px 8px 0; cursor: pointer; min-width: 0;
}
.cue-lines { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.cue-copy { background: none; border: none; color: var(--muted); font-size: 15px; padding: 8px 10px 8px 4px; margin: 0; cursor: pointer; line-height: 1.45; min-width: 0; }
.cue-copy:active { color: var(--accent); }
.cue-list.no-ko .cue-ko { display: none; }
.cue-body:hover { background: var(--panel-2); }
.cue-body:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.cue-time { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.cue-text { word-break: break-word; }
button.small { padding: 7px 12px; font-size: 14px; }

.section-list { list-style: none; padding: 0; margin: 8px 0 0; display: flex; flex-direction: column; gap: 6px; }
.section-list li {
  display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 6px 6px 12px;
}
.section-list .load { background: none; border: none; text-align: left; padding: 6px 0; font-variant-numeric: tabular-nums; }
.section-list .del { color: var(--muted); padding: 6px 10px; }
.section-list .empty { color: var(--muted); background: none; border: none; display: block; }

.help { color: var(--muted); font-size: 13px; }
.help p { margin: 4px 0; }
.help .version { margin-top: 10px; font-size: 11px; opacity: .7; font-variant-numeric: tabular-nums; }

kbd, code {
  background: var(--panel); border: 1px solid var(--line); border-radius: 4px;
  padding: 1px 5px; font-size: 12px;
}

@media (max-width: 480px) {
  .ab { grid-template-columns: 1fr; }
  .url-form { flex-wrap: wrap; }
  .url-form input { flex-basis: 100%; }
  .url-form button { flex: 1; }
}
