:root {
  --bg: #faf8f5;
  --surface: #ffffff;
  --surface-2: #f2efe9;
  --text: #1b1a18;
  --text-dim: #6b665e;
  --line: #e2ddd3;
  --accent: #2f6f6a;
  --accent-soft: #e3f0ee;
  --accent-strong: #235550;
  --play: #1f7a4d;
  --play-soft: #e2f4e9;
  --warn: #b45309;
  --warn-soft: #fdf1e0;
  --danger: #b3261e;
  --radius: 14px;
  --bar-h: 128px;
  --font-ui: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN",
    "Yu Gothic", Meiryo, "Noto Sans JP", "Helvetica Neue", Arial, sans-serif;
  --font-reading: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181a;
    --surface: #1e2124;
    --surface-2: #262a2e;
    --text: #eceef0;
    --text-dim: #a2a8ae;
    --line: #343a40;
    --accent: #6fc7bd;
    --accent-soft: #21322f;
    --accent-strong: #8fd8cf;
    --play: #67d29b;
    --play-soft: #1d2f26;
    --warn: #f0b45f;
    --warn-soft: #322818;
    --danger: #ff8a80;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  -webkit-text-size-adjust: 100%;
}

body {
  padding-bottom: calc(var(--bar-h) + env(safe-area-inset-bottom, 0px));
  overscroll-behavior-y: contain;
}

body.no-player { padding-bottom: env(safe-area-inset-bottom, 0px); }

a { color: var(--accent); }

button, select, input, textarea {
  font-family: inherit;
  font-size: 16px; /* 小于 16px 时 iOS 会自动放大页面 */
  color: inherit;
}

/* ------------------------------------------------------------------ 顶栏 */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--line);
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 17px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar .sub {
  display: block;
  font-size: 12px;
  font-weight: 400;
  color: var(--text-dim);
  margin-top: 2px;
}

.icon-btn {
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  padding: 0 12px;
  cursor: pointer;
}

.icon-btn:active { transform: scale(0.97); }
.icon-btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.icon-btn.ghost { background: transparent; }

main { max-width: 820px; margin: 0 auto; padding: 14px 14px 40px; }

/* ------------------------------------------------------------------ 卡片 */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}

.course-title { margin: 0 0 4px; font-size: 18px; }
.muted { color: var(--text-dim); font-size: 13px; }

.lesson-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 4px;
  border-bottom: 1px solid var(--line);
  text-decoration: none;
  color: inherit;
}
.lesson-row:last-child { border-bottom: none; }
.lesson-no {
  flex: 0 0 auto;
  min-width: 46px;
  height: 46px;
  border-radius: 12px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
}
.lesson-main { flex: 1; min-width: 0; }
.lesson-main strong { display: block; font-size: 16px; }
.lesson-main .muted { margin-top: 3px; }

.progress-ring { flex: 0 0 auto; font-size: 12px; color: var(--text-dim); text-align: right; }

/* -------------------------------------------------------------- 点读页面 */

.section-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin: 22px 0 8px;
  padding: 0 4px;
}
.section-head h2 { margin: 0; font-size: 15px; letter-spacing: 0.04em; color: var(--text-dim); font-weight: 600; }
.kind-chip {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-dim);
}

ol.sentences { list-style: none; margin: 0; padding: 0; }

.sentence {
  position: relative;
  display: block;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  margin-bottom: 10px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.08s ease;
  /* 滚动定位时给顶栏和底部播放栏留出空间，避免被遮挡 */
  scroll-margin-top: 80px;
  scroll-margin-bottom: calc(var(--bar-h) + 24px);
}
.sentence:active { transform: scale(0.995); }

.sentence-main {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 14px;
}

.editor {
  border-top: 1px dashed var(--line);
  padding: 12px 14px 14px;
  cursor: default;
}
.editor .mini.primary { background: var(--accent); border-color: var(--accent); color: #fff; }

.sentence .role {
  flex: 0 0 auto;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent-strong);
  background: var(--accent-soft);
  border-radius: 8px;
  padding: 3px 7px;
  margin-top: 4px;
}

.sentence .text {
  flex: 1;
  font-family: var(--font-reading);
  font-size: 1.25rem;
  line-height: 1.95;
  letter-spacing: 0.01em;
  word-break: normal;
  overflow-wrap: anywhere;
}

.sentence .meta {
  display: block;
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 6px;
  letter-spacing: 0;
  line-height: 1.6;
}

.sentence .badge {
  display: inline-block;
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 6px;
  margin-right: 6px;
}
.badge.missing { background: var(--warn-soft); color: var(--warn); }
.badge.stale { background: var(--warn-soft); color: var(--warn); }

.sentence.playing {
  background: var(--play-soft);
  border-color: var(--play);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--play) 22%, transparent);
}
.sentence.playing::before {
  content: "";
  position: absolute;
  left: 0; top: 10px; bottom: 10px;
  width: 4px;
  border-radius: 0 4px 4px 0;
  background: var(--play);
}
.sentence.loading { border-color: var(--play); }

.sentence .play-dot {
  flex: 0 0 auto;
  width: 30px; height: 30px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
  color: var(--text-dim);
  margin-top: 2px;
}
.sentence.playing .play-dot { background: var(--play); border-color: var(--play); color: #fff; }

/* 跟读倒计时条 */
.gap-bar {
  position: absolute;
  left: 16px; right: 16px; bottom: 6px;
  height: 3px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}
.gap-bar > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width 0.1s linear; }

/* ------------------------------------------------------------ 底部控制栏 */

.player {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom, 0px));
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: saturate(180%) blur(16px);
  border-top: 1px solid var(--line);
}

.player-row { display: flex; align-items: center; gap: 8px; max-width: 820px; margin: 0 auto; }
.player-row + .player-row { margin-top: 8px; }

.btn-round {
  flex: 0 0 auto;
  width: 48px; height: 48px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  cursor: pointer;
}
.btn-round.main {
  width: 60px; height: 60px;
  background: var(--play);
  border-color: var(--play);
  color: #fff;
  font-size: 24px;
}
.btn-round:active { transform: scale(0.95); }
.btn-round[disabled] { opacity: 0.35; }

.player-info { flex: 1; min-width: 0; }
.player-info .title { font-size: 13px; font-weight: 600; }
.player-info .sub { font-size: 12px; color: var(--text-dim); }

.seg {
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  border-radius: 10px;
  padding: 3px;
}
.seg button {
  flex: 1;
  border: none;
  background: transparent;
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  color: var(--text-dim);
  cursor: pointer;
  min-height: 36px;
}
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }

.field { display: flex; flex-direction: column; gap: 4px; }
.field label { font-size: 11px; color: var(--text-dim); }

select, input[type="text"], textarea {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 10px;
  padding: 10px 12px;
  min-height: 44px;
  width: 100%;
}

textarea { min-height: 180px; line-height: 1.9; font-family: var(--font-reading); font-size: 16px; }

.chip-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; }

.banner {
  margin: 0 0 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--warn-soft);
  color: var(--warn);
  font-size: 13px;
  line-height: 1.6;
}
.banner.err { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.banner.info { background: var(--accent-soft); color: var(--accent-strong); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--bar-h) + 16px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  max-width: 90vw;
  padding: 10px 16px;
  border-radius: 999px;
  background: rgba(20,20,20,0.92);
  color: #fff;
  font-size: 13px;
  z-index: 60;
  opacity: 0;
  transition: opacity 0.2s ease;
  pointer-events: none;
}
.toast.show { opacity: 1; }

/* ------------------------------------------------------------- 管理/导入 */

.ocr-line {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 8px 0;
  border-bottom: 1px dashed var(--line);
}
.ocr-line textarea { min-height: 48px; flex: 1; }
.ocr-line.low { background: var(--warn-soft); border-radius: 8px; }
.ocr-line .conf { flex: 0 0 46px; font-size: 11px; color: var(--text-dim); padding-top: 12px; text-align: right; }

.sentence-edit { display: flex; gap: 8px; align-items: flex-start; }
.sentence-edit textarea { min-height: 60px; }

.row-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.mini {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 9px;
  padding: 7px 10px;
  font-size: 12px;
  min-height: 36px;
  cursor: pointer;
}
.mini.danger { color: var(--danger); }

.admin-tag {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.steps { display: flex; gap: 6px; margin-bottom: 12px; flex-wrap: wrap; }
.steps span {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-dim);
}
.steps span.on { background: var(--accent); color: #fff; }

table.plain { width: 100%; border-collapse: collapse; font-size: 13px; }
table.plain td, table.plain th { padding: 6px 4px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }

.hidden { display: none !important; }

/* iPhone 竖屏：默认收起播放设置，把垂直空间留给课文 */
@media (max-width: 640px) {
  #settingsRow.collapsed { display: none; }
}

/* 句间停顿进度条只在播放/跟读时出现，平时不占视觉注意力 */
.gap-bar { opacity: 0; transition: opacity 0.2s ease; }
.sentence.playing .gap-bar { opacity: 1; }

@media (min-width: 700px) {
  :root { --bar-h: 116px; }
  .sentence .text { font-size: 1.35rem; }
  main { padding: 20px 20px 48px; }
}

/* 句子的中文翻译：比正文小一档、颜色淡一些，不抢日文的注意力 */
.sentence .translation {
  display: block;
  font-family: var(--font-ui);
  font-size: 13.5px;
  line-height: 1.7;
  color: var(--text-dim);
  margin-top: 6px;
  letter-spacing: 0;
}

/* 矢量图标：确保在任何按钮里都水平垂直居中 */
.btn-round svg, .icon-btn svg { display: block; }
.btn-round { line-height: 0; }
