:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --text: #1f1f1f;
  --muted: #7a7a7a;
  --accent: #b8860b;
  --accent-soft: #f0e6d2;
  --border: #e4e0d8;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  --reader-bg: #fbf9f4;
  --reader-text: #2a2a2a;
  --font-scale: 1;
  --line-height: 1.7;
  --serif: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

body[data-theme="sepia"] {
  --bg: #efe6d3;
  --surface: #f7f0e0;
  --text: #3a2f1f;
  --muted: #8a7a5c;
  --accent: #9c6b1f;
  --accent-soft: #e8dcc0;
  --border: #ddd0b4;
  --reader-bg: #f4ecd8;
  --reader-text: #4a3b26;
}

body[data-theme="dark"] {
  --bg: #121212;
  --surface: #1e1e1e;
  --text: #e8e6e1;
  --muted: #9a9a9a;
  --accent: #d4a94a;
  --accent-soft: #2a2620;
  --border: #2e2e2e;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  --reader-bg: #161616;
  --reader-text: #d8d4cc;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  transition: background 0.25s ease, color 0.25s ease;
}

.view {
  display: none;
  min-height: 100vh;
  flex-direction: column;
}
.view.active { display: flex; }

/* ===== Library ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.brand { display: flex; align-items: center; gap: 12px; }

.brand-mark {
  width: 42px; height: 42px;
  display: grid; place-items: center;
  background: var(--accent);
  color: #fff;
  font-family: var(--serif);
  font-size: 24px;
  font-weight: 700;
  border-radius: 10px;
}

.brand-text h1 {
  font-family: var(--serif);
  font-size: 20px;
  letter-spacing: 0.5px;
}
.brand-text p { font-size: 12px; color: var(--muted); letter-spacing: 1px; text-transform: uppercase; }

.icon-btn {
  width: 40px; height: 40px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  font-size: 18px;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
.icon-btn:hover { background: var(--accent-soft); }
.icon-btn:active { transform: scale(0.94); }

.library-toolbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 28px 24px 12px;
}
.library-toolbar h2 { font-family: var(--serif); font-size: 26px; }
.muted { color: var(--muted); font-size: 13px; }

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 24px;
  padding: 12px 24px 48px;
}

.book-card {
  cursor: pointer;
  border: none;
  background: none;
  text-align: left;
  font-family: inherit;
  color: inherit;
  transition: transform 0.18s ease;
}
.book-card:hover { transform: translateY(-4px); }

.book-cover {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 6px 10px 10px 6px;
  overflow: hidden;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 18px 14px;
  color: #fff;
}
.book-cover::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 8px;
  background: rgba(0, 0, 0, 0.25);
}
.book-cover .cover-title {
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.25;
  font-weight: 700;
  text-shadow: 0 1px 3px rgba(0,0,0,0.3);
}
.book-cover .cover-author {
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
  opacity: 0.85;
}

.book-meta { padding: 10px 2px 0; }
.book-meta h3 { font-size: 14px; font-weight: 600; margin-bottom: 2px; }
.book-meta p { font-size: 12px; color: var(--muted); }

.book-progress {
  margin-top: 8px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}
.book-progress span {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
}

/* ===== Reader ===== */
#reader-view { background: var(--reader-bg); }

.reader-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.reader-title { flex: 1; text-align: center; overflow: hidden; }
.reader-title h2 {
  font-family: var(--serif);
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.reader-title p { font-size: 12px; }

.reader-stage {
  flex: 1;
  display: flex;
  align-items: stretch;
  position: relative;
  overflow: hidden;
}

.page-nav {
  width: 56px;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 32px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  flex-shrink: 0;
}
.page-nav:hover { background: rgba(0,0,0,0.03); color: var(--accent); }
.page-nav:disabled { opacity: 0.2; cursor: default; }

.page-content {
  flex: 1;
  max-width: 720px;
  margin: 0 auto;
  padding: 32px 24px;
  overflow-y: auto;
  font-family: var(--serif);
  font-size: calc(19px * var(--font-scale));
  line-height: var(--line-height);
  color: var(--reader-text);
  outline: none;
  white-space: pre-wrap;
  scroll-behavior: smooth;
}
.page-content h3 {
  font-size: calc(22px * var(--font-scale));
  margin-bottom: 18px;
  color: var(--accent);
  font-weight: 600;
}
.page-content p { margin-bottom: 1em; text-align: justify; }
.page-content p:first-of-type::first-letter {
  font-size: 3em;
  float: left;
  line-height: 0.85;
  padding: 4px 8px 0 0;
  color: var(--accent);
  font-weight: 700;
}

.reader-footer {
  padding: 12px 24px 18px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.progress-track {
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
  margin-bottom: 8px;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.25s ease;
}
.footer-meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
}

/* ===== Settings panel ===== */
.settings-panel {
  position: fixed;
  bottom: 70px;
  right: 18px;
  width: 260px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 16px;
  z-index: 40;
  opacity: 0;
  visibility: hidden;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
}
.settings-panel.open {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.settings-row:last-child { border-bottom: none; }
.settings-row label { font-size: 13px; color: var(--muted); }

.stepper { display: flex; align-items: center; gap: 8px; }
.stepper button {
  width: 30px; height: 30px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.stepper button:hover { background: var(--accent-soft); }
.stepper span { font-size: 12px; min-width: 48px; text-align: center; }

.theme-swatches { display: flex; gap: 8px; }
.swatch {
  width: 26px; height: 26px;
  border-radius: 50%;
  border: 2px solid var(--border);
  cursor: pointer;
}
.swatch.light { background: #fbf9f4; }
.swatch.sepia { background: #f4ecd8; }
.swatch.dark { background: #161616; }
.swatch.active { border-color: var(--accent); }

/* ===== TOC drawer ===== */
.toc-drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(340px, 85vw);
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 50;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.25s ease;
}
.toc-drawer.open { transform: translateX(0); }
.toc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px;
  border-bottom: 1px solid var(--border);
}
.toc-header h3 { font-family: var(--serif); font-size: 18px; }
.toc-list {
  list-style: none;
  overflow-y: auto;
  padding: 8px 0;
}
.toc-list li button {
  width: 100%;
  text-align: left;
  padding: 12px 20px;
  border: none;
  background: none;
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  cursor: pointer;
  border-left: 3px solid transparent;
}
.toc-list li button:hover { background: var(--accent-soft); }
.toc-list li button.active {
  border-left-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 45;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s;
}
.overlay.active {
  opacity: 1;
  visibility: visible;
}

/* ===== Mobile ===== */
@media (max-width: 640px) {
  .page-nav { width: 36px; font-size: 26px; }
  .page-content { padding: 20px 16px; font-size: calc(17px * var(--font-scale)); }
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 16px; padding: 12px 16px 40px; }
  .app-header, .library-toolbar { padding-left: 16px; padding-right: 16px; }
}
