/* Caipi app design system: custom, no framework. Pairs with the landing.
   Fonts (loaded in base.html): JetBrains Mono only.
   JetBrains Mono = UI chrome, labels, code. System sans = document prose. */

/* Product skin. The reset, shared tokens, body base, and the button + url-field
   primitives live in tokens.css (loaded first). This file adds the product-only
   tokens and the app component library: nav, sidebar, tree, prose, forms,
   db viewer, plus the meter / badge / confirm-dialog / error-card components. */
:root {
  --surface: #141417;        /* sidebar, cards, code */
  --surface-2: #1c1c20;      /* hover / raised */
  --danger: #e5484d;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --nav-h: 3.25rem;
  --side-w: 16rem;
}
:root[data-theme="light"] {
  --surface: #ffffff;
  --surface-2: #f0f0f1;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --surface: #ffffff;
    --surface-2: #f0f0f1;
  }
}

code { font-family: var(--mono); font-size: 0.88em; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 0.05em 0.32em; }

/* ---------- shared bits ---------- */
.label {
  font-size: 0.7rem; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--dimmer);
}
.muted { color: var(--dim); }

button, input, select, textarea { font: inherit; color: inherit; }

/* Base .btn / .btn-accent / .btn-ghost / .btn-sm / .btn-icon live in tokens.css.
   These danger variants use the product-only --danger token, so they live here. */
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); font-weight: 600; }
.btn-danger:hover { filter: brightness(1.08); border-color: var(--danger); }
.btn-danger-ghost { color: var(--danger); border-color: transparent; }
.btn-danger-ghost:hover { border-color: var(--danger); color: var(--danger); }

/* ---------- top nav ---------- */
.topnav {
  position: sticky; top: 0; z-index: 20;
  height: var(--nav-h);
  display: flex; align-items: center; gap: 0.9rem;
  padding: 0 1rem;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.topnav .brand {
  font-family: var(--mono); font-weight: 600; font-size: 0.95rem; letter-spacing: 0;
  text-decoration: none; color: var(--ink); white-space: nowrap;
}
.topnav .brand .slash { color: var(--dimmer); }
.topnav .spacer { flex: 1; }
.topnav .actions { display: flex; align-items: center; gap: 0.5rem; }

.hamburger {
  display: inline-grid; place-items: center;
  width: 2rem; height: 2rem; flex: none;
  background: none; border: 1px solid transparent; border-radius: var(--radius);
  color: var(--dim); cursor: pointer; transition: color .15s, border-color .15s;
}
.hamburger:hover { color: var(--ink); border-color: var(--line); }

/* ---------- layout: sidebar + main ---------- */
/* App layout: the page itself never scrolls. The shell fills the viewport
   below the nav, and the sidebar and main pane are independent scroll
   containers — a long tree scrolls inside the sidebar, a long document
   scrolls inside the pane, neither moves the other. */
.shell { display: flex; align-items: stretch; height: calc(100dvh - var(--nav-h)); }
.sidebar {
  flex: none; width: var(--side-w);
  border-right: 1px solid var(--line);
  background: var(--bg);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.1rem 0.9rem;
  transition: margin-left .2s ease;
}
/* When the sidebar carries a pinned footer (the owner shell), it becomes a
   flex column: the file tree scrolls, the Settings button stays at the bottom. */
.sidebar:has(.side-scroll) { display: flex; flex-direction: column; overflow: hidden; }
.side-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.shell.collapsed .sidebar { margin-left: calc(-1 * var(--side-w)); }
.sidebar .side-label { margin: 0 0.4rem 0.7rem; }
.side-settings {
  display: flex; align-items: center; gap: 0.55rem;
  margin-top: 0.6rem; padding: 0.5rem 0.55rem;
  font-family: var(--mono); font-size: 0.85rem; letter-spacing: 0.02em;
  color: var(--dim); text-decoration: none;
  border-top: 1px solid var(--line); border-radius: var(--radius);
  transition: color .15s, background .15s;
}
.side-settings svg { flex: none; color: var(--dimmer); }
.side-settings:hover { color: var(--ink); background: var(--surface-2); }
.side-settings[aria-current="page"] { color: var(--ink); background: var(--surface-2); box-shadow: inset 2px 0 0 var(--accent); }
.side-settings[aria-current="page"] svg { color: var(--accent); }
.main { flex: 1; min-width: 0; overflow-y: auto; }

/* ---------- file tree: plain nested lists, no accordions ---------- */
.tree, .tree ul { list-style: none; }
.tree ul { padding-left: 0.85rem; border-left: 1px solid var(--line); margin-left: 0.45rem; }
.tree li { margin: 0.05rem 0; }
.tree details > summary {
  display: flex; align-items: center; gap: 0.35rem;
  padding: 0.2rem 0.4rem; font-size: 0.78rem; letter-spacing: 0.02em;
  color: var(--dim); cursor: pointer; list-style: none; user-select: none;
}
.tree details > summary::-webkit-details-marker { display: none; }
.tree details > summary::before {
  content: ""; flex: none;
  width: 0; height: 0;
  border-left: 6px solid var(--dimmer);
  border-top: 5px solid transparent;
  border-bottom: 5px solid transparent;
  transform-origin: center;
  transition: transform .15s ease;
}
.tree details[open] > summary::before { transform: rotate(90deg); }
.tree details > summary:hover { color: var(--ink); }
.tree a {
  display: block; padding: 0.2rem 0.4rem; border-radius: var(--radius);
  font-size: 0.82rem; color: var(--dim); text-decoration: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  transition: color .12s, background .12s;
}
.tree a:hover { color: var(--ink); background: var(--surface-2); }
.tree a[aria-current="page"] { color: var(--ink); background: var(--surface-2); box-shadow: inset 2px 0 0 var(--accent); }

/* ---------- document / prose (neutral, agent docs bring their own styling) ---------- */
.doc { max-width: 48rem; margin: 0 auto; padding: 2.5rem 1.5rem 5rem; }
/* neutral defaults only — agent-authored documents bring their own styling */
.prose { font-family: var(--sans); font-size: 1rem; line-height: 1.65; color: var(--ink); }
.prose > * + * { margin-top: 1rem; }
.prose h1, .prose h2, .prose h3, .prose h4 {
  font-family: var(--sans); font-weight: 600; line-height: 1.25; letter-spacing: -0.01em;
  margin-top: 2rem;
}
.prose h1 { font-size: 1.9rem; }
.prose h2 { font-size: 1.4rem; }
.prose h3 { font-size: 1.15rem; }
.prose h1:first-child, .prose h2:first-child { margin-top: 0; }
.prose a { color: var(--ink); text-decoration: underline; text-decoration-color: var(--dim); text-underline-offset: 2px; }
.prose a:hover { text-decoration-color: var(--ink); }
.prose strong { font-weight: 600; }
.prose ul, .prose ol { padding-left: 1.3rem; }
.prose li { margin-top: 0.35rem; }
.prose blockquote { border-left: 3px solid var(--line); padding-left: 1rem; color: var(--dim); }
.prose hr { border: 0; border-top: 1px solid var(--line); margin: 2rem 0; }
.prose img { max-width: 100%; height: auto; border-radius: var(--radius); }
.prose code {
  font-family: var(--mono); font-size: 0.86em;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 0.1em 0.35em;
}
.prose pre {
  font-family: var(--mono); font-size: 0.85rem; line-height: 1.55;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem 1.1rem; overflow-x: auto;
}
.prose pre code { background: none; border: 0; padding: 0; font-size: inherit; }
.prose table { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 0.85rem; }
.prose th, .prose td { border: 1px solid var(--line); padding: 0.5rem 0.7rem; text-align: left; }
.prose th { color: var(--dim); font-weight: 500; background: var(--surface); }

/* ---------- auth / centered pages ---------- */
.center-wrap {
  min-height: 100dvh; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 1.5rem;
  padding: 2rem 1.5rem; text-align: center;
}
.center-wrap .brand { font-family: var(--mono); font-weight: 600; font-size: 1.3rem; text-decoration: none; }
.center-wrap .brand .dot { color: var(--accent); }
.card {
  width: 100%; max-width: 26rem; text-align: left;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); padding: 1.6rem;
}
.card h1 { font-family: var(--mono); font-weight: 600; font-size: 1.25rem; margin-bottom: 0.4rem; }
.card p { color: var(--dim); font-size: 0.92rem; margin-bottom: 1.2rem; }
.auth-consent { max-width: 34rem; padding: 1.75rem; }
.auth-consent .label { margin-bottom: 0.65rem; color: var(--accent); }
.auth-lede { margin-bottom: 1.35rem; }
.consent-list {
  display: grid; gap: 0;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  margin-bottom: 1rem;
}
.consent-list > div {
  display: grid; grid-template-columns: 7.5rem 1fr; gap: 1rem;
  padding: 0.85rem 0;
}
.consent-list > div + div { border-top: 1px solid var(--line); }
.consent-list dt {
  color: var(--ink); font-size: 0.82rem; font-weight: 600;
}
.consent-list dd {
  color: var(--dim); font-size: 0.84rem; line-height: 1.55;
}
.consent-note {
  border-left: 2px solid var(--accent);
  padding-left: 0.85rem;
}
.card .consent-client {
  font-size: 0.78rem; margin-bottom: 1rem;
}
.consent-actions { margin-top: 0.25rem; }
@media (max-width: 520px) {
  .auth-consent { padding: 1.35rem; }
  .consent-list > div { grid-template-columns: 1fr; gap: 0.25rem; }
}

/* The .urlfield primitive lives in tokens.css (shared with the landing). */

/* ---------- theme toggle (lightbulb) ---------- */
.theme {
  display: inline-grid; place-items: center;
  width: 2rem; height: 2rem; flex: none;
  background: none; border: 1px solid transparent; border-radius: var(--radius);
  color: var(--dim); cursor: pointer; transition: color .2s, border-color .15s;
}
.theme:hover { color: var(--ink); border-color: var(--line); }
.theme svg { width: 17px; height: 17px; display: block; }
.theme.fixed { position: fixed; top: 1.25rem; right: 1.25rem; z-index: 30; }

/* ---------- forms ---------- */
.field { display: grid; gap: 0.5rem; margin-bottom: 1.1rem; }
.field > label { font-size: 0.78rem; color: var(--dim); letter-spacing: 0.02em; }
/* :where() keeps zero specificity so .urlfield input (underline style) still wins */
:where(input[type="text"], input[type="file"], textarea) {
  width: 100%; background: var(--surface); color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 0.55rem 0.7rem; font-family: var(--mono); font-size: 0.9rem;
}
:where(input[type="text"], textarea):focus { outline: none; border-color: var(--accent); }
input[type="file"] { cursor: pointer; padding: 0.4rem; font-size: 0.85rem; color: var(--dim); }
input[type="file"]::file-selector-button {
  font-family: var(--mono); font-size: 0.78rem; letter-spacing: 0.02em;
  background: none; color: var(--ink);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 0.35rem 0.85rem; margin-right: 0.8rem; cursor: pointer;
  transition: border-color .15s, color .15s;
}
input[type="file"]::file-selector-button:hover { border-color: var(--dim); }
.hint { list-style: none; font-size: 0.78rem; color: var(--dim); }
.hint li { margin-top: 0.25rem; }

/* alert */
.alert {
  border: 1px solid color-mix(in srgb, var(--danger) 45%, var(--line));
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  color: var(--danger); padding: 0.7rem 0.9rem; border-radius: var(--radius); font-size: 0.85rem;
}

/* google sign-in slot */
.gsi { display: flex; justify-content: center; min-height: 44px; }
.gsi:empty { display: none; }

/* utilities */
.stack { display: grid; gap: 1rem; }
.row { display: flex; gap: 0.6rem; align-items: center; }

/* public page (no app chrome) */
.public { max-width: 48rem; margin: 0 auto; padding: 3rem 1.5rem 5rem; }
.sitemark { margin-top: 4rem; padding-top: 1.2rem; border-top: 1px solid var(--line); text-align: center; font-size: 0.8rem; }
.sitemark a { color: var(--dim); text-decoration: none; }
.sitemark a:hover { color: var(--ink); }

/* upload result list */
.upload-list { list-style: none; }
.upload-list li { padding: 0.35rem 0; border-bottom: 1px solid var(--line); font-size: 0.85rem; }

/* ---------- responsive ---------- */
@media (max-width: 720px) {
  :root { --side-w: 15rem; }
  /* off-canvas: hidden by default, opens via .shell.open */
  .sidebar {
    position: fixed; top: var(--nav-h); bottom: 0; left: 0; z-index: 15;
    margin-left: calc(-1 * var(--side-w));
  }
  .shell.open .sidebar { margin-left: 0; box-shadow: 0 0 0 100vmax rgba(0,0,0,.4); }
  .doc { padding: 2rem 1.2rem 4rem; }
}

/* --- sqlite viewer ------------------------------------------------------ */
/* The DB viewer wants room to show many columns at a glance. Widen only the
   container that holds it, so normal prose keeps its 48rem reading width.
   max-width grows to ~90rem but never forces page-level horizontal scroll;
   the inner .db-grid-wrap keeps its own scroll for wider tables. */
.doc:has(.dbviewer), .public:has(.dbviewer) { max-width: 90rem; }
.dbviewer { display: flex; flex-direction: column; gap: 0.9rem; }

/* Tab bar: a labeled, segmented strip so even one table reads as a tab. */
.db-tabbar {
  display: flex; align-items: stretch; flex-wrap: wrap; gap: 0.5rem;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); padding: 0.3rem;
}
.db-tabbar-label {
  display: inline-flex; align-items: center;
  font-family: var(--mono); font-size: 0.62rem; letter-spacing: 0.12em;
  text-transform: uppercase; color: var(--dimmer);
  padding: 0 0.4rem 0 0.5rem; margin-right: 0.2rem;
  border-right: 1px solid var(--line);
}
.db-tabs { display: flex; flex-wrap: wrap; gap: 0.3rem; align-items: center; }
.db-tab {
  font-family: var(--mono); font-size: 0.78rem; cursor: pointer; line-height: 1.4;
  padding: 0.3rem 0.7rem; border: 1px solid transparent; border-radius: var(--radius);
  background: none; color: var(--dim);
  transition: color .12s, background .12s, border-color .12s;
}
.db-tab:hover { color: var(--ink); background: var(--surface-2); }
.db-tab.active {
  background: var(--accent); color: var(--accent-ink); border-color: var(--accent);
  font-weight: 600;
}
.db-empty { font-family: var(--mono); font-size: 0.78rem; color: var(--dim); padding: 0.3rem 0.4rem; }

/* Data grid */
.db-grid-wrap {
  overflow: auto; max-height: 70vh;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface);
}
.db-grid { border-collapse: separate; border-spacing: 0; width: 100%; font-size: 0.8rem; }
.db-grid th, .db-grid td {
  text-align: left; vertical-align: top; padding: 0;
  border-bottom: 1px solid var(--line); border-right: 1px solid var(--line);
}
.db-cell-inner {
  padding: 0.45rem 0.75rem; max-width: 28rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.db-grid th {
  position: sticky; top: 0; z-index: 1;
  background: var(--surface-2); color: var(--ink);
  font-weight: 600; letter-spacing: 0.01em; white-space: nowrap;
  box-shadow: inset 0 -1px 0 var(--line);
}
.db-grid tbody tr:nth-child(even) td { background: color-mix(in srgb, var(--surface-2) 45%, var(--surface)); }
.db-grid tbody tr:hover td { background: var(--surface-2); }
.db-grid tr:last-child td { border-bottom: none; }
.db-grid th:last-child, .db-grid td:last-child { border-right: none; }
.db-cell-num .db-cell-inner { font-variant-numeric: tabular-nums; text-align: right; }
.db-norows { padding: 0.9rem 0.75rem; color: var(--dim); font-style: italic; }
.db-null { color: var(--dimmer); font-style: italic; font-size: 0.72rem; letter-spacing: 0.04em; }

/* Pager */
.db-pager { display: flex; align-items: center; justify-content: flex-end; gap: 0.6rem; font-size: 0.78rem; }
.db-range { color: var(--dim); margin: 0 auto 0 0; font-family: var(--mono); }
.db-range strong { color: var(--ink); font-weight: 600; }
.db-pgbtn { padding: 0.35rem 0.85rem; font-size: 0.75rem; }
.db-pgbtn:disabled { opacity: 0.4; cursor: default; }
.db-pgbtn:disabled:hover { border-color: var(--line); }

/* ---------- meter (usage bars) ---------- */
.meter { margin-bottom: 1rem; }
.meter-head { display: flex; justify-content: space-between; font-size: 0.85rem; margin-bottom: 0.4rem; }
.meter-val { color: var(--dim); }
.bar { height: 6px; background: var(--surface-2); border-radius: var(--radius-pill); overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--accent); border-radius: var(--radius-pill); }

/* ---------- badge ---------- */
.badge {
  font-size: 0.65rem; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--accent-ink); background: var(--accent);
  border-radius: var(--radius-pill); padding: 0.15rem 0.55rem; font-weight: 600;
}

/* ---------- confirm dialog (modal) ---------- */
.confirm {
  width: min(26rem, calc(100vw - 2rem)); border: 1px solid var(--line);
  border-radius: var(--radius); background: var(--surface); color: var(--ink);
  padding: 1.4rem; margin: auto;
}
.confirm::backdrop { background: rgba(0,0,0,.55); }
.confirm h3 { font-family: var(--mono); font-weight: 600; font-size: 1.05rem; margin-bottom: 0.6rem; }
.confirm p { color: var(--dim); font-size: 0.88rem; margin-bottom: 0.5rem; }
.confirm-strong p:first-of-type { color: var(--ink); }
.confirm-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 1.1rem; }

/* ---------- error card (paired with .center-wrap + .card) ---------- */
.error-card { max-width: 24rem; text-align: center; }
.error-card .error-code {
  font-family: var(--mono); font-weight: 600; font-size: 3rem;
  color: var(--accent); line-height: 1; margin-bottom: 0.5rem;
}
.error-card h1 { font-size: 1.05rem; color: var(--ink); margin-bottom: 1.4rem; }
.error-actions { display: flex; gap: 0.6rem; justify-content: center; }
