
:root {
  --bg-primary: #ffffff;
  --bg-secondary: #f8fafc;
  --bg-sidebar: #f1f5f9;
  --border-color: #e2e8f0;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #94a3b8;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --accent-light: #eff6ff;
  --code-bg: #f8fafc;
  --code-border: #e2e8f0;
  --table-stripe: #f8fafc;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  --sidebar-width: 280px;
  --toc-width: 220px;
}

[data-theme="dark"] {
  --bg-primary: #0b0f19;
  --bg-secondary: #111827;
  --bg-sidebar: #0f172a;
  --border-color: #1e293b;
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --accent: #3b82f6;
  --accent-hover: #60a5fa;
  --accent-light: #1e293b;
  --code-bg: #090d16;
  --code-border: #1e293b;
  --table-stripe: #111827;
}

* {
  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.65;
  font-size: 16px;
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* Header */
.top-nav {
  position: sticky;
  top: 0;
  z-index: 100;
  height: 60px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  backdrop-filter: blur(8px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  text-decoration: none;
  color: var(--text-primary);
  font-weight: 700;
  font-size: 1.15rem;
}

.brand-badge {
  background: var(--accent);
  color: #ffffff;
  font-size: 0.75rem;
  padding: 0.15rem 0.5rem;
  border-radius: 9999px;
  font-weight: 600;
}

.nav-actions {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.search-trigger {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  padding: 0.4rem 0.85rem;
  border-radius: 0.5rem;
  color: var(--text-secondary);
  font-size: 0.875rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.search-trigger:hover {
  border-color: var(--accent);
  color: var(--text-primary);
}

.search-shortcut {
  background: var(--border-color);
  padding: 0.1rem 0.4rem;
  border-radius: 0.25rem;
  font-size: 0.75rem;
  font-family: var(--font-mono);
}

.theme-toggle-btn {
  background: none;
  border: 1px solid var(--border-color);
  border-radius: 0.5rem;
  padding: 0.4rem 0.6rem;
  cursor: pointer;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  font-size: 1rem;
}

/* Layout */
.layout {
  display: flex;
  min-height: calc(100vh - 60px);
}

/* Left Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  padding: 1.5rem 1rem;
  overflow-y: auto;
  position: sticky;
  top: 60px;
  height: calc(100vh - 60px);
  flex-shrink: 0;
}

.nav-group {
  margin-bottom: 1.5rem;
}

.nav-group-title {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
  padding-left: 0.5rem;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.65rem;
  color: var(--text-secondary);
  text-decoration: none;
  border-radius: 0.375rem;
  font-size: 0.9rem;
  transition: all 0.15s ease;
  margin-bottom: 0.15rem;
}

.nav-link:hover {
  background: var(--accent-light);
  color: var(--accent);
}

.nav-link.active {
  background: var(--accent-light);
  color: var(--accent);
  font-weight: 600;
}

/* Main Content */
.content-wrapper {
  flex: 1;
  display: flex;
  justify-content: center;
  min-width: 0;
}

.content {
  max-width: 820px;
  width: 100%;
  padding: 2.5rem 2rem 5rem;
}

/* Breadcrumbs */
.breadcrumbs {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 1.5rem;
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.breadcrumbs a {
  color: var(--text-secondary);
  text-decoration: none;
}

.breadcrumbs a:hover {
  color: var(--accent);
}

/* Typography */
h1 { font-size: 2.25rem; font-weight: 800; margin-bottom: 1.25rem; line-height: 1.25; border-bottom: 1px solid var(--border-color); padding-bottom: 0.5rem; }
h2 { font-size: 1.5rem; font-weight: 700; margin-top: 2.5rem; margin-bottom: 1rem; border-bottom: 1px solid var(--border-color); padding-bottom: 0.35rem; }
h3 { font-size: 1.25rem; font-weight: 600; margin-top: 1.75rem; margin-bottom: 0.75rem; }
h4 { font-size: 1.05rem; font-weight: 600; margin-top: 1.25rem; margin-bottom: 0.5rem; }

p { margin-bottom: 1.2rem; }

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

.header-anchor {
  opacity: 0;
  margin-left: -1.2rem;
  padding-right: 0.3rem;
  color: var(--text-muted);
  text-decoration: none;
}
h1:hover .header-anchor, h2:hover .header-anchor, h3:hover .header-anchor { opacity: 1; }

/* Lists */
ul, ol { margin-left: 1.5rem; margin-bottom: 1.25rem; }
li { margin-bottom: 0.35rem; }

/* Code Blocks */
pre {
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  border-radius: 0.5rem;
  padding: 1rem;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  line-height: 1.5;
  margin-bottom: 1.5rem;
}

code {
  font-family: var(--font-mono);
  font-size: 0.875em;
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  padding: 0.15rem 0.35rem;
  border-radius: 0.25rem;
}

pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: inherit;
}

.code-block {
  position: relative;
  margin-bottom: 1.5rem;
}

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--bg-sidebar);
  border: 1px solid var(--code-border);
  border-bottom: none;
  border-top-left-radius: 0.5rem;
  border-top-right-radius: 0.5rem;
  padding: 0.3rem 0.75rem;
  font-size: 0.75rem;
}

.code-header + pre {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  margin-bottom: 0;
}

.code-lang {
  font-weight: 600;
  text-transform: uppercase;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.code-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.playground-nav-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: linear-gradient(135deg, #2563eb, #7c3aed);
  color: #ffffff !important;
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.38rem 0.85rem;
  border-radius: 0.5rem;
  text-decoration: none !important;
  transition: opacity 0.15s ease;
}

.playground-nav-btn:hover {
  opacity: 0.92;
}

.playground-sidebar-link {
  font-weight: 700;
  color: var(--accent) !important;
  background: var(--accent-light);
  border: 1px solid var(--border-color);
}

.copy-btn, .run-btn, .play-btn {
  background: none;
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  font-size: 0.73rem;
  font-weight: 600;
  padding: 0.15rem 0.48rem;
  border-radius: 0.25rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.run-btn {
  color: var(--accent);
  border-color: var(--accent);
  background: var(--accent-light);
}

.copy-btn:hover, .run-btn:hover, .play-btn:hover {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.code-output {
  background: var(--bg-secondary);
  border: 1px solid var(--code-border);
  border-top: none;
  border-bottom-left-radius: 0.5rem;
  border-bottom-right-radius: 0.5rem;
  padding: 0.65rem 0.9rem;
  font-family: var(--font-mono);
  font-size: 0.84rem;
}

.code-output-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.74rem;
  color: var(--text-muted);
  margin-bottom: 0.4rem;
  padding-bottom: 0.3rem;
  border-bottom: 1px solid var(--border-color);
}

.code-output-pre {
  margin: 0;
  padding: 0;
  background: none;
  border: none;
  font-size: 0.84rem;
  line-height: 1.45;
  white-space: pre-wrap;
  color: var(--text-primary);
}

.code-output-res {
  margin-top: 0.35rem;
  padding-top: 0.35rem;
  border-top: 1px dashed var(--border-color);
  font-weight: 600;
  color: var(--accent);
}

/* Syntax Highlighting */
.tok-kw { color: #d73a49; font-weight: 600; }
.tok-type { color: #6f42c1; font-weight: 600; }
.tok-builtin { color: #005cc5; font-weight: 600; }
.tok-str { color: #032f62; }
.tok-char { color: #22863a; }
.tok-num { color: #005cc5; }
.tok-comment { color: #6a737d; font-style: italic; }
.tok-op { color: #d73a49; }
.tok-fn { color: #6f42c1; font-weight: 600; }
.tok-flag { color: #e36209; }
.tok-diff-add { color: #22863a; background: rgba(46, 160, 67, 0.15); display: block; }
.tok-diff-sub { color: #d73a49; background: rgba(248, 81, 73, 0.15); display: block; }
.tok-diff-hdr { color: #6f42c1; font-weight: bold; display: block; }

[data-theme="dark"] .tok-kw { color: #ff7b72; }
[data-theme="dark"] .tok-type { color: #d2a8ff; }
[data-theme="dark"] .tok-builtin { color: #79c0ff; }
[data-theme="dark"] .tok-str { color: #a5d6ff; }
[data-theme="dark"] .tok-char { color: #7ee787; }
[data-theme="dark"] .tok-num { color: #79c0ff; }
[data-theme="dark"] .tok-comment { color: #8b949e; }
[data-theme="dark"] .tok-op { color: #ff7b72; }
[data-theme="dark"] .tok-fn { color: #d2a8ff; }
[data-theme="dark"] .tok-flag { color: #ffa657; }

/* Tables */
.table-container {
  overflow-x: auto;
  margin-bottom: 1.5rem;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.95rem;
}

th, td {
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--border-color);
}

th {
  background: var(--bg-sidebar);
  font-weight: 600;
}

tr:nth-child(even) {
  background: var(--table-stripe);
}

/* Alerts */
.alert {
  padding: 1rem 1.25rem;
  border-radius: 0.5rem;
  margin-bottom: 1.5rem;
  border-left: 4px solid var(--accent);
  background: var(--bg-secondary);
}

.alert-title {
  font-weight: 700;
  font-size: 0.95rem;
  margin-bottom: 0.35rem;
}

.alert-note { border-left-color: #3b82f6; }
.alert-note .alert-title { color: #3b82f6; }

.alert-tip { border-left-color: #10b981; }
.alert-tip .alert-title { color: #10b981; }

.alert-important { border-left-color: #8b5cf6; }
.alert-important .alert-title { color: #8b5cf6; }

.alert-warning { border-left-color: #f59e0b; }
.alert-warning .alert-title { color: #f59e0b; }

.alert-caution { border-left-color: #ef4444; }
.alert-caution .alert-title { color: #ef4444; }

/* Math */
.math-inline {
  font-family: var(--font-mono);
  background: var(--code-bg);
  padding: 0.1rem 0.3rem;
  border-radius: 0.25rem;
}

.math-display {
  display: block;
  text-align: center;
  font-family: var(--font-mono);
  background: var(--code-bg);
  padding: 0.75rem;
  margin: 1rem 0;
  border-radius: 0.375rem;
  overflow-x: auto;
}

/* Details & Summary */
details {
  border: 1px solid var(--border-color);
  border-radius: 0.5rem;
  margin-bottom: 1.25rem;
  background: var(--bg-secondary);
  overflow: hidden;
}

summary {
  padding: 0.75rem 1rem;
  font-weight: 600;
  cursor: pointer;
  outline: none;
}

summary:hover {
  background: var(--accent-light);
}

details[open] summary {
  border-bottom: 1px solid var(--border-color);
}

details > div, details > p {
  padding: 1rem;
}

/* Page Navigation (Prev / Next) */
.page-nav {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border-color);
}

.page-nav-link {
  display: flex;
  flex-direction: column;
  padding: 0.75rem 1.25rem;
  border: 1px solid var(--border-color);
  border-radius: 0.5rem;
  text-decoration: none;
  min-width: 180px;
  transition: all 0.15s ease;
}

.page-nav-link:hover {
  border-color: var(--accent);
  background: var(--accent-light);
  text-decoration: none;
}

.page-nav-link .nav-dir {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  font-weight: 600;
}

.page-nav-link .nav-title {
  color: var(--accent);
  font-weight: 600;
  font-size: 0.95rem;
}

/* Right TOC */
.toc-sidebar {
  width: var(--toc-width);
  position: sticky;
  top: 60px;
  height: calc(100vh - 60px);
  overflow-y: auto;
  padding: 2.5rem 1rem;
  font-size: 0.85rem;
  flex-shrink: 0;
}

.toc-title {
  font-weight: 700;
  text-transform: uppercase;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.toc-link {
  display: block;
  color: var(--text-secondary);
  text-decoration: none;
  margin-bottom: 0.4rem;
  padding-left: 0.5rem;
  border-left: 2px solid transparent;
  transition: all 0.15s ease;
}

.toc-link:hover {
  color: var(--accent);
  border-left-color: var(--accent);
}

.toc-level-3 { padding-left: 1.25rem; }

/* Search Modal */
.search-modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  z-index: 1000;
  align-items: flex-start;
  justify-content: center;
  padding-top: 10vh;
}

.search-modal {
  background: var(--bg-primary);
  width: 90%;
  max-width: 600px;
  border-radius: 0.75rem;
  border: 1px solid var(--border-color);
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.search-input-box {
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border-color);
  padding: 0.75rem 1rem;
}

.search-input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  font-size: 1.1rem;
  color: var(--text-primary);
}

.search-results {
  max-height: 400px;
  overflow-y: auto;
  padding: 0.5rem;
}

.search-item {
  padding: 0.65rem 0.85rem;
  border-radius: 0.375rem;
  display: block;
  text-decoration: none;
  color: var(--text-primary);
  transition: background 0.15s ease;
}

.search-item:hover, .search-item.selected {
  background: var(--accent-light);
}

.search-item-title {
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--accent);
}

.search-item-desc {
  font-size: 0.825rem;
  color: var(--text-secondary);
}

/* Responsive */
@media (max-width: 1024px) {
  .toc-sidebar { display: none; }
}

@media (max-width: 768px) {
  .sidebar {
    display: none;
    position: fixed;
    top: 60px;
    left: 0;
    width: 100%;
    height: calc(100vh - 60px);
    z-index: 90;
  }
  .sidebar.open { display: block; }
  .content { padding: 1.5rem 1rem 3rem; }
}
