:root {
  --ink: #12161c;
  --ink-soft: #4a5461;
  --ink-faint: #7b8794;
  --line: #e2e6ec;
  --line-strong: #cdd4de;
  --paper: #ffffff;
  --canvas: #f4f6f9;
  --accent: #143a5a;
  --accent-soft: #e8eef5;
  --warn: #8a5a00;
  --warn-bg: #fdf6e6;
  --info: #1d4e6b;
  --info-bg: #eef5fa;
  --ok: #1f6b4a;
  --radius: 10px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  color: var(--ink);
  background: var(--canvas);
  -webkit-font-smoothing: antialiased;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 12px; }

.mark {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 12px;
  letter-spacing: .04em;
}

.brand-text h1 {
  margin: 0;
  font-size: 16px;
  letter-spacing: -.01em;
}

.brand-text p {
  margin: 1px 0 0;
  font-size: 12px;
  color: var(--ink-faint);
}

.actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.sep {
  width: 1px;
  height: 22px;
  background: var(--line);
  margin: 0 2px;
}

.btn {
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 13px;
  border-radius: 8px;
  border: 1px solid var(--line-strong);
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease;
}

.btn:hover { background: #f7f9fc; border-color: #b9c2cf; }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #0f2d46; }

/* ---------- layout ---------- */

main {
  display: grid;
  grid-template-columns: minmax(340px, 40%) minmax(420px, 60%);
  gap: 18px;
  padding: 18px 20px 8px;
  align-items: start;
  max-width: 1600px;
  margin: 0 auto;
}

.panel {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.form-panel { padding: 6px 18px 22px; }

.doc-panel { display: flex; flex-direction: column; overflow: hidden; }

.doc-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  background: #fbfcfe;
}

.spacer { flex: 1; }

.pill {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .02em;
  padding: 4px 9px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid #d5e0ec;
}

.pill.nonbinding { background: var(--warn-bg); color: var(--warn); border-color: #f0e2c4; }

.word-count { font-size: 11px; color: var(--ink-faint); }

.paper { padding: 22px; background: #eef1f5; }

.doc {
  background: var(--paper);
  padding: 54px 58px 60px;
  border: 1px solid var(--line);
  box-shadow: 0 1px 2px rgba(18, 22, 28, .04), 0 12px 28px rgba(18, 22, 28, .05);
  font-family: var(--serif);
  font-size: 14.2px;
  line-height: 1.62;
  color: #171b21;
  max-height: 78vh;
  overflow: auto;
}

/* ---------- checks ---------- */

.checks { padding: 16px 0 2px; }

.checks h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--ink-faint);
  margin: 0 0 10px;
}

.check {
  display: flex;
  gap: 9px;
  align-items: flex-start;
  font-size: 12.6px;
  line-height: 1.5;
  padding: 9px 11px;
  border-radius: 8px;
  margin-bottom: 7px;
  border: 1px solid transparent;
}

.check.warn { background: var(--warn-bg); border-color: #f0e2c4; color: #6b4700; }
.check.info { background: var(--info-bg); border-color: #d7e6f1; color: #17415a; }
.check.ok { background: #f1f8f4; border-color: #d6eade; color: var(--ok); }
.check .dot { font-size: 13px; line-height: 1.4; }

/* ---------- form ---------- */

.section {
  border-bottom: 1px solid var(--line);
  padding: 16px 0 4px;
}

.section:last-child { border-bottom: none; }

.section > header {
  display: flex;
  align-items: baseline;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}

.section > header h2 {
  font-size: 13px;
  margin: 0;
  letter-spacing: -.005em;
}

.section > header .num {
  font-size: 11px;
  color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
  min-width: 16px;
}

.section > header .chev {
  margin-left: auto;
  color: var(--ink-faint);
  font-size: 11px;
  transition: transform .15s ease;
}

.section.collapsed > header .chev { transform: rotate(-90deg); }
.section.collapsed .section-body { display: none; }

.section-body { padding-top: 12px; display: grid; gap: 12px; }

.section .hint {
  font-size: 12px;
  color: var(--ink-faint);
  margin: -2px 0 2px;
  line-height: 1.5;
}

.field { display: grid; gap: 5px; }

.field > label {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
}

.field .help { font-size: 11.5px; color: var(--ink-faint); line-height: 1.45; }

input[type="text"], input[type="date"], input[type="number"], select, textarea {
  font: inherit;
  font-size: 13.5px;
  padding: 8px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  width: 100%;
}

textarea { resize: vertical; min-height: 76px; line-height: 1.55; }

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(20, 58, 90, .12);
}

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.row.three { grid-template-columns: 1fr 1fr 1fr; }

.checks-grid { display: grid; gap: 7px; }

.checkbox {
  display: flex;
  gap: 9px;
  align-items: flex-start;
  font-size: 12.8px;
  line-height: 1.45;
  color: var(--ink-soft);
}

.checkbox input { margin-top: 2px; accent-color: var(--accent); }

.total-line {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 12.5px;
  padding: 9px 11px;
  background: var(--accent-soft);
  border-radius: 8px;
  color: var(--accent);
  font-weight: 600;
}

/* ---------- document typography ---------- */

.doc h1 {
  font-size: 19px;
  text-align: center;
  letter-spacing: .08em;
  text-transform: uppercase;
  margin: 0 0 4px;
  font-weight: 600;
}

.doc .doc-sub {
  text-align: center;
  font-size: 12.5px;
  color: var(--ink-soft);
  margin: 0 0 26px;
  font-style: italic;
}

.doc h2 {
  font-size: 13.4px;
  margin: 22px 0 7px;
  font-weight: 700;
}

.doc h2 .clause-no { margin-right: 7px; }

.doc p { margin: 0 0 9px; text-align: left; }

.doc p.lead { margin-bottom: 14px; }

.doc .deflist { margin: 0 0 10px; }

.doc .defrow { display: grid; grid-template-columns: 168px 1fr; gap: 10px; margin-bottom: 5px; }

.doc .defrow .k { font-weight: 600; }

.doc ol.subs { list-style: none; margin: 4px 0 10px; padding: 0; }

.doc ol.subs > li { display: grid; grid-template-columns: 30px 1fr; gap: 6px; margin-bottom: 5px; }

.doc ol.subs > li > .lbl { font-weight: 600; }

.doc ul.tight { margin: 2px 0 10px; padding-left: 20px; }
.doc ul.tight li { margin-bottom: 4px; }

.doc table {
  width: 100%;
  border-collapse: collapse;
  margin: 6px 0 12px;
  font-size: 13.2px;
}

.doc table th, .doc table td {
  border: 1px solid #d9dee6;
  padding: 6px 9px;
  text-align: left;
  vertical-align: top;
}

.doc table th { background: #f6f8fb; font-weight: 600; font-size: 12.4px; }
.doc td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.doc tr.total td { font-weight: 700; background: #f9fbfd; }

.doc .legend {
  border: 1px solid var(--line-strong);
  background: #fbfcfe;
  padding: 11px 13px;
  margin: 0 0 20px;
  font-size: 12.6px;
  line-height: 1.55;
}

.doc .legend strong { display: block; margin-bottom: 3px; }

.doc .sig { margin-top: 26px; }

.doc .sigrow {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 26px;
  margin-bottom: 22px;
  page-break-inside: avoid;
}

.doc .sigblock .party { font-weight: 600; margin-bottom: 14px; }
.doc .sigline { border-bottom: 1px solid #9aa4b1; height: 26px; margin-bottom: 4px; }
.doc .sigmeta { font-size: 12px; color: var(--ink-soft); }

.doc .docfoot {
  margin-top: 30px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  font-size: 11.4px;
  color: var(--ink-faint);
  font-family: var(--sans);
}

.doc .muted { color: var(--ink-faint); font-style: italic; }

/* ---------- footer + mobile ---------- */

.page-foot {
  max-width: 1600px;
  margin: 0 auto;
  padding: 6px 22px 26px;
}

.page-foot p {
  font-size: 11.5px;
  color: var(--ink-faint);
  line-height: 1.55;
  margin: 0;
  max-width: 900px;
}

.mobile-tabs { display: none; }

@media (max-width: 1080px) {
  main { grid-template-columns: 1fr; }
  .mobile-tabs {
    display: flex;
    gap: 6px;
    padding: 10px 20px 0;
  }
  .tab {
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    padding: 8px 14px;
    border-radius: 8px 8px 0 0;
    border: 1px solid var(--line);
    border-bottom: none;
    background: #e9edf3;
    color: var(--ink-soft);
    cursor: pointer;
  }
  .tab.is-active { background: var(--paper); color: var(--ink); }
  main.view-edit .doc-panel { display: none; }
  main.view-read .form-panel { display: none; }
  .doc { padding: 28px 22px; max-height: none; }
  .paper { padding: 12px; }
}

@media (max-width: 560px) {
  .row, .row.three { grid-template-columns: 1fr; }
  .doc .sigrow { grid-template-columns: 1fr; }
  .doc .defrow { grid-template-columns: 1fr; gap: 2px; }
  .topbar { padding: 10px 12px; }
  main { padding: 12px; }
}

/* ---------- print ---------- */

@media print {
  html, body { height: auto !important; overflow: visible !important; background: #fff; }
  .topbar, .mobile-tabs, .form-panel, .doc-bar, .page-foot, .checks { display: none !important; }
  main, main.view-edit, main.view-read {
    display: block !important;
    grid-template-columns: 1fr !important;
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    padding: 0 !important;
    max-width: none;
  }
  .doc-panel {
    display: block !important;
    border: none !important;
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
  }
  .paper { display: block !important; padding: 0 !important; background: #fff; overflow: visible !important; }
  .doc {
    display: block !important;
    border: none;
    box-shadow: none;
    padding: 0;
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    font-size: 11.4pt;
    line-height: 1.5;
    color: #000;
  }
  .doc h1 { font-size: 15pt; }
  .doc h2 { font-size: 11.6pt; page-break-after: avoid; }
  .doc table { page-break-inside: avoid; }
  .doc .sigrow { page-break-inside: avoid; }
  @page { margin: 20mm 18mm; }
}
