/* The Light Comes In at Four — preview
   Copyright (c) 2026 Richard Huang. All rights reserved. */

@font-face {
  font-family: "EB Garamond";
  src: url("/fonts/ebgaramond-regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "EB Garamond";
  src: url("/fonts/ebgaramond-italic.woff2") format("woff2");
  font-weight: 400;
  font-style: italic;
  font-display: swap;
}

:root {
  /* The room before dawn. Darker and warmer than Preview's neutral gray, so the
     paper reads as the only lit thing on the screen. */
  --canvas: #1a1917;
  --chrome: rgba(40, 38, 34, 0.72);
  --edge: rgba(255, 255, 255, 0.09);
  --ink: #ece7dd;
  --muted: #8f887c;
  /* Four o'clock light. The one accent, spent on focus, hover and the blinds. */
  --four: #e3bd7c;
  /* Paper before its art arrives: the same sheet, not yet in the light. */
  --unlit: #dcd6ca;

  --page-aspect: 0.647059;
  --page-h: 70svh;
  --serif: "EB Garamond", "Iowan Old Style", Georgia, "Times New Roman", serif;
  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;

  color-scheme: dark;
}

* { box-sizing: border-box; }

/* .cover and .bar set a display, which would otherwise beat the UA rule. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--canvas);
  color: var(--ink);
  font-family: var(--serif);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* Nothing here is meant to be selected, dragged out, or saved. */
body {
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

img, .page { -webkit-user-drag: none; }

:focus-visible {
  outline: 2px solid var(--four);
  outline-offset: 3px;
  border-radius: 3px;
}

/* ---------------------------------------------------------------- cover --- */

.cover {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  isolation: isolate;
  transition: opacity 620ms cubic-bezier(0.32, 0.72, 0, 1),
              transform 620ms cubic-bezier(0.32, 0.72, 0, 1);
}

.cover.is-leaving {
  opacity: 0;
  transform: scale(1.035);
  pointer-events: none;
}

/* The book itself, behind everything, out of focus. */
.cover__page {
  --h: min(76svh, 86vw / 0.647059);
  position: absolute;
  z-index: -3;
  width: calc(var(--h) * var(--page-aspect));
  height: var(--h);
  background: url("/backdrop.webp") center / 100% 100% no-repeat;
  filter: blur(13px) brightness(0.62) saturate(0.5);
  transform: scale(1.04);
}

/* "It comes through the blinds in bars and lies down across the bed."
   The bars enter from the upper left and drift, very slowly, all day. */
.cover__blinds {
  position: absolute;
  z-index: -2;
  inset: -25%;
  background: repeating-linear-gradient(
    99deg,
    rgba(227, 189, 124, 0.15) 0 22px,
    rgba(227, 189, 124, 0) 22px 76px
  );
  -webkit-mask-image: radial-gradient(58% 52% at 20% 6%, #000 0%, transparent 74%);
  mask-image: radial-gradient(58% 52% at 20% 6%, #000 0%, transparent 74%);
  animation: fall 52s cubic-bezier(0.45, 0, 0.55, 1) infinite alternate;
}

@keyframes fall {
  from { transform: translate3d(-1.5%, -4%, 0); }
  to   { transform: translate3d(2.5%, 7%, 0); }
}

.cover__scrim {
  position: absolute;
  z-index: -1;
  inset: 0;
  background: radial-gradient(70% 60% at 50% 48%, rgba(26, 25, 23, 0.66) 0%, rgba(26, 25, 23, 0.92) 100%);
}

.cover__inner {
  padding: 0 24px;
  text-align: center;
}

.cover__title {
  margin: 0;
  font-size: clamp(2rem, 5.6vw, 3.5rem);
  font-weight: 400;
  font-variant-caps: small-caps;
  font-feature-settings: "smcp" 1;
  letter-spacing: 0.11em;
  line-height: 1.16;
  text-indent: 0.11em;
  text-wrap: balance;
}

.cover__author {
  margin: 0.85em 0 0;
  font-style: italic;
  font-size: clamp(1.05rem, 2.1vw, 1.35rem);
  color: #cfc7b8;
  letter-spacing: 0.01em;
}

.open {
  margin-top: clamp(2.4rem, 6vh, 3.4rem);
  padding: 0.82em 1.9em;
  background: transparent;
  border: 1px solid rgba(236, 231, 221, 0.34);
  border-radius: 2px;
  color: var(--ink);
  font: 400 clamp(0.95rem, 1.6vw, 1.08rem)/1 var(--serif);
  letter-spacing: 0.055em;
  cursor: pointer;
  transition: border-color 260ms ease, color 260ms ease, background-color 260ms ease;
}

.open:hover {
  border-color: var(--four);
  background: rgba(227, 189, 124, 0.08);
  color: var(--four);
}

.open:active { transform: translateY(1px); }

/* Pressed before the manifest landed. The reader opens by itself the moment it
   arrives; until then the button breathes rather than sitting there dead. */
.open.is-waiting {
  border-color: rgba(227, 189, 124, 0.42);
  color: var(--four);
  cursor: progress;
  animation: breathing 1.9s ease-in-out infinite;
}

@keyframes breathing {
  50% { opacity: 0.52; }
}

.cover__meta {
  margin: 1.9em 0 0;
  font: 400 0.8rem/1 var(--sans);
  letter-spacing: 0.05em;
  color: var(--muted);
}

/* --------------------------------------------------------------- reader --- */

.reader {
  position: fixed;
  inset: 0;
  overflow: hidden auto;
  overscroll-behavior: contain;
  scroll-snap-type: y mandatory;
  scrollbar-width: none;
  opacity: 0;
  transition: opacity 700ms ease;
}
.reader::-webkit-scrollbar { display: none; }
.reader.is-open { opacity: 1; }

.reader.is-zoomed {
  overflow: auto;
  scroll-snap-type: none;
}

/* The bottom padding is the toolbar's berth: paper is never covered by chrome. */
.spread {
  display: grid;
  place-items: center;
  min-height: 100svh;
  padding: 3svh 3vw calc(3svh + 54px);
  scroll-snap-align: center;
  scroll-snap-stop: always;
}

.reader.is-zoomed .spread { min-height: 0; padding: 2.5svh 3vw; }

/* The element the scroll animation moves. Kept separate from .spread so snap
   positions stay put while the artwork drifts. */
.spread__inner {
  display: flex;
  box-shadow: 0 16px 46px rgba(0, 0, 0, 0.5), 0 2px 8px rgba(0, 0, 0, 0.34);
  will-change: opacity, transform;
}

/* The sheet under the art is unlit rather than white, so a page that never
   arrives reads as paper in shadow instead of a hole cut in the screen. */
.page {
  position: relative;
  width: calc(var(--page-h) * var(--page-aspect));
  height: var(--page-h);
  background: var(--unlit) center / 100% 100% no-repeat;
  flex: none;
}

/* Paper waiting for its art. The sheet is there from the first frame at its
   full size, so nothing shifts when the page arrives — it is simply unlit, and
   the four o'clock light crosses it while the image is on the way. */
.page::after {
  content: "";
  position: absolute;
  inset: 0;
  background-color: var(--unlit);
  background-image: linear-gradient(
    99deg,
    rgba(255, 249, 235, 0) 36%,
    rgba(255, 240, 209, 0.85) 50%,
    rgba(255, 249, 235, 0) 64%
  );
  background-repeat: no-repeat;
  background-size: 220% 100%;
  /* The band is wider than the sheet, so a rising percentage walks it left.
     140% parks it just past the left edge — near enough that it is back on the
     paper soon after each pass rather than resting out of sight. */
  background-position: 140% 0;
  pointer-events: none;
  transition: opacity 620ms cubic-bezier(0.32, 0.72, 0, 1);
}

/* Only the pages actually being fetched move; the rest sit still, so a long
   story does not animate two dozen sheets nobody is looking at. The pace is
   steady rather than eased — both ends of the travel are off the paper, and
   easing would only slow the light down where nobody can see it. */
.page.is-waiting::after {
  animation: crossing 2.6s linear infinite;
}

/* The art is decoded and painted underneath. Lift the sheet into the light. */
.page.is-lit::after { opacity: 0; }

@keyframes crossing {
  from { background-position: 140% 0; }
  to   { background-position: -40% 0; }
}

@keyframes lightening {
  50% { background-color: #ebe5d8; }
}

/* A hint of the gutter where two pages meet, as they fall away from the spine. */
.spread__inner[data-count="2"] .page:first-child {
  box-shadow: inset -16px 0 20px -16px rgba(0, 0, 0, 0.22);
}
.spread__inner[data-count="2"] .page:last-child {
  box-shadow: inset 16px 0 20px -16px rgba(0, 0, 0, 0.22);
}

/* ------------------------------------------------------------------ bar --- */

.bar {
  position: fixed;
  z-index: 20;
  bottom: max(22px, env(safe-area-inset-bottom));
  left: 50%;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 7px;
  background: var(--chrome);
  border: 1px solid var(--edge);
  border-radius: 999px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.42);
  -webkit-backdrop-filter: blur(22px) saturate(1.5);
  backdrop-filter: blur(22px) saturate(1.5);
  transform: translate(-50%, 0);
  transition: opacity 320ms ease, transform 320ms cubic-bezier(0.32, 0.72, 0, 1);
}

.bar.is-hidden {
  opacity: 0;
  transform: translate(-50%, 14px);
  pointer-events: none;
}

.bar__group { display: flex; align-items: center; gap: 2px; }

.bar__rule {
  width: 1px;
  height: 20px;
  margin: 0 5px;
  background: var(--edge);
}

.bar__btn {
  display: grid;
  place-items: center;
  width: 32px;
  height: 30px;
  padding: 0;
  background: none;
  border: 0;
  border-radius: 999px;
  color: var(--ink);
  cursor: pointer;
  transition: background-color 160ms ease, color 160ms ease, opacity 160ms ease;
}

.bar__btn--wide { width: auto; padding: 0 10px; font: 500 12px/1 var(--sans); font-variant-numeric: tabular-nums; }

.bar__btn:hover:not(:disabled) { background: rgba(255, 255, 255, 0.1); }
.bar__btn:disabled { opacity: 0.3; cursor: default; }

.bar__btn svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#layout .bar__single { display: none; }
#layout[aria-pressed="false"] .bar__spread { display: none; }
#layout[aria-pressed="false"] .bar__single { display: block; }

.bar__count {
  min-width: 84px;
  padding: 0 4px;
  text-align: center;
  font: 500 12px/1 var(--sans);
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  white-space: nowrap;
}

/* -------------------------------------------------------------- states --- */

/* Covers the landing screen entirely, since its button leads nowhere without
   scripting. Rendered only when <noscript> is honored. */
.noscript {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-content: center;
  gap: 14px;
  padding: 24px;
  background: var(--canvas);
  text-align: center;
  font-size: 1.15rem;
  line-height: 1.5;
}
.noscript p { margin: 0; max-width: 34ch; }

/* Phones read as one continuous scroll, page after page, so the pages flow
   rather than each taking a screen, and the snap points guide rather than gate. */
.reader--flow { scroll-snap-type: y proximity; }

.reader--flow .spread {
  min-height: 0;
  padding: 7px 0;
  scroll-snap-align: center;
  scroll-snap-stop: normal;
}
.reader--flow .spread:first-child { padding-top: 4svh; }
.reader--flow .spread:last-child { padding-bottom: 12svh; }
.reader--flow .spread__inner { box-shadow: 0 6px 20px rgba(0, 0, 0, 0.42); }

@media (max-width: 860px) {
  .bar { gap: 2px; padding: 4px 6px; bottom: max(16px, env(safe-area-inset-bottom)); }
  .bar__group--desktop, .bar__rule--desktop { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .cover__blinds { animation: none; }
  .cover, .reader, .bar { transition-duration: 1ms; }
  .spread__inner { transform: none !important; }
  .reader { scroll-behavior: auto; }

  /* Still say that something is happening, but by letting the whole sheet come
     up and go down in the light rather than moving anything across the screen. */
  .page.is-waiting::after { animation: lightening 2.8s ease-in-out infinite; }
}

/* A preview is for reading on a screen. There is nothing here to print. */
@media print {
  body > * { display: none !important; }
  body::after {
    content: "The Light Comes In at Four — Richard Huang. Preview only; not available for printing.";
    display: block;
    padding: 4rem 2rem;
    color: #000;
    font: 400 12pt/1.6 serif;
  }
}
