/*
 * The footer (docs/design/footer.md), copied by each subdomain's refresh script with SiteFooter.tsx
 * It reads only the tokens every site defines the way src/index.css does: --line,
 * --text-body, --text-lede, --text-label, --font-sans, --font-mono, the noir colours, `.label`
 * and `.shell`. Archivo and DM Mono are the only faces.
 *
 * The grid is the site's one grid: four columns below 1024px and twelve from it, with the site's
 * column gap (`--grid-gap`, else one line). Every vertical gap is a whole or half line.
 */

.site-footer {
  /* The one gap between the bar's items, at every width and in every locale. A site maps it, never overrides it per item. */
  --site-footer-gap: 1rem;
  /* One row of a link list: a line of text and half a line of space. The pitch of both groups and of the control that closes one. */
  --site-footer-pitch: calc(var(--line) * 1.5);
  /* The 44px target of the language and currency control, which is the tallest thing in a list. */
  --site-footer-after-height: 2.75rem;
  /* The AI use mark's box and its drop below the baseline, so its ring is the © circle's size and top. Archivo's, which are 4thcltr.com's; a site with another label face sets both from that face's © (docs/design/footer.md, rule 4). */
  --site-footer-mark-height: 1.065em;
  --site-footer-mark-drop: 0.185em;
  border-top: 1px solid var(--color-line-soft);
  font-family: var(--font-sans);
  font-size: var(--text-body);
  line-height: var(--line);
  color: var(--color-muted);
}

.site-footer-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  /* The site's own column gap, so the footer's edges are its grid's: one line where a site doesn't set one. */
  column-gap: var(--grid-gap, var(--line));
  row-gap: calc(var(--line) * 2);
}

.site-footer-grid > * {
  grid-column: 1 / -1;
  min-width: 0;
  overflow-wrap: break-word;
}

.site-footer-grid p {
  margin: 0;
}

/* Two lines above the fields, two below, and a rule between the fields and the bar. */
.site-footer-main {
  padding-block: calc(var(--line) * 2);
}

.site-footer .site-footer-about {
  margin-block-start: var(--line);
  max-width: 34ch;
  font-size: var(--text-lede, 1.25rem);
  line-height: var(--line);
  color: var(--color-copy);
  text-wrap: pretty;
}

/* A name in the sentence stays on one line. */
.site-footer-about a {
  white-space: nowrap;
}

/* The head is half a line of label, then half a line of space. */
.site-footer .site-footer-head {
  margin-block-end: calc(var(--line) / 2);
  color: var(--color-faint);
}

.site-footer-links {
  display: grid;
  row-gap: calc(var(--line) / 2);
  margin: 0;
  padding: 0;
  list-style: none;
}

/*
 * A row is one line tall, with or without the new-tab icon. The link is a block-level flex box
 * (`inline-size: fit-content` keeps the target to its words). An `inline-flex` link sits on its
 * parent's line box, and with an svg first its baseline is the svg's bottom edge, which made the
 * line box about 3px taller than a plain link's. The icon is centred in the line and never sets its height.
 */
.site-footer-links a {
  display: flex;
  inline-size: fit-content;
  min-block-size: var(--line);
  align-items: center;
  gap: 0.5rem;
  line-height: var(--line);
  color: var(--color-muted);
  transition: color 240ms ease;
}

.site-footer-links a:hover,
.site-footer-links a:focus-visible {
  color: var(--color-bone, var(--color-copy));
  text-decoration: underline;
  text-underline-offset: 0.2em;
}

/*
 * The control that closes a group (the language and currency row) is one more row of the list: its
 * text is centred one pitch below the last link's. Its target is 44px (`--site-footer-after-height`),
 * taller than a row, so the gap above it is the pitch less the half lines of both boxes.
 */
.site-footer-after {
  margin-block-start: calc(var(--site-footer-pitch) - (var(--line) + var(--site-footer-after-height)) / 2);
}

.site-footer-after .settings-trigger {
  min-block-size: var(--site-footer-after-height);
}

/*
 * The bar: its rule is the shell's width, so it is inset from the edge like the content. One line,
 * top aligned: the colophon at the inline start, then the version and AI use flush to the inline
 * end (the flex row mirrors in right-to-left). A flex row, not the grid: the two ends are not on
 * columns, they are on the grid's edges. The panel still anchors to the bar's box.
 *
 * Under 640px it is two lines (below): the longest AI use labels (Arabic 125px, Russian 135px) do not fit
 * beside the colophon and the version at 375px, and the layout cannot depend on the locale. 640px is the
 * footer's existing stack breakpoint.
 */
.site-footer-bar {
  display: flex;
  align-items: flex-start;
  /* One gap between every item of the bar: the colophon, the version and AI use. No slash between them. */
  column-gap: var(--site-footer-gap);
  padding-block: var(--line) calc(var(--line) * 2);
  border-top: 1px solid var(--color-line-soft);
}

.site-footer-bar > * {
  flex: none;
  /* Never wraps inside an item: a line that doesn't fit is a width problem, not a wrap. */
  white-space: nowrap;
}

/* The colophon takes the room, so everything after it sits flush at the inline end. */
.site-footer-bar > .site-footer-colophon {
  flex: 0 1 auto;
  margin-inline-end: auto;
  white-space: normal;
}

/*
 * Two lines, both top aligned and flush at the inline start: the colophon, then the version and AI
 * use. The blocks that stack sit a whole line apart, as everywhere else in the footer.
 */
@media (max-width: 639px) {
  .site-footer-bar {
    flex-wrap: wrap;
    row-gap: var(--line);
  }

  .site-footer-bar > .site-footer-colophon {
    flex-basis: 100%;
  }
}

/*
 * One label line. A host that trims the text box of its labels (4thcltr.com, `text-box: trim-both`)
 * would crush the line box, and the button's first baseline is measured against a full one, so the
 * footer keeps the full line box. !important because the host's trim selector is more specific.
 */
.site-footer .site-footer-colophon,
.site-footer .site-footer-version {
  text-box: normal !important;
}

.site-footer .site-footer-colophon,
.site-footer .site-footer-version {
  color: var(--color-faint);
}

/*
 * The label's tracking is added after every character, the last one too, so the version's box ends
 * 0.06em past its last glyph and the gap to the ring would read that much wider than the gap from the
 * ring to the "A" of AI USE (owner, 18 November 2026: "it should be equadistance from y in ditry to a
 * in AI"). The version's trailing tracking is taken back. A left-to-right text trails on its right,
 * which is the gap's side only in a left-to-right page; in a right-to-left one the gap is on the "V"
 * side, which has none.
 */
.site-footer-version:dir(ltr) {
  margin-inline-end: calc(var(--tracking-label, 0.06em) * -1);
}

.site-footer-version a {
  color: inherit;
  text-underline-offset: 0.3em;
}

.site-footer-version a:hover,
.site-footer-version a:focus-visible {
  color: var(--color-bone, var(--color-copy));
  text-decoration: underline;
}

/*
 * The AI use item: a button with the level's mark and "AI use", and the panel it opens (HTML Popover
 * API). The button never grows. Its target is 24px tall; the padding is cancelled by a negative
 * margin so the label sits on the colophon's first line.
 */
.site-footer-ai-use-button {
  /* Block-level, so the 28px line box of its wrapper cannot lower it. */
  display: flex;
  width: fit-content;
  /* The mark and the label share the label's baseline. Nothing is centred: the 24px target is the
     label's line plus 5px above and below, cancelled by the margin, so the label is on the bar's first line. */
  align-items: baseline;
  /* The same gap as between the bar's items, measured from the ring's visible edge: the mark's box is cropped to its art (markViewBox). */
  gap: var(--site-footer-gap);
  margin: -5px 0;
  padding: 5px 0;
  line-height: var(--text-label--line-height);
  border: 0;
  background: none;
  color: var(--color-faint);
  font-family: var(--font-label, var(--font-sans));
  cursor: pointer;
  text-underline-offset: 0.3em;
  transition: color 240ms ease;
}

.site-footer-ai-use-button:hover,
.site-footer-ai-use-button:focus-visible {
  color: var(--color-bone, var(--color-copy));
  text-decoration: underline;
}

.ai-use-mark {
  flex: none;
  /*
   * The mark's ring is the size of the © in "© 4th Culture / Toronto", and its top is the © circle's
   * top (owner, 18 November 2026: "as long as the top of the circle in the AI use icon is the same
   * height as the circle in copyright we are good"). The ring is 16 of the box's 24 units, so the box
   * is 1.5 times the © glyph. In the label font the © is 0.71em tall and spans 0.698em above the
   * baseline to 0.012em below it, so the box is 1.065em tall and its centre is 0.343em above the
   * baseline. A flex item stands on the baseline by its bottom edge, so the box is moved down by
   * half its height less that, with `inset`, which leaves the button's layout alone. A cap-height
   * box (8.2px) drew a 5.5px ring and an 8px ring sat 0.3px low: both read as tiny.
   */
  position: relative;
  height: var(--site-footer-mark-height, 1.065em);
  width: auto;
  inset-block-start: var(--site-footer-mark-drop, 0.185em);
  /* The box stands taller than the label's ascent by about 2px. The margin keeps that out of the button's height, so the target stays 24px and 44px. */
  margin-block-start: -2px;
}

/*
 * The panel, in the flow by default (a browser without the Popover API shows it so, and so does
 * paper). Under :popover-open support it is the top layer, placed on the grid below.
 */
.site-footer-ai-use-panel {
  box-sizing: border-box;
  margin: 0;
  padding: var(--line);
  border: 1px solid var(--color-line);
  background: var(--color-ground-lift);
  color: var(--color-muted);
  font-family: var(--font-sans);
  font-size: var(--text-panel, 0.875rem);
  line-height: var(--text-panel--line-height, calc(var(--line) * 3 / 4));
  text-transform: none;
  letter-spacing: normal;
  overflow-wrap: anywhere;
  /* The bar's items never wrap; the panel inside one does. */
  white-space: normal;
}

.ai-use-panel-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--line);
  margin-block-end: calc(var(--line) / 2);
}

.site-footer .ai-use-panel-head {
  color: var(--color-copy);
}

.ai-use-close {
  flex: none;
  min-height: 24px;
  padding: 0 0.25rem;
  border: 1px solid var(--color-line);
  background: none;
  color: var(--color-muted);
  cursor: pointer;
}

.ai-use-close:hover,
.ai-use-close:focus-visible {
  color: var(--color-bone, var(--color-copy));
}

.ai-use-rows {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  column-gap: var(--line);
  row-gap: calc(var(--line) / 2);
  align-items: baseline;
  margin: 0;
}

.ai-use-rows dt,
.ai-use-rows dd {
  margin: 0;
}

.ai-use-rows dt {
  color: var(--color-faint);
}

.ai-use-tag,
.ai-use-token {
  font-family: var(--font-mono, ui-monospace, monospace);
  font-size: var(--text-panel-mono, 0.8125rem);
  line-height: var(--text-panel--line-height, calc(var(--line) * 3 / 4));
}

.ai-use-tag {
  color: var(--color-copy);
}

.ai-use-token {
  margin-inline-end: 0.75rem;
  color: var(--color-copy);
}

.ai-use-name {
  color: var(--color-copy);
}

.ai-use-meaning {
  display: block;
}

.ai-use-tools {
  display: grid;
  row-gap: calc(var(--line) / 2);
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--color-copy);
}

.site-footer .ai-use-note {
  margin-block-start: calc(var(--line) / 2);
  color: var(--color-copy);
  text-wrap: pretty;
}

.site-footer .ai-use-spec-link {
  margin-block-start: calc(var(--line) / 2);
}

.ai-use-spec-link a,
.ai-use-spec-link a:visited,
.ai-use-spec-link a:hover,
.ai-use-spec-link a:focus-visible {
  color: var(--color-copy);
  text-decoration: underline;
  text-underline-offset: 0.25em;
}

/*
 * A group of the tag (a token and the separator after it) stays on one line, and a line breaks only
 * after a separator. Only a group wider than its column wraps, inside the group, so a long tool
 * name at 320px never scrolls the panel sideways.
 */
.ai-use-group {
  display: inline-block;
  max-width: 100%;
  overflow-wrap: anywhere;
}

/* Under 1024px the controls are thumb-sized (44px); the bleed is cancelled so the rows keep their pitch. */
@media (max-width: 1023px) {
  .site-footer-ai-use-button {
    margin-block: -15px;
    padding-block: 15px;
  }

  .ai-use-close {
    min-width: 44px;
    min-height: 44px;
  }
}

/* No Popover API: the content is in the flow, so nothing opens and nothing closes. */
@supports not selector(:popover-open) {
  .site-footer-ai-use-button,
  .ai-use-close {
    display: none;
  }

  /* The open panel is the only thing in the bar's second row. */
  .site-footer-bar {
    flex-wrap: wrap;
  }

  .site-footer-bar > .site-footer-ai-use {
    flex-basis: 100%;
  }
}

@supports selector(:popover-open) {
  /* Below 640px: a full-width sheet from the bottom, with the shell's gutters. */
  .site-footer-ai-use-panel {
    position: fixed;
    inset: auto var(--shell-gutter, 1.5rem) env(safe-area-inset-bottom, 0px) var(--shell-gutter, 1.5rem);
    width: auto;
    max-height: 70dvh;
    overflow-y: auto;
  }

  .site-footer-ai-use-panel:not(:popover-open) {
    display: none;
  }
}

/*
 * Where the room above the bar is shorter than the panel (a short, wide screen), the panel becomes
 * the bottom sheet it is on a phone, so it is never clipped at the top of the viewport.
 */
@position-try --ai-use-sheet {
  position-anchor: none;
  top: auto;
  right: var(--shell-gutter, 1.5rem);
  bottom: env(safe-area-inset-bottom, 0px);
  left: var(--shell-gutter, 1.5rem);
  width: auto;
}

/* From 640px the panel sits on the bar's grid, above it. */
@supports selector(:popover-open) and (anchor-name: --a) {
  .site-footer-bar {
    anchor-name: --site-footer-bar;
  }

  @media (min-width: 640px) {
    .site-footer-ai-use-panel:popover-open {
      position-anchor: --site-footer-bar;
      position-try-fallbacks: --ai-use-sheet;
      inset: auto;
      bottom: anchor(top);
      inset-inline-start: anchor(start);
      width: anchor-size(width);
    }
  }

  /* Columns 7 to 12: the inline-end half of the bar (the left half in RTL, where the grid mirrors), less half a gap on the inside. */
  @media (min-width: 1024px) {
    .site-footer-ai-use-panel:popover-open {
      inset-inline-start: calc(anchor(start) + anchor-size(width) / 2 + var(--grid-gap, var(--line)) / 2);
      width: calc(anchor-size(width) / 2 - var(--grid-gap, var(--line)) / 2);
    }
  }
}

/*
 * The groups, with no empty band. Below 640px they stack. From 640px group A takes columns 1 and 2
 * and group B columns 3 and 4. The bar is not on columns: it is one line (above).
 */
@media (min-width: 640px) {
  .site-footer-group-a {
    grid-column: 1 / span 2;
  }

  .site-footer-group-b {
    grid-column: 3 / span 2;
  }

}

@media (min-width: 1024px) {
  .site-footer-grid {
    grid-template-columns: repeat(12, minmax(0, 1fr));
  }

  .site-footer-main {
    padding-block-start: calc(var(--line) * 4);
  }

  .site-footer-brand {
    grid-column: 1 / span 6;
  }

  .site-footer-group-a {
    grid-column: 7 / span 3;
  }

  .site-footer-group-b {
    grid-column: 10 / span 3;
  }
}

/* On paper the groups go; the sentence, the colophon and the open panel print, one under the other. */
@media print {
  .site-footer-group {
    display: none !important;
  }

  .site-footer-grid {
    display: block;
    padding-block: 6pt;
  }

  /* `.site-footer` first, so the gap beats the `p` margin reset above it. */
  .site-footer .site-footer-grid > * + * {
    margin-block-start: 6pt;
  }

  .site-footer-ai-use-button,
  .ai-use-close {
    display: none;
  }

  .site-footer .site-footer-ai-use-panel {
    display: block;
    position: static;
    width: auto;
    max-height: none;
    overflow: visible;
    padding: 0;
    border: 0;
    background: none;
  }
}
