:root {
  --bg: #faf8f5;
  --fg: #2c2c2c;
  --muted: #888;
  --accent: #c0392b;
  --card-bg: #fff;
  --border: #e8e4df;
  --sidebar-bg: #f5f1ec;
  --hover-bg: #ede8e1;
  --active-bg: #e0d8cf;
  --radius: 6px;
  --max-width: 720px;
  --sidebar-width: 300px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: "Noto Serif SC", "Source Han Serif CN", Georgia, "Times New Roman", serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.8;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--accent);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

.loading {
  color: var(--muted);
  text-align: center;
  padding: 2rem;
}

/* === 主页 === */
.home-container {
  max-width: 960px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

.home-header {
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 2px solid var(--border);
}

.home-header h1 {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--fg);
}

.book-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1.5rem;
}

.book-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: box-shadow 0.2s, transform 0.15s;
  cursor: pointer;
  display: flex;
  flex-direction: column;
}

.book-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transform: translateY(-2px);
}

.book-cover {
  width: 100%;
  aspect-ratio: 5/7;
  background: var(--sidebar-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.book-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.book-cover-text {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--accent);
  color: #fff;
  font-size: 2.5rem;
  font-weight: 700;
  font-family: serif;
}

.book-info {
  padding: 0.5rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.book-info h3 {
  font-size: 1.05rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-info .author {
  color: var(--muted);
  font-size: 0.85rem;
}

.book-info .desc {
  font-size: 0.82rem;
  color: var(--muted);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-info .chapter-count {
  font-size: 0.78rem;
  color: var(--accent);
  margin-top: auto;
}

/* === 阅读器 === */
.reader-layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* 侧边栏 - 默认隐藏，点击菜单按钮打开 */
.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  width: var(--sidebar-width);
  height: 100vh;
  z-index: 100;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.25s ease;
}

.sidebar.open {
  transform: translateX(0);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1rem 0.5rem;
  border-bottom: 1px solid var(--border);
}

.back-link {
  font-size: 0.9rem;
  color: var(--accent);
}

.sidebar-close {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--muted);
  padding: 0.2rem 0.4rem;
  border-radius: 3px;
}

.sidebar-close:hover {
  background: var(--hover-bg);
}

.book-title {
  font-size: 1rem;
  font-weight: 600;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  color: var(--fg);
}

.toc {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem 0;
}

.toc-group {
  list-style: none;
}

.toc-item {
  display: flex;
  align-items: center;
  padding: 0.4rem 1rem;
  font-size: 0.86rem;
  color: var(--fg);
  border: none;
  background: none;
  text-align: left;
  width: 100%;
  cursor: pointer;
  line-height: 1.5;
  transition: background 0.15s;
}

.toc-item:hover {
  background: var(--hover-bg);
}

.toc-item.active {
  background: var(--active-bg);
  color: var(--accent);
  font-weight: 600;
}

.toc-item[data-level="2"] {
  padding-left: 1.8rem;
  font-size: 0.82rem;
}

.toc-item[data-level="3"] {
  padding-left: 2.6rem;
  font-size: 0.8rem;
}

.toc-item[data-level="4"] {
  padding-left: 3.4rem;
  font-size: 0.78rem;
}

.toc-item.no-link {
  cursor: default;
  font-weight: 600;
  color: var(--fg);
}

.toc-item.no-link:hover {
  background: transparent;
}

.toc-toggle {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.7rem;
  color: var(--muted);
  padding: 0.1rem 0.3rem;
  margin-right: 0.2rem;
  border-radius: 2px;
  transition: transform 0.2s;
  flex-shrink: 0;
}

.toc-toggle:hover {
  background: var(--hover-bg);
}

.toc-toggle.collapsed {
  transform: rotate(-90deg);
}

.toc-children {
  list-style: none;
}

.toc-children.collapsed {
  display: none;
}

/* 主内容区 */
.content-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  overflow-x: hidden !important;
  position: relative;
}

.content-header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1.5rem;
  background: var(--card-bg);
  max-width: var(--max-width);
  margin: 0 auto;
  width: 100%;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.content-header.hidden {
  transform: translateY(-100%);
  opacity: 0;
  pointer-events: none;
}

.content-header .chapter-indicator {
  flex: 1;
}

.menu-btn {
  background: none;
  border: 1px solid var(--border);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.3rem 0.6rem;
  border-radius: var(--radius);
  color: var(--fg);
}

.menu-btn:hover {
  background: var(--hover-bg);
}

.chapter-indicator {
  font-size: 0.85rem;
  color: var(--muted);
}

/* epub.js 渲染容器 - !important 覆盖 epub.js 内联样式 */
.epub-container {
  flex: 1;
  min-height: 0;
  overflow-x: hidden !important;
  overflow-y: auto !important;
  position: relative;
  max-width: var(--max-width);
  margin: 0 auto;
  width: 100%;
  padding: 0 1rem;
  box-sizing: border-box;
}

.epub-container .epub-view {
  max-width: 100% !important;
  overflow: hidden !important;
}

.epub-container iframe {
  border: none;
  overflow: hidden !important;
  max-width: 100% !important;
  box-sizing: border-box;
}

/* 底部导航 */
.chapter-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  background: var(--card-bg);
  max-width: var(--max-width);
  margin: 0 auto;
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 10;
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.chapter-nav.hidden {
  transform: translateY(100%);
  opacity: 0;
  pointer-events: none;
}

.nav-btn {
  background: none;
  border: 1px solid var(--border);
  padding: 0.5rem 1.2rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--fg);
  transition: background 0.15s;
}

.nav-btn:hover {
  background: var(--hover-bg);
}

.nav-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.progress {
  font-size: 0.85rem;
  color: var(--muted);
}

/* 遮罩 */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
  z-index: 50;
}

.overlay.active {
  display: block;
}

/* === 设置面板 === */
.settings-btn {
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  color: var(--muted);
  padding: 0.2rem 0.3rem;
  border-radius: 3px;
  margin-left: auto;
  transition: color 0.15s;
}

.settings-btn:hover {
  color: var(--fg);
}

.settings-panel {
  position: absolute;
  right: 0;
  top: 100%;
  width: 240px;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  z-index: 20;
  display: none;
  overflow: hidden;
}

.settings-panel.open {
  display: block;
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.9rem;
}

.settings-close {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  color: var(--muted);
  padding: 0.1rem 0.3rem;
  border-radius: 3px;
}

.settings-close:hover {
  background: var(--hover-bg);
}

.settings-item {
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-item label {
  font-size: 0.85rem;
  color: var(--fg);
}

.font-size-control {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.font-btn {
  background: none;
  border: 1px solid var(--border);
  padding: 0.25rem 0.6rem;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--fg);
  transition: background 0.15s;
}

.font-btn:hover {
  background: var(--hover-bg);
}

.font-size-value {
  font-size: 0.82rem;
  color: var(--muted);
  min-width: 2.5rem;
  text-align: center;
}

/* === 响应式 === */
@media (max-width: 768px) {
  .epub-container {
    padding: 1rem 0;
  }

  .home-container {
    padding: 1.5rem 1rem;
  }

  .book-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* 滚动条美化 */
.sidebar::-webkit-scrollbar,
.epub-container::-webkit-scrollbar {
  width: 4px;
}

.sidebar::-webkit-scrollbar-thumb,
.epub-container::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}