:root {
  --bg: #fafaf8;
  --fg: #1c1c1e;
  --muted: #8a8a8e;
  --line: #e6e6e2;
  --accent: #2b6cb0;
  --hl: #ffe08a;
  --pick: #cfe0f0;
  --zh: #0a8a5f;
  --card: #fff;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  font-family: system-ui, -apple-system, "Noto Sans TC", sans-serif;
  max-width: 720px;
  margin: 0 auto;
  padding: 1.2rem 1rem calc(6rem + env(safe-area-inset-bottom, 0px));
  line-height: 1.6;
  color: var(--fg);
  background: var(--bg);
}

h1 { font-size: 1.5rem; letter-spacing: -0.01em; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* 按鈕 */
button {
  font: inherit;
  padding: .4rem .7rem;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 8px;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
button:hover { background: #f0f0ec; }
button:active { background: #e6e6e0; }
button:disabled { opacity: .5; cursor: default; }

input[type="text"], input:not([type]) {
  font: inherit;
  padding: .4rem .6rem;
  border: 1px solid var(--line);
  border-radius: 8px;
}

/* ── 媒體庫 ── */
/* 底部固定區：手機拇指操作區，貼底跨滿寬度，蓋過 body 左右 margin。
   由上到下：搜尋框（僅媒體庫 tab）、tab 切換列。 */
.lib-bottom-fixed {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  max-width: 720px;
  margin: 0 auto;
  background: var(--card);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.lib-search {
  display: block;
  width: calc(100% - 1.6rem);
  box-sizing: border-box;
  margin: .5rem .8rem;
  padding: .5rem .7rem;
}
.lib-tabs {
  display: flex;
  gap: .4rem;
}
.lib-tabs button {
  flex: 1;
  border: none;
  border-top: 2px solid transparent;
  border-radius: 0;
  background: transparent;
  padding: .7rem .4rem;
  font-weight: 500;
  color: var(--muted);
}
.lib-tabs button.active { color: var(--accent); border-top-color: var(--accent); }
.lib-tabs button:hover { background: transparent; }

.library-scroll-area {
  max-height: calc(100vh - 12rem);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.stage-group {
  margin-bottom: .6rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.stage-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .65rem .8rem;
  font-weight: 500;
  background: transparent;
  border: none;
  border-radius: 0;
  color: var(--muted);
}
/* 進行中（下載中／翻譯中）：強調色 + 脈動指示點，一眼區分於等待中 */
.stage-header.active {
  color: var(--accent);
}
.stage-dot {
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: var(--accent);
  margin-right: .5rem;
  flex-shrink: 0;
  animation: stage-pulse 1.4s ease-in-out infinite;
}
@keyframes stage-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: .4; transform: scale(.7); }
}
.stage-header-label { display: flex; align-items: center; }
.stage-group .lib-list {
  margin: 0;
  padding: 0 .6rem .6rem;
  border-top: 1px solid var(--line);
}
.stage-group .lib-list .lib-item {
  border: none;
  border-radius: 0;
  border-bottom: 1px solid var(--line);
  margin-bottom: 0;
  padding: .7rem .2rem;
}
.stage-group .lib-list .lib-item:last-child { border-bottom: none; }
.stage-caret { transition: transform .15s; color: var(--muted); }
.stage-caret.open { transform: rotate(180deg); }

.progress {
  display: flex;
  align-items: center;
  gap: .5rem;
  margin-top: .4rem;
}
.progress-bar {
  height: 6px;
  flex: 1;
  background: var(--line);
  border-radius: 999px;
  overflow: hidden;
  position: relative;
}
.progress-bar::after {
  content: "";
  display: block;
  height: 100%;
  width: var(--pct, 0%);
  background: var(--accent);
  border-radius: 999px;
  transition: width .3s ease;
}
.progress-pct {
  font-size: .75rem;
  color: var(--muted);
  flex-shrink: 0;
  min-width: 2.6em;
  text-align: right;
}

.lib-actions {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
  margin: 1rem 0;
  display: grid;
  gap: .7rem;
}
.lib-list { list-style: none; padding: 0; margin: 0; }
.lib-item {
  padding: .8rem 1rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: .6rem;
}
.lib-item .name {
  font-weight: 500; margin-bottom: .5rem; overflow-wrap: anywhere;
  display: flex; align-items: baseline; gap: .4rem; flex-wrap: wrap;
}
.lib-item .name .idx { color: var(--muted); font-weight: 400; }
.lib-item .row { display: flex; align-items: center; gap: .5rem; flex-wrap: wrap; }
.lib-item .meta { color: var(--muted); font-size: .8rem; }
.lib-item .actions { margin-left: auto; display: flex; gap: .35rem; }

button.icon {
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--fg);
}
button.icon.danger { color: #b00020; }
button.icon:hover { background: #f0f0ec; }
button.icon.danger:hover { background: #fde2e1; }

.badge {
  font-size: .75rem;
  padding: .15rem .5rem;
  border-radius: 999px;
  white-space: nowrap;
}
.badge.pending, .badge.processing,
.badge.queued_download, .badge.downloading,
.badge.queued_translate, .badge.translating { background: #fff3cd; color: #8a6d00; }
.badge.ready { background: #d5f5e3; color: #0a7a4f; }
.badge.failed { background: #fde2e1; color: #b00020; }
.badge.partial { background: #e8f0fb; color: var(--accent); }

/* ── 長音檔切分：媒體庫合併卡片、展開子清單 ── */
.group-toggle {
  display: inline-flex; align-items: center; gap: .3rem;
  font-size: .72rem; font-weight: 600;
  color: var(--accent); background: #e8f0fb;
  border: none; border-radius: 999px; padding: .16rem .55rem;
  cursor: pointer;
}
.group-toggle:hover { background: var(--accent); color: #fff; }
.group-toggle .caret { transition: transform .15s; font-size: .65em; }
.group-toggle.open .caret { transform: rotate(180deg); }

.part-strip { margin: .5rem 0 0; display: flex; gap: 3px; }
.part-strip span { flex: 1; height: 5px; border-radius: 3px; background: #0a7a4f; opacity: .8; }
.part-strip span.pending { background: var(--line); opacity: 1; }

.part-sub-list {
  margin-top: .6rem; padding-top: .5rem; border-top: 1px solid var(--line);
  display: flex; flex-direction: column; gap: .3rem;
}
.part-sub-item { display: flex; align-items: center; gap: .5rem; padding: .3rem 0; }
.part-sub-item .p-idx { font-size: .78rem; color: var(--muted); width: 1.4em; flex-shrink: 0; }
.part-sub-item .p-name { flex: 1; font-size: .87rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--fg); }
.part-sub-item .p-actions { display: flex; gap: .1rem; flex-shrink: 0; }
.part-sub-item .p-actions button {
  width: 32px; height: 32px; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--fg); border: none; background: none; border-radius: 6px;
}
.part-sub-item .p-actions button:hover { background: #f0f0ec; }
.part-sub-item .p-actions button:disabled { opacity: .3; cursor: default; }
.part-sub-item .p-actions button:disabled:hover { background: none; }

/* 新增頁排隊清單：切分段落所屬標籤（維持攤平顯示，不合併，見 library.js
   queueGroups 邏輯——排隊中需要看到每段各自即時進度，不能合併起來） */
.part-of {
  display: inline-block;
  font-size: .72rem; font-weight: 600;
  color: var(--accent); background: #e8f0fb;
  border-radius: 999px; padding: .06rem .5rem;
  margin-left: .3rem;
}

/* ── 播放器控制列 ── */
/* 播放器頁面自己的底部留白：控制區內容較多（4 行 + 可能展開的 A-B 清單），
   蓋過全站共用的 body padding（後者是為 library 頁面的底部固定區設計的）。 */
body.player-page { padding-bottom: env(safe-area-inset-bottom, 0px); }

/* 底部固定控制區：手機拇指操作區，貼底跨滿寬度，蓋過 body 左右 margin。
   內容本身用捲動避免超出視窗（極端情況如展開 A-B 清單時）。 */
.player-bottom-fixed {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  max-width: 720px;
  margin: 0 auto;
  background: var(--bg);
  border-top: 1px solid var(--line);
  /* position:fixed 跳出 body 的文件流，left/right padding 要自己補回來
     （原本在文件流中時天然享有 body 的 1rem 左右留白） */
  padding: .5rem 1rem calc(env(safe-area-inset-bottom, 0px) + .5rem);
  max-height: 70vh;
  overflow-y: auto;
}
.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .4rem;
}
.controls + .controls { margin-top: .4rem; }
/* 工具列按鈕：統一高度、最小寬度，視覺整齊 */
.controls > button {
  height: 40px;
  min-width: 64px;
  padding: 0 .9rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.controls > .back { height: 40px; min-width: 44px; justify-content: center; }
/* 返回鈕 + 標題列：標題佔滿剩餘寬度，過長截斷加 … */
.title-row { flex-wrap: nowrap; }
.title-block {
  flex: 1;
  min-width: 0;            /* 讓 flex 子元素可收縮以觸發 ellipsis */
  display: flex;
  flex-direction: column;
}
/* 標題 + 分段 pill 同一行：pill 用 flex:0 0 auto 固定寬度，不參與標題的
   ellipsis 截斷邏輯，確保標題再長，分段資訊也不會被擠出可視範圍。 */
.title-line { display: flex; align-items: baseline; gap: .4rem; min-width: 0; }
.media-title {
  font-size: 1rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.part-pill {
  flex: 0 0 auto;
  font-size: .72rem;
  font-weight: 600;
  color: var(--accent);
  background: #e8f0fb;
  border-radius: 999px;
  padding: .06rem .5rem;
}
.media-meta {
  font-size: .8rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* 播放控制列：五顆按鈕等寬平均撐滿整行 */
.controls-play > button { flex: 1; }
.controls > button.on { background: #e8f0fb; border-color: var(--accent); color: var(--accent); }
/* 模式/循環/A-B 列：icon 為主，收窄寬度與左右留白讓「＋」同行 */
.controls > button.cycle-btn { min-width: 44px; padding: 0 .5rem; gap: .3rem; }
/* 模式列：按鈕等寬平均撐滿整行，與下方播放五鍵風格一致（不留右側空白） */
.mode-row > button.cycle-btn { flex: 1; }
.cycle-btn svg { flex-shrink: 0; }
/* 播放控制 icon 按鈕：弧形箭頭中央嵌「3」秒數 */
.controls > button.icon-btn { position: relative; min-width: 52px; }
.icon-btn .seek-num {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -38%);
  font-size: 9px;
  font-weight: 600;
  line-height: 1;
  pointer-events: none;
}
.controls .divider { width: 1px; height: 24px; background: var(--line); margin: 0 .2rem; }
/* 速度調整區：-/+/輸入框撐滿整行，與上方播放控制五鍵風格一致；
   仍是獨立按鈕組（非橫向滑桿），避免被誤認成播放進度條（見對話記錄） */
.speed-panel { flex-wrap: nowrap; }
.speed-label { font-size: .85rem; color: var(--muted); margin-right: .2rem; white-space: nowrap; flex-shrink: 0; }
.speed-step {
  flex: 1;
  height: 40px;
  padding: 0;
  font-size: 1.1rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.speed-input {
  flex: 1;
  height: 40px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.controls .back {
  display: inline-flex;
  align-items: center;
  padding: .4rem .7rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 1.1rem;
  line-height: 1;
}
.controls .back:hover { background: #f0f0ec; text-decoration: none; }

.ab-bar { margin: .8rem 0; display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }
.ab-list {
  list-style: none;
  padding: .4rem 0 0;
  margin: .4rem 0 0;
  display: grid;
  gap: .3rem;
  /* 固定約 2.5 段高（一段 li 36px + gap .3rem≈5px）：超過則清單內捲動 */
  max-height: 107px;
  overflow-y: auto;
  border-top: 1px solid var(--line);
}
.ab-list li { display: flex; align-items: center; gap: .5rem; border-radius: 6px; padding: 0 .3rem; }
.ab-list li.cur { background: #e8f0fb; }
.ab-list li button:first-child { width: 36px; height: 36px; padding: 0; }

/* ── 雙字幕 ── */
/* 底部留白貼合固定控制區的實際高度（由 player.js 用 ResizeObserver 動態
   量測寫入 --player-bar-h，隨標題換行／A-B 清單展開收合即時更新）；
   16px fallback 避免 JS 尚未執行時完全無留白。 */
.subs { margin-top: 1rem; margin-bottom: calc(var(--player-bar-h, 16px) + 1rem); }
.seg {
  padding: .7rem .9rem;
  border-radius: var(--radius);
  margin: .5rem 0;
  transition: background .15s;
}
.seg.active { background: var(--card); box-shadow: 0 1px 4px rgba(0,0,0,.06); }
.seg.focus { padding: 2rem 1rem; text-align: center; }

.en {
  margin: 0 0 .25rem;
  font-size: 1.15rem;
  display: flex;            /* flex + wrap：強制可折行，不依賴 inline 折行 */
  flex-wrap: wrap;
  gap: .1em .28em;          /* 行距 字距 */
}
.seg.focus .en { font-size: 1.45rem; line-height: 1.5; justify-content: center; }

.word {
  cursor: pointer;
  padding: .05em .12em;
  border-radius: 4px;
  transition: background .1s;
}
.word:hover { background: #eef; }
.word.hl { background: var(--hl); }
.word.pick { background: var(--pick); }

.zh { margin: 0; color: var(--zh); font-size: .95rem; }
.seg.focus .zh { font-size: 1.2rem; }

/* 字幕編輯表 */
table { width: 100%; border-collapse: collapse; font-size: .85rem; }
th, td { text-align: left; padding: .3rem .5rem; border-bottom: 1px solid var(--line); }

/* 就地按句編輯 */
.seg-head { position: relative; }
.seg-meta { font-size: .8rem; color: var(--muted); margin: 0 42px .3rem 0; }  /* 句索引＋開始時間 */
.seg-head .en { margin-right: 42px; }  /* 留空給右上角按鈕，不被撐高 */
.seg-tools {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  gap: .25rem;
}
.edit-btn { width: 30px; height: 30px; color: var(--muted); }
.edit-btn.ab-quick { font-size: .7rem; font-weight: 600; }
/* 電腦（可 hover）：按鈕平常隱藏，hover 該句或當前句才浮現，避免相鄰句按鈕擠成一條 */
@media (hover: hover) {
  .seg-tools { opacity: 0; transition: opacity .15s; pointer-events: none; }
  .seg:hover .seg-tools, .seg.active .seg-tools { opacity: 1; pointer-events: auto; }
}
/* 手機（無 hover）：按鈕一律常顯，每句都能操作 */
@media (hover: none) {
  .seg-tools { opacity: 1; }
}
.seg-edit {
  margin-top: .5rem;
  padding: .6rem;
  background: #f5f5f1;
  border-radius: 8px;
}
.seg-edit .fld { display: block; font-size: .8rem; color: var(--muted); margin-bottom: .5rem; }
.seg-edit .fld input { display: block; width: 100%; margin-top: .2rem; }

.edit-hint { color: var(--muted); font-size: .8rem; margin: .5rem 0; }
.edit-table td { padding: .2rem .3rem; }
.edit-table input[type="text"] { width: 100%; }
.edit-table input[type="number"] { width: 5rem; }
.edit-table .savemark { color: var(--zh); width: 1.5rem; text-align: center; }
.edit-table tr.justsaved { background: #e6f7ef; transition: background .3s; }
