/* ==========================================================================
   Gallery viewer — grid dump + morphing lightbox strip
   (horizontal on desktop, vertical on mobile).
   ========================================================================== */

/* The page scrollbar is switched off while the lightbox is up. Reserving its
   gutter keeps the grid the same width throughout, so the tile the closing
   morph lands on does not shift when the scrollbar comes back. No effect
   with overlay scrollbars. */
html:has(body.gallery-page) {
  scrollbar-gutter: stable;
}

body.gallery-page {
  overflow: auto;
  min-height: 100vh;
  background: var(--color-bg, #fff);
  color: var(--color-ink, #000);
}

/* Archive grid starts 300px from the top of the page. */
.gallery-main {
  padding: 300px var(--margin, 16px) var(--margin, 16px);
}

body.gallery-page .topbar {
  /* Keep the shared topbar above the grid; archive has no work-layer. */
  z-index: 10;
}

/* Fixed grid. Desktop: 6 columns, 10px / 200px gaps. Mobile (same 990px
   breakpoint as the rest of the site): 4 columns, 10px / 100px gaps.
   Tiles keep their media's own height. */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  align-items: start;
  column-gap: 10px;
  row-gap: 200px;
}

@media (max-width: 990px) {
  .gallery-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    row-gap: 100px;
  }
}

.gallery-item {
  position: relative;
  display: block;
  width: 100%;
  height: auto;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

/* Tile of the slide currently at the centre of the lightbox: its slot stays
   empty so the closing tile has somewhere to land. */
.gallery-item.is-in-lightbox {
  visibility: hidden;
  pointer-events: none;
}

.gallery-item img,
.gallery-item video {
  display: block;
  width: 100%;
  height: auto;
}

/* ---- Lightbox: one strip of every item ---- */

/* Shown/hidden with the `hidden` attribute only. The clip lives here (not on
   the viewport) so it is also the IntersectionObserver root for slide videos. */
.gallery-lightbox {
  position: fixed;
  inset: 0;
  /* Above the fixed topbar (z-index 100). On Home the lightbox lives inside
     .archive-layer, so that layer is also raised while the lightbox is open
     (see styles.css) — otherwise this z-index is trapped below the topbar. */
  z-index: 1100;
  overflow: hidden;
}

/* White field fades in under the travelling tile (grid dims in place). */
.gallery-lightbox__backdrop {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
}

.gallery-lightbox__viewport {
  position: absolute;
  inset: 0;
}

/* One continuous strip. Slide sizes, the gap and the axis are set from
   gallery.js so the JS layout maths and the rendered boxes can never
   disagree. data-axis="x": a row (desktop). data-axis="y": a column (mobile). */
.gallery-lightbox__track {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  display: flex;
  align-items: center;
  will-change: transform;
}

.gallery-lightbox__track[data-axis="y"] {
  width: 100%;
  height: auto;
  flex-direction: column;
}

.gallery-lightbox__slide {
  position: relative;
  flex: 0 0 auto;
  overflow: hidden;
}

.gallery-lightbox__slide img,
.gallery-lightbox__slide video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
}

.gallery-lightbox__slide.is-morph-source {
  visibility: hidden;
}

/* Travelling tile: the MASK box animates (left/top/width/height) while the
   media stays object-fit:cover inside — same technique as the Home cover. */
.gallery-lightbox__morph {
  position: fixed;
  z-index: 2;
  overflow: hidden;
  pointer-events: none;
  will-change: left, top, width, height;
}

.gallery-lightbox__morph > * {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* After landing it rides on its slide until the slide's own media is ready. */
.gallery-lightbox__morph.is-docked {
  position: absolute;
  inset: 0;
  z-index: 1;
  will-change: auto;
}

/* Lenis scroll proxy: scroll distance → strip distance, 1:1. Always scrolls
   vertically (wheel on desktop, native touch on mobile) whatever the strip's
   own axis. */
.gallery-lightbox__scroll {
  position: absolute;
  inset: 0;
  z-index: 3;
  overflow: auto;
  overscroll-behavior: none;
  opacity: 0;
  pointer-events: auto;
  scrollbar-width: none;
}

.gallery-lightbox__scroll::-webkit-scrollbar {
  display: none;
}

/* No wheel / swipe input while the opening or closing morph is in flight. */
.gallery-lightbox.is-opening .gallery-lightbox__scroll,
.gallery-lightbox.is-closing .gallery-lightbox__scroll {
  pointer-events: none;
}

.gallery-lightbox__scroll-inner {
  width: 1px;
  pointer-events: none;
}

body.gallery-lightbox-open {
  overflow: hidden;
}

/* ---- Mobile close button ----
   Same pill as the Project page's "All work" (.pj-mobile-allwork in
   project.css): 40px tall, 6px radius, 30% black + 40px blur, white label,
   spanning the screen with that page's 10px side margins. Desktop closes
   with a click anywhere instead, so it only exists at the mobile breakpoint. */
.gallery-lightbox__close {
  display: none;
}

@media (max-width: 990px) {
  .gallery-lightbox__close {
    position: absolute;
    left: 10px;
    right: 10px;
    bottom: 12px;
    z-index: 4; /* above the scroll proxy, which covers the whole lightbox */
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 16px;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(40px);
    -webkit-backdrop-filter: blur(40px);
    color: #ffffff;
    font-size: var(--view-btn-font);
    transition: opacity 200ms ease;
  }

  /* Out of the way while the tile is flying in or out. */
  .gallery-lightbox.is-opening .gallery-lightbox__close,
  .gallery-lightbox.is-closing .gallery-lightbox__close {
    opacity: 0;
    pointer-events: none;
  }
}

/* ---- Cross cursor (plus rotated 45°) while lightbox is open ---- */

#gallery-cursor {
  display: none;
}

@media (min-width: 991px) {
  html.gallery-has-cursor,
  html.gallery-has-cursor * {
    cursor: none !important;
  }

  html.gallery-has-cursor.gallery-cursor-pointer,
  html.gallery-has-cursor.gallery-cursor-pointer * {
    cursor: pointer !important;
  }

  #gallery-cursor {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    width: 10px;
    height: 10px;
    margin: -5px 0 0 -5px;
    z-index: 10000;
    pointer-events: none;
    color: #ffffff;
    mix-blend-mode: difference;
    opacity: 0;
    will-change: transform;
    transition: opacity 120ms linear;
  }

  #gallery-cursor.is-active {
    opacity: 1;
  }

  #gallery-cursor.is-hidden {
    opacity: 0;
  }

  #gallery-cursor .gallery-cursor__icon {
    position: absolute;
    inset: 0;
    transition: transform 280ms cubic-bezier(0.16, 1, 0.3, 1);
  }

  #gallery-cursor.is-close .gallery-cursor__icon {
    transform: rotate(45deg);
  }

  #gallery-cursor .gallery-cursor__h,
  #gallery-cursor .gallery-cursor__v {
    position: absolute;
    left: 50%;
    top: 50%;
    background: currentColor;
    transform: translate(-50%, -50%);
  }

  #gallery-cursor .gallery-cursor__h {
    width: 10px;
    height: 1.25px;
  }

  #gallery-cursor .gallery-cursor__v {
    width: 1.25px;
    height: 10px;
  }
}
