:root {
  --bg: #f6f0e4;
  --bg-card: #fffdf8;
  --text: #2c2218;
  --muted: #766756;
  --accent: #1c3f5e;
  --accent-warm: #8b5a2b;
  --accent-soft: #e9dfd0;
  --border: #ddd2c0;
  --hl: rgba(255, 200, 60, 0.42);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.14);
}
:root[data-theme="light"] {
  --bg: #f8f9fb;
  --bg-card: #ffffff;
  --text: #1c2430;
  --muted: #626d7a;
  --accent: #1e40af;
  --accent-warm: #b45309;
  --accent-soft: #e6eef9;
  --border: #e2e8f0;
  --hl: rgba(59, 130, 246, 0.2);
}
:root[data-theme="dark"] {
  --bg: #141619;
  --bg-card: #1d2025;
  --text: #e3e6eb;
  --muted: #98a2b3;
  --accent: #60a5fa;
  --accent-warm: #eab308;
  --accent-soft: #272b33;
  --border: #2f3540;
  --hl: rgba(234, 179, 8, 0.28);
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
  transition: background 0.2s, color 0.2s;
}
.view { display: none; min-height: 100vh; }
.view.active { display: block; }

.btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 11px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  text-decoration: none;
  transition: background 0.15s, border-color 0.15s;
}
.btn:hover { background: var(--accent-soft); }
.btn.active, .btn-primary {
  background: var(--accent);
  color: #ffffff;
  border-color: var(--accent);
}
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn-sm { padding: 4px 8px; font-size: 12px; }
.select-input {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
}

/* ==================== LIBRARY SHELF ==================== */
.lib-header {
  max-width: 1180px;
  margin: 0 auto;
  padding: 20px 20px 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: center;
  justify-content: space-between;
}
.lib-brand { display: flex; align-items: center; gap: 12px; }
.lib-logo { width: 46px; height: 46px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.12); }
.lib-brand h1 { margin: 0; font-size: 21px; line-height: 1.2; }
.lib-sub { margin: 2px 0 0; font-size: 13px; color: var(--muted); }
.lib-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.theme-group { display: inline-flex; gap: 4px; }

.lib-toolbar {
  max-width: 1180px;
  margin: 0 auto;
  padding: 6px 20px 18px;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}
.search-wrap { flex: 1 1 260px; max-width: 400px; }
.search-wrap input {
  width: 100%;
  padding: 9px 13px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text);
  font-size: 14px;
}
.cat-pills { display: flex; flex-wrap: wrap; gap: 7px; }
.pill {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: 999px;
  padding: 5px 13px;
  font-size: 13px;
  cursor: pointer;
}
.pill.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.lib-main { max-width: 1180px; margin: 0 auto; padding: 22px 20px 60px; }
.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 20px;
}
.book-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
  transition: transform 0.16s ease, box-shadow 0.16s ease;
}
.book-card:hover { transform: translateY(-2px); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.11); }
.book-cover-wrap {
  position: relative;
  height: 220px;
  background: var(--accent-soft);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
}
.book-cover-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.book-cover-fallback {
  padding: 20px;
  text-align: center;
  font-family: Georgia, serif;
  font-weight: 700;
  font-size: 18px;
  color: var(--accent);
}
.badge-row {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  gap: 6px;
  pointer-events: none;
}
.cat-badge, .offline-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  backdrop-filter: blur(6px);
}
.cat-badge { background: rgba(20, 22, 25, 0.72); color: #fff; }
.offline-badge { background: rgba(22, 163, 74, 0.9); color: #fff; }
.offline-badge.remote { background: rgba(118, 103, 86, 0.8); }

.book-body {
  padding: 14px 15px 15px;
  flex: 1;
  display: flex;
  flex-direction: column;
}
.book-vi { font-size: 16px; font-weight: 700; margin: 0 0 2px; cursor: pointer; }
.book-en { font-size: 13px; color: var(--muted); font-style: italic; margin: 0 0 4px; }
.book-auth { font-size: 12.5px; color: var(--accent-warm); font-weight: 600; margin: 0 0 8px; }
.book-desc {
  font-size: 12.5px;
  color: var(--muted);
  margin: 0 0 12px;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  flex: 1;
}
.book-meta { font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.prog-bar {
  height: 5px;
  background: var(--accent-soft);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 11px;
}
.prog-fill { height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.25s; }
.book-card-actions { display: flex; gap: 6px; align-items: center; }
.book-card-actions .open-btn { flex: 1; justify-content: center; }
.lib-empty { text-align: center; padding: 48px 20px; color: var(--muted); font-size: 15px; }

/* ==================== READER SHELL ==================== */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: sticky;
  top: 0;
  z-index: 40;
}
.sidebar-header { padding: 16px; border-bottom: 1px solid var(--border); }
.book-title { font-size: 15px; font-weight: 700; line-height: 1.3; }
.book-author { font-size: 12px; color: var(--muted); margin-top: 4px; }
.toc-list { overflow-y: auto; padding: 8px; flex: 1; }
.toc-item {
  display: block; width: 100%; text-align: left; border: 0;
  background: transparent; color: var(--text);
  padding: 8px 10px; border-radius: 8px; font-size: 13px; cursor: pointer;
}
.toc-item:hover { background: var(--accent-soft); }
.toc-item.active { background: var(--accent); color: #fff; font-weight: 600; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  position: sticky; top: 0; z-index: 30;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  padding: 8px 14px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap;
}
.top-left, .top-right { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.chap-indicator { font-size: 13px; color: var(--muted); font-weight: 600; max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.content-wrap {
  max-width: 760px; width: 100%; margin: 0 auto;
  padding: 26px 22px 110px;
}
article#bookContent {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 19px;
  line-height: 1.78;
}
article#bookContent img { max-width: 100%; height: auto; display: block; margin: 18px auto; border-radius: 8px; }
article#bookContent h1, article#bookContent h2, article#bookContent h3 { line-height: 1.3; margin: 1.1em 0 0.5em; }
article#bookContent .h1_num { font-size: 13px; text-transform: uppercase; letter-spacing: 0.14em; color: var(--muted); text-align: center; }
article#bookContent .h1, article#bookContent h1 { font-size: 1.55em; text-align: center; }
article#bookContent .ornament { text-align: center; color: var(--muted); margin: 22px 0; }
article#bookContent p { margin: 0 0 1em; text-align: justify; }
article#bookContent .EP, article#bookContent .EPA { font-style: italic; color: var(--muted); }
article#bookContent .FMEPA { font-size: 0.9em; color: var(--muted); margin-top: -0.6em; margin-bottom: 1.4em; }
article#bookContent .glo { background: var(--accent-soft); padding: 10px 14px; border-radius: 8px; margin-bottom: 10px; }

.w { cursor: pointer; border-radius: 3px; transition: background 0.1s; padding: 0 0.5px; }
.w:hover { background: var(--hl); }
.w.sel { background: var(--hl); outline: 1px solid var(--accent-warm); }
.w.saved { border-bottom: 2px dotted var(--accent-warm); }

.seg, .sent { border-radius: 3px; transition: background 0.15s; }
.seg.seg-active, .sent.sent-active { background: var(--accent-soft); padding: 1px 0; }
.seg-btn {
  display: inline-flex; align-items: center; justify-content: center;
  vertical-align: middle;
  margin: 0 3px 0 4px;
  padding: 0 5px;
  height: 18px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--muted);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  user-select: none;
}
.seg-btn:hover, .seg-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.sent-btn {
  display: inline-flex; align-items: center; justify-content: center;
  vertical-align: middle;
  margin: 0 4px 0 3px;
  padding: 0 6px;
  height: 18px;
  border-radius: 999px;
  border: 1px solid var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  user-select: none;
}
.sent-btn:hover, .sent-btn.active { background: var(--accent); color: #fff; }
body.clean-mode .seg-btn { display: none; }

.seg-trans, .sent-trans {
  display: none;
  margin: 6px 0 10px;
  padding: 8px 12px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 8px 8px 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 0.8em;
  line-height: 1.5;
  text-align: left;
  color: var(--text);
}
.sent-trans { border-left-width: 4px; font-weight: 500; }
.seg-trans.open, .sent-trans.open { display: block; }

.chapter-nav {
  display: flex; justify-content: space-between; margin-top: 38px; padding-top: 18px;
  border-top: 1px solid var(--border);
}

/* ==================== POPOVER & SELECTION PILL ==================== */
#popover {
  position: fixed; z-index: 100;
  width: min(370px, calc(100vw - 20px));
  max-height: 74vh; overflow-y: auto;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 13px 15px;
  display: none;
  font-size: 14px;
}
#popover.open { display: block; }
.pop-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.pop-word { font-size: 19px; font-weight: 700; color: var(--accent); }
.pop-ipa { font-size: 13px; color: var(--muted); font-family: monospace; margin-left: 6px; }
.pop-base { font-size: 12px; color: var(--muted); margin-bottom: 6px; font-style: italic; }
.pos-block { margin-bottom: 8px; }
.pos-tag { display: inline-block; font-size: 11px; font-weight: 700; background: var(--accent-soft); color: var(--accent); padding: 2px 7px; border-radius: 999px; margin-bottom: 4px; }
.def-list { margin: 0; padding-left: 16px; }
.def-list li { margin: 2px 0; }
.def-ex { list-style: none; color: var(--muted); font-size: 12.5px; margin-left: -6px; font-style: italic; }
.pop-glo { background: var(--accent-soft); border-left: 3px solid var(--accent); padding: 7px 9px; border-radius: 4px; font-size: 12.5px; margin-top: 8px; }
.pop-actions { display: flex; gap: 8px; margin-top: 10px; padding-top: 8px; border-top: 1px solid var(--border); }
.pop-sent { margin-top: 8px; padding: 8px 10px; background: var(--accent-soft); border-radius: 8px; font-size: 13px; display: none; }

#selPill {
  position: fixed; z-index: 95; display: none;
  background: var(--accent); color: #fff; border-radius: 999px;
  padding: 5px 12px; font-size: 12.5px; font-weight: 600; cursor: pointer;
  box-shadow: var(--shadow);
}

/* ==================== MODALS & FLASHCARD ==================== */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5);
  z-index: 120; display: none; align-items: center; justify-content: center; padding: 14px;
}
.modal-backdrop.open { display: flex; }
.modal {
  width: min(620px, 100%); max-height: 85vh; overflow-y: auto;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: 14px; padding: 18px; box-shadow: var(--shadow);
}
.modal-wide { width: min(720px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.search-input {
  width: 100%; padding: 8px 11px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
  font: inherit; margin-bottom: 12px;
}
.search-res-item {
  padding: 9px 10px; border-bottom: 1px solid var(--border);
  cursor: pointer; font-size: 13.5px; border-radius: 6px;
}
.search-res-item:hover { background: var(--accent-soft); }

.vocab-controls {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  justify-content: space-between; padding-bottom: 12px; margin-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
.tab-group, .vocab-filters { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.vocab-row {
  padding: 10px 4px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
}
.vocab-row-main { flex: 1; min-width: 0; }
.vocab-row-title { display: flex; align-items: baseline; flex-wrap: wrap; gap: 8px; }
.vocab-row-word { font-size: 16px; font-weight: 700; color: var(--accent); }
.vocab-row-ipa { font-size: 12px; color: var(--muted); font-family: monospace; }
.vocab-book-pill {
  font-size: 11px; background: var(--accent-soft); color: var(--accent);
  padding: 1px 7px; border-radius: 999px; font-weight: 600;
}
.vocab-row-def { font-size: 13px; margin-top: 3px; }
.vocab-row-ctx { font-size: 12px; color: var(--muted); font-style: italic; margin-top: 4px; }
.vocab-row-btns { display: flex; gap: 4px; flex-shrink: 0; }

.fc-status { text-align: center; font-size: 12.5px; color: var(--muted); margin-bottom: 10px; font-weight: 600; }
.fc-card {
  min-height: 240px;
  border: 2px solid var(--border);
  border-radius: 14px;
  background: var(--bg);
  padding: 24px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  cursor: pointer;
  user-select: none;
}
.fc-face { width: 100%; }
.fc-book-badge { display: inline-block; font-size: 11.5px; background: var(--accent-soft); color: var(--accent); padding: 2px 10px; border-radius: 999px; margin-bottom: 12px; font-weight: 600; }
.fc-word { font-size: 28px; font-weight: 800; color: var(--accent); }
.fc-word-sm { font-size: 20px; font-weight: 700; color: var(--accent); margin-bottom: 10px; }
.fc-ipa { font-size: 14px; color: var(--muted); font-family: monospace; margin: 4px 0 12px; }
.fc-ctx { font-size: 13.5px; color: var(--text); font-style: italic; max-width: 520px; margin: 0 auto 14px; }
.fc-defs { text-align: left; max-width: 520px; margin: 0 auto 12px; font-size: 14px; }
.fc-ctx-vi { font-size: 13px; color: var(--muted); border-top: 1px solid var(--border); padding-top: 8px; max-width: 520px; margin: 8px auto 10px; }
.fc-hint { font-size: 11.5px; color: var(--muted); margin-top: 10px; }
.fc-actions { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; margin-top: 14px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--accent); color: #fff; padding: 9px 16px; border-radius: 999px;
  font-size: 13px; font-weight: 600; box-shadow: var(--shadow);
  z-index: 200; opacity: 0; pointer-events: none; transition: opacity 0.2s;
}
.toast.show { opacity: 1; }

.sidebar-overlay { display: none; }
@media (max-width: 880px) {
  .sidebar { position: fixed; left: -290px; transition: left 0.2s; }
  .sidebar.open { left: 0; }
  .sidebar-overlay.open { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, 0.38); z-index: 35; }
  .content-wrap { padding: 18px 14px 90px; }
  .chap-indicator { max-width: 140px; }
}
