/* =========================================================
   Design tokens
   ========================================================= */
:root {
  /* Dark (default) — clean, high-contrast, rational */
  --bg:        #0A0B0D;
  --surface:   #131417;
  --surface-2: #191B1F;
  --border:    #2B2E35;
  --border-soft: #202227;
  --text:      #F3F5F7;
  --muted:     #9BA1AA;
  --muted-2:   #666C75;
  --accent:    #6E93E0;
  --accent-soft: rgba(110, 147, 224, 0.16);
  --shadow:    0 1px 0 rgba(255,255,255,0.02);

  /* Type */
  --font-sans: 'Inter', 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Segoe UI', Helvetica, Arial, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, 'SF Mono', Menlo, Consolas, monospace;

  /* Scale */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 40px;
  --space-8: 64px;

  --radius-sm: 3px;
  --radius-md: 5px;
  --radius-lg: 7px;

  --sidebar-w: 280px;
  --topbar-h: 60px;

  --t-fast: 150ms ease-out;
  --t-base: 220ms ease-out;
}

[data-theme="light"] {
  --bg:        #FBFBFD;
  --surface:   #FFFFFF;
  --surface-2: #F4F6F9;
  --border:    #E1E4EA;
  --border-soft: #ECEEF2;
  --text:      #0E1116;
  --muted:     #545B66;
  --muted-2:   #8A909B;
  --accent:    #2D62BE;
  --accent-soft: rgba(45, 98, 190, 0.09);
  --shadow:    0 1px 2px rgba(20,23,27,0.05);
}

/* =========================================================
   Reset / base
   ========================================================= */
* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }

::selection { background: var(--accent-soft); color: var(--text); }

/* =========================================================
   Layout
   ========================================================= */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: var(--space-5);
  padding: 0 var(--space-6);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(160%) blur(10px);
  -webkit-backdrop-filter: saturate(160%) blur(10px);
  border-bottom: 1px solid var(--border-soft);
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  font-weight: 600;
  letter-spacing: -0.01em;
  font-size: 15px;
}
.brand .dot {
  width: 9px; height: 9px;
  border-radius: 2px;
  background: var(--accent);
}
.brand .sub {
  color: var(--muted);
  font-weight: 400;
  font-size: 13px;
}

.search {
  flex: 1;
  max-width: 520px;
  position: relative;
}
.search input {
  width: 100%;
  height: 38px;
  padding: 0 var(--space-4) 0 38px;
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  outline: none;
  transition: border-color var(--t-fast), background var(--t-fast), box-shadow var(--t-fast);
}
.search input::placeholder { color: var(--muted-2); }
.search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
}
.search .icon {
  position: absolute;
  left: 12px; top: 50%;
  transform: translateY(-50%);
  width: 16px; height: 16px;
  color: var(--muted);
  pointer-events: none;
}
.search .kbd {
  position: absolute;
  right: 10px; top: 50%;
  transform: translateY(-50%);
  font: 500 11px var(--font-mono);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 6px;
  background: var(--surface-2);
  pointer-events: none;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}
.icon-btn {
  width: 36px; height: 36px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--muted);
  border: 1px solid transparent;
  transition: color var(--t-fast), background var(--t-fast), border-color var(--t-fast);
}
.icon-btn:hover { color: var(--text); background: var(--surface); border-color: var(--border); }
.icon-btn:active { transform: translateY(1px); }
.icon-btn svg { width: 16px; height: 16px; }

.lang-btn {
  font-family: var(--font-mono);
  font-size: 12.5px;
  font-weight: 500;
  letter-spacing: 0.02em;
  min-width: 36px;
  padding: 0 8px;
}
.lang-btn:hover { color: var(--accent); }

.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  min-height: calc(100vh - var(--topbar-h));
}

/* =========================================================
   Sidebar (the tree)
   ========================================================= */
.sidebar {
  position: sticky;
  top: var(--topbar-h);
  align-self: start;
  height: calc(100vh - var(--topbar-h));
  overflow-y: auto;
  padding: var(--space-5) var(--space-3) var(--space-7) var(--space-5);
  border-right: 1px solid var(--border-soft);
  background: var(--bg);
}
.sidebar::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.sidebar::-webkit-scrollbar-thumb:hover { background: var(--muted-2); }

.tree { list-style: none; }

.tree-cat {
  margin-bottom: var(--space-2);
}
.tree-cat-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-sm);
  width: 100%;
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  transition: color var(--t-fast), background var(--t-fast);
}
.tree-cat-header:hover { color: var(--text); background: var(--surface); }
.tree-cat-header .chev {
  width: 12px; height: 12px;
  transition: transform var(--t-base);
  color: var(--muted-2);
}
.tree-cat.collapsed .chev { transform: rotate(-90deg); }

.tree-items {
  list-style: none;
  padding-left: var(--space-4);
  margin: var(--space-1) 0 var(--space-3);
  border-left: 1px solid var(--border-soft);
  margin-left: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.tree-cat.collapsed .tree-items { display: none; }

.tree-link {
  display: block;
  padding: 7px var(--space-3);
  margin-left: -1px;
  border-left: 1px solid transparent;
  color: var(--muted);
  font-size: 13.5px;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  transition: color var(--t-fast), background var(--t-fast), border-color var(--t-fast);
}
.tree-link:hover { color: var(--text); background: var(--surface); }
.tree-link.active {
  color: var(--text);
  background: var(--accent-soft);
  border-left-color: var(--accent);
  font-weight: 500;
}
.tree-link.hidden { display: none; }

.tree-cat.empty { display: none; }

/* =========================================================
   Main content
   ========================================================= */
.main {
  padding: var(--space-7) var(--space-8) var(--space-8);
  max-width: 920px;
  width: 100%;
}

.intro {
  padding: var(--space-6) 0 var(--space-7);
  border-bottom: 1px solid var(--border-soft);
  margin-bottom: var(--space-7);
}
.intro h1 {
  font-size: 32px;
  font-weight: 600;
  letter-spacing: -0.02em;
  line-height: 1.2;
  margin-bottom: var(--space-3);
}
.intro p {
  color: var(--muted);
  max-width: 60ch;
  font-size: 15px;
}
.intro .meta {
  display: flex;
  gap: var(--space-4);
  margin-top: var(--space-5);
  font-size: 13px;
  color: var(--muted);
}
.intro .meta a { color: var(--accent); }
.intro .meta a:hover { text-decoration: underline; }

.template {
  padding: var(--space-7) 0;
  border-bottom: 1px solid var(--border-soft);
  scroll-margin-top: calc(var(--topbar-h) + var(--space-4));
}
.template:last-child { border-bottom: none; }

.template-header {
  margin-bottom: var(--space-5);
}
.template .title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-2);
}
.template h2 {
  font-size: 22px;
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.3;
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: var(--space-3);
}

/* Complexity badges */
.complexity {
  display: inline-flex;
  gap: 6px;
  flex-shrink: 0;
  padding-top: 4px;
}
.complexity .badge {
  font: 500 11.5px var(--font-mono);
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.complexity .badge .label {
  color: var(--muted);
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* When-to-use callout */
.when-to-use {
  margin: var(--space-5) 0;
  padding: var(--space-4) var(--space-5);
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  font-size: 14px;
  line-height: 1.6;
  color: var(--text);
}
.when-to-use .label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: 6px;
}
.when-to-use .label svg {
  width: 13px;
  height: 13px;
}
.when-to-use p {
  margin: 0;
  color: var(--text);
}
.when-to-use strong {
  font-weight: 600;
  color: var(--text);
}
.when-to-use code.inline {
  font-family: var(--font-mono);
  font-size: 12.5px;
  padding: 1px 6px;
  background: var(--surface-2);
  border: 1px solid var(--border-soft);
  border-radius: 4px;
  color: var(--text);
}

/* Problems list */
.problems {
  margin-top: var(--space-5);
}
.problems .label {
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: var(--space-3);
}
.problems ul {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}
.problems a {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
  padding: 7px 12px;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  transition: color var(--t-fast), border-color var(--t-fast), background var(--t-fast);
}
.problems a:hover {
  color: var(--text);
  border-color: var(--accent);
  background: var(--surface-2);
}
.problems a svg {
  width: 12px;
  height: 12px;
  color: var(--muted-2);
  transition: color var(--t-fast);
}
.problems a:hover svg {
  color: var(--accent);
}

@media (max-width: 880px) {
  .template .title-row { flex-direction: column; gap: var(--space-3); }
  .complexity { padding-top: 0; }
}
.template .cat-tag {
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
}
.template .summary {
  color: var(--muted);
  font-size: 14.5px;
  max-width: 64ch;
}
.template .tags {
  margin-top: var(--space-3);
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}
.template .tag {
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--muted);
  padding: 1px 8px;
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: 4px;
}

/* Code block + tabs */
.code-block {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.code-tabs {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border-soft);
  padding: 0 var(--space-2);
  background: var(--surface-2);
}
.code-tabs .tabs {
  display: flex;
}
.tab-btn {
  position: relative;
  padding: 10px 14px;
  font-size: 13px;
  color: var(--muted);
  font-family: var(--font-mono);
  letter-spacing: 0.02em;
  transition: color var(--t-fast);
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--text); }
.tab-btn.active::after {
  content: '';
  position: absolute;
  left: 14px; right: 14px;
  bottom: -1px;
  height: 2px;
  background: var(--accent);
  border-radius: 2px;
}
.tab-btn[disabled] {
  color: var(--muted-2);
  cursor: not-allowed;
  text-decoration: line-through;
  opacity: 0.6;
}

.copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  transition: color var(--t-fast), background var(--t-fast), border-color var(--t-fast);
}
.copy-btn:hover { color: var(--text); background: var(--surface); border-color: var(--border); }
.copy-btn.copied { color: var(--accent); border-color: var(--accent-soft); }
.copy-btn svg { width: 13px; height: 13px; }

.code-block pre {
  margin: 0;
  padding: var(--space-5) var(--space-5);
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13.5px;
  line-height: 1.65;
  background: transparent !important;
}
.code-block pre code {
  background: transparent !important;
  padding: 0 !important;
  font-family: inherit !important;
  font-size: inherit !important;
}

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

/* Hide non-active language panels */
.lang-panel { display: none; }
.lang-panel.active { display: block; }

/* =========================================================
   Search-empty state
   ========================================================= */
.empty-state {
  padding: var(--space-7);
  text-align: center;
  color: var(--muted);
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
  margin: var(--space-7) 0;
}
.empty-state .emoji {
  font-size: 24px;
  margin-bottom: var(--space-2);
  filter: grayscale(0.2);
}
.template.hidden { display: none; }

/* =========================================================
   Footer
   ========================================================= */
.footer {
  margin-top: var(--space-7);
  padding-top: var(--space-5);
  border-top: 1px solid var(--border-soft);
  font-size: 13px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: var(--space-3);
}
.footer a { color: var(--accent); }
.footer a:hover { text-decoration: underline; }

/* =========================================================
   Responsive
   ========================================================= */
@media (max-width: 880px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    width: 80%;
    max-width: 320px;
    height: calc(100vh - var(--topbar-h));
    transform: translateX(-100%);
    transition: transform var(--t-base);
    z-index: 5;
    background: var(--bg);
    box-shadow: 12px 0 32px rgba(0,0,0,0.3);
  }
  .sidebar.open { transform: translateX(0); }
  .main { padding: var(--space-5) var(--space-5) var(--space-7); }
  .intro h1 { font-size: 26px; }
  .search { max-width: none; }
  .search .kbd { display: none; }
  .menu-btn { display: inline-flex; }
}

@media (min-width: 881px) { .menu-btn { display: none; } }

/* =========================================================
   Prism overrides — match our theme
   ========================================================= */
code[class*="language-"], pre[class*="language-"] {
  color: var(--text);
  background: transparent;
  text-shadow: none;
}

/* Dark theme tokens (Prism) */
[data-theme="dark"] .token.comment,
[data-theme="dark"] .token.prolog,
[data-theme="dark"] .token.doctype,
[data-theme="dark"] .token.cdata { color: #5C5C68; font-style: italic; }
[data-theme="dark"] .token.punctuation { color: #8B8B96; }
[data-theme="dark"] .token.property,
[data-theme="dark"] .token.tag,
[data-theme="dark"] .token.constant,
[data-theme="dark"] .token.symbol,
[data-theme="dark"] .token.deleted { color: #E07A8B; }
[data-theme="dark"] .token.boolean,
[data-theme="dark"] .token.number { color: #D8B96C; }
[data-theme="dark"] .token.selector,
[data-theme="dark"] .token.attr-name,
[data-theme="dark"] .token.string,
[data-theme="dark"] .token.char,
[data-theme="dark"] .token.builtin,
[data-theme="dark"] .token.inserted { color: #A8C97A; }
[data-theme="dark"] .token.operator,
[data-theme="dark"] .token.entity,
[data-theme="dark"] .token.url,
[data-theme="dark"] .language-css .token.string,
[data-theme="dark"] .style .token.string,
[data-theme="dark"] .token.variable { color: #9AA3AE; }
[data-theme="dark"] .token.atrule,
[data-theme="dark"] .token.attr-value,
[data-theme="dark"] .token.function,
[data-theme="dark"] .token.class-name { color: #7AB8D8; }
[data-theme="dark"] .token.keyword { color: #B894D8; }
[data-theme="dark"] .token.regex,
[data-theme="dark"] .token.important { color: #E07A8B; }

/* Light theme tokens (Prism) */
[data-theme="light"] .token.comment,
[data-theme="light"] .token.prolog,
[data-theme="light"] .token.doctype,
[data-theme="light"] .token.cdata { color: #A39681; font-style: italic; }
[data-theme="light"] .token.punctuation { color: #6B6B76; }
[data-theme="light"] .token.property,
[data-theme="light"] .token.tag,
[data-theme="light"] .token.constant,
[data-theme="light"] .token.symbol,
[data-theme="light"] .token.deleted { color: #B5475A; }
[data-theme="light"] .token.boolean,
[data-theme="light"] .token.number { color: #8B6F3D; }
[data-theme="light"] .token.selector,
[data-theme="light"] .token.attr-name,
[data-theme="light"] .token.string,
[data-theme="light"] .token.char,
[data-theme="light"] .token.builtin,
[data-theme="light"] .token.inserted { color: #4E7A2E; }
[data-theme="light"] .token.operator,
[data-theme="light"] .token.entity,
[data-theme="light"] .token.url,
[data-theme="light"] .token.variable { color: #59606B; }
[data-theme="light"] .token.atrule,
[data-theme="light"] .token.attr-value,
[data-theme="light"] .token.function,
[data-theme="light"] .token.class-name { color: #2E6B8B; }
[data-theme="light"] .token.keyword { color: #6B3D8B; }
[data-theme="light"] .token.regex,
[data-theme="light"] .token.important { color: #B5475A; }
