/* Unblock America — single stylesheet, no dependencies.
 * Cache: 5 minutes (Cache-Control set in pra/urls.py serve_site_file).
 */

@layer reset, tokens, base, layout, components, motifs, utilities, print;

/* ---------- RESET ---------- */
@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  body, h1, h2, h3, h4, p, figure, blockquote, dl, dd { margin: 0; }
  ul[role="list"], ol[role="list"] { list-style: none; padding: 0; }
  img, picture, svg { max-width: 100%; display: block; }
  input, button, textarea, select { font: inherit; color: inherit; }
  button { background: none; border: 0; padding: 0; cursor: pointer; }
  a { color: inherit; }
}

/* ---------- TOKENS ---------- */
@layer tokens {
  :root {
    color-scheme: light dark;

    --color-ink:    light-dark(#14181f, #ecead8);
    --color-paper:  light-dark(#faf7ef, #10141b);
    --color-navy:   light-dark(#8b1a1a, #d97070);
    --color-red:    light-dark(#b22234, #d8404f);
    --color-rule:   color-mix(in oklab, currentColor 22%, transparent);
    --color-muted:  color-mix(in oklab, var(--color-ink) 65%, var(--color-paper));
    --color-focus:  light-dark(oklch(70% 0.18 250), oklch(78% 0.16 250));

    --font-text:    ui-serif, "Iowan Old Style", Charter, Cambria, Georgia, serif;
    --font-display: ui-serif, "Iowan Old Style", "Hoefler Text", Baskerville, Georgia, serif;
    --font-ui:      system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

    --fs-display: clamp(2.75rem, 6vw, 4.5rem);
    --fs-h1:      clamp(2rem, 4vw, 2.75rem);
    --fs-h2:      clamp(1.5rem, 2.5vw, 1.875rem);
    --fs-h3:      1.25rem;
    --fs-h4:      1.0625rem;
    --fs-body:    clamp(1rem, 0.4vw + 0.95rem, 1.125rem);
    --fs-lead:    1.25rem;
    --fs-small:   0.8125rem;

    --space:         1rem;
    --space-section: clamp(3rem, 8vw, 6rem);
    --space-block:   clamp(2rem, 4vw, 3rem);
    --gutter:        clamp(1rem, 4vw, 2.5rem);

    --width-prose: 68ch;
    --width-page:  72rem;

    --radius: 2px;
  }
}

/* ---------- BASE ---------- */
@layer base {
  html { font-family: var(--font-text); }
  body {
    background: var(--color-paper);
    color: var(--color-ink);
    font-size: var(--fs-body);
    line-height: 1.6;
    font-feature-settings: "kern", "liga", "onum";
    text-rendering: optimizeLegibility;
  }
  h1, h2, h3, h4 {
    font-family: var(--font-display);
    color: var(--color-navy);
    line-height: 1.2;
    text-wrap: balance;
  }
  h1 { font-size: var(--fs-h1); font-weight: 700; }
  h2 { font-size: var(--fs-h2); font-weight: 600; }
  h3 { font-size: var(--fs-h3); font-weight: 600; }
  h4 { font-size: var(--fs-h4); font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; }
  p  { max-width: var(--width-prose); text-wrap: pretty; }
  p + p { margin-top: 1em; }
  a { color: var(--color-navy); text-underline-offset: 0.2em; text-decoration-thickness: 1px; }
  a:hover { text-decoration-thickness: 2px; }
  small, .caption { font-size: var(--fs-small); color: var(--color-muted); }
  ::selection { background: var(--color-navy); color: var(--color-paper); }
  :focus-visible {
    outline: 3px solid var(--color-focus);
    outline-offset: 2px;
    border-radius: var(--radius);
  }
}

/* ---------- LAYOUT ---------- */
@layer layout {
  .container { max-width: var(--width-page); margin-inline: auto; padding-inline: var(--gutter); }
  .prose     { max-width: var(--width-prose); margin-inline: auto; }
  main       { padding-block: var(--space-section); }
  section    { margin-block: var(--space-section); }

  .hero { text-align: center; padding-block: clamp(1.5rem, 3vw, 2.5rem) clamp(3rem, 10vw, 7rem); }
  main:has(> .hero:first-child) { padding-block-start: 0; }
  main > .hero:first-child { margin-block-start: 0; }
  .hero__display {
    font-family: var(--font-display);
    font-size: var(--fs-display);
    font-weight: 700;
    line-height: 1.05;
    letter-spacing: 0.01em;
    font-variant-caps: small-caps;
    color: var(--color-navy);
    text-wrap: balance;
  }
  .hero__lead {
    font-size: var(--fs-lead);
    margin-top: 1rem;
    color: var(--color-muted);
    max-width: 50ch;
    margin-inline: auto;
  }
  .hero__actions { margin-top: 2rem; display: flex; gap: 1rem; flex-wrap: wrap; justify-content: center; }

  .closing-band { text-align: center; }
  .closing-band p:has(.btn) { margin-top: 1.5rem; display: flex; gap: 1rem; flex-wrap: wrap; justify-content: center; }

  .policies { container-type: inline-size; }
  .policy {
    margin-block: var(--space-block);
    padding-block-start: var(--space-block);
    border-block-start: 1px solid var(--color-rule);
    max-width: var(--width-prose);
    margin-inline: auto;
  }
  .policy:first-of-type { border-block-start: 0; padding-block-start: 0; }
  .policy__more {
    margin-block-start: 1rem;
    font-family: var(--font-ui);
    font-size: var(--fs-small);
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }
  .policy__more a { text-decoration: none; border-block-end: 1px solid var(--color-rule); }
  .policy__more a:hover { border-block-end-color: currentColor; }
  .policy__more a::after { content: " \2192"; }
  .policy__num {
    display: block;
    font-family: var(--font-display);
    font-size: 1rem;
    font-feature-settings: "lnum";
    color: var(--color-red);
    letter-spacing: 0.08em;
    text-transform: uppercase;
    margin-bottom: 0.25rem;
  }
  @container (min-width: 56rem) {
    .policy {
      display: grid;
      grid-template-columns: 6ch 1fr;
      column-gap: 2rem;
      max-width: none;
    }
    .policy__num {
      grid-column: 1;
      text-align: end;
      padding-top: 0.35rem;
    }
    .policy__body { grid-column: 2; max-width: var(--width-prose); }
  }
}

/* ---------- COMPONENTS ---------- */
@layer components {
  .site-header {
    position: sticky; top: 0; z-index: 10;
    background: color-mix(in oklab, var(--color-paper) 92%, transparent);
    backdrop-filter: blur(6px);
    border-block-end: 1px solid var(--color-rule);
  }
  .site-header__inner {
    display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between;
    gap: 1rem; padding-block: 0.875rem;
  }
  .wordmark {
    font-family: var(--font-display);
    font-variant-caps: small-caps;
    letter-spacing: 0.04em;
    font-weight: 700;
    color: var(--color-navy);
    text-decoration: none;
    font-size: 1.125rem;
  }
  .site-nav { display: flex; flex-wrap: wrap; gap: 1.25rem; font-family: var(--font-ui); font-size: 0.9375rem; }
  .site-nav a { text-decoration: none; }
  .site-nav a:hover, .site-nav a[aria-current="page"] { text-decoration: underline; text-underline-offset: 0.3em; }

  /* Buttons — square and stamp-like */
  .btn {
    display: inline-block;
    font-family: var(--font-ui);
    font-size: 0.9375rem;
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius);
    text-decoration: none;
    transition: transform 120ms ease, background-color 120ms ease;
  }
  .btn--primary {
    background: var(--color-navy);
    color: var(--color-paper);
    box-shadow: 0 0 0 2px var(--color-paper), 0 0 0 3.5px var(--color-navy);
  }
  .btn--primary:hover { background: color-mix(in oklab, var(--color-navy) 85%, black); transform: translateY(-1px); }
  .btn--secondary {
    color: var(--color-navy);
    border: 1.5px solid var(--color-navy);
    background: transparent;
  }
  .btn--secondary:hover { background: color-mix(in oklab, var(--color-navy) 8%, transparent); transform: translateY(-1px); }

  /* Forms */
  .form { display: grid; gap: 1.25rem; max-width: 32rem; }
  .field { display: grid; gap: 0.375rem; }
  .field > label { font-family: var(--font-ui); font-size: 0.9375rem; font-weight: 600; }
  .field--required > label::after { content: " *"; color: var(--color-red); }
  .field--checkbox > label {
    display: flex; align-items: center; gap: 0.5rem;
    font-weight: 400;
  }
  .field--checkbox input[type="checkbox"] { width: 1.1rem; height: 1.1rem; accent-color: var(--color-navy); }
  .field input[type="text"],
  .field input[type="email"],
  .field input[type="tel"],
  .field textarea,
  .field select {
    font-family: var(--font-ui);
    padding: 0.75rem 1rem;
    background: var(--color-paper);
    color: var(--color-ink);
    border: 1.5px solid var(--color-rule);
    border-radius: var(--radius);
    transition: border-color 120ms ease;
  }
  .field input:focus-visible, .field textarea:focus-visible, .field select:focus-visible {
    border-color: var(--color-navy);
    outline: 3px solid var(--color-focus);
    outline-offset: 1px;
  }
  .field input:invalid:not(:placeholder-shown),
  .field textarea:invalid:not(:placeholder-shown) {
    border-color: var(--color-red);
  }
  .field .hint { font-size: var(--fs-small); color: var(--color-muted); margin: 0; }

  .form-result {
    margin: 0;
    padding: 0.75rem 1rem;
    background: color-mix(in oklab, var(--color-navy) 8%, transparent);
    border-left: 3px solid var(--color-navy);
    font-size: 0.9375rem;
  }
  .form-result.error {
    background: color-mix(in oklab, var(--color-red) 10%, transparent);
    border-left-color: var(--color-red);
  }

  .site-footer {
    margin-top: var(--space-section);
    padding-block: 2rem 3rem;
    font-family: var(--font-ui);
    font-size: var(--fs-small);
    color: var(--color-muted);
  }
  .site-footer .bunting { margin-bottom: 2rem; }
  .site-footer__inner {
    display: grid; gap: 1rem;
    grid-template-columns: 1fr;
    container-type: inline-size;
  }
  @container (min-width: 36rem) {
    .site-footer__inner { grid-template-columns: 1fr auto; align-items: end; }
  }
  .site-footer nav { display: flex; gap: 1.25rem; flex-wrap: wrap; }
  .site-footer a { color: inherit; }

  .error-page { text-align: center; padding-block: clamp(4rem, 12vw, 8rem); }
  .error-page__code {
    font-family: var(--font-display);
    font-size: clamp(4rem, 14vw, 8rem);
    color: var(--color-red);
    line-height: 1;
    font-feature-settings: "lnum";
  }

  /* ---- research index: a filing drawer of documents ---- */
  .research-note {
    border-inline-start: 3px solid var(--color-red);
    padding-inline-start: 1rem;
    color: var(--color-muted);
    font-size: var(--fs-small);
    margin-block-start: 1.5rem;
  }

  .dossier { max-width: var(--width-prose); margin-inline: auto; }
  .dossier__heading {
    font-size: 1rem;
    font-family: var(--font-display);
    font-variant-caps: small-caps;
    letter-spacing: 0.1em;
    color: var(--color-red);
    padding-block-end: 0.5rem;
    border-block-end: 2px solid var(--color-rule);
  }
  .dossier__list { list-style: none; padding: 0; margin: 0; counter-reset: dossier; }
  .dossier__item {
    counter-increment: dossier;
    padding-block: 1.5rem;
    border-block-end: 1px solid var(--color-rule);
    display: grid;
    grid-template-columns: 2.5rem 1fr;
    column-gap: 0.75rem;
  }
  .dossier__item::before {
    content: counter(dossier, decimal-leading-zero);
    font-family: var(--font-display);
    font-feature-settings: "lnum";
    font-size: var(--fs-small);
    color: var(--color-muted);
    text-align: end;
    padding-block-start: 0.4rem;
  }
  .dossier__title { grid-column: 2; font-size: 1.1875rem; font-weight: 600; }
  .dossier__title a { text-decoration: none; }
  .dossier__title a:hover { text-decoration: underline; }
  .dossier__meta {
    grid-column: 2;
    font-family: var(--font-ui);
    font-size: var(--fs-small);
    color: var(--color-muted);
    margin-block-start: 0.25rem;
  }
  .dossier__blurb { grid-column: 2; margin-block-start: 0.5rem; color: var(--color-muted); }

  /* ---- one research document ---- */
  .doc__crumb {
    font-family: var(--font-ui);
    font-size: var(--fs-small);
    color: var(--color-muted);
    margin-block-end: 2rem;
  }
  .doc__crumb a { text-decoration: none; }
  .doc__crumb a:hover { text-decoration: underline; }

  .doc__layout { display: grid; gap: 2.5rem; }
  .doc__layout > * { min-width: 0; }

  .doc__toc {
    font-family: var(--font-ui);
    font-size: var(--fs-small);
    line-height: 1.5;
    border-inline-start: 2px solid var(--color-rule);
    padding-inline-start: 1rem;
    max-height: 22rem;
    overflow-y: auto;
  }
  .doc__toc-heading {
    font-size: var(--fs-small);
    font-family: var(--font-display);
    font-variant-caps: small-caps;
    letter-spacing: 0.1em;
    color: var(--color-red);
    margin-block-end: 0.5rem;
  }
  .doc__toc ul { list-style: none; padding: 0; margin: 0; }
  .doc__toc ul ul { padding-inline-start: 0.875rem; }
  .doc__toc li { margin-block: 0.3rem; }
  .doc__toc a { text-decoration: none; color: var(--color-muted); }
  .doc__toc a:hover { color: var(--color-navy); text-decoration: underline; }

  @media (width >= 64rem) {
    .doc__layout {
      grid-template-columns: 15rem minmax(0, var(--width-prose));
      justify-content: center;
      align-items: start;
      gap: 3.5rem;
    }
    .doc__toc {
      position: sticky;
      top: 5rem;
      max-height: calc(100dvh - 8rem);
    }
  }

  .doc__footer {
    max-width: var(--width-prose);
    margin: var(--space-block) auto 0;
    padding-block-start: 1.5rem;
    border-block-start: 2px solid var(--color-rule);
  }
  .doc__provenance {
    font-family: var(--font-ui);
    font-size: var(--fs-small);
    color: var(--color-muted);
  }
  .doc__seriatim {
    display: grid;
    gap: 1rem;
    margin-block-start: 1.5rem;
    grid-template-columns: 1fr;
  }
  @media (width >= 40rem) {
    .doc__seriatim:has(.doc__next):has(.doc__prev) { grid-template-columns: 1fr 1fr; }
  }
  .doc__seriatim a {
    text-decoration: none;
    border: 1px solid var(--color-rule);
    padding: 0.875rem 1rem;
    font-size: 0.9375rem;
  }
  .doc__seriatim a:hover { border-color: var(--color-navy); }
  .doc__seriatim span {
    display: block;
    font-family: var(--font-ui);
    font-size: var(--fs-small);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--color-muted);
    margin-block-end: 0.25rem;
  }
  .doc__next { text-align: end; grid-column: -2; }
}

/* ---------- RENDERED MARKDOWN ----------
 * The research documents are markdown on disk (see core/mdcorpus.py). The
 * reset zeroes every block margin, so everything a markdown file can produce
 * has to be given its rhythm back here.
 */
@layer components {
  /* The Sources sections are lists of bare URLs, and a 60-character URL is one
   * unbreakable word. Without this the longest one sets the page width and the
   * whole document scrolls sideways on a phone. */
  .md { overflow-wrap: break-word; }

  .md > * + * { margin-block-start: 1em; }
  .md h1 { margin-block-end: 0.6em; }
  .md h2 {
    margin-block-start: 2.25em;
    padding-block-end: 0.3em;
    border-block-end: 1px solid var(--color-rule);
  }
  .md h3 { margin-block-start: 1.75em; }
  .md h4 { margin-block-start: 1.5em; color: var(--color-red); }
  .md h2 + *, .md h3 + *, .md h4 + * { margin-block-start: 0.75em; }

  .md ul, .md ol { padding-inline-start: 1.5rem; max-width: var(--width-prose); }
  .md li + li { margin-block-start: 0.4em; }
  .md li > ul, .md li > ol { margin-block-start: 0.4em; }
  .md li p { max-width: none; }
  .md li p + p { margin-block-start: 0.5em; }

  .md blockquote {
    border-inline-start: 3px solid var(--color-red);
    padding: 0.25rem 0 0.25rem 1.25rem;
    font-size: var(--fs-lead);
    font-style: italic;
  }
  .md blockquote p { max-width: none; }

  .md code {
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
    font-size: 0.8125em;
    background: color-mix(in oklab, var(--color-ink) 8%, transparent);
    padding: 0.1em 0.35em;
    border-radius: var(--radius);
    overflow-wrap: break-word;
  }
  .md pre {
    background: color-mix(in oklab, var(--color-ink) 6%, transparent);
    border: 1px solid var(--color-rule);
    padding: 1rem 1.25rem;
    overflow-x: auto;
    font-size: 0.8125rem;
    line-height: 1.55;
  }
  .md pre code { background: none; padding: 0; font-size: inherit; }

  .md hr { border: 0; border-block-start: 1px solid var(--color-rule); margin-block: var(--space-block); }

  .table-scroll { overflow-x: auto; overscroll-behavior-x: contain; }
  .md table {
    border-collapse: collapse;
    font-family: var(--font-ui);
    font-size: 0.875rem;
    line-height: 1.45;
    min-width: 100%;
  }
  .md th, .md td {
    border: 1px solid var(--color-rule);
    padding: 0.5rem 0.7rem;
    text-align: start;
    vertical-align: top;
  }
  .md th {
    background: color-mix(in oklab, var(--color-ink) 7%, transparent);
    font-weight: 600;
    white-space: nowrap;
  }
  .md td p { max-width: none; }

  /* A link the source document makes to a repository file that is not
   * published on the site. core/mdcorpus.py demotes it rather than leaving a
   * href that would 404. */
  .md-unpublished {
    color: var(--color-muted);
    border-block-end: 1px dotted var(--color-rule);
    cursor: help;
  }
}

/* ---------- MOTIFS ---------- */
@layer motifs {
  .bunting {
    height: 4px;
    border: 0;
    margin: 0;
    background: linear-gradient(
      90deg,
      var(--color-red)   0      33.333%,
      var(--color-paper) 33.333% 66.666%,
      var(--color-navy)  66.666% 100%
    );
  }

  .seal {
    margin: 0 auto 1.5rem;
    text-align: center;
    font-family: var(--font-display);
    font-variant-caps: small-caps;
    font-size: 0.75rem;
    letter-spacing: 0.12em;
    color: var(--color-navy);
  }
}

/* ---------- UTILITIES ---------- */
@layer utilities {
  .visually-hidden {
    position: absolute; width: 1px; height: 1px;
    padding: 0; margin: -1px; overflow: hidden;
    clip: rect(0 0 0 0); white-space: nowrap; border: 0;
  }

  @media (prefers-reduced-motion: no-preference) {
    @supports (animation-timeline: view()) {
      .policy h2 {
        animation: reveal linear both;
        animation-timeline: view();
        animation-range: entry 0% entry 40%;
      }
      @keyframes reveal {
        from { opacity: 0.35; transform: translateY(6px); }
        to   { opacity: 1;    transform: translateY(0); }
      }
    }
  }
}

/* ---------- PRINT ---------- */
@layer print {
  @media print {
    :root { --color-paper: #fff; --color-ink: #000; --color-navy: #000; --color-red: #000; }
    .site-header, .site-footer, .btn, .site-nav, .seal,
    .doc__toc, .doc__crumb, .doc__seriatim { display: none !important; }
    body { font-size: 11pt; line-height: 1.45; }
    main { padding: 0; }
    section, .policy { margin: 0 0 1.5rem; break-inside: avoid; }
    .doc__layout { display: block; }
    .md h2, .md h3 { break-after: avoid; }
    .md table, .md pre, .md blockquote { break-inside: avoid; }
    .md th { background: none; border-bottom-width: 2px; }
    .bunting { background: #000; height: 1px; }
    a { text-decoration: underline; }
    a[href]::after { content: " (" attr(href) ")"; font-size: 0.85em; color: #444; }
  }
}
