/* The rainy valley behind the player.

   Every animation here moves only `transform` or `opacity`, so the compositor
   plays it without touching layout or paint. Frame animations use steps(),
   which makes each frame a jump the GPU does on its own. Under reduced motion
   the map just sits, see the end of the file. */

.scene { position: fixed; inset: 0; z-index: 0; overflow: hidden; }

/* ---------- world: the map and everything pinned to it ---------- */

/* A box with the map's own 1672:941 ratio that always covers the viewport,
   the same crop `background-size: cover` used to give. Being a size
   container, it defines --px: one pixel of the native map at the current
   scale, so sprites are placed in map coordinates and scale with it. */
.scene__world {
  --px: calc(100cqw / 1672);
  position: absolute;
  left: 50%;
  top: 50%;
  width: max(100vw, calc(100vh * 1672 / 941));
  height: max(100vh, calc(100vw * 941 / 1672));
  translate: -50% -50%;
  container-type: size;
  background: url("../background.webp") 0 0 / 100% 100% no-repeat;
  background-image: image-set(url("../background.avif") type("image/avif"),
                              url("../background.webp") type("image/webp"));
  image-rendering: pixelated;
  will-change: transform;
  animation: camera 150s ease-in-out infinite alternate;
}

/* A slow look around the valley. The scale stays constant so the layer is
   rasterised once; only its position changes. 6% of headroom covers the
   largest translate in any direction. */
@keyframes camera {
  0%   { transform: scale(1.06) translate(-1.8%, -1.2%); }
  50%  { transform: scale(1.06) translate(1.2%, -0.4%); }
  100% { transform: scale(1.06) translate(0.6%, 1.4%); }
}

/* Anything pinned to a landmark: --x/--y/--w/--h in native map pixels. */
.pin {
  position: absolute;
  left: calc(var(--x) * var(--px));
  top: calc(var(--y) * var(--px));
  width: calc(var(--w) * var(--px));
  height: calc(var(--h) * var(--px));
  pointer-events: none;
}

/* A horizontal sprite strip of --n frames. The inner element holds the whole
   strip and steps left one frame at a time. */
.strip { overflow: hidden; }
.strip > i {
  display: block;
  width: calc(var(--n) * 100%);
  height: 100%;
  background: 0 0 / 100% 100% no-repeat;
  image-rendering: pixelated;
  animation: strip var(--d) steps(var(--n)) infinite;
  animation-delay: var(--delay, 0s);
}
@keyframes strip { to { transform: translateX(-100%); } }

/* ---------- water ---------- */

/* Glints drifting over every river and lake, clipped to the water by a mask
   built from the map itself (scripts/build-scene-assets.py). Two layers move
   against each other so the surface reads as rippling, not sliding. */
.water {
  position: absolute;
  inset: 0;
  overflow: hidden;
  -webkit-mask: url("water-mask.png") 0 0 / 100% 100% no-repeat;
          mask: url("water-mask.png") 0 0 / 100% 100% no-repeat;
  pointer-events: none;
}
.water > i {
  position: absolute;
  inset: calc(-48 * var(--px));
  background: url("shimmer.png") 0 0 / calc(48 * var(--px)) calc(48 * var(--px));
  image-rendering: pixelated;
}
.water__a { opacity: .55; animation: glint-a 9s steps(48) infinite; }
.water > .water__b {
  background-position: calc(21 * var(--px)) calc(13 * var(--px));
  animation: glint-b 13s steps(48) infinite, shimmer 5.3s ease-in-out infinite alternate;
}
@keyframes glint-a { to { transform: translate(calc(48 * var(--px)), calc(16 * var(--px))); } }
@keyframes glint-b { to { transform: translate(calc(-48 * var(--px)), calc(8 * var(--px))); } }
@keyframes shimmer { from { opacity: .15; } to { opacity: .5; } }

/* Raindrops landing on the lakes: the ring opens through its six frames in
   the first fifth of the cycle, then the strip sits one frame past its end,
   on nothing, until the next drop. */
.ripple { opacity: .8; }
.ripple > i { animation-name: drop; }
@keyframes drop {
  0%        { transform: translateX(0); }
  20%, 100% { transform: translateX(-100%); }
}

/* The rowboat and the ducks: flyers.js moves and turns the pin; the body
   rocks inside it, a duck faster than the boat. */
.drifter > i {
  position: absolute;
  inset: 0;
  background: 0 0 / 100% 100% no-repeat;
  image-rendering: pixelated;
  animation: bob 3.4s ease-in-out infinite alternate;
}
.duck > i { animation-duration: 1.3s; }
@keyframes bob {
  from { transform: rotate(-6deg) translateY(-4%); }
  to   { transform: rotate(5deg) translateY(4%); }
}

/* Crows and their shadows; bats. Wings beat with the strip; the shadow is
   the same sprite, blacked out and faint, offset on the ground. */
.bird--shadow { filter: brightness(0); opacity: .32; }

/* Waterfalls: white streaks pouring down, faded at both ends. */
.falls {
  overflow: hidden;
  -webkit-mask: linear-gradient(transparent, #000 25%, #000 75%, transparent);
          mask: linear-gradient(transparent, #000 25%, #000 75%, transparent);
}
.falls > i {
  position: absolute;
  inset: calc(-24 * var(--px)) 0 0;
  background: url("falls.png") 0 0 / calc(12 * var(--px)) calc(24 * var(--px));
  image-rendering: pixelated;
  opacity: .8;
  animation: pour .6s steps(8) infinite;
}
@keyframes pour { to { transform: translateY(calc(24 * var(--px))); } }

/* ---------- landmarks ---------- */

.windmill-tower { background: url("windmill-tower.png") 0 0 / 100% 100%; image-rendering: pixelated; }
.windmill-sails {
  background: url("windmill-sails.png") 0 0 / 100% 100%;
  image-rendering: pixelated;
  animation: sails 16s steps(64) infinite;
}
@keyframes sails { to { transform: rotate(360deg); } }

.smoke { opacity: .75; }

/* Warm lamp light breathing irregularly: a flame, not a metronome. */
.lamp {
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(255, 196, 110, .55), rgba(255, 160, 70, .18) 55%, transparent);
  animation: flicker 3.7s linear infinite;
  animation-delay: var(--delay, 0s);
}
@keyframes flicker {
  0%, 100% { opacity: .85; }
  12% { opacity: .62; }
  20% { opacity: .9; }
  43% { opacity: .74; }
  51% { opacity: 1; }
  70% { opacity: .68; }
  83% { opacity: .92; }
}

/* Lit windows glow steadier and smaller than the street lamps; torches at the
   mine and cave mouths gutter fast. Same irregular curve, different pace. */
.lamp--window {
  background: radial-gradient(closest-side, rgba(255, 206, 128, .6), rgba(255, 170, 80, .16) 60%, transparent);
  animation-duration: 6.3s;
}
.lamp--torch {
  background: radial-gradient(closest-side, rgba(255, 168, 76, .7), rgba(255, 118, 40, .22) 55%, transparent);
  animation-duration: 1.6s;
}

/* A gust of wind combing the wheat: a soft band of light swept across the
   field, clipped to the stalks by a mask cut from the map. It crosses in
   the first 40% of each cycle and then the field rests. */
.wheat {
  overflow: hidden;
  mix-blend-mode: screen;               /* brighten the stalks, never grey them */
  -webkit-mask: url("wheat-mask.png") 0 0 / 100% 100% no-repeat;
          mask: url("wheat-mask.png") 0 0 / 100% 100% no-repeat;
}
.wheat > i {
  position: absolute;
  inset: 0 -60%;
  background: linear-gradient(105deg, transparent 38%, rgba(255, 214, 120, .22) 47%,
                              rgba(255, 226, 150, .32) 50%, rgba(255, 214, 120, .22) 53%, transparent 62%);
  transform: translateX(-40%);
  animation: gust 7.5s ease-in-out infinite;
}
/* ±40% of the band's box is exactly the run from just off the field's left
   edge to just off its right. */
@keyframes gust {
  0%        { transform: translateX(-40%); }
  40%, 100% { transform: translateX(40%); }
}

/* Low mist over the swamp and the ruins: two soft banks per spot drifting
   out of step (23s against 37s), so it never visibly repeats. */
.mist > i {
  position: absolute;
  inset: 0;
  background: radial-gradient(50% 45% at 40% 50%, rgba(200, 214, 210, .2), transparent 72%);
  animation: mist-a 23s ease-in-out infinite alternate;
}
.mist > i + i {
  background: radial-gradient(42% 40% at 62% 58%, rgba(192, 208, 206, .17), transparent 72%);
  animation: mist-b 37s ease-in-out infinite alternate;
}
@keyframes mist-a {
  from { transform: translate(-10%, 4%); opacity: .55; }
  to   { transform: translate(9%, -5%); opacity: 1; }
}
@keyframes mist-b {
  from { transform: translate(8%, -3%) scale(1.1); opacity: 1; }
  to   { transform: translate(-7%, 6%) scale(.95); opacity: .5; }
}

/* Fireflies: one native pixel-pair each; scene.js gives every one its own
   wandering path and blink. */
.firefly {
  border-radius: 1px;
  background: #e8f59a;
  box-shadow: 0 0 calc(3 * var(--px)) rgba(220, 245, 120, .9);
  opacity: 0;
}

/* Villagers: the pin moves along a route, the row picks the direction they
   face, and the strip cycles the walk. All three are transforms. */
.villager { overflow: hidden; }
.villager > b {
  display: block;
  width: 100%;
  height: 400%;                       /* four direction rows */
}
.villager > b > i {
  display: block;
  width: calc(var(--n, 6) * 100%);    /* the walk frames */
  height: 100%;
  background: 0 0 / 100% 100% no-repeat;
  image-rendering: pixelated;
  animation: strip var(--d, .66s) steps(var(--n, 6)) infinite;
}

/* ---------- weather over the world ---------- */

/* Cloud shadows sliding over the valley, under every sprite. The tile holds
   three soft shadows and is one world wide; the layer is two tiles wide and
   slides exactly one tile per loop, so the loop has no seam. Inside the
   world, so the shadows stay put on the ground while the camera drifts. */
.scene__clouds {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}
.scene__clouds > i {
  position: absolute;
  top: 0;
  bottom: 0;
  left: -100%;
  width: 200%;
  background:
    radial-gradient(18% 30% at 18% 28%, rgba(0, 6, 10, .17), transparent 70%) 0 0 / 50% 100% repeat-x,
    radial-gradient(24% 34% at 52% 74%, rgba(0, 6, 10, .14), transparent 70%) 0 0 / 50% 100% repeat-x,
    radial-gradient(15% 24% at 84% 40%, rgba(0, 6, 10, .15), transparent 70%) 0 0 / 50% 100% repeat-x;
  will-change: transform;
  animation: clouds 170s linear infinite;
}
@keyframes clouds { to { transform: translateX(50%); } }

/* A slow bank of mist drifting across the valley, behind the rain. Oversized
   and moved with transform only; deliberately faint. */
.scene__fog {
  position: absolute;
  top: 22%;
  height: 46%;
  left: -60%;
  width: 220%;
  pointer-events: none;
  background:
    radial-gradient(55% 60% at 30% 55%, rgba(190, 205, 210, .10), transparent 70%),
    radial-gradient(45% 55% at 68% 45%, rgba(180, 198, 205, .08), transparent 70%);
  animation: drift 84s ease-in-out infinite alternate;
}
@keyframes drift {
  from { transform: translateX(-6%); }
  to   { transform: translateX(10%); }
}

/* Both rain layers are a single 32x128 tile. The sprite's 8 frames are that
   tile offset by 16px each, so moving the layer 128px in steps(8) reproduces
   the animation exactly. The 128px of headroom above keeps the top edge from
   ever showing. Moving the layer (transform) instead of its background
   (background-position) keeps the rain off the main thread entirely. */
.rain {
  position: absolute;
  inset: -128px 0 0 0;
  pointer-events: none;
  image-rendering: pixelated;
  background-repeat: repeat;
  background-size: 32px 128px;
  will-change: transform;
}
.rain--heavy {
  background-image: url("../rain-heavy.png");
  opacity: .55;
  animation: fall .8s steps(8) infinite;    /* 8 frames x 100ms (author's spec) */
}
.rain--light {
  background-image: url("../rain-light.png");
  background-position: 13px 0;
  opacity: .75;
  animation: fall .4s steps(8) infinite;    /* 8 frames x 50ms */
}
@keyframes fall { to { transform: translateY(128px); } }

/* Darken the map and close the edges so the UI keeps contrast. */
.scene__grade {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(120% 85% at 50% 32%, transparent 38%, rgba(4, 7, 9, .72) 100%),
    linear-gradient(to bottom, rgba(8, 14, 18, .30) 0%, rgba(8, 14, 18, .15) 45%, rgba(6, 10, 13, .80) 100%);
}

/* Lightning: a pale wash over everything, fired by scene.js. Peak opacity
   stays low and there are at most two pulses, well under the three flashes
   a second that WCAG 2.3.1 treats as a seizure risk. */
.scene__flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(130% 90% at 40% 10%, rgba(214, 228, 255, .9), rgba(170, 190, 230, .55));
  opacity: 0;
}
.scene__flash--on { animation: flash .9s ease-out; }
@keyframes flash {
  0%   { opacity: 0; }
  6%   { opacity: .32; }
  14%  { opacity: .04; }
  30%  { opacity: .22; }
  100% { opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .scene__world, .scene__fog, .rain, .water > i, .water > .water__b, .falls > i,
  .strip > i, .windmill-sails, .lamp, .villager > b > i,
  .scene__clouds > i, .wheat > i, .mist > i, .mist > i + i, .drifter > i { animation: none; }
  .wheat, .ripple { display: none; }
  .scene__world { transform: scale(1.06); }
  .scene__flash { display: none; }
}
