/* Waidrn 3.0 — experimental per-realm background layer (V3-06d, #575).
 *
 * WHAT   One very subtle, fixed, full-viewport decorative art layer behind the
 *        v3 shell. Two stacked pseudo-elements crossfade (~600 ms) when the realm
 *        theme changes; the art drifts almost imperceptibly. No markup: the layer
 *        is generated from CSS on <body>, so there is no DOM node to place.
 * SCOPE  Only when <html data-v3-bg="on">. Legacy v2 pages never set it, so this
 *        file is completely inert there (no rule matches: no paint, no request).
 * THEME  <html data-realm-theme="0".."7">; no attribute => theme 0, so a no-JS
 *        page always paints the static default theme. The JS additionally derives
 *        the theme from <html data-realm-key> (see docs/V3-ART-BACKGROUND.md).
 * JS     v3-background.js resolves the theme, pins it, then activates the layer
 *        with data-v3-bg-ready="a|b". Before that attribute exists no theme SVG is
 *        referenced, so gated or opted-out pages never fetch one. Without JS the
 *        `scripting: none` block paints the static theme instead.
 * ASSET  One original static/art/backgrounds/bg-N.svg per theme, referenced only
 *        through the selected theme token; the other seven are never requested.
 * GATES  prefers-reduced-motion: no animation, instant theme change.
 *        prefers-contrast: more / forced-colors: layer removed.
 *        Save-Data, hardwareConcurrency <= 2 and the public opt-outs are enforced
 *        by v3-background.js, which sets data-v3-bg="off" (no rule matches).
 * ALPHA  The art is authored near-black (base #05070c, internal alphas <= .18), so
 *        a literal layer alpha <= .10 is imperceptible (measured: max delta RGB
 *        3/255 versus no layer). Dark themes therefore render the art at 0.60,
 *        which still reads as a very subtle, distant scene and keeps body text
 *        far above AA (measured contrast 15:1+). --v3bg-op is the single knob if
 *        a stricter ceiling is required. Light themes disable the layer: the art
 *        is authored for the dark shell (see section 4).
 * COST   13242 B raw / 4163 B gzip (mostly this contract comment); two animated
 *        pseudo-layers; no reflow, no layout shift (measured CLS 0), no per-frame
 *        paint (compositor transform/opacity only).
 * DOC    docs/V3-ART-BACKGROUND.md. Contract owner: v3 shell (#543/#544).
 */

/* -----------------------------------------------------------------------------
   1. Theme tokens (on <html>, inherited by the <body> pseudo-elements).
      Each token is the complete background-image stack for one theme: a faint
      accent gradient (the SVG's own halo colour) over the theme artwork.
   -------------------------------------------------------------------------- */
html[data-v3-bg="on"] {
  --v3bg-op: 0.6;    /* dark-theme art alpha; see ALPHA in the header */
  --v3bg-dur: 140s;  /* one full drift cycle, alternating */
  --v3bg-t-bg: radial-gradient(90% 70% at 50% 12%, rgba(143, 201, 191, 0.14), transparent 62%), url("/static/art/backgrounds/bg-0.svg");
}

html[data-v3-bg="on"][data-realm-theme="1"] { --v3bg-t-bg: radial-gradient(90% 70% at 50% 12%, rgba(224, 141, 97, 0.14), transparent 62%), url("/static/art/backgrounds/bg-1.svg"); }
html[data-v3-bg="on"][data-realm-theme="2"] { --v3bg-t-bg: radial-gradient(90% 70% at 50% 12%, rgba(169, 184, 255, 0.14), transparent 62%), url("/static/art/backgrounds/bg-2.svg"); }
html[data-v3-bg="on"][data-realm-theme="3"] { --v3bg-t-bg: radial-gradient(90% 70% at 50% 12%, rgba(134, 201, 169, 0.14), transparent 62%), url("/static/art/backgrounds/bg-3.svg"); }
html[data-v3-bg="on"][data-realm-theme="4"] { --v3bg-t-bg: radial-gradient(90% 70% at 50% 12%, rgba(159, 214, 184, 0.14), transparent 62%), url("/static/art/backgrounds/bg-4.svg"); }
html[data-v3-bg="on"][data-realm-theme="5"] { --v3bg-t-bg: radial-gradient(90% 70% at 50% 12%, rgba(159, 180, 200, 0.14), transparent 62%), url("/static/art/backgrounds/bg-5.svg"); }
html[data-v3-bg="on"][data-realm-theme="6"] { --v3bg-t-bg: radial-gradient(90% 70% at 50% 12%, rgba(232, 201, 135, 0.14), transparent 62%), url("/static/art/backgrounds/bg-6.svg"); }
html[data-v3-bg="on"][data-realm-theme="7"] { --v3bg-t-bg: radial-gradient(90% 70% at 50% 12%, rgba(196, 154, 85, 0.14), transparent 62%), url("/static/art/backgrounds/bg-7.svg"); }

/* -----------------------------------------------------------------------------
   2. Shell contract: the v3 shell paints its page base on <html> and keeps
      <body> transparent. A z-index:-1 fixed layer paints before in-flow block
      backgrounds, so an opaque <body> would hide it — the same contract the v2
      scene layer uses (static/scenes.css). Do not put transform, filter or
      contain on <body>: they would turn it into the fixed layer's containing
      block and break the viewport framing.
   -------------------------------------------------------------------------- */
html[data-v3-bg="on"] body {
  background-color: transparent;
}

/* -----------------------------------------------------------------------------
   3. The layer: two fixed, full-viewport, non-interactive pseudo-elements.
   -------------------------------------------------------------------------- */
html[data-v3-bg="on"] body::before,
html[data-v3-bg="on"] body::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background-color: transparent;
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  opacity: 0;
  /* 1.06 zoom is the overhang the drift needs so no edge is ever exposed. */
  transform: scale(1.06);
  transition: opacity 600ms ease;
  animation: v3bg-drift var(--v3bg-dur) ease-in-out infinite alternate;
}

/* No-JS path: a static default theme, straight from CSS. The `scripting: none`
   media query guarantees this block is dead while JS is enabled, so a JS-capable
   browser does not fetch a theme SVG before v3-background.js has run its gates. */
@media (scripting: none) {
  html[data-v3-bg="on"] body::before {
    background-image: var(--v3bg-t-bg);
    opacity: var(--v3bg-op);
  }
}

/* JS path: v3-background.js pins the resolved theme artwork on one layer, then
   sets data-v3-bg-ready="a|b". That attribute is the activation signal ("theme
   resolved") and names the visible layer; the other layer stays hidden and is
   filled with the incoming theme before each crossfade. */
html[data-v3-bg="on"][data-v3-bg-ready] body::before {
  background-image: var(--v3bg-img-a, var(--v3bg-t-bg));
}

html[data-v3-bg="on"][data-v3-bg-ready] body::after {
  background-image: var(--v3bg-img-b);
}

html[data-v3-bg="on"][data-v3-bg-ready="a"] body::before {
  opacity: var(--v3bg-op);
}

html[data-v3-bg="on"][data-v3-bg-ready="b"] body::after {
  opacity: var(--v3bg-op);
}

/* Very slow, compositor-only drift: transform is the only animated property,
   so nothing triggers layout or repaint after the first frame. */
@keyframes v3bg-drift {
  0%   { transform: scale(1.06) translate3d(-1.2%, -0.8%, 0); }
  100% { transform: scale(1.06) translate3d(1.2%, 0.8%, 0); }
}

/* -----------------------------------------------------------------------------
   4. Light theme: the generated art is authored for the dark shell (near-black
      sky, faint light strokes), so the layer is disabled instead of being forced
      over a light page: no request, no paint, no readability cost. If light-theme
      art ships later, delete this block and give [data-theme="light"] its own
      --v3bg-op/hue treatment. The shell sets data-theme (dark is the default).
   -------------------------------------------------------------------------- */
html[data-v3-bg="on"][data-theme="light"] body::before,
html[data-v3-bg="on"][data-theme="light"] body::after {
  display: none !important;
}

/* -----------------------------------------------------------------------------
   5. Hard gates. These override every state above.
   -------------------------------------------------------------------------- */

/* Reduced motion: keep the art, freeze it, switch themes instantly. */
@media (prefers-reduced-motion: reduce) {
  html[data-v3-bg="on"] body::before,
  html[data-v3-bg="on"] body::after {
    animation: none !important;
    transition: none !important;
  }
}

/* Tab hidden: pause the drift where it is (document.hidden -> data attribute
   set by v3-background.js). No teardown, so returning to the tab is seamless. */
html[data-v3-bg="on"][data-v3-bg-paused] body::before,
html[data-v3-bg="on"][data-v3-bg-paused] body::after {
  animation-play-state: paused;
}

/* High-contrast / forced-colors: decorative layer removed entirely. */
@media (prefers-contrast: more), (forced-colors: active) {
  html[data-v3-bg="on"] body::before,
  html[data-v3-bg="on"] body::after {
    display: none !important;
  }
}

/* Print: background art has no place on paper (and would eat toner). */
@media print {
  html[data-v3-bg="on"] body::before,
  html[data-v3-bg="on"] body::after {
    display: none !important;
  }
}

/* Public CSS-only opt-out: html[data-v3-bg="off"] (or no attribute at all)
   matches no rule in this file, so the layer costs nothing. The JS sets "off"
   for Save-Data / low-core / contrast gates and for the ?v3bg=off opt-out. */

/* -----------------------------------------------------------------------------
   6. The sky moves (the operator, 2026-11-18: "take from it animated background
      on current background"). Three ribbons of light drift and the stars
      twinkle: the motion of "Aurora Fold" in the palette study of pull request
      #662, set between the scene (.v3-page-art) and its veil (.v3-page-mask),
      so the veil dims it as it dims the tree. Drawn by `waidrn_ui::chrome::sky`.

      WHERE  Wherever section 1's layer is on (<html data-v3-bg="on">: every
             page but a wish's own, which has a scene that follows the pointer)
             and the profile is dark. The light profile's sky is not drawn yet.
      COST   Three ribbons and four groups of stars, each one layer that the
             compositor moves or fades; nothing is laid out or painted again
             after the first frame. The ribbons' softness is in the drawing (an
             SVG filter, rastered once), not a CSS filter worked every frame. A
             phone gets two ribbons.
      GATES  As section 5: with reduced motion asked for the sky stands still,
             as its first frame; with more contrast asked for it is not there.
      KNOB   --v3-sky-op scales all of it; 0 turns it off.
   -------------------------------------------------------------------------- */
.v3-sky {
  display: none;
}

/* What the ribbons are drawn with. It takes no room, and it is not `display: none`:
   a browser does not draw with a gradient or a filter of a drawing that is not shown. */
.v3-sky-defs {
  position: absolute;
  width: 0;
  height: 0;
}

html[data-shell="v3"][data-v3-bg="on"]:not([data-theme="light"]) .v3-sky {
  --v3-sky-op: 1;
  display: block;
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  pointer-events: none;
  opacity: var(--v3-sky-op);
}

/* The drawing is 760 high for a line that lies in its middle 320: the room above and
   below is where the line's soft edge fades, so the box starts above the page. */
.v3-sky-rib {
  position: absolute;
  top: -14vh;
  left: -6%;
  width: 112%;
  height: 76vh;
  min-height: 520px;
  will-change: transform;
}

.v3-sky-rib svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.v3-sky-rib--a { opacity: .9; }
.v3-sky-rib--b { opacity: .8; }
.v3-sky-rib--c { opacity: .5; }

.v3-sky-stars {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 62vh;
  min-height: 360px;
  opacity: .45;
}

@media (prefers-reduced-motion: no-preference) {
  .v3-sky-rib--a { animation: v3-sky-drift-a 46s ease-in-out infinite alternate; }
  .v3-sky-rib--b { animation: v3-sky-drift-b 64s ease-in-out infinite alternate; }
  .v3-sky-rib--c { animation: v3-sky-drift-c 52s ease-in-out infinite alternate; }
  .v3-sky-stars--1 { animation: v3-sky-twinkle 6s ease-in-out -0.6s infinite; }
  .v3-sky-stars--2 { animation: v3-sky-twinkle 5s ease-in-out -2.4s infinite; }
  .v3-sky-stars--3 { animation: v3-sky-twinkle 7s ease-in-out -1.8s infinite; }
  .v3-sky-stars--4 { animation: v3-sky-twinkle 8s ease-in-out -3.6s infinite; }
}

@keyframes v3-sky-drift-a { from { transform: translate3d(-3%, 0, 0); } to { transform: translate3d(3%, -2%, 0); } }
@keyframes v3-sky-drift-b { from { transform: translate3d(2%, 1%, 0); } to { transform: translate3d(-2%, -1%, 0); } }
@keyframes v3-sky-drift-c { from { transform: translate3d(1%, -1%, 0); } to { transform: translate3d(-1%, 2%, 0); } }
@keyframes v3-sky-twinkle { 0%, 100% { opacity: .18; } 50% { opacity: .65; } }

@media (max-width: 720px) {
  .v3-sky-rib--c { display: none; }
}

/* Tab hidden: the sky waits where it is, as the layer of section 3 does. */
html[data-v3-bg-paused] .v3-sky-rib,
html[data-v3-bg-paused] .v3-sky-stars {
  animation-play-state: paused;
}

@media (prefers-contrast: more), (forced-colors: active) {
  html[data-shell="v3"][data-v3-bg="on"]:not([data-theme="light"]) .v3-sky { display: none; }
}

@media print {
  html[data-shell="v3"][data-v3-bg="on"]:not([data-theme="light"]) .v3-sky { display: none; }
}
