/*
 * Logo-trace preloader - CSS port of LogoTraceLoader.tsx (no React).
 *
 * Phases (set by static/js/preloader.js on #site-preloader[data-phase]):
 *   loop     a short segment (16% of each path) travels around the outer
 *            frame and the mark, forever, while the page loads
 *   closing  the full outline draws itself closed      (0.6s ease-in-out)
 *   fading   outline cross-fades into the filled logo  (0.45s ease-out)
 *   done     filled logo only; then the overlay fades out (.is-hidden)
 *
 * The <path pathLength="1"> trick makes every dash value a fraction of the
 * path length, exactly like the original component.
 */

.preloader {
  --lt-size: clamp(96px, 22vmin, 148px);
  --lt-stroke: 18;          /* viewBox units (logo is 1095 units wide) */
  --lt-loop: 1.6s;          /* one trip around the logo */
  --lt-close: 0.6s;
  --lt-fade: 0.45s;

  position: fixed;
  inset: 0;
  z-index: 10000;
  display: grid;
  place-items: center;
  background: #f5f7fb;
  color: #02205e;           /* logo navy - paths use currentColor */
  opacity: 1;
  visibility: visible;
  transition: opacity 0.35s ease, visibility 0s linear 0.35s;
  /* Fail-safe: never block the page for more than 12s even if JS fails. */
  animation: lt-failsafe 0.35s ease 12s forwards;
}

.preloader.is-hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.logo-trace {
  width: var(--lt-size);
  height: var(--lt-size);
  overflow: visible;
}

/* Faint full route underneath the moving segment. */
.lt-track {
  opacity: 0.18;
}

.lt-track path {
  fill: none;
  stroke: currentColor;
  stroke-width: calc(var(--lt-stroke) / 2);
  stroke-linejoin: round;
}

/* The moving / closing stroke. */
.lt-trace path {
  fill: none;
  stroke: currentColor;
  stroke-width: var(--lt-stroke);
  stroke-linecap: round;
  stroke-linejoin: round;
}

.preloader[data-phase="loop"] .lt-trace path {
  stroke-dasharray: 0.16 0.84;
  animation: lt-loop var(--lt-loop) linear infinite;
}

.preloader[data-phase="closing"] .lt-trace path,
.preloader[data-phase="fading"] .lt-trace path {
  stroke-dasharray: 1 2;
  animation: lt-close var(--lt-close) ease-in-out both;
}

.preloader[data-phase="fading"] .lt-trace path {
  animation: none;
  stroke-dashoffset: 0;
}

/* Filled logo: hidden until the outline has closed. */
.lt-fill path {
  fill: currentColor;
}

.lt-fill {
  opacity: 0;
}

.preloader[data-phase="fading"] .lt-fill {
  animation: lt-fill-in var(--lt-fade) ease-out both;
}

.preloader[data-phase="fading"] .lt-outline {
  animation: lt-outline-out var(--lt-fade) ease-out both;
}

.preloader[data-phase="done"] .lt-fill {
  opacity: 1;
}

.preloader[data-phase="done"] .lt-outline {
  display: none;
}

@keyframes lt-loop {
  to { stroke-dashoffset: -1; }
}

@keyframes lt-close {
  from { stroke-dashoffset: 1; }
  to { stroke-dashoffset: 0; }
}

@keyframes lt-fill-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes lt-outline-out {
  from { opacity: 1; }
  to { opacity: 0; }
}

@keyframes lt-failsafe {
  to { opacity: 0; visibility: hidden; pointer-events: none; }
}

/* Reduced motion: no tracing at all - show the filled logo statically
   (same behaviour as the React component) and remove the overlay without
   a fade. */
@media (prefers-reduced-motion: reduce) {
  .preloader,
  .preloader * {
    animation: none !important;
    transition: none !important;
  }

  .preloader .lt-outline {
    display: none;
  }

  .preloader .lt-fill {
    opacity: 1;
  }

  .preloader.is-hidden {
    display: none;
  }
}
