:root {
  --font-serif: 'Fraunces', Georgia, 'Times New Roman', serif;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;

  --color-bg: #faf8f5;
  --color-paper: #faf8f5;
  --color-surface: #ffffff;
  --color-blood: #c34b1a;
  --color-blood-soft: rgba(195, 75, 26, 0.08);

  --color-text-primary: #1a1a1a;
  --color-text-secondary: #4a4a4a;
  --color-text-tertiary: #7a7a7a;
  --color-text-quaternary: #aaa;

  --color-border: rgba(0, 0, 0, 0.08);
  --color-border-light: rgba(0, 0, 0, 0.05);
  --color-rule-soft: rgba(0, 0, 0, 0.06);
  --color-rule-med: rgba(0, 0, 0, 0.12);

  --nav-bg: rgba(250, 248, 245, 0.85);
  --nav-ink: #1a1a1a;
  --nav-border: rgba(0, 0, 0, 0.06);

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-dropdown: 0 4px 16px rgba(0,0,0,0.08), 0 1px 3px rgba(0,0,0,0.04);

  --content-max: 760px;
  --grid-max: 1240px;
  --sidebar-width: 260px;
  --nav-height: 64px;
}

html.dark {
  --color-bg: #1a1a1e;
  --color-paper: #1a1a1e;
  --color-surface: #242428;
  --color-blood: #e8734a;
  --color-blood-soft: rgba(232, 115, 74, 0.1);

  --color-text-primary: #e8e6e3;
  --color-text-secondary: #b0ada8;
  --color-text-tertiary: #7a7774;
  --color-text-quaternary: #5a5754;

  --color-border: rgba(255, 255, 255, 0.08);
  --color-border-light: rgba(255, 255, 255, 0.04);
  --color-rule-soft: rgba(255, 255, 255, 0.06);
  --color-rule-med: rgba(255, 255, 255, 0.12);

  --nav-bg: rgba(26, 26, 30, 0.9);
  --nav-ink: #e8e6e3;
  --nav-border: rgba(255, 255, 255, 0.06);
}

/* === RESET === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; scroll-behavior: smooth; }
body {
  font-family: var(--font-sans);
  background-color: var(--color-bg);
  color: var(--color-text-primary);
  line-height: 1.7;
  min-height: 100vh;
}
a { color: inherit; text-decoration: none; }
img { max-width: 100%; height: auto; display: block; }

/* === TYPOGRAPHY === */
h1, h2, h3, h4 {
  font-family: var(--font-serif);
  color: var(--color-text-primary);
  line-height: 1.25;
  letter-spacing: -0.02em;
  font-weight: 700;
}
h1 { font-size: clamp(1.5rem, 3.5vw, 2.25rem); margin: 0 0 16px; }
h2 { font-size: 1.375rem; margin: 2.5rem 0 1rem; padding-bottom: 8px; border-bottom: 1px solid var(--color-rule-soft); }
h3 { font-size: 1.125rem; margin: 2rem 0 0.75rem; font-weight: 600; }
h4 { font-size: 1rem; margin: 1.5rem 0 0.5rem; font-weight: 600; }
p { margin: 0 0 1.1em; }
strong { font-weight: 600; }
em { font-style: italic; }

/* === NAV === */
.site-nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 50;
  height: var(--nav-height);
  background: var(--nav-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--nav-border);
}
.nav-inner {
  max-width: var(--grid-max);
  margin: 0 auto;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 28px;
}
.nav-brand {
  font-family: var(--font-serif);
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--nav-ink);
  letter-spacing: -0.01em;
}
.nav-links { display: flex; align-items: center; gap: 4px; }
.nav-link {
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--color-text-secondary);
  transition: background 120ms ease, color 120ms ease;
}
.nav-link:hover { background: var(--color-border-light); color: var(--color-text-primary); }
.nav-link.active { color: var(--color-blood); }

/* === THEME TOGGLE === */
.theme-toggle {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  color: var(--color-text-secondary);
  transition: background 120ms ease;
}
.theme-toggle:hover { background: var(--color-border-light); }
.theme-toggle svg { width: 18px; height: 18px; }
html.dark .theme-toggle .icon-sun { display: block; }
html.dark .theme-toggle .icon-moon { display: none; }
html:not(.dark) .theme-toggle .icon-sun { display: none; }
html:not(.dark) .theme-toggle .icon-moon { display: block; }

/* === LAYOUT === */
.page-wrapper {
  padding-top: var(--nav-height);
  min-height: 100vh;
}
.chapter-grid {
  max-width: var(--grid-max);
  margin: 0 auto;
  padding: 0 28px;
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  gap: 0 48px;
  align-items: start;
}

/* === SIDEBAR === */
.sidebar {
  position: sticky;
  top: calc(var(--nav-height) + 24px);
  max-height: calc(100vh - var(--nav-height) - 48px);
  padding: 32px 0 24px;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
.sidebar-title {
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--color-text-primary);
  line-height: 1.25;
  margin-bottom: 4px;
}
.sidebar-label {
  font-family: var(--font-sans);
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--color-text-tertiary);
  margin: 16px 0 10px;
  padding-bottom: 8px;
  border-bottom: 1px dashed var(--color-rule-soft);
}
.sidebar-nav { display: flex; flex-direction: column; }
.sidebar-item {
  display: flex;
  gap: 10px;
  padding: 5px 8px;
  margin: 1px -8px;
  border-radius: 4px;
  border-left: 2px solid transparent;
  font-size: 12.5px;
  line-height: 1.45;
  color: var(--color-text-secondary);
  transition: background 100ms ease;
}
.sidebar-item:hover { background: var(--color-border-light); }
.sidebar-item.active {
  color: var(--color-blood);
  background: var(--color-blood-soft);
  font-weight: 500;
  border-left-color: var(--color-blood);
}
.sidebar-num {
  font-family: var(--font-sans);
  font-size: 10.5px;
  color: var(--color-text-quaternary);
  flex-shrink: 0;
  width: 18px;
  text-align: right;
}
.sidebar-item.active .sidebar-num { color: var(--color-blood); }
.sidebar-item-sub { padding-left: 20px; font-size: 11.5px; }

/* === BREADCRUMB === */
.breadcrumb {
  font-size: 13px;
  color: var(--color-text-tertiary);
  display: flex;
  gap: 6px;
  align-items: center;
  padding: 20px 0 0;
  flex-wrap: wrap;
}
.breadcrumb a { color: var(--color-text-secondary); }
.breadcrumb a:hover { color: var(--color-text-primary); }

/* === ARTICLE === */
.article-main { padding: 0 0 80px; max-width: var(--content-max); }
.article-header { padding: 24px 0 32px; }
.article-header h1 { margin-bottom: 8px; }
.article-meta {
  font-size: 13px;
  color: var(--color-text-tertiary);
  margin-top: 10px;
}
.article-meta a {
  color: var(--color-text-primary);
  border-bottom: 1px solid var(--color-rule-med);
  padding-bottom: 2px;
  transition: color 120ms ease, border-color 120ms ease;
}
.article-meta a:hover { color: var(--color-blood); border-color: var(--color-blood); }

/* === CONTENT === */
.chapter-content h1 { font-size: 1.5rem; margin-top: 2rem; }
.chapter-content h2 { font-size: 1.25rem; }
.chapter-content a {
  color: var(--color-blood);
  border-bottom: 1px solid var(--color-rule-med);
  padding-bottom: 1px;
  transition: border-color 120ms ease;
}
.chapter-content a:hover { border-color: var(--color-blood); }

.chapter-content blockquote {
  margin: 1.5em 0;
  padding: 16px 20px;
  border-left: 3px solid var(--color-blood);
  background: var(--color-blood-soft);
  border-radius: 0 8px 8px 0;
  font-size: 0.9375rem;
}
.chapter-content blockquote p:last-child { margin-bottom: 0; }

.chapter-content ul, .chapter-content ol {
  margin: 0 0 1.1em;
  padding-left: 1.5em;
}
.chapter-content li { margin-bottom: 0.4em; }
.chapter-content li::marker { color: var(--color-text-tertiary); }

.chapter-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5em 0;
  font-size: 0.875rem;
}
.chapter-content th {
  text-align: left;
  font-weight: 600;
  padding: 10px 12px;
  border-bottom: 2px solid var(--color-rule-med);
  color: var(--color-text-secondary);
}
.chapter-content td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--color-rule-soft);
}
.chapter-content tr:last-child td { border-bottom: none; }

.chapter-content pre {
  margin: 1.5em 0;
  padding: 16px 20px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.6;
}
.chapter-content code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--color-border-light);
  padding: 2px 5px;
  border-radius: 4px;
}
.chapter-content pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}

.chapter-content hr {
  border: none;
  border-top: 1px dashed var(--color-rule-soft);
  margin: 2.5em 0;
}

.chapter-content img {
  margin: 1.5em 0;
  border-radius: 8px;
  border: 1px solid var(--color-border);
}

/* === KATEX === */
.katex-display { margin: 1.5em 0; overflow-x: auto; }

/* === INDEX PAGE === */
.index-hero {
  padding: 48px 0 32px;
  max-width: var(--content-max);
}
.index-hero h1 { margin-bottom: 12px; }
.index-hero p { color: var(--color-text-secondary); font-size: 1.0625rem; }

.article-list { list-style: none; padding: 0; }
.article-card {
  display: block;
  padding: 20px 0;
  border-bottom: 1px solid var(--color-rule-soft);
  transition: background 100ms ease;
}
.article-card:first-child { border-top: 1px solid var(--color-rule-soft); }
.article-card:hover { background: var(--color-border-light); margin: 0 -12px; padding: 20px 12px; border-radius: 8px; border-color: transparent; }
.article-card:hover + .article-card { border-top-color: transparent; }
.article-card-title {
  font-family: var(--font-serif);
  font-size: 1.0625rem;
  font-weight: 600;
  color: var(--color-text-primary);
  letter-spacing: -0.01em;
  margin-bottom: 4px;
}
.article-card-desc {
  font-size: 0.875rem;
  color: var(--color-text-secondary);
  line-height: 1.5;
}
.article-card-date {
  font-size: 0.75rem;
  color: var(--color-text-tertiary);
  margin-top: 6px;
}

/* === RIGHT PANEL (Agent) === */
.agent-panel {
  position: fixed;
  top: var(--nav-height);
  right: 0;
  width: 380px;
  height: calc(100vh - var(--nav-height));
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  box-shadow: -4px 0 16px rgba(0,0,0,0.04);
  display: flex;
  flex-direction: column;
  z-index: 40;
  transform: translateX(100%);
  transition: transform 250ms ease;
}
.agent-panel.open { transform: translateX(0); }

.agent-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}
.agent-panel-title {
  font-family: var(--font-serif);
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--color-text-primary);
}
.agent-panel-close {
  width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border: none; background: transparent;
  color: var(--color-text-tertiary);
  cursor: pointer; border-radius: 6px;
  transition: background 100ms ease;
}
.agent-panel-close:hover { background: var(--color-border-light); color: var(--color-text-primary); }

.agent-selected {
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
  max-height: 160px;
  overflow-y: auto;
}
.agent-selected-label {
  font-size: 10px;
  font-weight: 500;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--color-text-tertiary);
  margin-bottom: 6px;
}
.agent-selected-text {
  font-size: 0.8125rem;
  line-height: 1.5;
  color: var(--color-text-secondary);
  white-space: pre-wrap;
  word-break: break-word;
}
.agent-selected-text:empty::after {
  content: '选中页面文本后自动显示在此处';
  color: var(--color-text-quaternary);
  font-style: italic;
}

.agent-response {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  font-size: 0.875rem;
  line-height: 1.7;
  color: var(--color-text-primary);
}
.agent-response:empty::after {
  content: '点击「发送」后，AI 回复将显示在此处';
  color: var(--color-text-quaternary);
  font-style: italic;
  display: block;
  text-align: center;
  margin-top: 40px;
}
.agent-response p { margin: 0 0 0.8em; }
.agent-response code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--color-border-light);
  padding: 2px 5px;
  border-radius: 4px;
}
.agent-response pre {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  border-radius: 6px;
  padding: 12px;
  overflow-x: auto;
  margin: 0.8em 0;
  font-size: 0.8rem;
}
.agent-response pre code { background: none; padding: 0; }
.agent-response strong { font-weight: 600; }
.agent-response ul, .agent-response ol { padding-left: 1.2em; margin: 0 0 0.8em; }
.agent-response li { margin-bottom: 0.3em; }
.agent-response blockquote {
  border-left: 3px solid var(--color-blood);
  padding: 8px 12px;
  margin: 0.8em 0;
  background: var(--color-blood-soft);
  border-radius: 0 6px 6px 0;
}

.agent-input-area {
  padding: 12px 16px;
  border-top: 1px solid var(--color-border);
  flex-shrink: 0;
  display: flex;
  gap: 8px;
}
.agent-input {
  flex: 1;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: var(--color-bg);
  color: var(--color-text-primary);
  font-size: 0.8125rem;
  font-family: var(--font-sans);
  resize: none;
  outline: none;
  transition: border-color 150ms ease;
}
.agent-input:focus { border-color: var(--color-blood); }
.agent-input::placeholder { color: var(--color-text-quaternary); }
.agent-send {
  padding: 10px 16px;
  background: var(--color-blood);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 150ms ease;
}
.agent-send:hover { opacity: 0.9; }
.agent-send:disabled { opacity: 0.5; cursor: not-allowed; }

.agent-toggle {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 48px; height: 48px;
  border-radius: 50%;
  background: var(--color-blood);
  color: #fff;
  border: none;
  box-shadow: 0 4px 12px rgba(195, 75, 26, 0.3);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  z-index: 39;
  transition: transform 150ms ease, box-shadow 150ms ease;
}
.agent-toggle:hover { transform: scale(1.05); box-shadow: 0 6px 16px rgba(195, 75, 26, 0.4); }
.agent-panel.open ~ .agent-toggle { display: none; }

/* === MOBILE === */
@media (max-width: 1023px) {
  .chapter-grid { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .article-main { max-width: 100%; }
  .agent-panel { width: 100%; }
}
@media (max-width: 639px) {
  .nav-links { display: none; }
  .chapter-grid { padding: 0 16px; }
  .chapter-content blockquote { padding: 12px 14px; }
}
