/* ==========================================================================
   Page loader: the mascot wakes up, then the screen opens like an eyelid.
   Markup: template-parts/components/page-loader.php (first thing in <body>).
   Script: assets/js/components/page-loader.js (progress bar + exit).
   Every moving part is its own element and animates only transform/opacity,
   so the browser runs it on the compositor and it stays smooth while the
   page's own scripts are still parsing.
   ========================================================================== */

.cn-loader {
  /* Mascot width, and one unit of its 494.4 x 618 viewBox in px. */
  --cn-ld-size: clamp(112px, 12vmin + 56px, 168px);
  --cn-ld-u: calc(var(--cn-ld-size) / 494.4);
  /* Pivots inside the mascot box: body centre, eye centre, neck. */
  --cn-ld-body: 50.965% 61.089%;
  --cn-ld-eye: 50.96% 29.808%;
  --cn-ld-neck: 50.96% 50.4%;

  position: fixed;
  inset: 0;
  z-index: 2147483500; /* over the page progress bar, under the custom cursor */
  overflow: hidden;
  contain: strict;
  color: var(--cn-muted, rgba(248, 244, 240, 0.64));
  font-family: var(--cn-font, "Inter", "DM Sans", sans-serif);
  -webkit-user-select: none;
  user-select: none;
  /* Never trap the page if the script does not run. */
  animation: cn-ld-failsafe 0.4s 9s forwards;
}

/* Later pages in the same session (class set in <head>, before first paint). */
.cn-loader-skip .cn-loader {
  display: none;
}

.cn-loader.is-leaving {
  pointer-events: none;
}

/* ---- Eyelids: two panels with arched edges; opening them makes an eye shape. */
.cn-loader__lid {
  position: absolute;
  left: 0;
  width: 100%;
  /* 75% each, so the arches overlap in the middle while closed. */
  height: 75%;
}

.cn-loader__lid--top { top: 0; }
.cn-loader__lid--bottom { bottom: 0; }

.cn-loader__lid-shape {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  fill: var(--cn-bg, #000);
}

/* Edge line, shown only while the lids open (it is hidden under the other lid). */
.cn-loader__rim {
  position: absolute;
  left: 0;
  display: block;
  width: 100%;
  height: 30%;
  overflow: visible;
  opacity: 0;
}

.cn-loader__lid--top .cn-loader__rim { bottom: 0; }
.cn-loader__lid--bottom .cn-loader__rim { top: 0; }

.cn-loader__rim path {
  fill: none;
  stroke: url(#cn-loader-rim-gradient);
  stroke-width: 1.5;
  vector-effect: non-scaling-stroke;
}

.cn-loader__rim .cn-loader__rim-glow {
  stroke-width: 10;
  stroke-opacity: 0.18;
}

/* ---- Stage: mascot, glow, shadow and progress, centred over the lids. */
.cn-loader__stage {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: calc(var(--cn-ld-size) * 0.26);
  padding: 0 16px;
}

.cn-loader__figure {
  position: relative;
  width: var(--cn-ld-size);
  height: calc(var(--cn-ld-size) * 1.25); /* 618 / 494.4 */
}

.cn-loader__glow {
  position: absolute;
  left: 50%;
  top: 42%;
  width: calc(var(--cn-ld-size) * 3.2);
  height: calc(var(--cn-ld-size) * 3.2);
  margin: calc(var(--cn-ld-size) * -1.6) 0 0 calc(var(--cn-ld-size) * -1.6);
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(41, 104, 178, 0.3), rgba(41, 104, 178, 0.1) 45%, rgba(250, 163, 39, 0.04) 68%, rgba(250, 163, 39, 0));
  animation: cn-ld-fade 1.2s ease-out 0.1s both, cn-ld-breathe 3.6s ease-in-out 1.3s infinite;
}

.cn-loader__shadow {
  position: absolute;
  left: 22%;
  top: 88%;
  width: 56%;
  height: 9%;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(41, 104, 178, 0.55), rgba(41, 104, 178, 0));
  animation: cn-ld-fade 0.6s ease-out 0.2s both, cn-ld-shadow 2.8s ease-in-out 1.3s infinite;
}

.cn-loader__mascot {
  position: absolute;
  inset: 0;
  animation: cn-ld-float 2.8s ease-in-out 1.3s infinite;
}

/* Each part is a full-size layer holding one piece of the mascot. */
.cn-loader__part,
.cn-loader__sway {
  position: absolute;
  inset: 0;
}

.cn-loader__svg {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

/* Body pops in first. */
.cn-loader__body {
  transform-origin: var(--cn-ld-body);
  animation: cn-ld-pop 0.6s cubic-bezier(0.34, 1.56, 0.64, 1) 0.05s both;
}

/* Arms and legs swing out of the body, then keep moving; the right hand waves. */
.cn-loader__limb,
.cn-loader__sway {
  transform-origin: var(--cn-ld-body);
}

.cn-loader__limb {
  animation: cn-ld-limb 0.7s cubic-bezier(0.34, 1.56, 0.64, 1) var(--cn-ld-delay) both;
}

.cn-loader__sway {
  animation: cn-ld-sway var(--cn-ld-period) linear calc(var(--cn-ld-delay) + 0.9s) infinite;
}

.cn-loader__limb--lh { --cn-ld-from: 50deg;  --cn-ld-delay: 0.22s; --cn-ld-amp: 8deg;   --cn-ld-period: 2.6s; }
.cn-loader__limb--rh { --cn-ld-from: -50deg; --cn-ld-delay: 0.3s;  --cn-ld-amp: -16deg; --cn-ld-period: 1.5s; }
.cn-loader__limb--lf { --cn-ld-from: 40deg;  --cn-ld-delay: 0.38s; --cn-ld-amp: 5deg;   --cn-ld-period: 2.2s; }
.cn-loader__limb--rf { --cn-ld-from: -40deg; --cn-ld-delay: 0.46s; --cn-ld-amp: -6deg;  --cn-ld-period: 2.4s; }

/* Head drops onto the body with a squash, then tilts gently. */
.cn-loader__head {
  transform-origin: var(--cn-ld-neck);
  animation: cn-ld-head 0.85s 0.42s both;
}

.cn-loader__tilt {
  --cn-ld-amp: 3deg;
  transform-origin: var(--cn-ld-neck);
  animation: cn-ld-sway 3.6s linear 1.5s infinite;
}

/* The eye starts shut and opens once the head has landed ("waking up"). */
.cn-loader__eye {
  transform-origin: var(--cn-ld-eye);
  animation: cn-ld-open 0.5s ease-out 1.05s both;
}

/* Look loop and blink share one timeline so the blink lands on a glance. */
.cn-loader__blink {
  transform-origin: var(--cn-ld-eye);
  animation: cn-ld-blink 4.8s 1.6s infinite;
}

.cn-loader__pupil {
  animation: cn-ld-look 4.8s 1.6s infinite;
}

/* ---- Progress */
.cn-loader__meta {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: baseline;
  column-gap: 16px;
  row-gap: 12px;
  width: min(232px, 100%);
  font-size: 11px;
  font-weight: 500;
  line-height: 1;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  white-space: nowrap;
  animation: cn-ld-rise 0.7s cubic-bezier(0.2, 0.8, 0.2, 1) 0.6s both;
}

.cn-loader__count {
  color: var(--cn-white, #f8f4f0);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
}

.cn-loader__bar {
  grid-column: 1 / -1;
  height: 2px;
  overflow: hidden;
  border-radius: 2px;
  background: var(--cn-line, rgba(248, 244, 240, 0.14));
}

.cn-loader__fill {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, #2968b2, #faa327);
  transform: scaleX(0);
  transform-origin: 0 50%;
  will-change: transform;
}

/* ---- Exit: a wink, the mascot shrinks away, the lids open on the page. */
.cn-loader.is-leaving .cn-loader__blink {
  animation: cn-ld-wink 0.3s ease-in-out both;
}

.cn-loader.is-leaving .cn-loader__stage {
  animation: cn-ld-stage-out 0.45s cubic-bezier(0.55, 0, 0.75, 0.2) 0.2s forwards;
}

.cn-loader.is-leaving .cn-loader__rim {
  animation: cn-ld-show 0.2s ease-out 0.32s both;
}

.cn-loader.is-leaving .cn-loader__lid--top {
  animation: cn-ld-lid-up 0.95s cubic-bezier(0.76, 0, 0.24, 1) 0.4s forwards;
}

.cn-loader.is-leaving .cn-loader__lid--bottom {
  animation: cn-ld-lid-down 0.95s cubic-bezier(0.76, 0, 0.24, 1) 0.4s forwards;
}

/* Reduced motion: a still mascot and progress bar, then a short fade. */
@media (prefers-reduced-motion: reduce) {
  .cn-loader__glow,
  .cn-loader__shadow,
  .cn-loader__mascot,
  .cn-loader__part,
  .cn-loader__sway,
  .cn-loader.is-leaving .cn-loader__blink,
  .cn-loader.is-leaving .cn-loader__stage,
  .cn-loader.is-leaving .cn-loader__rim,
  .cn-loader.is-leaving .cn-loader__lid--top,
  .cn-loader.is-leaving .cn-loader__lid--bottom {
    animation: none !important;
  }

  .cn-loader__meta {
    animation-name: cn-ld-fade;
  }

  .cn-loader.is-leaving {
    animation: cn-ld-out 0.35s ease forwards;
  }
}

@keyframes cn-ld-failsafe { to { opacity: 0; visibility: hidden; } }
@keyframes cn-ld-fade { from { opacity: 0; } }
@keyframes cn-ld-show { from { opacity: 0; } to { opacity: 1; } }
@keyframes cn-ld-out { to { opacity: 0; } }
@keyframes cn-ld-rise { from { opacity: 0; transform: translateY(10px); } }
@keyframes cn-ld-pop { from { transform: scale(0); } }

@keyframes cn-ld-limb {
  from { transform: rotate(var(--cn-ld-from)) scale(0); }
  to { transform: rotate(0deg) scale(1); }
}

@keyframes cn-ld-head {
  0% {
    opacity: 0;
    transform: translateY(calc(var(--cn-ld-u) * -260)) scale(0.8);
    animation-timing-function: cubic-bezier(0.5, 0, 0.9, 0.4);
  }
  20% { opacity: 1; }
  45% {
    transform: translateY(0) scale(1.1, 0.84);
    animation-timing-function: cubic-bezier(0.25, 0.8, 0.35, 1);
  }
  72% {
    transform: translateY(calc(var(--cn-ld-u) * -16)) scale(0.96, 1.05);
    animation-timing-function: cubic-bezier(0.45, 0, 0.55, 1);
  }
  100% { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes cn-ld-open {
  0% { transform: scaleY(0.06); }
  70% { transform: scaleY(1.04); }
  100% { transform: scaleY(1); }
}

@keyframes cn-ld-blink {
  0%, 29% { transform: scaleY(1); }
  31.5% { transform: scaleY(0.06); }
  34%, 100% { transform: scaleY(1); }
}

/* Quick glances (saccades) with holds in between: left, right, up, centre. */
@keyframes cn-ld-look {
  0%, 8% {
    transform: translate(0px, 0px);
    animation-timing-function: cubic-bezier(0.3, 0, 0.1, 1);
  }
  12%, 30% {
    transform: translate(calc(var(--cn-ld-u) * -20), calc(var(--cn-ld-u) * 5));
    animation-timing-function: cubic-bezier(0.3, 0, 0.1, 1);
  }
  34%, 56% {
    transform: translate(calc(var(--cn-ld-u) * 20), calc(var(--cn-ld-u) * 5));
    animation-timing-function: cubic-bezier(0.3, 0, 0.1, 1);
  }
  60%, 78% {
    transform: translate(calc(var(--cn-ld-u) * 8), calc(var(--cn-ld-u) * -14));
    animation-timing-function: cubic-bezier(0.3, 0, 0.1, 1);
  }
  82%, 100% { transform: translate(0px, 0px); }
}

/* Pendulum (sine-like) around the element's origin, amplitude --cn-ld-amp. */
@keyframes cn-ld-sway {
  0% { transform: rotate(0deg); animation-timing-function: cubic-bezier(0.61, 1, 0.88, 1); }
  25% { transform: rotate(var(--cn-ld-amp)); animation-timing-function: cubic-bezier(0.12, 0, 0.39, 0); }
  50% { transform: rotate(0deg); animation-timing-function: cubic-bezier(0.61, 1, 0.88, 1); }
  75% { transform: rotate(calc(var(--cn-ld-amp) * -1)); animation-timing-function: cubic-bezier(0.12, 0, 0.39, 0); }
  100% { transform: rotate(0deg); }
}

@keyframes cn-ld-float {
  0%, 100% { transform: translateY(0px); }
  50% { transform: translateY(calc(var(--cn-ld-u) * -22)); }
}

@keyframes cn-ld-shadow {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(0.78); }
}

@keyframes cn-ld-breathe {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.08); }
}

@keyframes cn-ld-wink {
  0%, 100% { transform: scaleY(1); }
  45% { transform: scaleY(0.06); }
}

@keyframes cn-ld-stage-out { to { opacity: 0; transform: scale(0.9); } }
@keyframes cn-ld-lid-up { to { transform: translateY(-101%); } }
@keyframes cn-ld-lid-down { to { transform: translateY(101%); } }
