/* 상품 이미지 갤러리 - 프런트 스타일
 * 레이아웃 폭(컨테이너 쿼리)에 따라 좌/우 세로 썸네일과 하단 가로 썸네일이 전환됩니다.
 */

.apg-gallery-wrap {
  --apg-main-width: 600px;
  --apg-main-height: 600px;
  --apg-main-ratio: 1 / 1;
  --apg-thumb-width: 72px;
  --apg-thumb-height: 72px;
  --apg-thumb-outer-width: 74px;
  --apg-thumb-outer-height: 74px;
  --apg-border-width: 1px;
  --apg-active-color: #e2231a;
  --apg-zoom-scale: 2;
  --apg-zoom-box: 0.5;
  --apg-gap: 18px;
  --apg-thumb-gap: 10px;
  --apg-scroll-space: 12px;
  container-type: inline-size;
  box-sizing: border-box;
  width: 100%;
  max-width: calc(var(--apg-main-width) + var(--apg-thumb-outer-width) + var(--apg-gap) + var(--apg-scroll-space));
  min-width: 0;
}

.apg-gallery-wrap.apg-wrap-bottom {
  max-width: var(--apg-main-width);
}

.apg-gallery-wrap *,
.apg-gallery-wrap *::before,
.apg-gallery-wrap *::after {
  box-sizing: border-box;
}

.apg-product-gallery {
  position: relative;
  display: grid;
  grid-template-columns: calc(var(--apg-thumb-outer-width) + var(--apg-scroll-space)) minmax(0, 1fr);
  grid-template-areas: "thumbs main";
  align-items: start;
  gap: var(--apg-gap);
  width: 100%;
  min-width: 0;
}

.apg-product-gallery.apg-thumbs-right {
  grid-template-columns: minmax(0, 1fr) calc(var(--apg-thumb-outer-width) + var(--apg-scroll-space));
  grid-template-areas: "main thumbs";
}

.apg-product-gallery.apg-thumbs-bottom {
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas:
    "main"
    "thumbs";
  gap: var(--apg-thumb-gap);
}

/* 세로 썸네일은 메인 이미지 높이에 맞춰 스크롤됩니다. */
.apg-thumbs-col {
  grid-area: thumbs;
  position: relative;
  align-self: stretch;
  min-width: 0;
}

.apg-thumbnails {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: var(--apg-thumb-gap);
  margin: 0;
  padding: 0 var(--apg-scroll-space) 0 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

.apg-thumbs-right .apg-thumbnails {
  padding-right: 0;
  padding-left: var(--apg-scroll-space);
}

/* 세로 목록은 메인 이미지 기준 가운데 정렬. 목록이 넘치면 auto 여백이 0이 되어 스크롤이 유지됩니다. */
.apg-thumbs-left .apg-thumbnails > .apg-thumbnail:first-child,
.apg-thumbs-right .apg-thumbnails > .apg-thumbnail:first-child {
  margin-top: auto !important;
}

.apg-thumbs-left .apg-thumbnails > .apg-thumbnail:last-child,
.apg-thumbs-right .apg-thumbnails > .apg-thumbnail:last-child {
  margin-bottom: auto !important;
}

/* 하단 배치는 가로 스크롤 목록입니다. */
.apg-thumbs-bottom .apg-thumbs-col {
  position: static;
  align-self: auto;
}

.apg-thumbs-bottom .apg-thumbnails {
  position: static;
  flex-direction: row;
  padding: 0 0 8px;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x proximity;
}

.apg-thumbnail {
  appearance: none;
  box-sizing: content-box !important;
  flex: 0 0 auto;
  width: var(--apg-thumb-width);
  height: var(--apg-thumb-height);
  margin: 0 !important;
  padding: 0 !important;
  overflow: hidden;
  border: var(--apg-border-width) solid #dedede !important;
  border-radius: 0 !important;
  background: #fff !important;
  box-shadow: none !important;
  scroll-snap-align: start;
  cursor: pointer;
  transition: none;
}

.apg-thumbnail:hover {
  border-color: #777 !important;
}

.apg-thumbnail.is-active {
  border-color: var(--apg-active-color) !important;
  box-shadow: none !important;
}

.apg-thumbnail:focus-visible {
  outline: 2px solid #1d4ed8 !important;
  outline-offset: 2px;
}

.apg-thumbnail img {
  display: block;
  width: var(--apg-thumb-width) !important;
  height: var(--apg-thumb-height) !important;
  max-width: none !important;
  margin: 0 !important;
  object-fit: cover;
}

.apg-main-images {
  grid-area: main;
  justify-self: start;
  position: relative;
  width: 100%;
  max-width: var(--apg-main-width);
  min-width: 0;
  aspect-ratio: var(--apg-main-ratio);
  overflow: hidden;
}

/* aspect-ratio 미지원 브라우저 대비 */
@supports not (aspect-ratio: 1 / 1) {
  .apg-main-images {
    height: var(--apg-main-height);
  }
}

.apg-main-image {
  width: 100%;
  height: 100%;
}

.apg-main-image[hidden] {
  display: none !important;
}

.apg-main-image img {
  display: block;
  width: 100% !important;
  height: 100% !important;
  max-width: 100% !important;
  margin: 0 auto !important;
  object-fit: contain;
  object-position: center;
}

/* 마우스 오버 확대 (JS가 apg-zoom-ready를 붙였을 때만 동작) */
.apg-zoom-ready .apg-main-image img {
  cursor: zoom-in;
  transition: none;
}

/* 확대 화면은 항상 메인 이미지 영역 안에 겹쳐 표시합니다. */
.apg-zoom-view {
  position: absolute;
  z-index: 20;
  display: none;
  overflow: hidden;
  background: #fff;
  pointer-events: none;
}

/* 돋보기 방식은 커서를 따라다니는 작은 창이라 경계를 표시합니다. */
.apg-zoom-view.is-lens {
  border: 1px solid rgba(0, 0, 0, 0.25);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
}

.apg-zoom-view.is-visible {
  display: block;
}

.apg-zoom-view img {
  position: absolute;
  top: 0;
  left: 0;
  margin: 0 !important;
  max-width: none !important;
  object-fit: contain;
}

.apg-editor-message {
  padding: 18px;
  border: 1px dashed #a8a8a8;
  background: #f7f7f7;
  color: #555;
  text-align: center;
}

/* 태블릿: 간격만 줄여 좌/우 배치를 유지합니다. */
@container (max-width: 1024px) {
  .apg-product-gallery {
    --apg-gap: 14px;
  }
}

/* 좁은 폭(칼럼 480px 이하): 메인 이미지 아래 가로 썸네일로 전환합니다. */
@container (max-width: 480px) {
  .apg-product-gallery,
  .apg-product-gallery.apg-thumbs-right,
  .apg-product-gallery.apg-thumbs-bottom {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "main"
      "thumbs";
    gap: var(--apg-thumb-gap);
  }

  .apg-thumbs-col {
    position: static;
    align-self: auto;
  }

  .apg-thumbnails,
  .apg-thumbs-right .apg-thumbnails {
    position: static;
    flex-direction: row;
    padding: 0 0 8px;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x proximity;
  }

  .apg-thumbs-left .apg-thumbnails > .apg-thumbnail:first-child,
  .apg-thumbs-right .apg-thumbnails > .apg-thumbnail:first-child {
    margin-top: 0 !important;
  }

  .apg-thumbs-left .apg-thumbnails > .apg-thumbnail:last-child,
  .apg-thumbs-right .apg-thumbnails > .apg-thumbnail:last-child {
    margin-bottom: 0 !important;
  }

  .apg-main-images {
    justify-self: stretch;
    max-width: 100%;
  }
}

/* 컨테이너 쿼리 미지원 브라우저 대비 뷰포트 기준 폴백 */
@media (max-width: 690px) {
  .apg-gallery-wrap,
  .apg-gallery-wrap.apg-wrap-bottom {
    max-width: 100%;
  }

  .apg-product-gallery,
  .apg-product-gallery.apg-thumbs-right,
  .apg-product-gallery.apg-thumbs-bottom {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas:
      "main"
      "thumbs";
    gap: var(--apg-thumb-gap);
  }

  .apg-thumbs-col {
    position: static;
    align-self: auto;
  }

  .apg-thumbnails,
  .apg-thumbs-right .apg-thumbnails {
    position: static;
    flex-direction: row;
    padding: 0 0 8px;
    overflow-x: auto;
    overflow-y: hidden;
    scroll-snap-type: x proximity;
  }

  .apg-thumbs-left .apg-thumbnails > .apg-thumbnail:first-child,
  .apg-thumbs-right .apg-thumbnails > .apg-thumbnail:first-child {
    margin-top: 0 !important;
  }

  .apg-thumbs-left .apg-thumbnails > .apg-thumbnail:last-child,
  .apg-thumbs-right .apg-thumbnails > .apg-thumbnail:last-child {
    margin-bottom: 0 !important;
  }

  .apg-main-images {
    justify-self: stretch;
    max-width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .apg-thumbnail {
    transition: none;
  }
}
