/* ============================================================
   Hello Agents premium e-book reader
   ============================================================ */
:root {
  --font-serif: "Noto Serif SC", "Songti SC", "STSong", "Georgia", serif;
  --font-sans: "Noto Sans SC", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  --header-height: 64px;
  --sidebar-width: 320px;
  --content-width: 880px;
  --radius-sm: 10px;
  --radius-md: 18px;
  --radius-lg: 28px;
  --ease: cubic-bezier(.2, .8, .2, 1);
}

:root[data-theme="light"] {
  --bg: #f6f2ea;
  --bg-soft: #efe9dc;
  --surface: #fffdf8;
  --surface-raised: #ffffff;
  --surface-subtle: #f4efe4;
  --text: #20242a;
  --text-soft: #454c56;
  --muted: #7b8491;
  --border: rgba(32, 36, 42, .12);
  --border-strong: rgba(32, 36, 42, .22);
  --accent: #c75d2e;
  --accent-strong: #a94a24;
  --accent-soft: rgba(199, 93, 46, .12);
  --link: #b14d24;
  --code-bg: #f1ecdf;
  --code-text: #343a40;
  --shadow: 0 18px 50px rgba(45, 37, 26, .10);
  --shadow-soft: 0 8px 24px rgba(45, 37, 26, .07);
  --selection: rgba(199, 93, 46, .22);
}

:root[data-theme="dark"] {
  --bg: #0d1014;
  --bg-soft: #131821;
  --surface: #151a21;
  --surface-raised: #1b222b;
  --surface-subtle: #202833;
  --text: #eee7da;
  --text-soft: #c7cdd5;
  --muted: #929ba7;
  --border: rgba(238, 231, 218, .11);
  --border-strong: rgba(238, 231, 218, .22);
  --accent: #ff9a67;
  --accent-strong: #ffb18a;
  --accent-soft: rgba(255, 154, 103, .14);
  --link: #ffa274;
  --code-bg: #1e2530;
  --code-text: #e5edf6;
  --shadow: 0 18px 55px rgba(0, 0, 0, .32);
  --shadow-soft: 0 8px 24px rgba(0, 0, 0, .22);
  --selection: rgba(255, 154, 103, .25);
}

* { box-sizing: border-box; }

html {
  font-size: 17px;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background:
    radial-gradient(circle at 10% -10%, var(--accent-soft), transparent 34rem),
    var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  line-height: 1.7;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  transition: background-color .25s var(--ease), color .25s var(--ease);
}

::selection { background: var(--selection); }

a { color: inherit; text-decoration: none; }
button { font-family: inherit; }
img { max-width: 100%; }

/* ---------- Top progress bar ---------- */
.progress-track {
  position: fixed;
  z-index: 1000;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: transparent;
  pointer-events: none;
}
.progress-bar {
  width: 0;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #f4a261);
  box-shadow: 0 0 12px rgba(244, 162, 97, .45);
  transition: width .08s linear;
}

/* ---------- Header ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--header-height);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 18px;
  background: color-mix(in srgb, var(--surface) 78%, transparent);
  backdrop-filter: blur(18px) saturate(150%);
  -webkit-backdrop-filter: blur(18px) saturate(150%);
  border-bottom: 1px solid var(--border);
}
.header-spacer { flex: 1; }

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 11px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent), #d98c4a);
  font-family: var(--font-mono);
  font-size: .76rem;
  font-weight: 700;
  letter-spacing: -.02em;
  box-shadow: 0 6px 16px var(--accent-soft);
}
.brand-text {
  font-family: var(--font-sans);
  font-weight: 700;
  letter-spacing: -.02em;
  font-size: 1rem;
  white-space: nowrap;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: 1px solid transparent;
  border-radius: 11px;
  background: transparent;
  color: var(--text-soft);
  font-size: .9rem;
  font-weight: 700;
  cursor: pointer;
  transition: background .18s var(--ease), color .18s var(--ease), border-color .18s var(--ease), transform .18s var(--ease);
}
.icon-button:hover { background: var(--accent-soft); color: var(--accent); border-color: var(--border); }
.icon-button:active { transform: scale(.94); }

.hamburger,
.hamburger::before,
.hamburger::after {
  display: block;
  width: 17px;
  height: 1.8px;
  border-radius: 2px;
  background: currentColor;
  content: "";
  position: relative;
}
.hamburger::before { position: absolute; top: -5px; left: 0; }
.hamburger::after { position: absolute; top: 5px; left: 0; }

/* ---------- Layout / sidebar ---------- */
.layout-shell {
  display: flex;
  align-items: flex-start;
  min-height: calc(100vh - var(--header-height));
}

.sidebar {
  position: sticky;
  top: var(--header-height);
  width: var(--sidebar-width);
  height: calc(100vh - var(--header-height));
  flex: 0 0 var(--sidebar-width);
  overflow-y: auto;
  padding: 20px 14px 36px;
  border-right: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 42%, transparent);
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.sidebar::-webkit-scrollbar { width: 6px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 99px; }

.sidebar-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 4px 6px 18px;
}
.sidebar-kicker {
  margin: 0;
  color: var(--accent);
  font-size: .68rem;
  letter-spacing: .18em;
  font-weight: 700;
}
.sidebar-title {
  margin: 3px 0 0;
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 600;
}
.sidebar-close { display: none; }

.sidebar-group { margin: 0 0 16px; }
.sidebar-group-title {
  margin: 0 6px 7px;
  color: var(--muted);
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
}
.sidebar-chapters {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 2px;
}
.sidebar-link {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 8px 9px;
  border-radius: 10px;
  color: var(--text-soft);
  font-size: .87rem;
  line-height: 1.45;
  transition: background .16s var(--ease), color .16s var(--ease), transform .16s var(--ease);
}
.sidebar-link:hover { background: var(--accent-soft); color: var(--text); transform: translateX(2px); }
.sidebar-link.active { background: var(--accent-soft); color: var(--accent); font-weight: 700; }
.sidebar-number {
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: .68rem;
  min-width: 1.4rem;
  text-align: right;
}

.sidebar-overlay {
  display: none;
}

.main-area {
  flex: 1;
  min-width: 0;
  padding: 34px clamp(20px, 5vw, 72px) 70px;
}

/* ---------- Home ---------- */
.home-page { max-width: 1020px; margin: 0 auto; }

.hero {
  position: relative;
  overflow: hidden;
  padding: clamp(40px, 8vw, 84px) clamp(22px, 6vw, 64px);
  margin-bottom: 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background:
    linear-gradient(135deg, var(--surface-raised), var(--surface));
  box-shadow: var(--shadow);
}
.hero::after {
  content: "";
  position: absolute;
  width: 380px;
  height: 380px;
  right: -150px;
  top: -170px;
  border-radius: 50%;
  background: radial-gradient(circle, var(--accent-soft), transparent 66%);
  pointer-events: none;
}
.hero-inner { position: relative; z-index: 1; max-width: 720px; }
.hero-eyebrow {
  margin: 0 0 18px;
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: .72rem;
  letter-spacing: .2em;
  font-weight: 700;
}
.hero h1 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: clamp(2.6rem, 8vw, 5.4rem);
  line-height: 1.08;
  letter-spacing: -.04em;
}
.hero-subtitle {
  margin: 20px 0 0;
  font-size: clamp(1.05rem, 2vw, 1.35rem);
  color: var(--text-soft);
}
.hero-meta {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: .86rem;
}

.continue-card {
  margin-top: 30px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface-subtle);
  box-shadow: var(--shadow-soft);
  overflow: hidden;
}
.continue-link {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 18px;
  transition: background .2s var(--ease), transform .2s var(--ease);
}
.continue-link:hover { background: var(--accent-soft); transform: translateY(-2px); }
.continue-icon {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  flex: 0 0 42px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  font-size: .9rem;
  box-shadow: 0 8px 20px var(--accent-soft);
}
.continue-copy { min-width: 0; flex: 1; }
.continue-label {
  display: block;
  color: var(--muted);
  font-size: .7rem;
  letter-spacing: .1em;
  font-weight: 700;
}
.continue-title {
  display: block;
  margin-top: 3px;
  font-family: var(--font-serif);
  font-size: 1.05rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.continue-arrow { color: var(--accent); font-size: 1.2rem; }

.toc { display: grid; gap: 22px; }
.toc-section {
  padding: 24px clamp(18px, 3vw, 30px);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  box-shadow: var(--shadow-soft);
}
.toc-section-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.toc-section-index {
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: .76rem;
  font-weight: 700;
  letter-spacing: .08em;
  padding: 4px 8px;
  border-radius: 8px;
  background: var(--accent-soft);
}
.toc-section-head h2 {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 1.25rem;
  letter-spacing: -.01em;
}
.toc-chapters {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 4px;
}
.toc-item { min-width: 0; }
.toc-link {
  display: flex;
  align-items: baseline;
  gap: 13px;
  padding: 11px 12px;
  border-radius: 11px;
  color: var(--text-soft);
  transition: background .16s var(--ease), color .16s var(--ease), transform .16s var(--ease);
}
.toc-link:hover { background: var(--accent-soft); color: var(--text); transform: translateX(3px); }
.toc-number {
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: .72rem;
  min-width: 1.7rem;
  text-align: right;
}
.toc-title { flex: 1; font-size: 1rem; line-height: 1.5; }
.toc-arrow { opacity: 0; color: var(--accent); transform: translateX(-6px); transition: opacity .16s var(--ease), transform .16s var(--ease); }
.toc-link:hover .toc-arrow { opacity: 1; transform: translateX(0); }

/* ---------- Reader ---------- */
.reader-page { max-width: var(--content-width); margin: 0 auto; }
.reader-head { padding: 16px 0 34px; }
.reader-kicker {
  margin: 0 0 12px;
  color: var(--accent);
  font-family: var(--font-mono);
  font-size: .73rem;
  letter-spacing: .14em;
  font-weight: 700;
  text-transform: uppercase;
}
.reader-title {
  margin: 0;
  font-family: var(--font-serif);
  font-size: clamp(2rem, 5vw, 3.4rem);
  line-height: 1.15;
  letter-spacing: -.035em;
}

.chapter-nav {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 12px;
  align-items: stretch;
}
.top-nav { margin-top: 30px; }
.bottom-nav { margin-top: 52px; padding-top: 24px; border-top: 1px solid var(--border); }

.nav-link {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: var(--surface);
  transition: background .18s var(--ease), border-color .18s var(--ease), transform .18s var(--ease);
}
.nav-link:hover { background: var(--accent-soft); border-color: var(--border-strong); transform: translateY(-2px); }
.nav-link.prev { text-align: left; }
.nav-link.next { text-align: right; }
.nav-label {
  color: var(--muted);
  font-size: .68rem;
  letter-spacing: .1em;
  font-weight: 700;
}
.nav-title {
  margin-top: 3px;
  font-family: var(--font-serif);
  font-weight: 600;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.nav-link.disabled {
  opacity: .4;
  cursor: default;
  background: transparent;
}
.nav-link.disabled:hover { transform: none; background: transparent; border-color: var(--border); }
.nav-home {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 14px;
  color: var(--muted);
  font-size: .82rem;
  border-radius: 13px;
  border: 1px solid var(--border);
  transition: color .18s var(--ease), background .18s var(--ease);
}
.nav-home:hover { color: var(--accent); background: var(--accent-soft); }

/* ---------- Markdown article ---------- */
.markdown-body {
  font-family: var(--font-serif);
  font-size: 1.08rem;
  line-height: 1.85;
  color: var(--text);
  overflow-wrap: break-word;
}
.markdown-body > :first-child { margin-top: 0; }
.markdown-body > :last-child { margin-bottom: 0; }

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  font-family: var(--font-serif);
  line-height: 1.3;
  color: var(--text);
  margin: 2em 0 .7em;
  scroll-margin-top: calc(var(--header-height) + 24px);
}
.markdown-body h1 { font-size: 1.95em; letter-spacing: -.025em; }
.markdown-body h2 { font-size: 1.55em; letter-spacing: -.02em; padding-bottom: .3em; border-bottom: 1px solid var(--border); }
.markdown-body h3 { font-size: 1.3em; }
.markdown-body h4 { font-size: 1.15em; }
.markdown-body h5, .markdown-body h6 { font-size: 1em; }

.markdown-body p { margin: 1.1em 0; }
.markdown-body strong { font-weight: 700; color: var(--text); }
.markdown-body em { font-style: italic; }

.markdown-body a {
  color: var(--link);
  border-bottom: 1px solid color-mix(in srgb, var(--link) 35%, transparent);
  transition: color .15s var(--ease), border-color .15s var(--ease);
}
.markdown-body a:hover { color: var(--accent); border-color: var(--accent); }

.markdown-body ul,
.markdown-body ol { margin: 1em 0; padding-left: 1.5em; }
.markdown-body li { margin: .35em 0; }
.markdown-body li > ul,
.markdown-body li > ol { margin: .45em 0 .45em .6em; }

.markdown-body blockquote {
  margin: 1.5em 0;
  padding: .7em 1.2em;
  border-left: 4px solid var(--accent);
  background: var(--surface-subtle);
  border-radius: 0 12px 12px 0;
  color: var(--text-soft);
}
.markdown-body blockquote > :first-child { margin-top: 0; }
.markdown-body blockquote > :last-child { margin-bottom: 0; }

.markdown-body hr {
  margin: 2.4em auto;
  border: 0;
  height: 1px;
  background: var(--border);
  max-width: 180px;
}

.markdown-body img {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 1.8rem auto;
  border-radius: 12px;
  box-shadow: var(--shadow-soft);
  background: var(--surface);
}

.markdown-body table {
  width: 100%;
  max-width: 100%;
  margin: 1.6em 0;
  border-collapse: collapse;
  font-family: var(--font-sans);
  font-size: .88rem;
  line-height: 1.55;
  background: var(--surface);
}
.table-scroll {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  margin: 1.6em 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
}
.table-scroll table { margin: 0; border: 0; border-radius: 0; }
.markdown-body th,
.markdown-body td {
  padding: .7rem .8rem;
  border: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
.markdown-body thead th {
  background: var(--surface-subtle);
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
}
.markdown-body tbody tr:nth-child(even) { background: color-mix(in srgb, var(--surface-subtle) 60%, transparent); }

.markdown-body pre {
  margin: 1.6em 0;
  padding: 1.05rem 1.15rem;
  border-radius: 14px;
  background: #171a21 !important;
  color: #e6edf3;
  overflow-x: auto;
  border: 1px solid rgba(255, 255, 255, .06);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.03);
  line-height: 1.62;
  tab-size: 4;
}
.markdown-body pre code {
  display: block;
  min-width: max-content;
  padding: 0;
  background: transparent !important;
  color: inherit;
  font-family: var(--font-mono);
  font-size: .84rem;
  font-variant-ligatures: none;
  white-space: pre;
}
.markdown-body :not(pre) > code {
  padding: .18em .42em;
  border-radius: 6px;
  background: var(--code-bg);
  color: var(--code-text);
  font-family: var(--font-mono);
  font-size: .82em;
  white-space: normal;
}

.markdown-body .codehilite { background: #171a21; border-radius: 14px; overflow-x: auto; }

.markdown-body input[type="checkbox"] {
  appearance: none;
  width: 1em;
  height: 1em;
  margin: 0 .45em 0 0;
  border: 1.5px solid var(--border-strong);
  border-radius: 4px;
  vertical-align: -0.08em;
  background: transparent;
}
.markdown-body input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
  position: relative;
}
.markdown-body input[type="checkbox"]:checked::after {
  content: "✓";
  color: white;
  font-size: .72em;
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
}
.markdown-body li.task-list-item { list-style: none; margin-left: -1.25em; }

.markdown-body sup,
.markdown-body sub { font-size: .7em; }

/* Keep KaTeX display equations readable and scrollable on small screens */
.markdown-body .arithmatex,
.markdown-body .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
}
.markdown-body .katex { font-size: 1.04em; }

/* ---------- Responsive ---------- */
@media (max-width: 1024px) {
  .sidebar {
    position: fixed;
    top: var(--header-height);
    bottom: 0;
    left: 0;
    z-index: 500;
    height: calc(100vh - var(--header-height));
    transform: translateX(-104%);
    transition: transform .28s var(--ease);
    background: var(--surface);
    box-shadow: var(--shadow);
    width: min(86vw, 340px);
    border-right: 1px solid var(--border);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  .sidebar-close { display: inline-flex; }
  .sidebar-overlay {
    display: block;
    position: fixed;
    inset: var(--header-height) 0 0 0;
    z-index: 490;
    background: rgba(6, 8, 12, .42);
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s var(--ease);
  }
  body.sidebar-open .sidebar-overlay { opacity: 1; pointer-events: auto; }
  .main-area { padding-left: 20px; padding-right: 20px; }
}

@media (max-width: 640px) {
  html { font-size: 16px; }
  .app-header { padding: 0 10px; gap: 6px; }
  .brand-text { display: none; }
  .main-area { padding: 24px 14px 60px; }
  .hero { padding: 40px 22px; }
  .reader-title { font-size: 2rem; }
  .markdown-body { font-size: 1.04rem; line-height: 1.8; }
  .chapter-nav { grid-template-columns: 1fr; }
  .nav-link.next { text-align: left; }
  .nav-home { justify-self: start; padding: 8px 12px; }
  .toc-link { padding: 10px 8px; }
  .toc-title { font-size: .94rem; }
}

@media (max-width: 380px) {
  .icon-button { width: 33px; height: 33px; }
  .brand-mark { width: 31px; height: 31px; }
}
