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

/* ── Design tokens — light (default) ────────────────────────────────────── */
:root {
  /* Surface */
  --bg: #ffffff;
  --bg-sidebar: #fafafa;
  --bg-hover: #f4f4f5;
  --bg-active: #f0f0f0;
  --border: #e4e4e7;

  /* Text */
  --text: #09090b;
  --text-secondary: #52525b;
  --text-muted: #71717a;

  /* Sidebar nav */
  --nav-text: #52525b;
  --nav-hover: #09090b;
  --nav-active: #0070f3;
  --nav-active-bg: rgba(0, 112, 243, 0.08);
  --nav-section: #a1a1aa;

  /* Topbar */
  --topbar-bg: rgba(255, 255, 255, 0.85);
  --topbar-border: #e4e4e7;

  /* Inline code */
  --code-bg: #f4f4f5;
  --code-text: #09090b;

  /* Accent */
  --accent: #0070f3;
  --link: #0070f3;

  /* Blockquote */
  --bq-border: #e4e4e7;
  --bq-bg: #fafafa;

  /* Misc */
  --radius: 6px;
  --sidebar-w: 240px;
  --topbar-h: 60px;
  --content-max: 1280px;

  --font:
    "Geist", -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue",
    Arial, sans-serif;
  --font-mono:
    "Geist Mono", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    monospace;
}

/* ── Dark theme ──────────────────────────────────────────────────────────── */
[data-theme="dark"] {
  --bg: #0a0a0a;
  --bg-sidebar: #0a0a0a;
  --bg-hover: #111111;
  --bg-active: #1a1a1a;
  --border: #27272a;

  --text: #fafafa;
  --text-secondary: #a1a1aa;
  --text-muted: #52525b;

  --nav-text: #a1a1aa;
  --nav-hover: #fafafa;
  --nav-active: #60a5fa;
  --nav-active-bg: rgba(96, 165, 250, 0.1);
  --nav-section: #3f3f46;

  --topbar-bg: rgba(10, 10, 10, 0.85);
  --topbar-border: #27272a;

  --code-bg: #18181b;
  --code-text: #e4e4e7;

  --accent: #60a5fa;
  --link: #60a5fa;

  --bq-border: #3f3f46;
  --bq-bg: #111111;
}

/* System preference fallback (when JS hasn't run / no stored choice) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0a0a0a;
    --bg-sidebar: #0a0a0a;
    --bg-hover: #111111;
    --bg-active: #1a1a1a;
    --border: #27272a;
    --text: #fafafa;
    --text-secondary: #a1a1aa;
    --text-muted: #52525b;
    --nav-text: #a1a1aa;
    --nav-hover: #fafafa;
    --nav-active: #60a5fa;
    --nav-active-bg: rgba(96, 165, 250, 0.1);
    --nav-section: #3f3f46;
    --topbar-bg: rgba(10, 10, 10, 0.85);
    --topbar-border: #27272a;
    --code-bg: #18181b;
    --code-text: #e4e4e7;
    --accent: #60a5fa;
    --link: #60a5fa;
    --bq-border: #3f3f46;
    --bq-bg: #111111;
  }
}

/* ── Base ────────────────────────────────────────────────────────────────── */
html {
  color-scheme: light dark;
}
html,
body {
  height: 100%;
}
body {
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  transition:
    background 0.2s,
    color 0.2s;
}

/* ── App shell ───────────────────────────────────────────────────────────── */
.layout-app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Topbar ──────────────────────────────────────────────────────────────── */
.topbar {
  position: sticky;
  top: 0;
  z-index: 200;
  height: var(--topbar-h);
  background: var(--topbar-bg);
  backdrop-filter: saturate(180%) blur(12px);
  -webkit-backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--topbar-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  gap: 1rem;
  flex-shrink: 0;
}

/* ── Brand / Logo ────────────────────────────────────────────────────────── */
.brand-link {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
  color: var(--text);
}

.brand-logo {
  height: 24px;
  width: auto;
  display: block;
}

.brand-name {
  font-size: 0.9375rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}

.brand-sep {
  color: var(--border);
  font-weight: 300;
  font-size: 1.25rem;
  line-height: 1;
}

/* ── Topbar right controls ───────────────────────────────────────────────── */
.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.user-email {
  font-size: 0.8125rem;
  color: var(--text-muted);
  display: none; /* shown at wider viewport below */
}

@media (min-width: 900px) {
  .user-email {
    display: block;
  }
}

.btn-signout {
  font-size: 0.8125rem;
  color: var(--text-muted);
  text-decoration: none;
  padding: 0.3125rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition:
    color 0.15s,
    border-color 0.15s;
}
.btn-signout:hover {
  color: var(--text);
  border-color: var(--text-secondary);
}

/* ── Theme toggle ────────────────────────────────────────────────────────── */
.btn-theme {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  cursor: pointer;
  transition:
    color 0.15s,
    border-color 0.15s;
  flex-shrink: 0;
}
.btn-theme:hover {
  color: var(--text);
  border-color: var(--text-secondary);
}
.btn-theme svg {
  display: block;
}

/* ── Sidebar ─────────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
}

.sidebar-inner {
  padding: 1.25rem 0 3rem;
}

/* ── Nav tree ────────────────────────────────────────────────────────────── */
.nav-tree,
.nav-tree ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

/* Section (directory) */
/* Collapsed by default; .open class added server-side for active path */
.nav-section > ul { display: none; }
.nav-section.open > ul { display: block; }

/* Row wrapping the folder toggle button + (editors only) new-doc/new-folder icons */
.nav-folder-row { display: flex; align-items: center; }

/* Folder toggle button — resets browser defaults, looks like a label */
.nav-folder,
.nav-folder-sub {
  display: flex;
  align-items: center;
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  font-family: var(--font);
  user-select: none;
}

.nav-folder {
  padding: 0.5rem 0.75rem 0.25rem;
  /* padding-left overridden inline per depth */
  font-size: 0.6875rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--nav-section);
  margin-top: 1rem;
}
.nav-section:first-child > .nav-folder-row > .nav-folder { margin-top: 0; }

/* Nested directory label */
.nav-folder-sub {
  padding: 0.3rem 0.75rem 0.15rem;
  /* padding-left overridden inline per depth */
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--nav-text);
  margin-top: 0.5rem;
}

/* Chevron — CSS border trick, rotates when open */
.nav-chevron {
  flex-shrink: 0;
  margin-left: auto;
  margin-right: 2px;
  width: 5px;
  height: 5px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  transition: transform 0.15s ease;
  opacity: 0.6;
}
.nav-section.open > .nav-folder-row > .nav-folder > .nav-chevron,
.nav-section.open > .nav-folder-row > .nav-folder-sub > .nav-chevron {
  transform: rotate(45deg);
}

/* Leaf link — padding-left set inline by renderer based on depth */
.nav-item a {
  display: block;
  padding: 0.3125rem 1rem;
  font-size: 0.875rem;
  color: var(--nav-text);
  text-decoration: none;
  border-radius: 4px;
  margin: 0 0.375rem;
  transition:
    color 0.12s,
    background 0.12s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.nav-item a:hover {
  color: var(--nav-hover);
  background: var(--bg-hover);
}
.nav-item.active a {
  color: var(--nav-active);
  background: var(--nav-active-bg);
  font-weight: 500;
}

/* ── Content area ────────────────────────────────────────────────────────── */
.content {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--bg);
}

.content-inner {
  max-width: var(--content-max);
  margin: 0 auto;
  padding: 2.5rem 2.5rem 5rem;
}

/* ── Typography ──────────────────────────────────────────────────────────── */
.content-inner h1 {
  font-size: 2rem;
  font-weight: 700;
  line-height: 1.2;
  letter-spacing: -0.03em;
  color: var(--text);
  margin-bottom: 1rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid var(--border);
}
.content-inner h2 {
  font-size: 1.375rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text);
  margin: 2.5rem 0 0.75rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}
.content-inner h3 {
  font-size: 1.0625rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
  margin: 2rem 0 0.5rem;
}
.content-inner h4,
.content-inner h5,
.content-inner h6 {
  font-size: 0.9375rem;
  font-weight: 600;
  color: var(--text);
  margin: 1.5rem 0 0.375rem;
}

/* Heading anchor links */
.content-inner h1 a,
.content-inner h2 a,
.content-inner h3 a,
.content-inner h4 a {
  color: inherit;
  text-decoration: none;
}
.content-inner h2 a:hover::after,
.content-inner h3 a:hover::after {
  content: " #";
  color: var(--text-muted);
  font-weight: 400;
}

.content-inner p {
  color: var(--text-secondary);
  line-height: 1.75;
  margin-bottom: 1rem;
}

.content-inner a {
  color: var(--link);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color 0.15s;
}
.content-inner a:hover {
  border-bottom-color: var(--link);
}

/* Lists */
.content-inner ul,
.content-inner ol {
  padding-left: 1.5rem;
  margin-bottom: 1rem;
  color: var(--text-secondary);
}
.content-inner li {
  line-height: 1.75;
  margin-bottom: 0.375rem;
}
.content-inner li > ul,
.content-inner li > ol {
  margin-top: 0.375rem;
  margin-bottom: 0;
}

/* Tables */
.content-inner table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1.5rem;
  font-size: 0.875rem;
  display: block;
  overflow-x: auto;
}
.content-inner th {
  padding: 0.625rem 0.875rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.8125rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
}
.content-inner td {
  padding: 0.625rem 0.875rem;
  border-bottom: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: 0.9375rem;
}
.content-inner tr:last-child td {
  border-bottom: none;
}
.content-inner tr:hover td {
  background: var(--bg-hover);
}

/* ── Code ────────────────────────────────────────────────────────────────── */
/* Inline code */
.content-inner :not(pre) > code {
  font-family: var(--font-mono);
  font-size: 0.8125em;
  background: var(--code-bg);
  color: var(--code-text);
  border-radius: 4px;
  padding: 0.15em 0.4em;
  border: 1px solid var(--border);
}

/* Fenced code blocks — chroma handles colors; we handle the shell */
.content-inner pre {
  border-radius: 10px;
  margin: 1.25rem 0;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.06);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.content-inner pre code {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  line-height: 1.65;
  display: block;
  background: transparent;
  color: inherit;
  border: none;
  padding: 0;
}

/* chroma wrapper */
.content-inner .chroma {
  padding: 1.125rem 1.375rem;
  overflow-x: auto;
  border-radius: 10px;
  line-height: 1.65;
}

/* ── Blockquote ──────────────────────────────────────────────────────────── */
.content-inner blockquote {
  border-left: 2px solid var(--border);
  margin: 1.5rem 0;
  padding: 0.75rem 1.25rem;
  background: var(--bq-bg);
  border-radius: 0 var(--radius) var(--radius) 0;
}
.content-inner blockquote p {
  margin: 0;
  color: var(--text-secondary);
}

/* ── HR ──────────────────────────────────────────────────────────────────── */
.content-inner hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2.5rem 0;
}

/* ── Task lists ──────────────────────────────────────────────────────────── */
.content-inner input[type="checkbox"] {
  accent-color: var(--accent);
  cursor: default;
  margin-right: 0.375rem;
}

/* ── del / s ─────────────────────────────────────────────────────────────── */
.content-inner del,
.content-inner s {
  color: var(--text-muted);
}

/* ── PDF download button ──────────────────────────────────────────────────── */
.doc-actions {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 1.5rem;
}

.btn-pdf {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.3125rem 0.75rem;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-muted);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-family: var(--font);
  transition:
    color 0.15s,
    border-color 0.15s;
}
.btn-pdf:hover {
  color: var(--text);
  border-color: var(--text-secondary);
}
.btn-pdf svg {
  flex-shrink: 0;
}

/* ── Index page ──────────────────────────────────────────────────────────── */
.index-hero {
  margin-bottom: 2.5rem;
}
.index-hero h1 {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text);
  margin-bottom: 0.5rem;
  border-bottom: none;
  padding-bottom: 0;
}
.index-hero p {
  color: var(--text-muted);
  font-size: 0.9375rem;
}

/* Section grid */
.doc-tree {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.idx-section {
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}

/* Section header row — clearly a label, not a link */
.idx-section-hd {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 1rem;
  background: var(--bg-hover);
  border-bottom: 1px solid var(--border);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-muted);
  user-select: none;
}

/* Document link list */
.idx-links {
  list-style: none;
  padding: 0;
  margin: 0;
}

.idx-links li + li {
  border-top: 1px solid var(--border);
}

/* Each doc link — full-width row, clearly clickable */
.idx-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  text-decoration: none;
  background: var(--bg);
  color: var(--text);
  transition: background 0.12s;
  gap: 1rem;
  border-bottom: none; /* override global a:hover rule */
}
.idx-link:hover {
  background: var(--bg-hover);
  border-bottom: none;
}
.idx-link:hover .idx-link-title {
  color: var(--link);
}
.idx-link:hover .idx-arrow {
  transform: translateX(3px);
  color: var(--link);
}

.idx-link-title {
  font-size: 0.9375rem;
  color: var(--text-secondary);
  transition: color 0.12s;
}

.idx-arrow {
  flex-shrink: 0;
  color: var(--text-muted);
  transition:
    transform 0.12s,
    color 0.12s;
}

/* Sub-label inside a section (nested directory) */
.idx-sub-label {
  padding: 0.375rem 1rem;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  background: var(--bg-hover);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

/* Nested folder's own doc list, indented under its .idx-sub-label */
.idx-links-nested { padding-left: 0.75rem; }

/* ── Login / Auth pages ──────────────────────────────────────────────────── */
.layout-plain {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.auth-card {
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2.5rem 2.25rem;
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.auth-wordmark {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.625rem;
  margin-bottom: 0.375rem;
}

.auth-wordmark img {
  height: 28px;
  width: auto;
}

.auth-wordmark span {
  font-size: 1.0625rem;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}

.auth-subtitle {
  color: var(--text-muted);
  font-size: 0.875rem;
  line-height: 1.55;
  margin-bottom: 2rem;
}

.btn-google {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.75rem;
  width: 100%;
  padding: 0.625rem 1.25rem;
  font-size: 0.9375rem;
  font-weight: 500;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  transition:
    border-color 0.15s,
    box-shadow 0.15s;
  font-family: var(--font);
}
.btn-google:hover {
  border-color: var(--text-secondary);
  box-shadow: 0 0 0 3px var(--nav-active-bg);
  border-bottom: 1px solid var(--text-secondary);
}
.google-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

/* ── Denied page ──────────────────────────────────────────────────────────── */
.denied-card {
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2.5rem 2.25rem;
  width: 100%;
  max-width: 420px;
  text-align: center;
}

.denied-icon {
  font-size: 2.5rem;
  margin-bottom: 1rem;
  display: block;
}

.denied-card h1 {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.75rem;
}

.denied-card p {
  color: var(--text-muted);
  font-size: 0.9375rem;
  line-height: 1.6;
  margin-bottom: 0.75rem;
}

.denied-email {
  font-family: var(--font-mono);
  font-size: 0.8125rem;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.2em 0.5em;
  color: var(--text-secondary);
  word-break: break-all;
}

.btn-retry {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  margin-top: 1.5rem;
  color: var(--link);
  font-size: 0.9375rem;
  text-decoration: none;
  font-weight: 500;
  border-bottom: 1px solid transparent;
  transition: border-color 0.15s;
}
.btn-retry:hover {
  border-bottom-color: var(--link);
}

/* ── Responsive ──────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar {
    display: none;
  }
  .content-inner {
    padding: 1.5rem 1.25rem 4rem;
  }
  .user-email {
    display: none;
  }
}

/* ── Mermaid / C4 diagrams ───────────────────────────────────────────────── */
.content-inner .mermaid {
  display: flex;
  justify-content: center;
  margin: 1.75rem 0;
  overflow-x: auto;
  border-radius: var(--radius);
}
.content-inner .mermaid svg {
  display: block;
  /* No fixed width/height — viewBox drives the aspect ratio.
     min-height forces vertical room; width scales proportionally. */
  width: auto;
  height: auto;
  min-height: 820px;
  max-width: 100%;
}

/* ── Mermaid label text contrast fix ─────────────────────────────────────────
   classDef `color:` does not inherit into foreignObject HTML content across
   browsers (SVG and HTML have separate CSS inheritance contexts). All our
   classDef fills are dark, so forcing light text here is always correct.
   The cluster-label fix covers subgraph titles painted over by nested rects. */
.content-inner .mermaid .nodeLabel,
.content-inner .mermaid .nodeLabel p,
.content-inner .mermaid .nodeLabel span {
  color: #f8f8f2 !important;
}
/* SVG text-based labels (non-foreignObject path) */
.content-inner .mermaid .label text,
.content-inner .mermaid text.nodeLabel {
  fill: #f8f8f2 !important;
}
/* Edge labels */
.content-inner .mermaid .edgeLabel .label span,
.content-inner .mermaid .edgeLabel p {
  color: var(--text) !important;
  background: var(--bg) !important;
}

/* ── Print / PDF ──────────────────────────────────────────────────────────── */
@media print {
  .topbar,
  .sidebar,
  .doc-actions {
    display: none !important;
  }
  .layout-app {
    display: block;
    height: auto;
    overflow: visible;
  }
  .app-body {
    display: block;
    overflow: visible;
  }
  .content {
    overflow: visible;
    background: #fff;
  }
  .content-inner {
    max-width: 100%;
    padding: 0;
    margin: 0;
  }

  body {
    font-size: 11pt;
    color: #000;
    background: #fff;
  }
  .content-inner h1 {
    font-size: 20pt;
    border-color: #ccc;
  }
  .content-inner h2 {
    font-size: 14pt;
    border-color: #ccc;
    page-break-after: avoid;
  }
  .content-inner h3 {
    font-size: 12pt;
    page-break-after: avoid;
  }
  .content-inner p {
    color: #333;
  }
  pre,
  blockquote,
  table {
    page-break-inside: avoid;
  }

  .content-inner a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #555;
  }
  .content-inner a[href^="#"]::after {
    content: "";
  }

  .content-inner pre,
  .content-inner .chroma {
    background: #f5f5f5 !important;
    border: 1px solid #ccc !important;
    color: #000 !important;
    box-shadow: none !important;
  }
  .content-inner .chroma * {
    color: #000 !important;
    background: transparent !important;
  }
}
