:root {
  --loading-overlay-z: 250000;
  --loading-bg: #f7fbff;
  --loading-panel-bg: rgba(255, 255, 255, 0.72);
  --loading-panel-border: rgba(170, 201, 232, 0.62);
  --loading-text: #1f3d5c;
  --loading-subtext: rgba(40, 77, 110, 0.78);
  --loading-track: rgba(167, 201, 234, 0.42);
  --loading-fill-a: #8ed1ff;
  --loading-fill-b: #4ea9ff;
  --loading-fill-c: #7ad7c4;
  --loading-glow: rgba(113, 188, 255, 0.35);
  --loading-shadow: 0 30px 80px rgba(94, 151, 203, 0.16);
}

html.binworld-loading-lock,
body.binworld-loading-lock {
  overflow: hidden;
}

#loadingOverlay {
  position: fixed;
  inset: 0;
  z-index: var(--loading-overlay-z);
  display: flex;
  align-items: center;
  justify-content: center;
  direction: rtl;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  background:
    radial-gradient(circle at 18% 18%, rgba(255, 255, 255, 0.95), rgba(255, 255, 255, 0) 40%),
    radial-gradient(circle at 84% 20%, rgba(224, 244, 255, 0.82), rgba(224, 244, 255, 0) 36%),
    linear-gradient(180deg, #fafdff 0%, #eef7ff 56%, #f8fbff 100%);
  transition:
    opacity 460ms ease,
    visibility 460ms ease,
    transform 900ms cubic-bezier(0.19, 1, 0.22, 1);
  transform: scale(1.015);
  isolation: isolate;
}

#loadingOverlay.is-mounted,
#loadingOverlay.is-active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: scale(1);
}

#loadingOverlay.is-fading-out {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

#loadingOverlay.is-finishing #loadingSceneRoot {
  transform: translateY(-10px) scale(1.16);
}

#loadingOverlay.is-finishing #loadingPlanetWrap {
  transform: scale(1.18);
}

#loadingOverlay.is-finishing #loadingPlanet {
  animation-duration: 22s;
  filter: drop-shadow(0 28px 42px rgba(83, 148, 208, 0.24));
}

#loadingOverlay.is-finishing #loadingCloudRing {
  animation-duration: 5.2s, 4.4s;
  opacity: 0.92;
}

#loadingOverlay.is-finishing #loadingBackground {
  transform: scale(1.04);
  filter: saturate(1.08) brightness(1.03);
}

#loadingBackground,
#loadingBackground::before,
#loadingBackground::after {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

#loadingBackground {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.18), rgba(227, 242, 255, 0.12)),
    url("../assets/ui/loading/loading-bg.png") center center / cover no-repeat;
  filter: saturate(1.04);
  transform: scale(1.01);
  transition: transform 1400ms ease, filter 1400ms ease;
}

#loadingBackground::before {
  content: "";
  background:
    radial-gradient(circle at 50% 35%, rgba(255, 255, 255, 0.7), rgba(255, 255, 255, 0) 52%),
    radial-gradient(circle at 50% 58%, rgba(125, 203, 255, 0.18), rgba(125, 203, 255, 0) 48%);
  animation: loadingBgBreath 8.6s ease-in-out infinite;
}

#loadingBackground::after {
  content: "";
  background:
    radial-gradient(circle, rgba(255, 255, 255, 0.9) 0 1px, transparent 2px) 0 0 / 140px 140px,
    radial-gradient(circle, rgba(255, 255, 255, 0.8) 0 1.5px, transparent 2.4px) 34px 28px / 170px 170px;
  opacity: 0.44;
  mix-blend-mode: screen;
  animation: loadingSparkleDrift 14s linear infinite;
}

#loadingSceneRoot {
  position: relative;
  z-index: 2;
  width: min(94vw, 980px);
  max-width: 980px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 32px;
  padding: 56px 24px 48px;
  transition: transform 1200ms cubic-bezier(0.19, 1, 0.22, 1);
}

#loadingPlanetWrap {
  position: relative;
  width: min(64vw, 500px);
  height: min(64vw, 500px);
  max-width: 500px;
  max-height: 500px;
  min-width: 300px;
  min-height: 300px;
  display: grid;
  place-items: center;
  overflow: visible;
  transition: transform 1200ms cubic-bezier(0.19, 1, 0.22, 1);
}

#loadingGlow {
  position: absolute;
  inset: 6%;
  border-radius: 50%;
  background:
    radial-gradient(circle, rgba(190, 234, 255, 0.96) 0%, rgba(168, 223, 255, 0.78) 38%, rgba(155, 213, 255, 0.28) 62%, rgba(155, 213, 255, 0) 84%);
  filter: blur(20px);
  opacity: 0;
  transform: scale(0.76);
  animation: loadingGlowPulse 6s ease-in-out infinite;
  transition: opacity 420ms ease, transform 640ms ease;
}

#loadingPlanet {
  position: relative;
  z-index: 4;
  width: 126%;
  max-width: 540px;
  object-fit: contain;
  opacity: 0;
  transform: translateY(16px) scale(0.86) rotate(-8deg);
  filter: drop-shadow(0 28px 44px rgba(88, 151, 207, 0.28));
  transition:
    opacity 460ms ease,
    transform 880ms cubic-bezier(0.19, 1, 0.22, 1),
    filter 600ms ease;
}

#loadingCloudRing {
  position: absolute;
  inset: -11%;
  z-index: 2;
  width: 122%;
  height: 122%;
  object-fit: contain;
  opacity: 0;
  transform: translate3d(0, 18px, 0) scale(0.94);
  mix-blend-mode: screen;
  filter: blur(0.4px) drop-shadow(0 18px 30px rgba(151, 205, 240, 0.18));
  transition:
    opacity 460ms ease,
    transform 980ms cubic-bezier(0.19, 1, 0.22, 1);
}

#loadingPlanetWrap::before {
  content: "";
  position: absolute;
  inset: 4%;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.88) 0%, rgba(222, 244, 255, 0.22) 44%, rgba(222, 244, 255, 0) 76%);
  filter: blur(14px);
  opacity: 0.8;
}

#loadingPlanetWrap::after {
  content: "";
  position: absolute;
  left: 16%;
  right: 16%;
  bottom: 10%;
  height: 16%;
  border-radius: 50%;
  background: radial-gradient(ellipse at center, rgba(107, 167, 223, 0.25), rgba(107, 167, 223, 0) 68%);
  filter: blur(16px);
}

#loadingOverlay.is-planet-visible #loadingGlow {
  opacity: 1;
  transform: scale(1);
}

#loadingOverlay.is-planet-visible #loadingPlanet {
  opacity: 1;
  transform: translateY(0) scale(1) rotate(0deg);
  animation: loadingPlanetSpin 42s linear infinite;
}

#loadingOverlay.is-cloud-visible #loadingCloudRing {
  opacity: 0.9;
  transform: translate3d(0, 0, 0) scale(1);
  animation: loadingCloudDrift 8.8s ease-in-out infinite, loadingCloudFloat 5.8s ease-in-out infinite;
}

#loadingPanel {
  position: relative;
  z-index: 3;
  width: min(92vw, 620px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 22px 24px 20px;
  border: 1px solid var(--loading-panel-border);
  border-radius: 28px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.88), rgba(246, 251, 255, 0.7)),
    var(--loading-panel-bg);
  backdrop-filter: blur(16px);
  box-shadow: var(--loading-shadow);
  opacity: 0;
  transform: translateY(24px);
  transition:
    opacity 460ms ease,
    transform 780ms cubic-bezier(0.19, 1, 0.22, 1);
}

#loadingOverlay.is-panel-visible #loadingPanel {
  opacity: 1;
  transform: translateY(0);
}

#loadingText {
  margin: 0;
  color: var(--loading-text);
  font: 800 clamp(24px, 4.1vw, 36px) / 1.1 "Cairo", system-ui, sans-serif;
  text-align: center;
}

#loadingSubtext {
  min-height: 24px;
  margin: 0;
  color: var(--loading-subtext);
  font: 700 clamp(13px, 1.7vw, 16px) / 1.7 "Cairo", system-ui, sans-serif;
  text-align: center;
}

#loadingProgressBar {
  position: relative;
  width: 100%;
  height: 16px;
  overflow: hidden;
  border-radius: 999px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.84), rgba(207, 229, 248, 0.68)),
    var(--loading-track);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.9),
    inset 0 -2px 4px rgba(113, 152, 189, 0.12);
}

#loadingProgressFill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0%;
  border-radius: inherit;
  background:
    linear-gradient(90deg, var(--loading-fill-a) 0%, var(--loading-fill-b) 48%, var(--loading-fill-c) 100%);
  box-shadow:
    0 0 0 1px rgba(255, 255, 255, 0.24),
    0 0 24px var(--loading-glow);
  transition: width 220ms ease-out;
}

#loadingProgressFill::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0.62) 50%, rgba(255, 255, 255, 0) 100%);
  transform: translateX(-100%);
  animation: loadingSheen 2.3s linear infinite;
}

#loadingProgressMeta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  color: var(--loading-subtext);
  font: 700 14px/1.3 "Cairo", system-ui, sans-serif;
}

#loadingPercent {
  min-width: 56px;
  color: var(--loading-text);
  text-align: end;
}

#loadingTaskCounter {
  opacity: 0.82;
}

@keyframes loadingPlanetSpin {
  from { transform: translateY(0) scale(1) rotate(0deg); }
  to { transform: translateY(0) scale(1) rotate(360deg); }
}

@keyframes loadingCloudDrift {
  0%, 100% { transform: translate3d(-14px, 0, 0) scale(1.01); }
  50% { transform: translate3d(14px, -5px, 0) scale(1.05); }
}

@keyframes loadingCloudFloat {
  0%, 100% { filter: blur(0.4px) drop-shadow(0 16px 24px rgba(136, 185, 223, 0.12)); opacity: 0.84; }
  50% { filter: blur(0.2px) drop-shadow(0 24px 34px rgba(136, 185, 223, 0.22)); opacity: 0.96; }
}

@keyframes loadingGlowPulse {
  0%, 100% { opacity: 0.82; transform: scale(0.98); }
  50% { opacity: 1; transform: scale(1.05); }
}

@keyframes loadingBgBreath {
  0%, 100% { opacity: 0.94; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.03); }
}

@keyframes loadingSparkleDrift {
  from { transform: translate3d(0, 0, 0); }
  to { transform: translate3d(0, -26px, 0); }
}

@keyframes loadingSheen {
  from { transform: translateX(-120%); }
  to { transform: translateX(120%); }
}

@media (max-width: 640px) {
  #loadingSceneRoot {
    gap: 20px;
    padding: 42px 18px 32px;
  }

  #loadingPlanetWrap {
    width: min(84vw, 390px);
    height: min(84vw, 390px);
  }

  #loadingPanel {
    width: min(94vw, 460px);
    padding: 18px 18px 16px;
    border-radius: 22px;
  }

  #loadingProgressMeta {
    font-size: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #loadingBackground::before,
  #loadingBackground::after,
  #loadingPlanet,
  #loadingCloudRing,
  #loadingGlow,
  #loadingProgressFill::after {
    animation: none !important;
  }

  #loadingOverlay,
  #loadingSceneRoot,
  #loadingPlanetWrap,
  #loadingPanel,
  #loadingPlanet,
  #loadingCloudRing,
  #loadingGlow,
  #loadingBackground {
    transition-duration: 0.01ms !important;
  }
}
