:root {
  --bg: #fbfaf7; --fg: #222; --mut: #7a766e; --line: #e6e1d6; --acc: #2f6f5e; --card: #fff;
  --size: 19px; --lh: 1.7;
  /* Self-hosted (fonts.css). Fallbacks must cover stacked Vietnamese letters (ố, ắ, ề): Georgia doesn't. */
  --serif: Literata, "Noto Serif", "Times New Roman", serif;
  --sans: "Be Vietnam Pro", system-ui, Roboto, "Segoe UI", sans-serif;
  --bar-h: 56px;
}
:root[data-theme="sepia"] { --bg: #f4ecd8; --fg: #3b2f1e; --mut: #8a7a60; --line: #e0d2b2; --acc: #8a5a2b; --card: #f9f3e3; }
:root[data-theme="dark"]  { --bg: #121212; --fg: #d6d3cc; --mut: #8b8780; --line: #2c2c2c; --acc: #7fbfa8; --card: #1c1c1c; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--fg); font: 16px/1.4 var(--sans); }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
button:disabled { opacity: .35; cursor: default; }
[hidden] { display: none !important; }

.btn { padding: 9px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--card); white-space: nowrap; }
.btn.primary { background: var(--acc); color: var(--bg); border-color: var(--acc); }
.btn.on { border-color: var(--acc); color: var(--acc); font-weight: 600; }
.icon { width: 44px; height: 44px; font-size: 22px; flex: none; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 9px; background: var(--acc); color: var(--bg); font-size: 12px; font-weight: 600; }
.mut { color: var(--mut); }

/* ---------- bars */
.bar {
  display: flex; align-items: center; gap: 4px;
  min-height: var(--bar-h); padding: env(safe-area-inset-top) 12px 0;
  background: var(--bg); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10;
}
.bar h1 { flex: 1; margin: 0; font-size: 20px; }
.bar.fixed { position: fixed; left: 0; right: 0; padding-left: 4px; padding-right: 4px; transition: transform .2s; }
.bar.fixed.hide { transform: translateY(-100%); }
.titles { flex: 1; min-width: 0; }
.titles div { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#btitle { font-size: 12px; color: var(--mut); }
#ctitle { font-size: 15px; font-weight: 600; }

/* ---------- library */
#books { padding: 16px; max-width: 640px; margin: 0 auto; display: grid; gap: 10px; }
.empty { color: var(--mut); text-align: center; margin-top: 30vh; line-height: 1.6; }
.continue {
  display: grid; gap: 3px; text-align: left; padding: 16px; border-radius: 14px;
  background: var(--acc); color: var(--bg); margin-bottom: 6px;
}
.continue .lbl { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; opacity: .85; }
.continue b { font-size: 18px; }
.book { display: flex; align-items: stretch; background: var(--card); border: 1px solid var(--line); border-radius: 12px; }
.book .open { flex: 1; min-width: 0; display: grid; gap: 3px; text-align: left; padding: 12px 14px; }
.book .open b { font-size: 16px; }
.book .open span { font-size: 14px; }
.book .del { width: 48px; color: var(--mut); font-size: 16px; }

/* ---------- reader */
#text {
  max-width: 42em; margin: 0 auto;
  padding: calc(var(--bar-h) + env(safe-area-inset-top) + 12px) 18px 8px;
  font-size: var(--size); line-height: var(--lh); font-family: var(--serif);
  overflow-wrap: break-word;
}
:root[data-font="sans"] #text { font-family: var(--sans); }
#text h2 { font-size: 1.2em; line-height: 1.4; margin: .4em 0 1.1em; }
#text p { margin: 0 0 .85em; }
#chapnav { display: flex; gap: 10px; max-width: 42em; margin: 0 auto; padding: 16px 18px calc(32px + env(safe-area-inset-bottom)); }
#chapnav .btn { flex: 1; padding: 14px; }

/* ---------- panels */
#scrim { position: fixed; inset: 0; background: #0006; z-index: 20; }
#toc {
  position: fixed; top: 0; bottom: 0; left: 0; width: min(86vw, 420px); z-index: 21;
  background: var(--bg); display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
}
#toc header { padding: 16px; font-weight: 600; font-size: 18px; border-bottom: 1px solid var(--line); }
#toclist { list-style: none; margin: 0; padding: 0 0 env(safe-area-inset-bottom); overflow-y: auto; flex: 1; }
#toclist button { width: 100%; text-align: left; padding: 12px 16px; border-bottom: 1px solid var(--line); }
#toclist button.cur { color: var(--acc); font-weight: 600; background: var(--card); }

#settings {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  background: var(--bg); border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom)); display: grid; gap: 10px;
}
#settings .row { display: flex; align-items: center; gap: 8px; }
#settings .row span { flex: 1; color: var(--mut); }
#settings output { min-width: 3ch; text-align: center; }

#toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 30;
  max-width: 480px; margin: 0 auto; padding: 12px 16px; border-radius: 12px;
  background: var(--fg); color: var(--bg); box-shadow: 0 4px 16px #0003;
}
#toast.err { background: #b3261e; color: #fff; }
