/**
 * Image gallery — the shared shell.
 *
 * Arrangement lives in utility classes the component emits (grid-cols, gap,
 * aspect), so this file carries only what a utility cannot express: the figure
 * reset, the crop behaviour, the caption, and the lightbox.
 *
 * Every variant shares it. A variant that needs geometry of its own adds a block
 * here under its own `--<variant>` class rather than a second stylesheet.
 */

/* --- items ------------------------------------------------------------- */

.image-gallery__item {
  margin: 0;
  min-width: 0;
}

/* The frame owns the crop, and nothing else does. Putting the aspect ratio and
   the hidden overflow on the figure clipped the figcaption out of existence. */
.image-gallery__frame {
  display: block;
  position: relative;
  overflow: hidden;
  /* WIDTH MUST BE DEFINITE, or the height ceiling shrinks the cell instead of
     cropping it. A box with an aspect-ratio and an `auto` width recomputes its
     width from the ratio when max-height clamps the height — so the featured
     lead, a square two columns wide, came back 338px instead of 520 and left
     dead space inside its own cell. With the width fixed, max-height clamps only
     the height and object-fit: cover does the cropping, which is the point. */
  width: 100%;
}

.image-gallery__frame img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

/* `auto` aspect sets no height, so the image keeps its own proportions and the
   cover crop has nothing to crop against. Masonry and justified depend on this. */
.image-gallery__frame:not([class*="aspect-"]) img {
  height: auto;
}

/* The height ceiling, published as a token on the wrapper.
 *
 * An aspect ratio is a SHAPE, not a size. Once a gallery takes the full content
 * column, a square cell two columns wide is a square two columns TALL — a
 * picture that no longer fits the screen it is displayed on.
 *
 * It is a custom property rather than a bare max-height because one variant has
 * to do arithmetic with it: featured's lead cell must come to exactly two small
 * cells plus the gap between them, or the smalls stop stacking level with it.
 *
 * Measured against the viewport with the admin bar accounted for, the same way
 * hero.css does — a logged-in editor is the person most likely to be looking. */
.image-gallery--h-sm { --plexa-gallery-cap: calc(45vh - var(--plexa-chrome-top, 0px)); }
.image-gallery--h-md { --plexa-gallery-cap: calc(60vh - var(--plexa-chrome-top, 0px)); }
.image-gallery--h-lg { --plexa-gallery-cap: calc(75vh - var(--plexa-chrome-top, 0px)); }

/* The gap's VALUE, matching the utility that actually sets it. */
.image-gallery--g-none { --plexa-gallery-gap: 0rem; }
.image-gallery--g-sm   { --plexa-gallery-gap: 0.5rem; }
.image-gallery--g-md   { --plexa-gallery-gap: 1rem; }
.image-gallery--g-lg   { --plexa-gallery-gap: 2rem; }

.image-gallery__frame { max-height: var(--plexa-gallery-cap, none); }

/* On a phone the viewport is tall and narrow, so a share of it is a much bigger
   share of the page. A 60vh image there is most of the screen. */
@media (max-width: 700px) {
  .image-gallery--h-sm { --plexa-gallery-cap: 32vh; }
  .image-gallery--h-md { --plexa-gallery-cap: 42vh; }
  .image-gallery--h-lg { --plexa-gallery-cap: 55vh; }
}

.image-gallery[data-lightbox="true"] .image-gallery__item {
  cursor: zoom-in;
}

@media (hover: hover) {
  .image-gallery__item:hover .image-gallery__frame img {
    transform: scale(1.05);
  }
}

@media (prefers-reduced-motion: reduce) {
  .image-gallery__frame img,
  .image-gallery__item:hover .image-gallery__frame img {
    transition: none;
    transform: none;
  }
}

.image-gallery__caption {
  padding: 0.5rem 0.25rem 0;
  font-size: 0.8125rem;
  line-height: 1.4;
  color: var(--color-base-content);
}

/* --- lightbox ---------------------------------------------------------- */

.image-gallery__lightbox {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 2rem 1rem;
  /* A scrim, not a surface: it composites toward pure black whatever family is
     loaded, which is the same model Plexa_Ground_Tone::scrimmed() solves and
     hero.css's .hero-overlay--dark already uses. A role token here would go pale
     in the light families and stop being a blackout. */
  background: rgba(0, 0, 0, 0.92);
}

.image-gallery__lightbox[hidden] {
  display: none;
}

.image-gallery__lightbox-image {
  max-width: min(100%, 1400px);
  /* The admin bar moves the viewport origin for a logged-in reader, and the
     preview route is admin-only, so this is measured with one present every
     time it is looked at. */
  max-height: calc(80vh - var(--plexa-chrome-top, 0px));
  object-fit: contain;
}

.image-gallery__lightbox-caption {
  margin: 0;
  max-width: 60ch;
  text-align: center;
  font-size: 0.875rem;
  /* Over a blackout, not over a theme surface. */
  color: rgba(255, 255, 255, 0.92);
}

.image-gallery__lightbox-caption:empty {
  display: none;
}

.image-gallery__lightbox-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 3rem;
  height: 3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 9999px;
  /* Chrome over a blackout, the same class of literal as the close button. */
  color: rgba(255, 255, 255, 0.95);
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.35);
}
.image-gallery__lightbox-nav:hover,
.image-gallery__lightbox-nav:focus-visible { background: rgba(0, 0, 0, 0.8); }
.image-gallery__lightbox-nav--prev { left: 1rem; }
.image-gallery__lightbox-nav--next { right: 1rem; }

@media (max-width: 700px) {
  .image-gallery__lightbox-nav { width: 2.5rem; height: 2.5rem; font-size: 1.5rem; }
  .image-gallery__lightbox-nav--prev { left: 0.5rem; }
  .image-gallery__lightbox-nav--next { right: 0.5rem; }
}

.image-gallery__lightbox-close {
  position: absolute;
  /* Under the admin bar this control is not merely misplaced, it is unclickable:
     #wpadminbar is fixed above the overlay's stacking context. */
  top: calc(1rem + var(--plexa-chrome-top, 0px));
  right: 1rem;
  width: 2.75rem;
  height: 2.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.75rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 9999px;
  /* Chrome over arbitrary media, the same case carousel-component.css registers
     for its arrows: the control sits on the photograph, not on a theme surface,
     so it carries its own plate and glyph. */
  color: rgba(255, 255, 255, 0.95);
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.35);
}

.image-gallery__lightbox-close:hover,
.image-gallery__lightbox-close:focus-visible {
  background: rgba(0, 0, 0, 0.8);
}

/* =====================================================================
 * Variants
 *
 * Each block below is one child class's arrangement and nothing else. The
 * sourcing, the frame, the caption and the lightbox are shared and live above,
 * which is what lets a page swap one variant for another by changing a class
 * name and touching no other markup.
 *
 * Column counts arrive as data-cols rather than as an authored inline style:
 * a grid can use a utility class, but CSS columns and a flex basis cannot, and
 * `attr()` is not usable as a number here.
 * ===================================================================== */

/* --- masonry ----------------------------------------------------------- */

.image-gallery--masonry { column-gap: 1rem; }
.image-gallery--masonry[data-cols="2"] { column-count: 2; }
.image-gallery--masonry[data-cols="3"] { column-count: 3; }
.image-gallery--masonry[data-cols="4"] { column-count: 4; }
.image-gallery--masonry[data-cols="5"] { column-count: 5; }
.image-gallery--masonry[data-cols="6"] { column-count: 6; }

/* A figure split across a column break is the one thing that makes a masonry
   look broken rather than organic. */
.image-gallery--masonry .image-gallery__item {
  break-inside: avoid;
  margin-bottom: 1rem;
}

@media (max-width: 700px) {
  .image-gallery--masonry[data-cols] { column-count: 2; }
}

/* --- justified --------------------------------------------------------- */

/* Equal-height rows, widths following each image's own ratio. The flex-grow
   and flex-basis come from the image's dimensions, set per cell by the
   component — computed from the picture, not authored. */
.image-gallery--justified .image-gallery__cell { min-width: 0; }
.image-gallery--justified .image-gallery__frame img { height: 220px; }
.image-gallery--justified .image-gallery__item { height: 100%; }

@media (max-width: 700px) {
  .image-gallery--justified .image-gallery__frame img { height: 150px; }
}

/* --- mosaic ------------------------------------------------------------ */

/* Equal rows, so a feature spanning two of them comes out square against the
   singles beside it. Without this the rows size to their own content and the
   2x2 stops being a 2x2. */
.image-gallery--mosaic { grid-auto-rows: 1fr; }
.image-gallery--mosaic:is(.image-gallery--h-sm, .image-gallery--h-md, .image-gallery--h-lg) {
  grid-auto-rows: auto;
}

/* --- featured ---------------------------------------------------------- */

/* THE LEAD IS EXACTLY TWO SMALL CELLS PLUS THE GAP, so the smalls stack level
   with it and the block closes square at the bottom. Nothing else makes that
   true: with each frame sized from its own aspect ratio and then clipped by a
   ceiling, the lead's height and two rows of smalls are two unrelated numbers,
   and they were — the smalls ran past the bottom of the lead by whatever the
   ceiling happened to cut off.
   So the rows come from the ceiling rather than from the images: a small cell is
   half of it less half the gap, and the lead is all of it. Heights, not aspect
   ratios, which is why aspect-ratio is cleared here. */
/* Uncapped, equal rows are what make a two-row square come out square. Capped,
   the frames carry explicit heights and the rows must size to them instead. */
.image-gallery--featured { grid-auto-rows: 1fr; }
.image-gallery--featured:is(.image-gallery--h-sm, .image-gallery--h-md, .image-gallery--h-lg) {
  grid-auto-rows: auto;
}
/* THE CAPTION IS PART OF THE CELL, which the first version of this arithmetic
   forgot. A row is frame + caption, so two rows exceed the lead by one caption
   and the block missed closing by exactly that — visible as the small column
   running past the bottom of the lead.
   Captions also differ in height: "Termite damage to structural timber" wraps to
   two lines where "A protected home" does not, so the sum only closes if the
   slot is FIXED. Hence a reserved caption height here, clamped, rather than
   whatever the text happened to need. */
:is(.image-gallery--featured, .image-gallery--mosaic) { --plexa-gallery-cap-c: 0rem; }
:is(.image-gallery--featured, .image-gallery--mosaic).image-gallery--captioned { --plexa-gallery-cap-c: 2.5rem; }

:is(.image-gallery--featured, .image-gallery--mosaic).image-gallery--captioned .image-gallery__caption {
  height: var(--plexa-gallery-cap-c);
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

/* block height = lead cell = the ceiling, so the whole thing fits the viewport.
   lead cell = lead frame + caption            -> lead frame  = cap - caption
   lead cell = 2 small cells + the gap between -> small frame = (cap - gap)/2 - caption
   The max() is a floor: a short viewport at 45vh could otherwise drive a small
   cell negative. */
:is(.image-gallery--featured, .image-gallery--mosaic):is(.image-gallery--h-sm, .image-gallery--h-md, .image-gallery--h-lg) .image-gallery__frame {
  aspect-ratio: auto;
  max-height: none;
  height: max(4rem, calc((var(--plexa-gallery-cap) - var(--plexa-gallery-gap, 1rem)) / 2 - var(--plexa-gallery-cap-c)));
}
:is(.image-gallery--featured, .image-gallery--mosaic):is(.image-gallery--h-sm, .image-gallery--h-md, .image-gallery--h-lg) .image-gallery__item--lead .image-gallery__frame {
  height: max(8rem, calc(var(--plexa-gallery-cap) - var(--plexa-gallery-cap-c)));
}

/* Without a ceiling there is nothing to divide, so the lead goes back to being
   two rows of whatever the aspect ratio makes — which is what max_height: none
   asks for. */
:is(.image-gallery--featured, .image-gallery--mosaic) .image-gallery__item--lead .image-gallery__frame { height: 100%; }

/* --- filmstrip --------------------------------------------------------- */

.image-gallery--filmstrip {
  scroll-snap-type: x mandatory;
  scrollbar-width: thin;
  padding-bottom: 0.5rem;
}
.image-gallery--filmstrip .image-gallery__item {
  scroll-snap-align: start;
  flex: 0 0 auto;
}
.image-gallery--filmstrip[data-cols="2"] .image-gallery__item { width: 48%; }
.image-gallery--filmstrip[data-cols="3"] .image-gallery__item { width: 32%; }
.image-gallery--filmstrip[data-cols="4"] .image-gallery__item { width: 24%; }
.image-gallery--filmstrip[data-cols="5"] .image-gallery__item { width: 19%; }
.image-gallery--filmstrip[data-cols="6"] .image-gallery__item { width: 16%; }

@media (max-width: 700px) {
  .image-gallery--filmstrip[data-cols] .image-gallery__item { width: 78%; }
}

/* --- carousel ---------------------------------------------------------- */

.image-gallery--carousel { overflow: hidden; }
.image-gallery__track {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  scrollbar-width: none;
}
.image-gallery__track::-webkit-scrollbar { display: none; }
.image-gallery__track .image-gallery__item {
  flex: 0 0 100%;
  scroll-snap-align: center;
}

.image-gallery__nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 2.75rem;
  height: 2.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 9999px;
  /* Chrome over arbitrary photography, the same case carousel-component.css
     registers for its own arrows: the control sits on the picture, not on a
     theme surface, so it carries its own plate and glyph. */
  color: rgba(255, 255, 255, 0.95);
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.3);
}
.image-gallery__nav:hover,
.image-gallery__nav:focus-visible { background: rgba(0, 0, 0, 0.75); }
.image-gallery__nav--prev { left: 0.75rem; }
.image-gallery__nav--next { right: 0.75rem; }

.image-gallery__dots {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
  margin-top: 0.75rem;
}
.image-gallery__dot {
  width: 0.5rem;
  height: 0.5rem;
  border-radius: 9999px;
  cursor: pointer;
  border: 0;
  background: color-mix(in oklch, var(--color-base-content) 25%, transparent);
}
.image-gallery__dot.is-current {
  background: var(--color-primary);
  transform: scale(1.35);
}

/* --- viewer ------------------------------------------------------------ */

.image-gallery__slide { display: none; }
.image-gallery__slide.is-current { display: block; }

.image-gallery__rail {
  display: flex;
  flex-wrap: wrap;
  margin-top: 0.75rem;
}
.image-gallery__thumb {
  flex: 0 0 auto;
  width: 5.5rem;
  padding: 0;
  cursor: pointer;
  background: none;
  border: 2px solid transparent;
  border-radius: 0.25rem;
  overflow: hidden;
  line-height: 0;
  opacity: 0.6;
  transition: opacity 0.2s ease;
}
.image-gallery__thumb img { width: 100%; height: 4rem; object-fit: cover; }
.image-gallery__thumb.is-current,
.image-gallery__thumb:hover,
.image-gallery__thumb:focus-visible {
  opacity: 1;
  border-color: var(--color-primary);
}

.image-gallery--viewer-left { display: flex; gap: 1rem; align-items: flex-start; }
.image-gallery--viewer-left .image-gallery__stage { flex: 1 1 auto; min-width: 0; }
.image-gallery--viewer-left .image-gallery__rail {
  flex: 0 0 6rem;
  flex-direction: column;
  margin-top: 0;
}
@media (max-width: 700px) {
  .image-gallery--viewer-left { display: block; }
  .image-gallery--viewer-left .image-gallery__rail { flex-direction: row; margin-top: 0.75rem; }
}

/* --- marquee ----------------------------------------------------------- */

.image-gallery--marquee { overflow: hidden; }
.image-gallery__marquee {
  display: flex;
  width: max-content;
  animation: plexa-gallery-marquee 40s linear infinite;
}
.image-gallery__marquee-run { display: flex; gap: inherit; }
.image-gallery--marquee .image-gallery__item { flex: 0 0 14rem; }
.image-gallery--marquee-slow .image-gallery__marquee { animation-duration: 70s; }
.image-gallery--marquee-medium .image-gallery__marquee { animation-duration: 40s; }
.image-gallery--marquee-fast .image-gallery__marquee { animation-duration: 22s; }
.image-gallery--marquee:hover .image-gallery__marquee { animation-play-state: paused; }

/* Exactly -50%, because the track is rendered twice: the second pass is at the
   first pass's starting position at the end of the cycle, so the loop has no
   seam and nothing springs back. */
@keyframes plexa-gallery-marquee {
  from { transform: translateX(0); }
  to   { transform: translateX(-50%); }
}

/* Indefinite motion is the exact thing this setting exists to stop. */
@media (prefers-reduced-motion: reduce) {
  .image-gallery__marquee {
    animation: none;
    width: 100%;
    overflow-x: auto;
  }
}
