/* ローディング画面1 */
.p-loading {
  display: block;
  position: fixed;
  inset: 0;
  z-index: 10000;
  overflow: hidden;
  isolation: isolate;
  contain: paint;
  background: var(--color-red);

  @media (min-width: 768px) {
    --loading-unit: min(calc(100vw / 1549), calc(100dvh / 877));
    border: calc(4 * var(--loading-unit)) solid var(--color-black);
  }

  &[hidden],
  .is-skip-loading &,
  [data-loading="false"] & {
    display: none !important;
  }

  &.is-finished {
    opacity: 0;
    transition: opacity 400ms ease-out;
    pointer-events: none;
  }

  img {
    position: absolute;
    display: block;
    max-width: none;
    height: auto;
  }
}

.p-loading__catch1 {
  z-index: 3;
  top: calc(6% + 289.5 * var(--loading-unit));
  left: calc(48.1% + 116.5 * var(--loading-unit));
  transform: translate3d(-50%, -50%, 0);
  will-change: top, left, width, transform, opacity;
  backface-visibility: hidden;
  width: calc(233 * var(--loading-unit));
}

.p-loading__catch2 {
  z-index: 3;
  top: calc(22.5% + 314.5 * var(--loading-unit));
  left: calc(49.8% - 93.5 * var(--loading-unit));
  transform: translate3d(-50%, -50%, 0);
  will-change: top, left, width, transform, opacity;
  backface-visibility: hidden;
  width: calc(187 * var(--loading-unit));
}

.p-loading_obj {
  z-index: 1;
  pointer-events: none;

  &.--left {
    top: 0;
    left: 0;
    width: calc(521 * var(--loading-unit));
  }

  &.--right {
    right: 0;
    bottom: 0;
    width: calc(521 * var(--loading-unit));
  }
}

.p-loading_text {
  z-index: 2;

  &.--right {
    top: 5.4%;
    right: 11.9%;
    width: calc(179 * var(--loading-unit));
  }

  &.--left {
    left: 16.5%;
    bottom: 7%;
    width: calc(115 * var(--loading-unit));
  }
}

.p-loading_logo {
  z-index: 2;
  left: 15%;
  bottom: 43%;
  width: calc(163 * var(--loading-unit));
}

.p-loading_gummy {
  z-index: 2;
  pointer-events: none;

  &.--cola {
    top: 8%;
    left: 10%;
    width: calc(115 * var(--loading-unit));
  }

  &.--melon {
    top: 15%;
    left: 0.8%;
    width: calc(144.5 * var(--loading-unit));
  }

  &.--soda {
    right: 1.6%;
    bottom: 34.2%;
    width: calc(131.5 * var(--loading-unit));
  }

  &.--boost {
    right: 4.4%;
    bottom: 0.7%;
    width: calc(191 * var(--loading-unit));
  }
}

html:has(.p-loading:not([hidden])),
html.is-scroll-locked,
body.is-scroll-locked {
  overflow: hidden;
}

@media (max-width: 767px) {
  .p-loading {
    --loading-unit: min(calc(100vw / 390), calc(100dvh / 794));
    border: calc(2 * var(--loading-unit)) solid var(--color-black);
  }

  .p-loading__catch1 {
    top: calc(284 * var(--loading-unit));
    left: calc(50% + 32 * var(--loading-unit));
    transform: translate(-50%, -50%);
    width: calc(92 * var(--loading-unit));
  }

  .p-loading__catch2 {
    top: calc(462 * var(--loading-unit));
    left: calc(50% - 40 * var(--loading-unit));
    transform: translate(-50%, -50%);
    width: calc(76 * var(--loading-unit));
  }

  .p-loading_obj {
    &.--left, &.--right {
      width: 100%;
    }
  }

  .p-loading_text {
    &.--right {
      top: 3.4%;
      right: 5.1%;
      width: calc(94 * var(--loading-unit));
    }

    &.--left {
      left: 7.7%;
      bottom: 5%;
      width: calc(63 * var(--loading-unit));
    }
  }

  .p-loading_logo {
    left: 3%;
    bottom: 27%;
    width: calc(94 * var(--loading-unit));
  }

  .p-loading_gummy {
    &.--cola {
      top: 14%;
      left: 11%;
      width: calc(84 * var(--loading-unit));
    }

    &.--melon {
      top: 27%;
      left: -4.4%;
      width: calc(57.5 * var(--loading-unit));
    }

    &.--soda {
      right: 4%;
      bottom: 25%;
      width: calc(89 * var(--loading-unit));
    }

    &.--boost {
      right: 12%;
      bottom: 10%;
      width: calc(98.5 * var(--loading-unit));
    }
  }
}

/* 赤背景と左右の装飾から、演出を開始する。 */
.p-loading::before {
  content: '';
  position: absolute;
  inset: -45%;
  z-index: 0;
  opacity: 0;
  transform-origin: center;
  background: url('/assets/images/loading/loading-bg.svg') center / cover no-repeat;
  pointer-events: none;

  @media (max-width: 767px) {
    background-image: url('/assets/images/loading/sp/loading-sp.svg');
  }
}

.p-loading_text,
.p-loading__catch1,
.p-loading__catch2,
.p-loading_logo {
  opacity: 0;
  transform-origin: center;
  will-change: transform, opacity;
  backface-visibility: hidden;
}

.p-loading_text {
  filter: blur(12px);
}

.p-loading_obj {
  opacity: 1;
}

/* 1. gummyは4つとも初期状態で表示、すこしふわふわ動く */
.p-loading_gummy {
  opacity: 1;
  transform-origin: center;
  animation: loading-gummy-float 3.2s ease-in-out infinite;

  &.--cola {
    animation-duration: 3s;
    animation-delay: 0s;
  }

  &.--melon {
    animation-duration: 3.4s;
    animation-delay: -0.8s;
  }

  &.--soda {
    animation-duration: 2.8s;
    animation-delay: -1.6s;
  }

  &.--boost {
    animation-duration: 3.6s;
    animation-delay: -2.4s;
  }
}

.p-loading.is-playing {
  /* 2. textを1本ずつブラー解除しながらズームフェードイン。読み終える間を空けて次を出す */
  .p-loading_text {
    animation: loading-text-reveal 500ms cubic-bezier(0.16, 1, 0.3, 1) both;

    &.--right { animation-delay: 40ms; }
    &.--left { animation-delay: 1750ms; }
  }

  /* 3. bgの点滅だけで溜めを作ってから、catchを一気に飛び出させる */
  .p-loading__catch1,
  .p-loading__catch2 {
    animation: loading-catch-zoom 900ms ease-out 3500ms both;
  }

  /* 4. loading-bgは点滅ごとに角度を変えて高速フラッシュ。止めずにKV表示まで繰り返す */
  &::before {
    animation: loading-bg-flash-angle 1500ms steps(1, end) 2900ms infinite both;
  }

  /* 5. catchが収まった直後にロゴをポップ表示し、完成形を少し見せてから退場 */
  .p-loading_logo {
    animation: loading-pop-in 220ms cubic-bezier(0.18, 0.89, 0.32, 1.25) 4400ms both;
  }
}

/* 5.2秒後：catchとbg以外の要素が出現と逆の順でフェードアウト（logo → text → gummy → obj）。bgは is-finished で赤背景ごと消える */
.p-loading.is-leave {
  .p-loading_logo {
    animation: none;
    opacity: 0;
    transition: opacity 200ms ease-out 0ms;
  }

  .p-loading_text {
    animation: none;
    opacity: 0;
    filter: blur(8px);
    transition: opacity 220ms ease-out 80ms, filter 220ms ease-out 80ms;
  }

  .p-loading_gummy {
    animation: none;
    opacity: 0;
    transition: opacity 220ms ease-out 180ms;
  }

  .p-loading_obj {
    opacity: 0;
    transition: opacity 250ms ease-out 280ms;
  }
}

/* catchをKVと同じ位置に移動するアニメーション */
.p-loading.is-catch-moved {
  .p-loading__catch1 {
    top: calc(min(100vw, 1540px) * (882 / 1556) * 0.36);
    left: calc(50vw + (min(100vw, 1540px) * 0.195));
    transform: translate3d(-50%, -50%, 0);
    width: calc(233 * min(calc(100vw / 1540), 1px));
    animation: none;
    opacity: 1;
    will-change: top, left, width, transform;
    transition: top 220ms cubic-bezier(0.16, 1, 0.3, 1),
                left 220ms cubic-bezier(0.16, 1, 0.3, 1),
                width 220ms cubic-bezier(0.16, 1, 0.3, 1);

    @media (max-width: 767px) {
      top: calc(243 * (100vw / 390));
      left: calc(50% + 9 * (100vw / 390));
      width: calc(92 * (100vw / 390));
    }
  }

  .p-loading__catch2 {
    top: calc(min(100vw, 1540px) * (882 / 1556) * 0.64);
    left: calc(50vw - (min(100vw, 1540px) * 0.175));
    transform: translate3d(-50%, -50%, 0);
    width: calc(187 * min(calc(100vw / 1540), 1px));
    animation: none;
    opacity: 1;
    will-change: top, left, width, transform;
    transition: top 220ms cubic-bezier(0.16, 1, 0.3, 1),
                left 220ms cubic-bezier(0.16, 1, 0.3, 1),
                width 220ms cubic-bezier(0.16, 1, 0.3, 1);

    @media (max-width: 767px) {
      top: calc(496 * (100vw / 390));
      left: calc(50% - 9 * (100vw / 390));
      width: calc(76 * (100vw / 390));
    }
  }
}

@keyframes loading-gummy-float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-8px) rotate(2deg);
  }
}

@keyframes loading-text-reveal {
  0% {
    opacity: 0;
    filter: blur(12px);
    transform: scale(0.88);
  }
  100% {
    opacity: 1;
    filter: blur(0);
    transform: scale(1);
  }
}

@keyframes loading-catch-zoom {
  0% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(0.3);
  }
  12% {
    opacity: 1;
  }
  45% {
    transform: translate(-50%, -50%) scale(1.12);
  }
  70% {
    transform: translate(-50%, -50%) scale(0.97);
  }
  100% {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
  }
}

/* 0度/180度に回転しながら高速点滅（steps(1, end) で段階的に切り替え） */
@keyframes loading-bg-flash-angle {
  0%, 3%, 8%, 18%, 28%, 38%, 48%, 58%, 68%, 78%, 88% {
    opacity: 0;
    transform: rotate(0deg);
  }
  5%, 15%, 25%, 35%, 45%, 55%, 65%, 75%, 85%, 95%, 100% {
    opacity: 1;
    transform: rotate(0deg);
  }
  10%, 20%, 30%, 40%, 50%, 60%, 70%, 80%, 90% {
    opacity: 1;
    transform: rotate(180deg);
  }
  13%, 23%, 33%, 43%, 53%, 63%, 73%, 83%, 93% {
    opacity: 0;
    transform: rotate(180deg);
  }
}

@keyframes loading-pop-in {
  0% {
    opacity: 0;
    transform: scale(0.3);
  }
  60% {
    opacity: 1;
    transform: scale(1.15);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}
