/* ── Reset & Base ─────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --color-bg: #f8f9fb;
  --color-surface: #ffffff;
  --color-border: #e2e8f0;
  --color-text: #1a202c;
  --color-text-secondary: #64748b;
  --color-primary: #3b82f6;
  --color-primary-light: #dbeafe;
  --color-primary-dark: #1d4ed8;
  --color-accent: #8b5cf6;
  --color-code-bg: #f1f5f9;
  --color-code-text: #e11d48;
  --color-keyword: #7c3aed;
  --color-success: #10b981;
  --color-warning: #f59e0b;
  --color-danger: #ef4444;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.07), 0 2px 4px -2px rgba(0,0,0,0.05);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.08), 0 4px 6px -4px rgba(0,0,0,0.05);
  --radius: 8px;
  --radius-lg: 12px;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", "Cascadia Code", Consolas, monospace;
  --sidebar-width: 280px;
  --header-height: 64px;
}

html { scroll-behavior: smooth; font-size: 16px; }

body {
  font-family: var(--font-sans);
  background: var(--color-bg);
  color: var(--color-text);
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
}

.container { max-width: 1200px; margin: 0 auto; padding: 0 24px; }

a { color: var(--color-primary); text-decoration: none; transition: color 0.2s; }
a:hover { color: var(--color-primary-dark); }

/* ── Header ──────────────────────────────────────────── */
.site-header {
  background: linear-gradient(135deg, #1e293b 0%, #334155 100%);
  color: white;
  padding: 32px 0;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow-lg);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
}

.logo { display: flex; align-items: center; gap: 16px; }
.logo-icon { font-size: 2.5rem; }
.logo h1 { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; }
.subtitle { color: #94a3b8; font-size: 0.875rem; margin-top: 2px; }

.header-stats { display: flex; gap: 24px; }
.stat { text-align: center; }
.stat-num { display: block; font-size: 1.5rem; font-weight: 700; color: #60a5fa; }
.stat-label { font-size: 0.75rem; color: #94a3b8; text-transform: uppercase; letter-spacing: 0.05em; }

.chapter-header { padding: 16px 0; }
.back-link { color: #94a3b8; font-size: 0.875rem; display: inline-block; margin-bottom: 8px; }
.back-link:hover { color: white; }
.chapter-title-bar { display: flex; align-items: center; gap: 12px; }
.chapter-badge {
  background: var(--color-primary);
  color: white;
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}
.chapter-title-bar h1 { font-size: 1.25rem; font-weight: 600; }

/* ── Chapter Grid (Index) ────────────────────────────── */
.chapter-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 32px 0 64px;
}

.chapter-card {
  display: flex;
  align-items: center;
  gap: 20px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 20px 24px;
  transition: all 0.2s ease;
  box-shadow: var(--shadow-sm);
}

.chapter-card:hover {
  border-color: var(--color-primary);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
  color: inherit;
}

.chapter-icon { font-size: 2rem; flex-shrink: 0; width: 48px; text-align: center; }
.chapter-info { flex: 1; min-width: 0; }
.chapter-info h3 { font-size: 1.05rem; font-weight: 600; margin-bottom: 4px; color: var(--color-text); }
.chapter-preview { font-size: 0.8rem; color: var(--color-text-secondary); margin-bottom: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chapter-meta { display: flex; gap: 16px; font-size: 0.75rem; color: var(--color-text-secondary); }
.chapter-arrow { color: var(--color-text-secondary); font-size: 1.25rem; transition: transform 0.2s; }
.chapter-card:hover .chapter-arrow { transform: translateX(4px); color: var(--color-primary); }

/* ── Chapter Layout ──────────────────────────────────── */
.chapter-layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  gap: 32px;
  padding-top: 24px;
  padding-bottom: 64px;
  align-items: start;
}

/* ── Sidebar / TOC ───────────────────────────────────── */
.sidebar {
  position: sticky;
  top: calc(var(--header-height) + 24px);
  max-height: calc(100vh - var(--header-height) - 48px);
  overflow-y: auto;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow-sm);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--color-border);
}

.sidebar-header h3 { font-size: 0.875rem; font-weight: 600; color: var(--color-text-secondary); text-transform: uppercase; letter-spacing: 0.05em; }
.sidebar-toggle { display: none; background: none; border: none; font-size: 1.25rem; cursor: pointer; color: var(--color-text-secondary); }

.toc ul { list-style: none; }
.toc li { margin-bottom: 2px; }
.toc a {
  display: block;
  padding: 6px 8px;
  font-size: 0.8rem;
  color: var(--color-text-secondary);
  border-radius: 6px;
  transition: all 0.15s;
  line-height: 1.4;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.toc a:hover { background: var(--color-primary-light); color: var(--color-primary-dark); }
.toc a.active { background: var(--color-primary-light); color: var(--color-primary-dark); font-weight: 600; }

/* ── Chapter Content ─────────────────────────────────── */
.chapter-content {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: 40px 48px;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}

.chapter-content article { max-width: 100%; }

.chapter-content h2 {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 40px 0 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--color-primary-light);
  color: var(--color-text);
  scroll-margin-top: calc(var(--header-height) + 16px);
}

.chapter-content h3 {
  font-size: 1.2rem;
  font-weight: 600;
  margin: 32px 0 12px;
  color: var(--color-text);
  scroll-margin-top: calc(var(--header-height) + 16px);
}

.chapter-content h4 {
  font-size: 1.05rem;
  font-weight: 600;
  margin: 24px 0 8px;
  color: var(--color-text);
  scroll-margin-top: calc(var(--header-height) + 16px);
}

.chapter-content h5 {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 20px 0 8px;
  color: var(--color-text-secondary);
  scroll-margin-top: calc(var(--header-height) + 16px);
}

.chapter-content p {
  margin: 8px 0;
  color: var(--color-text);
  line-height: 1.8;
}

.chapter-content ol, .chapter-content ul {
  margin: 8px 0;
  padding-left: 24px;
}

.chapter-content li {
  margin: 4px 0;
  line-height: 1.7;
}

/* ── Code ────────────────────────────────────────────── */
.inline-code {
  font-family: var(--font-mono);
  background: var(--color-code-bg);
  color: var(--color-code-text);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.875em;
  word-break: break-all;
}

.keyword {
  color: var(--color-keyword);
  font-weight: 600;
}

/* ── Images ──────────────────────────────────────────── */
.doc-image {
  margin: 16px 0;
  text-align: center;
}

.doc-image img {
  max-width: 100%;
  height: auto;
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  cursor: zoom-in;
  transition: box-shadow 0.2s;
}

.doc-image img:hover {
  box-shadow: var(--shadow-lg);
}

/* ── Image Modal ─────────────────────────────────────── */
.image-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  z-index: 1000;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.image-modal.active { display: flex; }

.modal-content {
  position: relative;
  max-width: 95vw;
  max-height: 95vh;
}

.modal-content img {
  max-width: 100%;
  max-height: 90vh;
  border-radius: var(--radius);
  box-shadow: 0 0 40px rgba(0,0,0,0.5);
}

.modal-close {
  position: absolute;
  top: -40px;
  right: 0;
  background: rgba(255,255,255,0.2);
  border: none;
  color: white;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 1.25rem;
  cursor: pointer;
  transition: background 0.2s;
}

.modal-close:hover { background: rgba(255,255,255,0.4); }

/* ── Chapter Navigation ──────────────────────────────── */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  margin-top: 48px;
  padding-top: 24px;
  border-top: 1px solid var(--color-border);
}

.nav-prev, .nav-next {
  padding: 10px 20px;
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
  border-radius: var(--radius);
  font-weight: 500;
  font-size: 0.875rem;
  transition: all 0.2s;
}

.nav-prev:hover, .nav-next:hover {
  background: var(--color-primary);
  color: white;
}

/* ── Footer ──────────────────────────────────────────── */
.site-footer {
  background: #1e293b;
  color: #94a3b8;
  text-align: center;
  padding: 24px 0;
  font-size: 0.8rem;
}

/* ── Scrollbar ───────────────────────────────────────── */
.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: 4px; }
.sidebar::-webkit-scrollbar-thumb:hover { background: var(--color-text-secondary); }

/* ── Responsive ──────────────────────────────────────── */
@media (max-width: 900px) {
  .chapter-layout {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: relative;
    top: 0;
    max-height: none;
    display: none;
  }

  .sidebar.open { display: block; }
  .sidebar-toggle { display: block; }

  .chapter-content { padding: 24px 20px; }

  .header-stats { display: none; }

  .chapter-card { padding: 16px; gap: 12px; }
  .chapter-icon { font-size: 1.5rem; width: 36px; }
  .chapter-info h3 { font-size: 0.95rem; }
  .chapter-preview { display: none; }
}

@media (max-width: 600px) {
  .container { padding: 0 16px; }
  .logo h1 { font-size: 1.2rem; }
  .chapter-content { padding: 20px 16px; }
  .chapter-content h2 { font-size: 1.25rem; }
  .chapter-content h3 { font-size: 1.1rem; }
}

/* ── Search highlight ────────────────────────────────── */
mark {
  background: #fef08a;
  padding: 1px 3px;
  border-radius: 2px;
}

/* ── Theme Toggle ────────────────────────────────────── */
.theme-toggle {
  background: rgba(255,255,255,0.15);
  border: none;
  font-size: 1.25rem;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: var(--radius);
  transition: background 0.2s;
  line-height: 1;
}
.theme-toggle:hover { background: rgba(255,255,255,0.25); }

/* ── Search Box ──────────────────────────────────────── */
.search-box {
  position: relative;
  max-width: 600px;
  margin: 24px auto 0;
}

.search-box input {
  width: 100%;
  padding: 12px 20px;
  font-size: 1rem;
  border: 2px solid var(--color-border);
  border-radius: var(--radius-lg);
  background: var(--color-surface);
  color: var(--color-text);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  font-family: var(--font-sans);
}

.search-box input:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}

.search-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  max-height: 400px;
  overflow-y: auto;
  display: none;
  z-index: 50;
}

.search-results.active { display: block; }

.search-result-item {
  display: block;
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  transition: background 0.15s;
  color: var(--color-text);
}

.search-result-item:last-child { border-bottom: none; }
.search-result-item:hover { background: var(--color-primary-light); }

.search-result-item .result-chapter {
  font-size: 0.75rem;
  color: var(--color-primary);
  font-weight: 600;
}

.search-result-item .result-text {
  font-size: 0.875rem;
  color: var(--color-text-secondary);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── Dark Mode ───────────────────────────────────────── */
[data-theme="dark"] {
  --color-bg: #0f172a;
  --color-surface: #1e293b;
  --color-border: #334155;
  --color-text: #e2e8f0;
  --color-text-secondary: #94a3b8;
  --color-primary: #60a5fa;
  --color-primary-light: #1e3a5f;
  --color-primary-dark: #93c5fd;
  --color-accent: #a78bfa;
  --color-code-bg: #334155;
  --color-code-text: #f472b6;
  --color-keyword: #c084fc;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.4);
  --shadow-lg: 0 10px 15px -3px rgba(0,0,0,0.5);
}

[data-theme="dark"] .site-header {
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
}

[data-theme="dark"] .site-footer {
  background: #0f172a;
}

[data-theme="dark"] mark {
  background: #854d0e;
  color: #fef08a;
}

[data-theme="dark"] .search-box input {
  background: #0f172a;
  border-color: #334155;
}

[data-theme="dark"] .search-results {
  background: #1e293b;
  border-color: #334155;
}

[data-theme="dark"] .doc-image img {
  border-color: #334155;
}

/* ── Print styles ────────────────────────────────────── */
@media print {
  .site-header, .sidebar, .chapter-nav, .site-footer { display: none; }
  .chapter-layout { display: block; }
  .chapter-content { box-shadow: none; border: none; padding: 0; }
  .doc-image img { max-width: 80%; }
}
