/* ==========================================================================
   Tex — design tokens
   Light is the default; dark follows the system unless the viewer picks a
   theme, which sets data-theme on <html>.
   ========================================================================== */
:root {
  --bg: #f5f4f0;
  --panel: #ffffff;
  --panel-2: #faf9f6;
  --sunken: #efede7;
  --hover: #f0eee8;
  --ink: #1b1d1a;
  --ink-2: #454a44;
  --muted: #72786f;
  --line: #e4e1da;
  --line-strong: #d3cfc5;
  --accent: #1f6f4a;
  --accent-hover: #185c3c;
  --accent-ink: #ffffff;
  --accent-soft: #e2f0e8;
  --accent-text: #1a6843;
  --warn: #9a6200;
  --warn-soft: #fbf0d8;
  --danger: #b42318;
  --danger-soft: #fdebe9;
  --pdf-bg: #e8e6df;
  --editor-bg: #ffffff;
  --shadow-sm: 0 1px 2px rgba(27, 29, 26, .06);
  --shadow-md: 0 4px 16px rgba(27, 29, 26, .08), 0 1px 3px rgba(27, 29, 26, .06);
  --shadow-lg: 0 24px 64px rgba(27, 29, 26, .18), 0 2px 8px rgba(27, 29, 26, .08);
  --focus: 0 0 0 3px color-mix(in srgb, var(--accent) 28%, transparent);
  --radius: 8px;
  --radius-lg: 12px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e110f;
    --panel: #151916;
    --panel-2: #191e1a;
    --sunken: #101311;
    --hover: #1e2420;
    --ink: #eaeee9;
    --ink-2: #c2c9c1;
    --muted: #8b958c;
    --line: #242b26;
    --line-strong: #353e37;
    --accent: #6fcf97;
    --accent-hover: #8edcae;
    --accent-ink: #0a1d12;
    --accent-soft: #1a3024;
    --accent-text: #80d8a5;
    --warn: #e5b45e;
    --warn-soft: #2e2616;
    --danger: #ff8a7f;
    --danger-soft: #381d1b;
    --pdf-bg: #0b0e0c;
    --editor-bg: #121613;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-md: 0 6px 20px rgba(0, 0, 0, .4);
    --shadow-lg: 0 24px 64px rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0e110f;
  --panel: #151916;
  --panel-2: #191e1a;
  --sunken: #101311;
  --hover: #1e2420;
  --ink: #eaeee9;
  --ink-2: #c2c9c1;
  --muted: #8b958c;
  --line: #242b26;
  --line-strong: #353e37;
  --accent: #6fcf97;
  --accent-hover: #8edcae;
  --accent-ink: #0a1d12;
  --accent-soft: #1a3024;
  --accent-text: #80d8a5;
  --warn: #e5b45e;
  --warn-soft: #2e2616;
  --danger: #ff8a7f;
  --danger-soft: #381d1b;
  --pdf-bg: #0b0e0c;
  --editor-bg: #121613;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-md: 0 6px 20px rgba(0, 0, 0, .4);
  --shadow-lg: 0 24px 64px rgba(0, 0, 0, .6);
  color-scheme: dark;
}

/* ==========================================================================
   Base
   ========================================================================== */
*, *::before, *::after { box-sizing: border-box; }
html { background: var(--bg); -webkit-text-size-adjust: 100%; }
body { margin: 0; min-width: 320px; color: var(--ink); background: var(--bg); font-family: var(--font); font-size: 14px; line-height: 1.5; -webkit-font-smoothing: antialiased; }
a { color: var(--accent-text); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; }
:focus-visible { outline: none; box-shadow: var(--focus); }
[hidden] { display: none !important; }
code, kbd, pre { font-family: var(--mono); }
code { font-size: .92em; }
kbd { display: inline-grid; min-width: 18px; height: 18px; place-items: center; padding: 0 4px; border: 1px solid var(--line-strong); border-bottom-width: 2px; border-radius: 4px; color: var(--ink-2); background: var(--panel); font-size: 10.5px; font-weight: 500; line-height: 1; }
kbd + kbd { margin-left: 2px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
.icon-sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
svg { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.wordmark { color: var(--ink); font-size: 20px; font-weight: 700; letter-spacing: -.06em; text-decoration: none; }
.wordmark span { color: var(--accent); }

/* ==========================================================================
   Controls
   ========================================================================== */
.btn { display: inline-flex; height: 32px; align-items: center; justify-content: center; gap: 7px; border: 1px solid var(--line-strong); border-radius: var(--radius); padding: 0 12px; color: var(--ink); background: var(--panel); box-shadow: var(--shadow-sm); font-size: 13px; font-weight: 550; line-height: 1; text-decoration: none; white-space: nowrap; transition: background .12s, border-color .12s, color .12s; }
.btn:hover { background: var(--hover); }
.btn.primary { border-color: transparent; color: var(--accent-ink); background: var(--accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { border-color: transparent; color: #fff; background: var(--danger); }
:root[data-theme="dark"] .btn.danger { color: #2a0d0a; }
.btn.ghost { border-color: transparent; background: transparent; box-shadow: none; color: var(--ink-2); }
.btn.ghost:hover { color: var(--ink); background: var(--hover); }
.btn.small { height: 26px; padding: 0 9px; font-size: 12px; }
.btn.large { height: 42px; padding: 0 18px; font-size: 14px; }
.btn.grow { flex: 1; }
.btn:disabled { opacity: .55; }
.btn svg { width: 15px; height: 15px; }
.btn-kbd { height: 17px; margin-left: 2px; border: 0; background: color-mix(in srgb, var(--accent-ink) 18%, transparent); color: inherit; opacity: .85; }
.btn .when-busy { display: none; }
.btn.busy .when-busy { display: inline-block; }
.btn.busy .when-idle { display: none; }

.icon-btn { display: inline-grid; width: 32px; height: 32px; flex: none; place-items: center; border: 1px solid transparent; border-radius: var(--radius); padding: 0; color: var(--ink-2); background: transparent; text-decoration: none; transition: background .12s, color .12s; }
.icon-btn:hover { color: var(--ink); background: var(--hover); }
.icon-btn.outlined { border-color: var(--line-strong); background: var(--panel); box-shadow: var(--shadow-sm); }
.icon-btn.outlined:hover { background: var(--hover); }
.icon-btn.small { width: 26px; height: 26px; }
.icon-btn.small svg { width: 14px; height: 14px; }

.segmented { display: inline-flex; gap: 2px; border: 1px solid var(--line); border-radius: var(--radius); padding: 2px; background: var(--sunken); }
.segmented button { height: 24px; border: 0; border-radius: 6px; padding: 0 10px; color: var(--muted); background: transparent; font-size: 12px; font-weight: 550; }
.segmented button:hover { color: var(--ink); }
.segmented button[aria-checked="true"], .segmented button[aria-selected="true"] { color: var(--ink); background: var(--panel); box-shadow: var(--shadow-sm); }
.segmented.wide { display: flex; }
.segmented.wide button { flex: 1; height: 30px; font-size: 13px; }

input:not([type="checkbox"]):not([type="radio"]), select, textarea { border: 1px solid var(--line-strong); border-radius: var(--radius); background: var(--panel); outline: none; transition: border-color .12s, box-shadow .12s; }
input:not([type="checkbox"]):not([type="radio"]):focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: var(--focus); }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: .85; }
input[type="checkbox"] { accent-color: var(--accent); }

.spinner { display: inline-block; width: 14px; height: 14px; flex: none; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; opacity: .8; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2s; } * { transition: none !important; } }

/* ==========================================================================
   App shell
   ========================================================================== */
.app-body { height: 100dvh; overflow: hidden; }
.app { --sidebar-w: 268px; display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); grid-template-rows: 48px minmax(0, 1fr); height: 100dvh; }
.app.sidebar-collapsed { grid-template-columns: 0 minmax(0, 1fr); }
.app.sidebar-collapsed .sidebar { visibility: hidden; border-right: 0; }

.topbar-app { display: flex; grid-column: 1 / -1; align-items: center; gap: 6px; border-bottom: 1px solid var(--line); padding: 0 10px; background: var(--panel); }
.topbar-app .wordmark { margin: 0 8px 0 4px; }
.topbar-spacer { flex: 1; }
.account { display: flex; align-items: center; margin-left: 4px; }
.account-fallback { display: grid; width: 28px; height: 28px; overflow: hidden; place-items: center; border-radius: 50%; color: var(--accent-ink); background: var(--accent); font-size: 12px; font-weight: 700; }
.account.has-clerk .account-fallback { display: none; }

/* ---------- sidebar ---------- */
.sidebar { display: flex; min-width: 0; min-height: 0; flex-direction: column; overflow: hidden; border-right: 1px solid var(--line); background: var(--panel-2); }
.sidebar-actions { display: flex; gap: 6px; padding: 12px 12px 8px; }
.search { display: flex; align-items: center; gap: 8px; margin: 0 12px 8px; border: 1px solid var(--line); border-radius: var(--radius); padding: 0 8px 0 10px; color: var(--muted); background: var(--panel); }
.search:focus-within { border-color: var(--accent); box-shadow: var(--focus); }
.search svg { width: 14px; height: 14px; }
.search input { width: 100%; height: 32px; min-width: 0; border: 0 !important; padding: 0; background: transparent !important; box-shadow: none !important; font-size: 13px; }
.search input::-webkit-search-cancel-button { display: none; }
.search:focus-within .search-kbd { display: none; }
.doc-list { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 8px 12px; }
.doc-group { margin: 10px 8px 4px; color: var(--muted); font-size: 11px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; }
.doc-item { display: grid; grid-template-columns: 18px minmax(0, 1fr) auto; align-items: center; column-gap: 8px; width: 100%; border: 0; border-radius: 7px; padding: 7px 8px; color: var(--ink-2); background: transparent; text-align: left; }
.doc-item:hover { color: var(--ink); background: var(--hover); }
.doc-item[aria-current="true"] { color: var(--ink); background: var(--accent-soft); }
.doc-item-icon { display: grid; width: 18px; height: 18px; place-items: center; border-radius: 4px; color: var(--muted); font-family: var(--mono); font-size: 9.5px; font-weight: 600; }
.doc-item-icon.latex { color: #2f6fa8; background: color-mix(in srgb, #2f6fa8 12%, transparent); }
.doc-item-icon.typst { color: #1e8f8a; background: color-mix(in srgb, #1e8f8a 13%, transparent); }
:root[data-theme="dark"] .doc-item-icon.latex { color: #7fb4e6; }
:root[data-theme="dark"] .doc-item-icon.typst { color: #6dd3cd; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .doc-item-icon.latex { color: #7fb4e6; }
  :root:not([data-theme="light"]) .doc-item-icon.typst { color: #6dd3cd; }
}
.doc-item-title { overflow: hidden; font-size: 13px; font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }
.doc-item-time { color: var(--muted); font-size: 11.5px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.doc-item-dot { display: inline-block; width: 6px; height: 6px; margin-right: 6px; border-radius: 50%; vertical-align: 1px; }
.doc-item-dot.failed { background: var(--danger); }
.doc-item-dot.running, .doc-item-dot.queued { background: var(--warn); }
.doc-item mark { border-radius: 2px; color: inherit; background: color-mix(in srgb, var(--warn) 28%, transparent); }
.doc-list-empty { padding: 28px 16px; color: var(--muted); font-size: 13px; text-align: center; }
.doc-list-empty strong { display: block; margin-bottom: 4px; color: var(--ink); font-weight: 600; }
.doc-skeleton { display: grid; gap: 10px; padding: 10px 8px; }
.doc-skeleton span { height: 14px; border-radius: 4px; background: linear-gradient(90deg, var(--sunken), var(--hover), var(--sunken)); background-size: 200% 100%; animation: shimmer 1.2s infinite; }
.doc-skeleton span:nth-child(2) { width: 80%; }
.doc-skeleton span:nth-child(3) { width: 60%; }
@keyframes shimmer { to { background-position: -200% 0; } }
.sidebar-footer { border-top: 1px solid var(--line); padding: 10px 16px; color: var(--muted); font-size: 12px; }
.scrim { display: none; }

/* ---------- main ---------- */
.main { display: flex; min-width: 0; min-height: 0; flex-direction: column; background: var(--bg); }

.welcome { flex: 1; overflow-y: auto; padding: clamp(24px, 6vh, 72px) 24px 48px; }
.welcome-inner { width: min(720px, 100%); margin: 0 auto; }
.welcome h1 { margin: 0 0 6px; font-size: 26px; font-weight: 650; letter-spacing: -.03em; }
.welcome-lede { margin: 0 0 24px; color: var(--muted); font-size: 14.5px; }
.starter-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; margin-bottom: 10px; }
.starter { display: flex; align-items: flex-start; gap: 12px; width: 100%; border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 14px; color: var(--ink); background: var(--panel); box-shadow: var(--shadow-sm); text-align: left; transition: border-color .12s, transform .12s, box-shadow .12s; }
.starter:hover { border-color: var(--line-strong); box-shadow: var(--shadow-md); transform: translateY(-1px); }
.starter-icon { display: grid; width: 32px; height: 32px; flex: none; place-items: center; border-radius: 8px; color: var(--accent-text); background: var(--accent-soft); }
.starter-text { display: flex; min-width: 0; flex-direction: column; gap: 2px; }
.starter-text strong { font-size: 13.5px; font-weight: 600; }
.starter-text span { color: var(--muted); font-size: 12.5px; line-height: 1.45; }
.starter-engine { align-self: flex-start; margin-top: 4px; color: var(--muted); font-family: var(--mono); font-size: 10.5px; }
.starter-ai { border-style: dashed; background: transparent; box-shadow: none; }
.starter-ai .starter-icon { color: var(--warn); background: var(--warn-soft); }
.welcome-recent { margin-top: 32px; }
.welcome-recent h2 { margin: 0 0 8px; color: var(--muted); font-size: 12px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; }
.welcome-recent .doc-item { padding: 9px 10px; }

.editor-view { display: flex; flex: 1; min-height: 0; flex-direction: column; }
.doc-header { display: flex; align-items: center; gap: 12px; border-bottom: 1px solid var(--line); padding: 8px 12px 8px 16px; background: var(--panel); }
.doc-identity { display: flex; min-width: 0; flex: 1; align-items: center; gap: 12px; }
.doc-title { width: 100%; min-width: 80px; max-width: 460px; height: 32px; border-color: transparent !important; padding: 0 8px; margin-left: -8px; background: transparent !important; font-size: 15px; font-weight: 600; letter-spacing: -.01em; text-overflow: ellipsis; }
.doc-title:hover { border-color: var(--line) !important; }
.doc-title:focus { border-color: var(--accent) !important; background: var(--panel) !important; }
.doc-meta { display: flex; flex: none; align-items: center; gap: 10px; }
.save-state { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.save-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
.save-state[data-state="dirty"] .save-dot { background: var(--warn); }
.save-state[data-state="saving"] .save-dot { background: var(--warn); animation: pulse 1s ease-in-out infinite; }
.save-state[data-state="error"] { color: var(--danger); }
.save-state[data-state="error"] .save-dot { background: var(--danger); }
.save-state button { border: 0; padding: 0; color: var(--accent-text); background: none; font-size: 12px; font-weight: 600; text-decoration: underline; }
@keyframes pulse { 50% { opacity: .35; } }
.doc-actions { display: flex; flex: none; align-items: center; gap: 6px; }
.view-switch { display: none; }
.view-dot { display: inline-block; width: 6px; height: 6px; margin-left: 3px; border-radius: 50%; background: var(--accent); vertical-align: 1px; }
.view-dot.failed { background: var(--danger); }

.menu-wrap { position: relative; }
.menu { position: absolute; z-index: 30; top: calc(100% + 6px); right: 0; min-width: 232px; border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 5px; background: var(--panel); box-shadow: var(--shadow-lg); }
.menu-item { display: flex; width: 100%; align-items: center; gap: 10px; border: 0; border-radius: 6px; padding: 7px 9px; color: var(--ink); background: transparent; font-size: 13px; text-align: left; text-decoration: none; }
.menu-item:hover, .menu-item:focus-visible { background: var(--hover); box-shadow: none; }
.menu-item svg { color: var(--muted); }
.menu-item.danger { color: var(--danger); }
.menu-item.danger svg { color: currentColor; }
.menu-item.disabled { opacity: .45; pointer-events: none; }
.menu-toggle { cursor: pointer; }
.menu-toggle input { width: 15px; height: 15px; margin: 0; }
.menu-sep { height: 1px; margin: 5px 4px; background: var(--line); }

/* ---------- split ---------- */
.split { --split: 50%; display: grid; flex: 1; min-height: 0; grid-template-columns: minmax(280px, var(--split)) 1px minmax(280px, 1fr); }
.split.dragging { cursor: col-resize; user-select: none; }
.split.dragging iframe { pointer-events: none; }
.pane { display: flex; min-width: 0; min-height: 0; flex-direction: column; }
.pane-source { background: var(--editor-bg); }
.monaco-host { position: relative; flex: 1; min-height: 0; }
.editor-loading { display: flex; height: 100%; align-items: center; justify-content: center; gap: 10px; color: var(--muted); font-size: 13px; }
.editor-fallback { width: 100%; height: 100%; border: 0 !important; border-radius: 0 !important; padding: 16px 20px; resize: none; color: var(--ink); background: var(--editor-bg) !important; box-shadow: none !important; font-family: var(--mono); font-size: 13px; line-height: 1.65; tab-size: 2; }
.splitter { position: relative; z-index: 5; background: var(--line); cursor: col-resize; touch-action: none; }
.splitter::after { content: ""; position: absolute; inset: 0 -4px; }
.splitter:hover, .splitter:focus-visible, .split.dragging .splitter { background: var(--accent); box-shadow: none; }

.pane-pdf { position: relative; background: var(--pdf-bg); }
.pdf-bar { display: flex; height: 38px; flex: none; align-items: center; justify-content: space-between; gap: 8px; border-bottom: 1px solid var(--line); padding: 0 8px 0 12px; background: var(--panel-2); }
.pdf-bar-actions { display: flex; align-items: center; gap: 2px; }
.build-pill { display: inline-flex; min-width: 0; align-items: center; gap: 7px; overflow: hidden; color: var(--muted); font-size: 12px; white-space: nowrap; }
.build-pill > span:last-child { overflow: hidden; text-overflow: ellipsis; }
.build-dot { width: 7px; height: 7px; flex: none; border-radius: 50%; background: var(--line-strong); }
.build-pill[data-status="succeeded"] .build-dot { background: var(--accent); }
.build-pill[data-status="stale"] .build-dot { background: var(--accent); opacity: .45; }
.build-pill[data-status="queued"] .build-dot, .build-pill[data-status="running"] .build-dot { background: var(--warn); animation: pulse 1s ease-in-out infinite; }
.build-pill[data-status="failed"] { color: var(--danger); }
.build-pill[data-status="failed"] .build-dot { background: var(--danger); }
.pdf-stage { position: relative; display: flex; flex: 1; min-height: 0; }
#pdf-frame { flex: 1; width: 100%; border: 0; background: var(--pdf-bg); }
.pdf-empty { display: flex; flex: 1; flex-direction: column; align-items: center; justify-content: center; gap: 4px; padding: 32px; color: var(--muted); text-align: center; }
.sheet { display: flex; width: 84px; height: 108px; flex-direction: column; gap: 7px; margin-bottom: 16px; border-radius: 3px; padding: 16px 13px; background: var(--panel); box-shadow: var(--shadow-md); }
.sheet span { height: 4px; border-radius: 2px; background: var(--line-strong); }
.sheet span:first-child { width: 60%; height: 6px; margin-bottom: 4px; background: var(--muted); opacity: .5; }
.sheet span:nth-child(3) { width: 85%; }
.sheet span:nth-child(4) { width: 70%; }
.pdf-empty-title { margin: 0; color: var(--ink); font-size: 14px; font-weight: 600; }
.pdf-empty-hint { margin: 0; font-size: 12.5px; }
.pdf-busy { position: absolute; top: 12px; left: 50%; display: inline-flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; color: var(--ink-2); background: var(--panel); box-shadow: var(--shadow-md); font-size: 12px; transform: translateX(-50%); }

.log-panel { display: flex; max-height: 55%; flex: none; flex-direction: column; border-top: 1px solid var(--line); background: var(--panel); }
.log-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; border-bottom: 1px solid var(--line); padding: 6px 8px 6px 14px; font-size: 12.5px; }
.log-head-actions { display: flex; align-items: center; gap: 2px; }
.log-issues { margin: 0; padding: 0; overflow-y: auto; list-style: none; }
.log-issue { display: grid; grid-template-columns: 16px minmax(0, 1fr) auto; gap: 10px; align-items: start; width: 100%; border: 0; border-bottom: 1px solid var(--line); padding: 9px 14px; background: transparent; text-align: left; }
button.log-issue:hover { background: var(--hover); }
.log-issue svg { width: 14px; height: 14px; margin-top: 2px; color: var(--danger); }
.log-issue.warning svg { color: var(--warn); }
.log-issue-msg { overflow-wrap: anywhere; color: var(--ink); font-family: var(--mono); font-size: 12px; line-height: 1.5; }
.log-issue-line { color: var(--accent-text); font-family: var(--mono); font-size: 11.5px; white-space: nowrap; }
.log-issue.none { display: block; color: var(--muted); font-size: 12.5px; }
.log-full { flex: none; border-top: 0; }
.log-full summary { padding: 8px 14px; color: var(--muted); font-size: 12px; cursor: pointer; }
.log-full pre { max-height: 240px; margin: 0; overflow: auto; border-top: 1px solid var(--line); padding: 10px 14px; color: var(--ink-2); background: var(--sunken); font-size: 11.5px; line-height: 1.55; white-space: pre-wrap; overflow-wrap: anywhere; }

/* ==========================================================================
   Dialogs, menus, toasts
   ========================================================================== */
.dialog { width: min(520px, calc(100vw - 32px)); max-height: calc(100dvh - 48px); border: 1px solid var(--line); border-radius: 14px; padding: 0; color: var(--ink); background: var(--panel); box-shadow: var(--shadow-lg); }
.dialog-wide { width: min(680px, calc(100vw - 32px)); margin-top: min(12vh, 96px); }
.dialog-narrow { width: min(420px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(10, 12, 10, .45); backdrop-filter: blur(2px); }
.dialog[open] { animation: dialog-in .16s ease-out; }
@keyframes dialog-in { from { opacity: 0; transform: translateY(6px) scale(.985); } }
.dialog-form { display: flex; max-height: calc(100dvh - 50px); flex-direction: column; }
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; padding: 18px 18px 6px 22px; }
.dialog-head h2 { margin: 4px 0 0; font-size: 17px; font-weight: 650; letter-spacing: -.02em; }
.dialog-sub { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.dialog-body { display: flex; flex-direction: column; gap: 16px; overflow-y: auto; padding: 14px 22px 18px; }
.dialog-foot { display: flex; align-items: center; justify-content: flex-end; gap: 8px; border-top: 1px solid var(--line); padding: 12px 18px; background: var(--panel-2); border-radius: 0 0 14px 14px; }
.foot-hint { margin-right: auto; color: var(--muted); font-size: 12px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; font-weight: 600; color: var(--ink-2); }
.field input, .field textarea, .field select { width: 100%; padding: 8px 10px; color: var(--ink); font-size: 14px; font-weight: 400; }
.field textarea { resize: vertical; line-height: 1.55; }
.form-error { min-height: 0; margin: 0; color: var(--danger); font-size: 13px; }
.form-error:empty { display: none; }
.confirm-body { padding-top: 22px; }
.confirm-body h2 { margin: 0; font-size: 16px; font-weight: 650; }
.confirm-body p { margin: 0; color: var(--ink-2); font-size: 13.5px; }

.template-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.template-card { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; border: 1px solid var(--line); border-radius: 10px; padding: 11px; color: var(--ink); background: var(--panel); text-align: left; }
.template-card:hover { border-color: var(--line-strong); background: var(--panel-2); }
.template-card[aria-checked="true"] { border-color: var(--accent); background: var(--accent-soft); box-shadow: inset 0 0 0 1px var(--accent); }
.template-card svg { color: var(--accent-text); }
.template-card strong { font-size: 13px; font-weight: 600; }
.template-card span { color: var(--muted); font-size: 11.5px; font-weight: 400; line-height: 1.4; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: -6px; }
.chip { border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px; color: var(--ink-2); background: var(--panel-2); font-size: 12px; }
.chip:hover { border-color: var(--line-strong); color: var(--ink); }

.tabs { display: flex; gap: 2px; margin: 8px 22px 0; border-bottom: 1px solid var(--line); }
.tabs button { margin-bottom: -1px; border: 0; border-bottom: 2px solid transparent; padding: 8px 10px; color: var(--muted); background: none; font-size: 13px; font-weight: 550; }
.tabs button:hover { color: var(--ink); }
.tabs button[aria-selected="true"] { border-bottom-color: var(--accent); color: var(--ink); }
.tab-panels section { display: flex; flex-direction: column; gap: 14px; }
.panel-intro { margin: 0; color: var(--ink-2); font-size: 13px; }
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; min-width: 0; height: 32px; padding: 0 10px; font-size: 13px; }
.inline-form select { height: 32px; padding: 0 8px; font-size: 13px; }
.table-list { overflow: hidden; border: 1px solid var(--line); border-radius: var(--radius); }
.table-row { display: flex; align-items: center; gap: 12px; border-bottom: 1px solid var(--line); padding: 9px 12px; font-size: 13px; }
.table-row:last-child { border-bottom: 0; }
.table-row .grow { min-width: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.table-row .sub { color: var(--muted); font-size: 12px; white-space: nowrap; }
.table-empty { padding: 18px 12px; color: var(--muted); font-size: 13px; text-align: center; }
.tag { display: inline-flex; align-items: center; border-radius: 999px; padding: 2px 8px; color: var(--muted); background: var(--sunken); font-size: 11px; font-weight: 550; white-space: nowrap; }
.tag.ok { color: var(--accent-text); background: var(--accent-soft); }
.tag.warn { color: var(--warn); background: var(--warn-soft); }
.tag.bad { color: var(--danger); background: var(--danger-soft); }
.callout { border-radius: var(--radius); padding: 12px; font-size: 13px; }
.callout p { margin: 0 0 8px; }
.callout.success { border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); background: var(--accent-soft); }
.copy-row { display: flex; align-items: center; gap: 8px; }
.copy-row code { min-width: 0; flex: 1; overflow-wrap: anywhere; font-size: 12px; }
.code-card { position: relative; border: 1px solid var(--line); border-radius: var(--radius); background: var(--sunken); }
.code-card pre { margin: 0; overflow-x: auto; padding: 14px; color: var(--ink-2); font-size: 12px; line-height: 1.6; }
.code-copy { position: absolute; top: 8px; right: 8px; }

.shortcut-list { display: grid; gap: 2px; margin: 0; }
.shortcut-list div { display: flex; align-items: center; gap: 16px; border-bottom: 1px solid var(--line); padding: 8px 0; }
.shortcut-list div:last-child { border-bottom: 0; }
.shortcut-list dt { width: 96px; flex: none; }
.shortcut-list dd { margin: 0; color: var(--ink-2); font-size: 13px; }

.toasts { position: fixed; z-index: 100; right: 16px; bottom: 16px; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; pointer-events: none; }
.toast { display: flex; max-width: min(380px, calc(100vw - 32px)); align-items: center; gap: 10px; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; color: var(--ink); background: var(--panel); box-shadow: var(--shadow-lg); font-size: 13px; pointer-events: auto; animation: toast-in .18s ease-out; }
.toast svg { color: var(--accent-text); }
.toast.error svg { color: var(--danger); }
.toast button { margin-left: 6px; border: 0; padding: 0; color: var(--accent-text); background: none; font-size: 13px; font-weight: 600; }
.toast.leaving { opacity: 0; transform: translateY(4px); transition: opacity .2s, transform .2s; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- theme toggle ---------- */
.theme-icon { display: none; }
:root:not([data-theme]) .theme-system, :root[data-theme="light"] .theme-light, :root[data-theme="dark"] .theme-dark { display: block; }

/* ==========================================================================
   Responsive workspace
   ========================================================================== */
@media (max-width: 1080px) {
  .btn-kbd, .label-wide { display: none; }
  .starter-grid { grid-template-columns: 1fr; }
}

@media (max-width: 860px) {
  .app, .app.sidebar-collapsed { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; z-index: 50; top: 0; bottom: 0; left: 0; width: min(300px, 86vw); visibility: visible !important; border-right: 1px solid var(--line) !important; box-shadow: var(--shadow-lg); transform: translateX(-102%); transition: transform .2s ease; }
  .app.sidebar-open .sidebar { transform: none; }
  .app.sidebar-open .scrim { position: fixed; z-index: 45; inset: 0; display: block; background: rgba(10, 12, 10, .4); }
  .main { grid-column: 1; }
  .doc-header { flex-wrap: wrap; row-gap: 8px; padding: 8px 10px 8px 14px; }
  .doc-identity { flex-basis: 100%; }
  .view-switch { display: inline-flex; margin-right: auto; }
  .split { display: flex; }
  .splitter { display: none; }
  .pane { flex: 1; }
  .app[data-view="source"] .pane-pdf, .app[data-view="pdf"] .pane-source { display: none; }
  .template-grid { grid-template-columns: 1fr; }
  .template-card { flex-direction: row; align-items: center; }
}

@media (max-width: 520px) {
  .doc-meta .segmented { display: none; }
  .inline-form { flex-wrap: wrap; }
  .inline-form input { flex-basis: 100%; }
  .dialog-foot .foot-hint { display: none; }
}

/* ==========================================================================
   Public pages: landing, sign-in, session sync
   ========================================================================== */
.shell { width: min(1080px, calc(100% - 40px)); margin: 0 auto; }
.site-nav { display: flex; height: 64px; align-items: center; justify-content: space-between; gap: 12px; }
.site-nav-actions { display: flex; align-items: center; gap: 6px; }
.hero { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.05fr); gap: 48px; align-items: center; padding: clamp(40px, 9vh, 96px) 0 64px; }
.eyebrow { display: inline-flex; align-items: center; gap: 8px; margin: 0 0 18px; border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px 4px 8px; color: var(--ink-2); background: var(--panel); font-size: 12.5px; font-weight: 500; }
.eyebrow-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.hero h1 { margin: 0 0 18px; font-size: clamp(38px, 5.4vw, 60px); font-weight: 700; line-height: 1.02; letter-spacing: -.045em; }
.hero h1 em { color: var(--accent); font-style: normal; }
.lede { max-width: 520px; margin: 0 0 28px; color: var(--ink-2); font-size: 17px; line-height: 1.6; }
.hero-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.hero-art { position: relative; overflow: hidden; border: 1px solid var(--line); border-radius: 14px; background: var(--panel); box-shadow: var(--shadow-lg); }
.hero-art-bar { display: flex; align-items: center; gap: 6px; border-bottom: 1px solid var(--line); padding: 10px 12px; background: var(--panel-2); }
.hero-art-bar i { width: 9px; height: 9px; border-radius: 50%; background: var(--line-strong); }
.hero-art-body { display: grid; grid-template-columns: 1fr 1fr; min-height: 260px; }
.hero-code { margin: 0; border-right: 1px solid var(--line); padding: 16px; color: var(--ink-2); background: var(--editor-bg); font-size: 11.5px; line-height: 1.75; white-space: pre; overflow: hidden; }
.hero-code b { color: var(--accent-text); font-weight: 500; }
.hero-code i { color: var(--muted); }
.hero-pdf { display: grid; place-items: center; padding: 18px; background: var(--pdf-bg); }
.hero-page { width: 100%; max-width: 190px; aspect-ratio: 8.5 / 11; border-radius: 2px; padding: 18px 16px; color: #222; background: #fff; box-shadow: 0 6px 20px rgba(0, 0, 0, .15); font-family: Georgia, "Times New Roman", serif; }
.hero-page h3 { margin: 0 0 2px; font-size: 13px; text-align: center; }
.hero-page p { margin: 0 0 10px; color: #666; font-size: 7.5px; text-align: center; }
.hero-page .line { height: 3px; margin-bottom: 5px; border-radius: 2px; background: #d9d9d9; }
.hero-page .line.short { width: 70%; }
.hero-page .eq { margin: 10px 0; font-size: 10px; font-style: italic; text-align: center; }
.feature-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-bottom: 56px; }
.feature { border: 1px solid var(--line); border-radius: var(--radius-lg); padding: 20px; background: var(--panel); }
.feature-icon { display: grid; width: 32px; height: 32px; place-items: center; margin-bottom: 14px; border-radius: 8px; color: var(--accent-text); background: var(--accent-soft); }
.feature h2 { margin: 0 0 6px; font-size: 15px; font-weight: 600; letter-spacing: -.01em; }
.feature p { margin: 0; color: var(--ink-2); font-size: 13.5px; line-height: 1.6; }
.footer { display: flex; justify-content: space-between; gap: 12px; border-top: 1px solid var(--line); padding: 20px 0 28px; color: var(--muted); font-size: 12.5px; }
.footer a { color: inherit; text-decoration: none; }
.footer a:hover { color: var(--ink); }

.center-page { display: grid; min-height: calc(100dvh - 64px); place-items: center; padding: 24px 0 64px; }
.signin-card { display: flex; width: min(440px, 100%); flex-direction: column; align-items: center; gap: 6px; text-align: center; }
.signin-card h1 { margin: 0; font-size: 26px; font-weight: 650; letter-spacing: -.03em; }
.signin-card > p { margin: 0 0 18px; color: var(--muted); font-size: 14px; }
.clerk-mount { display: grid; width: 100%; min-height: 360px; place-items: start center; }
.status-card { display: flex; flex-direction: column; align-items: center; gap: 14px; color: var(--muted); font-size: 14px; text-align: center; }
.status-card .spinner { width: 22px; height: 22px; color: var(--accent); }
.loading { display: flex; min-height: 120px; align-items: center; justify-content: center; gap: 10px; color: var(--muted); font-size: 13px; }

@media (max-width: 860px) {
  .hero { grid-template-columns: 1fr; gap: 36px; }
  .feature-grid { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
  .shell { width: calc(100% - 32px); }
  .hero-art-body { grid-template-columns: 1fr; }
  .hero-code { border-right: 0; border-bottom: 1px solid var(--line); }
  .footer { flex-direction: column; }
}
