/**
 * Product Detail Page - Image Gallery Styles
 * Main image, zoom functionality, thumbnail navigation
 */

/* Main Product Gallery */
.product-gallery {
  position: relative;
}

.main-image-container {
  position: relative;
  width: 100%;
  height: auto;
  aspect-ratio: 4 / 3;
  background-color: #ffffff;
  overflow: hidden;
  cursor: zoom-in;
}

/* Dark mode: white base doesn't work with multiply — use a neutral dark bg instead */
[data-bs-theme="dark"] .main-image-container {
  background-color: #1f2937;
}


/* ─── Issue #3: Tiny Slider injects .tns-outer / .tns-inner / .tns-slider / .tns-item
       wrappers that have no explicit height by default, breaking the height: 100% chain
       on .zoom. Force all injected wrappers to fill the container. ─────────────────── */
.main-image-container .tns-outer,
.main-image-container .tns-inner,
.main-image-container .tns-slider,
.main-image-container .tns-item {
  height: 100%;
}

/* Hide all slides before TinySlider initializes (adds .tns-slider to #productModal).
   Without this, all slides stack vertically and flash visible on first load.
   Covers both .zoom (image) and .gallery-video-slide (video) slide types. */
#productModal:not(.tns-slider) .zoom,
#productModal:not(.tns-slider) .gallery-video-slide { display: none; }
#productModal:not(.tns-slider) > :first-child { display: block; }

/* Suppress background-image on the pre-init visible slide when it is a .zoom.
   Without this, the 200% zoomed background shows while <img> is still loading. */
#productModal:not(.tns-slider) .zoom:first-child { background-image: none !important; }

/* Main Image Zoom Functionality */
.zoom {
  background-position: 50% 50%;
  /* ─── Issue #2: background-size was missing — zoom hover effect showed image at
         natural pixel size instead of zoomed in. 200% makes the background 2× larger
         so the mouse-tracked background-position creates a real zoom effect. ───────── */
  background-size: 200%;
  background-repeat: no-repeat;
  /* White background-color is required for background-blend-mode: multiply to work.
     Multiply: bg-image light/white pixels blend into white → appear seamless.
     Dark product pixels are preserved perfectly (dark × white = dark). */
  background-color: #ffffff;
  background-blend-mode: multiply;
  /* Filter lives here (not on .zoom img) so it is applied to the entire rendered
     output of the element — background-image and <img> child alike. This means the
     zoom background view and the normal static view are processed identically:
       brightness(1.06) — lifts shadow pixels toward the background value.
       contrast(0.95)   — compresses tonal range, reducing the shadow's dark edge.
     Note: filter creates a new stacking context, so mix-blend-mode: multiply on
     .zoom img blends against .zoom's own white background — which is correct. */
  filter: brightness(1.06) contrast(0.95);
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  cursor: zoom-in;
}

/* Dark mode: multiply and the light-mode filter both require a white backdrop.
   Reset both and rely on the container vignette (::after) instead. */
[data-bs-theme="dark"] .zoom {
  background-color: #1f2937;
  background-blend-mode: normal;
  filter: none;
}

.zoom img {
  transition: opacity 0.3s ease;
  display: block;
  width: 100%;
  height: 100%;
  /* ─── Issue #4: object-fit: cover was cropping product images.
         contain shows the full image without cropping. ── */
  object-fit: contain;
  /* mix-blend-mode: multiply makes light/beige baked-in image backgrounds
     visually dissolve against the white container. Dark product colors are
     unaffected (dark × white = dark). Works only on light/white parent bg.
     Because .zoom has a filter (stacking context), this blends against .zoom's
     own white background — the correct target. */
  mix-blend-mode: multiply;
  /* Removed border-radius: the container already clips via overflow:hidden,
     and the image file has its own baked-in rounded corners — double-rounding
     caused an uneven stepped-corner effect. */
}

/* Dark mode: disable multiply — it would make everything near-black on dark bg. */
[data-bs-theme="dark"] .zoom img {
  mix-blend-mode: normal;
}

/* Hide zoom background until hover — prevents 200% background leaking through
   letterbox areas around object-fit:contain image on load */
.zoom:not(:hover) {
  background-image: none !important;
}

.zoom img:hover {
  opacity: 0;
}

/* Image Gallery Navigation */
.image-gallery-wrapper {
  position: relative;
  margin-top: 10px;
  overflow: hidden;
  min-height: 106px;
}

.image-gallery-nav {
  display: flex;
  gap: 0;
  transition: transform 0.3s ease;
  width: max-content;
  opacity: 0;
  animation: fadeInGallery 0.3s ease 0.2s forwards;
}

.gallery-nav-item {
  flex: 0 0 auto;
  overflow: hidden;
  cursor: pointer;
  /* border-radius lives here so overflow:hidden clips the image corners —
     keeps CSS rounding separate from any baked-in corners in the image file. */
  border-radius: 4px;
  /* Border moved from img to container so it surrounds the clipped image cleanly. */
  border: 2px solid transparent;
  transition: border-color 0.2s;
  padding: 1px;
  box-sizing: border-box;
  /* White base required for mix-blend-mode: multiply on the child img to work.
     Filter on the container covers the img in one pass — same pattern as .zoom. */
  background-color: #ffffff;
  filter: brightness(1.06) contrast(0.95);
}

/* Dark mode: white base and light-mode filter don't work on dark backgrounds. */
[data-bs-theme="dark"] .gallery-nav-item {
  background-color: #1f2937;
  filter: none;
}

.gallery-nav-item img {
  width: 100%;
  height: 100px;
  object-fit: cover;
  display: block;
  /* No border-radius here — container clips via overflow:hidden + border-radius.
     Removing it prevents the double-rounding stepped-corner artifact. */
  mix-blend-mode: multiply;
}

/* Dark mode: disable multiply — it would crush colours on a dark background. */
[data-bs-theme="dark"] .gallery-nav-item img {
  mix-blend-mode: normal;
}

.gallery-nav-item.active-item {
  border-color: var(--primary-green);
}

.gallery-controls {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  transform: translateY(-50%);
  pointer-events: none;
  padding: 0 var(--space-md);
}

.gallery-controls button {
  background: var(--primary-green);
  border: none;
  color: var(--background-white);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  pointer-events: all;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: var(--shadow-md);
}

.gallery-controls button:hover {
  background: var(--primary-green-dark);
  transform: scale(1.1);
  box-shadow: var(--shadow-lg);
}

.gallery-controls button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.gallery-controls button:disabled:hover {
  transform: none;
  background: var(--primary-green);
}

/* ─── Expand / Open-Lightbox Button ──────────────────────────────────────────
   Lives inside #mainImageContainer (position: relative), so absolute positioning
   works naturally — no JS inline-style hacks needed.                           */
.gallery-fullscreen-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 50;
  background: rgba(0, 0, 0, 0.45);
  border: none;
  color: #fff;
  width: 34px;
  height: 34px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 13px;
  transition: background 0.2s, transform 0.15s;
}

.gallery-fullscreen-btn:hover {
  background: rgba(0, 0, 0, 0.75);
  transform: scale(1.08);
}

/* ─── Lightbox Overlay ────────────────────────────────────────────────────────
   A dedicated fixed overlay independent of the gallery DOM.
   Opens via JS; zero inline-style hacks; works on all browsers + iOS Safari.  */
.gallery-lightbox {
  position: fixed;
  inset: 0;
  z-index: 9990;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
}

.gallery-lightbox.lb-open {
  opacity: 1;
  pointer-events: auto;
}

body.lb-body-lock {
  overflow: hidden !important;
  /* iOS Safari: overflow:hidden alone doesn't block scrolling.
     position:fixed + top:var(--lb-scroll-y) pins the body at the current
     scroll offset while the lightbox is open. JS saves/restores scrollY. */
  position: fixed !important;
  width: 100% !important;
  top: var(--lb-scroll-y, 0) !important;
}

/* Dark backdrop — click it to close */
.lb-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.93);
  cursor: pointer;
}

/* ─── Close Button ──────────────────────────────────────────────────────────── */
.lb-close {
  position: absolute;
  top: 14px;
  right: 14px;
  z-index: 20;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.25);
  color: #fff;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
  transition: background 0.2s, transform 0.2s;
}

.lb-close:hover {
  background: rgba(255, 255, 255, 0.28);
  transform: scale(1.1);
}

/* ─── Counter ────────────────────────────────────────────────────────────────── */
.lb-counter {
  position: absolute;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 20;
  color: rgba(255, 255, 255, 0.88);
  font-size: 13px;
  font-weight: 500;
  background: rgba(0, 0, 0, 0.45);
  padding: 4px 14px;
  border-radius: 20px;
  white-space: nowrap;
  pointer-events: none;
}

/* ─── Navigation Arrows ──────────────────────────────────────────────────────── */
.lb-nav {
  position: absolute;
  /* Shift up by half the thumb strip height so arrows centre on the image, not the overlay */
  top: calc(50% - 40px);
  transform: translateY(-50%);
  z-index: 20;
  background: rgba(255, 255, 255, 0.12);
  border: 1px solid rgba(255, 255, 255, 0.25);
  color: #fff;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
  transition: background 0.2s, transform 0.2s, opacity 0.2s;
}

.lb-prev { left: 16px; }
.lb-next { right: 16px; }

.lb-nav:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.28);
  transform: translateY(-50%) scale(1.1); /* matches the adjusted top, not raw 50% */
}

.lb-nav:disabled { cursor: not-allowed; }

/* ─── Slide Content ──────────────────────────────────────────────────────────── */
.lb-content {
  position: relative;
  z-index: 10;
  width: calc(100vw - 140px);
  height: calc(100vh - 160px);    /* fallback for browsers without dvh */
  height: calc(100dvh - 160px);   /* preferred: accounts for iOS browser chrome */
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none; /* transparent areas fall through to backdrop */
}

.lb-slide {
  display: none;
  width: 100%;
  height: 100%;
  align-items: center;
  justify-content: center;
}

.lb-slide.lb-active {
  display: flex;
  pointer-events: auto;
}

.lb-slide img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
  border-radius: 4px;
}

.lb-slide video {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 4px;
}

/* ─── Thumbnail Strip ────────────────────────────────────────────────────────── */
.lb-thumbs {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 14px 20px 18px;
  /* env() safe-area: pushes thumbs above iOS home indicator when viewport-fit=cover.
     Falls back to 18px on devices without a home bar. */
  padding-bottom: max(18px, env(safe-area-inset-bottom, 0px));
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.78));
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.lb-thumbs::-webkit-scrollbar { display: none; }

.lb-thumb {
  flex: 0 0 auto;
  width: 58px;
  height: 58px;
  padding: 0;
  border-radius: 5px;
  overflow: hidden;
  border: 2px solid transparent;
  cursor: pointer;
  opacity: 0.5;
  transition: opacity 0.2s, border-color 0.2s, transform 0.2s;
  position: relative;
  background: none;
}

.lb-thumb.lb-thumb-active {
  border-color: var(--primary-green, #0aad0a);
  opacity: 1;
  transform: scale(1.08);
}

.lb-thumb:hover:not(.lb-thumb-active) { opacity: 0.82; }

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

.lb-thumb-icon {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.9);
  font-size: 13px;
  pointer-events: none;
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
}

/* On mobile, zoom is disabled — show pointer so users know the image is tappable */
@media (max-width: 768px) {
  .main-image-container { cursor: pointer; }
}

/* ─── Lightbox Mobile ────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  /* Expand button: 44×44px meets Apple/Google minimum tap-target recommendation */
  .gallery-fullscreen-btn {
    width: 44px;
    height: 44px;
    font-size: 15px;
    border-radius: 8px;
  }

  .lb-content {
    /* Full width — arrows overlay the image (standard mobile lightbox pattern) */
    width: 100vw;
    height: calc(100vh - 148px);   /* dvh fallback */
    height: calc(100dvh - 148px);
  }

  .lb-nav {
    width: 40px;
    height: 40px;
    font-size: 15px;
    top: calc(50% - 34px);
    /* Darker background so arrows stay readable on top of light product images */
    background: rgba(0, 0, 0, 0.35);
    border-color: rgba(255, 255, 255, 0.2);
  }

  /* Arrows sit on top of the image edges */
  .lb-prev { left: 6px; }
  .lb-next { right: 6px; }

  .lb-nav:hover:not(:disabled) {
    background: rgba(0, 0, 0, 0.6);
    transform: translateY(-50%) scale(1.08);
  }

  .lb-close {
    /* Respect iOS status-bar notch */
    top: max(10px, env(safe-area-inset-top, 10px));
    right: 10px;
    width: 40px;
    height: 40px;
    font-size: 15px;
  }

  .lb-counter {
    top: max(12px, env(safe-area-inset-top, 12px));
    font-size: 12px;
    padding: 3px 11px;
  }

  .lb-thumb {
    width: 48px;
    height: 48px;
  }

  .lb-thumbs {
    padding: 10px 14px;
    padding-bottom: max(14px, env(safe-area-inset-bottom, 0px));
    gap: 5px;
  }
}

