/*
 * Stage 2A foundations: shared spacing/type tokens plus the figure, gallery and lightbox components.
 * Uses the page's existing theme variables (--line, --muted, --surface, --accent, ...), so all three themes apply.
 * Markup comes from public/figures.js.
 */
:root {
  /* spacing scale (rem, 4px-based steps) */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;
  --space-8: 4.5rem;
  --space-9: 7rem;
  /* figure and caption type */
  --fs-caption: 0.875rem;
  --fs-fig-label: 0.75rem;
  --lh-caption: 1.45;
  --ls-fig-label: 0.14em;
  --rule: 1px;
  --measure-caption: 46ch;
  --tone-filter: grayscale(1) contrast(1.05) brightness(0.96);
}

/* ---- Figure ---- */
.fig { margin: 0; min-width: 0; }
.fig__open {
  position: relative;
  display: block;
  width: 100%;
  padding: 0;
  border: var(--rule) solid var(--line);
  background: var(--surface);
  color: inherit;
  cursor: zoom-in;
  overflow: hidden;
}
.fig__open:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.fig__open img { display: block; width: 100%; height: auto; }
.fig__cap {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1) var(--space-3);
  align-items: baseline;
  max-width: var(--measure-caption);
  margin-top: var(--space-2);
  color: var(--muted);
  font-size: var(--fs-caption);
  line-height: var(--lh-caption);
}
.fig__no {
  color: var(--accent-text, var(--accent));
  font-size: var(--fs-fig-label);
  font-weight: 600;
  letter-spacing: var(--ls-fig-label);
  text-transform: uppercase;
  font-variant-numeric: tabular-nums;
}
.fig--native { max-width: var(--fig-native, 100%); }

/* Selective tone: greyscale that returns to colour on hover/focus. Opt in per figure; never global. */
.fig--tone .fig__open img { filter: var(--tone-filter); transition: filter 400ms ease; }
.fig--tone .fig__open:hover img,
.fig--tone .fig__open:focus-visible img { filter: none; }
@media (hover: none) { .fig--tone .fig__open img { filter: none; } }

/* ---- Gallery ---- */
.gallery { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-7); }
.gallery__group { min-width: 0; }
.gallery__group-title {
  margin-bottom: var(--space-4);
  color: var(--muted);
  font-size: var(--fs-fig-label);
  font-weight: 600;
  letter-spacing: var(--ls-fig-label);
  text-transform: uppercase;
}
/* mosaic: justified rows. Every photo keeps its natural ratio (no cropping, no bars); photos in a row share one height
   and reading order follows the figure numbers. --ratio is set per figure from the image's real dimensions. */
.gallery--mosaic .gallery__items,
.gallery--lead .gallery__items { display: flex; flex-wrap: wrap; gap: var(--space-5) var(--space-4); }
.gallery--mosaic .fig,
.gallery--lead .fig {
  flex: calc(var(--ratio, 1.5) * 100) 1 calc(var(--ratio, 1.5) * 15rem);
  max-width: min(var(--fig-native, 100%), calc(var(--ratio, 1.5) * 32rem));
}
.gallery--lead .fig:first-child { flex-basis: 100%; max-width: none; }
.gallery--lead .fig:first-child .fig__open { max-height: min(80vh, 44rem); }
.gallery--lead .fig:first-child .fig__open img { max-height: inherit; object-fit: contain; margin-inline: auto; }
/* strip: one scrolling row at a fixed height, good for long sets */
.gallery--strip .gallery__items {
  display: flex;
  gap: var(--space-4);
  padding-bottom: var(--space-3);
  overflow-x: auto;
  scroll-snap-type: x proximity;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}
.gallery--strip .fig { flex: 0 0 auto; min-width: 11rem; scroll-snap-align: start; }
.gallery--strip .fig__open { height: clamp(14rem, 36vw, 24rem); width: auto; }
.gallery--strip .fig__open img { width: auto; height: 100%; max-width: none; }
.gallery--strip .fig__cap { max-width: none; }
@media (max-width: 700px) {
  .gallery--mosaic .fig,
  .gallery--lead .fig { flex: 1 1 100%; max-width: min(var(--fig-native, 100%), 100%); }
  .gallery--strip .fig__open { height: clamp(12rem, 62vw, 18rem); }
}

/* ---- Lightbox ---- */
html.lightbox-open { overflow: hidden; }
.lightbox {
  position: fixed;
  inset: 0;
  width: 100%;
  max-width: none;
  height: 100dvh;
  max-height: none;
  margin: 0;
  padding: clamp(1rem, 3vw, 2rem);
  border: 0;
  background: rgba(var(--background-rgb, 16, 16, 16), 0.97);
  color: var(--foreground);
}
.lightbox[open] { display: grid; place-items: center; }
.lightbox::backdrop { background: rgba(var(--background-rgb, 16, 16, 16), 0.96); }
.lightbox__layout {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: var(--space-4);
  width: min(100%, 1400px);
  height: 100%;
}
.lightbox__header,
.lightbox__footer { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); }
.lightbox__title,
.lightbox__count {
  color: var(--muted);
  font-size: var(--fs-fig-label);
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}
.lightbox__count { white-space: nowrap; font-variant-numeric: tabular-nums; }
.lightbox__button {
  display: grid;
  place-items: center;
  width: 2.75rem;
  height: 2.75rem;
  border: var(--rule) solid var(--line-strong);
  background: rgba(var(--background-rgb, 16, 16, 16), 0.8);
  color: var(--foreground);
  font-size: 1.5rem;
  cursor: pointer;
  transition: border-color 180ms ease, color 180ms ease;
}
.lightbox__button:hover { border-color: var(--accent); color: var(--accent-text, var(--accent)); }
.lightbox__button:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
.lightbox__stage { position: relative; display: grid; min-height: 0; place-items: center; touch-action: pan-y; }
.lightbox__image {
  display: block;
  width: auto;
  height: auto;
  max-width: calc(100vw - 7rem);
  max-height: calc(100dvh - 11.5rem);
  object-fit: contain;
}
.lightbox__step { position: fixed; top: 50%; z-index: 1; transform: translateY(-50%); }
.lightbox__step--previous { left: clamp(1rem, 3vw, 2.5rem); }
.lightbox__step--next { right: clamp(1rem, 3vw, 2.5rem); }
.lightbox__caption { color: var(--foreground); font-size: 1rem; line-height: var(--lh-caption); }
.lightbox__no {
  color: var(--accent-text, var(--accent));
  font-size: var(--fs-fig-label);
  font-weight: 600;
  letter-spacing: var(--ls-fig-label);
  text-transform: uppercase;
}
@media (max-width: 700px) {
  .lightbox { padding: var(--space-4); }
  .lightbox__image { max-width: calc(100vw - 2rem); max-height: calc(100dvh - 11rem); }
  .lightbox__button { width: 2.5rem; height: 2.5rem; }
  .lightbox__step { top: auto; bottom: 4.25rem; transform: none; }
  .lightbox__step--previous { left: var(--space-4); }
  .lightbox__step--next { right: var(--space-4); }
  .lightbox__caption { font-size: var(--fs-caption); max-width: 60vw; }
}
@media (prefers-reduced-motion: reduce) {
  .fig--tone .fig__open img { transition: none; }
}
