/* ===== Design tokens ===== */
:root {
  --font-sans: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 12px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 2px 6px rgba(0, 0, 0, 0.06), 0 12px 32px rgba(0, 0, 0, 0.1);
  --shadow-focus: 0 0 0 3px;

  --ease: cubic-bezier(0.16, 1, 0.3, 1);
  --duration: 160ms;

  --topbar-h: 56px;
  --sidebar-w: 260px;
  --toc-w: 220px;
  --content-max: 780px;
}

/* ===== Light palette ===== */
html[data-effective-theme="light"] {
  --bg: #f8f9fb;
  --bg2: #f0f2f5;
  --surface: #ffffff;
  --surface2: #f5f6f8;
  --text: #1a1d24;
  --text2: #373c46;
  --muted: #6b7280;
  --border: rgba(0, 0, 0, 0.09);
  --border2: rgba(0, 0, 0, 0.05);
  --accent: #3b6ef6;
  --accent-soft: rgba(59, 110, 246, 0.1);
  --accent2: #16a06b;
  --warn: #c2720a;
  --warn-soft: rgba(194, 114, 10, 0.08);
  --accent2-soft: rgba(22, 160, 107, 0.08);
  --code-bg: #1b1e28;
  --code-text: #e4e8f0;
  --code-border: rgba(0, 0, 0, 0.12);
  --topbar-bg: rgba(255, 255, 255, 0.82);
  --sidebar-bg: rgba(255, 255, 255, 0.88);
}

/* ===== Dark palette ===== */
html[data-effective-theme="dark"] {
  --bg: #0c0e14;
  --bg2: #12151e;
  --surface: rgba(255, 255, 255, 0.05);
  --surface2: rgba(255, 255, 255, 0.035);
  --text: #e4e6ec;
  --text2: #b0b5c0;
  --muted: rgba(255, 255, 255, 0.48);
  --border: rgba(255, 255, 255, 0.1);
  --border2: rgba(255, 255, 255, 0.06);
  --accent: #6ea8ff;
  --accent-soft: rgba(110, 168, 255, 0.12);
  --accent2: #38d39f;
  --warn: #f5a623;
  --warn-soft: rgba(245, 166, 35, 0.1);
  --accent2-soft: rgba(56, 211, 159, 0.1);
  --code-bg: rgba(0, 0, 0, 0.5);
  --code-text: #dfe3ec;
  --code-border: rgba(255, 255, 255, 0.08);
  --topbar-bg: rgba(12, 14, 20, 0.78);
  --sidebar-bg: rgba(18, 21, 30, 0.92);
}

/* ===== Color scheme hint ===== */
@media (prefers-color-scheme: dark) {
  .app-shell[data-theme="auto"] { color-scheme: dark; }
}
@media (prefers-color-scheme: light) {
  .app-shell[data-theme="auto"] { color-scheme: light; }
}

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

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

html, body {
  height: 100%;
}

body {
  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;
}

a {
  color: inherit;
  text-decoration: none;
}

button {
  font-family: inherit;
}

/* ===== Accessibility ===== */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  z-index: 999;
  left: 16px;
  top: 12px;
  padding: 8px 16px;
  border-radius: var(--radius-md);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  font-weight: 600;
  font-size: 13px;
  transform: translateY(-200%);
  transition: transform var(--duration) var(--ease);
}
.skip-link:focus {
  transform: translateY(0);
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ===== App shell ===== */
.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ===== Topbar ===== */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 12px;
  height: var(--topbar-h);
  padding: 0 16px;
  background: var(--topbar-bg);
  backdrop-filter: blur(16px) saturate(1.6);
  -webkit-backdrop-filter: blur(16px) saturate(1.6);
  border-bottom: 1px solid var(--border2);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: -0.02em;
  white-space: nowrap;
  flex-shrink: 0;
}
.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #fff;
  font-family: var(--font-mono);
  font-weight: 700;
  font-size: 12px;
}
.brand-text {
  display: none;
}

/* ===== Buttons ===== */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text2);
  cursor: pointer;
  flex-shrink: 0;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.icon-btn:hover {
  background: var(--surface2);
  color: var(--text);
}
.icon-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 32px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text2);
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease), border-color var(--duration) var(--ease);
}
.pill-btn:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.pill-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.top-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
  margin-left: auto;
}

/* ===== Search ===== */
.search {
  position: relative;
  flex: 1;
  max-width: 480px;
  margin: 0 auto;
}
.search-input {
  width: 100%;
  height: 36px;
  border-radius: 999px;
  padding: 0 40px 0 14px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 13px;
  font-family: var(--font-sans);
  outline: none;
  transition: border-color var(--duration) var(--ease), box-shadow var(--duration) var(--ease);
}
.search-input::placeholder {
  color: var(--muted);
}
.search-input:focus {
  border-color: var(--accent);
  box-shadow: var(--shadow-focus) var(--accent-soft);
  background: var(--surface);
}
.search-hint {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  border: 1px solid var(--border2);
  border-radius: 6px;
  padding: 1px 6px;
  background: var(--surface);
  pointer-events: none;
}

.search-results {
  position: absolute;
  left: 0;
  right: 0;
  top: calc(100% + 6px);
  border-radius: var(--radius-md);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  overflow: hidden;
  display: none;
  max-height: 420px;
  overflow-y: auto;
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
}
.search-results.is-open {
  display: block;
}
.search-item {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 10px 14px;
  cursor: pointer;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  transition: background 80ms ease;
}
.search-item + .search-item {
  border-top: 1px solid var(--border2);
}
.search-item:hover,
.search-item.is-active {
  background: var(--accent-soft);
}
.search-item-title {
  font-weight: 600;
  font-size: 14px;
}
.search-item-meta {
  color: var(--muted);
  font-size: 12px;
  margin-top: 2px;
}
.search-empty {
  padding: 16px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

/* ===== Layout ===== */
.layout {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr var(--toc-w);
  gap: 24px;
  flex: 1;
  padding: 20px 20px 12px;
  max-width: 1440px;
  margin: 0 auto;
  width: 100%;
}

/* ===== Sidebar ===== */
.sidebar {
  position: sticky;
  top: calc(var(--topbar-h) + 16px);
  height: calc(100vh - var(--topbar-h) - 32px);
  overflow-y: auto;
  overscroll-behavior: contain;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--sidebar-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

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

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 10px;
}
.sidebar-title {
  font-weight: 700;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.nav {
  padding: 4px 8px 14px;
}
.nav-group {
  margin-bottom: 4px;
}
.nav-group-title {
  padding: 10px 10px 4px;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
.nav-link {
  display: flex;
  align-items: center;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  color: var(--text2);
  font-size: 14px;
  font-weight: 500;
  border: 1px solid transparent;
  transition: background var(--duration) var(--ease), color var(--duration) var(--ease);
}
.nav-link:hover {
  background: var(--accent-soft);
  color: var(--text);
}
.nav-link.is-active {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 600;
}

.sidebar-footer {
  padding: 10px 14px 14px;
  border-top: 1px solid var(--border2);
}
.kbd-help {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}
.kbd-row {
  display: flex;
  gap: 6px;
  align-items: center;
}
.kbd {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 5px;
  border: 1px solid var(--border2);
  background: var(--surface2);
  color: var(--text2);
  line-height: 1.5;
}

/* ===== Backdrop (mobile) ===== */
.backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

/* ===== Main content ===== */
.main {
  min-width: 0;
}
.content {
  position: relative;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--surface);
  overflow: hidden;
}
.article {
  padding: 28px 32px 36px;
  max-width: var(--content-max);
}

/* ===== Loading ===== */
.loading {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  gap: 10px;
  background: var(--surface);
  color: var(--muted);
  font-weight: 500;
  font-size: 14px;
}
.loading.is-visible {
  display: flex;
}
.spinner {
  width: 16px;
  height: 16px;
  border-radius: 999px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 800ms linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== TOC (right sidebar) ===== */
.toc {
  position: sticky;
  top: calc(var(--topbar-h) + 16px);
  height: calc(100vh - var(--topbar-h) - 32px);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 14px 12px;
}
.toc::-webkit-scrollbar {
  width: 4px;
}
.toc::-webkit-scrollbar-track {
  background: transparent;
}
.toc::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 999px;
}
.toc-title {
  font-weight: 700;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 8px;
}
.toc-nav {
  display: grid;
  gap: 2px;
}
.toc-link {
  font-size: 13px;
  color: var(--muted);
  padding: 4px 8px;
  border-radius: 6px;
  border-left: 2px solid transparent;
  transition: color var(--duration) var(--ease), background var(--duration) var(--ease);
}
.toc-link:hover {
  color: var(--text);
  background: var(--accent-soft);
}
.toc-link.is-active {
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 500;
}
.toc-link.is-sub {
  margin-left: 12px;
  font-size: 12.5px;
}
.toc-empty {
  color: var(--muted);
  font-size: 12px;
}

/* ===== Typography ===== */
.article h1 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.2;
  margin: 0 0 6px;
}
.article h2 {
  font-size: 19px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.3;
  margin: 32px 0 12px;
  padding-top: 20px;
  border-top: 1px solid var(--border2);
}
.article h2:first-child,
.article h1 + h2,
.article .card + h2,
.article .note + h2 {
  border-top: 0;
  padding-top: 0;
}
.article h3 {
  font-size: 15px;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 22px 0 8px;
  color: var(--text2);
}
.article p {
  margin: 8px 0;
  line-height: 1.7;
  color: var(--text2);
}
.article ul {
  margin: 8px 0 8px 18px;
  color: var(--text2);
}
.article li {
  margin: 4px 0;
  line-height: 1.6;
}
.article li + li {
  margin-top: 4px;
}
.article strong {
  font-weight: 600;
  color: var(--text);
}
.article a {
  color: var(--accent);
}
.article a:hover {
  text-decoration: underline;
}
.article code:not(pre code) {
  font-family: var(--font-mono);
  font-size: 0.9em;
  padding: 0.1em 0.4em;
  border-radius: 6px;
  background: var(--surface2);
  border: 1px solid var(--border2);
  color: var(--text);
  word-break: break-word;
}

.scroll-anchor {
  scroll-margin-top: calc(var(--topbar-h) + 20px);
}

/* ===== Content components ===== */
.card {
  border: 1px solid var(--border2);
  background: var(--surface2);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  margin: 14px 0;
}
.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.muted {
  color: var(--muted);
}

.note {
  border-left: 3px solid var(--accent2);
  padding: 12px 16px;
  border-radius: var(--radius-md);
  background: var(--accent2-soft);
  margin: 14px 0;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text2);
}
.note strong {
  color: var(--text);
}
.warn {
  border-left-color: var(--warn);
  background: var(--warn-soft);
}

/* ===== Code blocks ===== */
pre {
  position: relative;
  margin: 14px 0;
  border-radius: var(--radius-md);
  border: 1px solid var(--code-border);
  background: var(--code-bg);
  color: var(--code-text);
  overflow: auto;
}
pre::-webkit-scrollbar {
  height: 4px;
}
pre::-webkit-scrollbar-track {
  background: transparent;
}
pre::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 999px;
}
pre code {
  display: block;
  padding: 14px 16px;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
  tab-size: 2;
}
.code-toolbar {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 10px;
  background: rgba(0, 0, 0, 0.25);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}
.code-meta {
  font-family: var(--font-mono);
  font-size: 11px;
  color: rgba(255, 255, 255, 0.5);
  font-weight: 500;
}
.code-actions {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.code-btn {
  height: 26px;
  padding: 0 10px;
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.7);
  cursor: pointer;
  font-weight: 600;
  font-size: 11px;
  font-family: var(--font-sans);
  transition: background 120ms ease, color 120ms ease;
}
.code-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  color: rgba(255, 255, 255, 0.95);
}
.code-toast {
  position: absolute;
  right: 10px;
  top: 40px;
  padding: 4px 10px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  opacity: 0;
  transform: translateY(-4px);
  pointer-events: none;
  transition: opacity var(--duration) var(--ease), transform var(--duration) var(--ease);
}
.code-toast.is-visible {
  opacity: 1;
  transform: translateY(0);
}

/* ===== Tables ===== */
table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  margin: 14px 0;
  overflow: hidden;
  border-radius: var(--radius-md);
  border: 1px solid var(--border2);
  background: var(--surface2);
  font-size: 14px;
}
th,
td {
  text-align: left;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border2);
  vertical-align: top;
}
th {
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  background: var(--surface2);
}
tr:last-child td {
  border-bottom: 0;
}

/* ===== Footer ===== */
.footer {
  padding: 16px 20px 24px;
  color: var(--muted);
  font-size: 13px;
}
.footer-inner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  max-width: 1440px;
  margin: 0 auto;
  border-top: 1px solid var(--border2);
  padding-top: 16px;
}
.footer-links {
  display: flex;
  gap: 16px;
}
.footer a {
  transition: color var(--duration) var(--ease);
}
.footer a:hover {
  color: var(--text);
}

/* ===== Responsive: Desktop ===== */
@media (min-width: 1080px) {
  .brand-text {
    display: inline;
  }
  .nav-toggle {
    display: none;
  }
  .sidebar-header button {
    display: none;
  }
  .backdrop {
    display: none;
  }
}

/* ===== Responsive: Tablet ===== */
@media (max-width: 1079px) and (min-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
    max-width: 760px;
  }
  .toc {
    display: none;
  }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    width: 280px;
    z-index: 25;
    border-radius: 0;
    transform: translateX(-110%);
    transition: transform 220ms var(--ease);
  }
  .sidebar.is-open {
    transform: translateX(0);
  }
  .top-actions a {
    display: none;
  }
}

/* ===== Responsive: Mobile ===== */
@media (max-width: 767px) {
  :root {
    --topbar-h: 50px;
  }
  .layout {
    grid-template-columns: 1fr;
    padding: 12px 12px 8px;
    gap: 0;
  }
  .article {
    padding: 20px 16px 28px;
  }
  .toc {
    display: none;
  }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100dvh;
    width: min(88vw, 300px);
    z-index: 25;
    border-radius: 0;
    transform: translateX(-110%);
    transition: transform 220ms var(--ease);
  }
  .sidebar.is-open {
    transform: translateX(0);
  }
  .brand-text {
    display: none;
  }
  .top-actions a {
    display: none;
  }
  .search {
    max-width: none;
  }
  .grid-2 {
    grid-template-columns: 1fr;
  }
  .footer-inner {
    flex-direction: column;
    text-align: center;
  }
}

/* ===== Reduced motion ===== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0s !important;
    animation-duration: 0s !important;
    scroll-behavior: auto !important;
  }
}
