:root {
  --ink: #222;
  --muted: #767676; /* The lightest grey that reaches 4.5:1 on white. */
  --line: #e6e6e6;
  --fade: 1200ms;
  --top: 28px;
  --gutter: 24px;
  --footer-gap: 12px; /* Twice the divider's distance from the top of the footer. */
  --footer-line: 16px;
  --footer-pad: 32px; /* With the two above, sets the footer's height. */
  --bottom: calc(var(--footer-gap) + var(--footer-line) + var(--footer-pad)); /* The footer lives in this margin. */
  --gap: 16px;
  --target: 24px; /* WCAG 2.2 AA minimum. */
  --rail: clamp(210px, 26vw, 300px);
  --follow-room: 0px; /* Space kept under the controls where the Follow pill would cover them. */
}

/* Fingers need more room than a mouse: 44 is Apple's minimum, in CSS pixels. */
@media (pointer: coarse) {
  :root { --target: 44px; }
}

* { box-sizing: border-box; margin: 0; }

html, body { height: 100%; background: #fff; }

body {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100dvh;
  padding: var(--top) var(--gutter) var(--bottom);
  gap: var(--gap);
  color: var(--ink);
  /* Grows gently from .875rem on a laptop to about 1rem on a 4K screen. */
  font: 300 clamp(.875rem, .8rem + .1vw, 1.0625rem)/1.5 Roboto, ui-sans-serif, -apple-system, "Helvetica Neue", Arial, sans-serif;
}

@media (pointer: coarse) {
  body { font-size: .9375rem; }
}

/* The margin tops up the body's gap, so the name has the same space above and below. */
header { margin-bottom: calc(var(--top) - var(--gap)); text-align: center; }

h1 { font-size: .93em; font-weight: 700; letter-spacing: .02em; white-space: nowrap; }

/* A block so app.js can animate its width; pre keeps the space after the comma. */
#hi { display: inline-block; white-space: pre; }

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

svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 1.6; }

/* Stays in the corner of the window while a long caption scrolls. */
.follow {
  position: fixed;
  right: var(--gutter);
  bottom: var(--bottom);
  z-index: 1;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: .86em;
  white-space: nowrap;
  transition: color .2s, border-color .2s;
}
/* Keeps the pill small while giving it a full-size tap area. */
.follow::after {
  content: "";
  position: absolute;
  inset: 50% 0 auto;
  height: max(100%, var(--target));
  transform: translateY(-50%);
}

/* Opaque, so a long caption scrolls behind it rather than through it. */
footer {
  position: fixed;
  inset: auto 0 0;
  height: var(--bottom);
  padding: calc(var(--footer-gap) / 2) var(--gutter) 0;
  background: #fff;
  color: var(--muted);
  font-size: .8em;
  line-height: calc(var(--bottom) - var(--footer-gap) / 2); /* Centers the text below the divider. */
  text-align: center;
  white-space: nowrap;
}
footer::before {
  content: "";
  position: absolute;
  inset: calc(var(--footer-gap) / 2) var(--gutter) auto;
  height: 1px;
  background: var(--line);
}

main { position: relative; min-height: 0; }

/* A long caption takes height from the picture down to this floor; past it, the page scrolls. */
figure {
  display: grid;
  grid-template-rows: minmax(50dvh, 1fr) auto;
  gap: var(--gap);
  position: relative;
  height: 100%;
  transition: opacity var(--fade) ease-in-out;
}
figure.hidden { opacity: 0; }

/* A pen stroke that draws itself once the outgoing post has mostly faded.
   Sits at the middle of the picture area, 68px above centre to clear the caption. */
#loader {
  position: absolute;
  top: calc(50% - 68px);
  left: 50%;
  width: 32px;
  height: 12px;
  transform: translate(-50%, -50%);
  color: var(--muted);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
#loader path { stroke-dasharray: 1; stroke-linecap: round; }
figure.hidden + #loader {
  opacity: 1;
  transition-delay: calc(var(--fade) * 2 / 3);
}
figure.hidden + #loader path {
  animation: draw 900ms ease-in-out calc(var(--fade) * 2 / 3) infinite backwards;
}
@keyframes draw {
  from { stroke-dashoffset: 1; }
  70%, to { stroke-dashoffset: 0; }
}

#image, #video {
  width: 100%;
  height: 100%;
  min-height: 0;
  object-fit: contain;
  touch-action: pan-y pinch-zoom; /* Horizontal swipes change the post; app.js handles them. */
  /* A border would trace the letterboxed element; shadows trace the picture itself. */
  filter:
    drop-shadow(1px 0 0 var(--line))
    drop-shadow(-1px 0 0 var(--line))
    drop-shadow(0 1px 0 var(--line))
    drop-shadow(0 -1px 0 var(--line));
}
#image[hidden], #video[hidden] { display: none; }

/* The padding sits in the body's own bottom padding, and keeps that margin when the page scrolls. */
figcaption {
  width: min(100%, 56ch);
  margin-inline: auto;
  margin-bottom: calc(var(--bottom) * -1);
  padding-bottom: calc(var(--bottom) + var(--follow-room));
  text-align: center;
}

/* Reserves three lines so the drawing doesn't resize between most slides;
   longer descriptions always show in full, never in a scrolling box. */
#caption {
  min-height: 4.5em;
  white-space: pre-line;
  overflow-wrap: anywhere;
}

/* The controls wrap as one group, so they stay together on a narrow screen. */
.row, .controls {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  column-gap: 16px;
}
.row {
  margin-top: 8px;
  color: var(--muted);
  font-size: .86em;
}
.controls { flex-wrap: nowrap; column-gap: 4px; }
#date { white-space: nowrap; }

.like {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-width: var(--target);
  min-height: var(--target);
  transition: color .2s;
}

button {
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
}
.controls button {
  min-width: var(--target);
  min-height: var(--target);
  padding-inline: 6px;
}

/* Sits in the corner of the video frame; app.js works out where that is. */
#sound {
  position: absolute;
  display: grid;
  place-items: center;
  width: max(40px, var(--target));
  height: max(40px, var(--target));
  border: 1px solid var(--line);
  border-radius: 50%;
  background: rgb(255 255 255 / .9);
  color: var(--ink);
}
#sound[hidden] { display: none; }
#sound svg { width: 20px; height: 20px; stroke-linecap: round; stroke-linejoin: round; }
#sound[aria-pressed="true"] .off, #sound[aria-pressed="false"] .on { display: none; }

/* Only where hovering is real: on touch screens these would stick after a tap. */
@media (hover: hover) {
  .follow:hover { color: var(--ink); border-color: var(--muted); }
  .like:hover { color: #d6324a; }
  .like:hover svg { fill: currentColor; }
  button:hover { color: var(--ink); }
  #sound:hover { border-color: var(--muted); }
}

:focus-visible { outline: 1px solid var(--ink); outline-offset: 3px; }

.notice { padding-top: 30vh; color: var(--muted); text-align: center; }

@media (max-width: 600px) {
  :root { --top: 20px; --gutter: 16px; }
}

/* Too narrow for the pill to sit clear of the centred controls. */
@media (max-width: 700px) {
  :root { --follow-room: 40px; }
}

/* Upright phones: the width is what limits the picture, so it runs edge to edge. */
@media (max-width: 600px) and (orientation: portrait) {
  figure { margin-inline: calc(var(--gutter) * -1); }
  figcaption { width: 100%; padding-inline: var(--gutter); }
}

@media (max-height: 640px) and (orientation: portrait) {
  #caption { min-height: 3em; }
}

/* Wide, short screens: stacked, the caption and controls would take over a quarter of the
   height, or half on a sideways phone. Moving them beside the picture gives that height
   back, since nearly all the posts are square or upright. */
@media (min-aspect-ratio: 5/4) and (max-height: 900px) {
  :root { --follow-room: 40px; } /* The pill's corner is the bottom of the rail. */

  figure {
    grid-template: minmax(0, 1fr) / minmax(0, 1fr) var(--rail);
    gap: var(--gutter);
  }

  figcaption {
    display: flex;
    flex-direction: column;
    align-self: start;
    width: auto;
    min-height: calc(100% + var(--bottom)); /* Grows past the picture's row if it has to. */
    margin: 0 0 calc(var(--bottom) * -1);
    text-align: left;
  }

  /* An auto margin, unlike flex-end, can't push a tall caption up off the top of the page. */
  #caption { flex: none; margin-top: auto; min-height: 0; }

  .row { justify-content: flex-start; }
  .controls { margin-inline-start: -6px; }

  #loader {
    top: 50%;
    left: calc((100% - var(--rail) - var(--gutter)) / 2);
  }
}

/* Sideways phones can't spare a row for the header, so it moves into the rail too. */
@media (min-aspect-ratio: 5/4) and (max-height: 500px) {
  :root { --top: 17px; --gutter: 16px; --footer-pad: 12px; }
  body { grid-template-rows: minmax(0, 1fr); }

  header {
    position: absolute;
    top: var(--top);
    right: var(--gutter);
    width: var(--rail);
  }

  figcaption { padding-top: 44px; } /* Clears the header. */
}

@media (prefers-reduced-motion: reduce) {
  figure { transition: none; }
  #loader { display: none; }
}
