/* ============================================
   SHARED STYLE LIBRARY - Learn Explainers
   ============================================
   Include this CSS in any explainer page to get
   beautiful, consistent styling out of the box.
   ============================================ */

/* --- CSS Custom Properties --- */
:root {
  --bg-primary: #0f1117;
  --bg-secondary: #161922;
  --bg-tertiary: #1c1f2e;
  --bg-sidebar: #0c0e14;
  --bg-code: #0d0f17;
  --bg-hover: #1e2235;
  --accent: #3b82f6;
  --accent-dim: #2563eb;
  --accent-glow: rgba(59, 130, 246, 0.15);
  --accent-glow-strong: rgba(59, 130, 246, 0.3);
  --text-primary: #e4e7ef;
  --text-secondary: #9ca3b4;
  --text-muted: #6b7280;
  --text-heading: #f1f3f9;
  --border: #1e2235;
  --border-light: #2a2f45;
  --callout-info-bg: rgba(59, 130, 246, 0.08);
  --callout-info-border: #3b82f6;
  --callout-warn-bg: rgba(234, 179, 8, 0.08);
  --callout-warn-border: #eab308;
  --callout-tip-bg: rgba(34, 197, 94, 0.08);
  --callout-tip-border: #22c55e;
  --callout-danger-bg: rgba(239, 68, 68, 0.08);
  --callout-danger-border: #ef4444;
  --sidebar-width: 280px;
  --content-max-width: 820px;
  --header-height: 0px;
  --progress-height: 3px;
  --transition-fast: 0.15s ease;
  --transition-normal: 0.25s ease;
  --transition-slow: 0.4s ease;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'SF Mono', Consolas, monospace;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

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

html {
  scroll-behavior: smooth;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

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

body::before {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background:
    radial-gradient(ellipse 80% 60% at 10% 0%, rgba(59, 130, 246, 0.06) 0%, transparent 60%),
    radial-gradient(ellipse 60% 50% at 90% 100%, rgba(139, 92, 246, 0.04) 0%, transparent 60%);
  pointer-events: none;
  z-index: 0;
}

/* --- Reading Progress Bar --- */
.progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  height: var(--progress-height);
  background: linear-gradient(90deg, var(--accent), #8b5cf6);
  z-index: 1000;
  transition: width 0.1s linear;
  border-radius: 0 2px 2px 0;
  box-shadow: 0 0 10px rgba(59, 130, 246, 0.5);
}

/* --- Page Layout --- */
.page-wrapper {
  display: flex;
  min-height: 100vh;
  position: relative;
  z-index: 1;
}

/* --- Sidebar --- */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  z-index: 100;
  display: flex;
  flex-direction: column;
  scrollbar-width: thin;
  scrollbar-color: var(--border-light) transparent;
}

.sidebar::-webkit-scrollbar {
  width: 4px;
}

.sidebar::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 4px;
}

.sidebar-header {
  padding: 24px 20px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-header a {
  text-decoration: none;
  color: inherit;
}

.sidebar-logo {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-heading);
  letter-spacing: -0.02em;
}

.sidebar-logo span {
  color: var(--accent);
}

.sidebar-subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.sidebar-nav {
  padding: 16px 0;
  flex: 1;
}

.sidebar-section {
  margin-bottom: 8px;
}

.sidebar-section-title {
  padding: 6px 20px;
  font-size: 0.68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  user-select: none;
}

.sidebar-section-title::after {
  content: '';
  width: 5px;
  height: 5px;
  border-right: 1.5px solid var(--text-muted);
  border-bottom: 1.5px solid var(--text-muted);
  transform: rotate(45deg);
  transition: transform var(--transition-fast);
  flex-shrink: 0;
}

.sidebar-section.collapsed .sidebar-section-title::after {
  transform: rotate(-45deg);
}

.sidebar-section-items {
  overflow: hidden;
  transition: max-height var(--transition-normal);
  max-height: 1000px;
}

.sidebar-section.collapsed .sidebar-section-items {
  max-height: 0;
}

.sidebar-link {
  display: block;
  padding: 7px 20px 7px 28px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  text-decoration: none;
  transition: all var(--transition-fast);
  border-left: 2px solid transparent;
  line-height: 1.4;
}

.sidebar-link:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.sidebar-link.active {
  color: var(--accent);
  border-left-color: var(--accent);
  background: var(--accent-glow);
  font-weight: 500;
}

.sidebar-link .chapter-number {
  color: var(--text-muted);
  font-size: 0.75rem;
  margin-right: 6px;
  font-weight: 600;
}

.sidebar-link.active .chapter-number {
  color: var(--accent-dim);
}

/* --- Mobile Sidebar Toggle --- */
.sidebar-toggle {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 200;
  width: 40px;
  height: 40px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  color: var(--text-primary);
  font-size: 1.2rem;
  transition: background var(--transition-fast);
}

.sidebar-toggle:hover {
  background: var(--bg-tertiary);
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 90;
  backdrop-filter: blur(2px);
}

/* --- Main Content --- */
.content {
  margin-left: var(--sidebar-width);
  flex: 1;
  min-width: 0;
}

.content-inner {
  max-width: var(--content-max-width);
  margin: 0 auto;
  padding: 48px 40px 120px;
}

/* --- Typography --- */
h1, h2, h3, h4, h5, h6 {
  color: var(--text-heading);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.3;
}

h1 {
  font-size: 2.4rem;
  margin-bottom: 16px;
  background: linear-gradient(135deg, var(--text-heading), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

h2 {
  font-size: 1.7rem;
  margin-top: 64px;
  margin-bottom: 20px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

h2:first-child,
.chapter-content > h2:first-child {
  border-top: none;
  margin-top: 0;
  padding-top: 0;
}

h3 {
  font-size: 1.3rem;
  margin-top: 40px;
  margin-bottom: 14px;
  color: var(--text-primary);
}

h4 {
  font-size: 1.05rem;
  margin-top: 28px;
  margin-bottom: 10px;
  color: var(--text-primary);
}

p {
  margin-bottom: 16px;
  color: var(--text-primary);
}

strong {
  color: var(--text-heading);
  font-weight: 600;
}

em {
  font-style: italic;
}

a {
  color: var(--accent);
  text-decoration: none;
  transition: color var(--transition-fast);
}

a:hover {
  color: #60a5fa;
  text-decoration: underline;
}

/* --- Chapter Header --- */
.chapter-header {
  margin-bottom: 40px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border);
}

.chapter-label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--accent);
  margin-bottom: 8px;
}

.chapter-header h1 {
  margin-bottom: 12px;
}

.chapter-meta {
  font-size: 0.85rem;
  color: var(--text-muted);
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.chapter-meta span {
  display: flex;
  align-items: center;
  gap: 5px;
}

/* --- Lists --- */
ul, ol {
  margin-bottom: 16px;
  padding-left: 24px;
}

li {
  margin-bottom: 6px;
  color: var(--text-primary);
}

li::marker {
  color: var(--accent);
}

ul ul, ol ol, ul ol, ol ul {
  margin-top: 6px;
  margin-bottom: 6px;
}

/* --- Blockquotes --- */
blockquote {
  border-left: 3px solid var(--accent);
  padding: 12px 20px;
  margin: 20px 0;
  background: var(--accent-glow);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  color: var(--text-secondary);
  font-style: italic;
}

blockquote p {
  margin-bottom: 0;
  color: var(--text-secondary);
}

/* --- Inline Code --- */
code:not(pre code) {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--bg-tertiary);
  color: #e5a0f3;
  padding: 2px 7px;
  border-radius: 4px;
  border: 1px solid var(--border);
  word-break: break-word;
}

/* --- Code Blocks --- */
.code-block {
  margin: 20px 0;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg-code);
}

.code-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: var(--bg-tertiary);
  border-bottom: 1px solid var(--border);
  font-size: 0.78rem;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

.code-block-lang {
  display: flex;
  align-items: center;
  gap: 8px;
}

.code-block-lang::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.6;
}

.code-block-copy {
  background: none;
  border: 1px solid var(--border-light);
  color: var(--text-muted);
  cursor: pointer;
  padding: 3px 10px;
  border-radius: 4px;
  font-size: 0.72rem;
  font-family: var(--font-sans);
  transition: all var(--transition-fast);
}

.code-block-copy:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--text-muted);
}

.code-block-copy.copied {
  color: var(--callout-tip-border);
  border-color: var(--callout-tip-border);
}

.code-block pre {
  padding: 16px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.85rem;
  line-height: 1.65;
  color: var(--text-primary);
  scrollbar-width: thin;
  scrollbar-color: var(--border-light) transparent;
}

.code-block pre::-webkit-scrollbar {
  height: 4px;
}

.code-block pre::-webkit-scrollbar-track {
  background: transparent;
}

.code-block pre::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 4px;
}

.code-block pre code {
  font-family: inherit;
  font-size: inherit;
  background: none;
  border: none;
  padding: 0;
  color: inherit;
}

/* --- Syntax Highlighting (CSS-based) --- */
.token-keyword { color: #c678dd; }
.token-string { color: #98c379; }
.token-number { color: #d19a66; }
.token-comment { color: #5c6370; font-style: italic; }
.token-function { color: #61afef; }
.token-type { color: #e5c07b; }
.token-operator { color: #56b6c2; }
.token-property { color: #e06c75; }
.token-punctuation { color: #8891a5; }
.token-builtin { color: #e5c07b; }
.token-variable { color: #e06c75; }
.token-decorator { color: #d19a66; }
.token-tag { color: #e06c75; }
.token-attr { color: #d19a66; }

/* --- Tables --- */
.table-wrapper {
  margin: 20px 0;
  overflow-x: auto;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  scrollbar-width: thin;
  scrollbar-color: var(--border-light) transparent;
}

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

thead {
  background: var(--bg-tertiary);
}

th {
  text-align: left;
  padding: 12px 16px;
  font-weight: 600;
  color: var(--text-heading);
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

td {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
  vertical-align: top;
}

td:first-child code,
td:first-child {
  white-space: nowrap;
}

td code {
  word-break: keep-all;
}

tr:last-child td {
  border-bottom: none;
}

tbody tr:nth-child(even) {
  background: rgba(255, 255, 255, 0.015);
}

tbody tr {
  transition: background var(--transition-fast);
}

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

td code {
  font-size: 0.82em;
}

/* --- Callout Boxes --- */
.callout {
  margin: 24px 0;
  padding: 16px 20px;
  border-radius: var(--radius-md);
  border-left: 4px solid;
  position: relative;
}

.callout-title {
  font-weight: 600;
  font-size: 0.85rem;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.callout p {
  margin-bottom: 8px;
  font-size: 0.92rem;
  color: var(--text-secondary);
}

.callout p:last-child {
  margin-bottom: 0;
}

.callout.info {
  background: var(--callout-info-bg);
  border-left-color: var(--callout-info-border);
}

.callout.info .callout-title {
  color: var(--callout-info-border);
}

.callout.warning {
  background: var(--callout-warn-bg);
  border-left-color: var(--callout-warn-border);
}

.callout.warning .callout-title {
  color: var(--callout-warn-border);
}

.callout.tip {
  background: var(--callout-tip-bg);
  border-left-color: var(--callout-tip-border);
}

.callout.tip .callout-title {
  color: var(--callout-tip-border);
}

.callout.danger {
  background: var(--callout-danger-bg);
  border-left-color: var(--callout-danger-border);
}

.callout.danger .callout-title {
  color: var(--callout-danger-border);
}

/* --- SVG Diagrams --- */
.diagram {
  margin: 28px 0;
  padding: 24px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow-x: auto;
  text-align: center;
}

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

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

.diagram svg text {
  font-family: var(--font-sans);
}

.diagram svg .node-rect {
  fill: var(--bg-tertiary);
  stroke: var(--border-light);
  stroke-width: 1.5;
  rx: 8;
  ry: 8;
}

.diagram svg .node-rect-accent {
  fill: rgba(59, 130, 246, 0.12);
  stroke: var(--accent);
  stroke-width: 1.5;
  rx: 8;
  ry: 8;
}

.diagram svg .node-text {
  fill: var(--text-primary);
  font-size: 13px;
  font-weight: 500;
  text-anchor: middle;
  dominant-baseline: middle;
}

.diagram svg .node-text-sm {
  fill: var(--text-secondary);
  font-size: 11px;
  text-anchor: middle;
  dominant-baseline: middle;
}

.diagram svg .edge {
  stroke: var(--border-light);
  stroke-width: 1.5;
  fill: none;
  marker-end: url(#arrowhead);
}

.diagram svg .edge-accent {
  stroke: var(--accent);
  stroke-width: 1.5;
  fill: none;
  marker-end: url(#arrowhead-accent);
}

.diagram svg .label-text {
  fill: var(--text-muted);
  font-size: 10px;
  text-anchor: middle;
  dominant-baseline: middle;
}

/* --- Chapter Navigation (prev/next) --- */
.chapter-nav {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-top: 64px;
  padding-top: 32px;
  border-top: 1px solid var(--border);
}

.chapter-nav a {
  display: flex;
  flex-direction: column;
  padding: 16px 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  text-decoration: none;
  transition: all var(--transition-fast);
  min-width: 0;
  max-width: 48%;
}

.chapter-nav a:hover {
  border-color: var(--accent);
  background: var(--accent-glow);
  text-decoration: none;
}

.chapter-nav a .nav-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}

.chapter-nav a .nav-title {
  font-size: 0.95rem;
  color: var(--text-heading);
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chapter-nav .next {
  margin-left: auto;
  text-align: right;
}

/* --- Horizontal Rule --- */
hr {
  border: none;
  height: 1px;
  background: var(--border);
  margin: 40px 0;
}

/* --- Selection --- */
::selection {
  background: rgba(59, 130, 246, 0.3);
  color: #fff;
}

/* --- Scrollbar --- */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 3px;
}

/* --- Image --- */
img {
  max-width: 100%;
  border-radius: var(--radius-md);
}

/* --- Keyboard Shortcut --- */
kbd {
  font-family: var(--font-mono);
  font-size: 0.8em;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-light);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 6px;
  color: var(--text-secondary);
}

/* --- Badge / Tag --- */
.badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.badge-blue {
  background: rgba(59, 130, 246, 0.15);
  color: #60a5fa;
}

.badge-green {
  background: rgba(34, 197, 94, 0.15);
  color: #4ade80;
}

.badge-yellow {
  background: rgba(234, 179, 8, 0.15);
  color: #facc15;
}

.badge-red {
  background: rgba(239, 68, 68, 0.15);
  color: #f87171;
}

/* --- Stat Cards (for key metrics) --- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 16px;
  margin: 24px 0;
}

.stat-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 20px;
  text-align: center;
  transition: all var(--transition-fast);
}

.stat-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.stat-value {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1.2;
}

.stat-label {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* --- Responsive --- */
@media (max-width: 900px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform var(--transition-normal);
    z-index: 150;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-toggle {
    display: flex;
  }

  .sidebar-overlay.open {
    display: block;
  }

  .content {
    margin-left: 0;
  }

  .content-inner {
    padding: 60px 20px 80px;
  }

  h1 {
    font-size: 1.8rem;
  }

  h2 {
    font-size: 1.4rem;
  }

  .chapter-nav {
    flex-direction: column;
  }

  .chapter-nav a {
    max-width: 100%;
  }

  .stat-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 500px) {
  .content-inner {
    padding: 56px 14px 60px;
  }

  h1 {
    font-size: 1.5rem;
  }

  .stat-grid {
    grid-template-columns: 1fr 1fr;
  }

  .table-wrapper {
    font-size: 0.82rem;
  }

  th, td {
    padding: 8px 10px;
  }
}

/* --- Animations --- */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.content-inner > * {
  animation: fadeIn 0.4s ease both;
}

.content-inner > *:nth-child(1) { animation-delay: 0.05s; }
.content-inner > *:nth-child(2) { animation-delay: 0.1s; }
.content-inner > *:nth-child(3) { animation-delay: 0.15s; }
.content-inner > *:nth-child(4) { animation-delay: 0.2s; }
.content-inner > *:nth-child(5) { animation-delay: 0.25s; }
