:root {
  --bg: #faf7f2;
  --fg: #2a2926;
  --fg-zh: #5a4a3a;
  --muted: #8a8275;
  --accent: #a4540a;
  --rule: #e6dfd2;
  --sidebar-bg: #f1ebde;
  --reading-width: 38rem;
  --base-font: 17px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a17;
    --fg: #e9e4d8;
    --fg-zh: #cabd9f;
    --muted: #8a8275;
    --accent: #d6862e;
    --rule: #3a352d;
    --sidebar-bg: #221f1a;
  }
}

* { box-sizing: border-box; }

html {
  font-size: var(--base-font);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: Georgia, "Iowan Old Style", "Source Serif Pro", serif;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

#menu-toggle {
  position: fixed;
  top: 0.6rem;
  left: 0.6rem;
  z-index: 20;
  width: 2.4rem;
  height: 2.4rem;
  border: 1px solid var(--rule);
  background: var(--bg);
  color: var(--fg);
  border-radius: 4px;
  font-size: 1.2rem;
  cursor: pointer;
  display: none;
}

#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 16rem;
  padding: 1.5rem 1.2rem;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--rule);
  overflow-y: auto;
  z-index: 10;
}

#sidebar h1 {
  font-size: 1.3rem;
  margin: 0 0 0.2rem;
  letter-spacing: 0.02em;
}

.byline {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 0 0 1.2rem;
  font-style: italic;
}

#chapter-nav {
  display: flex;
  flex-direction: column;
  gap: 0.1rem;
  margin-bottom: 1.5rem;
}

.section-title {
  font-size: 0.75rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 1rem 0 0.4rem;
}

.section-title:first-child {
  margin-top: 0;
}

.chapter-link {
  display: block;
  padding: 0.4rem 0.5rem;
  border-radius: 4px;
  color: inherit;
  text-decoration: none;
  font-size: 0.92rem;
  cursor: pointer;
  border: none;
  background: none;
  text-align: left;
  font-family: inherit;
  width: 100%;
}

.chapter-link:hover {
  background: var(--rule);
}

.chapter-link.active {
  background: var(--rule);
  font-weight: 600;
  color: var(--accent);
}

.chapter-link .num {
  color: var(--muted);
  font-size: 0.78rem;
  margin-right: 0.4rem;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding-top: 0.8rem;
  border-top: 1px solid var(--rule);
}

#resume {
  padding: 0.5rem;
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent);
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.9rem;
}

#resume:hover {
  background: var(--accent);
  color: var(--bg);
}

.font-controls {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.font-controls button {
  flex: 1;
  padding: 0.3rem 0.6rem;
  border: 1px solid var(--rule);
  background: transparent;
  color: var(--fg);
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
}

.font-controls button:hover {
  background: var(--rule);
}

#reader {
  margin-left: 16rem;
  padding: 3rem 2rem 6rem;
  display: flex;
  justify-content: center;
}

.chapter {
  width: 100%;
  max-width: var(--reading-width);
}

.chapter-header {
  margin-bottom: 2.5rem;
  padding-bottom: 1.2rem;
  border-bottom: 1px solid var(--rule);
}

.chapter-header .section-tag {
  font-size: 0.75rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.3rem;
}

.chapter-header h2 {
  font-size: 1.7rem;
  font-weight: 600;
  margin: 0;
  letter-spacing: 0.01em;
}

.chapter-header .ch-num {
  color: var(--muted);
  font-style: italic;
  font-weight: normal;
  margin-right: 0.6rem;
}

.paragraph {
  position: relative;
  margin: 0 0 1.6rem;
  padding: 0.4rem 0.6rem;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.12s;
}

.paragraph:hover {
  background: var(--rule);
}

.paragraph.bookmarked {
  background: transparent;
}

.paragraph.bookmarked::before {
  content: "";
  position: absolute;
  left: -0.6rem;
  top: 0.4rem;
  bottom: 0.4rem;
  width: 3px;
  background: var(--accent);
  border-radius: 2px;
}

.paragraph .en {
  margin: 0;
}

.paragraph .zh {
  margin: 0.6rem 0 0;
  color: var(--fg-zh);
  font-family: "PingFang SC", "Hiragino Sans GB", "Songti SC", "Noto Serif CJK SC",
               "Source Han Serif SC", serif;
  line-height: 1.8;
}

.paragraph .zh.missing {
  color: var(--muted);
  font-style: italic;
}

#loading {
  color: var(--muted);
  font-style: italic;
}

@media (max-width: 768px) {
  :root { --base-font: 16px; }
  #menu-toggle { display: block; }
  #sidebar {
    transform: translateX(-100%);
    transition: transform 0.2s;
    width: 18rem;
    box-shadow: 2px 0 16px rgba(0, 0, 0, 0.2);
  }
  #sidebar.open { transform: translateX(0); }
  #reader { margin-left: 0; padding: 4rem 1.2rem 4rem; }
}
