:root {
  --paper: #fbfaf6;
  --ink: #1c1917;
  --muted: #78716c;
  --rule: #e7e5e4;
  --accent: #0f766e;
  --accent-ink: #115e59;
  --sidebar: #f5f5f4;
  --card: #ffffff;
  --danger: #b91c1c;
  --code-bg: #f5f5f4;
  --status-bg: #ecfdf5;
  --status-ink: #115e59;
  --status-border: #a7f3d0;
  --status-error-bg: #fef2f2;
  --status-error-border: #fecaca;
  --hover-border: #99f6e4;
  --shadow: 0 1px 2px rgba(28, 25, 23, 0.06), 0 8px 24px rgba(28, 25, 23, 0.04);
  --ui: "IBM Plex Sans", system-ui, sans-serif;
  --body: "Source Serif 4", "Iowan Old Style", Georgia, serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --sidebar-w: 280px;
  --bar-h: 56px;
  --measure: 46rem;
  color-scheme: light;
}

[data-theme="dark"] {
  --paper: #141210;
  --ink: #f5f2eb;
  --muted: #a8a29e;
  --rule: #3f3b37;
  --accent: #2dd4bf;
  --accent-ink: #5eead4;
  --sidebar: #1c1917;
  --card: #1c1917;
  --danger: #f87171;
  --code-bg: #292524;
  --status-bg: #134e4a;
  --status-ink: #99f6e4;
  --status-border: #115e59;
  --status-error-bg: #3f1d1d;
  --status-error-border: #7f1d1d;
  --hover-border: #134e4a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
}

body {
  font-family: var(--ui);
  color: var(--ink);
  background: var(--paper);
  display: flex;
  flex-direction: column;
}

a {
  color: var(--accent-ink);
  text-decoration: none;
}
a:hover { text-decoration: underline; }

code {
  font-family: var(--mono);
  font-size: 0.9em;
}

.topbar {
  height: var(--bar-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  border-bottom: 1px solid var(--rule);
  background: var(--card);
  z-index: 20;
}

.brand {
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
}
.brand:hover { text-decoration: none; color: var(--accent-ink); }

.brand span {
  font-weight: 400;
  color: var(--muted);
}

.crumb {
  flex: 1;
  min-width: 0;
  font-size: 0.88rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.crumb .sep {
  margin: 0 0.4em;
  color: var(--rule);
}

.crumb .here { color: var(--ink); }

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

.topbar-actions a,
.home-link {
  font-size: 0.85rem;
  color: var(--muted);
  white-space: nowrap;
}

.menu-btn,
.icon-btn {
  border: 1px solid var(--rule);
  background: var(--card);
  color: var(--ink);
  border-radius: 8px;
  padding: 6px 10px;
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}

.menu-btn { display: none; }

.icon-btn:hover { border-color: var(--accent); }

.seg {
  display: inline-flex;
  border: 1px solid var(--rule);
  border-radius: 8px;
  overflow: hidden;
}

.seg button {
  border: 0;
  border-right: 1px solid var(--rule);
  background: transparent;
  color: var(--muted);
  padding: 6px 10px;
  font: inherit;
  font-size: 0.75rem;
  cursor: pointer;
}

.seg button:last-child { border-right: 0; }

.seg button[aria-pressed="true"] {
  background: var(--sidebar);
  color: var(--ink);
  font-weight: 600;
}

.jump-bar {
  flex: 1;
  min-width: 140px;
  max-width: 380px;
  margin: 0 12px;
}

body.view-catalog .jump-bar { display: none; }

.jump-bar input,
.jump-hero input {
  width: 100%;
  font: inherit;
  font-size: 0.92rem;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--rule);
  border-radius: 10px;
  padding: 8px 12px;
  outline: none;
}

.jump-bar input:focus,
.jump-hero input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.18);
}

.jump-hero {
  margin: 0 0 28px;
}

.jump-hero input {
  font-size: 1.05rem;
  padding: 14px 16px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  background: var(--card);
}

.jump-error {
  margin: 8px 0 0;
  color: var(--danger);
  font-size: 0.85rem;
}

.saved-heading {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 0 0 12px;
}

.layout {
  flex: 1;
  min-height: 0;
  display: flex;
}

body.view-catalog .sidebar,
body.view-catalog .menu-btn,
body.view-catalog .topbar-files {
  display: none;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--rule);
  overflow: auto;
  padding: 16px 12px 32px;
  transition: width 0.18s ease, padding 0.18s ease;
}

.sidebar-head {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-bottom: 12px;
}

.sidebar-head-text {
  flex: 1;
  min-width: 0;
}

.sidebar-toggle {
  appearance: none;
  -webkit-appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  min-width: 52px;
  height: 32px;
  border: 1px solid var(--rule);
  background: var(--card);
  color: var(--ink);
  border-radius: 8px;
  font: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  padding: 0 8px;
  z-index: 2;
}

.sidebar-toggle:hover { border-color: var(--accent); }

.sidebar-title {
  font-size: 0.95rem;
  font-weight: 600;
  margin: 0 0 2px;
}

.sidebar-meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin: 0;
  word-break: break-all;
}

html[data-sidebar="collapsed"] .sidebar,
body.sidebar-collapsed .sidebar,
.sidebar.is-collapsed {
  width: 56px;
  padding: 12px 8px;
  overflow: hidden;
  cursor: pointer;
}

html[data-sidebar="collapsed"] .sidebar-head,
body.sidebar-collapsed .sidebar-head,
.sidebar.is-collapsed .sidebar-head {
  margin: 0;
  justify-content: center;
}

html[data-sidebar="collapsed"] .sidebar-head-text,
html[data-sidebar="collapsed"] .file-list,
html[data-sidebar="collapsed"] .folder-tools,
html[data-sidebar="collapsed"] .truncated,
body.sidebar-collapsed .sidebar-head-text,
body.sidebar-collapsed .file-list,
body.sidebar-collapsed .folder-tools,
body.sidebar-collapsed .truncated,
.sidebar.is-collapsed .sidebar-head-text,
.sidebar.is-collapsed .file-list,
.sidebar.is-collapsed .folder-tools,
.sidebar.is-collapsed .truncated {
  display: none;
}

.folder-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0 0 10px;
}

.folder-tools[hidden] { display: none; }

.folder-tools button {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid var(--rule);
  background: var(--card);
  color: var(--muted);
  border-radius: 6px;
  font: inherit;
  font-size: 0.7rem;
  padding: 4px 8px;
  cursor: pointer;
}

.folder-tools button:hover {
  border-color: var(--accent);
  color: var(--ink);
}

.file-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.file-list li { margin: 0; }

.file-list.nested {
  list-style: none;
  margin: 0;
  padding: 0;
}

.dir-row {
  display: flex;
  align-items: center;
  gap: 2px;
  margin: 8px 0 2px;
  padding-left: calc(var(--depth, 0) * 12px);
  min-width: 0;
}

.dir-twist {
  appearance: none;
  -webkit-appearance: none;
  flex-shrink: 0;
  width: 22px;
  height: 22px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  user-select: none;
}

.dir-twist:hover {
  background: rgba(15, 118, 110, 0.12);
  color: var(--ink);
}

.dir-twist .caret {
  display: block;
  width: 0;
  height: 0;
  border-top: 4px solid transparent;
  border-bottom: 4px solid transparent;
  border-left: 6px solid currentColor;
  transform: translateX(1px);
}

.dir-twist[aria-expanded="true"] .caret {
  transform: translate(0, 1px) rotate(90deg);
}

.dir-name {
  min-width: 0;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  text-decoration: none;
  border-radius: 4px;
  padding: 2px 4px;
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dir-name:hover {
  color: var(--accent-ink);
  text-decoration: none;
  background: rgba(15, 118, 110, 0.1);
}

.dir-count {
  flex-shrink: 0;
  margin-left: 2px;
  font-size: 0.65rem;
  color: var(--muted);
  letter-spacing: 0;
  text-transform: none;
}

.file-item .file-link {
  padding-left: calc(22px + var(--depth, 0) * 12px);
}

.file-item.is-root .file-link {
  padding-left: 8px;
}

.file-list .dir-up {
  margin: 0 0 12px;
  font-size: 0.8rem;
  text-transform: none;
  letter-spacing: 0;
}

.file-list .dir-up a {
  display: inline-block;
  color: var(--accent-ink);
  text-decoration: none;
  padding: 4px 8px;
  margin: 0 -8px;
  border-radius: 6px;
}

.file-list .dir-up a:hover {
  background: rgba(15, 118, 110, 0.1);
  text-decoration: none;
}

.file-link {
  display: block;
  padding: 6px 8px;
  border-radius: 6px;
  color: var(--ink);
  font-size: 0.88rem;
  line-height: 1.35;
  text-decoration: none;
}

.file-link:hover {
  background: rgba(15, 118, 110, 0.08);
  text-decoration: none;
}

.file-link.active {
  background: rgba(15, 118, 110, 0.14);
  color: var(--accent-ink);
  font-weight: 600;
}

.muted { color: var(--muted); font-size: 0.85rem; }

.truncated {
  margin-top: 16px;
  font-size: 0.75rem;
  color: var(--muted);
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.status {
  margin: 0;
  padding: 8px 24px;
  font-size: 0.8rem;
  background: var(--status-bg);
  color: var(--status-ink);
  border-bottom: 1px solid var(--status-border);
}
.status.error {
  background: var(--status-error-bg);
  color: var(--danger);
  border-bottom-color: var(--status-error-border);
}

.article {
  flex: 1;
  overflow: auto;
  padding: 32px 40px 80px;
}

#article {
  display: flex;
  justify-content: flex-start;
  width: 100%;
}

[data-align="center"] #article { justify-content: center; }
[data-align="right"] #article { justify-content: flex-end; }
[data-align="left"] #article { justify-content: flex-start; }

.catalog {
  max-width: 860px;
  margin: 0 auto;
}

.catalog h1 {
  font-family: var(--body);
  font-size: 2rem;
  font-weight: 600;
  margin: 8px 0 8px;
}

.lede {
  color: var(--muted);
  margin: 0 0 28px;
  max-width: 40em;
}

.repo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.repo-card {
  display: block;
  background: var(--card);
  border: 1px solid var(--rule);
  border-radius: 12px;
  padding: 18px 18px 16px;
  box-shadow: var(--shadow);
  color: inherit;
  text-decoration: none;
}

.repo-card:hover {
  text-decoration: none;
  border-color: var(--hover-border);
}

.repo-card h2 {
  font-size: 1.1rem;
  margin: 0 0 4px;
}

.repo-id {
  font-family: var(--mono);
  font-size: 0.75rem;
  color: var(--muted);
  margin: 0 0 8px;
}

.repo-card p { margin: 0; color: var(--ink); font-size: 0.92rem; line-height: 1.45; }

.empty { color: var(--muted); }
.empty.error { color: var(--danger); }

.article-msg {
  max-width: var(--measure);
  width: 100%;
}
.article-msg h1 {
  font-family: var(--body);
  font-size: 1.8rem;
  margin: 0 0 8px;
}
.article-msg.error h1 { color: var(--danger); }

.folder-pick {
  list-style: none;
  margin: 20px 0 8px;
  padding: 0;
  display: grid;
  gap: 10px;
}

.folder-card {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 14px 16px;
  border: 1px solid var(--rule);
  border-radius: 10px;
  background: var(--card);
  box-shadow: var(--shadow);
  color: inherit;
  text-decoration: none;
}

.folder-card:hover {
  text-decoration: none;
  border-color: var(--hover-border);
}

.folder-card-name {
  font-family: var(--ui);
  font-weight: 600;
  color: var(--ink);
}

.folder-card-meta {
  font-size: 0.8rem;
  color: var(--muted);
}

.pick-heading {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 24px 0 8px;
}

.pick-list {
  padding-left: 1.2em;
  line-height: 1.8;
}

.markdown-body {
  font-family: var(--body);
  font-size: 1.05rem;
  line-height: 1.75;
  max-width: var(--measure);
  width: 100%;
  color: var(--ink);
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4,
.markdown-body h5,
.markdown-body h6 {
  font-family: var(--ui);
  font-weight: 600;
  line-height: 1.3;
  margin: 1.6em 0 0.5em;
}

.markdown-body h1 { font-size: 2em; }
.markdown-body h2 {
  font-size: 1.45em;
  padding-bottom: 0.25em;
  border-bottom: 1px solid var(--rule);
}
.markdown-body h3 { font-size: 1.2em; }

.markdown-body p { margin: 1em 0; }

.markdown-body a { color: var(--accent-ink); }

.markdown-body code {
  font-family: var(--mono);
  font-size: 0.86em;
  background: var(--code-bg);
  padding: 0.15em 0.4em;
  border-radius: 4px;
}

.markdown-body pre {
  background: #1c1917;
  color: #f5f5f4;
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 1.2em 0;
}

.markdown-body pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: 0.88em;
  line-height: 1.55;
}

.markdown-body pre code.hljs {
  background: transparent;
  padding: 0;
}

.markdown-body blockquote {
  margin: 1em 0;
  padding: 0.2em 0 0.2em 1em;
  border-left: 3px solid var(--accent);
  color: var(--muted);
}

.markdown-body ul,
.markdown-body ol {
  margin: 1em 0;
  padding-left: 1.6em;
}

.markdown-body li { margin: 0.35em 0; }

.markdown-body hr {
  border: none;
  border-top: 1px solid var(--rule);
  margin: 2em 0;
}

.markdown-body table {
  display: block;
  width: max-content;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
  margin: 1.2em 0;
  font-size: 0.95em;
}

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--rule);
  padding: 8px 12px;
  text-align: left;
  vertical-align: top;
}

.markdown-body th {
  background: var(--sidebar);
  font-family: var(--ui);
  font-weight: 600;
}

.markdown-body img {
  max-width: 100%;
  height: auto;
}

.katex,
.katex-display {
  color: var(--ink);
}

.katex-display {
  margin: 1.2em 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0.2em 0;
}

.katex-error {
  color: var(--danger);
  font-family: var(--mono);
  font-size: 0.85em;
}

.backdrop {
  display: none;
}

@media (max-width: 800px) {
  .menu-btn { display: inline-flex; }
  .sidebar-toggle,
  .topbar-files { display: none; }
  html[data-sidebar="collapsed"] .sidebar,
  body.sidebar-collapsed .sidebar,
  .sidebar.is-collapsed {
    width: var(--sidebar-w);
    padding: 16px 12px 32px;
    overflow: auto;
    cursor: default;
  }
  html[data-sidebar="collapsed"] .sidebar-head-text,
  html[data-sidebar="collapsed"] .file-list,
  body.sidebar-collapsed .sidebar-head-text,
  body.sidebar-collapsed .file-list,
  .sidebar.is-collapsed .sidebar-head-text,
  .sidebar.is-collapsed .file-list {
    display: block;
  }
  html[data-sidebar="collapsed"] .folder-tools:not([hidden]),
  body.sidebar-collapsed .folder-tools:not([hidden]),
  .sidebar.is-collapsed .folder-tools:not([hidden]) {
    display: flex;
  }
  html[data-sidebar="collapsed"] .sidebar-head,
  body.sidebar-collapsed .sidebar-head,
  .sidebar.is-collapsed .sidebar-head {
    margin-bottom: 12px;
    justify-content: flex-start;
  }
  .sidebar {
    position: fixed;
    top: var(--bar-h);
    left: 0;
    bottom: 0;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  body.sidebar-open .sidebar { transform: none; }
  body.sidebar-open .backdrop {
    display: block;
    position: fixed;
    inset: var(--bar-h) 0 0 0;
    background: rgba(28, 25, 23, 0.35);
    z-index: 25;
  }
  .article { padding: 20px 18px 64px; }
  .crumb { display: none; }
  .seg button { padding: 6px 8px; font-size: 0.7rem; }
  .home-link { display: none; }
}

@media print {
  .topbar, .sidebar, .menu-btn, .status, .backdrop { display: none !important; }
  .article { overflow: visible; padding: 0; }
  body { height: auto; display: block; background: #fff; }
}
