:root {
  /* Sphere Configuration */
  --radius: max(1300px, 100vw);
  --circ: calc(var(--radius) * 3.14);
  --segments-x: 37;
  --segments-y: 37;

  /* Rotation */
  --sphere-rotation-y: 0;
  --sphere-rotation-x: 0;
  --rot-y: calc((360deg / var(--segments-x)) / 2);
  --rot-x: calc((360deg / var(--segments-y)) / 2);
  --rot-y-delta: 0deg;
  --rot-x-delta: 0deg;

  /* Item Positioning */
  --offset-x: 0;
  --offset-y: 0;
  --item-width: calc((var(--circ) / var(--segments-x)));
  --item-height: calc((var(--circ) / var(--segments-y)));
  --item-size-x: 1;
  --item-size-y: 1;

  /* Colors */
  --bg: rgb(235, 235, 235);
  --item-bg: rgb(225, 225, 225);
  --bg-scrim: rgba(0, 0, 0, 0.4);

  /* Gradients */
  --gradient-center: rgba(235, 235, 235, 0);
  --gradient-edge: rgba(235, 235, 235, 0.5);
  --gradient: radial-gradient(var(--gradient-center) 65%, var(--gradient-edge) 100%);
  --gradient-blur: radial-gradient(var(--gradient-center) 70%, var(--gradient-edge) 90%);
}


* {
  box-sizing: border-box;
}

/* Sphere Gallery Wrapper Styles */
.sphere-gallery-wrapper {
  width: 100%;
  overflow: hidden;
  background-color: var(--bg);
}

.sphere-gallery-wrapper main {
  display: flex;
  width: 100%;
  height: 100%;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  touch-action: none;
  position: relative;
  /* 3D空間内のどの位置でもクリック可能にする */
  pointer-events: auto;

  * {
    touch-action: none;
  }
}

.sphere-gallery-wrapper .stage {
  perspective: calc(var(--radius) * 2);
  width: 100%;
  height: 100%;
  z-index: 1;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 3D空間内の要素がクリック可能になるように */
  pointer-events: none;
}

/* .stage内の.item要素はクリック可能 */
.sphere-gallery-wrapper .stage .item {
  pointer-events: auto;
}

.sphere-gallery-wrapper .sphere {
  transform: translateZ(calc(var(--radius) * -1.5)) rotateY(var(--sphere-rotation-y)) rotateX(var(--sphere-rotation-x));
  transform-style: preserve-3d;
  position: relative;
  /* 3D空間内の要素がクリック可能になるように */
  pointer-events: none;
}

/* .sphere内の.item要素はクリック可能 */
.sphere-gallery-wrapper .sphere .item {
  pointer-events: auto;
}

.sphere-gallery-wrapper .overlay {
  background-image: var(--gradient);
  position: fixed;
  inset: 0;
  margin: auto;
  z-index: 3;
  content: "";
  pointer-events: none;
  opacity: 1;
}

.sphere-gallery-wrapper .overlay--blur {
  mask-image: var(--gradient-blur);
  backdrop-filter: blur(3px);
  position: fixed;
  inset: 0;
  margin: auto;
  z-index: 3;
  opacity: 1;
  content: "";
  pointer-events: none;
}

.sphere-gallery-wrapper .item {
  position: absolute;
  width: calc(var(--item-width) * var(--item-size-x));
  height: calc(var(--item-height) * var(--item-size-y));
  transform-origin: 50% 50%;
  transform-style: preserve-3d;
  backface-visibility: hidden;
  transition: transform 300ms;
  color: transparent;
  inset: -999px;
  margin: auto;
  /* 3D空間内でのクリック判定を確実にするため、z-indexを高く設定 */
  z-index: 10;
  /* クリック可能にする - 3D空間内のどの位置でもクリック可能 */
  pointer-events: auto !important;
  cursor: pointer;
  /* クリック範囲を最大化 */
  will-change: transform;
  /* 3D空間内でのクリック判定を確実にする */
  isolation: isolate;
  transform: rotateY(
      calc(
        var(--rot-y) * (var(--offset-x) + ((var(--item-size-x) - 1) / 2)) +
          var(--rot-y-delta, 0deg)
      )
    )
    rotateX(
      calc(
        calc(var(--rot-x) * (var(--offset-y) - ((var(--item-size-y) - 1) / 2))) +
          var(--rot-x-delta, 0deg)
      )
    )
    translateZ(var(--radius));
  /* フォーカス状態のスタイル */
  outline: none;
}

.sphere-gallery-wrapper .item:focus {
  outline: 2px solid rgba(255, 255, 255, 0.8);
  outline-offset: 4px;
  z-index: 3;
}

.sphere-gallery-wrapper .item:focus-visible {
  outline: 2px solid rgba(0, 123, 255, 0.8);
  outline-offset: 4px;
}


/* ============================================================================
   Base Styles
   ============================================================================ */
.sphere-gallery-wrapper * {
  transform-style: preserve-3d;
}

.sphere-gallery-wrapper .item__image {
  position: absolute;
  display: block;
  inset: 10px;
  border-radius: 12px;
  background-color: var(--item-bg);
  overflow: hidden;
  backface-visibility: hidden;
  transition: transform 300ms;
  cursor: pointer;
  pointer-events: auto;
  z-index: 1;
}

/* フォーカスされた画像を確実に前面に表示 */
.sphere-gallery-wrapper .item__image[data-focused="true"] {
  z-index: 100;
}

.sphere-gallery-wrapper .item__image:hover {
  /* ホバー時の視覚的フィードバック */
  filter: brightness(1.05);
  transform: scale(1.02);
}

.sphere-gallery-wrapper .item__image:active {
  transform: scale(0.98);
}

.sphere-gallery-wrapper .item__image img {
  object-fit: cover;
  width: 100%;
  height: 100%;
  pointer-events: none;
  backface-visibility: hidden;
  user-select: none;
  /* ドラッグを無効化 */
  -webkit-user-drag: none;
  -khtml-user-drag: none;
  -moz-user-drag: none;
  -o-user-drag: none;
  user-drag: none;
}

/* ============================================================================
   Viewer (Image Enlargement)
   ============================================================================ */
.sphere-gallery-wrapper .viewer {
  position: absolute;
  inset: 0;
  z-index: 9;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 100px;
}

.sphere-gallery-wrapper .viewer .frame {
  height: 100%;
  aspect-ratio: 1;
  border-radius: 32px;
  display: flex;
  position: relative;
  align-items: center;
  justify-content: center;
}

.sphere-gallery-wrapper .viewer .enlarge {
  position: absolute;
  z-index: 1;
  border-radius: 32px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  background-color: var(--item-bg);
}

.sphere-gallery-wrapper .viewer .enlarge img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

/* ローディングインジケーター */
.sphere-gallery-wrapper .viewer .enlarge__loading {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 48px;
  height: 48px;
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-top-color: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  z-index: 1;
}

@keyframes spin {
  to {
    transform: translate(-50%, -50%) rotate(360deg);
  }
}

/* エラーメッセージ */
.sphere-gallery-wrapper .viewer .enlarge__error {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: rgba(255, 255, 255, 0.9);
  text-align: center;
  padding: 16px;
  background-color: rgba(0, 0, 0, 0.7);
  border-radius: 8px;
  z-index: 2;
}

/* 閉じるボタン */
.sphere-gallery-wrapper .viewer .enlarge__close {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  z-index: 11;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s ease, transform 0.2s ease;
  backdrop-filter: blur(4px);
}

.sphere-gallery-wrapper .viewer .enlarge__close:hover {
  background-color: rgba(0, 0, 0, 0.8);
  transform: scale(1.1);
}

.sphere-gallery-wrapper .viewer .enlarge__close:active {
  transform: scale(0.95);
}

.sphere-gallery-wrapper .viewer .enlarge__close:focus {
  outline: 2px solid rgba(255, 255, 255, 0.8);
  outline-offset: 2px;
}

/* 画像インデックス表示 */
.sphere-gallery-wrapper .viewer .enlarge__index {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 16px;
  background-color: rgba(0, 0, 0, 0.6);
  color: white;
  border-radius: 20px;
  font-size: 14px;
  z-index: 11;
  backdrop-filter: blur(4px);
}


.sphere-gallery-wrapper .viewer .scrim {
  position: absolute;
  inset: 0;
  background-color: var(--bg-scrim);
  pointer-events: none;
  opacity: 0;
  transition: opacity 300ms;
  backdrop-filter: blur(3px);
  cursor: pointer;
}

.sphere-gallery-wrapper[data-enlarging="true"] .viewer .scrim,
body[data-enlarging="true"] .sphere-gallery-wrapper .viewer .scrim {
  opacity: 1;
  pointer-events: all;
}


.sphere-gallery-wrapper .item__image {
  cursor: pointer;
}

/* ============================================================================
   Mobile Styles
   ============================================================================ */
@media (max-aspect-ratio: 1/1) {
  .sphere-gallery-wrapper .viewer {
    padding: 20px;
  }

  .sphere-gallery-wrapper .viewer .frame {
    height: auto;
    width: calc(100% - 40px);
    max-width: 90vw;
  }

  .sphere-gallery-wrapper .viewer .enlarge {
    border-radius: 16px;
  }
}