:root {
  color-scheme: dark;
  --page: #0b1110;
  --surface: #121b19;
  --surface-raised: #17221f;
  --surface-hover: #1b2925;
  --line: #26342f;
  --text: #eef4f1;
  --muted: #91a29a;
  --subtle: #687b72;
  --accent: #56d6a0;
  --accent-strong: #20b879;
  --accent-ink: #08251a;
  --warning: #edc46f;
  --danger: #ff847b;
  --shadow: 0 24px 70px rgb(0 0 0 / 28%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html[data-theme="light"] {
  color-scheme: light;
  --page: #f3f6f4;
  --surface: #fff;
  --surface-raised: #f8faf9;
  --surface-hover: #eef5f1;
  --line: #dce5df;
  --text: #1a2a23;
  --muted: #60736a;
  --subtle: #82938b;
  --accent: #087c53;
  --accent-strong: #079b61;
  --accent-ink: #fff;
  --warning: #966300;
  --danger: #b83232;
  --shadow: 0 24px 70px rgb(34 61 46 / 12%);
}

* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; background: var(--page); color: var(--text); font-size: 14px; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
a { color: var(--accent); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }
.muted { color: var(--muted); }
.eyebrow { margin: 0 0 7px; color: var(--accent); font-size: 10px; font-weight: 750; letter-spacing: .14em; }
.legal-shell { width: min(1500px, 100%); margin: 0 auto; padding: 0 30px 45px; }
.legal-header { position: sticky; top: 0; z-index: 4; display: flex; min-height: 72px; align-items: center; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line); background: color-mix(in srgb, var(--page) 90%, transparent); backdrop-filter: blur(15px); }
.brand { display: inline-flex; align-items: center; gap: 11px; color: var(--text); text-decoration: none; }
.brand-mark { display: grid; width: 37px; height: 37px; place-items: center; border-radius: 11px; background: linear-gradient(140deg, var(--accent), var(--accent-strong)); color: var(--accent-ink); font-size: 20px; font-weight: 850; }
.brand strong, .brand small { display: block; }
.brand strong { font-size: 14px; }
.brand small { margin-top: 3px; color: var(--muted); font-size: 11px; }
.legal-header-actions, .editor-actions, .row-actions { display: flex; align-items: center; gap: 9px; }
.legal-header select, input, select, textarea { min-height: 38px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); color: var(--text); }
.legal-header select { max-width: 290px; padding: 7px 10px; }
.button { display: inline-flex; min-height: 36px; align-items: center; justify-content: center; padding: 0 13px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-raised); color: var(--text); text-decoration: none; font-size: 11px; font-weight: 650; }
.button:hover { border-color: var(--accent); }
.button-primary { border-color: var(--accent-strong); background: var(--accent-strong); color: var(--accent-ink); }
.button-quiet { background: transparent; color: var(--muted); }
.button-small { min-height: 30px; padding: 0 9px; font-size: 10px; }
.legal-shell h1 { margin: 0 0 7px; font-size: clamp(26px, 3vw, 36px); letter-spacing: -.04em; }
.legal-shell h2 { margin: 0 0 5px; font-size: 15px; }
.legal-shell h3 { margin: 0 0 5px; font-size: 13px; }
.page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin: 28px 0 20px; }
.page-actions { display: flex; align-items: center; gap: 9px; }
.page-heading p:last-child { margin: 0; }
.notice { display: flex; flex-direction: column; gap: 5px; margin: 18px 0; padding: 14px 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--muted); font-size: 11px; line-height: 1.55; }
.notice strong { color: var(--text); }
.notice-warning { border-color: color-mix(in srgb, var(--warning) 45%, var(--line)); background: color-mix(in srgb, var(--warning) 8%, var(--surface)); }
.message { min-height: 20px; margin: 10px 0; color: var(--muted); font-size: 12px; }
.message.error { color: var(--danger); }
.message.success { color: var(--accent); }
.toast-stack { position: fixed; z-index: 10000; top: max(12px, env(safe-area-inset-top)); right: max(12px, env(safe-area-inset-right)); width: min(390px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); overflow-y: auto; display: grid; gap: 8px; pointer-events: none; }
.toast { pointer-events: auto; display: flex; align-items: flex-start; gap: 12px; padding: 14px; border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: 10px; background: var(--surface-raised); color: var(--text); box-shadow: var(--shadow); font-size: 13px; line-height: 1.5; overflow-wrap: anywhere; }
.toast.error { border-left-color: var(--danger); }
.toast span { flex: 1; min-width: 0; }
.toast button { flex: none; width: 30px; height: 30px; border: 1px solid var(--line); border-radius: 8px; background: transparent; color: var(--text); }
.legal-grid { display: grid; grid-template-columns: minmax(270px, .75fr) minmax(0, 1.5fr); align-items: start; gap: 16px; }
.panel { min-width: 0; overflow: hidden; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); }
.panel-heading { display: flex; min-height: 70px; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 17px; border-bottom: 1px solid var(--line); }
.panel-heading p { margin: 0; font-size: 10px; }
.search-label { display: grid; gap: 7px; padding: 14px 16px; color: var(--muted); font-size: 10px; font-weight: 650; }
.search-label input { width: 100%; padding: 8px 10px; font-size: 12px; }
.type-catalog { display: grid; max-height: 540px; gap: 5px; overflow: auto; padding: 0 10px 12px; }
.type-card { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 9px 10px; border: 1px solid transparent; border-radius: 7px; background: transparent; color: var(--text); text-align: left; font-size: 11px; }
.type-card:hover { border-color: var(--line); background: var(--surface-hover); }
.type-card small { color: var(--subtle); font-size: 9px; white-space: nowrap; }
.source-shortcuts { padding: 15px 16px; border-top: 1px solid var(--line); }
.source-shortcuts p { color: var(--muted); font-size: 10px; line-height: 1.5; }
.source-links { display: flex; flex-wrap: wrap; gap: 8px 12px; margin-bottom: 13px; font-size: 10px; }
.table-wrap { width: 100%; overflow: auto; }
table { width: 100%; border-collapse: collapse; text-align: left; white-space: nowrap; }
th { padding: 11px 12px; color: var(--subtle); font-size: 9px; letter-spacing: .06em; text-transform: uppercase; }
td { padding: 11px 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 10px; }
td:first-child { color: var(--text); font-weight: 600; }
.status { display: inline-flex; padding: 4px 7px; border-radius: 30px; background: var(--surface-raised); color: var(--muted); font-size: 9px; text-transform: uppercase; }
.status-review { color: var(--warning); }
.status-final { color: var(--accent); }
.export-menu { position: relative; }
.export-menu summary { padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; color: var(--muted); cursor: pointer; font-size: 10px; list-style: none; }
.export-menu summary::-webkit-details-marker { display: none; }
.export-menu-items { position: absolute; top: calc(100% + 5px); right: 0; z-index: 3; display: grid; min-width: 145px; padding: 5px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-raised); box-shadow: var(--shadow); }
.export-menu-items button { padding: 8px; border: 0; border-radius: 5px; background: transparent; color: var(--text); text-align: left; font-size: 10px; }
.export-menu-items button:hover { background: var(--surface-hover); }
.empty-state { display: grid; gap: 5px; padding: 25px 16px; color: var(--muted); font-size: 11px; }
.empty-state strong { color: var(--text); }
.editor-panel { margin: 24px 0; }
.editor-panel .panel-heading h2 { margin: 0; }
.document-form { display: grid; gap: 14px; padding: 20px; }
.document-form label { display: grid; align-content: start; gap: 7px; color: var(--muted); font-size: 10px; font-weight: 650; }
.document-form input, .document-form select, .document-form textarea { width: 100%; padding: 9px 10px; font-size: 12px; }
.document-form textarea { resize: vertical; line-height: 1.55; }
#ocr-review-dialog:not([open]) { display: none; }
#ocr-review-dialog[open] { width: min(760px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); grid-template-rows: auto auto auto minmax(100px, 1fr) auto; gap: 10px; overflow: auto; color: var(--text); }
#ocr-review-dialog::backdrop { background: rgba(0, 0, 0, 0.7); }
#ocr-review-dialog h2, #ocr-review-dialog p { margin: 0; }
#ocr-review-dialog label { min-height: 0; grid-template-rows: auto minmax(0, 1fr); }
#ocr-review-text { min-height: 100px; height: 100%; resize: none; }
#ocr-review-dialog .editor-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
#document-content { min-height: 380px; font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 13px; }
.subsection { display: grid; gap: 12px; padding: 15px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface-raised); }
.subsection-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.subsection-heading p, .subsection > .muted { margin: 0; font-size: 10px; }
.sources-list { display: grid; gap: 9px; }
.source-row { display: grid; grid-template-columns: 1fr 1.2fr 1.5fr auto; align-items: end; gap: 8px; }
.source-row input { min-width: 0; }
.remove-source { padding: 0 7px; }
.attachments-list, .research-results { display: grid; gap: 9px; }
.attachment-row, .research-result { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.attachment-row > div:first-child, .research-result > div { display: grid; min-width: 0; gap: 4px; }
.attachment-row strong, .research-result a { overflow-wrap: anywhere; color: var(--text); font-size: 11px; }
.attachment-row span, .attachment-row p, .research-result p, .research-result small { margin: 0; color: var(--muted); font-size: 10px; line-height: 1.5; }
.attachment-actions { display: flex; flex: 0 0 auto; gap: 6px; }
.attachment-ai-select, .consent-label, .ai-companion-select { display: flex !important; align-items: flex-start; gap: 7px; color: var(--muted); font-size: 10px; line-height: 1.45; }
.attachment-ai-select { white-space: nowrap; }
.attachment-ai-select input, .consent-label input, .ai-companion-select input { width: auto !important; min-height: auto !important; margin: 2px 0 0; accent-color: var(--accent-strong); }
.ai-drafting { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.ai-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; }
.ai-actions .muted, #ai-status { font-size: 10px; }
.ai-review { display: grid; gap: 10px; }
.ai-review > h4, .ai-notice-list h4 { margin: 4px 0; font-size: 11px; }
.ai-notice-list, .ai-companion { display: grid; gap: 8px; padding: 11px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.ai-notice-list ul { display: grid; gap: 5px; margin: 0; padding-left: 18px; color: var(--muted); font-size: 10px; line-height: 1.5; }
.ai-companion-select span { display: grid; gap: 4px; }
.ai-companion-select strong { color: var(--text); font-size: 11px; }
.ai-companion-select small, .ai-companion > p { margin: 0; color: var(--muted); font-size: 10px; }
.ai-companion textarea { min-height: 160px; }
.research-group { display: grid; gap: 8px; }
.research-group h4 { margin: 8px 0 0; color: var(--text); font-size: 11px; }
.research-group h4 span { margin-left: 5px; color: var(--subtle); font-weight: 400; }
.research-provenance { margin: 0; overflow-wrap: anywhere; font-size: 10px; line-height: 1.5; }
.research-result { align-items: center; }
.research-result .button { flex: 0 0 auto; }
.subsection > p.muted { line-height: 1.5; }
.editor-submit { align-self: end; justify-content: flex-end; }
.legal-footer { margin-top: 24px; padding-top: 14px; border-top: 1px solid var(--line); color: var(--subtle); font-size: 10px; }
.auth-notice { margin: 20vh auto 0; width: min(560px, 100%); padding: 28px; }
.auth-notice p { color: var(--muted); font-size: 12px; }

@media (max-width: 900px) {
  .legal-grid { grid-template-columns: 1fr; }
  .type-catalog { max-height: 300px; }
}
@media (max-width: 650px) {
  .legal-shell { padding: 0 14px 30px; }
  .legal-header { align-items: flex-start; flex-direction: column; padding: 12px 0; }
  .legal-header-actions { width: 100%; flex-wrap: wrap; }
  .legal-header select { flex: 1; min-width: 160px; }
  .page-heading { align-items: flex-start; flex-direction: column; }
  .page-heading .button { align-self: flex-start; }
  .page-actions { flex-wrap: wrap; }
  .panel-heading { align-items: flex-start; flex-direction: column; }
  .editor-panel .panel-heading { flex-direction: row; align-items: center; }
  .form-grid { grid-template-columns: 1fr; }
  .source-row { grid-template-columns: 1fr 1fr; }
  .source-row label:nth-child(3) { grid-column: 1 / -1; }
  .attachment-row, .research-result { align-items: flex-start; flex-direction: column; }
  .editor-actions { flex-wrap: wrap; }
  .editor-submit { justify-content: flex-start; }
}
