/* Fun Builds: a waterfall of real project photographs, nothing else on screen.
   Names and descriptions live in content/fun-builds.json and reach search
   engines through alt text and JSON-LD; scripts/build-fun-builds.py writes the tiles.

   Packing: every tile spans rows in proportion to its photo (--rows is
   round(24 * height / width), set per tile). One row unit (--u) is 1/24 of a
   column width, derived from the container width, so a tile's height always
   matches its photo and nothing is cropped. Each tile spans one extra row for
   the gap. grid-auto-flow: dense keeps the order close to reading order. */

.fun-builds-page main { overflow: clip; }

.waterfall { padding-block: clamp(1.25rem, 2.5vw, 2rem) var(--section-y); background: var(--paper); }
.waterfall__head { margin-bottom: clamp(1rem, 2vw, 1.5rem); }
.waterfall__head .crumb { margin-bottom: 0.6rem; color: var(--ink-2); }
.waterfall__head .project-trail { margin: 1rem 0 0; }
.waterfall__head h1 {
  margin: 0;
  font-size: var(--type-page);
  font-weight: var(--w-medium);
  line-height: 0.98;
  letter-spacing: -0.04em;
  color: var(--ink);
}

.waterfall__frame { container-type: inline-size; }
.waterfall__grid {
  --cols: 2;
  --u: calc(100cqi / (25 * var(--cols) - 1));
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-auto-rows: var(--u);
  grid-auto-flow: dense;
  column-gap: var(--u);
}
@container (min-width: 640px) { .waterfall__grid { --cols: 3; } }
@container (min-width: 1000px) { .waterfall__grid { --cols: 4; } }

.waterfall__item {
  position: relative;
  display: block;
  grid-row-end: span calc(var(--rows) + 1);
  align-self: start;
  height: calc(var(--rows) * var(--u));
  border-radius: var(--radius-small);
  overflow: hidden;
  /* The photo's own average color holds the frame until it paints. */
  background: var(--tone, color-mix(in srgb, var(--rule) 55%, var(--paper)));
  cursor: zoom-in;
}
@container (min-width: 640px) { .waterfall__item { border-radius: var(--radius-panel); } }
.waterfall__item picture { display: block; width: 100%; height: 100%; }
.waterfall__item img,
.waterfall__item video { display: block; width: 100%; height: 100%; object-fit: cover; }
.waterfall__item--video { cursor: auto; }
a.waterfall__item:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }

/* Category and section anchors from other pages land on a tile, below the sticky header. */
.waterfall__item[id],
.waterfall__anchor { scroll-margin-top: 110px; }
.waterfall__anchor { position: absolute; top: 0; left: 0; width: 1px; height: 1px; }

@media (hover: hover) and (prefers-reduced-motion: no-preference) {
  a.waterfall__item img { transition: transform 0.5s cubic-bezier(0.2, 0.7, 0.2, 1); }
  a.waterfall__item:hover img { transform: scale(1.03); }
}

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

/* --- viewer (js/fun-viewer.js) ----------------------------------------------
   A native modal dialog, full screen on near-black: close at the top, the
   photo, then previous / counter / next within thumb reach. Never cropped. */
html.viewer-open { overflow: hidden; }
.viewer {
  --focus: #fff;
  position: fixed;
  inset: 0;
  width: 100%;
  max-width: none;
  height: 100%;
  max-height: none;
  margin: 0;
  padding: 0 0 env(safe-area-inset-bottom);
  border: 0;
  background: #090b10;
  color: #e9ecf3;
  overflow: hidden;
}
.viewer[open] { display: grid; grid-template-rows: minmax(0, 1fr) auto; }
.viewer::backdrop { background: #090b10; }
.viewer:focus { outline: none; }
.viewer__close { position: absolute; z-index: 1; top: 0.75rem; right: var(--gutter); background: #090b10; }
.viewer__stage {
  display: grid;
  grid-template: minmax(0, 1fr) / minmax(0, 1fr);
  place-items: center;
  min-height: 0;
  padding: 4.5rem var(--gutter) 0.5rem;
  touch-action: pinch-zoom;
  user-select: none;
  -webkit-user-select: none;
}
/* The image box fills the stage and the photo is contained inside it, so the
   frame never changes size while the full-size file replaces the tile image. */
.viewer__img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  -webkit-user-drag: none;
}
.viewer__bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  padding: 0.5rem var(--gutter) 0.9rem;
}
.viewer__count {
  min-width: 6.5em;
  margin: 0;
  font-family: var(--mono);
  font-size: 0.8rem;
  font-weight: 400;
  letter-spacing: 0.06em;
  text-align: center;
  font-variant-numeric: tabular-nums;
  color: #c8d1e2;
}
.viewer__btn {
  display: inline-grid;
  place-items: center;
  width: 48px;
  height: 48px;
  padding: 0;
  border: 1px solid rgb(232 234 240 / 0.28);
  border-radius: 50%;
  background: transparent;
  color: #fff;
  cursor: pointer;
}
.viewer__btn svg { width: 22px; height: 22px; }
.viewer__btn:hover { background: rgb(232 234 240 / 0.1); }
.viewer__btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }
@media (min-width: 900px) {
  /* The close button sits in the side margin, clear of the photo. */
  .viewer__stage { padding: 1.5rem 6rem 0.5rem; }
  .viewer__close { top: 1.25rem; right: 1.5rem; }
}
@media (max-width: 599px) {
  /* Phones: the photo runs edge to edge. */
  .viewer__stage { padding-inline: 0; }
}
