:root {
  --bg: #FAF6EE; --card: #FFFFFF; --ink: #22303C; --muted: #66727F; --line: #E7DFD0;
  --brand: #0E7C66; --brand-ink: #FFFFFF; --brand-soft: #E3F4EF;
  --hl: #FFD84D; --sent: #FFF4CC; --ipa: #A8431C; --stress: #C2410C; --stress-bg: #FFE8D9;
  --chip: #F4EEE2; --warn-bg: #FFF4E0; --warn-ink: #7A4A00; --shadow: 0 1px 2px rgba(60, 45, 20, .06), 0 6px 20px rgba(60, 45, 20, .06);
  --radius: 16px;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, -apple-system, sans-serif;
  --font-ipa: "Charis SIL", "Doulos SIL", "Gentium Plus", "Lucida Grande", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181D; --card: #1D232A; --ink: #EDF0F3; --muted: #9AA6B2; --line: #2D353F;
    --brand: #35C4A5; --brand-ink: #06261F; --brand-soft: #173530;
    --hl: #8A6A00; --sent: #2E2A1A; --ipa: #FFB38A; --stress: #FF9566; --stress-bg: #47271A;
    --chip: #28303A; --warn-bg: #3A2E14; --warn-ink: #FFD791; --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
button, .w, summary { touch-action: manipulation; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 16px/1.5 var(--font); }
button, select, textarea, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.ipa { font-family: var(--font-ipa); color: var(--ipa); font-weight: 500; letter-spacing: .01em; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--brand) 60%, transparent); outline-offset: 2px; }

/* ── Đầu trang ── */
.top { display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center; justify-content: space-between; max-width: 1040px; margin: 0 auto; padding: 20px 16px 8px; padding-top: max(20px, env(safe-area-inset-top)); padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); }
.brand { display: flex; align-items: center; gap: 12px; }
.logo { font-size: 38px; line-height: 1; }
.brand h1 { margin: 0; font-size: 22px; line-height: 1.2; letter-spacing: -.01em; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.prefs { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: center; }
.voice { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.voice select { max-width: 220px; padding: 7px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); }

.seg { display: inline-flex; padding: 3px; gap: 2px; background: var(--chip); border-radius: 12px; }
.seg button { border: 0; background: transparent; padding: 7px 12px; border-radius: 9px; font-size: 14px; font-weight: 600; color: var(--muted); white-space: nowrap; }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

/* ── Khung chung ── */
main { max-width: 1040px; margin: 0 auto; padding: 8px 16px 80px; padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right)); padding-bottom: calc(80px + env(safe-area-inset-bottom)); transition: padding-right .2s; }
.hint { display: flex; gap: 12px; align-items: center; justify-content: space-between; background: var(--brand-soft); font-size: 15px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-top: 14px; }
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 10px; }
.btn { border: 1px solid var(--line); background: var(--card); padding: 9px 16px; border-radius: 12px; font-weight: 650; white-space: nowrap; }
.btn:hover { border-color: color-mix(in srgb, var(--brand) 50%, var(--line)); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.small { padding: 6px 11px; font-size: 14px; border-radius: 10px; }
.btn.icon { padding: 8px 11px; }
.status { font-size: 13.5px; color: var(--muted); margin-left: auto; }
.status.bad { color: var(--warn-ink); background: var(--warn-bg); padding: 4px 10px; border-radius: 8px; }
.status button { border: 0; background: none; text-decoration: underline; color: inherit; padding: 0 0 0 6px; }

textarea#txt { display: block; width: 100%; min-height: 84px; resize: vertical; padding: 12px 14px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--bg); font-size: 18px; line-height: 1.45; }
textarea#txt:focus { outline: none; border-color: var(--brand); }

.lessons { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; padding-top: 12px; border-top: 1px dashed var(--line); font-size: 14px; color: var(--muted); }
.chip { display: inline-flex; align-items: center; background: var(--chip); border-radius: 999px; overflow: hidden; }
.chip button { border: 0; background: transparent; padding: 6px 4px 6px 12px; font-size: 14px; font-weight: 600; max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip button.del { padding: 6px 10px 6px 6px; color: var(--muted); font-weight: 400; }
.chip button.del.sure { color: #fff; background: #C0392B; padding: 6px 10px; }

/* ── Lời chào ── */
.intro { color: var(--muted); }
.intro h2 { margin: 0 0 6px; font-size: 17px; color: var(--ink); }
.intro ol { margin: 0; padding-left: 20px; }
.intro li { margin: 3px 0; }

/* ── Thẻ từ ── */
.wc-head { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; }
.wc-word { font-size: 44px; font-weight: 750; line-height: 1.1; letter-spacing: -.01em; margin-right: auto; overflow-wrap: anywhere; }
.panel .wc-word { font-size: 34px; }
.wc-btns { display: flex; gap: 8px; }
.wc-ipas { margin-top: 12px; display: grid; gap: 6px; }
.ipa-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.ipa-row .flag { font-size: 20px; }
.ipa-row .ipa { font-size: 20px; }
.ipa-row.main .ipa { font-size: 30px; }
.panel .ipa-row.main .ipa { font-size: 26px; }
.ipa-row .lbl { font-size: 13px; color: var(--muted); }
.ipa-row .none { color: var(--muted); font-size: 15px; }
.mini { border: 1px solid var(--line); background: var(--card); border-radius: 9px; padding: 3px 9px; font-size: 14px; }

.wc h3 { margin: 18px 0 8px; font-size: 13px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.syl { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.sy { font-family: var(--font-ipa); font-size: 22px; padding: 4px 12px; border-radius: 10px; background: var(--chip); }
.sy.st { background: var(--stress-bg); color: var(--stress); font-weight: 700; box-shadow: inset 0 -3px 0 var(--stress); }
.syl-cap { font-size: 13.5px; color: var(--muted); margin-top: 6px; }

.notes { display: grid; gap: 8px; margin-top: 14px; }
.note { background: var(--brand-soft); border-radius: 12px; padding: 10px 12px; font-size: 15px; }
.note.warn { background: var(--warn-bg); color: var(--warn-ink); }
.note ul { margin: 6px 0 0; padding-left: 18px; }
.note li { margin: 2px 0; }
.note li.picked { font-weight: 650; }
.note em { font-style: normal; color: var(--brand); font-size: 13px; margin-left: 4px; }

.sounds { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.sounds li { display: grid; grid-template-columns: 54px 1fr; gap: 2px 10px; align-items: baseline; padding: 8px 10px; border: 1px solid var(--line); border-radius: 12px; }
.sounds .ph { grid-row: span 2; font-size: 22px; text-align: center; align-self: center; }
.sounds .g { font-weight: 700; font-size: 15px; }
.sounds .g small { font-weight: 400; color: var(--muted); margin-left: 6px; font-size: 13px; }
.sounds .how { font-size: 14px; color: var(--muted); }
.wc-links { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 16px; font-size: 14px; }
.wc-links a { color: var(--brand); font-weight: 600; text-decoration: none; }
.wc-links a:hover { text-decoration: underline; }

/* ── Bài đọc ── */
.reader { padding: 0; overflow: clip; }
.player { position: sticky; top: 0; z-index: 5; display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; padding: 12px 14px; background: color-mix(in srgb, var(--card) 92%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); border-radius: var(--radius) var(--radius) 0 0; }
.player .grp { display: inline-flex; gap: 6px; align-items: center; }
.player .count { font-size: 13.5px; color: var(--muted); min-width: 64px; }
.player .opts { display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: center; width: 100%; font-size: 14.5px; }
.player label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.player input[type=checkbox] { width: 17px; height: 17px; accent-color: var(--brand); }
.player .fs { margin-left: auto; display: inline-flex; gap: 4px; }
#pp { min-width: 148px; }

.turn { display: flex; align-items: center; gap: 12px; margin: 12px 14px 0; padding: 10px 14px; border-radius: 12px; background: var(--stress-bg); color: var(--stress); font-weight: 700; }
.turn .bar { flex: 1; height: 8px; border-radius: 8px; background: color-mix(in srgb, var(--stress) 22%, transparent); overflow: hidden; }
.turn .bar i { display: block; height: 100%; width: 0; background: var(--stress); border-radius: 8px; }
.turn .bar i.run { animation: fill linear forwards; }
@keyframes fill { from { width: 0; } to { width: 100%; } }

.reading { --fs: 30px; font-size: var(--fs); padding: 18px 18px 10px; line-height: 1.15; }
.sent { }
.sp { border: 0; background: var(--brand-soft); color: var(--brand); width: 1.1em; height: 1.1em; font-size: .62em; border-radius: 50%; padding: 0; margin: .42em .15em 0 0; vertical-align: top; line-height: 1; display: inline-grid; place-items: center; }
.sp:hover { background: var(--brand); color: var(--brand-ink); }
.w { display: inline-flex; flex-direction: column; align-items: center; vertical-align: top; padding: .06em .14em .1em; margin: .08em 0; border-radius: .3em; cursor: pointer; transition: background-color .12s; }
.w .t { white-space: nowrap; font-weight: 600; line-height: 1.25; }
.w .ipa { font-size: .5em; line-height: 1.35; white-space: nowrap; min-height: 1.35em; }
.reading.noipa .w .ipa { display: none; }
.reading.noipa { line-height: 1.5; }
.w:hover { background: var(--chip); }
.sent.on .w { background: var(--sent); border-radius: 0; }
.sent.on .w:first-of-type { border-radius: .3em 0 0 .3em; }
.w.sel { box-shadow: inset 0 0 0 2px var(--brand); border-radius: .3em; }
.sent.on .w.now, .w.now { background: var(--hl); border-radius: .3em; }
.pn { vertical-align: top; }

.savebar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 12px 14px; border-top: 1px dashed var(--line); }
.savebar input { flex: 1; min-width: 180px; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); }
.savebar .ok { color: var(--brand); font-weight: 650; font-size: 14px; }

/* ── Bảng chi tiết từ ── */
.panel { position: fixed; z-index: 20; right: 14px; top: 14px; bottom: 14px; width: min(400px, calc(100vw - 28px)); overflow: auto; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 10px 40px rgba(0, 0, 0, .22); padding: 18px; }
.panel .x { position: sticky; top: 0; float: right; border: 0; background: var(--chip); width: 34px; height: 34px; border-radius: 50%; font-size: 20px; line-height: 1; z-index: 2; }
@media (min-width: 901px) { body.panel-open main, body.panel-open .top { padding-right: 430px; max-width: none; } }
@media (max-width: 900px) {
  .panel { left: 0; right: 0; bottom: 0; top: auto; width: auto; max-height: 56vh; border-radius: var(--radius) var(--radius) 0 0; }
  body.panel-open main { padding-bottom: 60vh; }
}
@media (max-width: 720px) {
  .voice select, .savebar input { font-size: 16px; }
  .panel { padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
  .player { position: static; }
  #rateSeg { max-width: 100%; }
  #rateSeg button { padding: 7px 8px; font-size: 13px; }
  .wc-word { font-size: 36px; }
  .reading { --fs: 26px; padding: 14px 12px 8px; }
  .status { margin-left: 0; width: 100%; }
}

.help summary { cursor: pointer; font-weight: 700; }
.help ul { margin: 10px 0 0; padding-left: 20px; color: var(--muted); font-size: 15px; }
.help li { margin: 6px 0; }
.help b { color: var(--ink); }

/* ── Nghĩa tiếng Việt ── */
.muted { color: var(--muted); font-size: 14px; }
.mean-quick { font-size: 19px; font-weight: 650; line-height: 1.35; }
.mean-quick small { font-weight: 400; font-size: 13.5px; color: var(--muted); }
.mean-more { margin-top: 6px; }
.mean-det summary { cursor: pointer; font-size: 14px; color: var(--brand); font-weight: 600; }
.mean-of { font-size: 14.5px; margin: 8px 0 2px; }
.pos { margin-top: 8px; }
.pos-tag { display: inline-block; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; color: var(--brand); background: var(--brand-soft); padding: 2px 8px; border-radius: 6px; }
.pos ol { margin: 4px 0 0; padding-left: 22px; font-size: 15px; }
.pos li { margin: 2px 0; }
.mean-src { display: inline-block; margin-top: 8px; font-size: 13.5px; color: var(--brand); text-decoration: none; }
.mean-src:hover { text-decoration: underline; }

/* ── Bảng từ vựng trong bài ── */
.vocab { border-top: 1px dashed var(--line); padding: 12px 14px; }
.vocab summary { cursor: pointer; font-weight: 700; font-size: 16px; }
.vocab .count { font-weight: 400; color: var(--muted); font-size: 14px; }
.vopt { display: inline-flex; align-items: center; gap: 6px; margin: 10px 0 4px; font-size: 14.5px; cursor: pointer; }
.vopt input { width: 17px; height: 17px; accent-color: var(--brand); }
.vlist { display: grid; }
.vrow { display: grid; grid-template-columns: auto minmax(84px, 150px) minmax(84px, 150px) 1fr; gap: 4px 12px; align-items: center; padding: 7px 6px; border-top: 1px solid var(--line); cursor: pointer; border-radius: 8px; }
.vrow:first-child { border-top: 0; }
.vrow:hover { background: var(--chip); }
.vw { font-weight: 700; font-size: 18px; overflow-wrap: anywhere; }
.vrow .ipa { font-size: 15px; overflow-wrap: anywhere; }
.vm { font-size: 15.5px; }
@media (max-width: 720px) {
  .vrow { grid-template-columns: auto 1fr 1fr; }
  .vm { grid-column: 2 / -1; }
}

/* ── Chụp ảnh → nhận chữ ── */
.ocrbox { display: flex; gap: 14px; align-items: flex-start; margin-top: 12px; padding: 12px 14px; border-radius: 12px; background: var(--brand-soft); font-size: 15px; }
.ocrbox.busy { align-items: center; }
.ocrbox.bad { background: var(--warn-bg); color: var(--warn-ink); }
.ocrbox > div { flex: 1; min-width: 0; }
.ocrbox img { width: 150px; max-height: 190px; object-fit: contain; border-radius: 8px; border: 1px solid var(--line); background: #fff; }
.ocrbox .btn { margin-top: 8px; }
.spin { width: 20px; height: 20px; border-radius: 50%; border: 3px solid color-mix(in srgb, var(--brand) 25%, transparent); border-top-color: var(--brand); animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.card.input.drag { outline: 3px dashed var(--brand); outline-offset: 2px; }
@media (max-width: 520px) { .ocrbox { flex-direction: column; } .ocrbox img { width: 100%; max-height: 220px; } }

/* ── Thông báo về tiếng ── */
.hint.warn { background: var(--warn-bg); color: var(--warn-ink); border-color: color-mix(in srgb, var(--warn-ink) 25%, var(--line)); }
.hint > div { flex: 1; min-width: 0; }
.nbtns { display: flex; flex-wrap: wrap; gap: 8px; }
.hint { flex-wrap: wrap; }
