/* ============================================================
   个人图书馆 — Stylesheet
   ============================================================ */

/* --- Reset & Variables --- */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --primary: #2563eb;
  --primary-light: #dbeafe;
  --primary-dark: #1d4ed8;
  --bg: #f8fafc;
  --bg-card: #ffffff;
  --text: #1e293b;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --border: #e2e8f0;
  --sidebar-width: 300px;
  --header-height: 52px;
  --bottom-bar-height: 52px;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.06);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.1);
}

[data-theme="dark"] {
  --primary: #60a5fa;
  --primary-light: #1e3a5f;
  --primary-dark: #3b82f6;
  --bg: #0f172a;
  --bg-card: #1e293b;
  --text: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --border: #334155;
  --shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
  --shadow-lg: 0 10px 25px rgba(0,0,0,0.4);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
    'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  height: 100vh;
  overflow: hidden;
  margin: 0;
}

#app {
  display: flex;
  height: 100vh;
  flex-direction: column;
}

/* --- Header --- */
.header {
  height: var(--header-height);
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 10px;
  flex-shrink: 0;
  box-shadow: var(--shadow);
  z-index: 100;
}
[data-theme="dark"] .header {
  background: #1e293b;
  border-bottom: 1px solid var(--border);
}
.header .logo { font-size: 22px; line-height: 1; }
.header h1 { font-size: 17px; font-weight: 600; }
.header .subtitle {
  font-size: 11px;
  opacity: 0.7;
  background: rgba(255,255,255,0.15);
  padding: 2px 10px;
  border-radius: 10px;
  margin-left: 4px;
}
.theme-toggle {
  margin-left: 8px;
  background: none;
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 6px;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  padding: 4px 10px;
  line-height: 1;
  transition: all 0.2s;
}
.theme-toggle:hover { background: rgba(255,255,255,0.15); }

/* Sidebar toggle button */
.sidebar-toggle {
  margin-left: auto;
  background: none;
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 6px;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  padding: 4px 10px;
  line-height: 1;
  transition: all 0.2s;
}
.sidebar-toggle:hover { background: rgba(255,255,255,0.15); }
[data-theme="dark"] .sidebar-toggle { border-color: rgba(255,255,255,0.2); }

.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ============================================================
   Sidebar — 可折叠树形分类菜单
   ============================================================ */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
  transition: width 0.25s ease, min-width 0.25s ease;
}
.sidebar.collapsed {
  width: 0;
  min-width: 0;
  border-right: none;
}
.sidebar.collapsed .sidebar-header,
.sidebar.collapsed .tree-menu,
.sidebar.collapsed .category-tabs,
.sidebar.collapsed .sidebar-footer {
  display: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.sidebar-header h3 { font-size: 15px; font-weight: 600; }
.sidebar-header #bookCount {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--border);
  padding: 2px 8px;
  border-radius: 8px;
}

/* --- 底部版权声明 --- */
.sidebar-footer {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.copyright {
  font-size: 10px;
  line-height: 1.5;
  color: var(--text-muted);
  text-align: center;
}

/* --- 树形菜单 --- */
.tree-menu {
  flex: 1;
  overflow-y: auto;
  padding: 4px 0;
}

/* 分类组 */
.tree-cat {}
.tree-cat + .tree-cat { border-top: 1px solid var(--border); }

/* 分类头部 — 点击展开/收起 */
.tree-cat-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px 8px 16px;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s;
}
.tree-cat-header:hover {
  background: var(--primary-light);
}

.tree-cat-arrow {
  font-size: 10px;
  color: var(--text-muted);
  width: 14px;
  text-align: center;
  flex-shrink: 0;
  transition: transform 0.2s;
}
.tree-cat.collapsed .tree-cat-arrow {
  transform: rotate(-90deg);
}

.tree-cat-icon { font-size: 16px; flex-shrink: 0; }
.tree-cat-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  flex: 1;
}
.tree-cat-count {
  font-size: 10px;
  color: var(--text-muted);
  background: var(--border);
  padding: 1px 6px;
  border-radius: 6px;
}

/* 分类下的书籍列表 */
.tree-items {
  overflow: hidden;
  transition: max-height 0.25s ease;
  max-height: 2000px;
}
.tree-cat.collapsed .tree-items {
  max-height: 0 !important;
}

/* 单本书 */
.tree-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 16px 7px 38px;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: all 0.12s;
}
.tree-item:hover { background: var(--primary-light); }
.tree-item.active {
  background: var(--primary-light);
  border-left-color: var(--primary);
}

.tree-item-icon { font-size: 16px; flex-shrink: 0; width: 20px; text-align: center; }
.tree-item-title {
  font-size: 13px;
  color: var(--text);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tree-item.active .tree-item-title {
  color: var(--primary);
  font-weight: 600;
}
.tree-item-format {
  font-size: 9px;
  padding: 1px 5px;
  border-radius: 3px;
  background: var(--border);
  color: var(--text-secondary);
  font-weight: 500;
  flex-shrink: 0;
}

/* ============================================================
   阅读区
   ============================================================ */
.reader-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  position: relative;
}

/* Empty State */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  gap: 12px;
}
.empty-state .empty-icon { font-size: 64px; opacity: 0.3; }
.empty-state p { font-size: 15px; }

/* ============================================================
   Loading — 进度条
   ============================================================ */
.loading-state {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 0 40px;
  background: var(--bg-card);
}
.loading-state.show {
  display: flex;
}
#loadingText {
  font-size: 15px;
  color: var(--text-secondary);
}
.loading-bar-track {
  width: 100%;
  max-width: 320px;
  height: 8px;
  background: var(--border);
  border-radius: 4px;
  overflow: hidden;
}
.loading-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), var(--primary-dark));
  border-radius: 4px;
}
.loading-pct {
  font-size: 13px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* ============================================================
   Reader Content
   ============================================================ */
.reader-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.book-meta-bar {
  padding: 8px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-card);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
}
.book-meta-bar h2 {
  font-size: 16px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-meta-bar .meta {
  font-size: 12px;
  color: var(--text-muted);
  flex-shrink: 0;
}

.render-area {
  flex: 1;
  overflow: hidden;
  position: relative;
  background: var(--bg-card);
}

#epubView { width: 100%; height: 100%; overflow: hidden; }
#epubView > * { width: 100% !important; height: 100% !important; }
#epubView iframe { width: 100% !important; height: 100% !important; }

#pdfView {
  width: 100%; height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
  gap: 10px;
}
#pdfView canvas {
  max-width: 100%;
  box-shadow: var(--shadow);
  border-radius: 4px;
}
#pdfView .pdf-page-label {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
  padding: 4px 0;
}

#textView {
  width: 100%; height: 100%;
  overflow-y: auto;
  padding: 32px 48px;
  font-size: 16px;
  line-height: 1.8;
  color: var(--text);
  max-width: 900px;
  margin: 0 auto;
}
#textView h1, #textView h2, #textView h3 { margin: 24px 0 12px; }
#textView p { margin: 8px 0; text-indent: 2em; }
#textView .mobi-download-msg {
  background: var(--primary-light);
  border: 1px solid var(--primary);
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  margin-bottom: 20px;
}
#textView .mobi-download-msg a {
  display: inline-block;
  padding: 8px 20px;
  background: var(--primary);
  color: #fff;
  text-decoration: none;
  border-radius: 6px;
  margin-top: 10px;
}

/* ============================================================
   Bottom Bar
   ============================================================ */
.bottom-bar {
  background: var(--bg-card);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.control-bar {
  height: var(--bottom-bar-height);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 16px;
}

/* 左侧工具组靠左，翻页组靠右 */
.ctrl-group-left {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.ctrl-group-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.ctrl-btn {
  padding: 6px 14px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--text-secondary);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.2s;
  white-space: nowrap;
  flex-shrink: 0;
}
.ctrl-btn:hover {
  background: var(--primary-light);
  color: var(--primary);
  border-color: var(--primary);
}
.ctrl-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.ctrl-btn:disabled:hover {
  background: var(--bg-card);
  color: var(--text-secondary);
  border-color: var(--border);
}

.progress-text {
  font-size: 13px;
  color: var(--text-secondary);
  min-width: 60px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.zoom-label {
  font-size: 12px;
  color: var(--text-secondary);
  min-width: 36px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
}

.slider-wrapper { flex: 1; display: flex; align-items: center; }

.progress-slider {
  width: 100%;
  height: 6px;
  -webkit-appearance: none;
  appearance: none;
  background: var(--border);
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}
.progress-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}
.progress-slider::-moz-range-thumb {
  width: 16px; height: 16px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  border: 2px solid #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.page-info { font-size: 11px; color: var(--text-muted); min-width: 40px; }
.mode-divider { width: 1px; height: 20px; background: var(--border); flex-shrink: 0; }

/* ============================================================
   TOC Panel
   ============================================================ */
.toc-panel {
  display: none;
  position: fixed;
  top: var(--header-height);
  left: var(--sidebar-width);
  width: 320px;
  max-height: 60vh;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 0 0 var(--radius) var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 200;
  flex-direction: column;
  overflow: hidden;
  transition: left 0.25s ease;
}
.sidebar-hidden .toc-panel {
  left: 0;
}
.toc-panel.show { display: flex; }
.toc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
}
.toc-close {
  background: none; border: none;
  font-size: 18px; cursor: pointer;
  color: var(--text-muted); padding: 2px 6px; border-radius: 4px;
}
.toc-close:hover { background: var(--border); }
.toc-body { overflow-y: auto; padding: 4px 0; max-height: 50vh; }
.toc-body ul { list-style: none; padding: 0; margin: 0; }
.toc-body li {
  padding: 8px 16px 8px 24px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
  border-left: 2px solid transparent;
  transition: all 0.15s;
}
.toc-body li:hover { background: var(--primary-light); color: var(--text); }
.toc-body li.active {
  background: var(--primary-light);
  color: var(--primary);
  border-left-color: var(--primary);
  font-weight: 500;
}

/* --- Scrollbar --- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* --- Responsive --- */
@media (max-width: 768px) {
  .sidebar { width: 100%; max-height: 40vh; border-right: none; border-bottom: 1px solid var(--border); }
  .reader-panel { flex: 1; }
  #textView { padding: 16px 20px; }
  .book-meta-bar { padding: 6px 12px; flex-wrap: wrap; }
  .book-meta-bar h2 { font-size: 14px; }
  .control-bar { flex-wrap: wrap; height: auto; padding: 6px 8px; gap: 4px; }
  .slider-wrapper { max-width: none; }
  .toc-panel { left: 0; width: 100%; max-height: 50vh; }
  .tree-item { padding-left: 30px; }
}
