:root {
  --bg: #f6efe2;
  --surface: #efe5d2;
  --surface-2: #e7dac2;
  --text: #271f17;
  --muted: #6b5a49;
  --border: #d8c6a8;
  --accent: #8c3b1a;
  --accent-soft: rgba(140, 59, 26, 0.12);
  --danger: #b91c1c;
  --ok: #1b6b3a;
  --shadow: 0 6px 20px rgba(42, 33, 24, 0.08);
  --font-size: 19px;
  --reader-font: 'Segoe UI', 'Roboto', 'Noto Sans', system-ui, -apple-system, sans-serif;
}

[data-theme="light"] {
  --bg: #f8f9fb;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --text: #16181d;
  --muted: #5a6270;
  --border: #dfe3ea;
  --accent: #1d5bbf;
  --accent-soft: rgba(29, 91, 191, 0.10);
  --shadow: 0 6px 20px rgba(20, 28, 42, 0.06);
}

[data-theme="dark"] {
  --bg: #111317;
  --surface: #1a1d24;
  --surface-2: #222630;
  --text: #e2dfd7;
  --muted: #98a0ad;
  --border: #2c313c;
  --accent: #e09f5a;
  --accent-soft: rgba(224, 159, 90, 0.14);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

[data-font="serif"] {
  --reader-font: 'Georgia', 'Times New Roman', 'Noto Serif', serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', 'Roboto', 'Noto Sans', system-ui, -apple-system, sans-serif;
  line-height: 1.65;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.18s ease, color 0.18s ease;
}

a { color: inherit; }

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(8px);
}
.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
}
.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1 1 280px;
  min-width: 0;
}
.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.brand-link {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-weight: 800;
  font-size: 17px;
  color: var(--accent);
  text-decoration: none;
  white-space: nowrap;
}
.brand-dot { font-size: 18px; }
.search-wrap { flex: 1 1 220px; max-width: 420px; }
.search-input, .input-sm, .select-sm {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 11px;
  font-size: 14px;
  font-family: inherit;
}
.input-sm { width: auto; min-width: 110px; padding: 6px 9px; }
.select-sm { width: auto; padding: 6px 9px; }
.search-input:focus, .input-sm:focus, .textarea-sm:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
  min-height: 36px;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.active, .btn-accent {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}
.btn-pill { border-radius: 999px; font-size: 12px; padding: 5px 11px; }
.btn.disabled { opacity: 0.42; pointer-events: none; }
.btn-sm { padding: 4px 9px; min-height: 30px; font-size: 12px; }

/* Settings Popover */
.settings-popover {
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.settings-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}
.setting-row {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.setting-label { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.setting-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.font-val { min-width: 42px; text-align: center; font-weight: 700; font-size: 13px; }
.tag-chip {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 700;
  font-size: 12px;
}

/* Main Page Container */
.page-wrap {
  max-width: 1200px;
  margin: 0 auto;
  padding: 18px 16px 84px;
}
.page-wrap.reader-wrap { max-width: 820px; }

.loading-box, .empty-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  color: var(--muted);
  text-align: center;
}

.section-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
  box-shadow: var(--shadow);
}
.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.section-title {
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  color: var(--accent);
}
.section-sub { font-size: 13px; color: var(--muted); }

/* Home Layout */
.home-stack { display: flex; flex-direction: column; gap: 18px; }

.featured-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 14px;
}
.featured-card {
  display: flex;
  gap: 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  text-decoration: none;
  transition: border-color 0.15s ease, transform 0.15s ease;
}
.featured-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.featured-cover {
  width: 88px;
  height: 124px;
  object-fit: cover;
  border-radius: 8px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  background: var(--surface-2);
}
.featured-info { min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.featured-info h3 { margin: 0; font-size: 16px; line-height: 1.35; color: var(--text); }
.meta-line { font-size: 12px; color: var(--muted); }
.clamp-3 {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 13px;
  color: var(--muted);
  margin: 0;
}

/* Middle 2-column: Left Hot Top 5 | Right Mới cập nhật */
.home-main-grid {
  display: grid;
  grid-template-columns: minmax(280px, 360px) 1fr;
  gap: 18px;
  align-items: start;
}

.hot-list { display: flex; flex-direction: column; gap: 10px; }
.hot-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  text-decoration: none;
}
.hot-item:hover { border-color: var(--accent); }
.rank-badge {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 13px;
  flex-shrink: 0;
  background: var(--surface-2);
  color: var(--text);
}
.rank-badge.top-1 { background: #c2410c; color: #fff; }
.rank-badge.top-2 { background: #b45309; color: #fff; }
.rank-badge.top-3 { background: #0f766e; color: #fff; }
.hot-thumb {
  width: 44px;
  height: 60px;
  object-fit: cover;
  border-radius: 6px;
  flex-shrink: 0;
  border: 1px solid var(--border);
}
.hot-meta { min-width: 0; flex: 1; }
.hot-meta strong { display: block; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hot-stats { font-size: 12px; color: var(--muted); }

.updated-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 12px;
}
.book-tile {
  display: flex;
  gap: 12px;
  padding: 12px;
  border-radius: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  text-decoration: none;
}
.book-tile:hover { border-color: var(--accent); }
.tile-cover {
  width: 68px;
  height: 96px;
  object-fit: cover;
  border-radius: 7px;
  flex-shrink: 0;
  border: 1px solid var(--border);
}
.tile-body { min-width: 0; flex: 1; display: flex; flex-direction: column; gap: 4px; }
.tile-body h3 { margin: 0; font-size: 15px; line-height: 1.3; }
.latest-pill {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 2px 8px;
  border-radius: 6px;
  margin-top: auto;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Bottom 2-column: Reading History | Public Chatbox */
.home-bottom-grid {
  display: grid;
  grid-template-columns: 1fr 390px;
  gap: 18px;
  align-items: start;
}

.history-list { display: flex; flex-direction: column; gap: 10px; }
.history-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  flex-wrap: wrap;
}
.history-info { min-width: 0; flex: 1 1 200px; }
.history-info a { text-decoration: none; font-weight: 700; font-size: 14px; color: var(--text); }
.history-info a:hover { color: var(--accent); }
.history-actions { display: flex; gap: 6px; align-items: center; }

/* Public Chatbox */
.chat-box { display: flex; flex-direction: column; gap: 10px; }
.chat-feed {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  height: 270px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-msg {
  padding: 7px 9px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 13px;
}
.chat-msg-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
  margin-bottom: 3px;
  font-size: 12px;
}
.author-badge { font-weight: 700; color: var(--accent); }
.time-muted { font-size: 11px; color: var(--muted); }
.chat-ref-chip {
  display: inline-block;
  margin-top: 5px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
}
.chat-ref-chip:hover { text-decoration: underline; }

.chat-tag-bar { display: flex; gap: 6px; flex-wrap: wrap; }
.chat-tag-bar .select-sm { flex: 1 1 120px; min-width: 0; font-size: 12px; }
.chat-form-row { display: flex; gap: 6px; }
.chat-form-row .input-sm { flex: 1; min-width: 0; }

/* Book Detail */
.book-hero {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 20px;
  align-items: start;
}
.book-hero-cover {
  width: 100%;
  aspect-ratio: 42 / 58;
  object-fit: cover;
  border-radius: 12px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
}
.book-hero-body h1 { margin: 0 0 8px; font-size: 26px; line-height: 1.25; }
.book-badges { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0 12px; }
.info-pill {
  font-size: 12px;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--muted);
}
.ai-notice-banner {
  margin: 16px 0;
  padding: 12px 15px;
  border-radius: 10px;
  border: 1px solid var(--accent);
  background: var(--accent-soft);
  font-size: 14px;
  display: flex;
  gap: 10px;
  align-items: flex-start;
}
.ai-notice-tag {
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  padding: 2px 7px;
  border-radius: 6px;
  flex-shrink: 0;
  margin-top: 2px;
}

.gallery-tabs { display: flex; gap: 8px; flex-wrap: wrap; }
.gallery-strip {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(165px, 195px);
  gap: 12px;
  overflow-x: auto;
  padding-bottom: 6px;
}
.gallery-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  padding: 0;
  color: inherit;
  font-family: inherit;
}
.gallery-card:hover { border-color: var(--accent); }
.gallery-card img {
  width: 100%;
  height: 205px;
  object-fit: cover;
  display: block;
}
.gallery-card-body { padding: 8px 10px; }
.gallery-card-body strong { display: block; font-size: 13px; }
.gallery-card-body span { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; }

.chapter-tools { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.chapter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 9px;
}
.chapter-link {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 9px;
  background: var(--bg);
  border: 1px solid var(--border);
  text-decoration: none;
  font-size: 14px;
}
.chapter-link:hover { border-color: var(--accent); }
.chapter-link.is-read { opacity: 0.72; }
.chapter-link.is-current { border-color: var(--accent); background: var(--accent-soft); opacity: 1; }
.chapter-link-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-badge { font-size: 11px; color: var(--accent); font-weight: 700; flex-shrink: 0; }

/* Reader View */
.reader-header { margin-bottom: 14px; }
.reader-breadcrumb { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.reader-breadcrumb a { color: var(--accent); text-decoration: none; font-weight: 600; }
.reader-title { margin: 0 0 6px; font-size: 27px; line-height: 1.3; }
.nav-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin: 14px 0;
  flex-wrap: wrap;
}
.tts-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  margin: 14px 0 22px;
  display: flex;
  flex-direction: column;
  gap: 9px;
}
.tts-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.tts-status { font-size: 13px; color: var(--muted); }
.tts-status.error { color: var(--danger); }

.chapter-protected {
  font-family: var(--reader-font);
  font-size: var(--font-size);
  line-height: 1.88;
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -webkit-touch-callout: none;
}
.chapter-protected p {
  margin: 0 0 1.15em;
  text-align: justify;
  cursor: pointer;
}
.chapter-protected p.speaking-para {
  background: var(--accent-soft);
  border-left: 3px solid var(--accent);
  padding: 4px 10px;
  border-radius: 6px;
}
.scene-break {
  text-align: center;
  color: var(--muted);
  letter-spacing: 0.4em;
  margin: 1.8em 0;
}
.print-notice { display: none; }

/* Comments */
.comments-box { margin-top: 28px; }
.textarea-sm {
  width: 100%;
  min-height: 78px;
  resize: vertical;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-family: inherit;
  font-size: 14px;
}
.comment-submit-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
  flex-wrap: wrap;
}
.comment-list { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }
.comment-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
.comment-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  margin-bottom: 5px;
}
.comment-body { margin: 0; font-size: 14px; white-space: pre-wrap; word-break: break-word; }

/* Lightbox */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(10, 11, 15, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.lightbox[hidden] { display: none; }
.lightbox-figure {
  margin: 0;
  max-width: min(92vw, 760px);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.lightbox-figure img {
  max-width: 100%;
  max-height: 76vh;
  border-radius: 10px;
  object-fit: contain;
}
.lightbox-figure figcaption { color: #f3eee7; font-size: 14px; text-align: center; }
.lightbox-close {
  position: fixed;
  top: 14px;
  right: 18px;
  width: 40px;
  height: 40px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 24px;
  cursor: pointer;
}

/* Mobile floating chat shortcut */
.fab-chat {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 35;
  border-radius: 999px;
  box-shadow: var(--shadow);
  display: none;
}

.toast-root {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  z-index: 90;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: none;
}
.toast-msg {
  background: #181a20;
  color: #f8f5ef;
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 13px;
  box-shadow: var(--shadow);
}

/* Responsive Tablet & Mobile */
@media (max-width: 900px) {
  .home-main-grid, .home-bottom-grid {
    grid-template-columns: 1fr;
  }
  .fab-chat { display: inline-flex; }
}

@media (max-width: 640px) {
  .topbar-inner { padding: 8px 12px; }
  .search-wrap { order: 3; flex: 1 1 100%; max-width: none; }
  .book-hero { grid-template-columns: 1fr; }
  .book-hero-cover { max-width: 170px; margin: 0 auto; }
  .reader-title { font-size: 23px; }
  .chapter-protected p { text-align: left; }
}

@media print {
  .chapter-protected { display: none !important; }
  .print-notice {
    display: block !important;
    padding: 24px;
    border: 1px solid #999;
    font-size: 16px;
  }
}
