/* ============ 主题变量（默认白色，可切换） ============ */
body {
  --bg: #eef1f6;
  --bg2: #f8fafc;
  --text: #171a21;
  --dim: #6b7280;
  --panel: rgba(255, 255, 255, 0.9);
  --surface: #ffffff;
  --line: rgba(0, 0, 0, 0.08);
  --hl: rgba(59, 130, 246, 0.13);
  --hl-edge: #3b82f6;
  --guide: rgba(0, 0, 0, 0.14);
  --thumb: #c3c8d2;
  --tshadow: none;
  --accent: #2563eb;
  --accent2: #10b981;
}

body[data-theme="black"] {
  --bg: #0d1117; --bg2: #161b24;
  --text: #e6ebf4; --dim: #8b94a7;
  --panel: rgba(17, 22, 31, 0.92); --surface: #171d29;
  --line: rgba(255, 255, 255, 0.09);
  --hl: rgba(79, 140, 255, 0.16); --hl-edge: #4f8cff;
  --guide: rgba(255, 255, 255, 0.14); --thumb: #2a3245;
  --tshadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  --accent: #4f8cff;
}

body[data-theme="warm"] {
  --bg: #f3e7d3; --bg2: #f7efe0;
  --text: #3d2f1e; --dim: #8a7357;
  --panel: rgba(247, 239, 224, 0.92); --surface: #fbf5ea;
  --line: rgba(90, 70, 40, 0.14);
  --hl: rgba(217, 119, 6, 0.16); --hl-edge: #d97706;
  --guide: rgba(90, 70, 40, 0.18); --thumb: #d4c3a8;
  --tshadow: none; --accent: #b45309;
}

body[data-theme="green"] {
  --bg: #dfead6; --bg2: #e9f2e3;
  --text: #22351f; --dim: #5c6f57;
  --panel: rgba(233, 242, 227, 0.92); --surface: #f0f6ec;
  --line: rgba(40, 70, 35, 0.12);
  --hl: rgba(22, 163, 74, 0.14); --hl-edge: #16a34a;
  --guide: rgba(40, 70, 35, 0.16); --thumb: #bfcfb6;
  --tshadow: none; --accent: #15803d;
}

body[data-theme="blue"] {
  --bg: #0f1a2b; --bg2: #15233a;
  --text: #dce5f5; --dim: #7f8ea8;
  --panel: rgba(16, 26, 42, 0.92); --surface: #182741;
  --line: rgba(255, 255, 255, 0.09);
  --hl: rgba(56, 132, 255, 0.18); --hl-edge: #5b9bff;
  --guide: rgba(255, 255, 255, 0.13); --thumb: #2b3a55;
  --tshadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  --accent: #5b9bff;
}

/* ============ 基础 ============ */
* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

body {
  background: linear-gradient(180deg, var(--bg2), var(--bg));
  color: var(--text);
  font-family: system-ui, "Segoe UI", "Microsoft YaHei", "PingFang SC", sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  transition: background 0.35s, color 0.35s;
}

#app { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* ============ 控制栏 ============ */
#controls {
  position: relative;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  align-items: center;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
  transition: opacity 0.25s, transform 0.25s;
}

.group { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.sep { width: 1px; height: 22px; background: var(--line); flex: none; }

#stat { margin-left: auto; color: var(--dim); font-size: 12px; white-space: nowrap; }
.mono { font-variant-numeric: tabular-nums; }

.btn {
  border: 1px solid var(--line);
  background: rgba(127, 127, 127, 0.06);
  color: var(--text);
  padding: 7px 12px;
  border-radius: 9px;
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
  white-space: nowrap;
}
.btn:hover { background: rgba(127, 127, 127, 0.14); border-color: rgba(127, 127, 127, 0.3); }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.accent { border-color: var(--accent); color: var(--accent); }
.btn.icon { min-width: 34px; text-align: center; }

/* 分段按钮 */
.seg {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: rgba(127, 127, 127, 0.05);
}
.seg-btn {
  border: 0;
  background: transparent;
  color: var(--dim);
  padding: 7px 12px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  transition: all 0.15s;
}
.seg-btn + .seg-btn { border-left: 1px solid var(--line); }
.seg-btn:hover { color: var(--text); }
.seg-btn.active { background: var(--accent); color: #fff; }

select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 6px 8px;
  font-size: 13px;
  max-width: 170px;
}
select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

input[type="range"] { accent-color: var(--accent); width: 110px; }

label { color: var(--dim); font-size: 13px; }

/* 手机：控制栏可滚动，首次由脚本默认采用竖屏稿面。 */
@media (max-width: 768px) {
  #controls {
    max-height: 42vh;
    overflow-y: auto;
    gap: 6px 8px;
    padding: 8px;
    -webkit-overflow-scrolling: touch;
  }
  #controls .sep { display: none; }
  #controls .group { gap: 5px; }
  #controls .btn, #controls .seg-btn { padding: 7px 9px; }
  #controls input[type="range"] { width: 82px; }
  #controls select { max-width: 135px; }
  #stat { margin-left: 0; width: 100%; text-align: right; }
  #text { font-size: min(11vw, 56px); padding-left: 5vw; padding-right: 5vw; }
}

/* 主题色块 */
.themes { display: flex; align-items: center; gap: 7px; }
.swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--line);
  cursor: pointer;
  flex: none;
  padding: 0;
  transition: transform 0.1s;
}
.swatch:hover { transform: scale(1.15); }
.swatch.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }

/* ============ 正文区 ============ */
#scroller {
  position: relative;
  z-index: 1;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
  scrollbar-color: var(--thumb) transparent;
}
#scroller::-webkit-scrollbar { width: 10px; }
#scroller::-webkit-scrollbar-thumb { background: var(--thumb); border-radius: 6px; }

#text {
  position: relative;
  max-width: min(90vw, 1080px);
  margin: 0 auto;
  padding: 85vh 6vw 85vh;
  line-height: 1.55;
  font-size: 56px;
  text-align: left;
  text-shadow: var(--tshadow);
  opacity: var(--text-opacity, 1);
}

/* 逐字稿位置：靠左 / 居中（默认）/ 靠右 */
body.pos-left #text { margin-left: 0; margin-right: auto; padding-left: 2vw; padding-right: 2vw; max-width: min(50vw, 900px); }
body.pos-right #text { margin-left: auto; margin-right: 0; padding-left: 2vw; padding-right: 2vw; max-width: min(50vw, 900px); }

/* 竖屏版式：逐字稿收窄为 9:16 竖幅（像手机屏幕），靠左 / 居中 / 靠右可自由切换 */
body.portrait #text { max-width: min(92vw, 58vh); margin: 0 auto; padding-left: 5vw; padding-right: 5vw; }
body.portrait.pos-left #text { margin-left: 0; margin-right: auto; }
body.portrait.pos-right #text { margin-left: auto; margin-right: 0; }

.para {
  margin: 0 0 0.6em;
  white-space: pre-wrap;
  word-break: break-word;
  cursor: default;
}
.para:empty { min-height: 0.5em; }

/* 逐字 span：行内无间隙，仅作行分组与单行高亮载体 */
.ch { border-radius: 3px; }

/* 高亮当前念到位置附近的几个字（开拍风格：小色块跟着念，不亮整行） */
body:not(.no-highlight) .ch.cur {
  background: var(--hl);
}
body:not(.no-highlight) .ch.hl-start {
  border-radius: 5px 0 0 5px;
  box-shadow: inset 3px 0 0 var(--hl-edge);
}
body:not(.no-highlight) .ch.hl-end {
  border-radius: 0 5px 5px 0;
}
body:not(.no-highlight) .ch.hl-start.hl-end {
  border-radius: 5px;
  box-shadow: inset 3px 0 0 var(--hl-edge);
}

/* 镜像 */
body.mirror #text { transform: scaleX(-1); }

/* 阅读参考线（分割线）：高亮块永远对齐到这条线上，可拖动右侧圆点调整位置 */
#guide {
  position: fixed;
  left: 0; right: 0;
  top: var(--anchor, 50%);
  border-top: 2px dashed var(--guide);
  pointer-events: none;
  z-index: 2;
}
#guideHandle {
  position: absolute;
  right: 14px;
  top: -11px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff;
  cursor: ns-resize;
  pointer-events: auto;
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.35);
}
#guideLabel {
  position: absolute;
  left: 14px;
  top: 8px;
  font-size: 11px;
  color: var(--dim);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 2px 8px;
  pointer-events: none;
  white-space: nowrap;
}

/* 底部进度条 */
#progress {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: 3px;
  background: transparent;
  z-index: 3;
  pointer-events: none;
}
#progressFill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.15s linear;
}

/* ============ 语音状态条 ============ */
#voiceBar {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 8px 16px;
  border-radius: 20px;
  background: var(--panel);
  border: 1px solid var(--line);
  backdrop-filter: blur(8px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  max-width: 84vw;
  z-index: 4;
}
#voiceBar.on { opacity: 1; }

#voiceDot { width: 10px; height: 10px; border-radius: 50%; background: var(--dim); flex: none; }
#voiceBar.on #voiceDot { background: var(--accent2); animation: pulse 1.2s infinite; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.6); }
  50% { box-shadow: 0 0 0 8px rgba(16, 185, 129, 0); }
}
#voiceText { color: var(--dim); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 60vw; }

/* ============ 空状态 ============ */
#emptyState {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  text-align: center;
  padding: 24px;
  z-index: 3;
}
#emptyState.hidden { display: none; }
#emptyState .logo { font-size: 56px; }
#emptyState h1 { font-size: 40px; margin: 0; }
#emptyState p { color: var(--dim); margin: 0; max-width: 560px; }
#emptyState .actions { display: flex; gap: 12px; margin-top: 8px; }
#emptyState .actions .btn { font-size: 15px; padding: 12px 20px; }
#emptyState .hint { font-size: 13px; color: var(--dim); opacity: 0.8; }

/* ============ 遮罩 / 弹窗 ============ */
#dropOverlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(13, 17, 26, 0.7);
  border: 2px dashed var(--accent);
  font-size: 30px;
  color: #fff;
  z-index: 8;
  margin: 12px;
  border-radius: 16px;
}
#dropOverlay.show { display: flex; }

#pasteModal, #helpModal {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.4);
  z-index: 10;
  padding: 20px;
  backdrop-filter: blur(3px);
}
#pasteModal.show, #helpModal.show { display: flex; }

#pasteModal .box, #helpModal .box {
  width: min(92vw, 720px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 18px 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
#pasteModal h3, #helpModal h3 { margin: 0 0 12px; }
.urlrow { display: flex; gap: 8px; margin-bottom: 12px; }
.urlrow input {
  flex: 1;
  min-width: 0;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 12px;
  font-size: 14px;
}
.urlrow input:focus { outline: 2px solid var(--accent); }
.divider {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--dim);
  font-size: 12px;
  margin-bottom: 10px;
}
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--line); }
#pasteModal textarea {
  width: 100%;
  height: 42vh;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 12px;
  resize: vertical;
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
}
#pasteModal textarea:focus { outline: 2px solid var(--accent); }
.row { display: flex; align-items: center; gap: 10px; margin-top: 14px; }
.row .spacer { flex: 1; }
#helpModal ul { line-height: 2; color: var(--text); padding-left: 18px; margin: 0 0 6px; }
#helpModal b { color: var(--accent); }
#helpModal .hint { color: var(--dim); font-size: 13px; margin: 4px 0 0; }

/* ============ Toast ============ */
#toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 9px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 20;
  max-width: 80vw;
  backdrop-filter: blur(8px);
}
#toast.show { opacity: 1; }

/* ============ 摄像头画面 ============ */
#cameraVideo {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  z-index: 0;
  background: #000;
  display: none;
  pointer-events: none;
}
#videoDim {
  position: fixed;
  inset: 0;
  z-index: 0;
  background: rgba(0, 0, 0, 0.16);
  display: none;
  pointer-events: none;
}
body.camera-on #cameraVideo,
body.camera-on #videoDim { display: block; }
body.camera-mirror #cameraVideo { transform: scaleX(-1); }

/* 有画面时：文字白色+描边，在任意画面上都清晰可读 */
body.camera-on .para {
  color: #fff;
  -webkit-text-stroke: 0.5px rgba(0, 0, 0, 0.55);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9), 0 0 6px rgba(0, 0, 0, 0.7);
}
body.camera-on #guide { border-top-color: rgba(255, 255, 255, 0.5); }

/* ============ 全屏 ============ */
body.is-fullscreen #controls {
  position: absolute;
  top: 0; left: 0; right: 0;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-100%);
}
body.is-fullscreen.show-controls #controls {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}
