/* ==========================================================================
   Background layer — 11 pure-CSS effects + the canvas the particle ones use.
   The effect is chosen per organisation (Настройки → Брендинг) and lands on
   <html data-bg="..."> with --bg-strength (0..1) from the intensity slider.

   Rules of the house: fixed, pointer-events:none, sits under everything, never
   tints text (all ink is drawn above), and every animation is disabled when the
   visitor asks for reduced motion.
   ========================================================================== */

#bg-canvas,
.bg-layer {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
  contain: strict;
}
#bg-canvas { display: none; }
html[data-bg="meteors"] #bg-canvas,
html[data-bg="sparkles"] #bg-canvas,
html[data-bg="vortex"] #bg-canvas,
html[data-bg="shooting"] #bg-canvas,
html[data-bg="ripple"] #bg-canvas,
html[data-bg="fireflies"] #bg-canvas,
html[data-bg="snowfall"] #bg-canvas,
html[data-bg="starfield"] #bg-canvas,
html[data-bg="matrix"] #bg-canvas,
html[data-bg="constellation"] #bg-canvas,
html[data-bg="beams_collision"] #bg-canvas { display: block; }

/* the app must stack above the background layer */
.app { position: relative; z-index: 1; }

/* light theme needs a lighter hand: the same opacity would wash the page out */
html[data-theme="light"] .bg-layer { opacity: .55; }

/* ------------------------------------------------------------------ Grid -- */
html[data-bg="grid"] body::before {
  content: "";
  position: fixed; inset: -2px; z-index: 0; pointer-events: none;
  background-image:
    linear-gradient(to right, var(--border-soft) 1px, transparent 1px),
    linear-gradient(to bottom, var(--border-soft) 1px, transparent 1px);
  background-size: 56px 56px;
  opacity: calc(var(--bg-strength) * 0.55);
  mask-image: radial-gradient(circle at 50% 0%, #000 15%, transparent 78%);
  -webkit-mask-image: radial-gradient(circle at 50% 0%, #000 15%, transparent 78%);
}

/* ------------------------------------------------------------------ Dots -- */
html[data-bg="dots"] body::before {
  content: "";
  position: fixed; inset: 0; z-index: 0; pointer-events: none;
  background-image: radial-gradient(var(--border-strong) 1.1px, transparent 1.2px);
  background-size: 22px 22px;
  opacity: calc(var(--bg-strength) * 0.5);
  mask-image: radial-gradient(circle at 50% 30%, #000 20%, transparent 85%);
  -webkit-mask-image: radial-gradient(circle at 50% 30%, #000 20%, transparent 85%);
}

/* ------------------------------------------------------------- Spotlight -- */
html[data-bg="spotlight"] body::before {
  content: "";
  position: fixed; inset: 0; z-index: 0; pointer-events: none;
  background:
    radial-gradient(600px 420px at 50% -8%, var(--accent-soft), transparent 70%),
    conic-gradient(from 180deg at 50% -14%, transparent 0deg,
                   var(--accent-line) 40deg, transparent 80deg);
  opacity: calc(var(--bg-strength) * 0.9);
  filter: blur(2px);
  animation: bg-sway 14s ease-in-out infinite alternate;
}
@keyframes bg-sway {
  from { transform: translateX(-6%) }
  to   { transform: translateX(6%) }
}

/* --------------------------------------------------------------- Aurora -- */
.bg-aurora span {
  position: absolute; width: 75vw; height: 60vh; border-radius: 50%;
  filter: blur(70px); mix-blend-mode: screen;
  opacity: calc(var(--bg-strength) * 0.55);
}
.bg-aurora span:nth-child(1) {
  left: -15%; top: -20%;
  background: radial-gradient(circle, var(--accent), transparent 62%);
  animation: aurora-a 19s ease-in-out infinite alternate;
}
.bg-aurora span:nth-child(2) {
  right: -18%; top: -6%;
  background: radial-gradient(circle, var(--accent-2), transparent 62%);
  animation: aurora-b 23s ease-in-out infinite alternate;
}
.bg-aurora span:nth-child(3) {
  left: 12%; bottom: -32%;
  background: radial-gradient(circle, var(--info), transparent 66%);
  animation: aurora-c 27s ease-in-out infinite alternate;
}
@keyframes aurora-a {
  from { transform: translate3d(0,0,0) scale(1) rotate(0deg) }
  to   { transform: translate3d(16vw, 8vh, 0) scale(1.25) rotate(24deg) }
}
@keyframes aurora-b {
  from { transform: translate3d(0,0,0) scale(1.1) rotate(0deg) }
  to   { transform: translate3d(-14vw, 12vh, 0) scale(0.9) rotate(-18deg) }
}
@keyframes aurora-c {
  from { transform: translate3d(0,0,0) scale(0.95) }
  to   { transform: translate3d(10vw, -10vh, 0) scale(1.3) }
}

/* ------------------------------------------------------ Gradient blobs --- */
.bg-gradient span {
  position: absolute; width: 46vw; height: 46vw; min-width: 320px; min-height: 320px;
  border-radius: 50%; filter: blur(60px);
  opacity: calc(var(--bg-strength) * 0.5);
  animation: blob-move 22s ease-in-out infinite;
}
.bg-gradient span:nth-child(1) { left: -8%; top: 4%; background: var(--accent); }
.bg-gradient span:nth-child(2) { right: -10%; top: 22%; background: var(--accent-2); animation-delay: -7s; }
.bg-gradient span:nth-child(3) { left: 26%; bottom: -18%; background: var(--info); animation-delay: -14s; }
@keyframes blob-move {
  0%,100% { transform: translate3d(0,0,0) scale(1) }
  33%     { transform: translate3d(7vw,-5vh,0) scale(1.15) }
  66%     { transform: translate3d(-6vw,6vh,0) scale(.9) }
}

/* ------------------------------------------------------ Liquid gradient --- */
.bg-liquid span {
  position: absolute; width: 60vw; height: 60vw;
  border-radius: 42% 58% 63% 37% / 41% 44% 56% 59%;
  filter: blur(80px);
  opacity: calc(var(--bg-strength) * 0.42);
  mix-blend-mode: screen;
  animation: liquid-morph 26s linear infinite;
}
.bg-liquid span:nth-child(1) { left: -12%; top: -14%; background: var(--accent); }
.bg-liquid span:nth-child(2) { right: -20%; bottom: -22%; background: var(--accent-2); animation-delay: -9s; }
.bg-liquid span:nth-child(3) { left: 30%; top: 26%; background: var(--info); animation-delay: -18s; }
@keyframes liquid-morph {
  0%   { transform: rotate(0deg) scale(1) }
  50%  { transform: rotate(180deg) scale(1.18) }
  100% { transform: rotate(360deg) scale(1) }
}

/* ----------------------------------------------------------------- Boxes -- */
/* a grid of tiles that light up in sequence, like a slow sweep across the board.
   CSS custom properties are not animatable without @property, so each glowing
   tile is its own element with its own keyframes instead. */
.bg-boxes::before {
  content: "";
  position: absolute; inset: 0;
  background-image:
    linear-gradient(to right, var(--border-soft) 1px, transparent 1px),
    linear-gradient(to bottom, var(--border-soft) 1px, transparent 1px);
  background-size: 72px 72px;
  opacity: calc(var(--bg-strength) * 0.55);
}
.bg-boxes span {
  position: absolute; width: 72px; height: 72px;
  border: 1px solid var(--accent-line); border-radius: 4px;
  background: var(--accent-soft);
  opacity: 0;
  animation: box-glow 9s ease-in-out infinite;
}
.bg-boxes span:nth-child(1) { left: 144px;  top: 72px;   animation-delay: 0s }
.bg-boxes span:nth-child(2) { left: 432px;  top: 144px;  animation-delay: .6s }
.bg-boxes span:nth-child(3) { left: 720px;  top: 216px;  animation-delay: 1.2s }
.bg-boxes span:nth-child(4) { left: 288px;  top: 360px;  animation-delay: 1.8s }
.bg-boxes span:nth-child(5) { left: 576px;  top: 432px;  animation-delay: 2.4s }
.bg-boxes span:nth-child(6) { left: 864px;  top: 504px;  animation-delay: 3s }
.bg-boxes span:nth-child(7) { left: 1008px; top: 288px;  animation-delay: 3.6s }
.bg-boxes span:nth-child(8) { left: 216px;  top: 576px;  animation-delay: 4.2s }
@keyframes box-glow {
  0%, 82%, 100% { opacity: 0 }
  6%, 18%       { opacity: calc(var(--bg-strength) * 0.85) }
  30%           { opacity: 0 }
}

/* ----------------------------------------------------------------- Beams -- */
.bg-beams svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.bg-beams path {
  fill: none; stroke: var(--accent); stroke-width: 1.2;
  stroke-dasharray: 220 1400;
  filter: drop-shadow(0 0 6px var(--accent));
  opacity: calc(var(--bg-strength) * 0.75);
  animation: beam-run 7s linear infinite;
}
.bg-beams path:nth-child(2) { stroke: var(--accent-2); animation-duration: 9s; animation-delay: -3s; }
.bg-beams path:nth-child(3) { stroke: var(--info); animation-duration: 11s; animation-delay: -6s; }
@keyframes beam-run {
  from { stroke-dashoffset: 1620 }
  to   { stroke-dashoffset: 0 }
}

/* ----------------------------------------------------------------- Lines -- */
.bg-lines svg { position: absolute; bottom: 0; left: 0; width: 220%; height: 42%; }
.bg-lines path {
  fill: none; stroke: var(--accent); stroke-width: 1.4;
  opacity: calc(var(--bg-strength) * 0.5);
  animation: lines-slide 24s linear infinite;
}
.bg-lines path:nth-child(2) { stroke: var(--accent-2); animation-duration: 31s; animation-direction: reverse; }
.bg-lines path:nth-child(3) { stroke: var(--info); animation-duration: 39s; }
@keyframes lines-slide {
  from { transform: translateX(0) }
  to   { transform: translateX(-45%) }
}

/* ------------------------------------------------------------------ Wavy -- */
.bg-wavy svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.bg-wavy path {
  fill: none; stroke: var(--accent-2); stroke-width: 1;
  opacity: calc(var(--bg-strength) * 0.45);
  animation: wavy-shift 16s ease-in-out infinite alternate;
}
.bg-wavy path:nth-child(2) { stroke: var(--info); animation-duration: 21s; animation-direction: alternate-reverse; }
@keyframes wavy-shift {
  from { transform: translateY(-8px) scaleY(1) }
  to   { transform: translateY(8px) scaleY(1.12) }
}

/* --------------------------------------------------------- reduced motion -- */
@media (prefers-reduced-motion: reduce) {
  .bg-aurora span, .bg-gradient span, .bg-liquid span,
  .bg-beams path, .bg-lines path, .bg-wavy path,
  html[data-bg="grid"] body::before,
  html[data-bg="dots"] body::before,
  html[data-bg="spotlight"] body::before,
  html[data-bg="boxes"] body::before {
    animation: none !important;
  }
}
