/* Shared by every page: reset, fonts, colours, the baseline grid and the
   grid overlay. Page stylesheets load after this and can override the
   tokens, e.g. --columns */

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

* {
  margin: 0;
  padding: 0;
}

img,
picture,
video,
canvas,
svg {
  display: block;
  max-width: 100%;
}

input,
button,
textarea,
select {
  font: inherit;
}

p,
h1,
h2,
h3,
h4,
h5,
h6 {
  overflow-wrap: break-word;
}

ul {
  padding: 0;
}

/* Document setup */
@font-face {
  font-family: "Neue Haas Grotesk Text";
  src: url(fonts/neue-haas-grotesk-text-roman.woff2) format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: fallback;
}

@font-face {
  font-family: "Neue Haas Grotesk Display";
  src: url(fonts/neue-haas-grotesk-display-roman.woff2) format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: fallback;
}

@font-face {
  font-family: "Editorial New";
  src: url(fonts/editorial-new-light.woff2) format("woff2");
  font-weight: 300;
  font-style: normal;
  font-display: fallback;
}

@font-face {
  font-family: "Editorial New";
  src: url(fonts/editorial-new-light-italic.woff2) format("woff2");
  font-weight: 300;
  font-style: italic;
  font-display: fallback;
}

@font-face {
  font-family: "Alte Haas Grotesk";
  src: url(fonts/alte-haas-grotesk-regular.woff2) format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: fallback;
}

:root {
  /* Everything vertical is a multiple of the baseline. 12px fits the 24px
     text lines and 36px display lines */
  --baseline: 12px;
  --module: calc(21 * var(--baseline));
  --margin: calc(3 * var(--baseline));
  --gutter: 24px;
  --columns: repeat(3, minmax(0, 1fr));
  /* Hover and focus shapes fade in quickly, and out a little more slowly */
  --hover-in: 120ms;
  --hover-out: 200ms;
  /* The grid overlay draws in from the top at a steady speed, in pixels
     per millisecond, and fades out */
  --grid-speed: 4;
  --grid-out: 200ms;
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --hover-in: 0s;
    --hover-out: 0s;
    --grid-out: 0s;
  }
}

[data-theme="light"] {
  --highlight: 255, 255, 255;
  --red: 255, 99, 99;
  --yellow: 235, 154, 80;
  --ink: 50, 57, 60;
  --backdrop: 38, 38, 38;
  --paper: 243, 242, 238;
  --grey: 169, 179, 185;
  /* Hover and focus shapes: translucent, so the grid shows through them,
     but over the paper they're the paper with its HSL lightness 2 points
     lower */
  --hover-fill: rgba(201, 196, 178, 0.1);
  --hover-highlight: rgba(var(--highlight), 0.85);
  --hover-shadow: rgba(var(--backdrop), 0.15);
}

[data-theme="dark"] {
  --highlight: 140, 145, 150;
  --red: 255, 99, 99;
  --yellow: 235, 154, 80;
  --ink: 255, 254, 250;
  --backdrop: 12, 12, 12;
  --paper: 38, 38, 38;
  --grey: 169, 179, 185;
  --hover-fill: rgba(18, 18, 18, 0.25);
  --hover-highlight: rgba(var(--highlight), 0.25);
  --hover-shadow: rgba(var(--backdrop), 0.55);
}

html {
  color: rgb(var(--ink));
  background-color: rgb(var(--backdrop));
}

body {
  font-family: "Alte Haas Grotesk", "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  font-size: 16px;
  font-style: normal;
  font-weight: 400;
  line-height: 24px;
  letter-spacing: -0.14px;
}

a {
  text-decoration: none;
  color: currentColor;
}

/* Taps show the page's own hover shapes, not the browser's grey box */
a,
button {
  -webkit-tap-highlight-color: transparent;
}

/* Wrapping: paragraphs keep full lines but avoid ending on a lone word;
   headings even out their lines. Browsers without support wrap as usual */
p {
  text-wrap: pretty;
}

h1,
h2 {
  text-wrap: balance;
}

/* The page's paper is the body. It's a layer of its own, so the grid
   overlay can sit on the paper but behind the content */
body {
  background-color: rgb(var(--paper));
  border-radius: 4px;
  isolation: isolate;
}

/* Grid overlay: baseline lines plus one child per column. It covers the
   nearest positioned ancestor, normally the page's body, behind the
   content */
.grid-display {
  display: grid;
  grid-template-columns: var(--columns);
  column-gap: var(--gutter);
  padding-inline: var(--margin);
  position: absolute;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background-image: repeating-linear-gradient(
    rgba(var(--ink), 0.1) 0 1px,
    transparent 1px 100%
  );
  background-size: 100% var(--baseline);
}

.grid-display > div {
  border-inline: 1px solid rgb(var(--red));
}

/* The overlay and its button's icon follow the page's data-grid, "on" or
   "off". Pages start with it off and restore a visitor's choice. Hidden,
   the whole overlay fades out at once */
[data-grid="off"] .grid-display {
  opacity: 0;
  visibility: hidden;
  transition:
    opacity var(--grid-out) ease,
    visibility 0s var(--grid-out);
}

/* Turned on with the button or G (data-grid-reveal), it's uncovered from
   the top down: the baselines appear one after another and the column
   lines draw down together. The edge moves at the same speed on every
   page, so it runs to 8000px, past the longest, and short pages are
   drawn sooner */
@property --grid-reveal {
  syntax: "<length>";
  inherits: false;
  initial-value: 100000px;
}

@keyframes grid-reveal {
  from {
    --grid-reveal: 0px;
  }
  to {
    --grid-reveal: 8000px;
  }
}

[data-grid="on"][data-grid-reveal] .grid-display {
  -webkit-mask-image: linear-gradient(
    #000 var(--grid-reveal),
    transparent var(--grid-reveal)
  );
  mask-image: linear-gradient(
    #000 var(--grid-reveal),
    transparent var(--grid-reveal)
  );
  animation: grid-reveal calc(8000ms / var(--grid-speed)) linear;
}

@media (prefers-reduced-motion: reduce) {
  [data-grid="on"][data-grid-reveal] .grid-display {
    animation: none;
  }
}

[data-grid="on"] .grid-icon {
  color: rgb(var(--red));
}

/* Small screens: one column on an 8px baseline */
@media (width < 800px) {
  :root {
    /* 24px text lines, 32px display lines and the 80px name only share
       an 8px rhythm, so that is the baseline here */
    --baseline: 8px;
    --module: calc(16 * var(--baseline));
    --margin: calc(4 * var(--baseline));
    --columns: minmax(0, 1fr);
  }

  .grid-display {
    padding-inline: var(--gutter);
  }
}

/* iPhone and iPad (WebKit on iOS, Safari and Chrome alike) draw the small
   serif text a little higher against the baseline than other browsers, so
   it's nudged down half a pixel there; an iPad in landscape, with 4
   columns, needs a full pixel. The hover shapes don't follow it: they sit
   a pixel higher than elsewhere, which centres them on the lettering
   there. Only iOS WebKit supports -webkit-touch-callout */
@supports (-webkit-touch-callout: none) {
  :root {
    --ios-nudge: 0.5px;
  }

  @media (width >= 1100px) {
    :root {
      --ios-nudge: 1px;
    }
  }

  .display-link,
  .display-text {
    position: relative;
    top: var(--ios-nudge);
  }

  /* A link's shape is its ::before, moved back up */
  .display-link::before {
    translate: 0 calc(-1px - var(--ios-nudge));
  }

  /* A button is its own shape, so the button moves up a pixel, keeping its
     height of the line plus 2px, while its text moves down inside it.
     The margins keep everything after it where it was */
  .display-container .display-button {
    height: calc(1lh + 2px);
    margin-block: -2.5px 0.5px;
    padding-top: calc(2.5px + var(--ios-nudge));
    padding-bottom: 0;
  }
}

/* Utilities */
.hidden {
  display: none;
}
