:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: "SF Mono", Monaco, Consolas, "Liberation Mono", "Courier New", monospace;

  /* Dark Theme Default */
  --bg-primary: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #21262d;
  --bg-card: rgba(22, 27, 34, 0.85);
  --border-color: #30363d;
  --border-focus: #58a6ff;
  --text-primary: #f0f6fc;
  --text-secondary: #8b949e;
  --text-muted: #6e7681;
  --accent-color: #2f81f7;
  --accent-hover: #388bfd;
  --accent-soft: rgba(56, 139, 253, 0.15);
  --success-color: #238636;
  --success-text: #3fb950;
  --warning-color: #d29922;
  --danger-color: #da3633;
  --code-bg: #0b0f14;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
}

[data-theme="light"] {
  --bg-primary: #f6f8fa;
  --bg-secondary: #ffffff;
  --bg-tertiary: #eaeef2;
  --bg-card: rgba(255, 255, 255, 0.95);
  --border-color: #d0d7de;
  --border-focus: #0969da;
  --text-primary: #1f2328;
  --text-secondary: #57606a;
  --text-muted: #8c959f;
  --accent-color: #0969da;
  --accent-hover: #0860ca;
  --accent-soft: rgba(9, 105, 218, 0.1);
  --success-color: #2da44e;
  --success-text: #1a7f37;
  --warning-color: #9a6700;
  --danger-color: #cf222e;
  --code-bg: #f6f8fa;
  --shadow-sm: 0 1px 3px rgba(31, 35, 40, 0.12);
  --shadow-md: 0 4px 12px rgba(31, 35, 40, 0.12);
  --shadow-lg: 0 8px 24px rgba(31, 35, 40, 0.15);
}

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

body {
  font-family: var(--font-sans);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* === Header === */
header.app-header {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: 12px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(10px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, #2f81f7, #bc8cff);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 700;
  font-size: 18px;
  box-shadow: var(--shadow-sm);
}

.brand h1 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
}

.brand p {
  font-size: 12px;
  color: var(--text-secondary);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.search-box {
  position: relative;
  width: 280px;
}

.search-box input {
  width: 100%;
  padding: 7px 12px 7px 32px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  transition: all 0.2s;
}

.search-box input:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-box svg {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  width: 14px;
  height: 14px;
}

.theme-toggle, .btn-github {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: all 0.2s;
  text-decoration: none;
}

.theme-toggle:hover, .btn-github:hover {
  background: var(--border-color);
}

/* === Layout Container === */
.app-container {
  display: flex;
  flex: 1;
  height: calc(100vh - 65px);
  overflow: hidden;
}

/* === Sidebar === */
aside.sidebar {
  width: 320px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-color);
  background: var(--bg-tertiary);
}

.tab-btn {
  flex: 1;
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  text-align: center;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.2s;
}

.tab-btn.active {
  color: var(--accent-color);
  border-bottom-color: var(--accent-color);
  background: var(--bg-secondary);
}

.chapter-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.chapter-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  margin-bottom: 4px;
  cursor: pointer;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  transition: all 0.15s;
  border: 1px solid transparent;
}

.chapter-item:hover {
  background: var(--bg-tertiary);
}

.chapter-item.active {
  background: var(--accent-soft);
  border-color: var(--accent-color);
}

.chapter-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  white-space: nowrap;
}

.chapter-item.active .chapter-badge {
  background: var(--accent-color);
  color: white;
}

.chapter-info h4 {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  line-height: 1.3;
}

.chapter-info p {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* === Main Content === */
main.content-area {
  flex: 1;
  overflow-y: auto;
  padding: 28px 40px;
  background: var(--bg-primary);
}

.detail-header {
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 20px;
  margin-bottom: 24px;
}

.meta-badges {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}

.badge {
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 4px;
  font-weight: 500;
}

.badge-vol {
  background: var(--accent-soft);
  color: var(--accent-color);
  border: 1px solid var(--accent-color);
}

.badge-complexity {
  background: rgba(210, 153, 34, 0.15);
  color: var(--warning-color);
  border: 1px solid var(--warning-color);
  font-family: var(--font-mono);
}

.badge-tag {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
}

.detail-title {
  font-size: 26px;
  font-weight: 700;
  margin-bottom: 8px;
}

.detail-subtitle {
  font-size: 14px;
  color: var(--text-secondary);
}

/* Section Cards */
.section-block {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 20px 24px;
  margin-bottom: 24px;
  box-shadow: var(--shadow-sm);
}

.section-block h3 {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-primary);
  border-bottom: 1px solid var(--border-color);
  padding-bottom: 8px;
}

.context-box {
  background: var(--bg-tertiary);
  border-left: 4px solid var(--accent-color);
  padding: 14px 16px;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 14px;
  color: var(--text-primary);
  margin-bottom: 16px;
}

.principles-content {
  font-size: 14px;
  color: var(--text-primary);
  line-height: 1.7;
}

.principles-content ul, .principles-content ol {
  padding-left: 20px;
  margin: 10px 0;
}

.principles-content li {
  margin-bottom: 6px;
}

/* Diagram Box */
.diagram-container {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  margin-top: 14px;
}

.svg-diagram-card, .mermaid-card {
  background: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow-x: auto;
}

.diagram-caption {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 12px;
  text-align: center;
}

/* Interactive Playground */
.playground-box {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 16px 20px;
}

.playground-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.playground-controls input, .playground-controls select {
  padding: 6px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text-primary);
  font-size: 13px;
}

.playground-btn {
  background: var(--accent-color);
  color: white;
  border: none;
  padding: 7px 16px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
}

.playground-btn:hover {
  background: var(--accent-hover);
}

.playground-btn.btn-secondary {
  background: var(--bg-secondary);
  color: var(--text-primary);
  border: 1px solid var(--border-color);
}

.playground-btn.btn-secondary:hover {
  background: var(--border-color);
}

.playground-output {
  background: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--success-text);
  max-height: 200px;
  overflow-y: auto;
  white-space: pre-wrap;
}

/* Code Viewer */
.code-viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.file-path {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--accent-color);
}

.copy-btn {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.copy-btn:hover {
  color: var(--text-primary);
  background: var(--border-color);
}

pre.code-block {
  background: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 16px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-primary);
  max-height: 480px;
}

pre.code-block code.hljs {
  background: transparent !important;
  padding: 0 !important;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
}

/* Responsive */
@media (max-width: 900px) {
  .app-container {
    flex-direction: column;
    height: auto;
  }
  aside.sidebar {
    width: 100%;
    max-height: 280px;
  }
  main.content-area {
    padding: 20px;
  }
}
