/* Tokens: see docs/design.md. Do not invent numbers here. */
:root {
  --font-iowan: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-georgia: Georgia, "Times New Roman", serif;
  --font-system: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-avenir: "Avenir Next", Avenir, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --read: var(--font-iowan);
  --sans: var(--font-system);

  --t-xs: 12px; --t-sm: 14px; --t-md: 17px; --t-lg: 19px; --t-xl: 24px; --t-2xl: 32px; --t-3xl: 40px;

  --s-1: 4px; --s-2: 8px; --s-3: 12px; --s-4: 16px; --s-5: 24px; --s-6: 32px; --s-7: 48px; --s-8: 64px;

  --measure: 31em; --measure-min: 20em;
  --w-toc: 220px; --w-panel: 460px; --w-panel-min: 320px; --w-settings: 300px;
  --h-sheet: 72vh; --h-drop: 60vh;

  --paper: #faf8f3; --raised: #ffffff; --ink: #1e1d1a; --muted: #6b675f; --rule: #e2ddd2;
  --accent: #2f5d8a; --hover: rgba(47, 93, 138, 0.07); --selected: rgba(47, 93, 138, 0.12);
  --superseded: #8a5a1f; --repealed: #8a2f2f;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.12);
  --dur: 180ms;
  color-scheme: light;
}

/* ---------- Settings ---------- */
:root[data-font="georgia"] { --read: var(--font-georgia); }
:root[data-font="system"] { --read: var(--font-system); }
:root[data-font="avenir"] { --read: var(--font-avenir); }

:root[data-size="s"] { --t-lg: 17px; --t-md: 15px; }
:root[data-size="l"] { --t-lg: 21px; --t-md: 18px; }
:root[data-size="xl"] { --t-lg: 24px; --t-md: 20px; }

:root[data-measure="narrow"] { --measure: 27em; }
:root[data-measure="wide"] { --measure: 36em; }

:root[data-panel="narrow"] { --w-panel: 380px; }
:root[data-panel="wide"] { --w-panel: 560px; }

:root[data-theme="dark"] {
  --paper: #15171b; --raised: #1d2026; --ink: #e7e3da; --muted: #9b968c; --rule: #2c3038;
  --accent: #8fb4dc; --hover: rgba(143, 180, 220, 0.10); --selected: rgba(143, 180, 220, 0.16);
  --superseded: #d9a35a; --repealed: #e08383;
  --shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #15171b; --raised: #1d2026; --ink: #e7e3da; --muted: #9b968c; --rule: #2c3038;
    --accent: #8fb4dc; --hover: rgba(143, 180, 220, 0.10); --selected: rgba(143, 180, 220, 0.16);
    --superseded: #d9a35a; --repealed: #e08383;
    --shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

@media (prefers-reduced-motion: reduce) {
  :root { --dur: 0ms; }
  html { scroll-behavior: auto !important; }
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: var(--s-7); }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--read);
  font-size: var(--t-lg);
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
button { font: inherit; color: inherit; }

/* ---------- Layout ---------- */
.page {
  display: grid;
  grid-template-columns: var(--w-toc) minmax(0, var(--measure));
  gap: var(--s-7);
  max-width: calc(var(--w-toc) + var(--measure) + var(--s-7) + var(--s-5) * 2);
  margin: 0 auto;
  padding: 0 var(--s-5);
}
.doc { min-width: 0; padding-bottom: var(--s-8); }

/* ---------- Header ---------- */
.masthead { padding: var(--s-7) 0 var(--s-5); border-bottom: 1px solid var(--rule); margin-bottom: var(--s-6); }
.masthead h1 { font-size: var(--t-3xl); line-height: 1.1; font-weight: 500; margin: 0 0 var(--s-3); letter-spacing: -0.01em; }
.masthead p { margin: 0; font-family: var(--sans); font-size: var(--t-sm); line-height: 1.5; color: var(--muted); }

/* ---------- Left column: settings + contents ---------- */
.side {
  position: sticky; top: 0; align-self: start;
  height: 100vh;
  display: flex; flex-direction: column;
  padding-top: var(--s-6);
  font-family: var(--sans); font-size: var(--t-sm); line-height: 1.5;
  z-index: 11; /* above the panel (10) so the settings popover is never covered */
}
.side-head { position: relative; margin-bottom: var(--s-5); }
.side-head button {
  display: inline-flex; align-items: center; gap: var(--s-2);
  background: none; border: 0; cursor: pointer;
  padding: var(--s-1) var(--s-2); margin-left: calc(-1 * var(--s-2));
  border-radius: var(--s-1);
  color: var(--muted);
}
.side-head button:hover, .side-head button[aria-expanded="true"] { background: var(--hover); color: var(--ink); }
.side-head button:focus-visible { outline: 2px solid var(--accent); }
.side-head svg { width: var(--s-4); height: var(--s-4); flex: none; }
.toc-toggle { display: none !important; }

.toc { flex: 1; min-height: 0; overflow-y: auto; padding-bottom: var(--s-6); }
.toc h2 { font-size: var(--t-xs); text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; margin: var(--s-5) 0 var(--s-2); }
.toc h2:first-of-type { margin-top: 0; }
.toc ol { list-style: none; margin: 0 0 0 calc(-1 * var(--s-2)); padding: 0; }
.toc li a { display: block; padding: var(--s-1) var(--s-2); color: var(--ink); text-decoration: none; border-radius: var(--s-1); white-space: nowrap; }
.toc li a:hover { background: var(--hover); }
.toc li a[aria-current="true"] { color: var(--accent); font-weight: 600; }
.toc .amend-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.toc .amend-grid a { padding-right: var(--s-1); }

/* ---------- Settings popover ---------- */
.settings {
  position: absolute; top: calc(100% + var(--s-2)); left: calc(-1 * var(--s-2));
  width: var(--w-settings);
  background: var(--raised); color: var(--ink);
  border: 1px solid var(--rule); border-radius: var(--s-2);
  box-shadow: var(--shadow);
  padding: var(--s-4);
  z-index: 20;
}
.settings fieldset { border: 0; margin: 0 0 var(--s-4); padding: 0; min-width: 0; }
.settings fieldset:last-child { margin-bottom: 0; }
.settings legend { font-size: var(--t-xs); text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; padding: 0; margin-bottom: var(--s-2); }
.seg {
  display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr);
  border: 1px solid var(--rule); border-radius: var(--s-1);
  overflow: hidden;
}
.seg.grid2 { grid-auto-flow: row; grid-template-columns: 1fr 1fr; }
.seg label { position: relative; display: block; cursor: pointer; }
.seg label + label { border-left: 1px solid var(--rule); }
.seg.grid2 label:nth-child(2n+1) { border-left: 0; }
.seg.grid2 label:nth-child(n+3) { border-top: 1px solid var(--rule); }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: flex; align-items: center; justify-content: center;
  min-height: var(--s-6); padding: var(--s-1) var(--s-2);
  text-align: center; white-space: nowrap;
}
.seg label:hover span { background: var(--hover); }
.seg input:checked + span { background: var(--selected); color: var(--accent); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.seg .a1 { font-size: var(--t-xs); } .seg .a2 { font-size: var(--t-sm); } .seg .a3 { font-size: var(--t-md); } .seg .a4 { font-size: var(--t-lg); }

/* ---------- Document ---------- */
.doc h2 { font-size: var(--t-2xl); line-height: 1.2; font-weight: 500; margin: var(--s-8) 0 var(--s-4); }
.doc h2:first-child { margin-top: 0; }
.doc h3 { font-size: var(--t-xl); line-height: 1.3; font-weight: 500; margin: var(--s-6) 0 var(--s-3); }
/* Article headings open an overview; they read as plain headings until hovered */
.doc .head-btn {
  display: block; width: calc(100% + var(--s-3) * 2);
  margin: 0 calc(-1 * var(--s-3)); padding: 0 var(--s-3);
  font: inherit; color: inherit; text-align: left;
  background: none; border: 0; cursor: pointer;
  transition: background-color var(--dur) ease-out;
}
.doc .head-btn:hover { background: var(--hover); }
.doc .head-btn:focus-visible { outline: 2px solid var(--accent); }
.doc .head-btn[aria-expanded="true"] { background: var(--selected); box-shadow: inset 3px 0 0 var(--accent); }
.doc .head-btn[aria-expanded="true"]:focus-visible { outline: none; }
.doc .dates { font-family: var(--sans); font-size: var(--t-sm); color: var(--muted); margin: calc(-1 * var(--s-3)) 0 var(--s-4); }

.unit {
  position: relative;
  margin: 0 calc(-1 * var(--s-3)) var(--s-4);
  padding: var(--s-1) var(--s-3);
  cursor: pointer;
  white-space: pre-line; /* keeps paragraph breaks inside multi-paragraph units */
  transition: background-color var(--dur) ease-out;
}
.unit:hover { background: var(--hover); }
.unit:focus-visible { outline: 2px solid var(--accent); outline-offset: 0; }
.unit[aria-expanded="true"] { background: var(--selected); box-shadow: inset 3px 0 0 var(--accent); }
.unit[aria-expanded="true"]:focus-visible { outline: none; }

.unit .n {
  position: absolute; left: calc(-1 * var(--s-6)); top: var(--s-1);
  width: var(--s-5); text-align: right;
  font-family: var(--sans); font-size: var(--t-xs); line-height: 1.65; color: var(--muted);
  user-select: none;
  font-variant-numeric: tabular-nums;
}

.unit.superseded, .unit.repealed { opacity: 0.7; }
.chip {
  display: inline-block; vertical-align: middle;
  font-family: var(--sans); font-size: var(--t-xs); line-height: 1.4; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.06em;
  border: 1px solid currentColor; border-radius: var(--s-1);
  padding: 0 var(--s-1); margin-right: var(--s-2);
  white-space: nowrap;
}
.chip.superseded { color: var(--superseded); }
.chip.repealed { color: var(--repealed); }

.signers { font-family: var(--sans); font-size: var(--t-sm); line-height: 1.5; margin-top: var(--s-4); columns: 2; column-gap: var(--s-5); }
.signers div { break-inside: avoid; margin-bottom: var(--s-3); }
.signers strong { display: block; font-weight: 600; }

/* ---------- Panel ---------- */
.panel {
  position: fixed; top: 0; right: 0; bottom: 0;
  width: var(--w-panel); max-width: 100%;
  background: var(--paper);
  border-left: 1px solid var(--rule);
  box-shadow: var(--shadow);
  overflow-y: auto;
  padding: var(--s-6) var(--s-5) var(--s-7);
  font-family: var(--sans); font-size: var(--t-md); line-height: 1.65;
  transform: translateX(100%);
  visibility: hidden;
  transition: transform var(--dur) ease-out, visibility 0s linear var(--dur);
  z-index: 10;
}
.panel.open { transform: none; visibility: visible; transition: transform var(--dur) ease-out; }

.panel .close {
  position: absolute; top: var(--s-5); right: var(--s-3);
  width: var(--s-6); height: var(--s-6);
  border: 0; border-radius: 50%; background: transparent; cursor: pointer;
  font-size: var(--t-xl); line-height: 1; color: var(--muted);
}
.panel .close:hover { background: var(--hover); color: var(--ink); }
.panel .close:focus-visible { outline: 2px solid var(--accent); }

.panel .kicker { font-size: var(--t-sm); color: var(--muted); margin: 0 var(--s-6) var(--s-1) 0; }
.panel h2 { font-family: var(--read); font-size: var(--t-xl); line-height: 1.3; font-weight: 500; margin: 0 var(--s-6) var(--s-4) 0; }
.panel .status { margin: 0 0 var(--s-2); }
.panel .status .chip { margin: 0 0 var(--s-2); }
.panel .status p { margin: 0; font-size: var(--t-sm); line-height: 1.5; color: var(--muted); }
.panel h3 { font-size: var(--t-xs); text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; margin: var(--s-5) 0 var(--s-2); }
.panel p { margin: 0 0 var(--s-3); }
.panel ul { margin: 0; padding-left: var(--s-4); }
.panel li { margin-bottom: var(--s-1); }
.panel .foot { font-size: var(--t-xs); border-top: 1px solid var(--rule); padding-top: var(--s-3); margin-top: var(--s-5); }
.panel .foot button { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font-size: var(--t-xs); }
.panel .handle { display: none; }
.panel .empty { display: none; }

/* ---------- Wide: panel gets its own column ---------- */
@media (min-width: 1180px) {
  .page {
    grid-template-columns: var(--w-toc) minmax(var(--measure-min), var(--measure)) minmax(var(--w-panel-min), var(--w-panel));
    max-width: calc(var(--w-toc) + var(--measure) + var(--w-panel) + var(--s-7) * 2 + var(--s-5) * 2);
  }
  .panel {
    position: sticky; top: 0; align-self: start;
    height: 100vh; width: auto;
    border-left: 0; box-shadow: none;
    padding-left: 0;
    transform: none; visibility: visible;
    transition: none;
  }
  .panel:not(.open) .inner, .panel:not(.open) .close { display: none; }
  .panel:not(.open) .empty { display: block; color: var(--muted); font-size: var(--t-sm); padding-top: var(--s-7); }
}

/* ---------- Mobile: contents and settings in a top bar; panel is a bottom sheet ---------- */
@media (max-width: 819px) {
  .page { grid-template-columns: minmax(0, 1fr); gap: 0; padding: 0 var(--s-4); }
  .side {
    height: auto; padding: 0;
    margin: 0 calc(-1 * var(--s-4));
    background: var(--paper); border-bottom: 1px solid var(--rule);
  }
  .side-head { display: flex; justify-content: space-between; align-items: center; margin: 0; padding: var(--s-2) var(--s-4); }
  .side-head button { margin-left: 0; }
  .toc-toggle { display: inline-flex !important; color: var(--ink) !important; font-weight: 600; }
  .settings { left: auto; right: var(--s-4); width: min(var(--w-settings), calc(100vw - var(--s-4) * 2)); }
  .settings .not-mobile { display: none; }
  .toc { display: none; flex: none; max-height: var(--h-drop); padding: 0 var(--s-4) var(--s-4); }
  .side.toc-open .toc { display: block; }
  .toc h2:first-of-type { margin-top: var(--s-2); }
  .masthead { padding-top: var(--s-5); }
  .unit .n { position: static; display: inline-block; width: auto; margin-right: var(--s-2); }
  .unit .n::after { content: "."; }
  .signers { columns: 1; }

  .panel {
    top: auto; left: 0; right: 0; bottom: 0;
    width: auto; max-height: var(--h-sheet);
    border-left: 0; border-top: 1px solid var(--rule);
    border-radius: var(--s-4) var(--s-4) 0 0;
    transform: translateY(100%);
  }
  .panel .handle {
    display: block; position: absolute; top: var(--s-2); left: 50%;
    width: var(--s-7); height: var(--s-1); margin-left: calc(-1 * var(--s-5));
    border-radius: var(--s-1); background: var(--rule);
  }
}

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
