/* Single product gallery (includes/product-gallery.php, js/product-gallery.js).
   Replaces the Twist/slick gallery. Mobile: scroll-snap track with the thumbnail row
   under it. From 1025px (the breakpoint the old layout used) the thumbnails run down
   the left, 10.5% wide against an 87% photo, the strip clipped to the photo's height.
   The frame takes the hero image's proportions (--stw-gallery-ratio, square for nearly
   everything) and other slides are contained in it, so nothing crops and nothing shifts. */

.stw-gallery {
  --stw-gallery-ratio: 1;
  position: relative;
  display: grid;
  gap: 12px;
}

.stw-gallery__main {
  position: relative;
  min-width: 0;
  background: var(--stw-grey-50);
  border-radius: var(--stw-radius-lg);
  overflow: hidden;
}

.stw-gallery__track {
  display: flex;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scroll-snap-type: x mandatory;
  scrollbar-width: none;
}

.stw-gallery__track::-webkit-scrollbar {
  display: none;
}

.stw-gallery__track.is-locked {
  overflow-x: hidden;
}

.stw-gallery__slide {
  position: relative;
  flex: 0 0 100%;
  aspect-ratio: var(--stw-gallery-ratio);
  scroll-snap-align: start;
  scroll-snap-stop: always;
}

.stw-gallery__link {
  display: block;
  width: 100%;
  height: 100%;
  cursor: zoom-in;
}

.stw-gallery__img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* The corner affordance the old gallery had: the whole photo is the link, the icon only says so. */
.stw-gallery__link::after {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  width: 40px;
  height: 40px;
  border-radius: 0 0 0 var(--stw-radius-md);
  background: #e9ecef url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M15 3h6v6M9 21H3v-6M21 3l-7 7M3 21l7-7'/%3E%3C/svg%3E") center / 18px no-repeat;
  pointer-events: none;
}

.stw-gallery__link:focus-visible,
.stw-gallery__thumb:focus-visible,
.stw-gallery__nav:focus-visible {
  outline: 3px solid var(--stw-ink);
  outline-offset: -3px;
}

/* Video slide (includes/woocommerce-gallery-video.php): letterboxed on black, never cropped. */
.stw-gallery-video {
  background: #000;
}

.stw-gallery-video video {
  display: block;
  width: 100%;
  height: 100%;
  background: #000;
  object-fit: contain;
}

/* Arrows: only once the script is running (without it the track still swipes and scrolls). */
.stw-gallery__nav {
  display: none;
  position: absolute;
  top: 50%;
  z-index: 2;
  width: 35px;
  height: 35px;
  margin: -17px 0 0;
  padding: 0;
  border: 0;
  border-radius: var(--stw-radius-pill);
  background: color-mix(in srgb, var(--stw-cyan) 72%, transparent);
  cursor: pointer;
  transition: background 0.15s ease;
}

.stw-gallery.is-ready .stw-gallery__nav {
  display: block;
}

.stw-gallery__nav:hover {
  background: var(--stw-cyan);
}

.stw-gallery__nav--prev {
  left: 16px;
}

.stw-gallery__nav--next {
  right: 16px;
}

.stw-gallery__nav::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 8px;
  height: 8px;
  border-top: 2px solid #fff;
  border-right: 2px solid #fff;
}

.stw-gallery__nav--next::before {
  margin: -4px 0 0 -6px;
  transform: rotate(45deg);
}

.stw-gallery__nav--prev::before {
  margin: -4px 0 0 -2px;
  transform: rotate(-135deg);
}

/* Thumbnails: a row under the photo, four across, scrolling sideways. */
.stw-gallery__thumbs-list {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}

.stw-gallery__thumbs-list::-webkit-scrollbar {
  display: none;
}

.stw-gallery__thumb {
  position: relative;
  flex: 0 0 calc((100% - 24px) / 4);
  aspect-ratio: 1;
  padding: 0;
  border: 2px solid transparent;
  border-radius: var(--stw-radius-sm);
  background: var(--stw-grey-50);
  overflow: hidden;
  opacity: 0.55;
  cursor: pointer;
  transition: opacity 0.15s ease, border-color 0.15s ease;
}

.stw-gallery__thumb:hover,
.stw-gallery__thumb[aria-current] {
  opacity: 1;
}

.stw-gallery__thumb[aria-current] {
  border-color: var(--stw-pink);
}

.stw-gallery__thumb-img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.stw-gallery-video-thumb::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 1.5rem;
  height: 1.5rem;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23fff' d='M9 7v10l8-5z'/%3E%3C/svg%3E") center / 100% no-repeat;
  pointer-events: none;
}

@media (min-width: 1025px) {
  .stw-gallery--multi {
    grid-template-columns: 10.5% 87%;
    justify-content: space-between;
    gap: 0;
  }

  .stw-gallery--multi .stw-gallery__main {
    grid-column: 2;
    grid-row: 1;
  }

  /* The strip is out of flow so the photo alone sets the row height; the strip fills it and scrolls. */
  .stw-gallery__thumbs {
    position: relative;
    grid-column: 1;
    grid-row: 1;
  }

  .stw-gallery__thumbs-list {
    position: absolute;
    inset: 0;
    flex-direction: column;
    overflow: hidden auto;
  }

  .stw-gallery__thumb {
    flex: 0 0 auto;
    width: 100%;
  }

  /* The badge belongs on the photo, which starts after the strip (100% less the photo's 87%). */
  .stw-product__gallery:has(.stw-gallery--multi) .stw-product__badge {
    left: calc(13% + 16px);
  }
}

/* Plan-builder products: always a square, cropped to fill; the active thumbnail is ink. */
body.stw-is-plan-builder .stw-gallery {
  --stw-gallery-ratio: 1 !important;
}

body.stw-is-plan-builder .stw-gallery__img {
  object-fit: cover;
}

body.stw-is-plan-builder .stw-gallery__thumb[aria-current] {
  border-color: var(--stw-ink);
}

/* Lightbox (native <dialog>). */
html.stw-lightbox-open {
  overflow: hidden;
}

.stw-lightbox {
  width: 100%;
  max-width: none;
  height: 100%;
  max-height: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  overflow: hidden;
}

.stw-lightbox[open] {
  display: flex;
  align-items: center;
  justify-content: center;
}

.stw-lightbox::backdrop {
  background: rgba(10, 0, 0, 0.9);
}

.stw-lightbox__img {
  display: block;
  max-width: calc(100% - 24px);
  max-height: calc(100% - 96px);
  object-fit: contain;
  touch-action: pan-y;
  user-select: none;
}

.stw-lightbox__count {
  position: absolute;
  bottom: 24px;
  left: 0;
  right: 0;
  margin: 0;
  color: #fff;
  font-size: 0.875rem;
  text-align: center;
}

.stw-lightbox__btn {
  position: absolute;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: var(--stw-radius-pill);
  background: rgba(255, 255, 255, 0.9);
  color: var(--stw-ink);
  cursor: pointer;
}

.stw-lightbox__btn:focus-visible {
  outline: 3px solid #fff;
  outline-offset: 2px;
}

.stw-lightbox__close {
  top: 16px;
  right: 16px;
}

.stw-lightbox__close::before,
.stw-lightbox__close::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 18px;
  height: 2px;
  margin: -1px 0 0 -9px;
  background: currentColor;
  transform: rotate(45deg);
}

.stw-lightbox__close::after {
  transform: rotate(-45deg);
}

.stw-lightbox__nav {
  top: 50%;
  margin-top: -22px;
}

.stw-lightbox__prev {
  left: 12px;
}

.stw-lightbox__next {
  right: 12px;
}

.stw-lightbox__nav::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 10px;
  height: 10px;
  border-top: 2px solid currentColor;
  border-right: 2px solid currentColor;
}

.stw-lightbox__next::before {
  margin: -5px 0 0 -7px;
  transform: rotate(45deg);
}

.stw-lightbox__prev::before {
  margin: -5px 0 0 -3px;
  transform: rotate(-135deg);
}

@media (prefers-reduced-motion: reduce) {
  .stw-gallery__thumb,
  .stw-gallery__nav {
    transition: none;
  }
}
