/* ===== CSS Variables & Themes ===== */
:root {
  --font-serif: "Noto Serif SC", "Source Han Serif SC", "Songti SC", STSong, SimSun, serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --radius: 12px;
  --transition: 0.25s ease;
  --header-h: 52px;
  --toolbar-h: 56px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  /* 默认浅色（JS 加载后会覆盖） */
  --bg: #f5f0e8;
  --bg-elevated: #fffdf8;
  --text: #2c2416;
  --text-secondary: #7a6f5e;
  --accent: #a0522d;
  --accent-soft: rgba(160,82,45,0.15);
  --border: rgba(44,36,22,0.12);
  --overlay: rgba(44,36,22,0.45);
  --toolbar-bg: rgba(255,253,248,0.94);
  --header-bg: rgba(255,253,248,0.94);
}

/* Chapter scroll progress */
.read-progress {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  z-index: 150;
  background: var(--border);
  pointer-events: none;
}

.read-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.1s linear;
}


[data-font="serif"] .content-body { font-family: var(--font-serif); }
[data-font="sans"] .content-body { font-family: var(--font-sans); }
[data-font="kai"] .content-body { font-family: "KaiTi", "STKaiti", "楷体", var(--font-serif); }

/* Brightness overlay */
.brightness-overlay {
  position: fixed;
  inset: 0;
  background: #000;
  opacity: var(--brightness-dim, 0);
  pointer-events: none;
  z-index: 500;
  transition: opacity 0.2s;
}

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

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  height: 100%;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  height: 100%;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button, input { font: inherit; border: none; background: none; color: inherit; }
ul { list-style: none; }

/* ===== App Layout ===== */
.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  height: 100vh;
  background: var(--bg);
  color: var(--text);
  transition: background var(--transition), color var(--transition);
}

.app.reading .header,
.app.reading .toolbar {
  transition: transform var(--transition), opacity var(--transition);
}

.app.reading.immersive .header {
  transform: translateY(calc(-100% - var(--safe-top)));
  opacity: 0;
  pointer-events: none;
}

.app.reading.immersive .toolbar {
  transform: translateY(calc(100% + var(--safe-bottom)));
  opacity: 0;
  pointer-events: none;
}

.app.reading.immersive .quick-settings {
  transform: translateY(calc(100% + var(--safe-bottom)));
  opacity: 0;
  pointer-events: none;
}

.theme-dots {
  display: flex;
  gap: 10px;
  flex: 1;
  flex-wrap: wrap;
  align-items: center;
}

/* ===== Header ===== */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 8px;
  height: calc(var(--header-h) + var(--safe-top));
  padding: var(--safe-top) var(--safe-right) 0 var(--safe-left);
  padding-left: max(8px, var(--safe-left));
  padding-right: max(8px, var(--safe-right));
  background: var(--header-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.header-center {
  flex: 1;
  min-width: 0;
  text-align: center;
}

.book-title {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chapter-label {
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  flex-shrink: 0;
  cursor: pointer;
  transition: background 0.15s;
}

.icon-btn:active { background: var(--border); }

/* ===== Shelf ===== */
.shelf {
  flex: 1;
  overflow-y: auto;
  padding: calc(var(--header-h) + var(--safe-top) + 24px) 24px calc(24px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
}

.shelf-cover {
  perspective: 800px;
}

.cover-art {
  width: 160px;
  height: 220px;
  background: linear-gradient(145deg, #2d1f0f 0%, #5c3d1e 40%, #8b5a2b 100%);
  border-radius: 4px 12px 12px 4px;
  box-shadow:
    4px 8px 24px rgba(0,0,0,0.25),
    inset -2px 0 8px rgba(0,0,0,0.3);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #f5e6c8;
  position: relative;
}

.cover-art::before {
  content: "";
  position: absolute;
  left: 8px;
  top: 0;
  bottom: 0;
  width: 3px;
  background: rgba(0,0,0,0.2);
  border-radius: 2px;
}

.cover-days {
  font-size: 3.5rem;
  font-weight: 900;
  line-height: 1;
  font-family: var(--font-serif);
}

.cover-sub {
  font-size: 1.1rem;
  letter-spacing: 0.3em;
  margin-top: 4px;
  font-family: var(--font-serif);
}

.shelf-info {
  width: 100%;
  max-width: 360px;
  text-align: center;
}

.shelf-info h2 {
  font-size: 1.5rem;
  font-family: var(--font-serif);
  margin-bottom: 4px;
}

.shelf-author {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-bottom: 8px;
}

.shelf-meta {
  color: var(--text-secondary);
  font-size: 0.85rem;
  margin-bottom: 20px;
}

.shelf-progress {
  margin-bottom: 20px;
}

.progress-bar {
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 6px;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s;
}

.progress-text {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.btn-primary, .btn-secondary {
  display: block;
  width: 100%;
  padding: 14px;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  margin-bottom: 10px;
}

.btn-primary:active { transform: scale(0.98); }

.btn-secondary {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-secondary:active { opacity: 0.8; }

/* ===== Reader ===== */
.reader {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding-top: calc(var(--header-h) + var(--safe-top));
  padding-bottom: calc(var(--toolbar-h) + var(--safe-bottom) + 16px);
  scroll-behavior: smooth;
}

.reader[data-margin="narrow"] .content { padding-left: 16px; padding-right: 16px; }
.reader[data-margin="normal"] .content { padding-left: 24px; padding-right: 24px; }
.reader[data-margin="wide"] .content { padding-left: 36px; padding-right: 36px; }

.content {
  max-width: 720px;
  margin: 0 auto;
  padding: 20px 24px 40px;
}

.content-title {
  font-family: var(--font-serif);
  font-size: 1.25rem;
  font-weight: 700;
  text-align: center;
  margin-bottom: 28px;
  color: var(--text);
}

.content-body {
  font-family: var(--font-serif);
  font-size: var(--reader-font-size, 18px);
  line-height: var(--reader-line-height, 1.9);
  color: var(--text);
  text-align: justify;
  word-break: break-all;
  hyphens: auto;
}

.content-body p {
  text-indent: 2em;
  margin-bottom: var(--paragraph-gap, 0.6em);
}

.content-body p:first-child {
  text-indent: 0;
}

/* Floating Aa button — outside reader to avoid tap conflicts */
.float-aa {
  position: fixed;
  right: calc(16px + var(--safe-right));
  bottom: calc(20px + var(--safe-bottom));
  z-index: 350;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  box-shadow: 0 2px 16px rgba(0,0,0,0.18);
  font-size: 1rem;
  font-weight: 700;
  font-family: var(--font-serif);
  color: var(--text);
  cursor: pointer;
  opacity: 0.92;
  transition: opacity 0.2s, transform 0.15s;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.float-aa:active { transform: scale(0.95); }

.app.reading:not(.immersive) .float-aa {
  bottom: calc(var(--toolbar-h) + 140px + var(--safe-bottom));
}

.app.reading .float-aa {
  opacity: 1;
  pointer-events: auto;
}

/* Quick settings bar */
.quick-settings {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--toolbar-h) + var(--safe-bottom));
  z-index: 110;
  background: var(--toolbar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding: 12px 16px 10px;
  transition: transform var(--transition), opacity var(--transition);
}

.quick-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

.quick-row:last-of-type { margin-bottom: 8px; }

.quick-label {
  font-size: 0.75rem;
  color: var(--text-secondary);
  min-width: 28px;
  flex-shrink: 0;
}

.quick-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
  min-width: 0;
}

.quick-step {
  width: 36px;
  height: 32px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

.quick-step:active { opacity: 0.7; }

.quick-val {
  font-size: 0.8rem;
  color: var(--text-secondary);
  min-width: 24px;
  text-align: center;
}

.quick-icon { color: var(--text-secondary); flex-shrink: 0; }

.theme-dots {
  display: flex;
  gap: 12px;
  flex: 1;
}

.theme-dot {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.15s;
  flex-shrink: 0;
}

.theme-dot:active { transform: scale(0.92); }

.theme-dot.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--bg-elevated), 0 0 0 4px var(--accent);
}

.theme-dot[data-theme="paper"] { background: #f5f0e8; }
.theme-dot[data-theme="day"] { background: #ffffff; border-color: #ddd; }
.theme-dot[data-theme="sepia"] { background: #f4ecd8; }
.theme-dot[data-theme="night"] { background: #1a1a1e; }
.theme-dot[data-theme="green"] { background: #c7edcc; }
.theme-dot[data-theme="ink"] { background: #2b2b2b; }

.theme-dot-custom {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f0e8 50%, #1a1a1e 50%);
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
}

.theme-dot-custom.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--bg-elevated), 0 0 0 4px var(--accent);
}

.quick-more {
  width: 100%;
  padding: 8px;
  font-size: 0.8rem;
  color: var(--accent);
  text-align: center;
  cursor: pointer;
  border-radius: 8px;
}

.quick-more:active { background: var(--border); }

.app.reading:not(.immersive) .reader {
  padding-bottom: calc(var(--toolbar-h) + 130px + var(--safe-bottom));
}

/* Tap zones — visual guide only, does not block scroll */
.reader-tap-zones {
  display: none;
}

/* ===== Toolbar ===== */
.toolbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  align-items: stretch;
  height: calc(var(--toolbar-h) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--toolbar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}

.tool-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 0.65rem;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color 0.15s;
}

.tool-btn:active { color: var(--accent); }

.tool-btn span { font-size: 0.65rem; }

/* ===== Drawer ===== */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: var(--overlay);
  animation: fadeIn 0.2s;
}

.drawer {
  position: fixed;
  z-index: 201;
  background: var(--bg-elevated);
  display: flex;
  flex-direction: column;
  animation: slideUp 0.3s ease;
}

.drawer-toc {
  bottom: 0;
  left: 0;
  right: 0;
  max-height: 75dvh;
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.15);
}

.drawer-settings {
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.15);
}

.drawer-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.drawer-header h3 {
  font-size: 1rem;
  font-weight: 600;
}

.drawer-meta {
  flex: 1;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.drawer-search {
  padding: 8px 16px;
  flex-shrink: 0;
}

.drawer-search input {
  width: 100%;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  outline: none;
}

.drawer-search input:focus {
  border-color: var(--accent);
}

.toc-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0 8px  calc(16px + var(--safe-bottom));
}

.toc-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

.toc-item:active,
.toc-item.active {
  background: var(--border);
}

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

.toc-num {
  font-size: 0.75rem;
  color: var(--text-secondary);
  min-width: 36px;
  text-align: right;
  flex-shrink: 0;
}

.toc-title {
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toc-item.read .toc-title::after {
  content: " ✓";
  color: var(--accent);
  font-size: 0.75rem;
}

/* ===== Settings ===== */
.settings-body {
  padding: 16px 20px calc(24px + var(--safe-bottom));
  max-height: 65dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.setting-group {
  margin-bottom: 24px;
}

.setting-group label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.setting-group input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.setting-hint {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 6px;
}

.setting-hint-inline {
  font-weight: normal;
  opacity: 0.8;
}

.custom-bg-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
}

.color-picker-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  cursor: pointer;
}

.color-picker-label input[type="color"] {
  width: 40px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  background: none;
}

.custom-bg-preview {
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-family: monospace;
}

.toggle-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
}

.toggle-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.btn-reset {
  width: 100%;
  padding: 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-secondary);
  font-size: 0.9rem;
  cursor: pointer;
}

.btn-reset:active { opacity: 0.8; }

/* UI controls: never trigger page turn underneath */
.quick-settings,
.toolbar,
.header,
.drawer,
.float-aa,
.overlay {
  touch-action: manipulation;
}

.theme-options {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.theme-chip {
  padding: 8px 16px;
  border-radius: 20px;
  border: 2px solid var(--border);
  font-size: 0.85rem;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.theme-chip.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 600;
}

.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}

.segmented button {
  flex: 1;
  padding: 10px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.segmented button.active {
  background: var(--accent);
  color: #fff;
}

.app[data-dark="true"] .theme-chip.active {
  background: rgba(255,255,255,0.1);
  border-color: rgba(255,255,255,0.22);
  color: var(--text);
}

.app[data-dark="true"] .segmented button.active {
  background: rgba(255,255,255,0.12);
  color: var(--text);
}

.app[data-dark="true"] .theme-dot.active {
  border-color: rgba(255,255,255,0.45);
  box-shadow: 0 0 0 2px var(--bg-elevated), 0 0 0 3px rgba(255,255,255,0.35);
}

.app[data-dark="true"] .setting-group input[type="range"],
.app[data-dark="true"] .quick-row input[type="range"] {
  accent-color: #8a8580;
}

.app[data-dark="true"] .read-progress-fill {
  background: rgba(255,255,255,0.35);
}

.app[data-dark="true"] .tool-btn:active,
.app[data-dark="true"] .quick-more {
  color: var(--text);
}

.app[data-dark="true"] .btn-primary {
  background: rgba(255,255,255,0.15);
  color: var(--text);
  border: 1px solid var(--border);
}

/* ===== Loading & Toast ===== */
.loading {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: var(--overlay);
  color: #fff;
  font-size: 0.9rem;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.toast {
  position: fixed;
  bottom: calc(var(--toolbar-h) + var(--safe-bottom) + 20px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 400;
  padding: 10px 20px;
  background: rgba(0,0,0,0.75);
  color: #fff;
  border-radius: 20px;
  font-size: 0.85rem;
  white-space: nowrap;
  animation: fadeIn 0.2s;
  pointer-events: none;
}

.app.reading.immersive .toast {
  bottom: calc(var(--safe-bottom) + 20px);
}

/* ===== Animations ===== */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Desktop ===== */
@media (min-width: 768px) {
  .drawer-toc {
    left: auto;
    right: 0;
    top: 0;
    bottom: 0;
    max-height: none;
    width: 380px;
    border-radius: 0;
    animation: slideLeft 0.3s ease;
  }

  @keyframes slideLeft {
    from { transform: translateX(100%); }
    to { transform: translateX(0); }
  }

  .shelf {
    flex-direction: row;
    justify-content: center;
    align-items: flex-start;
    padding-top: calc(var(--header-h) + var(--safe-top) + 48px);
    gap: 48px;
  }

  .content {
    padding-top: 32px;
  }
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

[hidden] { display: none !important; }
