:root {
  color-scheme: light dark;
  --background: #d3d9e0;
  --heading: #1f2933;
  --text: #3a4654;
  --rule: #c1c8d1;
  --rule-strong: #9ca6b3;
  --link: #2a4a70;
  --accent: #355f8e;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --background: #171b21;
    --heading: #d6dce4;
    --text: #b3bcc7;
    --rule: #29303a;
    --rule-strong: #46505d;
    --link: #c9d3df;
    --accent: #7fa6d6;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --background: #171b21;
  --heading: #d6dce4;
  --text: #b3bcc7;
  --rule: #29303a;
  --rule-strong: #46505d;
  --link: #c9d3df;
  --accent: #7fa6d6;
}
:root[data-theme="light"] {
  color-scheme: light;
  --background: #d3d9e0;
  --heading: #1f2933;
  --text: #3a4654;
  --rule: #c1c8d1;
  --rule-strong: #9ca6b3;
  --link: #2a4a70;
  --accent: #355f8e;
}
:root:has(#theme-light:checked) {
  color-scheme: light;
  --background: #d3d9e0;
  --heading: #1f2933;
  --text: #3a4654;
  --rule: #c1c8d1;
  --rule-strong: #9ca6b3;
  --link: #2a4a70;
  --accent: #355f8e;
}
:root:has(#theme-dark:checked) {
  color-scheme: dark;
  --background: #171b21;
  --heading: #d6dce4;
  --text: #b3bcc7;
  --rule: #29303a;
  --rule-strong: #46505d;
  --link: #c9d3df;
  --accent: #7fa6d6;
}
@media print {
  :root {
    color-scheme: light !important;
    --background: #d3d9e0 !important;
    --heading: #1f2933 !important;
    --text: #3a4654 !important;
    --rule: #c1c8d1 !important;
    --rule-strong: #9ca6b3 !important;
    --link: #2a4a70 !important;
    --accent: #355f8e !important;
  }
}

/* Colour comes only from the theme tokens that the build prepends (site/themes.toml). */

/* why: cascade layers keep the spec flavour separable; later layers win, so the spec-sheet
   layer restyles base without specificity games, and deleting it leaves a plain document. */
@layer base, spec-sheet, texture;

:root {
  --measure: 65ch;
}

/* --- base: a plain, readable document --- */

@layer base {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  body {
    max-width: calc(var(--measure) + 2.5rem);
    margin: 0 auto;
    padding: clamp(1.25rem, 5vw, 3.5rem) 1.25rem;
    background: var(--background);
    color: var(--text);
    font-family:
      -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 1rem;
    line-height: 1.6;
  }

  h1,
  h2 {
    color: var(--heading);
    line-height: 1.2;
  }

  h1 {
    margin: 3rem 0 0.25rem;
    font-size: clamp(1.75rem, 1.4rem + 1.5vw, 2.25rem);
    letter-spacing: -0.02em;
  }

  h2 {
    margin: 2.75rem 0 0.75rem;
    font-size: 1rem;
  }

  p {
    margin: 0 0 1rem;
  }

  .role {
    margin-bottom: 1.5rem;
    font-size: 1.05rem;
  }

  a {
    color: var(--link);
    text-decoration-color: var(--accent);
    text-underline-offset: 0.2em;
    overflow-wrap: anywhere;
  }

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

  .spec {
    margin: 0;
  }

  .spec div {
    display: grid;
    grid-template-columns: 9rem 1fr;
    gap: 1rem;
    padding: 0.5rem 0;
  }

  .spec dt {
    color: var(--heading);
  }

  .spec dd {
    margin: 0;
    overflow-wrap: anywhere;
  }

  .links {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1.5rem;
    margin: 0;
    padding: 0;
    list-style: none;
  }

  .strip,
  footer {
    font-size: 0.8rem;
  }

  .strip {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem 1rem;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 0.5rem;
  }

  header p,
  footer p {
    margin: 0;
  }

  footer {
    margin-top: 3.5rem;
    padding-top: 0.75rem;
  }

  /* The switch: radios stay focusable but hidden; their labels are the buttons. */
  .theme-switch {
    display: flex;
    gap: 0.25rem;
    margin: 0;
    padding: 0;
    border: 0;
  }

  .theme-switch legend,
  .theme-switch input {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .theme-switch label {
    padding: 0.1rem 0.45rem;
    border-bottom: 2px solid transparent;
    cursor: pointer;
  }

  .theme-switch label:hover {
    color: var(--heading);
  }

  .theme-switch input:checked + label {
    border-bottom-color: var(--accent);
    color: var(--heading);
    font-weight: 600;
  }

  a:focus-visible,
  .theme-switch input:focus-visible + label {
    outline: 2px solid var(--accent);
    outline-offset: 4px;
    border-radius: 9px;
  }

  @media (width <= 30rem) {
    .spec div {
      grid-template-columns: 1fr;
      gap: 0;
    }
  }

  /* why: on paper the switch does nothing; print always uses the light tokens. */
  @media print {
    .theme-switch {
      display: none;
    }
  }

  /* why: motion only for visitors who have not asked to reduce it. */
  @media (prefers-reduced-motion: no-preference) {
    a,
    .theme-switch label {
      transition:
        color 0.15s,
        border-color 0.15s;
    }
  }
}

/* --- spec-sheet layer ---
   Monospace labels, the doc-ID strip, numbered § sections and hairlines. Deleting this section
   softens the page into a plain document; markup and content stay as they are. */

@layer spec-sheet {
  .strip,
  footer,
  h2,
  .spec dt,
  .links,
  .theme-switch {
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  }

  .strip,
  footer {
    letter-spacing: 0.04em;
  }

  .strip {
    border-bottom: 1px solid var(--rule-strong);
  }

  footer {
    border-top: 1px solid var(--rule);
  }

  h2 {
    font-size: 0.8rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
  }

  .spec dt {
    font-size: 0.9rem;
  }

  .spec div {
    border-top: 1px solid var(--rule);
  }

  .spec div:last-child {
    border-bottom: 1px solid var(--rule);
  }

  /* § numbers are generated here, never written in content; the 404 page counts as §404. */
  main {
    counter-reset: section;
  }

  .not-found {
    counter-reset: section 403;
  }

  h2::before,
  .not-found::before {
    content: "§" counter(section) "\00a0";
    counter-increment: section;
    color: var(--accent);
  }
}

/* --- texture layer ---
   A faint dot grid over a darker shade in the side margins, both fading out before the text
   column. Deleting this section leaves a flat background. */

@layer texture {
  :root {
    --texture-ink: light-dark(
      color-mix(in srgb, var(--text) 35%, transparent),
      color-mix(in srgb, var(--rule-strong) 70%, transparent)
    );
    --texture: radial-gradient(
        circle,
        var(--texture-ink) 0.5px,
        transparent 1px
      )
      0 0 / 1rem 1rem;

    /* why: light-dark() follows the active theme's color-scheme, so one rule darkens the edges
       gently on the light ground and more on the dark one. */
    --edge-shade: light-dark(
      color-mix(in srgb, var(--background), black 22%),
      color-mix(in srgb, var(--background), black 42%)
    );
  }

  body::before {
    position: fixed;
    z-index: -1;
    inset: 0;
    background:
      var(--texture), linear-gradient(var(--edge-shade), var(--edge-shade));
    content: "";
    pointer-events: none;

    /* why: dots and shade show in the margins and fade to nothing where the text column starts,
       so it never sits behind body copy. On narrow screens the window covers the whole width. */
    mask-image: linear-gradient(
      to right,
      black,
      transparent calc(50% - var(--measure) / 2 - 2rem),
      transparent calc(50% + var(--measure) / 2 + 2rem),
      black
    );
  }

  @media print {
    body::before {
      display: none;
    }
  }
}
