:root {
  --body-size: 17.5px;
  /* paper (light) */
  --bg: #ece3cd;          /* desk around the page */
  --page: #f8f2e2;        /* paper */
  --text: #3a3226;
  --text-muted: #857961;
  --border: #d9cdad;
  --accent: #9c4a21;      /* sienna */
  --accent-soft: #efe2cc;
  --code-bg: #efe7d1;
  --topbar-bg: #f4ecd9f2;
  --shadow: 0 1px 3px rgba(90, 70, 40, 0.14), 0 8px 28px rgba(90, 70, 40, 0.09);
}
html.dark {
  --bg: #16130e;
  --page: #201c14;
  --text: #d8cfba;
  --text-muted: #938870;
  --border: #383021;
  --accent: #d9995b;
  --accent-soft: #2e2718;
  --code-bg: #191510;
  --topbar-bg: #1b1710f2;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 8px 28px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Georgia, "Times New Roman", "Noto Serif SC", "Source Han Serif SC",
    "Songti SC", STSong, SimSun, serif;
  font-size: var(--body-size);
  line-height: 2;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* UI chrome uses sans */
.topbar, .sidebar, .pager, .card, .account, .resume-btn, .cover .meta, .license, .card-pct {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Noto Sans CJK SC", "Microsoft YaHei", sans-serif;
}

.mobile-only { display: none !important; }

/* top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 20px 10px;
  background: var(--topbar-bg);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.topbar .home { color: var(--text); font-weight: 600; }
.topbar .chapter-label { color: var(--text-muted); }
.spacer { flex: 1; }
.topbar button {
  border: 1px solid var(--border);
  background: var(--page);
  color: var(--text);
  border-radius: 6px;
  padding: 3px 10px;
  font-size: 13px;
  cursor: pointer;
}
.topbar button:hover { border-color: var(--accent); color: var(--accent); }

#progress {
  position: fixed;
  top: 0;
  left: 0;
  height: 2px;
  width: 0;
  background: var(--accent);
  z-index: 30;
}

/* layout */
.layout {
  display: flex;
  max-width: 1240px;
  margin: 0 auto;
  align-items: flex-start;
}
.sidebar {
  position: sticky;
  top: 49px;
  width: 260px;
  flex-shrink: 0;
  max-height: calc(100vh - 49px);
  overflow-y: auto;
  padding: 24px 8px 40px 20px;
  font-size: 13px;
  line-height: 1.7;
}
.sidebar .side-title {
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  font-size: 11px;
  letter-spacing: 0.05em;
  margin: 18px 0 6px;
}
.sidebar ul { list-style: none; margin: 0; padding-left: 0; }
.sidebar ul ul { padding-left: 14px; }
.sidebar li { margin: 2px 0; }
.sidebar a { color: var(--text-muted); display: block; padding: 1px 6px; border-radius: 4px; }
.sidebar a:hover { color: var(--accent); text-decoration: none; }
.sidebar a.active, .sidebar .book-nav a.cur {
  color: var(--accent);
  background: var(--accent-soft);
}
.book-nav { margin-bottom: 24px; }

/* the "page" */
.content {
  flex: 1;
  min-width: 0;
  padding: 36px 40px 80px;
}
article {
  max-width: 780px;
  margin: 0 auto;
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: var(--shadow);
  padding: 56px 64px 64px;
}

/* typography */
article h1 {
  font-size: 1.65em;
  line-height: 1.5;
  margin: 0 0 1.2em;
  text-align: center;
  letter-spacing: 0.02em;
}
article h1::after {
  content: "";
  display: block;
  width: 48px;
  height: 2px;
  background: var(--accent);
  margin: 0.7em auto 0;
}
article h2 {
  font-size: 1.32em;
  line-height: 1.5;
  margin: 2.4em 0 0.8em;
  padding-top: 0.8em;
  border-top: 1px solid var(--border);
}
article h3 { font-size: 1.12em; margin: 1.8em 0 0.6em; }
article h4 { font-size: 1em; margin: 1.5em 0 0.5em; }
article p { margin: 0.9em 0; text-align: justify; }
article img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 1.6em auto;
}
html.dark article img { background: #f4ecd9; border-radius: 6px; padding: 8px; }
article blockquote {
  margin: 1.2em 0;
  padding: 2px 20px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 6px 6px 0;
  color: var(--text-muted);
}
article code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.1em 0.35em;
}
article pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 14px 16px;
  overflow-x: auto;
  line-height: 1.6;
}
article pre code { background: none; border: none; padding: 0; font-size: 0.82em; }
.table-wrap { overflow-x: auto; margin: 1.4em 0; }
article table {
  border-collapse: collapse;
  font-size: 0.88em;
  line-height: 1.7;
  min-width: 60%;
}
article th, article td {
  border: 1px solid var(--border);
  padding: 7px 12px;
  text-align: left;
  vertical-align: top;
}
article th { background: var(--code-bg); }
article hr { border: none; border-top: 1px solid var(--border); margin: 2.5em 0; }
.footnote { font-size: 0.85em; color: var(--text-muted); margin-top: 3em; }
.footnote hr { margin: 1em 0; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }

/* pager */
.pager {
  display: flex;
  max-width: 780px;
  margin: 2.5em auto 0;
  gap: 12px;
}
.nav-btn {
  border: 1px solid var(--border);
  background: var(--page);
  border-radius: 6px;
  padding: 10px 18px;
  color: var(--text);
  font-size: 14px;
}
.nav-btn:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }

/* cover / index */
.cover { max-width: 920px; margin: 0 auto; padding: 56px 24px 80px; }
.cover h1 { font-size: 2.3em; margin: 0 0 6px; letter-spacing: 0.03em; }
.cover .subtitle { color: var(--text-muted); font-size: 1.05em; margin: 0 0 8px; }
.cover .meta { font-size: 14px; color: var(--text-muted); margin-bottom: 20px; }

/* account box */
.account {
  border: 1px solid var(--border);
  background: var(--page);
  border-radius: 8px;
  padding: 14px 18px;
  margin-bottom: 20px;
  font-size: 14px;
  box-shadow: var(--shadow);
}
.account .hint { color: var(--text-muted); }
.account form { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.account input {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 14px;
  min-width: 0;
}
.account input:focus { outline: none; border-color: var(--accent); }
.account button, .account .linklike {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 6px;
  padding: 8px 16px;
  font-size: 14px;
  cursor: pointer;
}
.account button.ghost {
  background: transparent;
  color: var(--accent);
}
.account button:disabled { opacity: 0.55; cursor: default; }
.account .msg { margin-top: 8px; font-size: 13px; color: var(--text-muted); }
.account .msg.err { color: #b3403a; }

#resume-slot { margin-bottom: 24px; }
.resume-btn {
  display: inline-block;
  background: var(--accent);
  color: #fff;
  border-radius: 8px;
  padding: 12px 22px;
  font-size: 15px;
  font-weight: 500;
}
.resume-btn:hover { text-decoration: none; opacity: 0.92; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(255px, 1fr));
  gap: 12px;
}
.card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px 18px;
  color: var(--text);
  background: var(--page);
  box-shadow: var(--shadow);
}
.card:hover { border-color: var(--accent); text-decoration: none; }
.card-label { font-size: 12px; color: var(--text-muted); }
.card-title { font-weight: 600; line-height: 1.5; }
.card-pct { font-size: 12px; color: var(--accent); min-height: 1em; }
.license { margin-top: 40px; font-size: 12px; color: var(--text-muted); }

/* drawer backdrop + flip hint (mobile) */
#drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(30, 22, 10, 0.45);
  z-index: 39;
}
#drawer-backdrop.show { display: block; }
.flip-hint {
  position: fixed;
  left: 50%;
  bottom: calc(28px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  font-size: 13px;
  padding: 9px 16px;
  border-radius: 20px;
  z-index: 50;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.4s;
  pointer-events: none;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", sans-serif;
}
.flip-hint.show { opacity: 0.92; }

/* mobile */
@media (max-width: 900px) {
  .mobile-only { display: inline-block !important; }
  /* sidebar becomes a slide-in drawer */
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    width: 80%;
    max-width: 320px;
    max-height: none;
    z-index: 40;
    background: var(--page);
    border-right: 1px solid var(--border);
    box-shadow: var(--shadow);
    padding: calc(20px + env(safe-area-inset-top)) 16px calc(30px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
    transform: translateX(-105%);
    transition: transform 0.25s ease;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar a { padding: 6px 8px; }
  .content { padding: 0 0 calc(70px + env(safe-area-inset-bottom)); }
  article {
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding: 28px 20px 48px;
    max-width: none;
  }
  body.reading { background: var(--page); }
  .pager { padding: 0 16px; }
  .topbar { padding: calc(8px + env(safe-area-inset-top)) 14px 8px; }
  .topbar button { padding: 5px 12px; }
  #toc-btn { font-size: 15px; }
  :root { --body-size: 17px; }
}
