:root {
  --paper: #f6f1e7;
  --card: #fffdf8;
  --ink: #1f1b16;
  --muted: #6f665a;
  --line: #e3d9c8;
  --red: #b8322a;
  --red-soft: #f6e3dc;
  --jade: #2f6f5e;
  --t1: #c0392b; --t2: #2e8b57; --t3: #2f5fb3; --t4: #8e44ad; --t0: #8a8278;
  --zh: "PingFang SC", "Hiragino Sans GB", "Noto Sans SC", "Microsoft YaHei", sans-serif;
  --serif: "Songti SC", "Noto Serif SC", "STSong", Georgia, serif;
  --ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --radius: 14px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--paper); color: var(--ink); font: 16px/1.5 var(--ui); }
a { color: inherit; }
button, select, input, textarea { font: inherit; color: inherit; }

.top { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 20px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: rgba(246,241,231,.94); backdrop-filter: blur(8px); z-index: 10; }
.brand { display: flex; gap: 10px; align-items: center; text-decoration: none; }
.brand b { font-family: var(--serif); font-size: 19px; letter-spacing: .04em; display: block; line-height: 1.1; }
.brand small { color: var(--muted); font-size: 12px; letter-spacing: .08em; text-transform: uppercase; }
.seal { width: 36px; height: 36px; border-radius: 9px; background: var(--red); color: #fff; display: grid; place-items: center; font-family: var(--serif); font-size: 22px; }
.top nav { display: flex; gap: 16px; font-size: 14px; }
.top nav a { text-decoration: none; color: var(--muted); }
.top nav a:hover { color: var(--ink); }

main { max-width: 760px; margin: 0 auto; padding: 24px 16px 140px; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; letter-spacing: .01em; }
h1 { font-size: 26px; margin: 0 0 4px; }
h2 { font-size: 19px; margin: 32px 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
textarea { width: 100%; min-height: 170px; resize: vertical; border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: #fff; font-family: var(--zh); font-size: 16px; line-height: 1.6; }
textarea:focus, select:focus, input:focus { outline: 2px solid var(--red-soft); border-color: var(--red); }
select, input[type=text], input[type=password] { border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; background: #fff; max-width: 100%; }
label.field { display: block; margin: 14px 0 6px; font-weight: 600; font-size: 14px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.between { justify-content: space-between; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; padding: 3px; background: #fff; }
.seg button { border: 0; background: transparent; padding: 7px 14px; border-radius: 999px; cursor: pointer; font-size: 14px; }
.seg button[aria-pressed=true] { background: var(--ink); color: #fff; }

.btn { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 9px 16px; cursor: pointer; font-size: 14px; display: inline-flex; gap: 6px; align-items: center; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--red); border-color: var(--red); color: #fff; font-weight: 600; }
.btn.primary:hover { filter: brightness(1.07); }
.btn:disabled { opacity: .55; cursor: progress; }
.btn.ghost { background: transparent; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 5px 11px; font-size: 13px; cursor: pointer; }
.chip[aria-pressed=true] { background: var(--jade); border-color: var(--jade); color: #fff; }

.lessons { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; }
.lessons li a { display: flex; justify-content: space-between; gap: 12px; padding: 14px 16px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; text-decoration: none; }
.lessons li a:hover { border-color: var(--muted); }
.lessons .t { font-weight: 600; }
.empty { text-align: center; padding: 28px; color: var(--muted); border: 1px dashed var(--line); border-radius: 12px; }

.lesson-head { margin-bottom: 14px; }
.cast { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.cast span { font-size: 13px; background: #fff; border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; }
.cast span.B { background: var(--red-soft); border-color: #ecc9bd; }

.toolbar { display: flex; flex-wrap: wrap; gap: 6px; margin: 14px 0 18px; }

.lines { display: flex; flex-direction: column; gap: 14px; }
.line { display: flex; flex-direction: column; max-width: 88%; }
.line.B { align-self: flex-end; align-items: flex-end; }
.line.N { max-width: 100%; }
.who { font-size: 12px; color: var(--muted); margin: 0 6px 3px; }
.bubble { background: var(--card); border: 1px solid var(--line); border-radius: 18px 18px 18px 6px; padding: 12px 14px 10px; position: relative; transition: box-shadow .2s, border-color .2s, transform .2s; }
.line.B .bubble { background: var(--red-soft); border-color: #ecc9bd; border-radius: 18px 18px 6px 18px; }
.line.N .bubble { border-radius: 12px; }
.line.active .bubble { border-color: var(--red); box-shadow: 0 0 0 3px rgba(184,50,42,.15); transform: translateY(-1px); }
.line.yourturn .bubble { border-style: dashed; border-color: var(--jade); box-shadow: 0 0 0 3px rgba(47,111,94,.15); }
.zh { font-family: var(--zh); font-size: 22px; line-height: 1.9; word-break: break-all; }
.zh ruby { ruby-align: center; }
.zh rt { font-family: var(--ui); font-size: 11px; color: var(--muted); letter-spacing: 0; }
body:not(.show-pinyin) .zh rt { display: none; }
body.tones .zh .t1 { color: var(--t1); } body.tones .zh .t2 { color: var(--t2); }
body.tones .zh .t3 { color: var(--t3); } body.tones .zh .t4 { color: var(--t4); }
body.tones .zh .t0 { color: var(--t0); }
body.tones .zh rt { color: inherit; opacity: .8; }
body.hide-zh .zh { filter: blur(7px); cursor: pointer; transition: filter .2s; }
body.hide-zh .line.reveal .zh { filter: none; }
.meaning { color: var(--muted); font-size: 14px; margin-top: 2px; }
body:not(.show-meaning) .meaning { display: none; }
.actions { display: flex; gap: 4px; margin-top: 6px; align-items: center; }
.icon { border: 0; background: transparent; cursor: pointer; padding: 4px 6px; border-radius: 8px; color: var(--muted); font-size: 15px; line-height: 1; }
.icon:hover, .icon[aria-pressed=true] { background: rgba(0,0,0,.06); color: var(--ink); }
.delivery { font-size: 12px; color: var(--muted); font-style: italic; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--line); display: inline-block; margin-left: 4px; }
.dot.ready { background: var(--jade); } .dot.loading { background: #d9a441; animation: pulse 1s infinite; } .dot.err { background: var(--red); }
@keyframes pulse { 50% { opacity: .3; } }
.turn-tag { font-size: 12px; color: var(--jade); font-weight: 600; }

.phrases { display: grid; gap: 10px; }
.phrase { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; align-items: start; }
.phrase .pzh { font-family: var(--zh); font-size: 20px; }
.phrase .pp { color: var(--jade); font-size: 13px; }
.phrase .note { grid-column: 2; font-size: 13px; color: var(--muted); }

.player { position: fixed; left: 0; right: 0; bottom: 0; background: rgba(31,27,22,.96); color: #fff; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); z-index: 20; }
.player .inner { max-width: 760px; margin: 0 auto; display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.player .play { width: 46px; height: 46px; border-radius: 50%; border: 0; background: var(--red); color: #fff; font-size: 18px; cursor: pointer; }
.player .status { flex: 1; min-width: 120px; font-size: 13px; opacity: .85; }
.player .seg { background: transparent; border-color: rgba(255,255,255,.25); }
.player .seg button { color: #fff; padding: 5px 10px; font-size: 13px; }
.player .seg button[aria-pressed=true] { background: #fff; color: var(--ink); }

.banner { background: #fff4e0; border: 1px solid #f0d49b; border-radius: 12px; padding: 12px 14px; font-size: 14px; margin-bottom: 14px; }
.banner.err { background: #fde9e6; border-color: #f0b9b0; }
.engine { display: block; border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; margin-bottom: 8px; cursor: pointer; background: #fff; }
.engine:has(input:checked) { border-color: var(--red); box-shadow: 0 0 0 3px rgba(184,50,42,.12); }
.engine input { margin-right: 8px; }
.spinner { width: 16px; height: 16px; border: 2px solid rgba(255,255,255,.4); border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

#toast { position: fixed; left: 50%; top: 76px; transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .25s; z-index: 30; max-width: 90vw; }
#toast.on { opacity: 1; }

@media (max-width: 560px) {
  .top nav { gap: 10px; font-size: 13px; }
  .brand small { display: none; }
  .zh { font-size: 20px; }
  .line { max-width: 94%; }
}

/* ---- cost preview & confirm dialogs ---- */
.cost-preview { margin-top: 16px; padding: 10px 12px; border-radius: 10px; background: #f3efe4; border: 1px solid var(--line); font-size: 13px; color: var(--muted); line-height: 1.6; }
.cost-preview b { color: var(--ink); }
.audio-bar { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; font-size: 14px; margin-bottom: 6px; }
.btn.small { padding: 6px 12px; font-size: 13px; }
dialog#modal { border: 1px solid var(--line); border-radius: 16px; padding: 20px; max-width: min(440px, 92vw); background: var(--card); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog#modal::backdrop { background: rgba(31,27,22,.45); }
dialog#modal h3 { margin: 0 0 8px; font-size: 19px; }
.modal-body p { margin: 8px 0; }
.modal-actions { justify-content: flex-end; margin-top: 16px; }
.cost-big { font-size: 26px; font-weight: 700; font-family: var(--serif); margin: 6px 0 !important; }
.warn { background: #fff4e0; border: 1px solid #f0d49b; border-radius: 10px; padding: 8px 10px; font-size: 14px; }

/* ---- live captions (karaoke) ---- */
.line.active .zh .tok { opacity: .38; transition: opacity .12s, background-color .12s; }
.line.active .zh .tok.lit { opacity: 1; }
.zh .tok.now { background: rgba(184,50,42,.14); border-radius: 4px; box-shadow: 0 2px 0 var(--red); }

/* ---- progress bar (top) ---- */
#busy { position: fixed; left: 0; right: 0; top: 64px; z-index: 40; background: var(--card); border-bottom: 1px solid var(--line); box-shadow: 0 4px 14px rgba(0,0,0,.08); }
#busy .busy-inner { max-width: 760px; margin: 0 auto; padding: 10px 16px 6px; display: flex; gap: 10px; align-items: center; font-size: 14px; }
#busy .busy-label { font-weight: 600; flex: 1; }
#busy .busy-time { color: var(--muted); font-size: 12px; }
#busy .bar { height: 4px; background: var(--red-soft); overflow: hidden; }
#busy .bar i { display: block; height: 100%; background: var(--red); transition: width .3s; }
#busy .bar.indet i { width: 35%; animation: indet 1.2s ease-in-out infinite; }
@keyframes indet { 0% { transform: translateX(-100%); } 100% { transform: translateX(300%); } }
.spinner.dark { border-color: rgba(0,0,0,.15); border-top-color: var(--red); vertical-align: -3px; }

/* ---- voice cast ---- */
.cast { align-items: center; }
.voice-tag { font-weight: 500; color: var(--jade); margin-left: 4px; }
.role-row { display: flex; gap: 8px; align-items: center; padding: 6px 0; border-top: 1px solid var(--line); flex-wrap: wrap; }
.role-row select { max-width: 220px; }
.grow { flex: 1; min-width: 140px; }
.modal-body.castdlg { max-height: 62vh; overflow: auto; }
dialog#modal { max-width: min(520px, 94vw); }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; margin: 10px 0; color: var(--muted); }
.cost-box { margin-top: 14px; padding: 10px 12px; background: #f3efe4; border-radius: 10px; min-height: 44px; }
.err-text { color: var(--red); font-weight: 600; }
.finder { margin-top: 10px; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: #fff; max-height: 260px; overflow: auto; }
.finder-head { font-size: 14px; margin-bottom: 6px; }
.finder-row { display: flex; gap: 8px; align-items: center; padding: 6px 0; border-top: 1px solid var(--line); }
body.free-voice .paid-only { display: none; }

/* ---- bilingual & library ---- */
.meaning2 { font-size: 13px; opacity: .8; margin-top: 2px; }
details.unit { background: var(--card); border: 1px solid var(--line); border-radius: 12px; margin-bottom: 8px; padding: 4px 12px; }
details.unit summary { cursor: pointer; padding: 8px 0; list-style-position: inside; }
details.unit .lessons { margin: 4px 0 10px; }
details.unit .lessons li a { background: #fff; }
.nowrap { white-space: nowrap; }
#dropZone.drag { outline: 3px dashed var(--jade); outline-offset: 4px; }
