/* === CSS Variables & Theme === */
:root {
  --bg-primary: #ffffff;
  --bg-sidebar: #f8f9fa;
  --bg-toc: #f8f9fa;
  --bg-code: #f5f5f5;
  --bg-hover: #e9ecef;
  --bg-overlay: rgba(0, 0, 0, 0.4);
  --text-primary: #1a1a2e;
  --text-secondary: #666;
  --text-muted: #999;
  --border-color: #e0e0e0;
  --border-light: #eee;
  --accent: #2563eb;
  --accent-light: #dbeafe;
  --sidebar-width: 260px;
  --toc-width: 220px;
  --header-height: 50px;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.12);
  --radius: 6px;
  --transition: 0.2s ease;
}

[data-theme="dark"] {
  --bg-primary: #0d1117;
  --bg-sidebar: #161b22;
  --bg-toc: #161b22;
  --bg-code: #161b22;
  --bg-hover: #21262d;
  --bg-overlay: rgba(0, 0, 0, 0.6);
  --text-primary: #c9d1d9;
  --text-secondary: #8b949e;
  --text-muted: #6e7681;
  --border-color: #30363d;
  --border-light: #21262d;
  --accent: #58a6ff;
  --accent-light: #1c2d41;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
}

/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Hiragino Sans GB", sans-serif;
  color: var(--text-primary);
  background: var(--bg-primary);
  line-height: 1.75;
  overflow: hidden;
  height: 100vh;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* === Layout === */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr var(--toc-width);
  height: 100vh;
  overflow: hidden;
}

/* === Sidebar === */
.sidebar {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  position: relative;
  z-index: 20;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border-light);
  flex-shrink: 0;
}

.logo {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
}

.theme-toggle, .theme-toggle-mobile {
  background: none;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 6px;
  cursor: pointer;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition);
}
.theme-toggle:hover, .theme-toggle-mobile:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

/* Show sun in dark, moon in light */
.icon-sun { display: none; }
.icon-moon { display: block; }
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }

/* Sidebar search button */
.sidebar-search {
  padding: 8px 12px;
  flex-shrink: 0;
}

.sidebar-search-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  transition: var(--transition);
}
.sidebar-search-btn:hover {
  border-color: var(--accent);
  color: var(--text-secondary);
}
.sidebar-search-btn kbd {
  margin-left: auto;
  background: var(--bg-code);
  border: 1px solid var(--border-color);
  border-radius: 3px;
  padding: 1px 6px;
  font-size: 0.75rem;
  font-family: inherit;
  color: var(--text-muted);
}

/* Sidebar navigation */
.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}
.sidebar-nav::-webkit-scrollbar { width: 4px; }
.sidebar-nav::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 2px; }

.nav-section { margin-bottom: 2px; }

.nav-section-header {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  cursor: pointer;
  user-select: none;
  transition: var(--transition);
}
.nav-section-header:hover { color: var(--text-primary); }
.nav-section-header .chevron {
  margin-right: 6px;
  transition: transform 0.2s ease;
  flex-shrink: 0;
}
.nav-section.collapsed .chevron { transform: rotate(-90deg); }
.nav-section.collapsed .nav-section-body { display: none; }

.nav-group-label {
  padding: 6px 16px 2px 32px;
  font-size: 0.78rem;
  color: var(--text-muted);
  font-weight: 500;
}

.nav-page {
  display: block;
  padding: 5px 16px 5px 32px;
  color: var(--text-secondary);
  font-size: 0.88rem;
  text-decoration: none;
  border-left: 3px solid transparent;
  transition: var(--transition);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-page:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
  text-decoration: none;
}
.nav-page.active {
  color: var(--accent);
  background: var(--accent-light);
  border-left-color: var(--accent);
  font-weight: 500;
}
.nav-group .nav-page {
  padding-left: 42px;
}

/* Level badges */
.nav-page .level-badge {
  display: inline-block;
  font-size: 0.65rem;
  padding: 1px 5px;
  border-radius: 3px;
  margin-left: 4px;
  vertical-align: middle;
  font-weight: 500;
}
.level-badge.beginner { background: #dcfce7; color: #166534; }
.level-badge.intermediate { background: #dbeafe; color: #1e40af; }
.level-badge.advanced { background: #fef3c7; color: #92400e; }
[data-theme="dark"] .level-badge.beginner { background: #064e3b; color: #6ee7b7; }
[data-theme="dark"] .level-badge.intermediate { background: #1e3a5f; color: #93c5fd; }
[data-theme="dark"] .level-badge.advanced { background: #78350f; color: #fcd34d; }

/* === Content Area === */
.content {
  overflow-y: auto;
  padding: 32px 40px;
  height: 100vh;
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}
.content::-webkit-scrollbar { width: 6px; }
.content::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 3px; }

#article {
  max-width: 820px;
  margin: 0 auto;
}

/* Page title (dynamically inserted by app.js) */
.page-title {
  margin-bottom: 1.5rem;
  padding-bottom: 1rem;
  border-bottom: 2px solid var(--border-color);
}
.page-title h1 {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--text-primary);
  margin: 0;
  line-height: 1.3;
}
.page-meta {
  margin-top: 8px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

#article h2 {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 2rem 0 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border-light);
  color: var(--text-primary);
}

#article h3 {
  font-size: 1.2rem;
  font-weight: 600;
  margin: 1.5rem 0 0.75rem;
  color: var(--text-primary);
}

#article h4 {
  font-size: 1.05rem;
  font-weight: 600;
  margin: 1.2rem 0 0.5rem;
}

#article p {
  margin: 0.75rem 0;
}

#article strong {
  font-weight: 600;
  color: var(--text-primary);
}

#article ul, #article ol {
  margin: 0.5rem 0;
  padding-left: 1.5rem;
}

#article li {
  margin: 0.25rem 0;
}

/* Source references */
.source-ref {
  font-size: 0.82rem;
  color: var(--text-muted);
  border-left: 3px solid var(--border-color);
  padding: 4px 12px;
  margin: 8px 0;
  background: var(--bg-code);
  border-radius: 0 var(--radius) var(--radius) 0;
}

/* Source file links (dead links) */
a[href$=".py"], a[href*=".py#"], a[href$=".pyd"] {
  color: var(--text-muted);
  pointer-events: none;
  text-decoration: line-through dotted;
}

/* Tables */
#article table {
  width: 100%;
  border-collapse: collapse;
  margin: 1rem 0;
  font-size: 0.9rem;
  overflow-x: auto;
  display: block;
}

#article th, #article td {
  border: 1px solid var(--border-color);
  padding: 8px 12px;
  text-align: left;
}

#article th {
  background: var(--bg-code);
  font-weight: 600;
  white-space: nowrap;
}

#article tr:nth-child(even) {
  background: var(--bg-sidebar);
}

#article tr:hover {
  background: var(--bg-hover);
}

/* Code blocks */
#article pre {
  background: var(--bg-code);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 16px;
  overflow-x: auto;
  margin: 1rem 0;
  position: relative;
  font-size: 0.88rem;
  line-height: 1.6;
}

#article code {
  font-family: "Cascadia Code", "Fira Code", "JetBrains Mono", Consolas, monospace;
  font-size: 0.9em;
}

#article :not(pre) > code {
  background: var(--bg-code);
  padding: 2px 6px;
  border-radius: 3px;
  border: 1px solid var(--border-light);
}

/* Code language label */
.code-lang {
  position: absolute;
  top: 6px;
  left: 12px;
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  pointer-events: none;
}

/* Copy button */
.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 0.75rem;
  cursor: pointer;
  color: var(--text-muted);
  opacity: 0;
  transition: var(--transition);
}
#article pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--text-primary); background: var(--bg-hover); }

/* Mermaid diagrams */
.mermaid-wrapper {
  text-align: center;
  margin: 1.5rem 0;
  padding: 20px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  overflow-x: auto;
}

.mermaid-wrapper svg {
  max-width: 100%;
  height: auto;
}

/* Loading state */
.loading {
  text-align: center;
  padding: 4rem 0;
  color: var(--text-muted);
  font-size: 1rem;
}

/* === Page Navigation (Prev/Next) === */
.page-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 820px;
  margin: 2rem auto 0;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border-light);
  gap: 16px;
}

.page-nav-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 0.88rem;
  text-decoration: none;
  transition: var(--transition);
  flex: 1;
  min-width: 0;
}
.page-nav-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
  text-decoration: none;
}
.page-nav-btn.next { justify-content: flex-end; text-align: right; }
.page-nav-btn .label { font-size: 0.75rem; color: var(--text-muted); }
.page-nav-btn .title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* === TOC (Table of Contents) === */
.toc {
  background: var(--bg-toc);
  border-left: 1px solid var(--border-color);
  padding: 20px 16px;
  height: 100vh;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}
.toc::-webkit-scrollbar { width: 4px; }
.toc::-webkit-scrollbar-thumb { background: var(--border-color); border-radius: 2px; }

.toc-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
}

.toc-nav ul {
  list-style: none;
  padding: 0;
}

.toc-nav li {
  margin: 0;
}

.toc-nav a {
  display: block;
  padding: 3px 0;
  font-size: 0.82rem;
  color: var(--text-secondary);
  text-decoration: none;
  transition: var(--transition);
  line-height: 1.4;
}
.toc-nav a:hover { color: var(--accent); }
.toc-nav a.active {
  color: var(--accent);
  font-weight: 500;
}
.toc-nav .toc-h3 { padding-left: 14px; }

/* === Mobile Header === */
.mobile-header {
  display: none;
  align-items: center;
  padding: 0 12px;
  height: var(--header-height);
  background: var(--bg-sidebar);
  border-bottom: 1px solid var(--border-color);
  gap: 12px;
  flex-shrink: 0;
}
.hamburger {
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  padding: 4px;
  display: flex;
}
.mobile-title {
  font-weight: 600;
  font-size: 1rem;
  flex: 1;
}
.mobile-actions {
  display: flex;
  gap: 4px;
}
.search-btn-mobile {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  display: flex;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg-overlay);
  z-index: 15;
}

/* === Search Modal === */
.search-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg-overlay);
  z-index: 100;
  align-items: flex-start;
  justify-content: center;
  padding-top: 15vh;
}
.search-overlay.active { display: flex; }

.search-modal {
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  width: 90%;
  max-width: 560px;
  box-shadow: var(--shadow-md);
  overflow: hidden;
}

.search-input-wrap {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  gap: 10px;
  border-bottom: 1px solid var(--border-light);
  color: var(--text-muted);
}
.search-input-wrap input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 1rem;
  background: transparent;
  color: var(--text-primary);
  font-family: inherit;
}
.search-input-wrap input::placeholder { color: var(--text-muted); }

.search-close {
  background: var(--bg-code);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  padding: 3px 8px;
  font-size: 0.7rem;
  cursor: pointer;
  color: var(--text-muted);
  font-family: inherit;
}

.search-results {
  max-height: 400px;
  overflow-y: auto;
  padding: 8px;
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}

.search-hint {
  text-align: center;
  padding: 24px 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.search-result-item {
  display: block;
  padding: 10px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  text-decoration: none;
  color: var(--text-primary);
  transition: var(--transition);
}
.search-result-item:hover {
  background: var(--bg-hover);
  text-decoration: none;
}
.search-result-item .result-title {
  font-weight: 600;
  font-size: 0.92rem;
  margin-bottom: 2px;
}
.search-result-item .result-section {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.search-result-item .result-context {
  font-size: 0.82rem;
  color: var(--text-secondary);
  line-height: 1.4;
}
.search-result-item mark {
  background: var(--accent-light);
  color: var(--accent);
  padding: 0 2px;
  border-radius: 2px;
}

/* === Responsive === */
@media (max-width: 1023px) {
  .toc { display: none; }
  .layout {
    grid-template-columns: var(--sidebar-width) 1fr;
  }
}

@media (max-width: 767px) {
  .mobile-header { display: flex; }
  .layout {
    grid-template-columns: 1fr;
    padding-top: var(--header-height);
    margin-top: calc(var(--header-height) * -1);
  }
  .sidebar {
    position: fixed;
    left: -100%;
    top: 0;
    width: 280px;
    height: 100vh;
    z-index: 30;
    transition: left 0.25s ease;
    box-shadow: var(--shadow-md);
  }
  .sidebar.open { left: 0; }
  .sidebar-backdrop.open { display: block; }
  .content {
    padding: 20px 16px;
  }
  .toc { display: none; }
  .page-nav { flex-direction: column; }
  .page-nav-btn { text-align: center; justify-content: center; }
}

/* === Print === */
@media print {
  .sidebar, .toc, .mobile-header, .page-nav { display: none; }
  .content { padding: 0; height: auto; overflow: visible; }
  .layout { display: block; }
}
