:root {
  --bg: #13161f;
  --bg-soft: #1a1e2a;
  --card: #1e2333;
  --text: #e8e6e3;
  --text-dim: #9aa0ad;
  --accent: #5e72eb;
  --accent-soft: rgba(94, 114, 235, 0.16);
  --border: rgba(255, 255, 255, 0.08);
  --danger: #e06c75;
  --read-fs: 20px;
  --read-font: Georgia, "Noto Serif", "Times New Roman", serif;
  --read-lh: 1.72;
}

html[data-theme='paper'] {
  --bg: #f5eedb;
  --bg-soft: #f0e7cf;
  --card: #fffaf0;
  --text: #3a352b;
  --text-dim: #8b826d;
  --accent: #b8872e;
  --accent-soft: rgba(184, 135, 46, 0.14);
  --border: rgba(90, 75, 40, 0.14);
}

html[data-theme='light'] {
  --bg: #ffffff;
  --bg-soft: #f2f3f5;
  --card: #ffffff;
  --text: #201f1c;
  --text-dim: #8b8d92;
  --accent: #4c5ecf;
  --accent-soft: rgba(76, 94, 207, 0.1);
  --border: rgba(0, 0, 0, 0.1);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Roboto", "Helvetica Neue", system-ui, -apple-system, sans-serif;
  transition: background 0.25s ease, color 0.25s ease;
}

button {
  font-family: inherit;
  border: none;
  background: none;
  color: inherit;
  cursor: pointer;
}

/* ============ Topbar ============ */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: calc(0.75rem + env(safe-area-inset-top)) 1rem 0.75rem;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}

.topbar h1 {
  margin: 0;
  flex: 1;
  font-size: 1.15rem;
  font-weight: 650;
  letter-spacing: 0.2px;
}

.count { font-size: 0.8rem; color: var(--text-dim); }

.icon-btn {
  display: grid;
  place-items: center;
  width: 2.4rem;
  height: 2.4rem;
  border-radius: 50%;
  color: var(--text);
  background: var(--bg-soft);
  transition: transform 0.1s ease, background 0.2s ease;
}
.icon-btn:active { transform: scale(0.92); }
.icon-btn.accent { background: var(--accent); color: #fff; }

/* ============ Shelf ============ */

.shelf {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 1rem;
  padding: 1rem;
}

.book {
  display: flex;
  flex-direction: column;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  transition: transform 0.12s ease;
}
.book:active { transform: scale(0.98); }

.book-cover {
  position: relative;
  aspect-ratio: 3 / 4.2;
  display: grid;
  place-items: center;
  color: #fff;
}

.cover-letter {
  font-family: Georgia, serif;
  font-size: 3.4rem;
  font-weight: 600;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
}

.cover-progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 4px;
  background: rgba(255, 255, 255, 0.25);
}
.cover-fill {
  height: 100%;
  background: var(--accent);
  transition: width 0.4s ease;
}

.book-meta { padding: 0.7rem 0.8rem 0.4rem; flex: 1; }
.book-meta h3 {
  margin: 0 0 0.25rem;
  font-size: 0.92rem;
  font-weight: 600;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-sub { margin: 0.1rem 0 0; font-size: 0.78rem; color: var(--text-dim); }

.book-actions {
  display: flex;
  gap: 0.5rem;
  padding: 0.55rem 0.8rem 0.8rem;
}

.btn {
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.55rem 0.9rem;
  transition: filter 0.15s ease, transform 0.1s ease;
}
.btn:active { transform: scale(0.96); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: var(--bg-soft); color: var(--text-dim); }
.btn.small { padding: 0.45rem 0.75rem; font-size: 0.78rem; flex: 1; }

/* ============ Empty state ============ */

/* ============ Progress ============ */

.progress {
  margin: 0.5rem 1rem 0;
  padding: 0.8rem 1rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.progress-head {
  display: flex;
  justify-content: space-between;
  font-size: 0.8rem;
  color: var(--text-dim);
  margin-bottom: 0.5rem;
}
.progress-track {
  height: 6px;
  border-radius: 99px;
  background: var(--bg-soft);
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  border-radius: 99px;
  transition: width 0.2s ease;
}

/* ============ Reader ============ */

.reader-body { overscroll-behavior-y: contain; }

.toolbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: calc(0.55rem + env(safe-area-inset-top)) 0.8rem 0.55rem;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
  transition: transform 0.24s ease;
}
.toolbar.hide { transform: translateY(-120%); }

.toolbar-title {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.25;
}
.toolbar-title > span:first-child {
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.toolbar-sub { font-size: 0.72rem; color: var(--text-dim); }

.reader-main {
  padding-top: 4.2rem;
  padding-bottom: 6rem;
}

.pg {
  position: relative;
  max-width: 44rem;
  margin: 0 auto;
  padding: 3.2rem 1.35rem 0;
  content-visibility: auto;
  contain-intrinsic-size: auto 800px;
}
.pg + .pg::before {
  content: "";
  display: block;
  margin: 0 auto 1.9rem;
  width: 2rem;
  height: 1px;
  background: var(--text-dim);
  opacity: 0.4;
}

.pg-inner {
  font-family: var(--read-font);
  font-size: var(--read-fs);
  line-height: var(--read-lh);
  letter-spacing: 0.15px;
  text-align: left;
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  transition: font-size 0.15s ease;
}

.pg-empty {
  font-family: inherit;
  color: var(--text-dim);
  font-style: italic;
  text-align: center;
  opacity: 0.75;
}

.pg-mark {
  text-align: center;
  font-size: 0.72rem;
  color: var(--text-dim);
  opacity: 0.55;
  padding-top: 2.2rem;
  letter-spacing: 0.2em;
}

.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 6rem 1rem;
  color: var(--text-dim);
  font-size: 0.9rem;
}
.loading-on .loading { display: none; }

.spinner {
  width: 34px; height: 34px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ============ Sheets (TOC / Settings) ============ */

.toc-sheet, .settings-sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 50;
  background: var(--card);
  border-radius: 18px 18px 0 0;
  border-top: 1px solid var(--border);
  box-shadow: 0 -8px 40px rgba(0, 0, 0, 0.35);
  transform: translateY(105%);
  transition: transform 0.24s cubic-bezier(0.2, 0.8, 0.3, 1);
  padding-bottom: env(safe-area-inset-bottom);
  max-height: 74vh;
  display: flex;
  flex-direction: column;
}
.toc-sheet.open, .settings-sheet.open { transform: none; }

.toc-sheet::before, .settings-sheet::before {
  content: "";
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: -1;
  opacity: 0;
  transition: opacity 0.24s ease;
}
.toc-sheet.open::before, .settings-sheet.open::before { opacity: 1; }

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

.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.toc-item {
  padding: 0.85rem 1.1rem;
  font-size: 0.92rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  color: var(--text);
}
.toc-item:active { background: var(--accent-soft); }

.settings-body { padding: 0.5rem 1rem; }

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.95rem;
}
.setting-row:last-child { border-bottom: none; }

.seg {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.seg button {
  padding: 0.45rem 0.7rem;
  border-radius: 9px;
  background: var(--bg-soft);
  color: var(--text-dim);
  font-size: 0.88rem;
  font-weight: 600;
  transition: all 0.15s ease;
}
.seg button.active { background: var(--accent); color: #fff; }
.seg button:active { transform: scale(0.94); }
.seg > span { min-width: 2.2rem; text-align: center; font-variant-numeric: tabular-nums; color: var(--text-dim); font-size: 0.8rem; }

.theme-row { display: flex; gap: 0.6rem; }
.theme-dot {
  width: 2rem; height: 2rem;
  border-radius: 50%;
  border: 2.5px solid transparent;
  box-shadow: inset 0 0 0 1px var(--border);
  transition: transform 0.15s ease;
}
.theme-dot:active { transform: scale(0.9); }
.theme-dot.active { border-color: var(--accent); }
.theme-dot.light { background: #fffdf8; }
.theme-dot.paper { background: #f5eedb; }
.theme-dot.dark { background: #20242f; }

/* ============ Toast ============ */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(1.4rem + env(safe-area-inset-bottom));
  transform: translate(-50%, 16px);
  background: #262a35;
  color: #f2f2f2;
  padding: 0.7rem 1.1rem;
  border-radius: 999px;
  font-size: 0.85rem;
  z-index: 90;
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
  max-width: 88vw;
  text-align: center;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

html[data-theme='paper'] .toast, html[data-theme='light'] .toast {
  background: #2e2a1f;
}
