:root {
  --navy: #0d2b4f;
  --ink: #132033;
  --muted: #617083;
  --line: #b8c4d1;
  --line-soft: #d9e0e7;
  --app-bg: #f2f5f8;
  --white: #ffffff;
  --teal: #078b8e;
  --blue: #2780b9;
  --review: #2b438a;
  --purple: #7043a0;
  --magenta: #96307d;
  --raspberry: #c6145a;
  --success: #188247;
  --danger: #b62d3c;
  --focus: #ffbf47;
  --control-text: #091729;
  --control-font: 15px;
  font-family: Aptos, "Segoe UI", Arial, sans-serif;
  color: var(--ink);
  background: var(--app-bg);
}

* { box-sizing: border-box; }

html { min-width: 320px; background: var(--app-bg); }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--app-bg);
  color: var(--ink);
}

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

button { color: inherit; }

.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 100;
  transform: translateY(-160%);
  padding: 10px 14px;
  color: var(--white);
  background: var(--navy);
}

.skip-link:focus { transform: translateY(0); }

.app-shell { min-height: 100vh; }

.utility-bar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: minmax(250px, 1fr) minmax(300px, 520px) auto;
  align-items: center;
  min-height: 64px;
  gap: 18px;
  padding: 10px 24px;
  border-bottom: 1px solid var(--line-soft);
  background: var(--white);
}

.brand-button {
  appearance: none;
  border: 0;
  padding: 4px 0;
  background: none;
  color: var(--navy);
  font-size: 20px;
  font-weight: 800;
  letter-spacing: .015em;
  text-align: left;
  cursor: pointer;
}

.record-reference {
  display: grid;
  grid-template-columns: auto minmax(180px, 1fr);
  align-items: center;
  gap: 12px;
  color: var(--navy);
  font-size: 13px;
  font-weight: 700;
}

.record-reference input {
  width: 100%;
  height: 40px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: var(--white);
  color: var(--ink);
  font-family: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 14px;
}

.utility-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.button {
  min-height: 40px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: var(--white);
  color: var(--navy);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.2;
  cursor: pointer;
}

.button-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}

.button:hover { background: #f5f8fb; border-color: #8fa1b5; }
.button:active { transform: translateY(1px); }
.button:disabled { cursor: not-allowed; opacity: .45; transform: none; }

.button:focus-visible,
.brand-button:focus-visible,
.gate-button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.button-primary {
  border-color: var(--teal);
  background: var(--teal);
  color: var(--white);
}

.button-primary:hover { background: #04787b; border-color: #04787b; }

.gate-strip {
  position: sticky;
  top: 64px;
  z-index: 25;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0;
  padding: 12px 24px 0;
  background: var(--white);
}

.gate-button {
  position: relative;
  min-height: 44px;
  border: 0;
  border-right: 1px solid rgba(255, 255, 255, .72);
  padding: 8px 12px;
  color: var(--white);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .015em;
  cursor: pointer;
}

.gate-button[aria-current="step"]::after {
  position: absolute;
  right: 14px;
  bottom: 7px;
  left: 14px;
  height: 3px;
  background: var(--white);
  content: "";
}

.gate-button.is-complete::before {
  margin-right: 7px;
  content: "✓";
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 312px;
  gap: 18px;
  padding: 14px 24px 94px;
  max-width: 1760px;
  margin: 0 auto;
}

.document-canvas {
  min-width: 0;
  min-height: 760px;
  padding: 24px 18px 28px;
  border: 1px solid #e1e6ec;
  background: var(--white);
  box-shadow: 0 2px 8px rgba(13, 43, 79, .055);
}

.page-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 22px;
  padding: 0 2px;
}

.page-heading h1 {
  margin: 0 0 10px;
  color: var(--gate-color, var(--teal));
  font-size: clamp(27px, 2.15vw, 38px);
  font-weight: 800;
  line-height: 1.08;
  letter-spacing: -.02em;
}

.page-heading p {
  max-width: 900px;
  margin: 0;
  color: var(--muted);
  font-size: 16px;
  line-height: 1.45;
}

.record-link-note {
  flex: 0 0 auto;
  padding: 8px 10px;
  border: 1px solid var(--line-soft);
  color: var(--muted);
  font-size: 12px;
}

.form-section { margin-top: 24px; }

.form-section:first-of-type { margin-top: 0; }

.form-section-header {
  margin: 0 0 12px;
}

.form-section-header h2 {
  margin: 0;
  color: var(--gate-color, var(--teal));
  font-size: 20px;
  font-weight: 800;
  line-height: 1.25;
}

.form-section-header p {
  margin: 7px 0 0;
  padding: 11px 14px;
  border-left: 5px solid var(--gate-color, var(--teal));
  background: #edf4f8;
  color: #34465a;
  font-size: 13px;
  line-height: 1.45;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(var(--columns, 2), minmax(0, 1fr));
  border-top: 1px solid var(--line);
  border-left: 1px solid var(--line);
}

.field-block {
  min-width: 0;
  display: grid;
  grid-template-columns: minmax(138px, 25%) minmax(0, 1fr);
  align-items: stretch;
  min-height: 74px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}

.field-block.is-full { grid-column: 1 / -1; }

.field-block > label {
  display: flex;
  align-items: center;
  padding: 13px 12px;
  border-right: 1px solid var(--line);
  color: var(--navy);
  font-size: 13px;
  font-weight: 800;
  line-height: 1.35;
}

.control-wrap {
  display: flex;
  align-items: center;
  min-width: 0;
  padding: 10px;
}

.control {
  width: 100%;
  min-width: 0;
  min-height: 39px;
  padding: 9px 10px;
  border: 1px solid #aebdcb;
  border-radius: 3px;
  background: var(--white);
  color: var(--control-text);
  font-size: var(--control-font);
  font-weight: 500;
  line-height: 1.5;
  letter-spacing: .002em;
}

textarea.control {
  min-height: 82px;
  resize: vertical;
}

.control::placeholder {
  color: #78889a;
  font-weight: 400;
  opacity: 1;
}

.control.is-filled { border-color: #879db3; background: var(--white); }

.data-table-wrap {
  width: 100%;
  overflow-x: auto;
  border: 1px solid var(--line);
}

.data-table {
  width: 100%;
  min-width: 760px;
  border-collapse: collapse;
  table-layout: fixed;
}

.data-table th {
  padding: 12px 10px;
  border-right: 1px solid rgba(255,255,255,.75);
  background: var(--gate-color, var(--teal));
  color: var(--white);
  font-size: 11px;
  font-weight: 800;
  line-height: 1.25;
  text-align: left;
  text-transform: uppercase;
}

.data-table td {
  min-width: 0;
  padding: 8px;
  border-top: 1px solid var(--line);
  border-right: 1px solid var(--line);
  vertical-align: middle;
}

.data-table tr:first-child td { border-top: 0; }
.data-table td:last-child,
.data-table th:last-child { border-right: 0; }

.data-table .control {
  min-height: 42px;
  border-color: #afbdca;
  font-size: 13px;
}

.data-table textarea.control { min-height: 72px; }

.label-only {
  display: block;
  color: var(--navy);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.35;
}

.level-stack {
  display: grid;
  gap: 10px;
}

.level-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(160px, 220px);
  align-items: center;
  gap: 18px;
  padding: 14px;
  border: 1px solid var(--line);
  background: var(--white);
}

.level-row label {
  color: var(--navy);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.4;
}

.worksheet-tabs {
  display: flex;
  gap: 8px;
  margin: 0 0 16px;
}

.worksheet-tab[aria-selected="true"] {
  border-color: var(--review);
  background: var(--review);
  color: var(--white);
}

.worksheet-meta {
  display: grid;
  grid-template-columns: 1.5fr .62fr 1.35fr .5fr;
  border-top: 1px solid var(--line);
  border-left: 1px solid var(--line);
}

.worksheet-meta .mini-field {
  min-width: 0;
  padding: 8px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: #f4f6fa;
}

.worksheet-meta .mini-field.is-full { grid-column: 1 / 4; }

.mini-field label {
  display: block;
  margin-bottom: 5px;
  color: var(--navy);
  font-size: 10px;
  font-weight: 800;
  text-transform: uppercase;
}

.mini-field .control {
  min-height: 34px;
  padding: 7px 8px;
  font-size: 13px;
}

.mini-field textarea.control { min-height: 50px; }

.risk-table { min-width: 960px; table-layout: fixed; }
.risk-table th { padding: 11px 6px; font-size: 9px; text-align: center; }
.risk-table td { padding: 5px; }
.risk-table textarea.control { min-height: 286px; font-size: 12px; font-weight: 500; line-height: 1.55; }
.risk-table input.control { min-height: 286px; font-size: 12px; font-weight: 500; text-align: center; }
.risk-table th:nth-child(1), .risk-table td:nth-child(1) { width: 10%; }
.risk-table th:nth-child(2), .risk-table td:nth-child(2) { width: 13%; }
.risk-table th:nth-child(3), .risk-table td:nth-child(3) { width: 15%; }
.risk-table th:nth-child(4), .risk-table td:nth-child(4),
.risk-table th:nth-child(5), .risk-table td:nth-child(5) { width: 4%; }
.risk-table th:nth-child(6), .risk-table td:nth-child(6) { width: 6%; }
.risk-table th:nth-child(7), .risk-table td:nth-child(7) { width: 17%; }
.risk-table th:nth-child(8), .risk-table td:nth-child(8) { width: 7%; }
.risk-table th:nth-child(9), .risk-table td:nth-child(9) { width: 11%; }
.risk-table th:nth-child(10), .risk-table td:nth-child(10) { width: 13%; }

.team-grid {
  display: grid;
  grid-template-columns: 1fr 1.5fr 1fr 1.5fr;
  border-left: 1px solid var(--line);
}

.team-grid .mini-field {
  padding: 5px 6px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: #f4f6fa;
}

.team-grid .mini-field label { margin-bottom: 3px; font-size: 8px; }
.team-grid .mini-field .control { min-height: 32px; padding: 6px 7px; font-size: 11px; font-weight: 500; }

.status-rail {
  align-self: start;
  position: sticky;
  top: 132px;
  border: 1px solid var(--line-soft);
  background: var(--white);
}

.rail-section { padding: 14px 15px; border-bottom: 1px solid var(--line-soft); }
.rail-section:last-child { border-bottom: 0; }

.rail-section h2 {
  margin: 0 0 10px;
  color: var(--navy);
  font-size: 13px;
  font-weight: 800;
}

.rail-copy { color: var(--muted); font-size: 12px; line-height: 1.4; }

.progress-line {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin: 9px 0 6px;
  color: var(--navy);
  font-size: 11px;
}

.progress-track { height: 7px; overflow: hidden; background: #dfe6ed; }
.progress-track span { display: block; width: 0; height: 100%; background: var(--gate-color, var(--teal)); transition: width .2s ease; }

.rail-gates { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }

.rail-gate-button {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr) auto;
  align-items: center;
  width: 100%;
  gap: 8px;
  padding: 3px 0;
  border: 0;
  background: none;
  color: var(--navy);
  font-size: 12px;
  text-align: left;
  cursor: pointer;
}

.rail-gate-number {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--rail-color);
  color: var(--white);
  font-weight: 800;
}

.rail-gate-state { color: #94a2b1; }
.rail-gate-button.is-complete .rail-gate-state { color: var(--success); }
.rail-gate-button[aria-current="step"] .rail-gate-label { font-weight: 800; }

.save-state { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; color: var(--navy); font-size: 12px; }
.save-check { color: var(--success); font-size: 16px; font-weight: 900; }

.page-previews { display: flex; align-items: flex-end; gap: 12px; margin: 10px 0; }

.page-mini {
  display: grid;
  align-content: start;
  gap: 5px;
  padding: 8px;
  border: 1px solid var(--line);
  background: var(--white);
  box-shadow: 0 2px 5px rgba(13, 43, 79, .10);
}

.page-mini.portrait { width: 76px; height: 104px; }
.page-mini.landscape { width: 112px; height: 78px; }
.page-mini span { height: 5px; background: var(--teal); }
.page-mini b { height: 10px; background: var(--review); }
.page-mini i { height: 4px; background: #d7dee6; }

.export-section { display: grid; gap: 8px; }
.button-rail { width: 100%; border-color: var(--navy); }

.workflow-footer {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 35;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 20px;
  min-height: 70px;
  padding: 12px 24px;
  border-top: 1px solid var(--line-soft);
  background: rgba(255,255,255,.97);
}

.footer-status { color: var(--muted); font-size: 12px; font-weight: 700; }
.footer-actions { display: flex; gap: 10px; }

.toast {
  position: fixed;
  right: 24px;
  bottom: 86px;
  z-index: 80;
  max-width: min(420px, calc(100vw - 48px));
  padding: 12px 16px;
  border-left: 5px solid var(--success);
  background: var(--navy);
  color: var(--white);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(10px);
  transition: opacity .18s ease, transform .18s ease;
}

.toast.is-visible { opacity: 1; transform: translateY(0); }
.toast.is-error { border-left-color: #ff6374; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(13, 43, 79, .34);
}

.modal-backdrop[hidden] { display: none; }

.modal {
  width: min(480px, 100%);
  padding: 24px;
  border: 1px solid var(--line);
  background: var(--white);
  box-shadow: 0 16px 50px rgba(13, 43, 79, .18);
}

.modal h2 { margin: 0 0 10px; color: var(--navy); font-size: 22px; }
.modal p { margin: 0; color: var(--muted); font-size: 14px; line-height: 1.5; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 24px; }

.print-workbook { display: none; }

@media (max-width: 1180px) {
  .utility-bar { grid-template-columns: 1fr auto; }
  .record-reference { grid-column: 1 / -1; grid-row: 2; }
  .gate-strip { top: 114px; }
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .status-rail { position: static; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .rail-section { border-right: 1px solid var(--line-soft); }
  .preview-section { display: none; }
}

@media (max-width: 760px) {
  :root { --control-font: 16px; }
  .utility-bar { position: static; display: flex; flex-wrap: wrap; padding: 12px 14px; }
  .brand-button { width: 100%; font-size: 18px; }
  .record-reference { width: 100%; grid-template-columns: 1fr; gap: 5px; }
  .utility-actions { width: 100%; display: grid; grid-template-columns: repeat(2, 1fr); }
  .utility-actions .help-link { grid-column: 1 / -1; }
  .gate-strip { position: static; grid-template-columns: repeat(3, 1fr); padding: 0 14px; }
  .gate-button { min-height: 42px; font-size: 10px; }
  .workspace { padding: 12px 10px 112px; }
  .document-canvas { min-height: auto; padding: 18px 10px 24px; box-shadow: none; }
  .page-heading { display: block; }
  .page-heading h1 { font-size: 25px; }
  .page-heading p { font-size: 14px; }
  .record-link-note { display: inline-block; margin-top: 12px; }
  .field-grid { grid-template-columns: 1fr; }
  .field-block { grid-template-columns: 1fr; }
  .field-block > label { padding: 10px; border-right: 0; border-bottom: 1px solid var(--line-soft); }
  .control-wrap { padding: 8px; }
  .data-table-wrap { overflow: visible; border: 0; }
  .data-table { display: block; min-width: 0; }
  .data-table thead { display: none; }
  .data-table tbody, .data-table tr, .data-table td { display: block; width: 100% !important; }
  .data-table tr { margin-bottom: 14px; border: 1px solid var(--line); }
  .data-table td { display: grid; grid-template-columns: minmax(112px, 34%) minmax(0, 1fr); align-items: center; gap: 8px; border-right: 0; }
  .data-table td::before { content: attr(data-label); color: var(--navy); font-size: 11px; font-weight: 800; line-height: 1.3; text-transform: uppercase; }
  .data-table .control,
  .mini-field .control,
  .team-grid .mini-field .control,
  .risk-table textarea.control,
  .risk-table input.control { font-size: 16px; font-weight: 500; line-height: 1.5; }
  .data-table textarea.control,
  .risk-table textarea.control,
  .risk-table input.control { min-height: 72px; text-align: left; }
  .worksheet-meta { grid-template-columns: 1fr; }
  .worksheet-meta .mini-field.is-full { grid-column: auto; }
  .team-grid { grid-template-columns: 1fr; border-top: 1px solid var(--line); }
  .worksheet-tabs { overflow-x: auto; }
  .status-rail { grid-template-columns: 1fr; }
  .export-section { display: none; }
  .workflow-footer { display: grid; grid-template-columns: 1fr; gap: 7px; padding: 9px 12px; }
  .footer-status { text-align: center; }
  .footer-actions { display: grid; grid-template-columns: 1fr 1fr; }
  .button { min-height: 44px; }
}

@page portraitPage { size: A4 portrait; margin: 10mm 12mm 11mm; }
@page landscapePage { size: A4 landscape; margin: 8mm 10mm 9mm; }

@media print {
  html, body { background: var(--white) !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .app-shell, .toast, .modal-backdrop, .skip-link { display: none !important; }
  .print-workbook { display: block !important; }

  .print-page {
    page: portraitPage;
    position: relative;
    width: 100%;
    height: 276mm;
    overflow: hidden;
    break-after: page;
    color: #132033;
    background: #fff;
    font-family: Aptos, "Segoe UI", Arial, sans-serif;
    font-size: 8.2pt;
  }

  .print-page.landscape {
    page: landscapePage;
    height: 190mm;
    font-size: 6.8pt;
  }

  .print-page:last-child { break-after: auto; }

  .print-topline { display: flex; align-items: center; gap: 9px; margin-bottom: 5px; color: var(--navy); font-size: 7.5pt; font-weight: 800; }
  .print-topline span { color: var(--muted); font-weight: 500; }

  .print-gates { display: grid; grid-template-columns: repeat(6, 1fr); height: 7mm; margin-bottom: 4mm; }
  .print-gates div { display: grid; place-items: center; color: #fff; font-size: 5.7pt; font-weight: 800; }

  .print-title { margin: 0 0 3mm; color: var(--page-color, var(--teal)); font-size: 18pt; line-height: 1.08; }
  .print-subtitle { margin: -1mm 0 4mm; color: var(--muted); font-size: 9pt; }
  .print-section-title { margin: 3.2mm 0 2mm; color: var(--page-color, var(--teal)); font-size: 11pt; }

  .print-table { width: 100%; border-collapse: collapse; table-layout: fixed; }
  .print-table th { padding: 2.2mm 1.8mm; border: .3mm solid #fff; background: var(--page-color, var(--teal)); color: #fff; font-size: 6.5pt; text-align: left; text-transform: uppercase; }
  .print-table td { padding: 2mm 1.8mm; border: .25mm solid #9cafc2; vertical-align: middle; line-height: 1.28; }
  .print-table .label-cell { width: 24%; color: var(--navy); font-weight: 800; }
  .print-table.compact td { padding: 1.35mm 1.5mm; }
  .print-table.compact th { padding: 1.8mm 1.5mm; }
  .print-table.dense { font-size: 6.5pt; }
  .print-table.dense td { padding: 1.25mm; }
  .print-table.risk { font-size: 6pt; }
  .print-table.risk th { padding: 1.6mm .8mm; text-align: center; }
  .print-table.risk td { height: 88mm; padding: 1.4mm; vertical-align: top; }

  .print-pair-grid { display: grid; grid-template-columns: 1fr 1fr; border-top: .25mm solid #9cafc2; border-left: .25mm solid #9cafc2; }
  .print-pair { display: grid; grid-template-columns: 31% 1fr; min-height: 14mm; border-right: .25mm solid #9cafc2; border-bottom: .25mm solid #9cafc2; }
  .print-pair.full { grid-column: 1 / -1; min-height: 28mm; }
  .print-pair strong, .print-pair span { display: flex; align-items: center; padding: 2mm; line-height: 1.3; }
  .print-pair strong { border-right: .25mm solid #9cafc2; color: var(--navy); }

  .print-risk-meta { display: grid; grid-template-columns: 1.5fr .58fr 1.4fr .5fr; border-top: .25mm solid #8fa4bb; border-left: .25mm solid #8fa4bb; }
  .print-risk-meta div { padding: 1.4mm; border-right: .25mm solid #8fa4bb; border-bottom: .25mm solid #8fa4bb; }
  .print-risk-meta .wide { grid-column: 1 / 4; }
  .print-risk-meta b { display: block; margin-bottom: .8mm; color: var(--navy); font-size: 5.5pt; text-transform: uppercase; }

  .print-team { display: grid; grid-template-columns: 1fr 1.55fr 1fr 1.55fr; border-left: .25mm solid #8fa4bb; }
  .print-team div { min-height: 8mm; padding: 1mm 1.2mm; border-right: .25mm solid #8fa4bb; border-bottom: .25mm solid #8fa4bb; }
  .print-team b { display: block; margin-bottom: .7mm; color: var(--navy); font-size: 5.4pt; text-transform: uppercase; }

  .print-footer { position: absolute; right: 0; bottom: 0; left: 0; display: flex; justify-content: space-between; color: #596779; font-size: 6.2pt; }
  .print-disclaimer { position: absolute; right: 20%; bottom: 0; left: 20%; color: #c84630; font-size: 5.7pt; font-weight: 800; text-align: center; }
  .print-cover-title { margin: 16mm 0 4mm; color: var(--navy); font-size: 32pt; line-height: 1.2; letter-spacing: -.02em; }
  .print-cover-intro { margin: 0 0 8mm; color: var(--muted); font-size: 12pt; }
}
