:root {
  --bg: #ffffff;
  --surface: #f5f5f0;
  --text: #1a1a1a;
  --text-muted: #888;
  --border: #e0e0e0;
  --accent: #4a7c59;
  --accent-light: #e8f0ea;
  --toolbar-bg: rgba(245,245,240,0.96);
  --statusbar-bg: rgba(248,248,245,0.97);
  --font-size: 18px;
  --max-width: 680px;
  --line-height: 1.8;
  --font-family: Georgia, serif;
}
body.theme-sepia {
  --bg: #f4efe6; --surface: #ede8dc; --text: #3b2f1e; --text-muted: #7a6a55;
  --border: #d4c9b0; --accent: #8b6914; --accent-light: #f0e8d0;
  --toolbar-bg: rgba(237,232,220,0.96); --statusbar-bg: rgba(240,235,224,0.97);
}
body.theme-dark {
  --bg: #1c1c1e; --surface: #2c2c2e; --text: #e0ddd8; --text-muted: #666;
  --border: #3a3a3c; --accent: #6b9e7a; --accent-light: #1e2e21;
  --toolbar-bg: rgba(28,28,30,0.96); --statusbar-bg: rgba(22,22,24,0.97);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%; height: 100dvh;
  background: var(--bg); color: var(--text); overflow: hidden;
}

.screen { display: none; height: 100vh; height: 100dvh; }
.screen.active { display: flex; flex-direction: column; }
#library-screen.active { overflow-y: auto; }

/* -- HEADER -- */
.lib-header {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 20px;
  padding-top: calc(14px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10; flex-wrap: wrap;
}
.lib-header h1 { font-size: 20px; font-weight: 700; flex: 1; }
.lib-header-right { display: flex; align-items: center; gap: 8px; }
.today-stat { font-size: 13px; color: var(--accent); font-weight: 600; }
.reading-time-stat { font-size: 13px; color: var(--text-muted); font-weight: 500; }
.upload-btn {
  background: var(--accent); color: white;
  padding: 8px 14px; border-radius: 8px;
  cursor: pointer; font-size: 14px; font-weight: 500;
  white-space: nowrap; transition: opacity 0.2s;
}
.upload-btn:hover { opacity: 0.85; }

/* -- TABS -- */
.lib-tabs {
  display: flex; gap: 0;
  background: var(--surface);
  border-bottom: 2px solid var(--border);
  position: sticky; top: 56px; z-index: 9;
  padding: 0 12px;
}
.lib-tab {
  flex: 1; padding: 10px 8px; border: none; background: none;
  cursor: pointer; font-size: 13px; font-weight: 600;
  color: var(--text-muted); border-bottom: 2px solid transparent;
  margin-bottom: -2px; transition: all 0.15s;
  white-space: nowrap;
}
.lib-tab:hover { color: var(--text); }
.lib-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* -- SEARCH BAR -- */
.lib-search {
  padding: 10px 20px 6px;
  position: sticky; top: 102px; z-index: 8;
  background: var(--bg);
}
.lib-search.hidden { display: none; }
.lib-search input {
  width: 100%; border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 14px; font-size: 14px;
  background: var(--surface); color: var(--text); outline: none;
  transition: border-color 0.15s;
}
.lib-search input:focus { border-color: var(--accent); }

/* -- UPLOAD -- */
.upload-progress {
  padding: 10px 20px; background: var(--accent-light);
  display: flex; align-items: center; gap: 12px;
}
.upload-progress.hidden { display: none; }
.upload-bar { flex: 1; height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; }
.upload-fill { height: 100%; background: var(--accent); width: 0%; animation: pulse 1.5s ease-in-out infinite; }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.5} }

/* -- BOOKS GRID -- */
.books-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px; padding: 20px;
}
.books-grid.hidden { display: none; }
.book-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px;
  cursor: pointer; position: relative;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06); user-select: none;
}
.book-card:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,0,0,0.12); }
.book-cover {
  width: 100%; aspect-ratio: 2/3;
  background: linear-gradient(135deg, var(--accent-light), var(--border));
  border-radius: 6px; display: flex; align-items: center;
  justify-content: center; font-size: 36px; margin-bottom: 10px;
  overflow: hidden;
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
}
.book-cover.has-image { background: var(--surface); }
.book-cover img {
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.book-title {
  font-size: 13px; font-weight: 600; line-height: 1.3; margin-bottom: 4px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.book-author { font-size: 11px; color: var(--text-muted); margin-bottom: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-progress-bar { height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; margin-bottom: 4px; }
.book-progress-fill { height: 100%; background: var(--accent); transition: width 0.3s; }
.book-meta { font-size: 11px; color: var(--text-muted); }

/* -- BOOK MENU BUTTON -- */
.book-menu-btn {
  position: absolute; top: 8px; right: 8px;
  background: rgba(0,0,0,0.12); border: none; border-radius: 50%;
  width: 26px; height: 26px; cursor: pointer; font-size: 16px;
  display: none; align-items: center; justify-content: center;
  color: var(--text); line-height: 1; font-weight: 700;
}
.book-card:hover .book-menu-btn { display: flex; }
.book-menu-btn:hover { background: rgba(0,0,0,0.25); }

/* -- CONTEXT MENU -- */
.book-context-menu {
  position: fixed; z-index: 100;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 4px 0;
  box-shadow: 0 8px 28px rgba(0,0,0,0.18);
  min-width: 160px;
}
.book-context-menu.hidden { display: none; }
.book-context-menu button {
  display: block; width: 100%; text-align: left;
  padding: 9px 16px; border: none; background: none;
  cursor: pointer; font-size: 13px; color: var(--text);
  transition: background 0.1s;
}
.book-context-menu button:hover { background: var(--accent-light); }
.menu-divider { height: 1px; background: var(--border); margin: 4px 0; }
.menu-danger { color: #c62828 !important; }
.menu-danger:hover { background: #fde8e8 !important; }

.empty-state { text-align: center; padding: 80px 20px; color: var(--text-muted); }
.empty-state.hidden { display: none; }
.empty-icon { font-size: 60px; margin-bottom: 16px; }
.empty-state p { font-size: 17px; margin-bottom: 6px; }
.empty-state .sub { font-size: 13px; }

/* -- FLIBUSTA SEARCH -- */
.flibusta-browser { }
.flibusta-browser.hidden { display: none; }
.flib-toolbar {
  display: flex; gap: 8px; padding: 10px 16px; flex-wrap: wrap;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.flib-toolbar input {
  flex: 1 1 200px; border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 12px; font-size: 14px;
  background: var(--bg); color: var(--text); outline: none;
}
.flib-toolbar input:focus { border-color: var(--accent); }
.flib-toolbar button {
  background: var(--accent); color: white; border: none;
  border-radius: 8px; padding: 8px 14px; cursor: pointer;
  font-size: 13px; font-weight: 600; white-space: nowrap;
}
.flib-status { padding: 8px 20px; font-size: 13px; color: var(--text-muted); }
.flib-results { max-height: calc(100vh - 220px); overflow-y: auto; }
.flib-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 20px; border-bottom: 1px solid var(--border);
}
.flib-item:last-child { border-bottom: none; }
.flib-info { flex: 1; min-width: 0; }
.flib-title { font-size: 14px; font-weight: 600; line-height: 1.3; }
.flib-author { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.flib-desc { font-size: 11px; color: var(--text-muted); margin-top: 3px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.flib-btns { display: flex; gap: 5px; flex-shrink: 0; }
.flib-dl-btn {
  border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 10px; cursor: pointer; font-size: 12px; font-weight: 600;
  background: var(--bg); color: var(--text); transition: all 0.15s;
}
.flib-dl-btn:hover { background: var(--accent); color: white; border-color: var(--accent); }
.flib-dl-btn.loading { opacity: 0.5; pointer-events: none; }

/* -- READER -- */
#reader-screen { position: relative; overflow: hidden; }
#reader-screen.active { display: block; }

.reader-toolbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 50;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: var(--toolbar-bg); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  transition: transform 0.25s ease, opacity 0.25s ease;
}
.toolbar-hidden { transform: translateY(-100%); opacity: 0; pointer-events: none; }
.reader-toolbar button {
  background: none; border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 12px; cursor: pointer; color: var(--text);
  font-size: 13px; white-space: nowrap; transition: background 0.15s;
}
.reader-toolbar button:hover { background: var(--accent-light); }
.reader-title-text {
  flex: 1; font-size: 13px; font-weight: 600; color: var(--text-muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.toolbar-right { display: flex; gap: 6px; }

.settings-panel {
  position: fixed; top: 54px; left: 50%; transform: translateX(-50%);
  z-index: 60; background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 16px 20px; box-shadow: 0 8px 32px rgba(0,0,0,0.2);
  min-width: 300px; display: flex; flex-direction: column; gap: 13px;
}
.settings-panel.hidden { display: none; }
.settings-row {
  display: flex; align-items: center;
  justify-content: space-between; gap: 12px;
  font-size: 13px; color: var(--text-muted);
}
.btn-group { display: flex; align-items: center; gap: 6px; }
.btn-group button {
  border: 1px solid var(--border); border-radius: 6px;
  padding: 5px 10px; cursor: pointer; font-size: 12px;
  background: var(--bg); color: var(--text); transition: background 0.15s;
}
.btn-group button:hover { background: var(--accent-light); }
.btn-group span { min-width: 30px; text-align: center; color: var(--text); font-size: 13px; }
.settings-row select {
  border: 1px solid var(--border); border-radius: 6px;
  padding: 5px 8px; font-size: 12px; background: var(--bg); color: var(--text);
}

.toc-panel {
  position: fixed; top: 0; left: 0; width: 300px;
  height: 100vh; height: 100dvh;
  z-index: 70; background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: 4px 0 24px rgba(0,0,0,0.15);
  display: flex; flex-direction: column;
  transform: translateX(-100%); transition: transform 0.3s ease;
}
.toc-panel.open { transform: translateX(0); }
.toc-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
  font-weight: 600; font-size: 15px;
}
.toc-header button { background: none; border: none; cursor: pointer; font-size: 18px; color: var(--text-muted); }
#toc-list { flex: 1; overflow-y: auto; padding: 6px 0; }
.toc-item {
  padding: 10px 20px; cursor: pointer; font-size: 14px;
  border-left: 3px solid transparent; transition: background 0.15s;
  color: var(--text); line-height: 1.4;
}
.toc-item:hover { background: var(--accent-light); }
.toc-item.active { border-left-color: var(--accent); background: var(--accent-light); font-weight: 600; }

/* Search panel — drops down from under the toolbar */
.search-panel {
  position: fixed; top: 0; left: 0; right: 0; z-index: 65;
  background: var(--surface); border-bottom: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  max-height: 75vh; display: flex; flex-direction: column;
  transition: transform 0.25s ease, opacity 0.25s ease;
}
.search-panel.hidden {
  transform: translateY(-100%); opacity: 0; pointer-events: none;
}
.search-bar {
  display: flex; gap: 8px; align-items: center;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  background: var(--toolbar-bg); backdrop-filter: blur(10px);
}
.search-bar input {
  flex: 1; border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 14px; font-size: 14px;
  background: var(--bg); color: var(--text); outline: none;
}
.search-bar input:focus { border-color: var(--accent); }
.search-close {
  background: none; border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 10px; cursor: pointer; color: var(--text); font-size: 14px;
}
.search-close:hover { background: var(--accent-light); }
.search-status { padding: 6px 18px; font-size: 12px; color: var(--text-muted); }
.search-status:empty { display: none; }
.search-results { flex: 1; overflow-y: auto; padding: 4px 0 8px; }
.search-result-chapter {
  font-size: 12px; font-weight: 700; text-transform: uppercase;
  color: var(--accent); padding: 10px 18px 4px;
  letter-spacing: 0.05em;
}
.search-result-item {
  padding: 9px 18px; cursor: pointer; font-size: 13px; line-height: 1.45;
  color: var(--text); border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}
.search-result-item:hover { background: var(--accent-light); }
.search-result-item mark {
  background: var(--accent); color: white;
  padding: 0 2px; border-radius: 2px; font-weight: 600;
}

/* Bookmarks panel — mirrors TOC but slides from right */
.bookmarks-panel {
  position: fixed; top: 0; right: 0; width: 320px;
  height: 100vh; height: 100dvh;
  z-index: 70; background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -4px 0 24px rgba(0,0,0,0.15);
  display: flex; flex-direction: column;
  transform: translateX(100%); transition: transform 0.3s ease;
}
.bookmarks-panel.open { transform: translateX(0); }
.bookmarks-add { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.bookmarks-add-btn {
  width: 100%; background: var(--accent); color: white; border: none;
  border-radius: 8px; padding: 10px; cursor: pointer;
  font-size: 13px; font-weight: 600;
}
.bookmarks-add-btn:hover { opacity: 0.9; }
.bookmarks-list { flex: 1; overflow-y: auto; padding: 4px 0; }
.bookmark-item {
  padding: 12px 16px; border-bottom: 1px solid var(--border);
  cursor: pointer; transition: background 0.15s; position: relative;
}
.bookmark-item:hover { background: var(--accent-light); }
.bookmark-chapter {
  font-size: 13px; font-weight: 600; color: var(--text);
  padding-right: 28px; line-height: 1.3;
}
.bookmark-meta { font-size: 11px; color: var(--text-muted); margin-top: 3px; }
.bookmark-note {
  font-size: 12px; color: var(--text); margin-top: 6px;
  font-style: italic; line-height: 1.35;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden;
}
.bookmark-del {
  position: absolute; top: 10px; right: 10px;
  background: none; border: none; cursor: pointer;
  font-size: 14px; color: var(--text-muted); padding: 4px 6px;
  border-radius: 4px; line-height: 1;
}
.bookmark-del:hover { color: #c62828; background: rgba(198,40,40,0.08); }
.bookmarks-empty { padding: 40px 16px; text-align: center; color: var(--text-muted); font-size: 13px; }

.nav-zone { position: fixed; top: 0; bottom: calc(46px + env(safe-area-inset-bottom)); z-index: 30; }
.zone-left { left: 0; width: 22%; cursor: w-resize; }
.zone-right { right: 0; width: 22%; cursor: e-resize; }
.zone-center { left: 22%; width: 56%; cursor: default; }

.reader-viewport {
  position: fixed; top: 0; left: 0; right: 0;
  bottom: calc(46px + env(safe-area-inset-bottom));
  overflow: hidden; background: var(--bg);
}
.reader-content {
  position: absolute; top: 0; left: 0; right: 0;
  will-change: transform;
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.content-body {
  max-width: var(--max-width); margin: 0 auto;
  padding: 48px 24px 56px;
}
.chapter-title {
  font-family: var(--font-family);
  font-size: calc(var(--font-size) * 1.25);
  font-weight: 700; line-height: 1.3; margin-bottom: 28px; color: var(--text);
}
.chapter-text {
  font-family: var(--font-family);
  font-size: var(--font-size);
  line-height: var(--line-height); color: var(--text);
}
.chapter-text p { margin-bottom: 1em; text-indent: 1.5em; }
.chapter-text p:first-child { text-indent: 0; }
.chapter-text .book-img {
  display: block; max-width: 100%; height: auto;
  margin: 1.5em auto; border-radius: 4px;
}

/* Wipe overlay */
.wipe-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  z-index: 5;
  pointer-events: none;
  background: var(--bg);
  overflow: hidden;
}
.wipe-bar-elem {
  position: absolute;
  left: 0; right: 0;
  height: 2px;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent), 0 0 2px var(--accent);
  z-index: 6;
  pointer-events: none;
}

.book-progress-track {
  position: fixed; bottom: calc(46px + env(safe-area-inset-bottom)); left: 0; right: 0;
  height: 2px; background: var(--border); z-index: 41;
}
.book-progress-fill-bar { height: 100%; background: var(--accent); transition: width 0.4s ease; }

.reader-statusbar {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: calc(46px + env(safe-area-inset-bottom));
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px;
  padding-bottom: env(safe-area-inset-bottom);
  gap: 8px;
  background: var(--statusbar-bg); backdrop-filter: blur(10px);
  border-top: 1px solid var(--border); z-index: 40;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
.status-item { font-size: 12px; color: var(--text-muted); white-space: nowrap; min-width: 60px; pointer-events: none; }
.status-page { font-size: 13px; font-weight: 600; color: var(--text); white-space: nowrap; pointer-events: none; }
.reading-timer-display { color: var(--accent); font-weight: 600; }
.autoscroll-btn {
  background: none; border: 1px solid var(--border); border-radius: 50%;
  width: 28px; height: 28px; cursor: pointer; color: var(--text);
  font-size: 12px; display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, border-color 0.15s; flex-shrink: 0;
}
.autoscroll-btn:hover { background: var(--accent-light); }
.autoscroll-btn.active { background: var(--accent); color: white; border-color: var(--accent); }
.status-right { text-align: right; }
.settings-divider { height: 1px; background: var(--border); margin: 2px 0; }
.btn-danger { color: #c62828 !important; border-color: #c62828 !important; }
.btn-danger:hover { background: #fde8e8 !important; }

.toast {
  position: fixed; bottom: 58px; left: 50%; transform: translateX(-50%);
  background: rgba(0,0,0,0.75); color: #fff;
  padding: 8px 18px; border-radius: 20px; font-size: 13px;
  z-index: 1000; transition: opacity 0.3s; pointer-events: none; white-space: nowrap;
}

@media (max-width: 600px) {
  .books-grid { grid-template-columns: repeat(2, 1fr); gap: 12px; padding: 14px; }
  .settings-panel { min-width: 88vw; }
  .toc-panel { width: 100%; }
  .bookmarks-panel { width: 100%; }
  .zone-left { width: 28%; }
  .zone-right { width: 28%; }
  .zone-center { left: 28%; width: 44%; }
  .content-body { padding: 40px 18px 50px; }
  .lib-tabs { padding: 0 6px; }
  .lib-tab { font-size: 12px; padding: 9px 4px; }
  .lib-search { padding: 8px 14px 4px; }
  .flib-toolbar { padding: 8px 10px; }
  .flib-toolbar input { font-size: 13px; padding: 7px 10px; flex: 1 1 100%; }
  .flib-results { max-height: calc(100vh - 280px); }
  .flib-item { flex-direction: column; align-items: flex-start; gap: 8px; }
  .book-menu-btn { display: flex; }
}

::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
