/* books.phucnt.org — trình đọc. Hai kiểu: cuộn (mặc định) và lật trang (data-mode="paged"). */

:root {
  --bar-h: 52px;
  --foot-h: 34px;
  --measure: 36;
  --pad: clamp(20px, 5.5vw, 44px);
  --read: var(--serif);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
}
:root[data-measure='s'] { --measure: 30; }
:root[data-measure='l'] { --measure: 44; }
:root[data-font='sans'] { --read: var(--ui); }
:root[data-font='system'] { --read: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif; }

html.reader-root { scroll-padding-top: calc(var(--bar-h) + var(--sat) + 16px); }
body.reader { min-height: 100vh; min-height: 100dvh; }

/* ───────── thanh công cụ ───────── */

.rbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20;
  height: calc(var(--bar-h) + var(--sat)); padding: var(--sat) max(6px, env(safe-area-inset-right)) 0 max(6px, env(safe-area-inset-left));
  display: flex; align-items: center; gap: 2px;
  background: color-mix(in srgb, var(--paper) 90%, transparent);
  -webkit-backdrop-filter: blur(12px) saturate(1.2); backdrop-filter: blur(12px) saturate(1.2);
  border-bottom: 1px solid var(--rule);
  transition: transform .28s cubic-bezier(.3, .7, .2, 1);
}
body:not(.chrome-on) .rbar { transform: translateY(-102%); }
.rbtn {
  flex: none; width: 44px; height: 44px; display: grid; place-items: center;
  border: 0; border-radius: 50%; background: none; color: var(--ink-2); cursor: pointer; text-decoration: none;
}
.rbtn:hover { background: var(--paper-2); color: var(--ink); }
.rbtn[aria-expanded='true'] { background: var(--ink); color: var(--paper); }
.rbtn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.rbar-title { flex: 1; min-width: 0; display: grid; padding: 0 8px; line-height: 1.25; }
.rbar-title b { font: 600 10.5px/1.3 var(--ui); letter-spacing: .13em; text-transform: uppercase; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rbar-title span { font: 500 16px/1.25 var(--display); font-variation-settings: 'opsz' 30; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
:root[data-theme='eink'] .rbar { background: #fff; -webkit-backdrop-filter: none; backdrop-filter: none; }

.runhead { display: none; }

/* ───────── chân trang: tiến độ ───────── */

.rfoot {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
  height: calc(var(--foot-h) + var(--sab)); padding-bottom: var(--sab);
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  pointer-events: none;
}
.rfoot-bar { display: block; height: 2px; background: var(--rule); }
.rfoot-bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s ease; }
.rfoot-text {
  display: flex; justify-content: space-between; gap: 16px; align-items: center;
  height: calc(var(--foot-h) - 2px); padding-inline: max(var(--pad), env(safe-area-inset-left));
  font: 500 11.5px/1 var(--ui); letter-spacing: .04em; color: var(--ink-3); font-feature-settings: 'tnum';
  white-space: nowrap;
}
:root[data-theme='eink'] .rfoot { background: #fff; -webkit-backdrop-filter: none; backdrop-filter: none; }

/* ───────── kiểu cuộn ───────── */

.stage { padding: calc(var(--bar-h) + var(--sat) + clamp(26px, 6vw, 70px)) 0 calc(var(--foot-h) + var(--sab) + clamp(40px, 8vw, 90px)); }
#text { max-width: calc(var(--measure) * 1em + 2 * var(--pad)); margin-inline: auto; padding-inline: var(--pad); }
.sentinel { height: 0; margin: 0; padding: 0; }

/* ───────── chữ ───────── */

.prose {
  font-family: var(--read); font-size: var(--size); line-height: var(--lh);
  color: var(--ink); overflow-wrap: break-word; hyphens: manual;
  font-kerning: normal; font-variant-ligatures: common-ligatures;
}
.prose p { margin: 0 0 .9em; text-wrap: pretty; orphans: 2; widows: 2; }
:root[data-justify] .prose p { text-align: justify; text-wrap: wrap; }
:root[data-para='indent'] .prose p { margin-bottom: 0; text-indent: 1.7em; }
:root[data-para='indent'] .prose :is(h2, h3, .chap-head, .scene, figure, hr, blockquote, ul, ol) + p,
:root[data-para='indent'] .prose p.verse + p { text-indent: 0; }
:root[data-para='indent'] .prose p:is(.verse, .scene, .sign) { margin-block: .9em; }
.prose em { font-style: italic; }
.prose strong { font-weight: 650; }
.prose a { color: var(--accent); }
.prose a.btn { color: var(--ink); text-decoration: none; }
.prose a.btn--primary { color: var(--on-accent); }

.chap-head { text-align: center; padding: clamp(10px, 4vh, 44px) 0 0; margin-bottom: clamp(30px, 6vh, 56px); }
.chap-head .kicker { font-size: 11.5px; }
.chap-head .kicker::before { display: none; }
.chap-head h1 { margin: 14px 0 0; font: 400 clamp(1.9em, 7vw, 2.9em)/1.04 var(--display); font-variation-settings: 'opsz' 144; letter-spacing: -.028em; text-wrap: balance; }
.chap-head .chap-author { margin: 14px 0 0; font: italic 300 1.08em/1.3 var(--display); color: var(--ink-2); text-indent: 0; }
.chap-head::after { content: ''; display: block; width: 46px; height: 2px; margin: clamp(22px, 4vh, 36px) auto 0; background: var(--seal); }

.prose h2 { margin: 2.3em 0 1em; text-align: center; font: 400 1.6em/1.15 var(--display); font-variation-settings: 'opsz' 100; letter-spacing: -.018em; text-wrap: balance; break-after: avoid; }
.prose h3 { margin: 2em 0 .9em; text-align: center; font: italic 400 1.18em/1.25 var(--display); font-variation-settings: 'opsz' 40; color: var(--ink-2); text-wrap: balance; break-after: avoid; }
.prose .chap-head + h2 { margin-top: 0; }
.prose h2 + h3 { margin-top: -.3em; }

/* chữ đầu chương */
.prose .chap-head + p:not(.verse):not(.scene)::first-letter,
.prose .chap-head + h2 + p:not(.verse):not(.scene)::first-letter,
.prose .chap-head + h2 + h3 + p:not(.verse):not(.scene)::first-letter {
  float: left; margin: .05em .09em 0 0; padding-top: .04em;
  font: 400 3.55em/.86 var(--display); font-variation-settings: 'opsz' 144; color: var(--accent);
}
.prose .chap-head + p, .prose .chap-head + h2 + p, .prose .chap-head + h2 + h3 + p { text-indent: 0 !important; }

.prose p.verse { text-indent: 0; padding-left: 1.5em; text-align: left; text-wrap: wrap; }
@media (max-width: 520px) { .prose p.verse { padding-left: .5em; font-size: .96em; } }
.prose p.scene { text-indent: 0; text-align: center; margin: 1.9em 0; letter-spacing: .32em; color: var(--ink-3); break-after: avoid; }
.prose p.sign { text-indent: 0; text-align: right; font-style: italic; }
.prose hr { border: 0; height: 1.2em; margin: 2em 0; text-align: center; overflow: visible; }
.prose hr::after { content: '⁂'; color: var(--ink-3); }
.prose blockquote { margin: 1.4em 0; padding-left: 1.2em; border-left: 2px solid var(--rule); color: var(--ink-2); }
.prose ul, .prose ol { margin: 0 0 .9em; padding-left: 1.5em; }
.prose ul { list-style: disc; }
.prose ol { list-style: decimal; }

.prose figure.illus { margin: 2.2em 0; text-align: center; break-inside: avoid; }
.prose figure.illus img { display: inline-block; width: 30em; max-width: 100%; height: auto; border-radius: 6px; box-shadow: 0 2px 16px rgb(var(--shadow) / .16); }
:root[data-theme='night'] .prose figure.illus img { filter: brightness(.86); }

.prose sup { font-size: .66em; line-height: 0; vertical-align: super; }
.prose a.footnote-ref { padding: .35em .3em; margin: 0 -.2em 0 -.24em; font: 600 1em/1 var(--ui); text-decoration: none; white-space: nowrap; }
.prose a.footnote-backref { text-decoration: none; font-family: var(--ui); }

.notes { margin-top: 3.2em; padding-top: 1.3em; border-top: 1px solid var(--rule); font-size: .84em; line-height: 1.6; color: var(--ink-2); }
.prose .notes-title { margin: 0 0 1em; text-align: left; font: 600 .78em/1.3 var(--ui); letter-spacing: .14em; text-transform: uppercase; color: var(--ink-3); }
.notes ol { padding-left: 1.7em; }
.notes li p { margin: 0 0 .55em !important; text-indent: 0 !important; text-align: left; }

.end-card { margin: 3.4em 0 0; padding: 1.9em 1.2em; border-block: 1px solid var(--rule); text-align: center; break-inside: avoid; }
.end-card p { margin: 0 0 .6em !important; text-indent: 0 !important; text-align: center !important; }
.end-mark { color: var(--seal); letter-spacing: .3em; }
.end-title { font: 400 1.5em/1.15 var(--display); font-variation-settings: 'opsz' 100; letter-spacing: -.015em; }
.end-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 1.2em !important; }

.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 2.6em; break-inside: avoid; font-family: var(--ui); }
.pager a { display: grid; gap: 5px; padding: 15px 17px; border: 1px solid var(--rule); border-radius: 12px; text-decoration: none; color: var(--ink); transition: border-color .15s ease, background-color .15s ease; }
.pager a:hover { border-color: var(--ink-3); background: var(--paper-2); }
.pager span { font: 600 10.5px/1.3 var(--ui); letter-spacing: .13em; text-transform: uppercase; color: var(--ink-3); }
.pager b { font: 500 1.02em/1.25 var(--display); font-variation-settings: 'opsz' 30; }
.pager-next { text-align: right; }

/* ───────── kiểu lật trang ───────── */

html.reader-root[data-mode='paged'], html.reader-root[data-mode='paged'] body { height: 100%; overflow: hidden; overscroll-behavior: none; }
html.reader-root[data-mode='paged'] { --bar-h: 48px; --foot-h: 40px; }
:root[data-mode='paged'] .stage {
  position: fixed; left: 0; right: 0; top: calc(var(--bar-h) + var(--sat)); bottom: calc(var(--foot-h) + var(--sab));
  padding: 0; overflow: hidden; touch-action: pan-y pinch-zoom; cursor: default;
}
:root[data-mode='paged'] #text {
  max-width: none; margin: 0; height: 100%;
  padding: 0 var(--side, 24px);
  column-count: var(--cols, 1); column-gap: var(--gap, 48px); column-fill: auto;
  transform: translate3d(var(--tx, 0px), 0, 0);
  transition: transform .34s cubic-bezier(.3, .7, .2, 1);
  will-change: transform;
}
:root[data-mode='paged'] .no-anim #text, :root[data-theme='eink'] #text { transition: none; }
:root[data-mode='paged'] .stage[data-cols='2']::after { content: ''; position: absolute; top: 4%; bottom: 4%; left: 50%; width: 1px; background: linear-gradient(var(--paper), var(--rule) 18%, var(--rule) 82%, var(--paper)); pointer-events: none; }
:root[data-mode='paged'] .chap-head { padding-top: clamp(6px, 3vh, 30px); }
:root[data-mode='paged'] .prose figure.illus img { width: auto; max-height: calc(var(--page-h, 80vh) - 2em); }
:root[data-mode='paged'] .runhead {
  display: grid; place-items: center; position: fixed; top: var(--sat); left: 0; right: 0; height: var(--bar-h); z-index: 5;
  margin: 0; padding-inline: 60px; font: italic 400 13.5px/1 var(--serif); color: var(--ink-3);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; pointer-events: none;
}
:root[data-mode='paged'] .rfoot { background: transparent; -webkit-backdrop-filter: none; backdrop-filter: none; }
:root[data-mode='paged'] .rfoot-bar { margin-inline: max(var(--pad), env(safe-area-inset-left)); background: var(--rule); }

.pg { display: none; }
@media (hover: hover) and (pointer: fine) {
  :root[data-mode='paged'] .pg {
    display: grid; place-items: center; position: fixed; top: 50%; z-index: 6; width: 40px; height: 84px; margin-top: -42px;
    border: 0; background: none; color: var(--ink-3); opacity: .35; cursor: pointer; border-radius: 10px; transition: opacity .15s ease, background-color .15s ease;
  }
  :root[data-mode='paged'] .pg:hover { opacity: 1; background: var(--paper-2); color: var(--ink); }
  .pg[disabled] { visibility: hidden; }
  .pg-prev { left: 6px; }
  .pg-next { right: 6px; }
  .pg svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
}

/* ───────── ngăn kéo, bảng tuỳ chỉnh ───────── */

.scrim { position: fixed; inset: 0; z-index: 40; background: rgb(12 9 4 / .36); opacity: 0; pointer-events: none; transition: opacity .22s ease; }
.scrim.on { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; z-index: 41; background: var(--paper); color: var(--ink);
  font: 400 14.5px/1.5 var(--ui);
  box-shadow: 0 18px 60px rgb(var(--shadow) / .3); border: 1px solid var(--rule);
  visibility: hidden; overflow: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  transition: transform .3s cubic-bezier(.3, .7, .2, 1), opacity .2s ease, visibility 0s linear .3s;
}
.sheet.on { visibility: visible; transform: none !important; opacity: 1 !important; transition: transform .3s cubic-bezier(.3, .7, .2, 1), opacity .2s ease; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.sheet-head h2 { font: 400 22px/1.15 var(--display); font-variation-settings: 'opsz' 60; letter-spacing: -.015em; }
.sheet-close { flex: none; width: 40px; height: 40px; margin-right: -8px; border: 0; border-radius: 50%; background: none; color: var(--ink-2); font: 400 24px/1 var(--ui); cursor: pointer; }
.sheet-close:hover { background: var(--paper-2); color: var(--ink); }

.sheet--toc { top: 0; bottom: 0; left: 0; width: min(88vw, 390px); padding: calc(16px + var(--sat)) 20px calc(26px + var(--sab)) max(20px, env(safe-area-inset-left)); transform: translateX(-104%); border-width: 0 1px 0 0; }
.sheet--prefs, .sheet--handoff, .sheet--note {
  left: 0; right: 0; bottom: 0; max-height: 88vh; max-height: 88dvh; padding: 18px 20px calc(22px + var(--sab));
  border-radius: 20px 20px 0 0; border-width: 1px 0 0; transform: translateY(104%);
}
@media (min-width: 760px) {
  .sheet--prefs, .sheet--handoff {
    left: auto; bottom: auto; right: 12px; top: calc(var(--bar-h) + var(--sat) + 8px); width: 390px; max-height: calc(100dvh - var(--bar-h) - 28px);
    border-radius: 18px; border-width: 1px; padding: 18px 22px 22px; transform: translateY(-10px); opacity: 0;
  }
  .sheet--note { left: 50%; right: auto; bottom: 22px; width: min(640px, calc(100vw - 40px)); margin-left: calc(min(640px, 100vw - 40px) / -2); border-radius: 18px; border-width: 1px; padding: 18px 26px 22px; transform: translateY(20px); opacity: 0; }
}

/* mục lục */
.dtoc-book { display: grid; gap: 2px; padding: 4px 0 16px; text-decoration: none; border-bottom: 2px solid var(--ink); }
.dtoc-book b { font: 500 22px/1.15 var(--display); font-variation-settings: 'opsz' 60; letter-spacing: -.015em; }
.dtoc-book span { font-size: 13px; color: var(--ink-3); }
.dtoc-row { display: grid; grid-template-columns: 1fr auto; gap: 1px 12px; align-items: baseline; padding: 13px 2px; border-bottom: 1px solid var(--rule); text-decoration: none; }
.dtoc-row:hover .dtoc-title { text-decoration: underline; text-decoration-color: var(--ink-3); }
.dtoc-kicker { grid-column: 1 / -1; font: 600 10.5px/1.3 var(--ui); letter-spacing: .13em; text-transform: uppercase; color: var(--ink-3); }
.dtoc-title { font: 400 18px/1.25 var(--display); font-variation-settings: 'opsz' 30; }
.dtoc-meta { font-size: 12px; color: var(--ink-3); white-space: nowrap; }
.dtoc-row.is-current .dtoc-kicker { color: var(--seal); }
.dtoc-row.is-current .dtoc-title { font-weight: 600; }
.dheads { padding: 6px 0 10px; border-bottom: 1px solid var(--rule); }
.dhead { display: block; padding: 7px 2px 7px 14px; font: 400 15.5px/1.3 var(--serif); color: var(--ink-2); text-decoration: none; border-left: 2px solid var(--rule); }
.dhead:hover { color: var(--ink); border-left-color: var(--accent); }
.dhead.l3 { padding-left: 30px; font-size: 14.5px; font-style: italic; }

/* tuỳ chỉnh */
.opt { display: grid; gap: 9px; padding: 14px 0; border-top: 1px solid var(--rule); }
.opt:first-of-type { border-top: 0; padding-top: 8px; }
.opt-label { font: 600 10.5px/1.3 var(--ui); letter-spacing: .13em; text-transform: uppercase; color: var(--ink-3); }
.opt-hint { font-size: 12.5px; color: var(--ink-3); }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border: 1px solid var(--rule); border-radius: 12px; overflow: hidden; }
.seg button { min-height: 44px; padding: 8px 6px; border: 0; border-left: 1px solid var(--rule); background: none; font: 500 14px/1.2 var(--ui); cursor: pointer; }
.seg button:first-child { border-left: 0; }
.seg button[aria-pressed='true'] { background: var(--ink); color: var(--paper); }
.seg button[data-face='literata'] { font-family: var(--serif); font-size: 15.5px; }
.seg button[data-face='system'] { font-family: system-ui, sans-serif; }
.swatches { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.swatch { display: grid; justify-items: center; gap: 6px; padding: 2px 0; border: 0; background: none; cursor: pointer; font: 500 11.5px/1.2 var(--ui); color: var(--ink-2); }
.swatch i { width: 40px; height: 40px; border-radius: 50%; border: 1px solid rgb(120 110 95 / .55); background: var(--sw); display: grid; place-items: center; font: 500 15px/1 var(--display); font-style: normal; color: var(--sw-ink); }
.swatch[aria-pressed='true'] i { outline: 2px solid var(--accent); outline-offset: 3px; }
.swatch[aria-pressed='true'] { color: var(--ink); }
.stepper { display: grid; grid-template-columns: 48px 1fr 48px; align-items: center; border: 1px solid var(--rule); border-radius: 12px; overflow: hidden; }
.stepper button { height: 44px; border: 0; background: none; font: 500 17px/1 var(--display); cursor: pointer; }
.stepper button:hover { background: var(--paper-2); }
.stepper button[disabled] { opacity: .3; cursor: default; }
.stepper output { text-align: center; font: 500 14px/1 var(--ui); font-feature-settings: 'tnum'; border-inline: 1px solid var(--rule); align-self: stretch; display: grid; place-items: center; }
.opt-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.opt-2 > div { display: grid; gap: 9px; }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; min-height: 36px; }
.switch-row span { font-size: 14.5px; }
.switch { position: relative; flex: none; width: 46px; height: 28px; border-radius: 999px; border: 1px solid var(--ink-3); background: var(--paper-3); cursor: pointer; padding: 0; transition: background-color .15s ease; }
.switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: var(--paper); border: 1px solid var(--ink-3); transition: transform .18s ease; }
.switch[aria-checked='true'] { background: var(--accent); border-color: var(--accent); }
.switch[aria-checked='true']::after { transform: translateX(18px); border-color: transparent; }
.link-btn { border: 0; background: none; padding: 6px 0; font: 500 13px/1.3 var(--ui); color: var(--ink-3); text-decoration: underline; text-underline-offset: .2em; cursor: pointer; justify-self: start; }
.link-btn:hover { color: var(--ink); }

/* đọc tiếp trên thiết bị khác */
.handoff-lede { color: var(--ink-2); margin-bottom: 14px; }
.qr { width: 208px; height: 208px; margin: 4px auto 16px; padding: 12px; border-radius: 14px; background: #fff; border: 1px solid var(--rule); display: grid; place-items: center; color: #777; font-size: 13px; text-align: center; }
.qr svg { width: 100%; height: 100%; display: block; }
.link-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.link-row input { min-width: 0; height: 44px; padding: 0 14px; border: 1px solid var(--rule); border-radius: 999px; background: var(--paper-2); font: 400 13px/1 var(--ui); color: var(--ink-2); }
.handoff-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.handoff-actions .btn { min-height: 44px; padding: 8px 16px; font-size: 14px; }
.handoff-note { margin-top: 12px; font-size: 12.5px; color: var(--ink-3); line-height: 1.5; }

/* chú thích */
.note-body { font: 400 17px/1.6 var(--serif); padding-bottom: 4px; }
.note-body p { margin: 0 0 .6em; }
.note-num { font: 600 11px/1.3 var(--ui); letter-spacing: .13em; text-transform: uppercase; color: var(--seal); }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--foot-h) + var(--sab) + 18px); z-index: 45;
  max-width: min(92vw, 460px); padding: 11px 18px; border-radius: 999px;
  background: var(--ink); color: var(--paper); font: 500 13.5px/1.35 var(--ui); text-align: center;
  transform: translate(-50%, 12px); opacity: 0; pointer-events: none; transition: opacity .25s ease, transform .25s ease;
}
.toast.on { opacity: 1; transform: translate(-50%, 0); }

@media print {
  .rbar, .rfoot, .runhead, .sheet, .scrim, .toast, .pg, .pager, .end-actions { display: none !important; }
  .stage { padding: 0; }
}
