/* ===== CSS Variables ===== */
:root {
  --bg: #ffffff;
  --bg-secondary: #f9fafb;
  --bg-tertiary: #f1f5f9;
  --text: #0f172a;
  --text-secondary: #64748b;
  --text-tertiary: #94a3b8;
  --border: #e2e8f0;
  --border-light: #f1f5f9;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-light: #eff6ff;
  --primary-lighter: #dbeafe;
  --code-bg: #f8fafc;
  --code-border: #e2e8f0;
  --sidebar-width: 260px;
  --toc-width: 220px;
  --header-height: 60px;
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 16px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.06);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.08);
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"] {
  --bg: #0f172a;
  --bg-secondary: #1e293b;
  --bg-tertiary: #1e293b;
  --text: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-tertiary: #64748b;
  --border: #1e293b;
  --border-light: #1e293b;
  --primary: #3b82f6;
  --primary-hover: #60a5fa;
  --primary-light: #172554;
  --primary-lighter: #1e3a5f;
  --code-bg: #1e293b;
  --code-border: #334155;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.4);
}

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

html { scroll-behavior: smooth; scroll-padding-top: 80px; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Noto Sans', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.7;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--primary); text-decoration: none; transition: color var(--transition); }
a:hover { color: var(--primary-hover); }

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

/* ===== Sidebar ===== */
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  overflow-x: hidden;
  border-right: 1px solid var(--border);
  padding: 20px 12px;
  background: var(--bg-secondary);
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

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

.sidebar-logo {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 20px;
  padding: 0 10px;
  letter-spacing: -0.01em;
}

.sidebar-logo a {
  color: var(--text);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-logo a::before {
  content: '';
  width: 24px;
  height: 24px;
  background: var(--primary);
  border-radius: var(--radius-sm);
  display: inline-block;
  flex-shrink: 0;
}

.sidebar-search {
  margin-bottom: 16px;
}

.sidebar-search input {
  width: 100%;
  padding: 7px 10px 7px 32px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--bg);
  color: var(--text);
  font-size: 0.8125rem;
  font-family: inherit;
  transition: border-color var(--transition), box-shadow var(--transition);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2394a3b8' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='8'/%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 10px center;
  background-size: 14px;
}

.sidebar-search input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.sidebar-search input::placeholder { color: var(--text-tertiary); }

.sidebar-nav { margin-bottom: 24px; }

/* Nav group (collapsible) */
.nav-group { margin-bottom: 2px; }

.sidebar-nav-title {
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  padding: 16px 10px 4px;
}

.sidebar-nav-title:first-child { padding-top: 0; }

.nav-toggle {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  user-select: none;
}

.nav-toggle::after {
  content: '';
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid var(--text-tertiary);
  transition: transform 0.2s ease;
  flex-shrink: 0;
}

.nav-group.open .nav-toggle::after {
  transform: rotate(180deg);
}

.nav-group-items {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.3s ease, opacity 0.2s ease;
  opacity: 0;
}

.nav-group.open .nav-group-items {
  max-height: 600px;
  opacity: 1;
}

.sidebar-nav-item {
  display: block;
  padding: 5px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 0.8125rem;
  line-height: 1.5;
  transition: all var(--transition);
  font-weight: 450;
}

.sidebar-nav-item:hover {
  background: var(--bg-tertiary);
  color: var(--text);
  text-decoration: none;
}

.sidebar-nav-item.active {
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 550;
}

/* ===== Main content ===== */
.main {
  padding: 36px 56px 64px;
  max-width: 780px;
  min-width: 0;
}

.main h1 {
  font-size: 1.875rem;
  font-weight: 750;
  margin-bottom: 6px;
  line-height: 1.25;
  letter-spacing: -0.025em;
}

.main .subtitle {
  font-size: 1rem;
  color: var(--text-secondary);
  margin-bottom: 32px;
  line-height: 1.6;
}

.main h2 {
  font-size: 1.25rem;
  font-weight: 650;
  margin-top: 48px;
  margin-bottom: 16px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
  letter-spacing: -0.01em;
  scroll-margin-top: 24px;
}

.main h2:first-of-type { border-top: none; padding-top: 0; margin-top: 32px; }

.main h3 {
  font-size: 1rem;
  font-weight: 600;
  margin-top: 28px;
  margin-bottom: 10px;
  letter-spacing: -0.005em;
}

.main h4 {
  font-size: 0.9375rem;
  font-weight: 600;
  margin-top: 24px;
  margin-bottom: 8px;
}

.main p {
  margin-bottom: 14px;
  font-size: 0.9375rem;
  color: var(--text);
}

.main ul, .main ol {
  margin-bottom: 14px;
  padding-left: 22px;
}

.main li {
  margin-bottom: 5px;
  font-size: 0.9375rem;
}

.main strong { font-weight: 600; }

/* ===== Code blocks ===== */
pre {
  position: relative;
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  overflow-x: auto;
  margin-bottom: 16px;
  font-size: 0.8125rem;
  line-height: 1.65;
  scrollbar-width: thin;
}

pre::-webkit-scrollbar { height: 4px; }
pre::-webkit-scrollbar-track { background: transparent; }
pre::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

code {
  font-family: 'JetBrains Mono', 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 0.875em;
}

:not(pre) > code {
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.8125em;
  word-break: break-word;
}

/* ===== Tables ===== */
table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
  font-size: 0.875rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

th, td {
  padding: 9px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

th {
  background: var(--bg-secondary);
  font-weight: 600;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  white-space: nowrap;
}

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

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

/* ===== Callout ===== */
.callout {
  border-left: 3px solid var(--primary);
  background: var(--primary-light);
  padding: 14px 16px;
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  margin-bottom: 20px;
}

.callout p { margin-bottom: 0; font-size: 0.875rem; }
.callout p + p { margin-top: 8px; }

.callout-title {
  font-weight: 600;
  margin-bottom: 4px;
  font-size: 0.875rem;
  color: var(--primary);
}

/* ===== Cards ===== */
.cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 28px;
}

.card {
  display: block;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  transition: all var(--transition);
  background: var(--bg);
}

.card:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-md);
  text-decoration: none;
  transform: translateY(-1px);
}

.card h3 {
  font-size: 0.875rem;
  font-weight: 600;
  margin: 0 0 4px 0;
  color: var(--text);
}

.card p {
  font-size: 0.8125rem;
  color: var(--text-secondary);
  margin: 0;
  line-height: 1.5;
}

/* ===== Steps ===== */
.steps {
  border-left: 2px solid var(--border);
  margin-left: 12px;
  padding-left: 24px;
}

.step {
  position: relative;
  margin-bottom: 24px;
}

.step::before {
  content: '';
  position: absolute;
  left: -29px;
  top: 8px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--primary);
  border: 2px solid var(--bg);
  box-shadow: 0 0 0 2px var(--primary-lighter);
}

.step h3 {
  font-size: 0.9375rem;
  font-weight: 600;
  margin-top: 0;
  margin-bottom: 6px;
}

.step p {
  font-size: 0.875rem;
  color: var(--text-secondary);
}

/* ===== TOC ===== */
.toc {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 36px 16px 36px 0;
  border-left: 1px solid var(--border);
  font-size: 0.75rem;
  scrollbar-width: thin;
}

.toc-title {
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  margin-bottom: 10px;
  padding-left: 12px;
}

.toc a {
  display: block;
  padding: 3px 10px;
  color: var(--text-tertiary);
  border-left: 2px solid transparent;
  margin-left: -1px;
  transition: all var(--transition);
  line-height: 1.5;
}

.toc a:hover {
  color: var(--text);
  text-decoration: none;
}

.toc a.active {
  color: var(--primary);
  border-left-color: var(--primary);
  font-weight: 500;
}

/* ===== Theme toggle ===== */
.theme-toggle {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  z-index: 100;
  box-shadow: var(--shadow-md);
  transition: all var(--transition);
}

.theme-toggle:hover {
  box-shadow: var(--shadow-lg);
  transform: scale(1.05);
}

/* ===== Copy button ===== */
.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  cursor: pointer;
  font-size: 0.6875rem;
  font-family: inherit;
  color: var(--text-tertiary);
  transition: all var(--transition);
  opacity: 0;
}

pre:hover .copy-btn { opacity: 1; }

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

/* ===== Badge ===== */
.badge {
  display: inline-block;
  background: var(--primary-light);
  color: var(--primary);
  padding: 2px 10px;
  border-radius: 20px;
  font-size: 0.6875rem;
  font-weight: 600;
  margin-bottom: 12px;
  letter-spacing: 0.01em;
}

/* ===== Mobile hamburger ===== */
.mobile-menu-btn {
  display: none;
  position: fixed;
  top: 14px;
  left: 14px;
  z-index: 200;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-sm);
}

.mobile-menu-btn svg {
  width: 18px;
  height: 18px;
  stroke: var(--text);
  stroke-width: 2;
  fill: none;
  stroke-linecap: round;
}

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

/* ===== Selection ===== */
::selection {
  background: var(--primary-lighter);
  color: var(--text);
}

/* ===== Scrollbar for main ===== */
.main::-webkit-scrollbar { width: 4px; }
.main::-webkit-scrollbar-track { background: transparent; }
.main::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

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

@media (max-width: 768px) {
  .mobile-menu-btn { display: flex; }
  .sidebar-overlay.active { display: block; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 160;
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    width: 280px;
    box-shadow: var(--shadow-lg);
  }

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

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

  .main {
    padding: 56px 20px 48px;
  }

  .main h1 { font-size: 1.5rem; }
  .main h2 { font-size: 1.125rem; }

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

  .theme-toggle { bottom: 16px; right: 16px; }
}

/* ===== Print ===== */
@media print {
  .sidebar, .toc, .theme-toggle, .mobile-menu-btn, .copy-btn { display: none !important; }
  .layout { grid-template-columns: 1fr; }
  .main { max-width: 100%; padding: 0; }
  body { background: white; color: black; }
}
