/* The first screen: the valley in the dark. */

/* A scene is a size container, so its layers can be sized from it (cqw, cqh). */
.scene { container-type: size; }

/* Every layer is a box with the painting's shape, placed by a focal point (--fx, --fy: 0 keeps the
   left/top edge, 1 the right/bottom). Things inside a layer are placed in percent of the painting.
   Layers are drawn a little oversized so drifting never shows an edge.

   Which painting, and how it is fitted, follows the shape of the scene itself (container queries),
   not the window: the scene does not change shape when a phone's toolbar slides away.
     under 4:5         the tall painting, covering the scene
       3:5 to 4:5      ...shown from a little higher, for more sky (upright tablets, small phones)
     4:5 to 11:10      the wide painting, set at the foot of the scene with the sky carried on above it
     11:10 and wider   the wide painting, covering the scene */
.valley .layer {
  --ratio: 0.46154;       /* the tall painting, 1728 x 3744 */
  --fx: 0.5;
  --fy: 1;
  --stage-w: max(100cqw, 100cqh * var(--ratio));
}
@container (min-aspect-ratio: 3/5) { .valley .layer { --fy: 0.93; } }
@media (min-aspect-ratio: 4/5) {
  .valley .layer { --ratio: 1.77778; --fx: 0.86; --fy: 0.6; }      /* the wide painting, 3840 x 2160 */
  @container (max-aspect-ratio: 11/10) {
    .valley .layer { --stage-w: 149cqw; --fx: 0.912; --fy: 1; }
    /* the painted sky melts into the sky drawn behind it, which carries on to the top of the scene */
    .valley .layer.back img { -webkit-mask-image: linear-gradient(to bottom, transparent 0%, #000 30%); mask-image: linear-gradient(to bottom, transparent 0%, #000 30%); }
  }
}
.scene .layer {
  --w: var(--stage-w);
  --h: calc(var(--stage-w) / var(--ratio));
  inset: auto;
  width: var(--w);
  height: var(--h);
  left: calc((100cqw - var(--w)) * var(--fx));
  top: calc((100cqh - var(--h)) * var(--fy));
  scale: var(--oversize);
}
.scene .layer img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: fill; }

.valley { --oversize: 1.03; }
/* the sky behind the paintings: their own top colour, a shade deeper overhead */
.valley::before { content: ""; position: absolute; inset: 0; background: linear-gradient(to bottom, var(--sky-top) 0%, var(--sky-mid) 46%, var(--sky-low) 62%, var(--night) 70%); }

/* ---- the opening ----
   Until the valley's pictures are all in, the first screen is plain night and the words: nothing is
   shown half-arrived. Then js/hero.js marks the scene "up" and, once, the powerhouse windows flicker
   on and their light spreads out over the valley ("opening"), with the sky and the leaves rising
   behind it. What stands on the village (glow, smoke, water, the line) waits for the village. */
html.js .valley:not(.up)::before, html.js .valley:not(.up) .layer, html.js .valley:not(.up) .cue, html.js .valley:not(.up) canvas.wind { opacity: 0; }
html.js .valley:not(.has-village) .layer.village > :not(picture) { visibility: hidden; }
@property --reveal { syntax: "<number>"; inherits: false; initial-value: 1.3; }

/* The foot of the scene is cut with the page's brushed edge, and plain night lies below it. As the page
   scrolls the leaves in front rise by --lift (js/hero.js); the night rises with them, so the straight
   foot of their picture never shows. */
.valley::after { content: ""; position: absolute; left: 0; right: 0; bottom: -1px; height: calc(var(--edge) + var(--lift, 0px)); background: var(--night); -webkit-mask: url("../assets/img/edge/top.webp") top / auto var(--edge) repeat-x, linear-gradient(#000, #000) bottom / 100% calc(100% - var(--edge) + 1px) no-repeat; mask: url("../assets/img/edge/top.webp") top / auto var(--edge) repeat-x, linear-gradient(#000, #000) bottom / 100% calc(100% - var(--edge) + 1px) no-repeat; pointer-events: none; z-index: 5; }

/* canvases are placed in their layer by js/hero.js, from js/data/valley.js */
.scene canvas.sky, .scene canvas.smoke, .scene canvas.fireflies { position: absolute; inset: auto; pointer-events: none; }
/* the wind's canvas (petals, birds) covers the whole scene, in front of the village and behind the leaves */
.scene canvas.wind { pointer-events: none; }
.scene canvas.sky { left: 0; top: 0; width: 100%; }
.scene canvas.fireflies { left: 0; bottom: 0; width: 100%; height: 42%; top: auto; }

/* drifting cloud and mist: wide painted strips, far wider than the scene, moving slowly out of step */
.air { position: absolute; background-repeat: no-repeat; background-size: 100% 100%; pointer-events: none; }
.cloud { left: -20%; width: 130%; height: 9%; opacity: 0.17; filter: blur(7px); }
.cloud-a { top: 9%; background-image: url("../assets/img/air/cloud-1-1024.webp"); }
.cloud-b { top: 31%; background-image: url("../assets/img/air/cloud-2-1024.webp"); opacity: 0.13; }
.mist { left: -40%; width: 180%; opacity: 0.32; }
.mist-a { top: 52%; height: 20%; background-image: url("../assets/img/air/mist-1-1024.webp"); }
.mist-b { top: 66%; height: 26%; background-image: url("../assets/img/air/mist-2-1024.webp"); opacity: 0.18; }
@media (min-aspect-ratio: 4/5) {
  .cloud { height: 16%; left: -10%; width: 90%; }
  .cloud-a { top: 6%; }
  .cloud-b { top: 22%; left: 30%; }
  .mist-a { top: 44%; height: 34%; left: -60%; width: 150%; }
  .mist-b { top: 62%; height: 40%; left: -70%; width: 170%; }
}

/* the powerhouse windows' glow, breathing faintly; placed from js/data/valley.js */
.glow { position: absolute; border-radius: 50%; background: radial-gradient(closest-side, rgba(255, 190, 92, 0.44), rgba(255, 170, 70, 0.16) 55%, transparent); mix-blend-mode: screen; pointer-events: none; }

/* the power line: poles painted for this valley, and wire drawn by code (js/poles.js) */
.poles { position: absolute; inset: 0; pointer-events: none; }
.poles svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.poles img.pole { opacity: 1; position: absolute; inset: auto; object-fit: fill; filter: var(--pole-tone); }
.poles .foot { position: absolute; aspect-ratio: 3 / 1; transform: translate(-50%, -50%); background: radial-gradient(closest-side, rgba(7, 9, 18, 0.55), transparent); }
/* --wire is the wire's thickness in the drawing's own units; it grows and shrinks with the painting */
.poles path { fill: none; stroke-linecap: round; stroke-width: var(--wire); }
.poles .shade { stroke: var(--ink); stroke-opacity: 0.38; stroke-width: calc(var(--wire) * 2.6); }
.poles stop.warm { stop-color: var(--amber); }
.poles stop.cool { stop-color: var(--haze); }
.poles .tail { stroke: var(--haze); stroke-opacity: 0.75; }
.poles .cable { stroke: var(--brass); stroke-opacity: 0.62; stroke-width: calc(var(--wire) * 1.5); }
.poles .shade.cable { stroke: var(--ink); stroke-opacity: 0.45; stroke-width: calc(var(--wire) * 3.4); }
/* current: a short bright stretch that runs the length of a line, and a spark at the loose end */
.poles .pulse { stroke: var(--filament); stroke-width: calc(var(--wire) * 2); stroke-dasharray: 4 96; stroke-dashoffset: 4; opacity: 0; }
.poles .arc { fill: var(--filament); opacity: 0; }

/* ---- the words ----
   Placed from the scene's own shape, like the paintings, so they never move when a toolbar does. */
.title { position: absolute; z-index: 6; left: 0; right: 0; top: 15.5cqh; padding: 0 var(--gutter); text-align: center; text-shadow: var(--text-shadow); }
/* each line is only as wide as its words, so nothing invisible reaches over the painting */
.title > * { width: fit-content; }
.title { display: flex; flex-direction: column; align-items: center; }
.wordmark { font-family: var(--display); font-weight: 500; font-size: var(--wordmark); line-height: 1; letter-spacing: var(--wordmark-track); margin-right: calc(-1 * var(--wordmark-track)); color: var(--filament); text-shadow: var(--glow), 0 2px 18px rgba(7, 9, 18, 0.6); white-space: nowrap; }
.tagline { margin-top: 0.62em; font-family: var(--display); font-style: italic; font-weight: 500; font-size: var(--tagline); line-height: 1.2; color: var(--tagline-ink); text-wrap: balance; }
.kind { white-space: nowrap; margin-top: 1.5em; font-family: var(--ui); font-weight: 600; font-size: var(--kind); letter-spacing: var(--kind-track); margin-right: calc(-1 * var(--kind-track)); text-transform: uppercase; color: var(--mist); }

/* 3:5 to 4:5 (upright tablets, the smallest phones): little sky is left above the Bell Tower, which
   stands on the right, so the words go top left, where the far hills leave the sky deepest. */
@container (min-aspect-ratio: 3/5) {
  .title { --avail: calc(70cqw - var(--gutter)); left: var(--gutter); right: auto; top: 3cqh; padding: 0; text-align: left; align-items: flex-start; max-width: var(--avail); }
  .wordmark { font-size: clamp(24px, calc(var(--avail) / 8.4), 72px); }
  .tagline { margin-top: 0.5em; font-size: clamp(15px, 3.1cqw, 26px); }
  .kind { margin-top: 1.1em; }
}
/* 4:5 to 11:10 (a window about as wide as it is tall): the sky carries on above the painting, and the
   words sit in it, centred. --room is the height of sky above the Bell Tower. */
@container (min-aspect-ratio: 4/5) {
  .title { --room: calc(100cqh - 72.9cqw); left: 0; right: 0; top: 4cqh; padding: 0 var(--gutter); text-align: center; align-items: center; max-width: none; }
  .wordmark { font-size: clamp(26px, min(10.5cqw, var(--room) * 0.34), 132px); }
  .tagline { margin-top: 0.62em; font-size: clamp(16px, min(2.6cqw, var(--room) * 0.11), 34px); }
  .kind { margin-top: 1.5em; }
}
/* 11:10 and wider: the valley fills the right, so the words sit in the open sky, top left. --avail is
   the width of sky between the gutter and the Bell Tower's hill. */
@container (min-aspect-ratio: 11/10) {
  .title { --stage: max(100cqw, 177.78cqh); --avail: calc((100cqw - var(--stage)) * 0.86 + var(--stage) * 0.57 - var(--gutter)); left: var(--gutter); right: auto; top: 8cqh; padding: 0; text-align: left; align-items: flex-start; max-width: var(--avail); }
  .wordmark { font-size: clamp(24px, calc(var(--avail) / 8.4), 120px); }
  .tagline { font-size: clamp(16px, calc(var(--avail) / 21), 34px); }
  .kind { margin-top: 1.5em; }
}
/* a phone on its side */
@container (min-aspect-ratio: 11/10) and (max-height: 480px) {
  .title { top: 7cqh; }
  .wordmark { font-size: clamp(24px, calc(var(--avail) / 9.8), 52px); }
  .tagline { margin-top: 0.45em; font-size: 16px; }
  .kind { margin-top: 0.9em; font-size: 11px; letter-spacing: 0.14em; margin-right: -0.14em; }
}

/* ---- the scroll cue: the cable itself, leaving the foot of the screen, with the current running down it ---- */
.cue { position: absolute; z-index: 6; left: var(--cue-x, 50%); bottom: 0; width: var(--cable); height: var(--cue-h); margin-left: calc(var(--cable) / -2); border-radius: var(--cable) var(--cable) 0 0; background: linear-gradient(to bottom, transparent, var(--brass) 70%); opacity: 0.8; }
/* once the valley has been lit (js/finale.js) the cable is gone from the lane: the cue and its run below go with it */
html.valley-lit .cue, html.valley-lit #powerhouse::before { opacity: 0; }
.cue::after { content: ""; position: absolute; left: 50%; top: 0; width: var(--spark); height: var(--spark); margin-left: calc(var(--spark) / -2); border-radius: 50%; background: var(--filament); box-shadow: 0 0 10px 3px rgba(255, 190, 92, 0.75); opacity: 0; }

/* running water, and the windows' gold shimmering on the stream */
/* (only its top and foot are softened: its sides are the painted fall's own) */
.scene canvas.fall { position: absolute; inset: auto; pointer-events: none; mix-blend-mode: screen; -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 14%, #000 78%, transparent 100%); mask-image: linear-gradient(to bottom, transparent 0, #000 14%, #000 78%, transparent 100%); }
.glint { position: absolute; border-radius: 50%; background: radial-gradient(closest-side, rgba(255, 196, 110, 0.3), rgba(255, 180, 90, 0.1) 60%, transparent); mix-blend-mode: screen; pointer-events: none; }

/* ---- life: everything below moves only when the visitor has not asked for reduced motion ---- */
/* The leaves in front sway in the wind: a slow lean, and a gust that takes them further about a third
   of the way through each round. One round is --wind long, the same as the round of js/wind.js. */
@keyframes sway {
  0%, 100% { transform: scale(1.1) rotate(0deg) skewX(0deg); }
  16% { transform: scale(1.1) rotate(0.6deg) skewX(-1.4deg); }
  30% { transform: scale(1.1) rotate(1.3deg) skewX(-3deg); }
  37% { transform: scale(1.105) rotate(2.2deg) skewX(-5deg) translateX(0.9%); }
  45% { transform: scale(1.1) rotate(0.8deg) skewX(-1.9deg); }
  52% { transform: scale(1.1) rotate(1.4deg) skewX(-2.9deg); }
  59% { transform: scale(1.102) rotate(1.9deg) skewX(-4deg) translateX(0.6%); }
  70% { transform: scale(1.1) rotate(0.2deg) skewX(-0.4deg); }
  84% { transform: scale(1.1) rotate(-0.7deg) skewX(1.3deg); }
}
}
@keyframes drift-a { from { transform: translateX(0); } to { transform: translateX(16%); } }
@keyframes drift-b { from { transform: translateX(0); } to { transform: translateX(-13%); } }
/* dynamo light: steady, with a dip or two as the wheel labours */
@keyframes breathe { 0%, 100% { opacity: 0.9; } 30% { opacity: 1; } 33% { opacity: 0.5; } 35.5% { opacity: 0.95; } 37.5% { opacity: 0.62; } 41% { opacity: 1; } 72% { opacity: 0.84; } 74% { opacity: 0.55; } 77% { opacity: 1; } }
/* One round of the current, --current long: along the strung wire, a spark at its loose end, down the
   cable on the lane, then down the cue and off the screen. */
@keyframes run-wire { 0% { stroke-dashoffset: 4; opacity: 0; } 2%, 20% { opacity: 0.95; } 23%, 100% { stroke-dashoffset: -96; opacity: 0; } }
@keyframes arc { 0%, 21% { opacity: 0; } 22% { opacity: 1; } 23.5% { opacity: 0.15; } 25% { opacity: 0.9; } 29%, 100% { opacity: 0; } }
@keyframes run-cable { 0%, 24% { stroke-dashoffset: 4; opacity: 0; } 26%, 44% { opacity: 0.9; } 48%, 100% { stroke-dashoffset: -96; opacity: 0; } }
@keyframes cue { 0%, 46% { transform: translateY(0); opacity: 0; } 49%, 60% { opacity: 1; } 64%, 100% { transform: translateY(var(--cue-h)); opacity: 0; } }
@keyframes rise-in { from { opacity: 0; transform: translateY(10px); } }
/* a filament warming up: a dull ember, a flicker or two, then the steady glow */
@keyframes warm {
  0% { opacity: 0; color: var(--ember-cold); text-shadow: none; }
  22% { opacity: 0.55; color: var(--ember); text-shadow: 0 0 10px rgba(240, 120, 40, 0.35); }
  34% { opacity: 0.28; color: var(--ember-dull); }
  52% { opacity: 0.92; color: var(--ember-hot); text-shadow: 0 0 22px rgba(255, 170, 70, 0.5); }
  62% { opacity: 0.7; color: var(--ember-bright); }
  100% { opacity: 1; }
}
@keyframes lamp-on { 0% { opacity: 0; } 28% { opacity: 0.75; } 44% { opacity: 0.2; } 68% { opacity: 1; } 80% { opacity: 0.7; } 100% { opacity: 1; } }
@keyframes spread { from { --reveal: 0.2; } to { --reveal: 1.3; } }
@media (prefers-reduced-motion: no-preference) {
  html.js .valley::before, html.js .valley .layer { transition: opacity var(--rise) ease-out; }
  html.js .valley::before, html.js .valley .layer.back { transition-delay: 350ms; }
  html.js .valley .layer.front { transition-delay: 700ms; }
  html.js .valley .layer.haze { transition-delay: 900ms; }
  html.js .valley .cue { transition: opacity var(--fade) ease-out 1900ms; }
  /* the light is a soft-edged circle about the windows (--lamp-x, --lamp-y, from js/hero.js) that grows past the far corner */
  .valley.opening .layer.village {
    -webkit-mask-image: radial-gradient(circle farthest-corner at var(--lamp-x) var(--lamp-y), #000 calc(var(--reveal) * 100% - 16%), transparent calc(var(--reveal) * 100%));
    mask-image: radial-gradient(circle farthest-corner at var(--lamp-x) var(--lamp-y), #000 calc(var(--reveal) * 100% - 16%), transparent calc(var(--reveal) * 100%));
    animation: lamp-on var(--lamp-on) linear both, spread var(--spread) cubic-bezier(0.3, 0.45, 0.3, 1) var(--lamp-on) both;
    transition: none;
  }
  .valley .layer.front picture img { transform-origin: 50% 100%; animation: sway var(--wind) ease-in-out infinite; }
  .cloud-a { animation: drift-a var(--cloud-a) ease-in-out infinite alternate; }
  .cloud-b { animation: drift-b var(--cloud-b) ease-in-out infinite alternate; }
  .mist-a { animation: drift-a var(--mist-a) ease-in-out infinite alternate; }
  .mist-b { animation: drift-b var(--mist-b) ease-in-out infinite alternate; }
  .glow { animation: breathe var(--breathe) ease-in-out infinite; }
  .glint { animation: breathe calc(var(--breathe) * 0.63) ease-in-out infinite reverse; }
  .poles .pulse.on-wire { animation: run-wire var(--current) linear infinite; }
  .poles .arc { animation: arc var(--current) linear infinite; }
  .poles .pulse.on-cable { animation: run-cable var(--current) linear infinite; }
.cue::after { animation: cue var(--current) linear infinite; }
  .wordmark span { display: inline-block; animation: warm var(--warm-up) ease-out backwards; animation-delay: calc(var(--i) * var(--warm-step)); }
  .tagline { animation: rise-in 700ms ease-out 650ms backwards; }
  .kind { animation: rise-in 700ms ease-out 900ms backwards; }
}
