/*
 * © 2026 Code Precision. All rights reserved.
 * Copying, redistribution, modification, extraction, or reuse of this Code Precision-authored file,
 * in whole or substantial part, is prohibited without prior express written permission from Code Precision.
 * No permission is granted for AI/ML training, fine-tuning, dataset creation, text/data mining,
 * embedding/index construction for generative systems, synthetic-data generation, or image-model training.
 * NoAI · NoAIImage · NoMLTraining
 */
/*
 * CSS/Canvas portal fallback renderer.
 *
 * Geometry is measured from the same .portal-surface used by portal-webgl.js.
 * Shared runtime state drives both renderers. Canvas reproduces the halo;
 * CSS reproduces the portal material; a hand-rolled Canvas 2D emitter draws
 * the shared S-pose dust.
 */

.portal-css-replica {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  overflow: visible;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  z-index: 0;
}

.portal-css-replica::before {
  content: none;
}

.portal-css-replica__poster {
  position: absolute;
  left: 50%;
  top: 50%;
  width: var(--cp-css-portal-surface-size, 0px);
  height: var(--cp-css-portal-surface-size, 0px);
  z-index: 1;
  display: block;
  object-fit: fill;
  border-radius: 50%;
  clip-path: circle(50% at 50% 50%);
  -webkit-clip-path: circle(50% at 50% 50%);
  transform: translate(-50%, -50%);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}


.portal-css-replica__glow,
.portal-css-replica__playlist-video,
.portal-css-replica__fresnel,
.portal-css-replica__sweep,
.portal-css-replica__noise,
.portal-css-replica__particles,
.portal-css-replica__ring {
  position: absolute;
  display: block;
  pointer-events: none;
}

/* The shader samples the complete source texture across a square UV field.
   object-fit: fill deliberately preserves that mapping instead of applying a
   browser cover crop. The measured diameter is the exact .portal-surface box. */
.portal-css-replica__playlist-video {
  left: 50%;
  top: 50%;
  width: var(--cp-css-portal-surface-size, 0px);
  height: var(--cp-css-portal-surface-size, 0px);
  z-index: 2;
  display: block;
  object-fit: fill;
  border-radius: 50%;
  clip-path: circle(50% at 50% 50%);
  -webkit-clip-path: circle(50% at 50% 50%);
  transform: translate(-50%, -50%);
  background: transparent;
  opacity: 0;
  visibility: hidden;
  will-change: opacity;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.portal-css-replica__playlist-video.is-css-replica-front {
  opacity: 1;
  visibility: visible;
}

.portal-css-replica__fresnel,
.portal-css-replica__sweep,
.portal-css-replica__noise {
  left: 50%;
  top: 50%;
  width: var(--cp-css-portal-surface-size, 0px);
  height: var(--cp-css-portal-surface-size, 0px);
  border-radius: 50%;
  clip-path: circle(50% at 50% 50%);
  -webkit-clip-path: circle(50% at 50% 50%);
  transform: translate(-50%, -50%);
}

/* GLSL: haze = vec3(0.12 * pow(radiusRatio, 1.8)). */
.portal-css-replica__fresnel {
  z-index: 3;
  background-image: var(--cp-css-fresnel-gradient, none);
  background-repeat: no-repeat;
  mix-blend-mode: plus-lighter;
}

/* GLSL: pow(smoothed sin(angle * 2), 5) * fresnel * 0.28. */
.portal-css-replica__sweep {
  z-index: 4;
  background-image: var(--cp-css-sweep-angular, none);
  background-repeat: no-repeat;
  mix-blend-mode: plus-lighter;
  -webkit-mask-image: var(--cp-css-sweep-radial, none);
  mask-image: var(--cp-css-sweep-radial, none);
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-mode: alpha;
  mask-mode: alpha;
}

/* The shader adds (noise - 0.5) * 0.05. Runtime-generated 64px maps split
   that signed term into additive and subtractive components without using the
   contrast-changing overlay/soft-light approximations. */
.portal-css-replica__noise {
  background-repeat: repeat;
  background-size: var(--cp-css-noise-tile-size, 64px) var(--cp-css-noise-tile-size, 64px);
  image-rendering: pixelated;
}

.portal-css-replica__noise--negative {
  z-index: 5;
  background-image: var(--cp-css-noise-negative, none);
  mix-blend-mode: difference;
}

.portal-css-replica__noise--positive {
  z-index: 6;
  background-image: var(--cp-css-noise-positive, none);
  mix-blend-mode: plus-lighter;
}

/* Match the WebGL time terms without introducing another requestAnimationFrame
   loop. The two-lobed sweep turns at half its shader phase rate:
   (uTime * 0.6) / 2 = 0.3 rad/s. Blue noise uses uTime * 0.05. */
@keyframes cp-css-portal-sweep-spin {
  from { transform: translate(-50%, -50%) rotate(0deg); }
  to { transform: translate(-50%, -50%) rotate(360deg); }
}

@keyframes cp-css-portal-noise-spin {
  from { transform: translate(-50%, -50%) rotate(0deg); }
  to { transform: translate(-50%, -50%) rotate(360deg); }
}

body.portal-css-replica-mode .portal-css-replica__sweep {
  animation: cp-css-portal-sweep-spin var(--cp-portal-sweep-duration, 20.943951s) linear infinite;
  will-change: transform;
}

body.portal-css-replica-mode .portal-css-replica__noise {
  animation: cp-css-portal-noise-spin var(--cp-portal-noise-duration, 125.663706s) linear infinite;
  will-change: transform;
}

/* CSS fallback + reduced motion is intentionally image-only. The portal keeps
   its static material/ring treatment, but the only changing visual content is
   the active case poster selected by portal navigation. No fallback animation
   or video playback is exposed in this combined degradation profile. */
@media (prefers-reduced-motion: reduce) {
  body.portal-css-replica-mode .portal-css-replica__sweep,
  body.portal-css-replica-mode .portal-css-replica__noise {
    animation-play-state: paused !important;
  }

  body.portal-css-replica-mode .portal-css-replica[data-cp-has-poster="true"] .portal-css-replica__poster {
    opacity: 1 !important;
    visibility: visible !important;
  }

  body.portal-css-replica-mode .portal-css-replica__playlist-video {
    opacity: 0 !important;
    visibility: hidden !important;
  }

  body.portal-css-replica-mode .portal-css-replica__particles {
    opacity: 0 !important;
    visibility: hidden !important;
  }

  /* The same control remains available for WebGL reduced motion, but CSS
     fallback intentionally offers poster navigation only. */
  body.portal-css-replica-mode .portal-rm-playback-toggle {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
  }
}

/* S-mode is a composed still, not a pause-at-whatever-frame-was-live state.
   Match portal-webgl.js's SCREENSHOT_PORTAL_TIME (5.236s): the two-lobed
   specular is held at 90deg and the slower blue-noise rotation at 15deg. */
body.screenshot-pose-mode .portal-css-replica__sweep {
  animation: none !important;
  transform: translate(-50%, -50%) rotate(90deg) !important;
  will-change: auto;
}

body.screenshot-pose-mode .portal-css-replica__noise {
  animation: none !important;
  transform: translate(-50%, -50%) rotate(15deg) !important;
  will-change: auto;
}

/* These two infinite animations drive a 6-layer mix-blend-mode composite
   stack (fresnel + sweep + noise x2 + particles x2) that the browser must
   recompute together every animated frame — not just the rotating element.
   Nothing about that stack is gated by scroll position on its own, so pause
   it explicitly once the portal leaves the viewport (JS toggles this class
   via the same IntersectionObserver already used to pause the particle/glow
   canvases). animation-play-state: paused freezes at the current rotation
   and resumes from exactly there — no visual jump on re-entry. */
body.portal-css-replica-mode.portal-css-replica-offscreen .portal-css-replica__sweep,
body.portal-css-replica-mode.portal-css-replica-offscreen .portal-css-replica__noise {
  animation-play-state: paused;
}

/* The shared popup motion controller uses this state for Case/Explore overlays.
   Freeze the declarative fallback rotations at their current frame, matching
   WebGL runtime.suspend() while the popup is active. */
body.portal-css-replica-mode.portal-css-replica-suspended .portal-css-replica__sweep,
body.portal-css-replica-mode.portal-css-replica-suspended .portal-css-replica__noise {
  animation-play-state: paused !important;
}

/* The field is larger than the portal by exactly the shader envelope:
   uGlowRadius = 1.16R, followed by the fixed 20px outer feather. */
.portal-css-replica__glow {
  left: 50%;
  top: 50%;
  width: var(--cp-css-glow-field-size, 0px);
  height: var(--cp-css-glow-field-size, 0px);
  transform: translate(-50%, -50%);
  border-radius: 50%;
  z-index: 0;
  background: transparent;
  image-rendering: auto;
}

.portal-css-replica__particles {
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  overflow: visible;
  opacity: 0;
  visibility: hidden;
  background: transparent;
}

/* WebGL CustomBlending uses SrcAlpha for both colour and alpha. On a
   transparent framebuffer that becomes two independent screen-space terms:
   black alpha² coverage, followed by the additive shader colour. */
.portal-css-replica__particles--coverage {
  z-index: 8;
}

.portal-css-replica__particles--additive {
  z-index: 9;
  mix-blend-mode: plus-lighter;
}

.portal-css-replica__ring {
  inset: 0;
  border-radius: 50%;
  z-index: 7;
  background-image: var(--cp-css-ring-gradient, none);
  background-repeat: no-repeat;
  -webkit-mask-image: var(--cp-css-ring-mask, none);
  mask-image: var(--cp-css-ring-mask, none);
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-mode: alpha;
  mask-mode: alpha;
}

body.portal-css-replica-mode #portal-canvas {
  opacity: 0 !important;
  visibility: hidden !important;
}

body.portal-css-replica-mode .portal-css-replica {
  opacity: 1;
  visibility: visible;
}

body.portal-css-replica-mode .portal-css-replica__particles {
  opacity: 1;
  visibility: visible;
}

body.portal-css-replica-mode .portal-surface,
body.portal-css-replica-mode .portal-surface video {
  background: transparent !important;
}

body.portal-css-replica-mode #portal-loader,
body.portal-css-replica-mode .portal-mobile-caption {
  opacity: 0 !important;
  visibility: hidden !important;
  pointer-events: none !important;
}

/* WebGL reduced motion owns the optional video control. CSS fallback hides it
   in the reduced-motion media rule above and otherwise leaves it untouched. */

.portal-renderer-test-toggle[hidden] {
  display: none !important;
}

.portal-renderer-test-toggle {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  z-index: 120;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-height: 34px;
  padding: 7px 12px;
  transform: translateX(-50%);
  border: 1px solid rgba(255, 255, 255, 0.42);
  border-radius: 999px;
  background: rgba(10, 10, 32, 0.84);
  color: #fff;
  box-shadow: 0 5px 18px rgba(0, 0, 36, 0.26);
  font: 600 10px/1 var(--font-mono, monospace);
  letter-spacing: 0.08em;
  cursor: pointer;
}

.portal-renderer-test-toggle__prefix {
  opacity: 0.58;
}

.portal-renderer-test-toggle__value {
  min-width: 72px;
  color: var(--lightblue, #55f0ff);
  text-align: left;
}

.portal-renderer-test-toggle:disabled {
  cursor: default;
  opacity: 0.72;
}

.portal-renderer-test-toggle:focus-visible {
  outline: 2px solid var(--lightblue, #55f0ff);
  outline-offset: 3px;
}

@media (hover: hover) and (pointer: fine) {
  .portal-renderer-test-toggle:hover {
    border-color: rgba(85, 240, 255, 0.8);
  }
}

@media only screen and (orientation: portrait),
       only screen and (max-width: 1023.98px),
       only screen and (max-height: 599.98px) {
  .portal-renderer-test-toggle {
    bottom: max(12px, env(safe-area-inset-bottom));
  }
}
