:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-muted: #f8fafb;
  --line: #d8dee5;
  --line-strong: #bcc6d1;
  --text: #17212b;
  --muted: #637083;
  --sidebar: #20262d;
  --sidebar-muted: #aeb8c3;
  --primary: #1f63d3;
  --primary-dark: #174ea9;
  --primary-soft: #eaf1fd;
  --success: #16784b;
  --success-soft: #e7f5ee;
  --warning: #9a6500;
  --warning-soft: #fff5d9;
  --danger: #b4232f;
  --danger-soft: #fdecef;
  --focus: #78a8f5;
  --shadow: 0 8px 26px rgba(27, 39, 51, 0.09);
  font-family: "Segoe UI", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
}
* { box-sizing: border-box; }
html, body { min-height: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button, a { -webkit-tap-highlight-color: transparent; }
a { color: inherit; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 0.35rem; font-size: 1.75rem; line-height: 1.2; letter-spacing: 0; }
h2 { margin-bottom: 0.35rem; font-size: 1.08rem; line-height: 1.3; letter-spacing: 0; }
h3 { margin-bottom: 0.5rem; font-size: 0.96rem; letter-spacing: 0; }
p { color: var(--muted); }
code { font-family: Consolas, monospace; font-size: 0.88em; overflow-wrap: anywhere; }
[hidden] { display: none !important; }

.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;
}

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 232px minmax(0, 1fr); }
.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  color: #fff;
  background: var(--sidebar);
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 11px; min-height: 48px; padding: 4px 7px; color: #fff; text-decoration: none; }
.brand-mark {
  display: inline-grid;
  place-items: center;
  flex: 0 0 34px;
  width: 34px;
  height: 34px;
  border-radius: 6px;
  color: #fff;
  background: var(--primary);
  font-weight: 800;
}
.brand strong { display: block; font-size: 1rem; }
.brand small { display: block; color: var(--sidebar-muted); font-size: 0.72rem; }
.nav-list { display: grid; gap: 4px; margin-top: 24px; }
.nav-link {
  display: grid;
  grid-template-columns: 24px 1fr;
  align-items: center;
  min-height: 42px;
  padding: 0 10px;
  border-radius: 5px;
  color: var(--sidebar-muted);
  text-decoration: none;
  font-size: 0.92rem;
  font-weight: 600;
}
.nav-link > span { font-size: 1.1rem; text-align: center; }
.nav-link:hover { color: #fff; background: #2b333c; }
.nav-link.active { color: #fff; background: #303a45; box-shadow: inset 3px 0 var(--primary); }
.sidebar-account { margin-top: auto; padding: 14px 7px 2px; border-top: 1px solid #39424b; }
.account-label, .account-role { display: block; overflow: hidden; text-overflow: ellipsis; }
.account-label { font-size: 0.86rem; font-weight: 700; }
.account-role { margin: 3px 0 12px; color: var(--sidebar-muted); font-size: 0.72rem; }
.main-area { min-width: 0; }
.mobile-header { display: none; }
.page-container { width: min(1460px, 100%); margin: 0 auto; padding: 30px 34px 54px; }

.page-header, .case-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 24px; margin-bottom: 24px; }
.page-header p, .case-header p { margin-bottom: 0; }
.page-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.compact-header { margin-bottom: 18px; }
.eyebrow { margin-bottom: 5px !important; color: var(--primary); font-size: 0.76rem; font-weight: 700; text-transform: uppercase; }
.back-link, .text-link { color: var(--primary-dark); text-decoration: none; font-weight: 650; }
.back-link { display: inline-block; margin-bottom: 9px; font-size: 0.84rem; }
.back-link:hover, .text-link:hover { text-decoration: underline; }
.case-title-line { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.case-meta { display: flex; flex-direction: column; align-items: flex-end; color: var(--muted); font-size: 0.78rem; white-space: nowrap; }

.button, .icon-button {
  border: 1px solid transparent;
  border-radius: 5px;
  cursor: pointer;
  font-weight: 650;
  text-decoration: none;
  transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}
.button { display: inline-flex; align-items: center; justify-content: center; min-height: 40px; padding: 0 15px; font-size: 0.88rem; }
.button-primary { color: #fff; background: var(--primary); border-color: var(--primary); }
.button-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.button-secondary { color: #24415f; background: #fff; border-color: var(--line-strong); }
.button-secondary:hover { background: var(--surface-muted); border-color: #91a0af; }
.button-quiet { color: var(--text); background: transparent; border-color: var(--line); }
.sidebar .button-quiet { color: #fff; border-color: #4a5561; }
.button-quiet:hover { background: rgba(127, 142, 157, 0.1); }
.button-danger-quiet { color: var(--danger); background: transparent; border-color: #e8b7bd; }
.button-danger-quiet:hover { background: var(--danger-soft); }
.button-small { min-height: 32px; padding: 0 10px; font-size: 0.78rem; }
.button-full { width: 100%; }
.button:disabled, .icon-button:disabled { cursor: not-allowed; opacity: 0.58; }
.icon-button { display: inline-grid; place-items: center; width: 36px; height: 36px; padding: 0; color: var(--text); background: transparent; border-color: transparent; font-size: 1.15rem; }
.icon-button:hover { background: var(--surface-muted); border-color: var(--line); }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }

input, select, textarea {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  color: var(--text);
  background: #fff;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
}
input:hover, select:hover, textarea:hover { border-color: #8c9aa8; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); outline: 2px solid rgba(31, 99, 211, 0.16); }
input[aria-invalid="true"], select[aria-invalid="true"] { border-color: var(--danger); }
label > span { display: block; margin-bottom: 5px; color: #344354; font-size: 0.78rem; font-weight: 650; }
label > small { display: block; min-height: 16px; margin-top: 4px; color: var(--muted); font-size: 0.71rem; }
label > small:not(:empty)[data-error-for] { color: var(--danger); }

.workbench { display: grid; grid-template-columns: minmax(0, 1.65fr) minmax(280px, 0.8fr); border: 1px solid var(--line); background: var(--surface); border-radius: 6px; overflow: hidden; }
.workbench-main, .workbench-upload { padding: 20px; }
.workbench-upload { border-left: 1px solid var(--line); background: var(--surface-muted); }
.workbench-upload p { margin-bottom: 14px; font-size: 0.84rem; }
.lookup-form { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 9px; }
.lookup-results { margin-top: 12px; border-top: 1px solid var(--line); }
.lookup-result { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 14px; align-items: center; padding: 12px 2px; border-bottom: 1px solid var(--line); }
.lookup-result strong, .lookup-result span { display: block; }
.lookup-result span { color: var(--muted); font-size: 0.78rem; }
.upload-form { display: grid; gap: 10px; }
.file-picker { display: block; min-width: 0; }
.file-picker > span { display: grid; place-items: center; min-height: 42px; margin: 0; padding: 8px; border: 1px dashed #8d9aa7; border-radius: 5px; color: #344354; background: #fff; cursor: pointer; }
.file-picker input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }
.file-picker.has-file > span { border-style: solid; border-color: var(--success); color: var(--success); background: var(--success-soft); }

.stats-strip { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 18px 0 30px; border: 1px solid var(--line); background: var(--surface); border-radius: 6px; }
.stats-strip > div { min-width: 0; padding: 15px 18px; border-right: 1px solid var(--line); }
.stats-strip > div:last-child { border-right: 0; }
.stats-strip strong, .stats-strip span { display: block; }
.stats-strip strong { font-size: 1.25rem; }
.stats-strip span { color: var(--muted); font-size: 0.75rem; }

.section-block { margin-top: 24px; }
.section-heading { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; margin-bottom: 11px; }
.section-heading h2, .section-heading p { margin-bottom: 0; }
.section-heading p { font-size: 0.8rem; }
.table-wrap { width: 100%; overflow-x: auto; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.data-table { width: 100%; min-width: 720px; border-collapse: collapse; font-size: 0.84rem; }
.data-table th { padding: 9px 12px; color: #48586a; background: #f0f3f6; border-bottom: 1px solid var(--line); text-align: left; font-size: 0.72rem; font-weight: 750; text-transform: uppercase; white-space: nowrap; }
.data-table td { padding: 11px 12px; border-bottom: 1px solid #e7ebef; vertical-align: middle; }
.data-table tbody tr:last-child td { border-bottom: 0; }
.data-table tbody tr:hover { background: #fbfcfd; }
.data-table td > span, .data-table td > a + span, .data-table td > strong + span { display: block; margin-top: 2px; color: var(--muted); font-size: 0.72rem; }
.table-primary { color: var(--text); font-weight: 750; text-decoration: none; }
.table-primary:hover { color: var(--primary-dark); text-decoration: underline; }
.table-action { width: 52px; text-align: right; }
.compact-table { min-width: 560px; font-size: 0.8rem; }
.compact-table th, .compact-table td { padding: 8px 10px; }

.status { display: inline-flex; align-items: center; min-height: 24px; padding: 2px 8px; border-radius: 999px; font-size: 0.7rem; font-weight: 750; white-space: nowrap; }
.status-draft { color: #596777; background: #edf0f3; }
.status-review { color: var(--warning); background: var(--warning-soft); }
.status-ready { color: #18589e; background: var(--primary-soft); }
.status-generated { color: var(--success); background: var(--success-soft); }
.status-revoked { color: var(--danger); background: var(--danger-soft); }
.empty-state { display: grid; place-items: center; min-height: 150px; padding: 28px; border: 1px dashed var(--line-strong); border-radius: 6px; color: var(--muted); text-align: center; }
.empty-state strong { color: var(--text); }
.empty-state span { display: block; margin-top: -32px; font-size: 0.82rem; }

.filter-bar { display: grid; grid-template-columns: minmax(240px, 1fr) 220px auto; gap: 9px; margin-bottom: 15px; }
.filter-grow { min-width: 0; }

.alert { margin: 0 0 16px; padding: 11px 13px; border: 1px solid; border-radius: 5px; font-size: 0.84rem; }
.alert-error { color: #7c1c27; background: var(--danger-soft); border-color: #efbbc1; }
.alert-warning { color: #6e4a00; background: var(--warning-soft); border-color: #ecd18d; }
.alert-success { color: #0f5937; background: var(--success-soft); border-color: #afd9c7; }

.case-editor { border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.editor-toolbar { position: sticky; top: 0; z-index: 10; display: flex; justify-content: space-between; align-items: center; gap: 12px; min-height: 62px; padding: 10px 14px; border-bottom: 1px solid var(--line); background: rgba(255, 255, 255, 0.98); border-radius: 6px 6px 0 0; }
.segmented-tabs { display: inline-flex; max-width: 100%; padding: 3px; border: 1px solid var(--line); border-radius: 5px; background: #eef1f4; overflow-x: auto; }
.segmented-tabs button { min-height: 34px; padding: 0 12px; border: 0; border-radius: 3px; color: #526172; background: transparent; cursor: pointer; font-size: 0.79rem; font-weight: 700; white-space: nowrap; }
.segmented-tabs button[aria-selected="true"] { color: var(--text); background: #fff; box-shadow: 0 1px 4px rgba(30, 42, 55, 0.12); }
.toolbar-actions { display: flex; gap: 7px; }
.action-progress { display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 40px; color: var(--primary-dark); background: var(--primary-soft); border-bottom: 1px solid #c7d9f6; font-size: 0.82rem; }
.spinner { width: 16px; height: 16px; border: 2px solid #b6cdf3; border-top-color: var(--primary); border-radius: 50%; animation: spin 0.7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.tab-panel { padding: 0 24px 26px; }
.form-section { padding: 24px 0; border-bottom: 1px solid var(--line); }
.form-section:last-child { border-bottom: 0; }
.form-section-heading { display: grid; grid-template-columns: minmax(180px, 0.4fr) minmax(280px, 1fr); gap: 20px; margin-bottom: 15px; }
.form-section-heading h2, .form-section-heading p { margin: 0; }
.form-section-heading p { font-size: 0.8rem; }
.form-grid { display: grid; gap: 14px 12px; }
.form-grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.form-grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }

.summary-metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 24px 0; border: 1px solid var(--line); border-radius: 6px; }
.summary-metrics > div { padding: 15px; border-right: 1px solid var(--line); }
.summary-metrics > div:last-child { border-right: 0; }
.summary-metrics span, .summary-metrics strong { display: block; }
.summary-metrics span { color: var(--muted); font-size: 0.72rem; }
.summary-metrics strong { margin-top: 3px; font-size: 1.05rem; }
.calculation-tables { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(260px, 0.7fr); gap: 22px; }
.calculation-tables p { padding: 14px; border-left: 3px solid var(--primary); background: var(--surface-muted); font-size: 0.84rem; }
.calculation-audit { margin: 18px 0 0; color: var(--muted); font-family: Consolas, monospace; font-size: 0.7rem; }
.documents-panel { padding-top: 24px; }
.document-actions { display: flex; justify-content: flex-end; margin-bottom: 12px; }
.document-list { border: 1px solid var(--line); border-radius: 6px; overflow: hidden; }
.document-row { display: grid; grid-template-columns: 48px minmax(0, 1fr) 28px; gap: 12px; align-items: center; min-height: 70px; padding: 10px 14px; border-bottom: 1px solid var(--line); text-decoration: none; }
.document-row:last-child { border-bottom: 0; }
.document-row:hover { background: var(--surface-muted); }
.document-row strong, .document-row small { display: block; }
.document-row small { margin-top: 3px; color: var(--muted); }
.document-icon { display: grid; place-items: center; width: 44px; height: 36px; color: #7a2e2e; background: #fbeaea; border-radius: 4px; font-size: 0.65rem; font-weight: 800; }
.case-footer { display: flex; justify-content: space-between; align-items: center; gap: 15px; padding: 16px 2px; color: var(--muted); font-size: 0.75rem; }

.admin-tabs { margin-bottom: 16px; }
.admin-layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); gap: 24px; }
.admin-tool { padding: 18px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); align-self: start; }
.admin-tool > p { font-size: 0.8rem; }
.admin-table-area { min-width: 0; }
.stack-form { display: grid; gap: 13px; }
.import-layout { grid-template-columns: minmax(300px, 0.7fr) minmax(360px, 1.3fr); }
.import-help { padding: 18px 0; }
.import-help p { max-width: 760px; font-size: 0.86rem; line-height: 1.65; }
.import-result { margin-top: 14px; padding: 12px; border-radius: 5px; background: var(--success-soft); color: var(--success); font-size: 0.82rem; }
.key-dialog { width: min(620px, calc(100% - 30px)); padding: 24px; border: 1px solid var(--line); border-radius: 7px; box-shadow: var(--shadow); }
.key-dialog::backdrop { background: rgba(20, 27, 34, 0.56); }
.dialog-close { position: absolute; top: 10px; right: 10px; }
.secret-output { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; align-items: center; margin-top: 18px; padding: 10px; border: 1px solid var(--line); background: var(--surface-muted); border-radius: 5px; }
.secret-output code { user-select: all; }

.login-layout { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: #e9edf1; }
.login-panel { width: min(420px, 100%); padding: 30px; border: 1px solid var(--line); border-radius: 7px; background: var(--surface); box-shadow: var(--shadow); }
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 26px; }
.login-panel h1 { font-size: 1.45rem; }
.login-panel > p { margin-bottom: 22px; font-size: 0.88rem; }
.login-note { display: block; margin-top: 18px; color: var(--muted); font-size: 0.7rem; text-align: center; }

.toast-region { position: fixed; right: 18px; bottom: 18px; z-index: 100; display: grid; gap: 8px; width: min(390px, calc(100% - 36px)); pointer-events: none; }
.toast { padding: 11px 13px; border: 1px solid #aeb8c2; border-radius: 5px; color: #fff; background: #26313c; box-shadow: var(--shadow); font-size: 0.82rem; animation: toast-in 160ms ease-out; }
.toast.success { border-color: #5aa980; background: #135f3c; }
.toast.error { border-color: #d56c76; background: #8b1d28; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

@media (max-width: 1080px) {
  .page-container { padding: 26px 24px 48px; }
  .form-grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .summary-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .summary-metrics > div:nth-child(2) { border-right: 0; }
  .summary-metrics > div:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .calculation-tables { grid-template-columns: 1fr; }
  .admin-layout { grid-template-columns: 260px minmax(0, 1fr); }
}

@media (max-width: 820px) {
  .app-shell { display: block; }
  .sidebar { position: fixed; left: 0; transform: translateX(-105%); width: 246px; transition: transform 160ms ease; box-shadow: var(--shadow); }
  .sidebar.is-open { transform: translateX(0); }
  .mobile-header { position: sticky; top: 0; z-index: 15; display: grid; grid-template-columns: 40px auto minmax(0, 1fr); align-items: center; min-height: 52px; padding: 0 14px; border-bottom: 1px solid var(--line); background: #fff; }
  .mobile-org { justify-self: end; max-width: 45vw; overflow: hidden; color: var(--muted); font-size: 0.72rem; text-overflow: ellipsis; white-space: nowrap; }
  .page-container { padding: 20px 15px 40px; }
  .page-header, .case-header { align-items: stretch; flex-direction: column; gap: 13px; }
  .page-actions { justify-content: flex-start; }
  .case-meta { align-items: flex-start; }
  .workbench { grid-template-columns: 1fr; }
  .workbench-upload { border-top: 1px solid var(--line); border-left: 0; }
  .stats-strip { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stats-strip > div:nth-child(2) { border-right: 0; }
  .stats-strip > div:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .filter-bar { grid-template-columns: 1fr; }
  .editor-toolbar { position: static; align-items: stretch; flex-direction: column; }
  .segmented-tabs { width: 100%; }
  .segmented-tabs button { flex: 1; }
  .toolbar-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .toolbar-actions .button-primary { grid-column: span 2; }
  .tab-panel { padding: 0 15px 20px; }
  .form-section-heading { grid-template-columns: 1fr; gap: 4px; }
  .form-grid-3, .form-grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .admin-layout, .import-layout { grid-template-columns: 1fr; }
  .admin-tool { max-width: none; }
}

@media (max-width: 560px) {
  h1 { font-size: 1.45rem; }
  .lookup-form { grid-template-columns: 1fr; }
  .page-actions { display: grid; grid-template-columns: 1fr; }
  .stats-strip { grid-template-columns: 1fr; }
  .stats-strip > div { border-right: 0; border-bottom: 1px solid var(--line); }
  .stats-strip > div:last-child { border-bottom: 0; }
  .form-grid-3, .form-grid-4 { grid-template-columns: 1fr; }
  .span-2 { grid-column: span 1; }
  .toolbar-actions { grid-template-columns: 1fr; }
  .toolbar-actions .button-primary { grid-column: auto; }
  .summary-metrics { grid-template-columns: 1fr; }
  .summary-metrics > div { border-right: 0; border-bottom: 1px solid var(--line); }
  .summary-metrics > div:last-child { border-bottom: 0; }
  .case-footer { align-items: stretch; flex-direction: column; }
  .secret-output { grid-template-columns: 1fr; }
  .login-panel { padding: 22px; }
}
