/* ============================================================================
   MANDELSON.CO — Masonry image gallery + full-screen lightbox
   Reusable. Reuses the site's design tokens (colors, radii, spacing).
   Pair with: src/_includes/masonry-gallery.njk + src/js/lightbox.js
   ============================================================================ */

.mgl-section {
  margin: var(--space-2xl) 0;
}

.mgl-heading {
  margin-bottom: var(--space-xl);
}

/* --- Masonry grid via CSS columns (preserves natural aspect ratios) -------
   Edge-to-edge: no column gaps, no per-tile margin, no rounded corners.
   `overflow: visible` so a hover-scaled tile can lift over its neighbours. */
.masonry-gallery {
  column-count: 4;
  column-gap: 0;
  overflow: visible;
}

@media (max-width: 1100px) {
  .masonry-gallery { column-count: 3; }
}

/* Mobile: keep 3 columns so tiles stay small and the 50+ image page stays
   short. Only drop to 2 on very narrow phones where 3 would be unusably tiny. */
@media (max-width: 768px) {
  .masonry-gallery { column-count: 3; }
}

@media (max-width: 340px) {
  .masonry-gallery { column-count: 2; }
}

.masonry-gallery__tile {
  display: block;
  width: 100%;
  padding: 0;
  margin: 0;              /* tiles butt up edge-to-edge */
  border: none;
  background: var(--bg-dark-alt);
  border-radius: 0;       /* clean slate, no rounded corners */
  overflow: hidden;
  cursor: pointer;
  /* Crisp taps: kill the 300ms tap delay and double-tap-zoom so a tap on a
     tile fires click immediately instead of being swallowed by gesture
     disambiguation on touch devices. */
  touch-action: manipulation;
  break-inside: avoid;
  -webkit-column-break-inside: avoid;
  position: relative;
  line-height: 0;
  transform-origin: center;
  transition: transform 0.25s var(--ease-out-cubic),
              box-shadow 0.25s var(--ease-out-cubic);
}

.masonry-gallery__tile img {
  display: block;
  width: 100%;
  height: auto;          /* keep natural aspect ratio, no square crop */
  object-fit: contain;
}

.masonry-gallery__tile::after {
  content: "";
  position: absolute;
  inset: 0;
  box-shadow: inset 0 0 0 1px var(--border-on-dark);
  opacity: 0;
  transition: opacity 0.25s var(--ease-out-cubic);
  pointer-events: none;
}

/* Keyboard focus: ring only, no scale (avoids clipping surprises). */
.masonry-gallery__tile:focus-visible {
  outline: none;
  z-index: 3;
  box-shadow: 0 0 0 3px var(--accent), 0 16px 40px rgba(0, 0, 0, 0.45);
}

.masonry-gallery__tile:focus-visible::after {
  opacity: 1;
}

/* Smooth hover enlarge — DESKTOP POINTER DEVICES ONLY.
   Gated so touch/mobile (no true hover) never triggers the "pop".
   The scaled tile raises z-index and casts a shadow so it lifts above and
   slightly overlaps its neighbours. Transform-scale (not literal reflow),
   because reflowing CSS columns on hover is janky. */
@media (hover: hover) and (pointer: fine) {
  .masonry-gallery__tile:hover {
    transform: scale(1.06);
    z-index: 4;
    box-shadow: 0 16px 44px rgba(0, 0, 0, 0.5);
  }
  .masonry-gallery__tile:hover::after {
    opacity: 1;
  }
}

/* ============================================================================
   Full-screen lightbox
   ============================================================================ */
.mgl-lightbox {
  position: fixed;
  inset: 0;
  z-index: 10001;
  background: rgba(0, 0, 0, 0.94);
  backdrop-filter: blur(20px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.35s ease, visibility 0.35s;
}

.mgl-lightbox.is-open {
  opacity: 1;
  visibility: visible;
}

/* Two columns on wide viewports: image left, info panel right. */
.mgl-lightbox__content {
  margin: 0;
  max-width: 94vw;
  max-height: 90vh;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: var(--space-xl);
}

/* Left: image stage, scaled to fit height. */
.mgl-lightbox__stage {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mgl-lightbox__img {
  max-width: 100%;
  max-height: 90vh;
  width: auto;
  height: auto;
  object-fit: contain;      /* full detail visible, never cropped */
  border-radius: 0;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
}

/* Right: info panel. Scrolls internally; scroll never chains to the page. */
.mgl-lightbox__info {
  flex: 0 0 clamp(280px, 28vw, 400px);
  display: flex;
  flex-direction: column;
  max-height: 90vh;
  overflow-y: auto;
  overscroll-behavior: contain;   /* stop scroll chaining to <body> */
  padding-right: var(--space-sm);
}

.mgl-lightbox__caption {
  color: rgba(255, 255, 255, 0.82);
  font-size: var(--text-sm);
  line-height: 1.6;
  margin: 0;
  text-align: left;
}

/* Subtle note for upscales whose prompt was traced from the base generation. */
.mgl-lightbox__note {
  margin: var(--space-sm) 0 0;
  color: rgba(255, 255, 255, 0.45);
  font-size: var(--text-xs);
  line-height: 1.5;
  font-style: italic;
  text-align: left;
}

.mgl-lightbox__note[hidden] {
  display: none;
}

.mgl-lightbox__meta {
  margin: var(--space-lg) 0 0;
  padding-top: var(--space-md);
  border-top: 1px solid var(--border-on-dark);
  display: grid;
  gap: var(--space-sm);
}

.mgl-lightbox__meta-row {
  display: flex;
  align-items: baseline;
  gap: var(--space-sm);
}

.mgl-lightbox__meta dt {
  flex: 0 0 auto;
  color: rgba(255, 255, 255, 0.45);
  font-size: var(--text-xs);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.mgl-lightbox__meta dd {
  margin: 0;
  color: rgba(255, 255, 255, 0.82);
  font-size: var(--text-sm);
  word-break: break-word;
}

.mgl-lightbox__counter {
  color: rgba(255, 255, 255, 0.45);
  font-size: var(--text-xs);
  letter-spacing: 0.08em;
  margin: var(--space-md) 0 0;
  text-align: left;
}

.mgl-lightbox__close,
.mgl-lightbox__nav {
  position: absolute;
  color: white;
  border: none;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  z-index: 2;
  transition: background 0.3s ease;
}

.mgl-lightbox__close {
  top: var(--space-xl);
  right: var(--space-xl);
  background: rgba(255, 255, 255, 0.08);
}

.mgl-lightbox__close:hover,
.mgl-lightbox__close:focus-visible {
  background: rgba(255, 255, 255, 0.18);
  outline: none;
}

.mgl-lightbox__nav {
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255, 255, 255, 0.1);
}

.mgl-lightbox__nav:hover,
.mgl-lightbox__nav:focus-visible {
  background: rgba(255, 255, 255, 0.22);
  outline: none;
}

.mgl-lightbox__nav--prev { left: var(--space-xl); }
.mgl-lightbox__nav--next { right: var(--space-xl); }

.mgl-lightbox__close:focus-visible,
.mgl-lightbox__nav:focus-visible {
  box-shadow: 0 0 0 3px var(--accent);
}

/* Narrow viewports: stack — image on top, info panel (scrollable) below. */
@media (max-width: 768px) {
  .mgl-lightbox {
    align-items: stretch;
  }
  .mgl-lightbox__content {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-md);
    max-width: 94vw;
    max-height: 92vh;
    padding: calc(var(--space-xl) + 24px) var(--space-md) var(--space-md);
  }
  .mgl-lightbox__stage {
    flex: 0 0 auto;
    max-height: 58vh;
  }
  .mgl-lightbox__img {
    max-height: 58vh;
  }
  .mgl-lightbox__info {
    flex: 1 1 auto;
    max-height: none;               /* scroll within the flex column */
    min-height: 0;
    padding-right: 0;
  }
  .mgl-lightbox__nav {
    top: auto;
    bottom: var(--space-md);
    transform: none;
  }
  .mgl-lightbox__nav--prev { left: var(--space-lg); }
  .mgl-lightbox__nav--next { right: var(--space-lg); }
}
