:root {
  --bg: #f7f2e8;
  --surface: #fffdf8;
  --surface-2: #efe8da;
  --ink: #26221c;
  --ink-soft: #5d554a;
  --muted: #8a8173;
  --line: #e2d9c8;
  --accent: #1d6b64;
  --accent-ink: #ffffff;
  --accent-soft: #d6ebe6;
  --vi: #8a5a2b;
  --highlight: #fff1c9;
  --speaking: #1d6b64;
  --shadow: 0 1px 2px rgba(38, 34, 28, .06), 0 8px 24px rgba(38, 34, 28, .06);
  --serif: "Literata", "Iowan Old Style", Georgia, serif;
  --sans: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", sans-serif;
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17150f;
    --surface: #211e17;
    --surface-2: #2b271e;
    --ink: #eee7da;
    --ink-soft: #c5bcab;
    --muted: #938a79;
    --line: #3a352a;
    --accent: #6cc2b7;
    --accent-ink: #0f1c1a;
    --accent-soft: #1f3a36;
    --vi: #e0b27f;
    --highlight: #3a3220;
    --speaking: #7fd3c8;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #17150f;
  --surface: #211e17;
  --surface-2: #2b271e;
  --ink: #eee7da;
  --ink-soft: #c5bcab;
  --muted: #938a79;
  --line: #3a352a;
  --accent: #6cc2b7;
  --accent-ink: #0f1c1a;
  --accent-soft: #1f3a36;
  --vi: #e0b27f;
  --highlight: #3a3220;
  --speaking: #7fd3c8;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.6 var(--sans);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
button, select { font: inherit; color: inherit; }
code { font-size: .9em; background: var(--surface-2); padding: .1em .35em; border-radius: 4px; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- khung */

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  max-width: 1080px; margin: 0 auto; padding: 14px 16px;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; }
.brand-mark {
  display: grid; place-items: center; width: 36px; height: 36px; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink); font: 600 16px/1 var(--serif);
}
.brand-text { font-weight: 700; line-height: 1.1; }
.brand-text small { display: block; font-weight: 500; font-size: 12px; color: var(--muted); letter-spacing: .04em; }

.icon-btn {
  display: inline-grid; place-items: center; width: 40px; height: 40px;
  border: 0; border-radius: 50%; background: transparent; cursor: pointer; color: var(--ink-soft);
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }

main { max-width: 1080px; margin: 0 auto; padding: 0 16px 160px; outline: none; }

.btn {
  display: inline-block; padding: 10px 18px; border-radius: 999px;
  background: var(--accent); color: var(--accent-ink); text-decoration: none; font-weight: 600;
}
.empty { text-align: center; padding: 80px 0; }

.eyebrow {
  margin: 0 0 8px; font-size: 13px; font-weight: 600; letter-spacing: .08em;
  text-transform: uppercase; color: var(--accent);
}

/* ---------------------------------------------------------------- thư viện */

.hero { padding: 36px 0 28px; max-width: 720px; }
.hero h1 {
  margin: 0; font: 500 clamp(30px, 5vw, 48px)/1.15 var(--serif); letter-spacing: -.01em;
}
.hero h1 em { color: var(--accent); }
.lede { margin: 16px 0 0; font-size: 17px; color: var(--ink-soft); }

.filters { display: flex; flex-wrap: wrap; gap: 10px 20px; margin: 8px 0 22px; }
.chip-group { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 6px 14px; cursor: pointer; font-size: 14px; font-weight: 500; color: var(--ink-soft);
}
.chip[aria-pressed="true"] { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.search { flex: 1 1 220px; max-width: 320px; }
.search input {
  width: 100%; border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 999px; padding: 6px 14px; font: 14px var(--sans);
}
.search input::placeholder { color: var(--muted); }
.result-count { margin: -10px 0 14px; font-size: 13px; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 16px; }
.card {
  position: relative; display: flex; flex-direction: column; gap: 4px;
  padding: 20px 20px 16px; border-radius: var(--radius); background: var(--surface);
  border: 1px solid var(--line); text-decoration: none; overflow: hidden;
  transition: transform .15s ease, box-shadow .15s ease;
}
.card:hover { transform: translateY(-2px); box-shadow: var(--shadow); }
.card-meta { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); }
.level {
  font-weight: 700; font-size: 12px; padding: 1px 8px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
}
.card-title { margin: 6px 0 0; font: 600 22px/1.25 var(--serif); }
.card-title-en { margin: 0; font: italic 16px/1.35 var(--serif); color: var(--ink-soft); }
.card-author { margin: 6px 0 0; font-size: 14px; color: var(--muted); }
.card-teaser { margin: 8px 0 0; font-size: 14px; color: var(--ink-soft); }
.card-foot {
  display: flex; justify-content: space-between; margin-top: auto; padding-top: 14px;
  font-size: 13px; color: var(--muted);
}
.card-foot .ok { color: var(--accent); font-weight: 600; }
.card-progress {
  display: block; margin: 10px -20px -16px; padding: 6px 20px 8px; font-size: 12px; color: var(--accent);
  background: linear-gradient(90deg, var(--accent-soft) var(--p), transparent var(--p));
}

/* ---------------------------------------------------------------- trình đọc */

.reader { max-width: 760px; margin: 0 auto; }
.back { display: inline-block; margin: 8px 0 18px; font-size: 14px; color: var(--muted); text-decoration: none; }
.back:hover { color: var(--ink); }
.work-head h1 { margin: 0; font: 600 clamp(30px, 5vw, 42px)/1.15 var(--serif); }
.title-en { margin: 4px 0 0; font: italic 20px/1.3 var(--serif); color: var(--ink-soft); }
.byline { margin: 10px 0 0; color: var(--muted); }

.intro { margin: 16px 0 0; border-top: 1px solid var(--line); padding-top: 10px; }
.intro summary { cursor: pointer; font-weight: 600; font-size: 15px; color: var(--ink-soft); }
.intro p { margin: 10px 0 0; color: var(--ink-soft); }
.intro .intro-en { font-family: var(--serif); font-style: italic; }

.controls {
  position: sticky; top: 0; z-index: 5; display: flex; flex-wrap: wrap; gap: 10px 22px;
  margin: 22px -16px 8px; padding: 12px 16px;
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.control { display: flex; align-items: center; gap: 8px; }
.control-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.seg-toggle { display: inline-flex; padding: 3px; border-radius: 999px; background: var(--surface-2); }
.seg-toggle button {
  border: 0; background: transparent; padding: 5px 12px; border-radius: 999px;
  font-size: 14px; font-weight: 500; cursor: pointer; color: var(--ink-soft); white-space: nowrap;
}
.seg-toggle button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.mini, .sub-btn {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 5px 12px; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.sub-btn { font-weight: 700; }
.sub-btn span { font-weight: 500; color: var(--muted); margin-left: 2px; }
.sub-btn[data-state="on"] { border-color: var(--accent); color: var(--accent); }
.sub-btn[data-state="on"] span { color: var(--accent); }
.sub-btn[data-state="off"] { text-decoration: line-through; color: var(--muted); }

.subs-off-note {
  margin: 20px 0; padding: 14px 16px; border-radius: var(--radius);
  background: var(--surface-2); color: var(--ink-soft); font-size: 15px;
}

/* các câu */
.text { margin-top: 12px; }
.seg {
  position: relative; padding: 10px 14px 10px 44px; margin: 0 -14px 2px -44px;
  border-radius: 12px; cursor: pointer; transition: background .2s ease;
}
.seg:hover { background: color-mix(in srgb, var(--surface-2) 60%, transparent); }
.seg.active { background: var(--highlight); }
.seg-num {
  position: absolute; left: 12px; top: 14px; width: 22px; text-align: right;
  font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums;
}
.line { margin: 0; transition: color .2s ease, filter .25s ease; }
.line[data-lang="en"] { font: 400 20px/1.55 var(--serif); }
.line[data-lang="vi"] { font: 400 16.5px/1.55 var(--sans); color: var(--vi); margin-top: 2px; }
.line.speaking { color: var(--speaking); }
.line[data-lang="vi"].speaking { color: var(--speaking); }
.is-poem .seg { padding-top: 6px; padding-bottom: 6px; }
.is-poem .seg-num { top: 10px; }
.is-poem .stanza-end { margin-bottom: 22px; }

/* phụ đề: hiện, mờ, tắt */
.reader[data-sub-en="off"] .line[data-lang="en"],
.reader[data-sub-vi="off"] .line[data-lang="vi"] { display: none; }
.reader[data-sub-en="blur"] .line[data-lang="en"],
.reader[data-sub-vi="blur"] .line[data-lang="vi"] { filter: blur(6px); user-select: none; }
.reader[data-sub-en="blur"] .seg:is(.reveal, .played, .played-en) .line[data-lang="en"],
.reader[data-sub-vi="blur"] .seg:is(.reveal, .played, .played-vi) .line[data-lang="vi"],
.reader[data-sub-en="blur"] .stage-line.reveal[data-lang="en"],
.reader[data-sub-vi="blur"] .stage-line.reveal[data-lang="vi"] { filter: none; user-select: auto; }
.reader[data-sub-en="off"][data-sub-vi="off"] .seg { min-height: 36px; }

/* từng câu, chữ lớn */
.stage {
  display: flex; flex-direction: column; justify-content: center; gap: 18px;
  min-height: 46vh; padding: 36px 8px; text-align: center;
}
.stage-count { margin: 0; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.stage .line[data-lang="en"] { font-size: clamp(24px, 4.2vw, 34px); line-height: 1.4; }
.stage .line[data-lang="vi"] { font-size: clamp(18px, 3vw, 23px); }
.stage .line { cursor: default; }

/* từ vựng */
.w {
  border: 0; padding: 0; background: none; cursor: help; font: inherit; color: inherit;
  text-decoration: underline dotted; text-decoration-color: var(--accent);
  text-underline-offset: 4px; text-decoration-thickness: 2px;
}
.vocab { margin-top: 48px; padding-top: 20px; border-top: 1px solid var(--line); }
.vocab h2 { margin: 0; font: 600 22px var(--serif); }
.vocab dl { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; margin: 14px 0 0; }
.vocab dl div { padding: 10px 12px; border-radius: 10px; background: var(--surface); border: 1px solid var(--line); }
.vocab dt { font: 600 17px var(--serif); }
.vocab dd { margin: 2px 0 0; font-size: 14px; color: var(--ink-soft); }
.ipa { font: 400 13px var(--sans); color: var(--muted); margin-left: 4px; }
.source { margin-top: 32px; font-size: 13px; color: var(--muted); }

.popover {
  position: fixed; z-index: 30; max-width: min(320px, calc(100vw - 24px));
  padding: 12px 14px; border-radius: 12px; background: var(--surface);
  border: 1px solid var(--line); box-shadow: var(--shadow);
}
.popover p { margin: 6px 0 0; font-size: 15px; }
.pop-head { display: flex; align-items: center; gap: 6px; }
.pop-head b { font: 600 19px var(--serif); }
.pop-say { width: 32px; height: 32px; margin-left: auto; }

/* ---------------------------------------------------------------- trình phát */

.player {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: var(--surface); border-top: 1px solid var(--line);
  box-shadow: 0 -6px 24px rgba(0, 0, 0, .06);
  padding-bottom: env(safe-area-inset-bottom);
}
.progress {
  display: block; width: 100%; height: 14px; margin: -7px 0 0; padding: 5px 0; border: 0;
  background: transparent; cursor: pointer;
}
.progress i {
  display: block; height: 4px; background: var(--accent); transform-origin: left;
  transform: scaleX(0); transition: transform .3s ease;
  box-shadow: 0 0 0 100vw var(--surface-2) inset;
}
.progress { background: linear-gradient(var(--surface-2), var(--surface-2)) center / 100% 4px no-repeat; }
.progress i { box-shadow: none; }
.player-row {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px;
  max-width: 1080px; margin: 0 auto; padding: 6px 16px 10px;
}
.player-info { display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.now { font-weight: 600; font-size: 14px; font-variant-numeric: tabular-nums; }
.voice-src { font-size: 12px; }
.player-main { display: flex; align-items: center; gap: 6px; }
.play {
  display: grid; place-items: center; width: 54px; height: 54px; border: 0; border-radius: 50%;
  background: var(--accent); color: var(--accent-ink); cursor: pointer;
}
.play:hover { filter: brightness(1.08); }
.play .i-pause, .is-playing .play .i-play { display: none; }
.is-playing .play .i-pause { display: block; }
.player-opts { display: flex; justify-content: flex-end; align-items: center; gap: 6px; flex-wrap: wrap; }
.opt {
  border: 1px solid var(--line); background: transparent; border-radius: 999px;
  padding: 5px 11px; font-size: 13px; cursor: pointer; white-space: nowrap; color: var(--ink-soft);
}
.opt[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }
.rate select {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 5px 8px; font-size: 13px; cursor: pointer;
}

.shadow-bar {
  position: absolute; left: 50%; bottom: calc(100% + 10px); transform: translateX(-50%);
  width: min(320px, calc(100vw - 32px)); padding: 8px 14px 10px; border-radius: 12px;
  background: var(--ink); color: var(--bg); font-size: 14px; font-weight: 600; text-align: center;
}
.shadow-bar i {
  display: block; height: 3px; margin-top: 6px; border-radius: 3px;
  background: var(--accent); transform-origin: left;
}

/* ---------------------------------------------------------------- màn hình hẹp */

@media (max-width: 720px) {
  .hero { padding-top: 16px; }
  .controls { gap: 8px 14px; }
  .control-label { display: none; }
  .seg { padding-left: 16px; margin-left: -16px; margin-right: -16px; }
  .seg-num { display: none; }
  .line[data-lang="en"] { font-size: 18.5px; }
  .player-row { grid-template-columns: 1fr auto; padding: 4px 12px 8px; gap: 6px 10px; }
  .player-info { order: 1; }
  .player-main { order: 2; }
  .player-opts { order: 3; grid-column: 1 / -1; justify-content: center; }
  .play { width: 48px; height: 48px; }
  main { padding-bottom: 190px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; }
}
