/* ===== FONT FACE ===== */
@font-face {
  font-family: 'Josefin Sans';
  src: url('/fonts/JosefinSans-Light.woff2') format('woff2');
  font-weight: 300;
  font-style: normal;
  font-display: swap;
}

/* ===== TOKENS ===== */
:root {
  --bg: #333333;
  --text: #eee;
  --link: #fff;
  --link-hover: #ccc;

  /* Timings (audio timings live in js/player.js, the artwork's in js/art.js) */
  --caption-fade: 1.3s;     /* each caption line dissolving in and out (set from player.js) */

  --font-sans: 'Josefin Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;

  /* ----- Layout system -----
     One number drives everything: --cover, the size of the square artwork.
     It is worked out from the screen alone, so nothing can move or resize
     when playback starts, pauses or resumes.

     The stage is three rows: logo / artwork / text. The rows above and below
     the artwork share the spare height equally, which puts the artwork
     exactly in the middle of the screen whenever there is room for that.
     Should a screen ever be too small for that, the artwork is allowed to
     grow and sit a little off centre instead — still fixed. */
  --vh: 1vh;                                   /* upgraded to dvh below */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --avail-h: calc(100 * var(--vh) - var(--safe-t) - var(--safe-b));
  --avail-w: calc(100vw - var(--safe-l) - var(--safe-r));

  --page-margin: clamp(14px, calc(4.5 * var(--vh)), 56px);  /* above and below everything */
  --inner-h: calc(var(--avail-h) - 2 * var(--page-margin));

  --text-fs: clamp(10.5px, 0.6rem + 0.22vw, 13px);   /* caption size */
  /* Height of the caption. This is only a first estimate: js/player.js
     measures the real height and overwrites it. */
  --text-h: calc(var(--text-fs) * 1.9);
  --logo-ratio: 0.18;       /* logo font size as a share of the artwork */
  --gap-ratio: 0.07;        /* space logo→artwork and artwork→text */
  --cover-floor: 300px;     /* below this, prefer a bigger artwork over exact centring */

  /* Largest artwork that still sits exactly in the centre of the screen:
     room for the caption below it, and for the logo above it, twice over */
  --cover-centred: min(
    calc((var(--inner-h) - 2 * var(--text-h)) / (1 + 2 * var(--gap-ratio))),
    calc(var(--inner-h) / (1 + 2 * (0.775 * var(--logo-ratio) + var(--gap-ratio))))
  );
  /* Largest artwork that fits at all (logo + artwork + text stacked) */
  --cover-fit: calc((var(--inner-h) - var(--text-h)) / (1 + 0.775 * var(--logo-ratio) + 2 * var(--gap-ratio)));

  --cover: clamp(
    96px,
    min(
      calc(var(--avail-w) * 0.84),
      max(var(--cover-centred), min(var(--cover-fit), var(--cover-floor)))
    ),
    720px
  );
  --gap: calc(var(--cover) * var(--gap-ratio));
}

@supports (height: 1dvh) {
  :root { --vh: 1dvh; }
}

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;              /* the page never scrolls, on any device */
  overscroll-behavior: none;
  background: var(--bg);
}

body {
  color: var(--text);
  font-family: var(--font-sans);
  font-weight: 300;
  line-height: 1.6;
  letter-spacing: 0.2px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* ===== STAGE ===== */
.stage {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  /* Equal rows above and below the artwork = artwork dead centre.
     Each row can never be smaller than what it holds. */
  grid-template-rows: minmax(min-content, 1fr) auto minmax(min-content, 1fr);
  justify-items: center;
  padding:
    calc(var(--page-margin) + var(--safe-t))
    var(--safe-r)
    calc(var(--page-margin) + var(--safe-b))
    var(--safe-l);
  overflow: hidden;
}

/* ===== LOGO ===== */
.logo {
  grid-row: 1;
  align-self: end;
  /* The gap is measured from the baseline, so remove the space the
     line box keeps below it (0.275em for this font at line-height 1.05). */
  margin: 0 0 max(0px, calc(var(--gap) - 0.275em)) 0;
  text-align: center;
  color: var(--text);
  font-weight: 300;
  letter-spacing: 0.03em;
  line-height: 1.05;
  font-size: calc(var(--cover) * var(--logo-ratio));
  white-space: nowrap;
  /* Optical centring: the J's hook, the N's wide right side-bearing and the
     trailing letter-spacing leave the ink 0.071em left of centre. */
  transform: translateX(0.071em);
  user-select: none;
  -webkit-user-select: none;
}

/* ===== ARTWORK / PLAY BUTTON ===== */
.cover {
  grid-row: 2;
  position: relative;
  display: block;
  width: var(--cover);
  height: var(--cover);
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--bg);
  color: inherit;
  font: inherit;
  overflow: hidden;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  outline: none;
  contain: layout paint;         /* nothing inside can disturb the page layout */
}

.cover:focus-visible {
  outline: 2px solid #ffffff55;
  outline-offset: 6px;
}

.cover > * {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;          /* the button itself takes every click */
}

/* ----- The canvas: play mark, animation and pause mark in one ----- */
#gen {
  opacity: 0;
  transition: opacity 1.8s ease;
}

#gen.ready {
  opacity: 1;
}

/* ===== CAPTION =====
   One short line at a time, set small and wide like the logo. Every line
   occupies the same place, so the caption's size never changes as the
   lines come and go, and nothing around it can shift. */
.caption {
  grid-row: 3;
  align-self: start;
  margin: var(--gap) 0 0 0;
  padding: 0 6vw;
  width: 100%;
  font-size: var(--text-fs);
  line-height: 1.9;
  text-align: center;
  user-select: none;
  -webkit-user-select: none;
}

.caption .lines {
  display: grid;
  justify-items: center;
  margin: 0;
  padding: 0;
  list-style: none;
}

.caption .lines li {
  grid-area: 1 / 1;                 /* all lines stacked in one cell */
  max-width: 100%;
  text-transform: uppercase;
  letter-spacing: 0.26em;
  padding-left: 0.26em;             /* balances the tracking after the last letter */
  color: rgba(255, 255, 255, 0.74);
  text-wrap: balance;
  opacity: 0;
  filter: blur(2px);
  transition:
    opacity var(--caption-fade) ease-in-out,
    filter var(--caption-fade) ease-in-out;
  will-change: opacity;
}

.caption .lines li.on {
  opacity: 1;
  filter: blur(0);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.noscript {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 12px;
  margin: 0;
  text-align: center;
  font-size: 13px;
  opacity: 0.7;
}

/* ===== SCREEN SIZES =====
   The artwork size adapts by itself (see :root). These only tune the
   type for tighter screens. */

@media (max-height: 600px) {
  :root { --logo-ratio: 0.16; }
}

/* Narrow screens: a little less tracking so every line stays on one row */
@media (max-width: 480px) {
  .caption { padding: 0 16px; }
  .caption .lines li {
    letter-spacing: 0.18em;
    padding-left: 0.18em;
  }
}

/* ----- Short, wide screens (phones held sideways, squat windows) -----
   There is no height for a stack, so the three parts sit side by side:
   logo | artwork | text. The artwork is still exactly in the centre. */
@media (orientation: landscape) and (max-height: 520px) {
  :root {
    --text-fs: clamp(9.5px, 2.9vh, 12px);
    --side-min: 11.5rem;       /* least width kept for the logo and the text */
    --cover: clamp(
      96px,
      min(var(--inner-h), calc(var(--avail-w) - 2 * var(--side-min))),
      720px
    );
    --side-w: calc((var(--avail-w) - var(--cover)) / 2);
  }

  .stage {
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    align-items: center;
  }

  .logo,
  .cover,
  .caption {
    grid-row: 1;
    align-self: center;
  }

  .logo {
    grid-column: 1;
    margin: 0;
    /* as large as the artwork suggests, but never wider than its column */
    font-size: min(calc(var(--cover) * 0.2), calc(var(--side-w) / 4.4));
  }

  .cover { grid-column: 2; }

  .caption {
    grid-column: 3;
    margin: 0;
    padding: 0 clamp(12px, 2.4vw, 32px);
    line-height: 2;
  }

  .caption .lines li {
    letter-spacing: 0.2em;
    padding-left: 0.2em;
  }
}

/* ===== MOTION PREFERENCES ===== */
@media (prefers-reduced-motion: reduce) {
  .caption .lines li { filter: none; transition: opacity var(--caption-fade) ease; }
}
