:root {
  --bg: #f7f6f3;
  --surface: #ffffff;
  --text: #1c1b19;
  --muted: #6b6760;
  --border: #e4e1da;
  --accent: #c2410c;
  --accent-soft: #fdeee5;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 16px rgb(0 0 0 / 0.04);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141312;
    --surface: #1d1c1a;
    --text: #ecebe8;
    --muted: #a19d95;
    --border: #2f2d2a;
    --accent: #fb923c;
    --accent-soft: #3a2316;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.55 var(--font); }
a { color: inherit; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.button {
  display: inline-flex; align-items: center; gap: 6px; flex-shrink: 0;
  padding: 7px 12px; border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); color: var(--text); font-size: 14px; text-decoration: none; white-space: nowrap;
}
.button:hover { border-color: var(--accent); color: var(--accent); }

.badge {
  display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600;
  background: var(--accent-soft); color: var(--accent);
}
.badge-markdown { background: #e0ecff; color: #1d4ed8; }
.badge-image { background: #dcfce7; color: #15803d; }
.badge-mermaid { background: #f3e8ff; color: #7e22ce; }
.badge-pdf { background: #fee2e2; color: #b91c1c; }
.badge-excalidraw { background: #e0f2fe; color: #0369a1; }
@media (prefers-color-scheme: dark) {
  .badge-markdown { background: #172554; color: #93c5fd; }
  .badge-image { background: #052e16; color: #86efac; }
  .badge-mermaid { background: #2e1065; color: #d8b4fe; }
  .badge-pdf { background: #450a0a; color: #fca5a5; }
  .badge-excalidraw { background: #082f49; color: #7dd3fc; }
}

.tags { display: flex; flex-wrap: wrap; gap: 4px; margin: 0; padding: 0; list-style: none; }
.tags li { padding: 0 7px; border: 1px solid var(--border); border-radius: 6px; font-size: 12px; color: var(--muted); }
.byline { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: 0; color: var(--muted); font-size: 13px; }

/* Catalog */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px; padding: 12px max(16px, calc((100% - 1200px) / 2));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; text-decoration: none; white-space: nowrap; }
.search { flex: 1; }
.search input {
  width: 100%; padding: 10px 14px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text); font: inherit; font-size: 16px;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.chips {
  display: flex; gap: 8px; overflow-x: auto; padding: 14px max(16px, calc((100% - 1200px) / 2)) 4px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex-shrink: 0; padding: 6px 12px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); color: var(--text); font: inherit; font-size: 14px; cursor: pointer;
}
.chip span { color: var(--muted); margin-left: 2px; }
.chip[aria-pressed="true"] { background: var(--text); border-color: var(--text); color: var(--bg); }
.chip[aria-pressed="true"] span { color: inherit; opacity: 0.7; }

.catalog { max-width: 1200px; margin: 0 auto; padding: 8px 16px 64px; }
.group { margin-top: 28px; scroll-margin-top: 80px; }
.group > header h2 { margin: 0; font-size: 20px; }
.group > header .count { color: var(--muted); font-weight: 400; font-size: 15px; }
.group > header p { margin: 2px 0 0; color: var(--muted); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; margin: 14px 0 0; padding: 0; list-style: none; }
.card a {
  display: flex; flex-direction: column; gap: 8px; height: 100%; padding: 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  text-decoration: none; transition: border-color 0.15s, transform 0.15s;
}
.card a:hover { border-color: var(--accent); transform: translateY(-1px); }
.card h3 { margin: 0; font-size: 16px; line-height: 1.35; }
.card .desc { margin: 0; color: var(--muted); font-size: 14px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.card .byline { margin-top: auto; }
.empty { margin: 48px 0; text-align: center; color: var(--muted); }

/* Viewer */
.viewbar {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.back { flex-shrink: 0; padding: 6px 8px; border-radius: 8px; text-decoration: none; font-weight: 600; }
.back:hover { background: var(--accent-soft); color: var(--accent); }
.viewbar-title { flex: 1; min-width: 0; }
.viewbar h1 { margin: 0; font-size: 16px; line-height: 1.3; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewbar .byline a { color: var(--muted); }

.view-frame { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.frame { flex: 1; width: 100%; border: 0; background: #fff; }

.doc { max-width: 960px; margin: 24px auto; padding: 0 16px 64px; }
.doc.image { max-width: none; text-align: center; }
.doc.image img { max-width: 100%; height: auto; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; }
.doc.diagram { max-width: none; overflow-x: auto; }
.doc.diagram .mermaid { display: flex; justify-content: center; }
.notice { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 20px; margin-top: 32px; max-width: 640px; }

.markdown { font-size: 16px; line-height: 1.7; }
.markdown h1, .markdown h2, .markdown h3 { line-height: 1.3; margin: 1.6em 0 0.5em; }
.markdown h1 { font-size: 28px; margin-top: 0; }
.markdown h2 { font-size: 22px; padding-bottom: 4px; border-bottom: 1px solid var(--border); }
.markdown a { color: var(--accent); }
.markdown img { max-width: 100%; height: auto; }
.markdown code { font-family: var(--mono); font-size: 0.9em; padding: 1px 5px; border-radius: 5px; background: var(--accent-soft); }
.markdown pre { overflow-x: auto; padding: 14px 16px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.markdown pre code { padding: 0; background: none; }
.markdown pre.mermaid { display: flex; justify-content: center; background: var(--surface); }
.markdown table { display: block; overflow-x: auto; border-collapse: collapse; }
.markdown th, .markdown td { padding: 6px 12px; border: 1px solid var(--border); text-align: left; }
.markdown th { background: var(--surface); }
.markdown blockquote { margin: 1em 0; padding: 4px 16px; border-left: 3px solid var(--accent); color: var(--muted); }

@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; gap: 10px; }
  .search { flex-basis: 100%; }
  .grid { grid-template-columns: 1fr; }
  .back span { display: none; }
  .viewbar { gap: 8px; padding: 8px 10px; }
  .viewbar .button { padding: 6px 10px; font-size: 13px; }
  .doc { margin-top: 16px; }
}
