/*
 * legitphp.com: the foundation.
 *
 * This file makes plain HTML readable and accessible and nothing more. It has no brand, no layout
 * ideas and no decoration. Design is added after it, outside the `base` layer, so that any later
 * rule wins without a specificity fight.
 *
 * What it promises (WCAG 2.2 AA throughout, AAA where it costs nothing):
 *   - Text contrast of at least 7:1 and boundary contrast of at least 3:1, in light and in dark.
 *   - Sizes in rem, so the reader's own font size and zoom are respected.
 *   - A line length of about 70 characters, a line height of 1.5, and room between blocks.
 *   - Links that are underlined, so colour is never the only signal.
 *   - A focus outline that is always visible, 3px thick.
 *   - Targets of at least 44 by 44 CSS pixels for controls and navigation.
 *   - Nothing wider than a 320px screen, with no sideways scrolling of the page.
 *   - No motion, and a guard for readers who ask for less of it.
 *   - Sensible output in forced-colours mode, with more contrast on request, and on paper.
 */

@layer base {
  /* 1. Tokens ---------------------------------------------------------------------------- */

  :root {
    /* Lets the browser draw form controls and scrollbars to match. */
    color-scheme: light dark;

    --bg: #ffffff;
    --surface: #f2f2f2;
    --text: #1a1a1a;
    --text-muted: #4d4d4d;
    --link: #0b44a8;
    --link-visited: #5d2a9c;
    --border: #767676;
    --focus: #0b44a8;
    --mark: #fff2a8;

    --font-text:
      system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", "Liberation Sans", Arial,
      sans-serif;
    --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

    /* Type scale: each step is 1.2 times the one before, from the reader's own 1rem. */
    --step--1: 0.875rem;
    --step-0: 1rem;
    --step-1: 1.2rem;
    --step-2: 1.44rem;
    --step-3: 1.728rem;
    --step-4: 2.074rem;

    --line: 1.5;
    --line-tight: 1.25;

    /* Space scale, on a quarter-rem grid. */
    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.5rem;
    --space-6: 2rem;
    --space-7: 3rem;

    /* 60 zero-widths holds about 70 characters of running text. */
    --measure: 60ch;
    --gutter: clamp(1rem, 5vw, 2rem);
    /* 44 CSS pixels at the default font size. */
    --target: 2.75rem;
  }

  @media (prefers-color-scheme: dark) {
    :root {
      --bg: #121212;
      --surface: #222222;
      --text: #e8e8e8;
      --text-muted: #b3b3b3;
      --link: #9cc2ff;
      --link-visited: #d2b4ff;
      --border: #8c8c8c;
      --focus: #9cc2ff;
      --mark: #4a3d00;
    }
  }

  @media (prefers-contrast: more) {
    :root {
      --text: #000000;
      --text-muted: #1a1a1a;
      --link: #062a70;
      --link-visited: #3d1470;
      --border: #000000;
      --focus: #000000;
    }
  }

  @media (prefers-contrast: more) and (prefers-color-scheme: dark) {
    :root {
      --bg: #000000;
      --surface: #1a1a1a;
      --text: #ffffff;
      --text-muted: #e8e8e8;
      --link: #c4dbff;
      --link-visited: #e6d6ff;
      --border: #ffffff;
      --focus: #ffffff;
    }
  }

  /* 2. Document -------------------------------------------------------------------------- */

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

  html {
    /* Stops phones from enlarging text on their own when the screen turns. */
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--font-text);
    font-size: var(--step-0);
    line-height: var(--line);
    /* A long word or address breaks instead of pushing the page sideways. */
    overflow-wrap: break-word;
  }

  body > :is(header, main, footer) {
    max-inline-size: calc(var(--measure) + 2 * var(--gutter));
    margin-inline: auto;
    padding-inline: var(--gutter);
  }

  body > header {
    padding-block: var(--space-4);
  }

  body > main {
    padding-block: var(--space-5) var(--space-7);
  }

  body > footer {
    padding-block: var(--space-5);
    color: var(--text-muted);
  }

  /* The skip link moves focus here; an outline around the whole page body says nothing. */
  main:focus {
    outline: none;
  }

  :target {
    scroll-margin-block-start: var(--space-5);
  }

  /* 3. Text ------------------------------------------------------------------------------ */

  h1,
  h2,
  h3,
  h4,
  h5,
  h6 {
    margin-block: var(--space-6) var(--space-3);
    line-height: var(--line-tight);
    text-wrap: balance;
  }

  :is(h1, h2, h3, h4, h5, h6):first-child {
    margin-block-start: 0;
  }

  h1 {
    font-size: var(--step-4);
  }

  h2 {
    font-size: var(--step-3);
  }

  h3 {
    font-size: var(--step-2);
  }

  h4 {
    font-size: var(--step-1);
  }

  h5,
  h6 {
    font-size: var(--step-0);
  }

  p,
  ul,
  ol,
  dl,
  blockquote,
  pre,
  figure,
  table,
  details,
  address,
  fieldset {
    margin-block: 0 var(--space-4);
  }

  p {
    text-wrap: pretty;
  }

  small {
    font-size: var(--step--1);
  }

  b,
  strong {
    font-weight: 700;
  }

  address {
    font-style: normal;
  }

  /* Keeps a raised or lowered character from pushing its line apart. */
  sub,
  sup {
    position: relative;
    font-size: 0.75em;
    line-height: 0;
    vertical-align: baseline;
  }

  sup {
    inset-block-start: -0.5em;
  }

  sub {
    inset-block-end: -0.25em;
  }

  mark {
    background: var(--mark);
    color: var(--text);
  }

  abbr[title] {
    text-decoration: underline dotted;
    cursor: help;
  }

  hr {
    block-size: 0;
    margin-block: var(--space-6);
    border: 0;
    border-block-start: 1px solid var(--border);
  }

  /* 4. Links and focus ------------------------------------------------------------------- */

  a {
    color: var(--link);
    text-decoration-line: underline;
    text-decoration-thickness: max(0.0625em, 1px);
    text-underline-offset: 0.15em;
  }

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

  a:hover {
    text-decoration-thickness: max(0.125em, 2px);
  }

  :focus-visible {
    outline: 0.1875rem solid var(--focus);
    outline-offset: 0.125rem;
  }

  .skip-link {
    position: absolute;
    inset-block-start: var(--space-2);
    inset-inline-start: var(--space-2);
    z-index: 1;
    display: inline-flex;
    align-items: center;
    min-block-size: var(--target);
    padding-inline: var(--space-4);
    background: var(--bg);
    border: 1px solid var(--border);
  }

  /* Out of sight, still read aloud and still reachable by keyboard. */
  .visually-hidden,
  .skip-link:not(:focus, :active) {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    min-block-size: 0;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
    border: 0;
  }

  /* 5. Lists ----------------------------------------------------------------------------- */

  ul,
  ol {
    padding-inline-start: var(--space-5);
  }

  li + li {
    margin-block-start: var(--space-1);
  }

  li > :is(ul, ol) {
    margin-block: var(--space-1) 0;
  }

  dt {
    font-weight: 700;
  }

  dd {
    margin-inline-start: var(--space-5);
  }

  dd + dt {
    margin-block-start: var(--space-3);
  }

  /* 6. Navigation ------------------------------------------------------------------------ */

  nav ul {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1) var(--space-5);
    margin: 0;
    padding: 0;
    list-style: none;
  }

  nav li + li {
    margin-block-start: 0;
  }

  nav a {
    display: inline-flex;
    align-items: center;
    min-block-size: var(--target);
  }

  /* 7. Quotations, code and figures ------------------------------------------------------ */

  blockquote {
    margin-inline: 0;
    padding-inline-start: var(--space-4);
    border-inline-start: 0.25rem solid var(--border);
  }

  blockquote > :last-child {
    margin-block-end: 0;
  }

  /* Naming the family twice stops browsers from shrinking monospace text to 13px. */
  code,
  kbd,
  samp,
  pre {
    font-family: var(--font-mono), monospace;
    font-size: 1em;
  }

  :not(pre) > :is(code, samp) {
    padding: 0.1em 0.3em;
    background: var(--surface);
  }

  kbd {
    padding: 0.1em 0.4em;
    border: 1px solid var(--border);
    border-radius: 0.25em;
  }

  pre {
    padding: var(--space-4);
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    tab-size: 4;
  }

  img,
  svg,
  video,
  canvas {
    max-inline-size: 100%;
    block-size: auto;
    vertical-align: middle;
  }

  figure {
    margin-inline: 0;
  }

  figcaption {
    margin-block-start: var(--space-2);
    color: var(--text-muted);
    font-size: var(--step--1);
  }

  /* 8. Tables ---------------------------------------------------------------------------- */

  /* A wide table scrolls inside this region; the page itself never scrolls sideways. */
  .table-scroll {
    margin-block-end: var(--space-4);
    overflow-x: auto;
  }

  .table-scroll > table {
    margin-block-end: 0;
  }

  table {
    border-collapse: collapse;
    font-variant-numeric: tabular-nums;
  }

  caption {
    padding-block-end: var(--space-2);
    font-weight: 700;
    text-align: start;
  }

  th,
  td {
    padding: var(--space-2) var(--space-3);
    border-block-end: 1px solid var(--border);
    text-align: start;
    vertical-align: top;
  }

  /* 9. Disclosure ------------------------------------------------------------------------ */

  summary {
    min-block-size: var(--target);
    padding-block: var(--space-2);
    cursor: pointer;
  }

  details > :last-child {
    margin-block-end: 0;
  }

  /* 10. Forms ---------------------------------------------------------------------------- */

  button,
  input,
  select,
  textarea {
    max-inline-size: 100%;
    color: inherit;
    font: inherit;
    letter-spacing: inherit;
  }

  label {
    display: block;
    margin-block-end: var(--space-1);
    font-weight: 700;
  }

  input:not([type="checkbox"], [type="radio"], [type="range"], [type="color"], [type="file"]),
  select,
  textarea {
    min-block-size: var(--target);
    padding: var(--space-2) var(--space-3);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 0.25rem;
  }

  textarea {
    resize: vertical;
  }

  input:is([type="checkbox"], [type="radio"]) {
    inline-size: 1.5rem;
    block-size: 1.5rem;
    margin: 0;
    vertical-align: middle;
  }

  /* A checkbox or radio button has its label beside it, not above it. */
  input:is([type="checkbox"], [type="radio"]) + label {
    display: inline;
    margin-inline-start: var(--space-2);
    font-weight: inherit;
  }

  button,
  input:is([type="button"], [type="submit"], [type="reset"]) {
    min-inline-size: var(--target);
    min-block-size: var(--target);
    padding: var(--space-2) var(--space-4);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.25rem;
    cursor: pointer;
  }

  :disabled {
    cursor: not-allowed;
  }

  fieldset {
    min-inline-size: 0;
    padding: var(--space-3) var(--space-4) var(--space-4);
    border: 1px solid var(--border);
  }

  legend {
    padding-inline: var(--space-2);
    font-weight: 700;
  }

  /* 11. Reader preferences --------------------------------------------------------------- */

  /* Nothing here moves. This is for whatever is added later. */
  @media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
      animation-duration: 0.01ms !important;
      animation-iteration-count: 1 !important;
      scroll-behavior: auto !important;
      transition-duration: 0.01ms !important;
    }
  }

  /* The reader's own palette replaces ours. Backgrounds vanish, so anything that relied on one
     gets a border instead. */
  @media (forced-colors: active) {
    :not(pre) > :is(code, samp),
    mark {
      outline: 1px solid;
    }
  }

  /* 12. Paper ---------------------------------------------------------------------------- */

  @media print {
    :root {
      --bg: #ffffff;
      --surface: #ffffff;
      --text: #000000;
      --text-muted: #000000;
      --link: #000000;
      --link-visited: #000000;
      --border: #000000;
      --mark: #ffffff;
    }

    body > :is(header, main, footer) {
      max-inline-size: none;
      padding-inline: 0;
    }

    .skip-link,
    nav {
      display: none;
    }

    /* A printed link is useless without its address. */
    a[href^="http"]::after {
      content: " (" attr(href) ")";
      font-size: var(--step--1);
    }

    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
      break-after: avoid;
    }

    pre,
    blockquote,
    figure,
    table,
    img {
      break-inside: avoid;
    }

    pre {
      white-space: pre-wrap;
    }
  }
}

/*
 * Design.
 *
 * Each rule below is a decision Mike made after seeing it. The rules sit outside the `base` layer,
 * so they win over the foundation without a specificity fight.
 */

:root {
  /* The page is short, so everything is set a step larger. This scales text, spacing and targets
     together, and still follows the reader's own font size. */
  font-size: 125%;

  /* DIN, the lettering written as an engineering standard for signs and machine plates. Every
     name here is a typeface that ships with an operating system; nothing is downloaded. */
  --font-display:
    Bahnschrift, "DIN Alternate", "Franklin Gothic Medium", "Nimbus Sans Narrow", sans-serif-condensed, sans-serif;
}

/* The greeting is in the display lettering. The text stays in the system typeface. */
h1 {
  font-family: var(--font-display);
  font-size: clamp(1.8rem, 10vw, 2.8rem);
  font-weight: 700;
  line-height: 1.05;
  letter-spacing: 0.005em;
}

/* The marked words get a line drawn under them by hand, in red pencil. The line is decoration:
   it lives in a pseudo-element, so a screen reader meets the word and nothing else. */
:root {
  --pencil: #c0261c;
}

@media (prefers-color-scheme: dark) {
  :root {
    --pencil: #ff7566;
  }
}

@media (prefers-contrast: more) {
  :root {
    --pencil: #8f1208;
  }
}

@media (prefers-contrast: more) and (prefers-color-scheme: dark) {
  :root {
    --pencil: #ffb3a8;
  }
}

@media print {
  :root {
    --pencil: #000000;
  }
}

.marked {
  position: relative;
  display: inline-block;
  font-style: normal;
  white-space: nowrap;
}

.marked::after {
  content: "";
  position: absolute;
  inset-inline: -0.08em;
  inset-block-end: -0.1em;
  block-size: 0.3em;
  background: var(--pencil);
  -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 12' preserveAspectRatio='none'%3E%3Cpath d='M2 6C20 2 38 9 58 5S88 3 98 6' fill='none' stroke='%23000' stroke-width='3.4' stroke-linecap='round'/%3E%3Cpath d='M5 9.5C30 6.5 62 10.5 95 8' fill='none' stroke='%23000' stroke-width='1.6' stroke-linecap='round'/%3E%3C/svg%3E");
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 12' preserveAspectRatio='none'%3E%3Cpath d='M2 6C20 2 38 9 58 5S88 3 98 6' fill='none' stroke='%23000' stroke-width='3.4' stroke-linecap='round'/%3E%3Cpath d='M5 9.5C30 6.5 62 10.5 95 8' fill='none' stroke='%23000' stroke-width='1.6' stroke-linecap='round'/%3E%3C/svg%3E");
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-size: 100% 100%;
  mask-size: 100% 100%;
}

/* In forced-colours mode a background is thrown away, which would erase the line. Draw it in the
   reader's own text colour instead. */
@media (forced-colors: active) {
  .marked::after {
    forced-color-adjust: none;
    background: CanvasText;
  }
}
