/* =============================================================
   Ben's Gunsmithing — Gallery page
   Justified-row photo grid + fullscreen photo viewer, plus the
   empty state shown before any photos exist. Loaded on top of
   site.css by gallery.php.
   ============================================================= */

/* -------------------------------------------------------------
   1. Photo grid

   Photos are NEVER cropped. Below 640px it's one photo per row at
   full container width. Above that it's justified rows: each row
   scales its photos to a shared height and fills the width exactly.

   How the row math works: an item's final width is
     basis + freeSpace x (grow / sum-of-grows)
   With BOTH basis and grow proportional to the aspect ratio, every
   final width is (aspect x K) for one K per row — so every item in
   the row lands on the same height. No JS, no cropping, and the
   left-to-right order Ben sets in the admin panel is preserved.
   ------------------------------------------------------------- */
.gallery-grid {
  display: flex;
  flex-direction: column;
  gap: var(--s-xl);
}

.gallery-item {
  margin: 0;
  min-width: 0;
}

/* The image frame carries the aspect ratio; the caption sits outside it,
   so caption length never distorts the photo. */
.gallery-item__frame {
  display: block;
  aspect-ratio: var(--ar, 1);
  border: 1px solid var(--cream-dark);
  border-radius: var(--r-sm);
  overflow: hidden;
  background-color: var(--white);
  cursor: zoom-in;
  transition: border-color var(--transition);
}
/* <picture> wraps the img so a WebP <source> can be offered. Give it real
   dimensions rather than display:contents so the img's 100%/100% still
   resolves against the frame. */
.gallery-item__frame picture {
  display: block;
  width: 100%;
  height: 100%;
}
.gallery-item__frame img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover; /* aspect already matches — this only absorbs rounding */
}

/* Flat per DESIGN.md: the action color reports interactivity, nothing lifts. */
.gallery-item__frame:hover { border-color: var(--green); }
.gallery-item__frame:focus-visible {
  outline: 3px solid var(--green);
  outline-offset: 3px;
  border-color: var(--green);
}

/* Ben's description. Absent entirely when he hasn't written one. */
.gallery-item__caption {
  margin-top: var(--s-sm);
  font-size: var(--fs-small);
  line-height: 1.5;
  color: var(--charcoal);
  text-wrap: pretty;
}

/* One photo per row all the way up to 1024px. Ben's photos are detail-dense
   (multi-panel before/after collages), so a phone or tablet showing two 350px
   thumbnails shows nothing readable. Full container width is the point. */

@media (min-width: 1024px) {
  .gallery-grid {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: var(--s-md);
    /* Target row height. Sized so square photos land two per row at ~518px,
       roughly 50% wider than the old three-across grid. */
    --row-h: 460px;
  }

  .gallery-item {
    flex-grow: var(--ar, 1);
    flex-basis: calc(var(--ar, 1) * var(--row-h));
  }

  /* Absorbs the leftover space of a partial last row so a lone final photo
     stays near --row-h instead of stretching across the whole width. */
  .gallery-grid::after {
    content: '';
    flex-grow: 999999;
    flex-basis: 0;
  }

  /* Uneven caption lengths would make row bottoms ragged, so clamp here.
     The full text stays in the DOM for screen readers, and the fullscreen
     viewer always shows all of it. */
  .gallery-item__caption {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
}

/* -------------------------------------------------------------
   2. Fullscreen photo viewer (lightbox)

   Native <dialog> in the top layer, so it covers the sticky header
   and the mobile CTA bar without inventing z-index values. The
   slide track is a CSS scroll-snap container: swipe is the real
   platform gesture, not emulated pointer math.
   ------------------------------------------------------------- */
.lightbox {
  /* Reset the UA dialog box. Note the display rule lives on [open] —
     setting display here would override the UA's display:none and leave
     the dialog visible while closed. */
  margin: 0;
  padding: 0;
  border: 0;
  inset: 0;
  width: 100vw;
  max-width: 100vw;
  height: 100dvh;
  max-height: 100dvh;
  background-color: transparent;
  color: var(--cream);
  overflow: hidden;
}

.lightbox[open] {
  display: grid;
  grid-template-rows: 1fr auto;
}

/* Mountain Brown Deep, matching the hero/page-header scrim device.
   Never black (DESIGN.md, The No-Black Rule). */
.lightbox::backdrop { background-color: rgba(74, 44, 15, 0.97); }

/* --- Slide track --- */
.lightbox__track {
  display: flex;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  overscroll-behavior: contain;
  scrollbar-width: none;
  min-height: 0; /* lets the 1fr row actually shrink */
}
.lightbox__track::-webkit-scrollbar { display: none; }

.lightbox__slide {
  flex: 0 0 100%;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: var(--s-md);
  display: flex;
  align-items: center;
  justify-content: center;
  scroll-snap-align: center;
  scroll-snap-stop: always; /* one photo per swipe, no flinging past three */
}
.lightbox__slide img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain; /* the whole photo, always */
  cursor: zoom-in;
}

/* --- Zoom.
   A square photo on a portrait phone can only ever be as wide as the screen,
   so "fullscreen" alone barely beats the grid. Ben's photos are multi-panel
   before/after collages, so being able to magnify and pan is what actually
   makes the viewer useful on a phone. Tap the photo to toggle. */
.lightbox__slide--zoomed {
  overflow: auto;
  padding: 0;
  align-items: flex-start;
  justify-content: flex-start;
  scrollbar-width: none;
  overscroll-behavior: contain;
}
.lightbox__slide--zoomed::-webkit-scrollbar { display: none; }
.lightbox__slide--zoomed img {
  max-width: none;
  max-height: none;
  width: var(--zoom-w);
  height: auto;
  cursor: zoom-out;
}

/* While zoomed, dragging pans the photo instead of flipping to the next one. */
.lightbox__track--zoomed {
  overflow-x: hidden;
  scroll-snap-type: none;
}

/* --- Bottom bar: caption + counter --- */
.lightbox__bar {
  padding: var(--s-md) var(--s-lg);
  padding-bottom: max(var(--s-md), env(safe-area-inset-bottom));
  border-top: 1px solid rgba(245, 240, 232, 0.2);
  max-height: 34dvh;
  overflow-y: auto;
  text-align: center;
}

.lightbox__caption {
  margin: 0 auto var(--s-sm);
  max-width: 68ch;
  font-family: var(--font-body);
  font-size: 1.125rem;
  line-height: 1.55;
  color: var(--cream);
  text-wrap: pretty;
}
/* No caption on this photo: collapse the element, keep the bar for the counter. */
.lightbox__caption:empty { display: none; }

.lightbox__controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--s-md);
}

.lightbox__counter {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: var(--fs-small);
  letter-spacing: var(--tracking-label);
  text-transform: uppercase;
  color: var(--tan-light);
  min-width: 8ch;
}

/* --- Buttons: hero-secondary treatment (brown-deep fill, cream stroke) --- */
.lightbox__btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  padding: 0;
  background-color: var(--brown-dark);
  color: var(--cream);
  border: 2px solid var(--cream);
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: background-color var(--transition), color var(--transition);
}
.lightbox__btn:hover:not(:disabled) {
  background-color: var(--cream);
  color: var(--brown-dark);
}
.lightbox__btn:focus-visible {
  outline: 3px solid var(--cream);
  outline-offset: 3px;
}
.lightbox__btn:disabled {
  opacity: 0.35;
  cursor: default;
}
.lightbox__btn svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.5;
  stroke-linecap: square;
  stroke-linejoin: miter;
}

.lightbox__close {
  position: absolute;
  top: var(--s-md);
  right: var(--s-md);
}

/* Single photo in the gallery: nothing to navigate to. */
.lightbox--single .lightbox__nav { display: none; }

@media (min-width: 640px) {
  /* Wide enough for side arrows: lift them out of the bar onto the edges. */
  .lightbox__nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
  }
  .lightbox__nav--prev { left: var(--s-lg); }
  .lightbox__nav--next { right: var(--s-lg); }

  .lightbox__slide { padding: var(--s-xl) 88px; }
  .lightbox__close { top: var(--s-lg); right: var(--s-lg); }
}

/* --- Motion ---
   Scrolling between photos eases; the viewer itself just appears.

   There is deliberately NO entrance fade. An opacity 0 -> 1 keyframe means the
   viewer's default state is invisible, and anything that stops the animation
   from running (a throttled background tab, a headless renderer, a browser
   that skips animations) leaves it stuck at opacity 0 with the photo gone.
   A reveal must never be the only thing making content visible. DESIGN.md
   wants this site essentially static anyway. */
@media (prefers-reduced-motion: no-preference) {
  .lightbox__track { scroll-behavior: smooth; }
}

/* Page behind the viewer must not scroll (showModal alone doesn't stop iOS). */
html.lightbox-open,
html.lightbox-open body { overflow: hidden; }

/* -------------------------------------------------------------
   3. Empty state — intentional, on-brand, with the phone CTA
   ------------------------------------------------------------- */
.gallery-empty {
  max-width: var(--measure);
  margin-inline: auto;
  text-align: center;
  padding-block: var(--s-2xl);
}
.gallery-empty > * + * { margin-top: var(--s-lg); }

.gallery-empty__cta {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: var(--s-md);
}
