:root {
  --bg: #f4f6f8; --surface: #ffffff; --surface-2: #f8fafb; --border: #dde3e8; --text: #1b2733; --muted: #5d6b78;
  --accent: #1f5f8b; --accent-hover: #174a6d; --on-accent: #ffffff; --accent-soft: #e5f0f7;
  --ok: #1e7a4c; --ok-soft: #e3f4ea; --warn: #9a6200; --warn-soft: #fdf1dc; --bad: #b3261e; --bad-soft: #fbe7e6;
  --info: #3b5bcc; --info-soft: #e8edfc; --neutral-soft: #eceff2;
  --radius: 8px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f151b; --surface: #17202a; --surface-2: #1c2632; --border: #2b3846; --text: #e3e9ef; --muted: #9aa8b5;
    --accent: #5aa6dc; --accent-hover: #7bbbe6; --on-accent: #0b1620; --accent-soft: #1b3346;
    --ok: #5cc98f; --ok-soft: #173327; --warn: #e7b14d; --warn-soft: #382a12; --bad: #f08a82; --bad-soft: #3d1c1a;
    --info: #8fa6ff; --info-soft: #1f2748; --neutral-soft: #222d39; --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 var(--font); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 16px 0 8px; }
code, .mono { font-family: var(--mono); font-size: 12px; }

.shell { display: grid; grid-template-columns: 232px minmax(0, 1fr); min-height: 100vh; }
.sidebar { background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; gap: 10px; align-items: center; padding: 18px 16px; color: var(--text); border-bottom: 1px solid var(--border); }
.brand:hover { text-decoration: none; }
.brand small { display: block; color: var(--muted); font-size: 12px; }
nav { padding: 10px 8px; flex: 1; overflow-y: auto; }
nav a { display: flex; justify-content: space-between; align-items: center; padding: 8px 10px; border-radius: 6px; color: var(--text); }
nav a:hover { background: var(--surface-2); text-decoration: none; }
nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-section { margin: 16px 10px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.me { padding: 12px 16px; border-top: 1px solid var(--border); font-size: 13px; }
.me select { width: 100%; margin-top: 8px; }
.pill { background: var(--bad); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; min-width: 18px; text-align: center; }

main { padding: 28px 32px 60px; max-width: 1280px; width: 100%; min-width: 0; outline: none; }
.loading { color: var(--muted); padding: 40px 0; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head p { margin: 0; color: var(--muted); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 16px; }
.grid { display: grid; gap: 16px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.stat b { display: block; font-size: 26px; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: 13px; }
.stat.alert b { color: var(--bad); }
.empty { color: var(--muted); padding: 8px 0; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; white-space: nowrap; background: var(--neutral-soft); color: var(--muted); }
.badge.Effective, .badge.Signed, .badge.Completed { background: var(--ok-soft); color: var(--ok); }
.badge.Draft, .badge.Waiting { background: var(--neutral-soft); color: var(--muted); }
.badge.In-Review, .badge.Pending, .badge.Open { background: var(--info-soft); color: var(--info); }
.badge.Approved { background: var(--accent-soft); color: var(--accent); }
.badge.Rejected, .badge.Obsolete, .badge.Overdue { background: var(--bad-soft); color: var(--bad); }
.badge.Superseded, .badge.Cancelled, .badge.Withdrawn { background: var(--neutral-soft); color: var(--muted); text-decoration: line-through; }
.badge.warn { background: var(--warn-soft); color: var(--warn); }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 7px 14px; border-radius: 6px; font: inherit; font-weight: 500; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--bad); border-color: var(--bad); }
.btn.small { padding: 3px 10px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

label { display: block; font-weight: 600; font-size: 13px; margin: 12px 0 4px; }
label .hint { font-weight: 400; color: var(--muted); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--text); font: inherit;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { min-height: 80px; resize: vertical; }
select[multiple] { min-height: 120px; }
.checkbox { display: flex; gap: 8px; align-items: center; font-weight: 400; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.filters > *, .filters input[type=text], .filters input[type=date], .filters select { width: auto; min-width: 150px; }
.filters input[type=search] { flex: 1; min-width: 220px; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 0 16px; }

.doc-head { display: flex; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.doc-head > .actions { align-self: flex-start; }
td.mono { white-space: nowrap; }
.doc-head .number { font-family: var(--mono); color: var(--muted); font-size: 13px; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px 20px; margin-top: 14px; }
.facts div span { display: block; font-size: 12px; color: var(--muted); }
.banner { padding: 10px 14px; border-radius: 6px; margin: 12px 0 0; background: var(--warn-soft); color: var(--warn); }
.banner.info { background: var(--info-soft); color: var(--info); }
.banner.bad { background: var(--bad-soft); color: var(--bad); }
.mt { margin-top: 14px; }
.empty.pad { padding: 16px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin: 18px 0 16px; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 10px 14px; font: inherit; color: var(--muted); cursor: pointer; border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.rev { border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; background: var(--surface); }
.rev-head { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; align-items: center; }
.rev-head h3 { margin: 0; }
.meta { color: var(--muted); font-size: 13px; }
.sig-list { margin: 10px 0 0; padding: 0; list-style: none; }
.sig-list li { padding: 8px 10px; border-left: 3px solid var(--ok); background: var(--surface-2); margin-bottom: 6px; border-radius: 0 6px 6px 0; }
.sig-list li.rej { border-left-color: var(--bad); }
.route { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }
.route .step { border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px; background: var(--surface-2); font-size: 13px; }
.audit-details { font-family: var(--mono); font-size: 11.5px; color: var(--muted); word-break: break-word; max-width: 520px; }

.modal-backdrop { position: fixed; inset: 0; background: rgba(10,16,24,.5); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 50; overflow-y: auto; }
.modal { background: var(--surface); border-radius: 10px; width: 100%; max-width: 560px; padding: 22px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal.wide { max-width: 760px; }
.modal h2 { margin-bottom: 4px; }
.modal footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }
.modal .error { color: var(--bad); background: var(--bad-soft); padding: 8px 12px; border-radius: 6px; margin-top: 12px; }
.sign-box { border: 1px solid var(--border); background: var(--surface-2); padding: 12px 14px; border-radius: 8px; margin-top: 14px; }
.sign-box p { margin: 0 0 8px; font-size: 13px; }
.step-builder { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-top: 10px; background: var(--surface-2); }
.step-builder header { display: flex; justify-content: space-between; align-items: center; }

#toast { position: fixed; bottom: 20px; right: 20px; z-index: 60; }
#toast div { background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; margin-top: 8px; box-shadow: var(--shadow); max-width: 380px; }
#toast div.err { background: var(--bad); color: #fff; }

@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: static; height: auto; }
  nav { display: flex; flex-wrap: wrap; gap: 4px; }
  .nav-section { width: 100%; }
  main { padding: 20px 16px 60px; }
  .grid.two { grid-template-columns: 1fr; }
}
@media print {
  .sidebar, .actions, .tabs, .btn { display: none !important; }
  .shell { display: block; }
  main::before { content: "UNCONTROLLED WHEN PRINTED — verify the current revision in Keystone DMS"; display: block; font-weight: 700; margin-bottom: 12px; }
}
