/* ========== Base ========== */
:root {
  --sidebar-w: 280px;
  --content-w: 720px;

  /* Font scale, dynamically adjusted by JS */
  --font-scale: 1;
  --base-size: 17px;

  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
}

/* Dark (default) */
[data-theme="dark"] {
  --bg: #0d0f12;
  --bg-elev: #15181d;
  --bg-hover: #1d2127;
  --text: #d8dce3;
  --text-soft: #8a92a0;
  --text-faint: #5a6270;
  --accent: #e4a62a;
  --accent-dim: #b7831e;
  --border: #252932;
  --border-soft: #1a1d23;
  --quote: #c0a263;
  --code-bg: #1a1d23;
  --progress: linear-gradient(90deg, #e4a62a, #d23f3f);
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

/* Light */
[data-theme="light"] {
  --bg: #f8f4ec;
  --bg-elev: #fffbf3;
  --bg-hover: #ece5d4;
  --text: #1d2028;
  --text-soft: #555a66;
  --text-faint: #8a8f9a;
  --accent: #8a5a00;
  --accent-dim: #a67a1f;
  --border: #d5cbba;
  --border-soft: #e5dcc7;
  --quote: #7a5a1a;
  --code-bg: #ece5d4;
  --progress: linear-gradient(90deg, #8a5a00, #a32222);
  --shadow: 0 10px 40px rgba(90, 70, 30, 0.15);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei",
    "Source Han Sans CN", "Noto Sans CJK SC", sans-serif;
  font-size: calc(var(--base-size) * var(--font-scale));
  line-height: 1.85;
  letter-spacing: 0.015em;
  min-height: 100vh;
  transition: background 0.3s var(--ease), color 0.3s var(--ease);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ========== Progress bar ========== */
.progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 3px;
  background: transparent;
  z-index: 100;
  pointer-events: none;
}
.progress-bar-fill {
  height: 100%;
  width: 0%;
  background: var(--progress);
  transition: width 0.15s ease-out;
}

/* ========== Sidebar ========== */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-w);
  height: 100vh;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform 0.35s var(--ease);
  z-index: 50;
}

.sidebar-header {
  padding: 28px 24px 20px;
  border-bottom: 1px solid var(--border-soft);
}

.book-title {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.title-main {
  font-family: "STKaiti", "KaiTi", "楷体", serif;
  font-size: 34px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.1em;
  line-height: 1;
}

.title-sub {
  font-size: 13px;
  color: var(--text-faint);
  letter-spacing: 0.4em;
  font-weight: 400;
}

.chapter-list {
  flex: 1;
  overflow-y: auto;
  padding: 12px 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.chapter-list::-webkit-scrollbar { width: 6px; }
.chapter-list::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.chapter-item {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 12px 24px;
  color: var(--text-soft);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  border-left: 2px solid transparent;
  text-decoration: none;
}
.chapter-item:hover {
  background: var(--bg-hover);
  color: var(--text);
}
.chapter-item.active {
  background: var(--bg-hover);
  color: var(--accent);
  border-left-color: var(--accent);
}

.chapter-num {
  font-size: 12px;
  color: var(--text-faint);
  font-family: "SF Mono", Menlo, monospace;
  min-width: 28px;
  letter-spacing: 0.05em;
}
.chapter-item.active .chapter-num { color: var(--accent-dim); }

.chapter-title {
  font-size: 15px;
  letter-spacing: 0.03em;
}

.sidebar-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-link {
  color: var(--text-soft);
  text-decoration: none;
  font-size: 13px;
  padding: 6px 0;
  transition: color 0.15s;
}
.sidebar-link:hover { color: var(--accent); }

/* ========== Sidebar Toggle ========== */
.sidebar-toggle {
  position: fixed;
  top: 20px;
  left: 20px;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  cursor: pointer;
  z-index: 51;
  display: none;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.sidebar-toggle:hover { background: var(--bg-hover); }

/* ========== Main Reader ========== */
.reader {
  margin-left: var(--sidebar-w);
  min-height: 100vh;
  padding: 48px 48px 120px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.toolbar {
  position: sticky;
  top: 16px;
  z-index: 10;
  margin-left: auto;
  display: flex;
  gap: 8px;
  margin-bottom: 32px;
  align-self: flex-end;
}

.toolbar-group {
  display: flex;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  box-shadow: var(--shadow);
}

.tool-btn {
  background: transparent;
  border: none;
  color: var(--text-soft);
  padding: 6px 12px;
  border-radius: 5px;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  transition: background 0.15s, color 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
}
.tool-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.theme-icon { font-size: 16px; line-height: 1; }
[data-theme="dark"] .light-icon { display: none; }
[data-theme="light"] .dark-icon { display: none; }

/* ========== Content ========== */
.content {
  width: 100%;
  max-width: var(--content-w);
}

/* ---- Home view ---- */
.home-view {
  padding: 80px 0;
  text-align: center;
  animation: fadeIn 0.6s var(--ease);
}

.home-title {
  font-family: "STKaiti", "KaiTi", "楷体", serif;
  font-size: clamp(64px, 12vw, 120px);
  font-weight: 600;
  color: var(--accent);
  margin: 0;
  letter-spacing: 0.15em;
  line-height: 1.1;
}

.home-subtitle {
  color: var(--text-soft);
  font-size: 16px;
  margin: 24px auto 60px;
  max-width: 480px;
  letter-spacing: 0.05em;
}

.home-hook {
  max-width: 560px;
  margin: 0 auto;
  text-align: left;
  padding: 32px 40px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 12px;
  line-height: 2;
  color: var(--text);
}
.home-hook p { margin: 0 0 14px; }
.home-hook p:last-child { margin-bottom: 0; }
.home-hook strong { color: var(--accent); font-weight: 600; }
.home-hook em { color: var(--text-soft); font-style: italic; }

.home-cta {
  margin: 56px 0 20px;
  padding: 14px 40px;
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 999px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  letter-spacing: 0.1em;
  font-family: inherit;
  transition: transform 0.2s var(--ease), background 0.2s;
  box-shadow: 0 6px 20px rgba(228, 166, 42, 0.3);
}
.home-cta:hover {
  transform: translateY(-2px);
  background: var(--accent-dim);
}

.home-meta {
  display: flex;
  justify-content: center;
  gap: 10px;
  color: var(--text-faint);
  font-size: 13px;
  letter-spacing: 0.05em;
}

/* ---- Chapter view ---- */
.chapter-view {
  width: 100%;
  animation: fadeIn 0.5s var(--ease);
}

.chapter-body {
  font-size: inherit;
  line-height: 2;
  color: var(--text);
}

.chapter-body h1 {
  font-family: "STKaiti", "KaiTi", "楷体", serif;
  font-size: 2.2em;
  color: var(--accent);
  text-align: center;
  margin: 20px 0 60px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  letter-spacing: 0.1em;
}

.chapter-body h2 {
  font-size: 1.05em;
  color: var(--text-soft);
  text-align: center;
  margin: 48px 0 28px;
  font-weight: 400;
  letter-spacing: 0.4em;
  position: relative;
}
.chapter-body h2::before,
.chapter-body h2::after {
  content: "";
  display: inline-block;
  width: 20px;
  height: 1px;
  background: var(--border);
  vertical-align: middle;
  margin: 0 16px;
}

.chapter-body p {
  margin: 1.2em 0;
  text-indent: 2em;
}

/* Text blocks without indent (dialogue, special markers) */
.chapter-body p:has(> strong:first-child:last-child) {
  text-indent: 0;
  text-align: center;
}

.chapter-body blockquote {
  margin: 1.5em 0;
  padding: 0.8em 1.4em;
  border-left: 3px solid var(--accent);
  background: var(--bg-elev);
  color: var(--quote);
  border-radius: 4px;
  font-style: normal;
}
.chapter-body blockquote p {
  margin: 0.5em 0;
  text-indent: 0;
}

.chapter-body strong {
  color: var(--accent);
  font-weight: 600;
}

.chapter-body em {
  color: var(--text);
  font-style: italic;
}

.chapter-body hr {
  margin: 60px auto;
  width: 40%;
  border: none;
  border-top: 1px solid var(--border);
  position: relative;
}
.chapter-body hr::after {
  content: "✦";
  position: absolute;
  top: -12px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg);
  padding: 0 12px;
  color: var(--accent);
  font-size: 14px;
}

.chapter-body code {
  background: var(--code-bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: "SF Mono", Menlo, monospace;
  font-size: 0.9em;
  color: var(--quote);
}

.chapter-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5em 0;
  font-size: 0.95em;
}
.chapter-body th, .chapter-body td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border-soft);
  text-align: left;
}
.chapter-body th {
  color: var(--accent);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

.chapter-body em:last-child:first-child,
.chapter-body p:last-child em {
  display: block;
  text-align: center;
  color: var(--text-faint);
}

/* Navigation */
.chapter-nav {
  display: flex;
  gap: 12px;
  margin-top: 80px;
  padding-top: 32px;
  border-top: 1px solid var(--border);
  justify-content: space-between;
  align-items: center;
}

.nav-btn {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 24px;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  letter-spacing: 0.05em;
  transition: background 0.15s, transform 0.15s, border-color 0.15s;
}
.nav-btn:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--accent-dim);
  transform: translateY(-1px);
}
.nav-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}
.nav-btn.nav-home {
  background: transparent;
  color: var(--text-soft);
}

/* ========== Animations ========== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ========== Responsive ========== */
@media (max-width: 900px) {
  .sidebar {
    transform: translateX(-100%);
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-toggle { display: flex; }
  .reader {
    margin-left: 0;
    padding: 80px 20px 100px;
  }
  .toolbar {
    margin-right: 0;
  }
  .home-hook {
    padding: 24px 20px;
  }
}

@media (max-width: 500px) {
  .chapter-body h1 { font-size: 1.8em; }
  .home-title { font-size: 72px; }
  :root { --base-size: 16px; }
}

/* Overlay for mobile sidebar */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 49;
  opacity: 0;
  transition: opacity 0.3s var(--ease);
}
.sidebar-overlay.show {
  display: block;
  opacity: 1;
}

/* ========== Selection ========== */
::selection {
  background: var(--accent);
  color: var(--bg);
}
