/* =============================================
   ADVENTO KALENDORIUS: WHAT MOVES WITH THE SCROLLING
   js/motion.js measures the page and hands over --p, --pin, --up (on [data-scrub]) and --sway (on [data-sway]);
   the rules here and in advento-kalendorius.css say what those numbers do. Every rule names its resting
   value, so the page is whole without the script and stands still for readers who asked for calm motion.
   ============================================= */

/* =============================================
   THE HERO, ARRIVING (once, as the page opens)
   The night settles in, the title rises, the Christmas things come up out of the snow one after another,
   far ones first, and then their candles light. Only for a page with its script and a reader who did not ask
   for calm motion: otherwise everything simply stands there. The garden comes down on its thread by itself
   (js/garden.js).
   ============================================= */
@media (prefers-reduced-motion: no-preference) {
  /* the sky comes down from a lighter blue hour into the night, and the stars come out */
  .js .adv-hero > .adv-hero__sky::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(180deg, var(--adv-night-2) 0%, var(--adv-night-3) 46%, var(--adv-dusk) 82%, #B5C8EC 100%);
    opacity: 0;
    animation: adv-night-falls 1.3s cubic-bezier(.22, 1, .36, 1) both;
  }

  .js .adv-hero > .adv-hero__sky > .adv-hero__stars {
    animation: adv-come-out 1.3s ease-out 0.25s backwards;
  }

  .js .adv-hero > .adv-hero__sky > .adv-hero__stars--b {
    animation: adv-twinkle 5.5s ease-in-out infinite alternate, adv-come-out 1.5s ease-out 0.55s backwards;
  }

  /* the last light on the horizon sinks to its evening height */
  .js .adv-hero > .adv-hero__sky > .adv-hero__glow {
    transform-origin: 50% 100%;
    animation: adv-last-light 1.6s cubic-bezier(.22, 1, .36, 1) backwards;
  }

  /* the words around the title rise with it (the title's own lines rise by blog.css) */
  .js .adv-hero .crumbs,
  .js .adv-hero__meta {
    animation: adv-text-up 0.7s cubic-bezier(.22, 1, .36, 1) backwards;
  }

  .js .adv-hero__eyebrow {
    animation: adv-text-up 0.7s cubic-bezier(.22, 1, .36, 1) 0.06s backwards;
  }

  .js .adv-hero__more > * {
    animation: adv-text-up 0.75s cubic-bezier(.22, 1, .36, 1) 0.4s backwards;
  }

  .js .adv-hero__more > .adv-status { animation-delay: 0.48s; }
  .js .adv-hero__more > .adv-hero__byline { animation-delay: 0.54s; }
  .js .adv-hero__more > .adv-hero__actions { animation-delay: 0.6s; }

  /* the Christmas things come up out of the snow (or from behind the near hill) with a springy settle:
     each one rises above the line its feet stand on, stretches a little, squashes and stands */
  .js .adv-hero .adv-tree {
    animation: adv-below-snow calc(var(--arrive, 0.5s) + 0.85s) linear backwards;
  }

  .js .adv-hero .adv-tree > img {
    transform-origin: 50% 100%;
    animation: adv-come-up 0.85s var(--arrive, 0.5s) backwards;
  }

  .js .adv-hero .adv-tree > i {
    animation: adv-candle 3.4s ease-in-out infinite alternate, adv-light-up 0.9s ease-out calc(var(--arrive, 0.5s) + 0.7s) backwards;
  }

  .js .adv-hero .adv-tree > i + i {
    animation: adv-candle 4.6s ease-in-out -2.1s infinite alternate, adv-light-up 0.9s ease-out calc(var(--arrive, 0.5s) + 0.88s) backwards;
  }

  .js .adv-hero .adv-grove--front .adv-tree::before {
    animation: adv-shadow-in 0.45s ease-out calc(var(--arrive, 0.5s) + 0.85s) backwards;
  }

  /* far ones first */
  .adv-tree--mazoji { --arrive: 0.25s; }
  .adv-tree--siaudine { --arrive: 0.36s; }
  .adv-tree--didzioji { --arrive: 0.46s; }
  .adv-tree--megzta-balta { --arrive: 0.6s; }
  .adv-tree--zibintas { --arrive: 0.68s; }
  .adv-tree--megzta { --arrive: 0.76s; }
  .adv-tree--rogutes { --arrive: 0.84s; }
  .adv-tree--dovanos { --arrive: 0.92s; }

  /* the buttons that turn the garden come once it hangs there */
  .js .adv-hero .adv-turn:not([hidden]) {
    animation: adv-text-up 0.6s cubic-bezier(.22, 1, .36, 1) 1.1s backwards;
  }
}

@keyframes adv-night-falls {
  from { opacity: 0.75; }
  to { opacity: 0; }
}

@keyframes adv-come-out {
  from { opacity: 0; }
}

@keyframes adv-last-light {
  from { scale: 1 1.55; }
}

@keyframes adv-text-up {
  from { opacity: 0; transform: translateY(14px); }
}

@keyframes adv-below-snow {
  from, to { clip-path: inset(-60% -60% 0 -60%); }
}

@keyframes adv-come-up {
  0% { transform: translateY(104%) scale(0.9, 1.1); animation-timing-function: cubic-bezier(.2, .7, .3, 1); }
  50% { transform: translateY(0) scale(0.95, 1.07); animation-timing-function: cubic-bezier(.3, 0, .4, 1); }
  68% { transform: translateY(0) scale(1.045, 0.95); animation-timing-function: cubic-bezier(.3, 0, .4, 1); }
  84% { transform: translateY(0) scale(0.99, 1.018); animation-timing-function: cubic-bezier(.3, 0, .4, 1); }
  100% { transform: translateY(0) scale(1, 1); }
}

/* the candles flare as they are lit and then breathe on (the missing end is the breathing itself) */
@keyframes adv-light-up {
  0% { opacity: 0; }
  45% { opacity: 1; }
}

@keyframes adv-shadow-in {
  from { opacity: 0; scale: 0.4 1; }
}

/* =============================================
   THE HERO, SCROLLING AWAY
   The header carries data-scrub: --up goes 0 → 1 over the first screen of scrolling. The scene comes apart
   into depth: the stars stay back most, then the evening light, the far hill with its spruce, the trees
   between the hills. The near hill flattens a little towards its foot (which stays put, so no edge ever shows)
   and whatever stands on its snow rides it down, so the things keep their feet in the snow. The title column
   lifts and softens. data-sway on the hills makes the light ones among the trees lean in the scrolling's wind.
   ============================================= */
@media (prefers-reduced-motion: no-preference) {
  .adv-hero .adv-hero__stars {
    translate: 0 calc(var(--up, 0) * 40vh);
    will-change: translate;
  }

  .adv-hero .adv-hero__stars--b {
    translate: 0 calc(var(--up, 0) * 30vh);
  }

  .adv-hero .adv-hero__glow {
    translate: 0 calc(var(--up, 0) * 20vh);
    will-change: translate;
  }

  /* how far (in hill heights, over a screen of scrolling) the far hill and the trees between the hills sink;
     the trees must sink faster than the near crest flattens (0.14 × the snow's height under them) */
  .adv-hills {
    --far-sink: 0.45;
    --mid-sink: 0.24;
  }

  .adv-hills__far,
  .adv-grove--far {
    translate: 0 calc(var(--up, 0) * var(--hill) * var(--far-sink));
    will-change: translate;
  }

  .adv-hills__far ~ .adv-grove:not(.adv-grove--far):not(.adv-grove--front) {
    translate: 0 calc(var(--up, 0) * var(--hill) * var(--mid-sink));
    will-change: translate;
  }

  .adv-hills__near {
    transform-origin: 50% 100%;
    scale: 1 calc(1 - var(--up, 0) * 0.14);
    will-change: scale;
  }

  /* exactly as far as the snow under its feet has come down */
  .adv-grove--front .adv-tree {
    translate: -50% calc(var(--up, 0) * 0.14 * (var(--s) * var(--hill) - var(--down, 10px)));
  }

  .adv-hills .adv-tree {
    transform-origin: 50% 100%;
    rotate: calc(var(--sway, 0) * var(--lean, 0) * 1deg);
    will-change: translate, rotate;
  }

  .adv-tree--siaudine { --lean: 2.2; }
  .adv-tree--megzta-balta { --lean: 1.8; }
  .adv-tree--megzta { --lean: 1.5; }
  .adv-tree--mazoji { --lean: 1; }
  .adv-tree--didzioji { --lean: 0.8; }
  .adv-tree--zibintas { --lean: 0.6; }

  .adv-hero .adv-hero__top {
    translate: 0 calc(var(--up, 0) * -12vh);
    opacity: calc(1 - var(--up, 0) * var(--up, 0) * 0.8);
    will-change: translate;
  }
}

@media (prefers-reduced-motion: no-preference) and (min-width: 1024px) {
  .adv-hero .adv-hero__more {
    translate: 0 calc(var(--up, 0) * -7vh);
    opacity: calc(1 - var(--up, 0) * var(--up, 0) * 0.7);
    will-change: translate;
  }
}

@media (prefers-reduced-motion: no-preference) and (max-width: 1023px) {
  /* stacked under the title, the hills are looked at half a screen later: they come apart more gently */
  .adv-hills {
    --far-sink: 0.3;
    --mid-sink: 0.2;
  }

  /* on a phone the far spruce stays home and the things stand in another order: back ones first */
  .adv-tree--siaudine { --arrive: 0.3s; }
  .adv-tree--didzioji { --arrive: 0.4s; }
  .adv-tree--zibintas { --arrive: 0.55s; }
  .adv-tree--megzta { --arrive: 0.63s; }
  .adv-tree--dovanos { --arrive: 0.71s; }
  .adv-tree--megzta-balta { --arrive: 0.79s; }
}

/* the moon catches the crest of each hill: a thin bright rim that follows the snow's edge (the hill's own mask
   keeps it on the snow; this one takes away everything a few pixels under the crest) */
.adv-hills__far::before,
.adv-hills__near::before {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0.95) 50%, rgba(255, 255, 255, 0.35));
  -webkit-mask: linear-gradient(#000 0 0), var(--adv-hills) repeat-x 50% calc(100% + 3px) / var(--near-w) 100%;
  mask: linear-gradient(#000 0 0), var(--adv-hills) repeat-x 50% calc(100% + 3px) / var(--near-w) 100%;
  -webkit-mask-composite: xor;
  mask-composite: exclude;
}

.adv-hills__far::before {
  opacity: 0.45;
  -webkit-mask: linear-gradient(#000 0 0), var(--adv-hills-far) repeat-x calc(50% + var(--far-x)) calc(100% + 2px) / var(--far-w) 100%;
  mask: linear-gradient(#000 0 0), var(--adv-hills-far) repeat-x calc(50% + var(--far-x)) calc(100% + 2px) / var(--far-w) 100%;
  -webkit-mask-composite: xor;
  mask-composite: exclude;
}

/* =============================================
   THE WHOLE PAGE
   ============================================= */

/* the snow that falls through every section (js/motion.js draws it; never there in calm motion) */
.adv-snow {
  position: fixed;
  inset: 0;
  z-index: 998;
  display: block;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* the reading bar is a golden straw; with the script it also carries a red wooden bead at its tip */
.adv .read-progress {
  top: 2px;
  height: 4px;
}

.adv .read-progress__bar {
  border-radius: 0 2px 2px 0;
  background: linear-gradient(180deg, #FFF4CC 0%, var(--adv-gold-light) 24%, var(--adv-gold) 58%, #C08C20 100%);
  box-shadow: 0 1px 2px rgba(107, 74, 0, 0.35);
}

.adv .read-progress.is-straw {
  container-type: inline-size;
}

.adv .read-progress.is-straw .read-progress__bar {
  transform: scaleX(var(--read, 0)) !important;
}

.adv .read-progress.is-straw::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  width: 10px;
  height: 10px;
  margin: -5px 0 0 -5px;
  border-radius: 50%;
  background: radial-gradient(circle at 34% 30%, #F4A3AE 0, var(--adv-red) 40%, var(--adv-red-deep) 100%);
  box-shadow: 0 1px 3px rgba(80, 10, 25, 0.45);
  translate: calc(var(--read, 0) * 100cqw) 0;
  scale: min(1, calc(var(--read, 0) * 60));
}

/* =============================================
   THE ELF TURNS UP AROUND THE PAGE
   Away from his garden he is a picture (scripts/render-elf.mjs photographs the same doll in four poses, all
   at 1600 px to a metre; where each picture is held is written in virselis/nykstukas.json):
     over the top of the shelf of games he peeks out, and ducks when someone points at him
     down the wall of "Kas tas sodas?" he lets himself on a thread, as far as the reader has scrolled
     on the box of sources he sits and dangles his legs
     in the snow of the closing night he waves goodbye
   He is decoration (aria-hidden); pointing at him or tapping him makes him hop.
   ============================================= */
.adv-elf {
  position: absolute;
  z-index: 2;
  pointer-events: none;
}

.adv-elf > img,
.adv-shelf__peek > img,
.adv-cta__prop--elf > img {
  display: block;
  width: auto;
  max-width: none;
  pointer-events: auto;
  -webkit-user-select: none;
  user-select: none;
  -webkit-user-drag: none;
}

/* --- on a thread: his fist is 88.6% across and 21.2% down the picture --- */
.adv-elf--kabo {
  --h: clamp(104px, 10.4vw, 150px);
  --rope: calc(22px + var(--pin, 1) * clamp(40px, 6.6vw, 96px));
  top: -6px;
  right: calc(max(0px, (100% - var(--container)) / 2) + var(--space-lg) + clamp(8px, 2vw, 30px));
  width: 0;
  height: var(--rope);
  rotate: calc(1.5deg + var(--sway, 0) * 8deg);
  transform-origin: 50% 0;
}

.adv-elf--kabo::before {
  content: '';
  position: absolute;
  left: -1px;
  top: 0;
  bottom: -2px;
  width: 2px;
  border-radius: 1px;
  background: linear-gradient(90deg, var(--adv-red), var(--adv-red-deep));
}

.adv-elf--kabo > img {
  position: absolute;
  left: 0;
  top: 100%;
  height: var(--h);
  translate: -88.6% -21.2%;
  filter: drop-shadow(0 10px 10px rgba(92, 58, 12, 0.22));
}

/* --- sitting: the line he sits on is 83.1% down the picture, his legs hang over the box --- */
.adv-elf--sedi {
  --h: clamp(92px, 9vw, 124px);
  --land: clamp(0, var(--pin, 1) * 2.6 - 0.9, 1);
  position: relative;
  display: block;
  height: 0;
}

.adv-elf--sedi > img {
  position: absolute;
  right: clamp(14px, 5%, 46px);
  bottom: 0;
  height: var(--h);
  opacity: clamp(0, var(--land) * 3, 1);
  translate: 0 calc(16.9% - (1 - var(--land)) * 46px);
  filter: drop-shadow(0 6px 6px rgba(27, 42, 82, 0.16));
}

/* --- waving in the snow of the closing night: between his feet is 71.8% across the picture --- */
.adv-cta__prop--elf {
  aspect-ratio: 328 / 596;
  --t: 0.3725;
  --k: 1.06;
  /* beside the two buttons (together 457px wide, in the middle): his waving mitten, the right edge of
     the picture, stays 22px short of them at every width */
  --x: calc(50% - 250px - var(--m) * 0.0613);
  --s: 0.5;
  --rise: 58px;
  translate: -71.8% calc(var(--low) * var(--rise));
}

.adv-cta__prop--elf > img {
  width: 100%;
  height: 100%;
}

@media (max-width: 1023px) {
  .adv-cta__prop--elf { --x: 37%; --s: 0.34; }
}

@media (max-width: 759px) {
  .adv-cta__prop--elf { --x: 33%; --s: 0.3; --k: 1.15; }
}

/* --- peeking over the shelf: the edge under his mittens is 56.2% down the picture, his neck 78.3% across.
   Only what is above the edge is shown; he comes up after the toys have been set out. --- */
.adv-shelf__peek {
  --h: clamp(104px, 10vw, 138px);
  position: absolute;
  left: 46%;
  bottom: -1px;
  width: calc(var(--h) * 385 / 470);
  height: calc(var(--h) * 0.562);
  overflow: hidden;
  translate: -78.3% 0;
}

.adv-shelf__peek > img {
  position: absolute;
  left: 0;
  top: 0;
  height: var(--h);
}

/* On a narrow phone the top of the shelf is only some 310px wide: the lantern steps aside, the red knitted
   tree takes its place, and he peeks out half-way between that tree and the straw one (their inner edges
   are 7% + 0.2034 --toy and 94% - 0.8776 --toy from the left; the middle of his picture is 28.3% of its
   width before the place it is held at) */
@media (max-width: 479px) {
  .adv-shelf__toy--lantern {
    display: none;
  }

  .adv-shelf__toy--red {
    left: 7%;
  }

  .adv-shelf__peek {
    --h: 96px;
    left: calc(50.5% - var(--toy) * 0.3371 + var(--h) * 0.2318);
  }
}

@media (prefers-reduced-motion: no-preference) {
  .js .adv-shelf__peek > img {
    translate: 0 60%;
    transition: translate 0.75s cubic-bezier(0.34, 1.56, 0.64, 1) 1.5s;
  }

  .js .adv-shelf__panel.is-in + .adv-shelf__top .adv-shelf__peek > img {
    translate: 0 0;
  }

  /* found: he ducks behind the shelf and comes up again */
  .js .adv-shelf__panel.is-in + .adv-shelf__top .adv-shelf__peek > img:hover {
    animation: adv-elf-duck 1.7s cubic-bezier(0.5, 0, 0.3, 1);
  }

  .adv-elf > img:hover,
  .adv-cta__prop--elf > img:hover {
    animation: adv-elf-hop 0.7s cubic-bezier(0.3, 0, 0.3, 1);
  }
}

/* a hop on the spot: scale and rotate only, so the place the picture is held at stays where it is */
@keyframes adv-elf-hop {
  0%, 100% { scale: 1; rotate: 0deg; }
  18% { scale: 1.04 0.94; }
  45% { scale: 0.96 1.07; rotate: -5deg; }
  70% { scale: 1.02 0.98; rotate: 3deg; }
}

@keyframes adv-elf-duck {
  0%, 100% { translate: 0 0; }
  14%, 58% { translate: 0 47%; }
  78% { translate: 0 -3%; }
}

@media print {
  .adv-snow,
  .adv .read-progress {
    display: none;
  }
}
