:root {
  --bg: #f6f7f9; --panel: #ffffff; --text: #111827; --muted: #6b7280;
  --them: #ffffff; --me: #e8f0fe; --accent: #2563eb; --accent-text: #fff;
  --border: #e5e7eb; --mark: #fde68a; --error: #b91c1c; --unsure: #d97706;
  --zh: #0f172a; --en: #1254bf;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120; --panel: #111827; --text: #e5e7eb; --muted: #9ca3af;
    --them: #1f2937; --me: #1e3a5f; --accent: #3b82f6; --border: #374151;
    --mark: #854d0e; --error: #f87171; --unsure: #fbbf24; --zh: #f9fafb; --en: #87ccff;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue", sans-serif;
  -webkit-text-size-adjust: 100%; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--error); min-height: 1.2em; }
button { font: inherit; cursor: pointer; border: 0; border-radius: 12px; }
button.primary { background: var(--accent); color: var(--accent-text); padding: 12px 20px; font-weight: 600; }
button.ghost { background: transparent; color: var(--muted); padding: 8px 12px; }
button.big { flex: 1; font-size: 20px; padding: 16px; }
button.big.stop { background: #dc2626; }

.screen { height: 100dvh; display: flex; flex-direction: column; }
.login-box { margin: auto; width: min(360px, 90vw); text-align: center; }
.login-box h1 { margin-bottom: 4px; }
.login-box input { width: 100%; font-size: 18px; padding: 12px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text); margin: 16px 0 12px; }
.login-box button { width: 100%; }

header { display: flex; align-items: center; gap: 10px; padding: 10px 14px;
  padding-top: max(10px, env(safe-area-inset-top)); background: var(--panel); border-bottom: 1px solid var(--border); }
.brand { font-weight: 700; display: flex; align-items: center; gap: 8px; }
.brand img { display: block; }
.login-logo { display: block; margin: 0 auto 8px; }
header select { font: inherit; padding: 6px 8px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); }
.status { margin-left: auto; display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--muted); }
.dot { width: 9px; height: 9px; border-radius: 50%; background: #9ca3af; }
.status.live .dot { background: #16a34a; animation: pulse 1.6s infinite; }
.status.warn .dot { background: #f59e0b; }
.status.err .dot { background: #dc2626; }
@keyframes pulse { 50% { opacity: .35; } }

main { flex: 1; overflow-y: auto; padding: 12px 12px 4px; -webkit-overflow-scrolling: touch; }
.hint { color: var(--text); background: var(--panel); border: 1px dashed var(--border);
  border-radius: 14px; padding: 6px 16px; line-height: 1.55; font-size: 15px; }

.utt { margin: 0 0 10px; max-width: 92%; padding: 10px 12px; border-radius: 16px;
  background: var(--them); border: 1px solid var(--border); }
.utt.me { margin-left: auto; background: var(--me); max-width: 85%; }
.utt .tag { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px;
  background: var(--bg); color: var(--muted); border: 1px solid var(--border); margin-bottom: 4px; }
.utt.me .tag { color: var(--accent); }
.utt .en { font-size: 15px; color: var(--en); line-height: 1.4; }
.utt.me .en { color: var(--text); font-size: 16px; }
.utt .zh { font-size: 23px; font-weight: 650; color: var(--zh); line-height: 1.4; margin-top: 4px; }
.utt.me .zh { font-size: 14px; font-weight: 400; color: var(--muted); }
.utt .pending { opacity: .55; }
.utt .unsure { text-decoration: underline dotted var(--unsure); text-underline-offset: 3px; }
.utt .zh:empty { display: none; }
mark { background: var(--mark); color: inherit; border-radius: 4px; padding: 0 2px; }

#suggest { background: var(--panel); border-top: 1px solid var(--border); padding: 8px 12px; max-height: 40dvh; overflow-y: auto; }
.suggest-head { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; }
.suggest-head #suggest-meta { margin-left: auto; font-weight: 400; font-size: 12px; }
.sug { display: block; width: 100%; text-align: left; background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px; padding: 9px 12px; margin-top: 7px; }
.sug .en { font-size: 18px; font-weight: 600; line-height: 1.35; }
.sug .zh { font-size: 14px; color: var(--muted); margin-top: 2px; }
.sug.speaking { border-color: var(--accent); }
.sug.skeleton { opacity: .5; }

#phrases { display: flex; gap: 8px; overflow-x: auto; padding: 8px 12px; background: var(--panel);
  border-top: 1px solid var(--border); scrollbar-width: none; }
#phrases::-webkit-scrollbar { display: none; }
#phrases button { flex: none; background: var(--bg); color: var(--text); border: 1px solid var(--border);
  padding: 8px 12px; border-radius: 999px; font-size: 14px; }

footer { display: flex; align-items: center; gap: 10px; padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom)); background: var(--panel); position: relative; }
.level { position: absolute; left: 0; right: 0; top: 0; height: 3px; }
#level-bar { height: 100%; width: 0; background: #16a34a; transition: width .1s linear; }

.toast { position: fixed; left: 50%; bottom: 140px; transform: translateX(-50%); background: #111827;
  color: #fff; padding: 10px 14px; border-radius: 12px; font-size: 14px; max-width: 90vw; z-index: 10; }

/* ---------- modes ---------- */
.modes { display: flex; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 2px; }
.modes button { background: transparent; color: var(--muted); padding: 5px 10px; border-radius: 8px; font-size: 14px; }
.modes button.on { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.topic { background: var(--bg); color: var(--text); border: 1px solid var(--border); padding: 5px 10px;
  border-radius: 10px; font-size: 14px; max-width: 34vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 520px) { .brand { display: none; } header { gap: 8px; padding-left: 10px; padding-right: 10px; } }
.hint .lecture-tip { display: none; }
main.lecture .hint .lecture-tip { display: block; }

/* ---------- lecture view ---------- */
main.lecture { --lecture-zh: 24px; }
.para { margin: 0 0 14px; padding: 2px 4px 12px; border-bottom: 1px solid var(--border); }
.para .who { font-size: 12px; color: var(--accent); margin-bottom: 2px; }
.para .zh { font-size: var(--lecture-zh); font-weight: 600; line-height: 1.5; color: var(--zh); }
.para .en { font-size: calc(var(--lecture-zh) * .62); color: var(--en); line-height: 1.45; margin-top: 4px; }
.para .pending { opacity: .55; }
.para .zh:empty { display: none; }
main.zh-only .para .en { display: none; }
#lecture-bar { display: flex; gap: 8px; padding: 8px 12px; background: var(--panel); border-top: 1px solid var(--border); }
#lecture-bar button { background: var(--bg); color: var(--text); border: 1px solid var(--border); padding: 8px 12px;
  border-radius: 999px; font-size: 14px; }
#lecture-bar button.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
#lecture-bar #export { margin-left: auto; }
.jump { position: fixed; right: 16px; bottom: 170px; background: var(--accent); color: var(--accent-text);
  padding: 8px 14px; border-radius: 999px; font-size: 14px; box-shadow: 0 2px 8px rgba(0,0,0,.25); z-index: 5; }

/* ---------- terms dialog ---------- */
dialog { width: min(520px, 92vw); border: 1px solid var(--border); border-radius: 16px; padding: 16px;
  background: var(--panel); color: var(--text); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h3 { margin: 0 0 6px; }
dialog .small { font-size: 13px; margin: 0 0 10px; line-height: 1.5; }
dialog textarea { width: 100%; font: 16px/1.5 ui-monospace, Menlo, monospace; padding: 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text); resize: vertical; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

/* ---------- languages, my bubbles, toggles ---------- */
.utt-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.utt .say { background: var(--bg); border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px;
  font-size: 14px; color: var(--accent); }
.utt.me .big { font-size: 21px; font-weight: 650; color: var(--text); line-height: 1.4; margin-top: 4px; }
.utt.me .small { font-size: 14px; color: var(--muted); line-height: 1.4; margin-top: 2px; }
.utt .big:empty, .utt .small:empty { display: none; }
[dir="auto"]:dir(rtl) { text-align: right; }
.lang-manage { margin: 8px 0; font-size: 14px; }
.lang-manage summary { cursor: pointer; color: var(--accent); padding: 4px 0; }
#lang-list { max-height: 38dvh; overflow-y: auto; border: 1px solid var(--border); border-radius: 10px; padding: 6px 10px; }
.lang-item { display: block; padding: 5px 0; }
span.tag { cursor: default; }
#phrases button.toggle { color: var(--muted); }
#phrases button.toggle.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
dialog .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 10px 0; font-size: 16px; }
dialog .row select { font: inherit; padding: 6px 8px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); }
dialog .row.check { justify-content: flex-start; font-size: 14px; }

/* ---------- caption menu & ask ChatGPT ---------- */
.utt, .para { position: relative; -webkit-touch-callout: none; }
.more { background: transparent; color: var(--muted); border: 0; padding: 0 6px; font-size: 18px; line-height: 1; }
.para .more { position: absolute; top: 0; right: 0; }
.utt .utt-head .more { margin-left: auto; }
.utt.pressed, .para.pressed { outline: 2px solid var(--accent); outline-offset: 2px; }
dialog.menu { width: min(360px, 92vw); padding: 10px; }
dialog.menu form { display: flex; flex-direction: column; gap: 6px; }
dialog.menu button { text-align: left; padding: 12px; background: var(--bg); color: var(--text); border: 1px solid var(--border); }
dialog.menu button.ask { color: var(--accent); font-weight: 600; }
dialog.menu button.ghost { text-align: center; border: 0; background: transparent; color: var(--muted); }
.menu-preview { margin: 2px 4px 6px; font-size: 14px; color: var(--muted); max-height: 4.5em; overflow: hidden; }
dialog.ask-dialog { width: min(640px, 100vw); height: min(88dvh, 900px); max-height: 100dvh; padding: 0;
  display: none; flex-direction: column; }
dialog.ask-dialog[open] { display: flex; }
.ask-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.ask-body { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 12px; }
.ask-quote { background: var(--bg); border: 1px solid var(--border); border-radius: 14px; padding: 10px 12px; }
.ask-quote .o { color: var(--en); font-weight: 600; }
.ask-quote .t { color: var(--muted); font-size: 14px; margin-top: 2px; }
.ask-q { align-self: flex-end; background: var(--me); border-radius: 12px; padding: 8px 12px; max-width: 85%; }
.ask-a { line-height: 1.6; }
.ask-a.error { color: var(--error); }
.ask-a ul { margin: 4px 0; padding-left: 20px; }
.ask-a p { margin: 4px 0; }
.ask-actions { display: flex; gap: 8px; margin-top: 6px; }
.ask-actions button { background: var(--bg); border: 1px solid var(--border); color: var(--text); padding: 4px 10px; font-size: 13px; border-radius: 999px; }
.ask-form { display: flex; gap: 8px; padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); border-top: 1px solid var(--border); }
.ask-form textarea { flex: 1; font: inherit; font-size: 16px; padding: 8px 10px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); resize: none; max-height: 30vh; }
.utt .utt-head .say { margin-left: auto; }
.utt .utt-head .say + .more { margin-left: 4px; }

/* ---------- header on narrow phones: wrap instead of squeezing ---------- */
header { flex-wrap: wrap; row-gap: 6px; }
.modes button, .topic, header select, .status { white-space: nowrap; }
@media (max-width: 520px) {
  .modes button { padding: 5px 8px; }
  .topic { max-width: 42vw; }
  header select { padding: 5px 6px; }
}

/* ---------- typed text ---------- */
.type-bar { display: flex; gap: 8px; padding: 8px 12px; background: var(--panel); border-top: 1px solid var(--border); }
.type-bar textarea { flex: 1; font: inherit; font-size: 16px; padding: 8px 10px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--bg); color: var(--text); resize: none; max-height: 30vh; }
.type-bar button { padding: 8px 16px; }
.utt.typed { border-style: dashed; }
.utt.typed .big, .utt.typed .small { white-space: pre-wrap; }
.utt .tag.kbd { font-size: 12px; }
