/* ============================================================
   Festival Gallery — luxury masonry + woven testimonials
   Scroll & reveal (v2): reserved boxes, staggered fade,
   dual-gate reveal (loaded + due), zero-reflow lazy loading.
   ============================================================ */

.gallery-page {
  padding: 4.5rem 1.5rem 6rem;
  background:
    linear-gradient(180deg, rgba(250, 243, 228, 0.55) 0%, rgba(241, 226, 196, 0.45) 100%),
    url('../img/festivals-bg.jpg') center/cover no-repeat fixed;
  background-color: var(--cream-soft);
}

.gallery-head {
  max-width: 820px;
  margin: 0 auto 3.5rem;
}
.gallery-head .section-sub-body { margin-bottom: 0; }

/* ---- Masonry ---- */
.gallery-masonry {
  max-width: 1240px;
  margin: 0 auto;
  column-count: 3;
  column-gap: 1.4rem;
}
@media (max-width: 900px) { .gallery-masonry { column-count: 2; } }
@media (max-width: 560px) { .gallery-masonry { column-count: 1; max-width: 460px; } }

/* ---- Hero row — guaranteed all-image top row, uniform across breakpoints.
   Sits above the masonry; shows immediately (eager) for a fast first paint. ---- */
.gallery-hero {
  max-width: 1240px;
  margin: 0 auto 2rem;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1.4rem;
}
@media (max-width: 900px) { .gallery-hero { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 560px) { .gallery-hero { grid-template-columns: 1fr; max-width: 460px; } }
/* Keep the hero item count matched to the column count so the last row is
   never a partial row with a blank cell. Hero picks are landscape and not
   part of the masonry, so a hidden one simply doesn't show at that width.
   To support a future 4-column hero, raise HERO_COUNT (app.py) and add a
   wider breakpoint with `:nth-child(n+5)`. */
.gallery-hero .gallery-hero-item:nth-child(n+4) { display: none; }          /* 3 columns → max 3 */
@media (max-width: 900px) {
  .gallery-hero .gallery-hero-item:nth-child(n+3) { display: none; }        /* 2 columns → max 2 */
}
/* Scoped under .gallery-hero so it beats the later `.gallery-item` aspect-ratio
   and margin rules (equal specificity would otherwise let those win). */
.gallery-hero .gallery-hero-item {
  margin: 0;
  aspect-ratio: 3 / 2;   /* uniform tidy row regardless of each source ratio */
}

.gallery-masonry > * {
  break-inside: avoid;
  margin-bottom: 2rem;
}

/* Reveal state — shared by the hero row and the masonry. Each tile gets
   --reveal-delay and --reveal-dur as custom props. The fade begins once the
   tile is in view AND its image is ready (set via JS). Two-phase reveal:
   card fades to ~50% first, then slowly resolves to full opacity for a
   softer, less harsh entrance. */
.gallery-hero > *,
.gallery-masonry > * {
  opacity: 0;
  transform: translateY(8px);
  transition: opacity var(--reveal-dur, 0.7s) cubic-bezier(0.25, 0.46, 0.45, 0.94),
              transform var(--reveal-dur, 0.7s) cubic-bezier(0.25, 0.46, 0.45, 0.94);
  transition-delay: var(--reveal-delay, 0ms);
  will-change: opacity, transform;
}
.gallery-hero > *.is-revealed,
.gallery-masonry > *.is-revealed {
  opacity: 0.5;
  transform: none;
}
.gallery-hero > *.is-revealed.is-resolved,
.gallery-masonry > *.is-revealed.is-resolved {
  opacity: 1;
  transition: opacity 1.2s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
/* A broken image simply leaves an empty spot rather than an empty card. */
.gallery-item.is-broken {
  display: none;
}

/* ---- Image tiles — reserved box via aspect-ratio ---- */
.gallery-item {
  position: relative;
  margin: 0 0 2rem;
  border-radius: 6px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: var(--shadow-soft);
  outline: 1px solid rgba(212, 162, 76, 0.35);
  outline-offset: 0;
  /* Reserved space from manifest dimensions — prevents reflow on load. */
  aspect-ratio: var(--ar, 3 / 2);
}
.gallery-item img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.65s cubic-bezier(0.22, 0.61, 0.36, 1);
}
.gallery-frame {
  position: absolute;
  inset: 10px;
  border: 1px solid rgba(255, 255, 255, 0.7);
  border-radius: 3px;
  opacity: 0;
  transition: opacity 0.4s ease, inset 0.4s ease;
  pointer-events: none;
}
.gallery-item::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(58, 36, 24, 0) 55%, rgba(58, 36, 24, 0.45) 100%);
  opacity: 0;
  transition: opacity 0.4s ease;
  z-index: 1;
}
.gallery-item:hover img,
.gallery-item:focus-visible img { transform: scale(1.06); }
.gallery-item:hover .gallery-frame,
.gallery-item:focus-visible .gallery-frame { opacity: 1; inset: 12px; }
.gallery-item:hover::before,
.gallery-item:focus-visible::before { opacity: 1; }
.gallery-item:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }

/* ---- Testimonial cards woven in ---- */
.gallery-quote {
  position: relative;
  margin: 0 0 2rem;
  padding: 2.2rem 1.75rem 1.75rem;
  background: linear-gradient(180deg, var(--cream-soft) 0%, #fff 100%);
  border: 1px solid rgba(212, 162, 76, 0.4);
  border-radius: 6px;
  box-shadow: var(--shadow-soft);
  text-align: center;
}
.gallery-quote-mark {
  display: block;
  font-family: 'Cormorant Garamond', serif;
  font-size: 3.4rem;
  line-height: 0.6;
  color: var(--gold);
  margin-bottom: 0.5rem;
}
.gallery-quote p {
  font-family: 'Cormorant Garamond', serif;
  font-style: italic;
  font-size: 1.22rem;
  line-height: 1.5;
  color: var(--brown-deep);
  margin: 0 0 1.1rem;
}
.gallery-quote footer {
  font-size: 0.72rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--gold-deep);
  font-weight: 600;
}
.gallery-quote footer span {
  display: block;
  margin-top: 0.3rem;
  letter-spacing: 0.14em;
  color: var(--text-soft);
  font-weight: 500;
}

.gallery-empty {
  text-align: center;
  font-family: 'Cormorant Garamond', serif;
  font-style: italic;
  font-size: 1.3rem;
  color: var(--text-soft);
  padding: 3rem 0;
}

/* ---- Gold custom scrollbar (scoped to gallery page) ---- */
.gallery-page::-webkit-scrollbar {
  width: 10px;
}
.gallery-page::-webkit-scrollbar-track {
  background: rgba(212, 162, 76, 0.08);
  border-radius: 5px;
}
.gallery-page::-webkit-scrollbar-thumb {
  background: rgba(212, 162, 76, 0.45);
  border-radius: 5px;
}
.gallery-page::-webkit-scrollbar-thumb:hover {
  background: rgba(212, 162, 76, 0.65);
}
.gallery-page {
  scrollbar-width: thin;
  scrollbar-color: rgba(212, 162, 76, 0.45) rgba(212, 162, 76, 0.08);
}

/* ---- Lightbox — staggered open, lazy image fade-in ---- */
.gallery-lightbox {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2.5rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s ease;
}
.gallery-lightbox.open { opacity: 1; pointer-events: auto; }
.gallery-lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(28, 16, 10, 0.92);
  backdrop-filter: blur(2px);
}
/* Content wrapper: image + controls rise in together after backdrop opens.
   Controls stay hidden until the image loads (is-ready class added by JS). */
.gallery-lightbox-content {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transform: translateY(16px) scale(0.97);
  transition: opacity 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0.2s,
              transform 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0.2s;
}
.gallery-lightbox.open .gallery-lightbox-content {
  opacity: 1;
  transform: none;
}
/* Controls: hidden until image is ready. */
.gallery-lightbox-close,
.gallery-lightbox-nav {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s ease, background 0.2s, transform 0.2s;
}
.gallery-lightbox-content.is-ready .gallery-lightbox-close,
.gallery-lightbox-content.is-ready .gallery-lightbox-nav {
  opacity: 1;
  pointer-events: auto;
}
/* Image: fades in when lazy-loaded image is ready.
   Fixed container box so portrait and landscape images display at the same
   visual size — the box is sized to the most common ratio (4:3 / 1.33).
   Images use object-fit: contain so they fill the box without cropping. */
/* Stage: fixed box that holds both stacked images for the crossfade. */
.gallery-lightbox-stage {
  position: relative;
  z-index: 1;
  width: min(92vw, 1100px);
  height: min(88vh, 92vw * 0.75);
  max-height: 88vh;
}
/* Spinner: shown while an image is downloading (is-loading on the stage). */
.gallery-lightbox-spinner {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 42px;
  height: 42px;
  margin: -21px 0 0 -21px;
  z-index: 0;
  border-radius: 50%;
  border: 3px solid rgba(212, 162, 76, 0.25);
  border-top-color: rgba(212, 162, 76, 0.9);
  opacity: 0;
  transition: opacity 0.25s ease;
}
.gallery-lightbox-stage.is-loading .gallery-lightbox-spinner {
  opacity: 1;
  animation: gallery-spin 0.8s linear infinite;
}
@keyframes gallery-spin { to { transform: rotate(360deg); } }
.gallery-lightbox-img {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  border-radius: 4px;
  box-shadow: 0 30px 90px rgba(0, 0, 0, 0.6);
  border: 3px solid rgba(212, 162, 76, 0.6);
  object-fit: contain;
  object-position: center;
  background: #1a1008;
  opacity: 0;
  transition: opacity 0.9s cubic-bezier(0.22, 0.61, 0.36, 1);
}
.gallery-lightbox-img.is-loaded {
  opacity: 1;
}
.gallery-lightbox-close {
  position: absolute;
  top: 1.2rem;
  right: 1.4rem;
  z-index: 2;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 1px solid rgba(212, 162, 76, 0.5);
  background: rgba(0, 0, 0, 0.35);
  color: var(--cream-soft);
  font-size: 1.4rem;
  cursor: pointer;
}
.gallery-lightbox-close:hover { background: var(--gold); color: #fff; transform: rotate(90deg); }
.gallery-lightbox-nav {
  position: absolute;
  z-index: 2;
  top: 50%;
  transform: translateY(-50%);
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: 1px solid rgba(212, 162, 76, 0.45);
  background: rgba(0, 0, 0, 0.3);
  color: var(--cream-soft);
  font-size: 1.9rem;
  line-height: 1;
  cursor: pointer;
}
.gallery-lightbox-nav:hover { background: var(--gold); color: #fff; }
.gallery-lightbox-prev { left: 1.5rem; }
.gallery-lightbox-next { right: 1.5rem; }
.gallery-lightbox-prev:hover { transform: translateY(-50%) translateX(-3px); }
.gallery-lightbox-next:hover { transform: translateY(-50%) translateX(3px); }

@media (max-width: 560px) {
  .gallery-lightbox-nav { width: 44px; height: 44px; font-size: 1.5rem; }
  .gallery-lightbox-prev { left: 0.5rem; }
  .gallery-lightbox-next { right: 0.5rem; }
}

@media (prefers-reduced-motion: reduce) {
  .gallery-hero > *,
  .gallery-masonry > * { opacity: 1; transform: none; transition: none; }
  .gallery-item img, .gallery-frame, .gallery-item::before { transition: none; }
}
