/* --- themes --- */
:root,
[data-theme="light"] {
  color-scheme: light;
  --desk: #ddd9d2;
  --paper: #f0ede8;
  --sheet: #fffffe;
  --panel: #f5f3ef;
  --ink: #1a1a18;
  --muted: #4a4844;
  --rule: #c8c2b8;
  --rule-strong: #1a1a18;
  --link: #0b4a75;
  --link-hover: #083558;
  --nav-active: #ebe8e2;
  --badge-ok-bg: #d8eadc;
  --badge-ok-fg: #0f3d1a;
  --badge-ok-border: #6b9e74;
  --focus: #0b4a75;
  --skip-bg: #1a1a18;
  --skip-fg: #fffffe;
  --shadow: 0 2px 4px rgba(26, 26, 24, 0.06), 0 16px 48px rgba(26, 26, 24, 0.1);
}

[data-theme="dim"] {
  color-scheme: light;
  --desk: #b5afa4;
  --paper: #ccc6bc;
  --sheet: #ebe7e0;
  --panel: #e0dbd3;
  --ink: #1a1917;
  --muted: #3d3b36;
  --rule: #a39d92;
  --rule-strong: #1a1917;
  --link: #094264;
  --link-hover: #062f48;
  --nav-active: #d4cec4;
  --badge-ok-bg: #a8c8ae;
  --badge-ok-fg: #0a3318;
  --badge-ok-border: #4a7a54;
  --focus: #094264;
  --skip-bg: #1a1917;
  --skip-fg: #ebe7e0;
  --shadow: 0 2px 4px rgba(26, 25, 23, 0.08), 0 18px 52px rgba(26, 25, 23, 0.14);
}

[data-theme="dark"] {
  color-scheme: dark;
  --desk: #0a0a09;
  --paper: #121211;
  --sheet: #1c1c1a;
  --panel: #242422;
  --ink: #f0eeea;
  --muted: #b8b4ac;
  --rule: #3d3b38;
  --rule-strong: #d8d4cc;
  --link: #8ec8f0;
  --link-hover: #b8e0ff;
  --nav-active: #2a2a28;
  --badge-ok-bg: #1a3d28;
  --badge-ok-fg: #b8e8c4;
  --badge-ok-border: #4a8a5c;
  --focus: #8ec8f0;
  --skip-bg: #f0eeea;
  --skip-fg: #121211;
  --shadow: 0 2px 6px rgba(0, 0, 0, 0.25), 0 20px 56px rgba(0, 0, 0, 0.4);
}

@media (prefers-contrast: more) {
  :root,
  [data-theme="light"],
  [data-theme="dim"] {
    --muted: #1a1816;
    --rule: #5a5650;
  }
  [data-theme="dark"] {
    --muted: #f0eeea;
    --rule: #8a8680;
  }
}

:root {
  --font-scale: 1;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
  --serif: 'Literata', Georgia, 'Times New Roman', serif;
  --measure: 42rem;
  --sidebar: 14.5rem;
  --pad-x: 3rem;
  --pad-y: 1.75rem;
  --body-size: 0.9375rem;
  --heading-track: 0.08em;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: calc(100% * var(--font-scale));
  scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
}

body {
  display: grid;
  grid-template-columns: var(--sidebar) 1fr;
  min-height: 100vh;
  background: var(--desk);
  color: var(--ink);
  font-family: var(--serif);
  font-size: var(--body-size);
  line-height: 1.65;
}

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

.skip-link {
  position: fixed;
  top: 0.5rem;
  left: 0.5rem;
  z-index: 100;
  padding: 0.6rem 1rem;
  background: var(--skip-bg);
  color: var(--skip-fg);
  font-family: var(--mono);
  font-size: 0.75rem;
  text-decoration: none;
  transform: translateY(-200%);
  transition: transform 0.15s;
}

.skip-link:focus {
  transform: translateY(0);
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

a:focus-visible,
button:focus-visible,
input:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

a {
  color: var(--link);
  text-decoration: underline;
  text-underline-offset: 0.12em;
}

a:hover { color: var(--link-hover); }

.toc a,
.meta-table a,
.back,
.theme-option span {
  text-decoration: none;
}

.contract-table a {
  text-decoration: underline;
}

/* --- sidebar --- */
.toc {
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
  padding: 1.5rem 1rem 1.75rem;
  background: var(--paper);
  border-right: 1px solid var(--rule);
  font-family: var(--mono);
  font-size: 0.65rem;
  display: flex;
  flex-direction: column;
}

.back {
  color: var(--muted);
  margin-bottom: 0.5rem;
  line-height: 1.8;
}

.back:hover { color: var(--link); }

.toc-doc-id {
  letter-spacing: 0.12em;
  color: var(--muted);
  margin-bottom: 0.65rem;
}

.toc-title {
  font-size: 0.6rem;
  font-weight: 500;
  letter-spacing: var(--heading-track);
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.45rem;
  padding-bottom: 0.45rem;
  border-bottom: 1px solid var(--rule);
}

.toc nav a {
  display: grid;
  grid-template-columns: 1.35rem 1fr;
  gap: 0.4rem;
  align-items: center;
  color: var(--ink);
  padding: 0.35rem 0.3rem;
  line-height: 1.35;
  min-height: 2.25rem;
}

.toc-num {
  color: var(--muted);
  text-align: right;
}

.toc nav a:hover,
.toc nav a:hover .toc-num { color: var(--link); }

.toc nav a.is-active {
  font-weight: 600;
  background: var(--nav-active);
  outline: 1px solid var(--rule);
}

.toc nav a.is-active .toc-num { color: var(--ink); }

.prefs {
  margin-top: auto;
  padding-top: 0.85rem;
  border-top: 1px solid var(--rule);
}

.prefs-heading {
  font-size: 0.58rem;
  letter-spacing: var(--heading-track);
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.45rem;
}

.theme-field { border: 0; margin: 0 0 0.55rem; padding: 0; }
.theme-legend { font-size: 0.58rem; color: var(--muted); margin-bottom: 0.25rem; }
.theme-options { display: flex; gap: 0.15rem; }
.theme-option { flex: 1; cursor: pointer; }
.theme-option input { position: absolute; opacity: 0; width: 0; height: 0; }

.theme-option span {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 2.25rem;
  border: 1px solid var(--rule);
  background: var(--sheet);
  color: var(--ink);
}

.theme-option input:checked + span {
  font-weight: 600;
  background: var(--nav-active);
  outline: 1px solid var(--rule-strong);
}

.font-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.25rem 0.4rem;
}

.font-label { width: 100%; font-size: 0.58rem; color: var(--muted); }
.font-buttons { display: flex; gap: 0.15rem; }

.font-btn {
  font-family: var(--mono);
  font-size: 0.68rem;
  min-width: 2.25rem;
  min-height: 2.25rem;
  border: 1px solid var(--rule);
  background: var(--sheet);
  color: var(--ink);
  cursor: pointer;
}

.font-btn:hover:not(:disabled) { background: var(--nav-active); }
.font-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.font-scale-readout { font-size: 0.58rem; color: var(--muted); }

.meta {
  margin-top: 0.55rem;
  color: var(--muted);
  line-height: 1.5;
  font-size: 0.58rem;
}

/* --- instrument (contract page) --- */
.main {
  min-width: 0;
  background: var(--desk);
}

.desk {
  padding: 2rem clamp(1rem, 3vw, 2.5rem) 3rem;
  min-height: 100vh;
}

.instrument {
  max-width: var(--measure);
  margin: 0 auto;
  background: var(--sheet);
  border: 1px solid var(--rule-strong);
  box-shadow: var(--shadow);
}

/* letterhead */
.letterhead {
  padding: 2.25rem var(--pad-x) 1.75rem;
  text-align: center;
  border-bottom: 1px solid var(--rule-strong);
}

.doc-class {
  font-family: var(--mono);
  font-size: 0.65rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 0.75rem;
}

.letterhead h1 {
  font-size: 1.5rem;
  font-weight: 600;
  line-height: 1.3;
  letter-spacing: 0.01em;
  text-transform: uppercase;
  margin-bottom: 0.75rem;
}

.doc-lead {
  font-size: 0.875rem;
  color: var(--muted);
  line-height: 1.55;
  max-width: 36rem;
  margin: 0 auto 1.25rem;
}

.meta-table {
  width: 100%;
  max-width: 22rem;
  margin: 0 auto;
  border-collapse: collapse;
  font-size: 0.8125rem;
  text-align: left;
}

.meta-table th,
.meta-table td {
  border: 1px solid var(--rule);
  padding: 0.4rem 0.65rem;
  vertical-align: top;
}

.meta-table th {
  width: 6.5rem;
  font-family: var(--mono);
  font-size: 0.6rem;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--panel);
}

.meta-table td {
  font-family: var(--mono);
  font-size: 0.75rem;
}

.badge {
  font-family: var(--mono);
  font-size: 0.58rem;
  padding: 0.1rem 0.35rem;
  background: var(--badge-ok-bg);
  color: var(--badge-ok-fg);
  border: 1px solid var(--badge-ok-border);
  letter-spacing: 0.05em;
}

/* shared section rhythm */
.preamble,
.article {
  padding: var(--pad-y) var(--pad-x);
  border-top: 1px solid var(--rule);
}

.clause-anchor {
  scroll-margin-top: 1.25rem;
}

.section-heading {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  font-family: var(--mono);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: var(--heading-track);
  text-transform: uppercase;
  color: var(--ink);
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--rule);
}

.section-num {
  flex-shrink: 0;
  min-width: 1.75rem;
  color: var(--muted);
}

.preamble p,
.article > p,
.subsection p {
  margin-bottom: 0.75rem;
}

.preamble p:last-child,
.article > p:last-of-type:not(.section-lead),
.subsection p:last-child {
  margin-bottom: 0;
}

.section-lead {
  color: var(--muted);
  font-size: 0.875rem;
  margin-bottom: 1rem !important;
}

/* subsections: uniform 2.x / 3.x numbering */
.subsections {
  display: flex;
  flex-direction: column;
  gap: 0;
  border: 1px solid var(--rule);
}

.subsection {
  padding: 0.85rem 1rem 0.85rem 1rem;
  border-top: 1px solid var(--rule);
  display: grid;
  grid-template-columns: 2.5rem 1fr;
  gap: 0.65rem 1rem;
  align-items: start;
}

.subsections .subsection:first-child {
  border-top: none;
}

.article[data-article="2"] .subsections {
  counter-reset: sub;
}

.article[data-article="2"] .subsection {
  counter-increment: sub;
}

.article[data-article="2"] .subsection::before {
  content: "2." counter(sub);
  font-family: var(--mono);
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--muted);
  padding-top: 0.15rem;
}

.article[data-article="3"] .subsections {
  counter-reset: sub;
}

.article[data-article="3"] .subsection {
  counter-increment: sub;
}

.article[data-article="3"] .subsection::before {
  content: "3." counter(sub);
  font-family: var(--mono);
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--muted);
  padding-top: 0.15rem;
}

.subsection-title {
  font-family: var(--serif);
  font-size: var(--body-size);
  font-weight: 600;
  line-height: 1.4;
  margin: 0;
  grid-column: 2;
  grid-row: 1;
}

.subsection > p {
  grid-column: 2;
  grid-row: 2;
  margin: 0;
  font-size: 0.875rem;
  color: var(--ink);
}

.subsection::before {
  grid-column: 1;
  grid-row: 1 / -1;
}

.xref {
  font-size: 0.8125rem;
  color: var(--muted);
}

.xref a {
  font-family: var(--mono);
  font-size: 0.75rem;
}

/* contract tables */
.contract-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
  margin-top: 0.25rem;
}

.contract-table th,
.contract-table td {
  border: 1px solid var(--rule);
  padding: 0.5rem 0.75rem;
  text-align: left;
  vertical-align: top;
}

.contract-table thead th {
  font-family: var(--mono);
  font-size: 0.6rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--panel);
}

.contract-table tbody th {
  width: 9rem;
  font-family: var(--mono);
  font-size: 0.6rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  background: var(--panel);
}

.contract-table td {
  font-size: 0.875rem;
}

.contract-table--contact td a {
  font-family: var(--mono);
  font-size: 0.8125rem;
}

/* final article */
.article--final {
  border-top: 3px double var(--rule-strong);
}

.instrument-footer {
  margin-top: 1.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--rule);
  font-family: var(--mono);
  font-size: 0.65rem;
  color: var(--muted);
  line-height: 1.65;
  text-align: center;
}

.instrument-footer p + p {
  margin-top: 0.2rem;
}

/* responsive */
@media (max-width: 860px) {
  :root {
    --pad-x: 1.5rem;
    --pad-y: 1.35rem;
  }

  body { grid-template-columns: 1fr; }

  .toc {
    position: static;
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--rule);
  }

  .toc nav {
    display: flex;
    flex-wrap: wrap;
    gap: 0.2rem;
  }

  .toc nav a {
    min-height: auto;
    padding: 0.3rem 0.5rem;
  }

  .meta { display: none; }

  .desk { padding: 1rem 0.75rem 2rem; }

  .subsection {
    grid-template-columns: 2rem 1fr;
    gap: 0.35rem 0.65rem;
  }
}

@media (max-width: 480px) {
  .letterhead h1 { font-size: 1.2rem; }

  .subsection {
    grid-template-columns: 1fr;
    padding-left: 0.85rem;
  }

  .subsection::before {
    grid-column: 1;
    grid-row: auto;
    margin-bottom: 0.25rem;
  }

  .subsection-title,
  .subsection > p {
    grid-column: 1;
  }
}

@media print {
  .skip-link,
  .prefs,
  .back { display: none !important; }

  body { display: block; background: #fff; }
  .desk { padding: 0; }
  .instrument { box-shadow: none; border: none; max-width: none; }
}
