/* ==========================================================================
   gallery.css — photo gallery: carousel, thumbnail grid, lightbox.
   Builds on the tokens in style.css; no hard-coded colors.
   ========================================================================== */

#photos-body {
  --gallery-fade: 700ms;              /* calm cross-fade */
  --gallery-well: var(--bg-sunken);   /* letterbox behind photos */
}

/* ---------- Carousel ---------- */
.carousel {
  position: relative;
  border-radius: var(--radius);
}
.carousel:focus-visible { outline-offset: 4px; border-radius: var(--radius); }

.carousel__stage {
  position: relative;
  /* Large, yet capped at ~60vh so the controls below stay thumb-reachable. */
  height: clamp(15rem, 62vw, 60vh);
  max-height: 60vh;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--gallery-well);
  touch-action: pan-y;               /* vertical scroll stays native; horizontal = swipe */
  user-select: none;
  -webkit-user-select: none;
}

.carousel__slide {
  position: absolute;
  inset: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--gallery-well);
  opacity: 0;
  visibility: hidden;
  z-index: 0;
  transition: opacity var(--gallery-fade) ease-in-out, visibility 0s linear var(--gallery-fade);
}
/* Outgoing slide stays put underneath while the new one fades in over it
   (no dip to the background half-way through). */
.carousel__slide.was-current {
  opacity: 1;
  visibility: visible;
  z-index: 1;
  transition: none;
}
.carousel__slide.is-current {
  opacity: 1;
  visibility: visible;
  z-index: 2;
  transition: opacity var(--gallery-fade) ease-in-out, visibility 0s;
}

.carousel__img {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  object-fit: contain;
  -webkit-user-drag: none;
}
/* Never show a half-loaded or broken image: reveal only once loaded. */
.carousel__img:not(.is-loaded),
.gallery-thumb__img:not(.is-loaded),
.lightbox__img:not(.is-loaded) { visibility: hidden; }

.carousel__caption {
  flex: 0 0 auto;
  width: 100%;
  padding: var(--space-2) var(--space-4) var(--space-3);
  text-align: center;
  font-family: var(--font-text);
  font-style: italic;
  font-size: var(--step--1);
  line-height: 1.4;
  color: var(--ink-soft);
}

/* Reduced motion (media query or live flip via JS): instant cuts. */
.carousel--cut .carousel__slide,
.carousel--cut .carousel__slide.is-current { transition: none; }

/* Controls row */
.carousel__controls {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: var(--space-3);
  margin-top: var(--space-3);
}
.carousel__nav { display: flex; gap: var(--space-2); }

.gallery-icon-btn {
  padding: 0;
  width: var(--tap);
  height: var(--tap);
  color: var(--ink-soft);
  background: var(--bg-elev);
}
.gallery-icon-btn:hover { color: var(--accent); }
.gallery-icon-btn svg { width: 20px; height: 20px; }

.carousel__toggle {
  padding: 0 var(--space-4) 0 var(--space-3);
  color: var(--ink-soft);
  background: var(--bg-elev);
}
.carousel__toggle:hover { color: var(--accent); }
.carousel__toggle svg { width: 18px; height: 18px; flex: none; }
.carousel.is-paused .carousel__toggle { color: var(--accent); border-color: var(--accent); }

.carousel__meta {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-1);
  min-width: 0;
}
.carousel__counter {
  font-size: var(--step--1);
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
}
.carousel__meta [hidden],
.carousel [hidden] { display: none; }

/* Thin progress dots */
.carousel__dots {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
  max-width: 100%;
}
.carousel__dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--border-strong);
  transition: background-color var(--dur-ui) ease, transform var(--dur-ui) ease;
}
.carousel__dot.is-current { background: var(--accent); transform: scale(1.35); }

/* Many photos: a hairline progress bar instead of a row of dots */
.carousel__bar {
  position: relative;
  width: min(12rem, 100%);
  height: 3px;
  border-radius: 3px;
  background: var(--border);
  overflow: hidden;
}
.carousel__bar-fill {
  position: absolute;
  inset: 0 auto 0 0;
  background: var(--accent);
  border-radius: inherit;
  transition: width var(--gallery-fade) ease-in-out;
}

@media (max-width: 400px) {
  .carousel__toggle-text { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
  .carousel__toggle { width: var(--tap); padding: 0; }
}

/* ---------- Thumbnail grid ---------- */
.gallery-grid-wrap { margin-top: var(--space-7); }
.gallery-grid__title {
  font-size: var(--step-1);
  color: var(--ink-soft);
  margin-bottom: var(--space-4);
}
.gallery-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(7.5rem, 30%), 1fr));
  gap: var(--space-3);
}
@media (min-width: 768px) {
  .gallery-grid { grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr)); }
}
.gallery-grid__item { margin: 0; }

.gallery-thumb {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  min-height: var(--tap);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--gallery-well);
  overflow: hidden;
  cursor: zoom-in;
  transition: border-color var(--dur-ui) ease;
}
.gallery-thumb:hover { border-color: var(--accent); }
.gallery-thumb__img {
  width: 100%;
  height: 100%;
  max-width: none;
  object-fit: cover;
  transition: opacity var(--dur-ui) ease;
}
.gallery-thumb:hover .gallery-thumb__img { opacity: 0.88; }

/* ---------- Lightbox ---------- */
html.gallery-lock { overflow: hidden; }

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 300;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  margin: 0;
  border: 0;
  color: var(--ink);
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 97%, transparent);
  display: grid;
  grid-template-columns: var(--tap) 1fr var(--tap);
  grid-template-rows: auto 1fr;
  align-items: center;
  column-gap: var(--space-3);
  padding: var(--space-3) var(--gutter) var(--space-5);
}
.lightbox:not([open]):not(div),
.lightbox[hidden] { display: none; }
.lightbox::backdrop { background: transparent; }

.lightbox__close {
  grid-column: 3;
  grid-row: 1;
  justify-self: end;
}
.lightbox__prev { grid-column: 1; grid-row: 2; }
.lightbox__next { grid-column: 3; grid-row: 2; }

.lightbox__frame {
  grid-column: 2;
  grid-row: 2;
  height: 100%;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: pan-y;
}
.lightbox__figure {
  margin: 0;
  max-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}
.lightbox__img {
  max-width: 100%;
  max-height: calc(100vh - 11rem);
  max-height: calc(100dvh - 11rem);
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  -webkit-user-drag: none;
  animation: gallery-fade-in var(--dur-fade) var(--ease-out);
}
.lightbox__caption {
  max-width: var(--measure);
  text-align: center;
  font-size: var(--step--1);
  color: var(--ink-soft);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.lightbox__caption-text { font-family: var(--font-text); font-style: italic; font-size: var(--step-0); color: var(--ink); }
.lightbox__caption-text[hidden] { display: none; }
.lightbox__count { font-variant-numeric: tabular-nums; letter-spacing: 0.04em; }
.lightbox [hidden] { display: none; }

@keyframes gallery-fade-in { from { opacity: 0; } to { opacity: 1; } }
.lightbox--cut .lightbox__img { animation: none; }

/* Phones: arrows move beneath the photo, within thumb reach */
@media (max-width: 640px) {
  .lightbox {
    grid-template-columns: 1fr auto auto;
    grid-template-rows: auto 1fr auto;
    padding-inline: var(--space-4);
  }
  .lightbox__frame { grid-column: 1 / -1; grid-row: 2; }
  .lightbox__prev { grid-column: 2; grid-row: 3; margin-top: var(--space-3); }
  .lightbox__next { grid-column: 3; grid-row: 3; margin-top: var(--space-3); }
  .lightbox__img { max-height: calc(100vh - 13rem); max-height: calc(100dvh - 13rem); }
}

@media (prefers-reduced-motion: reduce) {
  .carousel__slide,
  .carousel__slide.is-current,
  .carousel__bar-fill { transition: none; }
  .lightbox__img { animation: none; }
}

@media print {
  .carousel__controls, .lightbox { display: none !important; }
}
