:root {
  --app-color: #007AFF;
  --app-color-dark: #0056cc;
  --bg: #f5f7fa;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --text-primary: #1a1d23;
  --text-secondary: #6b7280;
  --text-muted: #9ca3af;
  --border: #e5e7eb;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 4px 12px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.1), 0 8px 32px rgba(0,0,0,0.06);
  --toc-width: 260px;
  --header-height: 64px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --surface: #1a1d23;
    --surface-2: #22262e;
    --text-primary: #f0f2f5;
    --text-secondary: #9ca3af;
    --text-muted: #6b7280;
    --border: #2d3139;
    --shadow: 0 1px 3px rgba(0,0,0,0.3), 0 4px 12px rgba(0,0,0,0.2);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.4), 0 8px 32px rgba(0,0,0,0.2);
  }
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-primary);
  line-height: 1.7;
  font-size: 16px;
}

/* Header */
#site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--header-height);
  background: linear-gradient(135deg, var(--app-color) 0%, var(--app-color-dark) 100%);
  color: #fff;
  display: flex;
  align-items: center;
  padding: 0 24px;
  gap: 16px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.2);
}

#site-header .app-icon {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: rgba(255,255,255,0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

#site-header h1 {
  font-size: 18px;
  font-weight: 700;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#site-header h1 span.subtitle {
  font-size: 13px;
  font-weight: 400;
  opacity: 0.8;
  margin-left: 8px;
}

/* Language switcher */
#lang-switcher {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

#lang-switcher label {
  font-size: 13px;
  opacity: 0.9;
}

#lang-select {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: #fff;
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 13px;
  cursor: pointer;
  outline: none;
  max-width: 160px;
}

#lang-select option { background: var(--surface); color: var(--text-primary); }

/* Layout */
#layout {
  display: flex;
  max-width: 1280px;
  margin: 0 auto;
  padding: 32px 24px;
  gap: 32px;
  align-items: flex-start;
}

/* TOC sidebar */
#toc-sidebar {
  width: var(--toc-width);
  flex-shrink: 0;
  position: sticky;
  top: calc(var(--header-height) + 24px);
  max-height: calc(100vh - var(--header-height) - 48px);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

#toc-sidebar h2 {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 12px;
}

#toc-list { list-style: none; padding: 0; }
#toc-list li { margin: 2px 0; }

#toc-list a {
  display: block;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 13.5px;
  padding: 5px 8px;
  border-radius: var(--radius-sm);
  transition: all 0.15s ease;
  border-left: 2px solid transparent;
}

#toc-list a:hover,
#toc-list a.active {
  color: var(--app-color);
  border-left-color: var(--app-color);
}

#toc-list .toc-h3 { padding-left: 20px; font-size: 12.5px; }

/* Main content */
#content {
  flex: 1;
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px 48px;
  box-shadow: var(--shadow);
}

/* Loading / Error states */
#status-message {
  text-align: center;
  padding: 80px 24px;
  color: var(--text-secondary);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--app-color);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 16px;
}

@keyframes spin { to { transform: rotate(360deg); } }
.error-icon { font-size: 48px; margin-bottom: 16px; }

/* Markdown typography */
#md-content h1, #md-content h2, #md-content h3,
#md-content h4, #md-content h5, #md-content h6 {
  color: var(--text-primary);
  font-weight: 700;
  line-height: 1.3;
  margin: 1.8em 0 0.6em;
  scroll-margin-top: calc(var(--header-height) + 24px);
}

#md-content h1 { font-size: 2em; margin-top: 0; padding-bottom: 12px; border-bottom: 2px solid var(--border); }
#md-content h2 { font-size: 1.5em; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
#md-content h3 { font-size: 1.2em; }
#md-content h4 { font-size: 1.05em; }

#md-content p { margin: 0.8em 0; }

#md-content a { color: var(--app-color); text-decoration: none; border-bottom: 1px solid transparent; transition: border-color 0.15s; }
#md-content a:hover { border-bottom-color: var(--app-color); }

#md-content ul, #md-content ol { margin: 0.8em 0; padding-left: 1.6em; }
#md-content li { margin: 0.3em 0; }

#md-content blockquote {
  border-left: 4px solid var(--app-color);
  background: rgba(0, 122, 255, 0.06);
  margin: 1.2em 0;
  padding: 12px 16px;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-secondary);
}

#md-content code {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 0.875em;
  font-family: 'SF Mono', 'Fira Code', Consolas, monospace;
}

#md-content pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 16px 20px;
  overflow-x: auto;
  margin: 1.2em 0;
}

#md-content pre code { background: none; border: none; padding: 0; font-size: 0.875em; }

#md-content table { width: 100%; border-collapse: collapse; margin: 1.2em 0; font-size: 14px; }
#md-content th, #md-content td { border: 1px solid var(--border); padding: 10px 14px; text-align: left; }
#md-content th { background: var(--surface-2); font-weight: 600; font-size: 13px; }
#md-content tr:nth-child(even) { background: rgba(0,0,0,0.02); }

#md-content img { max-width: 100%; border-radius: var(--radius-sm); border: 1px solid var(--border); margin: 0.8em 0; }
#md-content hr { border: none; border-top: 1px solid var(--border); margin: 2em 0; }

/* Footer */
#site-footer {
  text-align: center;
  padding: 24px;
  font-size: 12px;
  color: var(--text-muted);
}

/* Responsive */
@media (max-width: 900px) {
  #toc-sidebar { display: none; }
  #layout { padding: 16px; gap: 0; }
  #content { padding: 24px 20px; }
}

@media (max-width: 480px) {
  #site-header { padding: 0 16px; }
  #site-header h1 span.subtitle { display: none; }
  #lang-select { max-width: 110px; }
}
