/* =========================================================
 * Phàm Nhân Tu Tiên – giao diện đọc truyện
 * ========================================================= */

/* ---------- Biến chung (JS ghi đè --font, --fs, --lh, --align, --margin) ---------- */
:root {
  --ui-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font: "Literata", Georgia, serif;
  --fs: 20px;
  --lh: 1.7;
  --align: justify;
  --margin: 20px;
  --row-h: 52px; /* chiều cao 1 dòng mục lục – phải khớp ROW_H trong app.js */

  /* Vùng an toàn (tai thỏ, Dynamic Island, thanh Home) */
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

/* ---------- Màu nền (data-theme trên <html>) ---------- */
:root, [data-theme="sepia"] {
  --bg: #f4ecd8; --fg: #3f3122; --sub: #8b7758; --bar: #eadfc5;
  --line: rgba(60, 40, 10, .14); --accent: #a8692b; --accent-fg: #fff;
  color-scheme: light;
}
[data-theme="light"] {
  --bg: #ffffff; --fg: #1f1f1f; --sub: #6b6b6b; --bar: #f2f2f2;
  --line: rgba(0, 0, 0, .1); --accent: #2f6fd6; --accent-fg: #fff;
  color-scheme: light;
}
[data-theme="gray"] {
  --bg: #2f3033; --fg: #d7d7d5; --sub: #9a9a98; --bar: #3a3b3e;
  --line: rgba(255, 255, 255, .12); --accent: #8fb3e8; --accent-fg: #111;
  color-scheme: dark;
}
[data-theme="dark"] {
  --bg: #000000; --fg: #b4b4b4; --sub: #777777; --bar: #141414;
  --line: rgba(255, 255, 255, .1); --accent: #c9a45c; --accent-fg: #000;
  color-scheme: dark;
}
[data-theme="green"] {
  --bg: #cde6d0; --fg: #1e3323; --sub: #4f6c55; --bar: #bcdabf;
  --line: rgba(20, 60, 30, .15); --accent: #2f7a45; --accent-fg: #fff;
  color-scheme: light;
}

/* ---------- Nền tảng ---------- */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html {
  touch-action: manipulation;          /* tắt zoom khi chạm đúp */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--bg);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font);
  transition: background-color .25s, color .25s;
}

button {
  font: inherit; color: inherit; background: none;
  border: 0; padding: 0; cursor: pointer;
  touch-action: manipulation;
}
button:disabled { cursor: default; }

.ui { font-family: var(--ui-font); font-size: 15px; }
.sub { color: var(--sub); }
[hidden] { display: none !important; }

/* ---------- Màn hình đọc ---------- */
.reader {
  min-height: 100vh;
  min-height: 100dvh;
  padding:
    calc(var(--sat) + 24px)
    calc(var(--sar) + var(--margin))
    calc(var(--sab) + 116px)   /* chừa chỗ cho thanh dưới cố định (~76px) */
    calc(var(--sal) + var(--margin));
}
.content { max-width: 720px; margin: 0 auto; }

.ch-title {
  font-size: calc(var(--fs) * 1.3);
  line-height: 1.35;
  font-weight: 700;
  margin: .4em 0 1.1em;
}

/* ---------- Đầu trang chương (tên truyện + nút điều hướng) ---------- */
.ch-head {
  position: relative;
  margin: 0 0 1.6em;
  padding-bottom: 1.2em;
  border-bottom: 1px solid var(--line);
  text-align: center;
}
.head-book {
  display: flex; align-items: center; justify-content: center;
  min-height: 44px;
  padding: 0 52px;            /* chừa chỗ cho nút cài đặt bên phải */
  font-size: 19px; font-weight: 700; line-height: 1.3;
  letter-spacing: .02em;
  text-transform: uppercase;
  color: var(--accent);
}
.ch-head .ch-title {
  margin: 2px 0 0;
  padding: 0 8px;
  font: 500 15px/1.4 var(--ui-font);
  color: var(--sub);
}
.head-icon {
  position: absolute; top: 0; right: 0;
  display: flex; align-items: center; justify-content: center;
  width: 44px; height: 44px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--accent-fg);
}
.head-icon svg { width: 22px; height: 22px; }

.head-nav { display: flex; justify-content: center; gap: 6px; margin-top: 16px; }
.hbtn {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
  flex: 1 1 0; min-width: 0; max-width: 170px; min-height: 44px;
  padding: 0 8px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 14px; font-weight: 600;
}
.hbtn-icon { flex: 0 0 48px; }
.hbtn svg { flex: none; width: 16px; height: 16px; }
.hbtn-icon svg { width: 20px; height: 20px; }
.hbtn-text { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.hbtn:disabled { opacity: .4; }
.lbl-short { display: none; }

/* Màn hẹp (lề rộng): nhãn ngắn "Trước/Tiếp", tên truyện nhỏ lại */
.ch-head { container-type: inline-size; }
@container (max-width: 320px) {
  .lbl-long { display: none; }
  .lbl-short { display: inline; }
  .head-book { font-size: 15px; letter-spacing: 0; padding: 0 48px; }
}
.hbtn:active:not(:disabled), .head-icon:active { filter: brightness(.9); }

.paras p {
  font-size: var(--fs);
  line-height: var(--lh);
  text-align: var(--align);
  margin: 0 0 .85em;
  overflow-wrap: break-word;
}
.paras .notice { text-align: center; color: var(--sub); margin: 3em 0; }

.chap-nav { display: flex; gap: 12px; margin: 2.5em 0 1em; }
.btn {
  flex: 1;
  min-height: 48px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bar);
  font-weight: 600;
}
.btn:active:not(:disabled) { filter: brightness(.94); }
.btn:disabled { opacity: .35; }
.notice .btn { display: inline-block; flex: none; margin-top: 12px; padding: 10px 20px; color: var(--fg); }

/* Vạch tải nhỏ ở mép trên (chỉ hiện nếu tải lâu hơn 150ms) */
.loader {
  position: fixed; left: 0; right: 0; top: var(--sat);
  height: 3px; z-index: 50; pointer-events: none; opacity: 0;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  background-size: 50% 100%;
  background-repeat: no-repeat;
}
body.loading .loader { animation: loader-in 0s .15s forwards, loader-slide 1s linear infinite; }
@keyframes loader-in { to { opacity: 1; } }
@keyframes loader-slide { from { background-position: -50% 0; } to { background-position: 150% 0; } }

/* ---------- Thanh công cụ trên/dưới ---------- */
.bar {
  position: fixed; left: 0; right: 0; z-index: 20;
  background: var(--bar);
  color: var(--fg);
  transition: transform .25s ease;
}
.topbar {
  top: 0;
  padding: var(--sat) calc(var(--sar) + 8px) 0 calc(var(--sal) + 8px);
  border-bottom: 1px solid var(--line);
  transform: translateY(-100%);
}
.bottombar {                   /* luôn hiện, không ẩn theo bars-visible */
  bottom: 0;
  padding: 2px calc(var(--sar) + 8px) var(--sab) calc(var(--sal) + 8px);
  border-top: 1px solid var(--line);
}
body.bars-visible .bar { transform: none; }

.bar-row { display: flex; align-items: center; gap: 8px; min-height: 52px; }
.bar-title {
  flex: 1; min-width: 0; padding-left: 8px;
  font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn {
  min-height: 44px; min-width: 44px;
  padding: 0 12px;
  border-radius: 10px;
  white-space: nowrap;
}
.icon-btn:active:not(:disabled) { background: var(--line); }
.icon-btn:disabled { opacity: .3; }

/* ---------- Thanh dưới kiểu trình phát (gọn, ~76px) ---------- */
.seek-row {
  display: flex; align-items: center; gap: 8px;
  height: 24px;
  font-size: 11px; color: var(--sub);
  font-variant-numeric: tabular-nums;
}
.seek { flex: 1; min-width: 0; height: 20px; margin: 0; accent-color: var(--accent); }
.player { display: flex; align-items: center; height: 48px; }
.pl-btn {
  flex: 1 1 0; min-width: 0;
  display: flex; align-items: center; justify-content: center;
  height: 40px;
  border-radius: 10px;
}
.pl-btn svg { width: 22px; height: 22px; }
.pl-btn b { font-size: 14px; font-variant-numeric: tabular-nums; }
.pl-btn:active:not(:disabled) { background: var(--line); }
.pl-btn:disabled { opacity: .3; }
.pl-main {
  flex: 0 0 44px;
  display: flex; align-items: center; justify-content: center;
  width: 44px; height: 44px; margin: 0 4px;
  border-radius: 50%;
  background: var(--accent); color: var(--accent-fg);
}
.pl-main svg { width: 22px; height: 22px; }
.pl-main .ic-pause, .pl-main.playing .ic-play { display: none; }
.pl-main.playing .ic-pause { display: block; }
.pl-main:active { filter: brightness(.9); }

/* ---------- Mục lục ---------- */
.panel {
  position: fixed; inset: 0; z-index: 30;
  display: flex; flex-direction: column;
  background: var(--bg);
  transform: translateX(100%);
  visibility: hidden;
  transition: transform .28s ease, visibility 0s .28s;
}
.panel.open { transform: none; visibility: visible; transition: transform .28s ease; }

.panel-head {
  padding: calc(var(--sat) + 6px) calc(var(--sar) + 12px) 12px calc(var(--sal) + 16px);
  background: var(--bar);
  border-bottom: 1px solid var(--line);
}
.panel-title-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.panel-title-row h2 { margin: 0; font-size: 18px; }

.tabs { display: flex; gap: 6px; margin: 6px 0 10px; }
.tab { padding: 7px 14px; border-radius: 999px; color: var(--sub); }
.tab.active { background: var(--bg); color: var(--fg); font-weight: 600; }

.search {
  width: 100%;
  font: inherit;
  font-size: 16px; /* >= 16px để iOS không tự zoom khi gõ */
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
  -webkit-appearance: none;
  appearance: none;
}

.toc-list, .recent-list {
  flex: 1;
  position: relative;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
.toc-spacer { position: relative; margin-bottom: var(--sab); }

.toc-row {
  position: absolute; top: 0; left: 0; right: 0;
  display: block;
  height: var(--row-h);
  line-height: calc(var(--row-h) - 1px);
  padding: 0 calc(var(--sar) + 16px) 0 calc(var(--sal) + 16px);
  border-bottom: 1px solid var(--line);
  text-align: left;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.toc-row:active, .recent-row:active { background: var(--line); }
.toc-row.current {
  color: var(--accent);
  font-weight: 700;
  background: var(--bar);
  box-shadow: inset 4px 0 0 var(--accent);
}

.recent-row {
  display: block; width: 100%;
  padding: 12px calc(var(--sar) + 16px) 12px calc(var(--sal) + 16px);
  border-bottom: 1px solid var(--line);
  text-align: left;
}
.recent-row small { display: block; margin-top: 2px; color: var(--sub); font-size: 13px; }

.empty { position: absolute; left: 0; right: 0; top: 0; margin: 0; padding: 32px 16px; text-align: center; color: var(--sub); }

/* ---------- Cài đặt (bottom sheet) ---------- */
.backdrop {
  position: fixed; inset: 0; z-index: 35;
  background: rgba(0, 0, 0, .35);
  opacity: 0; pointer-events: none;
  transition: opacity .25s;
}
body.sheet-open .backdrop { opacity: 1; pointer-events: auto; }

.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  max-width: 640px; max-height: 85vh;
  margin: 0 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 8px calc(var(--sar) + 20px) calc(var(--sab) + 20px) calc(var(--sal) + 20px);
  background: var(--bar);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -8px 30px rgba(0, 0, 0, .2);
  transform: translateY(105%);
  visibility: hidden;
  transition: transform .28s ease, visibility 0s .28s;
}
body.sheet-open .sheet { transform: none; visibility: visible; transition: transform .28s ease; }
.sheet-handle { width: 40px; height: 5px; margin: 4px auto 8px; border-radius: 3px; background: var(--line); }

.field { margin: 16px 0; }
.field-label { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 13px; color: var(--sub); }
.field-label span { color: var(--fg); font-variant-numeric: tabular-nums; }

.swatches { display: flex; flex-wrap: wrap; gap: 12px; }
.swatch { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px; color: var(--sub); }
.swatch i {
  display: flex; align-items: center; justify-content: center;
  width: 46px; height: 46px;
  border-radius: 50%;
  border: 1px solid rgba(128, 128, 128, .4);
  font-style: normal; font-size: 15px;
}
.swatch.active { color: var(--fg); font-weight: 600; }
.swatch.active i { outline: 2px solid var(--accent); outline-offset: 2px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--bg);
  font-size: 15px;
}
.chip.active { border-color: var(--accent); color: var(--accent); font-weight: 700; }

input[type="range"] { width: 100%; height: 28px; margin: 0; accent-color: var(--accent); }

.seg { display: flex; overflow: hidden; border: 1px solid var(--line); border-radius: 10px; }
.seg button { flex: 1; padding: 10px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.active { background: var(--accent); color: var(--accent-fg); font-weight: 600; }

/* ---------- Mũi tên vuốt chuyển chương ---------- */
.swipe-arrow {
  position: fixed; top: 50%; z-index: 25;
  width: 48px; height: 48px; margin-top: -24px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%;
  background: var(--bar);
  color: var(--fg);
  font: 30px/1 var(--ui-font);
  box-shadow: 0 2px 12px rgba(0, 0, 0, .25);
  opacity: 0;
  pointer-events: none;
  transition: transform .2s ease, opacity .2s ease, background-color .15s, color .15s;
}
.swipe-arrow.left  { left: -48px; }
.swipe-arrow.right { right: -48px; }
.swipe-arrow.dragging { transition: background-color .15s, color .15s; }
.swipe-arrow.ready { background: var(--accent); color: var(--accent-fg); }
.swipe-arrow.disabled { opacity: .35 !important; }

/* Giảm chuyển động nếu người dùng bật trong hệ thống */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation-duration: 0s !important; }
}
