/**
 * TASK-009 撮影事例ギャラリー
 * /gallery/ の .wp-block-group.tres-gallery または
 * [data-tres-gallery] 内だけに適用する。
 * 写真順はgallery.jsが初期化時に1回だけランダム化する。
 */

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery) {
  --tres-gallery-gap: 8px;
  --tres-gallery-row-height: 280px;
  --tres-gallery-border: #d8dee5;
  --tres-gallery-text: #1f2933;
  --tres-gallery-muted: #5b6773;
  --tres-gallery-active: #0068b7;
  --tres-gallery-active-text: #fff;
  --tres-gallery-focus: #ffbf47;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery) [hidden] {
  display: none !important;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  :is(.gallery-filters, .tres-gallery__filters) {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-block: 1.5rem;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  [data-gallery-filter],
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  :is([data-gallery-more], .gallery-more-button) {
  min-block-size: 44px;
  border: 1px solid var(--tres-gallery-border);
  border-radius: 999px;
  background: #fff;
  color: var(--tres-gallery-text);
  cursor: pointer;
  font: inherit;
  line-height: 1.4;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  [data-gallery-filter] {
  padding: 0.625rem 1rem;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  [data-gallery-filter][aria-pressed="true"] {
  border-color: var(--tres-gallery-active);
  background: var(--tres-gallery-active);
  color: var(--tres-gallery-active-text);
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  [data-gallery-filter]:hover,
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  :is([data-gallery-more], .gallery-more-button):hover {
  border-color: var(--tres-gallery-active);
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  [data-gallery-filter]:focus-visible,
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  :is([data-gallery-more], .gallery-more-button):focus-visible,
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__lightbox-trigger:focus-visible,
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__lightbox-trigger
  > :is(a, button):focus-visible {
  outline: 3px solid var(--tres-gallery-focus);
  outline-offset: 3px;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__status {
  margin-block: 0 0.75rem;
  color: var(--tres-gallery-muted);
  font-size: 0.875rem;
}

/**
 * JavaScript読込前・無効時のフォールバック。
 * 画像を非表示にせず、通常のレスポンシブグリッドで全件表示する。
 */
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  align-items: start;
  gap: var(--tres-gallery-gap);
}

/**
 * JavaScript初期化後は、WordPress標準ギャラリーのFlexbox配置を無効化。
 * gallery.jsが各画像の座標と寸法を直接指定する。
 */
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)[data-gallery-enhanced="true"]
  .tres-gallery__grid {
  position: relative !important;
  display: block !important;
  width: 100% !important;
  min-height: 1px;
  margin: 0 !important;
  padding: 0 !important;
  gap: 0 !important;
  overflow: visible;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)[data-gallery-enhanced="true"]
  .tres-gallery__grid
  > :is(.wp-block-image, [data-gallery-item], .gallery-item) {
  position: absolute !important;
  inset: auto !important;
  top: var(--tres-gallery-item-y, 0) !important;
  left: var(--tres-gallery-item-x, 0) !important;
  display: block !important;
  flex: none !important;
  flex-grow: 0 !important;
  flex-shrink: 0 !important;
  align-self: auto !important;
  box-sizing: border-box !important;
  width: var(--tres-gallery-item-width, 1px) !important;
  min-width: 0 !important;
  max-width: none !important;
  height: var(--tres-gallery-item-height, 1px) !important;
  min-height: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
  overflow: visible;
  transform: none !important;
}

/**
 * カテゴリ切替・追加表示でhiddenになった項目は、上の絶対配置ルールより
 * 必ず優先して非表示にする。非表示項目の旧座標・1pxの初期寸法が
 * レイアウトへ残ることを防ぐ。
 */
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)[data-gallery-enhanced="true"]
  .tres-gallery__grid
  > :is(.wp-block-image, [data-gallery-item], .gallery-item)[hidden] {
  display: none !important;
  visibility: hidden !important;
  pointer-events: none !important;
}

/**
 * WordPressのis-cropped・columns-*・lightbox用ラッパーによる
 * width:auto、flex-grow、余白を、このギャラリー内だけで上書きする。
 */
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)[data-gallery-enhanced="true"]
  .tres-gallery__grid
  > :is(.wp-block-image, [data-gallery-item], .gallery-item)
  > :is(a, div:not(.components-drop-zone)) {
  display: block !important;
  flex: none !important;
  box-sizing: border-box !important;
  width: 100% !important;
  max-width: none !important;
  height: 100% !important;
  margin: 0 !important;
  padding: 0 !important;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)[data-gallery-enhanced="true"]
  .tres-gallery__grid
  > :is(.wp-block-image, [data-gallery-item], .gallery-item)
  img {
  display: block !important;
  box-sizing: border-box !important;
  width: 100% !important;
  min-width: 100% !important;
  max-width: none !important;
  height: 100% !important;
  min-height: 100% !important;
  margin: 0 !important;
  padding: 0 !important;
  object-fit: contain !important;
  object-position: center !important;
  vertical-align: top !important;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__lightbox-trigger {
  cursor: zoom-in;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__more-wrap {
  display: flex;
  justify-content: center;
  margin-block-start: 2rem;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  :is([data-gallery-more], .gallery-more-button) {
  min-inline-size: min(100%, 18rem);
  padding: 0.75rem 1.5rem;
}

/**
 * JavaScriptがIntersectionObserverへ対応している場合は自動追加表示へ切り替える。
 * ボタンはHTML上に残し、非対応ブラウザ・JavaScript無効時の操作手段にする。
 */
:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)[data-gallery-autoload="true"]
  :is(.tres-gallery__more-wrap, [data-gallery-more], .gallery-more-button) {
  display: none !important;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__autoload-sentinel {
  display: block;
  box-sizing: border-box;
  width: 100%;
  height: 1px;
  margin: 0;
  padding: 0;
  overflow: hidden;
  pointer-events: none;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__autoload-indicator {
  display: none;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__autoload-sentinel.is-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  height: auto;
  min-height: 3.5rem;
  margin-block-start: 1rem;
  overflow: visible;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__autoload-sentinel.is-loading
  .tres-gallery__autoload-indicator {
  display: inline-flex;
  align-items: center;
  gap: 0.625rem;
  color: var(--tres-gallery-muted);
  font-size: 0.875rem;
  line-height: 1.4;
}

:is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
  .tres-gallery__autoload-spinner {
  flex: none;
  box-sizing: border-box;
  width: 1.25rem;
  height: 1.25rem;
  border: 2px solid rgb(0 104 183 / 22%);
  border-top-color: var(--tres-gallery-active);
  border-radius: 50%;
  animation: tres-gallery-spin 700ms linear infinite;
}

@keyframes tres-gallery-spin {
  to {
    transform: rotate(1turn);
  }
}

/**
 * ライトボックス
 * body直下へ追加するため、ギャラリーのルートセレクターから分離する。
 */
body.tres-gallery-lightbox-open {
  overflow: hidden;
  padding-inline-end: calc(
    var(--tres-gallery-body-padding-end, 0px) +
      var(--tres-gallery-scrollbar-width, 0px)
  );
}

.tres-gallery-lightbox[hidden],
.tres-gallery-lightbox [hidden] {
  display: none !important;
}

.tres-gallery-lightbox {
  position: fixed;
  z-index: 100000;
  inset: 0;
  display: grid;
  box-sizing: border-box;
  place-items: center;
  width: 100%;
  height: 100%;
  padding: clamp(0.75rem, 2vw, 1.5rem);
  background: rgb(0 0 0 / 88%);
  color: #fff;
  overscroll-behavior: contain;
}

.tres-gallery-lightbox__panel {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) auto auto;
  align-items: center;
  box-sizing: border-box;
  width: min(100%, 100rem);
  height: 100%;
  min-height: 0;
}

.tres-gallery-lightbox__title {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.tres-gallery-lightbox__image-wrap {
  grid-column: 1;
  grid-row: 1;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

.tres-gallery-lightbox__image {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  opacity: 1;
  transition: opacity 160ms ease;
}

.tres-gallery-lightbox.is-loading .tres-gallery-lightbox__image {
  opacity: 0.35;
}

.tres-gallery-lightbox__close,
.tres-gallery-lightbox__previous,
.tres-gallery-lightbox__next {
  appearance: none;
  display: grid;
  place-items: center;
  box-sizing: border-box;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: 1px solid rgb(255 255 255 / 45%);
  border-radius: 999px;
  background: rgb(0 0 0 / 45%);
  color: #fff;
  cursor: pointer;
  font-size: 0;
  line-height: 0;
  text-align: center;
  text-indent: 0;
}

.tres-gallery-lightbox__close > svg,
.tres-gallery-lightbox__previous > svg,
.tres-gallery-lightbox__next > svg {
  display: block !important;
  flex: none;
  box-sizing: content-box;
  width: 24px !important;
  height: 24px !important;
  margin: 0;
  padding: 0;
  overflow: visible;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: square;
  stroke-linejoin: miter;
  pointer-events: none;
  transform: none !important;
}

.tres-gallery-lightbox__close:hover,
.tres-gallery-lightbox__previous:hover,
.tres-gallery-lightbox__next:hover {
  background: rgb(255 255 255 / 18%);
}

.tres-gallery-lightbox__close:focus-visible,
.tres-gallery-lightbox__previous:focus-visible,
.tres-gallery-lightbox__next:focus-visible {
  outline: 3px solid #ffbf47;
  outline-offset: 3px;
}

.tres-gallery-lightbox__close {
  position: absolute;
  z-index: 1;
  top: 0;
  right: 0;
  width: 48px;
  height: 48px;
}

.tres-gallery-lightbox__previous {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 0;
  width: 48px;
  height: 48px;
  transform: translateY(-50%);
}

.tres-gallery-lightbox__next {
  position: absolute;
  z-index: 1;
  top: 50%;
  right: 0;
  width: 48px;
  height: 48px;
  transform: translateY(-50%);
}

.tres-gallery-lightbox__caption {
  grid-column: 1 / -1;
  grid-row: 2;
  max-width: 60rem;
  margin: 0.75rem auto 0;
  color: #fff;
  font-size: 0.9375rem;
  line-height: 1.6;
  text-align: center;
}

.tres-gallery-lightbox__counter {
  grid-column: 1 / -1;
  grid-row: 3;
  margin: 0.25rem 0 0;
  color: rgb(255 255 255 / 78%);
  font-size: 0.8125rem;
  line-height: 1.4;
  text-align: center;
}

@media (max-width: 63.9375rem) {
  :is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery) {
    --tres-gallery-row-height: 220px;
  }

  :is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
    .tres-gallery__grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 39.9375rem) {
  :is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery) {
    --tres-gallery-row-height: 160px;
  }

  :is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
    :is(.gallery-filters, .tres-gallery__filters) {
    gap: 0.375rem;
  }

  :is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
    [data-gallery-filter] {
    flex: 1 1 calc(50% - 0.375rem);
    padding-inline: 0.75rem;
  }

  .tres-gallery-lightbox {
    padding: 0.625rem;
  }

  .tres-gallery-lightbox__close {
    top: 0.25rem;
    right: 0.25rem;
  }

  .tres-gallery-lightbox__previous,
  .tres-gallery-lightbox__next {
    width: 44px;
    height: 44px;
    background: rgb(0 0 0 / 62%);
  }

  .tres-gallery-lightbox__caption {
    margin-block-start: 0.5rem;
    padding-inline: 0.5rem;
    font-size: 0.875rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  :is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery) *,
  :is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery) *::before,
  :is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery) *::after,
  .tres-gallery-lightbox,
  .tres-gallery-lightbox *,
  .tres-gallery-lightbox *::before,
  .tres-gallery-lightbox *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }

  :is([data-tres-gallery]:not(body), .wp-block-group.tres-gallery)
    .tres-gallery__autoload-spinner {
    animation: none !important;
  }
}
