/* Related products: fluid responsive grid (only change for related products, not gallery) */
.single-product-layout .related.products ul.products {
  display: grid !important;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)) !important;
  gap: 1rem !important;
  list-style: none !important;
  margin: 0 !important;
  padding: 0 !important;
}

.single-product-layout .related.products ul.products li.product {
  width: 100% !important;
}

/* Gallery: do not force widths on thumbnails (Flexslider handles it) */
/* Only center the gallery container itself */
.single-product-layout .product-images {
  display: block !important;
  text-align: center !important;
}

.single-product-layout .product-images .woocommerce-product-gallery {
  display: inline-block !important;
  margin: 0 auto !important;
  max-width: 100% !important;
}

@media (min-width: 992px) {
  /* Prevent extremely tall images stretching the viewport */
  .single-product-layout .woocommerce-product-gallery__image img {
    max-height: 720px !important;
    width: auto !important;
    margin-left: auto !important;
    margin-right: auto !important;
  }
}

/* Center gallery within left column on large screens without breaking Flexslider internals */
@media (min-width: 992px) {
  /* Safer centering: don't change display of .product-images (Flexslider depends on block/float behavior).
     Center the gallery container with margin:auto and use inline-block so inner floats remain intact. */
  .single-product-layout .product-images {
    text-align: center !important;
  }

  .single-product-layout .product-images .woocommerce-product-gallery {
    display: inline-block !important;
    vertical-align: top !important;
    margin: 0 auto !important;
    max-width: 760px !important;
    width: auto !important;
  }

  /* Let Flexslider keep managing the slides inside the wrapper: do not force width:100% on wrapper */
  .single-product-layout .product-images .woocommerce-product-gallery__wrapper {
    margin-left: 0 !important;
    margin-right: 0 !important;
  }

  .single-product-layout .product-images .product-image-wrapper {
    margin-left: 0.5rem !important;
    margin-right: 0.5rem !important;
  }

  /* Thumbnails: small, square, and centered; avoid stretching */
  .single-product-layout .flex-control-thumbs {
    display: flex !important;
    gap: 0.5rem !important;
    padding: 0 !important;
    margin: 0.5rem 0 0 0 !important;
    justify-content: flex-start !important;
    align-items: center !important;
    flex-wrap: wrap !important;
  }
  .single-product-layout .flex-control-thumbs li {
    list-style: none !important;
    padding: 4px !important;
    background: #fff !important;
    border: 1px solid rgba(0,0,0,0.08) !important;
    border-radius: 4px !important;
    box-sizing: border-box !important;
  }
  .single-product-layout .flex-control-thumbs li img {
    width: 72px !important;
    height: 72px !important;
    object-fit: cover !important;
    display: block !important;
  }

  /* Lightweight fallback viewer (used only if Flexslider fails to show images) */
  .tma-fallback-viewer {
    display: none;
    width: 100%;
    max-width: 760px;
    margin: 0 auto 1rem auto;
    background: #fff;
    box-sizing: border-box;
    padding: 8px;
    border-radius: 4px;
    box-shadow: 0 0 0 rgba(0,0,0,0);
    position: relative;
    z-index: 9999; /* ensure viewer is on top */
    align-items: center;
    justify-content: center;
    pointer-events: auto;
  }
  .tma-fallback-viewer.active {
    display: flex !important;
  }
  .tma-fallback-viewer img {
    max-width: 100% !important;
    max-height: 720px !important;
    width: auto !important;
    height: auto !important;
    object-fit: contain !important;
    display: block !important;
    margin: 0 auto !important;
  }
}
