:root {
  color-scheme: light;
  --bg: #ffffff;
  --sidebar: #fbfbfa;
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --surface-soft: #f6f8fa;
  --ink: #111827;
  --text: #2f3742;
  --muted: #64717f;
  --faint: #8a96a3;
  --line: #e5e7eb;
  --line-strong: #cfd6dd;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --accent-ink: #1d4ed8;
  --code-bg: #111827;
  --code-ink: #edf2f7;
  --inline-code-bg: #f3f4f6;
  --inline-code-ink: #1f2937;
  --topbar: rgba(255, 255, 255, 0.9);
  --tree-hover: #f0f2f4;
  --shadow: 0 20px 54px rgba(17, 24, 39, 0.14);
  --warning: #8a5a10;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d1117;
  --sidebar: #0f141b;
  --surface: #0d1117;
  --surface-raised: #151b23;
  --surface-soft: #111b26;
  --ink: #f0f3f6;
  --text: #c8d1dc;
  --muted: #98a5b3;
  --faint: #788594;
  --line: #27313c;
  --line-strong: #394656;
  --accent: #6ea8ff;
  --accent-soft: #13253c;
  --accent-ink: #9fc5ff;
  --code-bg: #05070a;
  --code-ink: #edf2f7;
  --inline-code-bg: #18212c;
  --inline-code-ink: #dce6f2;
  --topbar: rgba(13, 17, 23, 0.9);
  --tree-hover: #171f29;
  --shadow: 0 20px 54px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: 312px minmax(0, 1fr);
  min-height: 100vh;
  transition: grid-template-columns 160ms ease;
}

body.nav-collapsed .app-shell {
  grid-template-columns: 0 minmax(0, 1fr);
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
  border-right: 1px solid var(--line);
  background: var(--sidebar);
  padding: 18px 14px 28px;
  transition:
    opacity 140ms ease,
    padding 160ms ease,
    transform 160ms ease;
}

body.nav-collapsed .sidebar {
  overflow: hidden;
  border-right: 0;
  padding-right: 0;
  padding-left: 0;
  opacity: 0;
  pointer-events: none;
}

.brand {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 18px;
}

.brand > div:nth-child(2) {
  min-width: 0;
}

.brand-mark {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border-radius: 7px;
  background: var(--ink);
  color: var(--bg);
  font-size: 15px;
  font-weight: 800;
}

.brand h1 {
  margin: 0;
  font-size: 15px;
  line-height: 1.2;
}

.brand p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.sidebar-collapse-button {
  display: grid;
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  margin-left: auto;
  place-items: center;
  border-radius: 6px;
  padding: 0;
  color: var(--muted);
}

.sidebar-collapse-button:hover {
  color: var(--ink);
}

.sidebar-rail-toggle {
  position: fixed;
  z-index: 9;
  top: 16px;
  left: 10px;
  display: none;
  width: 32px;
  height: 32px;
  place-items: center;
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 0;
}

body.nav-collapsed .sidebar-rail-toggle {
  display: grid;
}

.controls {
  display: grid;
  gap: 8px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 16px;
  margin-bottom: 16px;
}

.field-label {
  margin-top: 6px;
  color: var(--faint);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

select,
input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-raised);
  color: var(--ink);
  padding: 9px 10px;
}

select:focus,
input:focus,
button:focus-visible {
  border-color: var(--accent);
  outline: 2px solid var(--accent-soft);
}

.page-list {
  padding-bottom: 32px;
}

.nav-section-title {
  margin: 8px 0 10px;
  color: var(--faint);
  font-size: 11px;
  font-weight: 750;
  text-transform: uppercase;
}

.tree-node {
  min-width: 0;
}

.tree-children {
  margin: 3px 0 8px 10px;
  border-left: 1px solid var(--line);
  padding-left: 9px;
}

.tree-link,
.tree-heading,
.page-link {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  padding: 7px 8px;
  text-align: left;
}

.tree-heading {
  cursor: default;
  color: var(--faint);
  font-size: 12px;
  font-weight: 750;
  text-transform: uppercase;
}

.tree-link {
  font-size: 13px;
  line-height: 1.25;
}

.tree-link:hover,
.page-link:hover {
  background: var(--tree-hover);
}

.tree-link.active,
.page-link.active {
  background: var(--accent-soft);
  color: var(--accent-ink);
  font-weight: 750;
}

.page-link strong {
  display: block;
  font-size: 13px;
  line-height: 1.25;
}

.page-link span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.content {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
}

.topbar {
  position: sticky;
  z-index: 5;
  top: 0;
  display: flex;
  gap: 14px;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  background: var(--topbar);
  backdrop-filter: blur(12px);
  padding: 13px 24px;
}

.topbar > div:nth-child(2) {
  flex: 1;
  min-width: 0;
}

.eyebrow {
  color: var(--muted);
  font-size: 12px;
}

#page-title {
  overflow: hidden;
  margin: 2px 0 0;
  font-size: 18px;
  line-height: 1.25;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-actions {
  display: flex;
  gap: 8px;
}

button {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface-raised);
  color: var(--ink);
  cursor: pointer;
  padding: 8px 11px;
}

button:hover {
  border-color: var(--line-strong);
}

.icon-button {
  display: grid;
  width: 38px;
  height: 38px;
  padding: 0;
  place-items: center;
}

.mobile-menu-button {
  display: none;
}

.reader-grid {
  display: block;
  width: min(100%, 900px);
  margin: 0 auto;
  padding: 44px 42px 96px;
}

body.nav-collapsed .reader-grid {
  width: min(100%, 980px);
}

.article {
  min-width: 0;
}

.article h1 {
  margin: 0 0 20px;
  color: var(--ink);
  font-size: 40px;
  line-height: 1.08;
  letter-spacing: 0;
}

.article h2 {
  margin: 40px 0 13px;
  border-top: 1px solid var(--line);
  padding-top: 28px;
  color: var(--ink);
  font-size: 25px;
  line-height: 1.25;
  letter-spacing: 0;
}

.article h3 {
  margin: 28px 0 10px;
  color: var(--ink);
  font-size: 18px;
  line-height: 1.35;
  letter-spacing: 0;
}

.article h4 {
  margin: 22px 0 8px;
  font-size: 15px;
}

.article p,
.article li {
  color: var(--text);
  font-size: 15.5px;
  line-height: 1.75;
}

.article p {
  margin: 0 0 15px;
}

.article ul {
  margin: 0 0 18px;
  padding-left: 22px;
}

.article a {
  color: var(--accent);
  font-weight: 650;
  text-decoration: none;
}

.article a:hover {
  text-decoration: underline;
}

.article code {
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--inline-code-bg);
  padding: 1px 5px;
  color: var(--inline-code-ink);
  font-size: 0.9em;
}

.article pre {
  overflow: auto;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--code-bg);
  color: var(--code-ink);
  padding: 15px 16px;
  line-height: 1.55;
}

.article pre code {
  border: 0;
  background: transparent;
  color: inherit;
  padding: 0;
}

.article blockquote {
  margin: 18px 0;
  border-left: 3px solid var(--accent);
  background: var(--surface-soft);
  padding: 11px 15px;
}

.article table {
  display: block;
  overflow-x: auto;
  width: 100%;
  margin: 18px 0;
  border-collapse: collapse;
  font-size: 14px;
}

.article th,
.article td {
  border: 1px solid var(--line);
  padding: 9px 11px;
  text-align: left;
  vertical-align: top;
}

.article th {
  background: var(--surface-soft);
  font-weight: 750;
}

.article details {
  margin: 0 0 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 12px 14px;
}

.article summary {
  cursor: pointer;
  color: var(--accent-ink);
  font-weight: 800;
}

.mermaid {
  position: relative;
  overflow: auto;
  margin: 18px 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-raised);
  cursor: zoom-in;
  padding: 18px;
}

.mermaid::after {
  position: absolute;
  right: 10px;
  bottom: 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: color-mix(in srgb, var(--surface-raised) 88%, transparent);
  color: var(--muted);
  content: "Click to expand";
  font-size: 11px;
  padding: 4px 8px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease;
}

.mermaid:hover::after,
.mermaid:focus::after {
  opacity: 1;
}

.toc {
  position: fixed;
  z-index: 8;
  top: 72px;
  right: 0;
  width: 268px;
  max-height: calc(100vh - 104px);
  overflow: auto;
  border: 1px solid var(--line);
  border-right: 0;
  border-radius: 8px 0 0 8px;
  background: color-mix(in srgb, var(--surface-raised) 94%, transparent);
  box-shadow: var(--shadow);
  padding: 15px 16px;
  transform: translateX(214px);
  transition:
    transform 160ms ease,
    opacity 160ms ease;
}

.toc::before {
  position: absolute;
  top: 12px;
  left: 12px;
  color: var(--muted);
  content: "Contents";
  font-size: 12px;
  font-weight: 750;
  line-height: 1;
  writing-mode: vertical-rl;
}

.toc:hover,
.toc:focus-within {
  transform: translateX(0);
}

.toc-title {
  margin-bottom: 10px;
  color: var(--faint);
  font-size: 11px;
  font-weight: 750;
  text-transform: uppercase;
}

.toc-title,
.toc-link {
  opacity: 0;
  transform: translateX(8px);
  transition: opacity 120ms ease;
}

.toc:hover .toc-title,
.toc:hover .toc-link,
.toc:focus-within .toc-title,
.toc:focus-within .toc-link {
  opacity: 1;
  transform: translateX(0);
}

.toc-link {
  display: block;
  margin: 0 0 8px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
  text-decoration: none;
}

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

.toc-link.level-h3 {
  padding-left: 10px;
}

.pager {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-top: 52px;
  border-top: 1px solid var(--line);
  padding-top: 22px;
}

.pager-link {
  min-height: 78px;
  border-color: var(--line);
  background: var(--surface-raised);
  padding: 14px 15px;
  text-align: left;
}

.pager-link.next {
  text-align: right;
}

.pager-link span {
  display: block;
  margin-bottom: 5px;
  color: var(--faint);
  font-size: 12px;
}

.pager-link strong {
  display: block;
  color: var(--ink);
  font-size: 14px;
  line-height: 1.35;
}

dialog {
  width: min(760px, calc(100vw - 32px));
  max-height: 80vh;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 48px rgba(12, 17, 22, 0.18);
  padding: 0;
}

.diagram-dialog {
  width: min(1180px, calc(100vw - 32px));
  height: min(820px, calc(100vh - 32px));
  max-height: calc(100vh - 32px);
}

.diagram-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-raised);
  padding: 12px 14px;
}

.diagram-toolbar h2 {
  margin: 0;
  font-size: 16px;
}

.diagram-toolbar > div {
  display: flex;
  gap: 8px;
}

.diagram-stage {
  overflow: auto;
  width: 100%;
  height: calc(100% - 58px);
  background:
    linear-gradient(var(--line) 1px, transparent 1px),
    linear-gradient(90deg, var(--line) 1px, transparent 1px),
    var(--surface);
  background-size: 28px 28px;
  padding: 42px;
}

.diagram-canvas {
  width: max-content;
  min-width: 100%;
  transform-origin: top left;
}

.diagram-canvas svg {
  display: block;
  max-width: none;
}

dialog::backdrop {
  background: rgba(16, 20, 24, 0.38);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  padding: 14px 16px;
}

.dialog-header h2 {
  margin: 0;
  font-size: 18px;
}

.dialog-body {
  color: var(--text);
  padding: 18px;
}

.dialog-body p {
  margin: 0 0 14px;
  line-height: 1.6;
}

.dialog-body dl {
  display: grid;
  gap: 8px;
  margin: 16px 0;
}

.dialog-body dl > div {
  display: grid;
  grid-template-columns: 96px minmax(0, 1fr);
  gap: 12px;
  border-top: 1px solid var(--line);
  padding-top: 8px;
}

.dialog-body dt {
  color: var(--faint);
  font-size: 12px;
  font-weight: 750;
  text-transform: uppercase;
}

.dialog-body dd {
  margin: 0;
}

@media (max-width: 1120px) {
  .reader-grid {
    width: min(100%, 900px);
  }
}

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  body.nav-collapsed .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    z-index: 10;
    inset: 0 auto 0 0;
    width: min(88vw, 320px);
    transform: translateX(-100%);
    transition: transform 160ms ease;
  }

  body.nav-open .sidebar {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
    padding: 18px 14px 28px;
  }

  body.nav-collapsed .sidebar {
    border-right: 1px solid var(--line);
    transform: translateX(-100%);
    opacity: 1;
    pointer-events: auto;
    padding: 18px 14px 28px;
  }

  body.nav-open.nav-collapsed .sidebar {
    transform: translateX(0);
  }

  .sidebar-collapse-button,
  .sidebar-rail-toggle {
    display: none;
  }

  body.nav-collapsed .sidebar-rail-toggle {
    display: none;
  }

  .mobile-menu-button {
    display: grid;
  }

  .topbar {
    padding: 12px 14px;
  }

  .top-actions {
    display: none;
  }

  .reader-grid {
    padding: 28px 18px 64px;
  }

  .toc {
    display: none;
  }

  .article h1 {
    font-size: 30px;
  }

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

  .diagram-toolbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .diagram-stage {
    height: calc(100% - 96px);
    padding: 24px;
  }
}
