/*
 * fx.css — platform visual-effects layer.
 *
 * Three jobs, all keyed off variables that visualRuntime.js publishes on <html>:
 *
 *   glassmorphism + animatable gradients
 *   compositor-only motion primitives
 *   adaptive degradation (data-gfx="high|medium|low", --gfx-blur, --gfx-particles)
 *
 * Effects here are OPT-IN via utility classes. Nothing below restyles an
 * existing element, so adding this stylesheet cannot regress a game's look —
 * it only makes the primitives available.
 */

/* ===== Animatable gradient angle ========================================
 * A plain `background: conic-gradient(from 0deg, …)` cannot be transitioned or
 * animated, because the browser has no idea the token inside the function is an
 * angle. Registering it with @property gives it a type, and it becomes
 * interpolable like any other animatable value. This is the whole trick behind
 * a rotating conic border that costs nothing per frame in JS.
 */
@property --fx-angle {
    syntax: '<angle>';
    initial-value: 0deg;
    inherits: false;
}

@property --fx-glow {
    syntax: '<number>';
    initial-value: 0;
    inherits: false;
}

@property --holo-angle {
    syntax: '<angle>';
    initial-value: 0deg;
    inherits: false;
}

:root {
    /* Written by visualRuntime.js while a page asks for them (cues and pointer
       speed drive them); declared here so stylesheets have sane values at rest. */
    --audio-bass: 0;
    --audio-mid: 0;
    --audio-treble: 0;
    --audio-peak: 0;
    /* The beat, 0..1: a cue lands on the low band and pointer speed adds a little.
       The small offset keeps a slow drift of the mouse from lighting anything; at rest
       the band is 0 and so is this. */
    --audio-pulse: clamp(0, calc(var(--audio-bass) * 1.3 - 0.06), 1);

    /* Tension & biometric heartbeat variables */
    --fx-tension: 0;
    --fx-pulse-rate: 1;

    /* Overwritten by visualRuntime.js's tier logic. */
    --gfx-blur: 14px;
    --gfx-particles: 1;
    --gfx-shadows: 1;

    --fx-glass-bg: rgba(255, 255, 255, 0.06);
    --fx-glass-stroke: rgba(255, 255, 255, 0.14);
    --fx-glass-highlight: rgba(255, 255, 255, 0.22);
}

@keyframes fx-rotate-angle {
    to { --fx-angle: 360deg; }}

/* ===== Advanced surface primitives ==================================
 * The four below are what separate "translucent panel" from "pane of glass".
 * All are opt-in utilities; the applied layer that puts them on real chrome is
 * glass.css, kept separate so this file stays free of side effects.
 */

/* The sheen sweep glass.css runs across a game card on hover. Animated with a
 * transform on a pseudo-element, not `background-position`: a moving gradient
 * position repaints the element every frame, a transform stays on the compositor.
 * The live tilt is the `[data-fx-tilt]` rule further down. */
@keyframes fx-sheen-sweep {
    from { transform: translateX(0) skewX(-18deg); }
    to   { transform: translateX(420%) skewX(-18deg); }}

/* 3D Holographic Foil Card — thin-film Bragg diffraction sheen */
.fx-holo-card {
    position: relative;
    overflow: hidden;
    transform-style: preserve-3d;
    transition: transform 0.16s ease-out, box-shadow 0.16s ease-out;
}

.fx-holo-card::after {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 5;
    border-radius: inherit;
    opacity: calc(var(--holo-glare, 0) * 0.72);
    background:
        radial-gradient(circle at var(--holo-x, 50%) var(--holo-y, 50%),
            rgba(255, 255, 255, 0.40) 0%,
            transparent 50%),
        conic-gradient(from var(--holo-angle, 0deg) at var(--holo-x, 50%) var(--holo-y, 50%),
            rgba(255, 0, 128, 0.22),
            rgba(0, 229, 255, 0.32),
            rgba(250, 204, 21, 0.28),
            rgba(52, 211, 153, 0.32),
            rgba(168, 85, 247, 0.28),
            rgba(255, 0, 128, 0.22));
    mix-blend-mode: color-dodge;
    transition: opacity 0.2s ease-out;
}

:root[data-gfx='low'] .fx-holo-card::after { display: none; }
@media (prefers-reduced-motion: reduce) { .fx-holo-card::after { display: none; } }

@keyframes fx-heartbeat {
    from { opacity: 0.85; transform: scale(1); }
    to   { opacity: 1.0; transform: scale(calc(1 + var(--fx-tension, 0) * 0.02)); }
}

/* ===== Compositor-only motion ===============================================
 * Animates ONLY transform. In Blazor WASM the main thread runs the .NET runtime,
 * so anything that needs layout or paint per frame competes with the app itself.
 * `.fx-pop` is the toast entry (impactBus.js adds the class).
 */
.fx-pop {
    animation: fx-pop 0.26s cubic-bezier(0.34, 1.56, 0.64, 1) both;
    will-change: transform;
}
@keyframes fx-pop {
    0%   { transform: scale(0.86); }
    60%  { transform: scale(1.04); }
    100% { transform: scale(1); }}

/* ===== Impact / game feel ===============================================
 * impactBus.js publishes --po-punch and --po-shake on <html> and drives the
 * flash layer's opacity directly. Everything here is the *declarative* half:
 * what those numbers look like.
 */

/* Full-screen tint spike. Injected once by impactBus.js and left in the DOM —
 * it is a single always-composited layer at opacity 0, which is cheaper than
 * creating and destroying an element per hit (each creation would force a
 * style/layout pass at exactly the moment the frame budget is tightest).
 *
 * `mix-blend-mode: screen` rather than a flat white overlay: screen lifts the
 * existing image toward white instead of washing it out, so the playfield stays
 * readable through the flash.
 */
.po-impact-flash {
    position: fixed;
    inset: 0;
    z-index: var(--z-fx-flash, 9990);
    pointer-events: none;
    opacity: 0;
    background: radial-gradient(ellipse at center,
        color-mix(in srgb, var(--fx-accent, #66b6ff) 45%, #ffffff) 0%,
        color-mix(in srgb, var(--fx-accent, #66b6ff) 30%, transparent) 55%,
        transparent 100%);
    mix-blend-mode: screen;
    will-change: opacity;
    contain: strict;
}

/* ===== Shared GPU particle overlay ======================================
 * gpuFx.js injects this canvas once, lazily, on the first burst. See the
 * --z-fx-* tokens in app.css for why it sits above the modal layer.
 *
 * `opacity: 0` at rest is load-bearing. gpuFx.js stops its rAF loop the moment
 * the last particle dies, and a fully transparent canvas lets the compositor
 * drop the layer entirely instead of keeping a viewport-sized texture resident
 * for the rest of the session.
 */
.po-gpufx {
    position: fixed;
    inset: 0;
    z-index: var(--z-fx-particles, 9991);
    pointer-events: none;
    opacity: 0;
    contain: strict;
}

:root[data-gfx='low'] .po-impact-flash { display: none; }

@media (prefers-reduced-motion: reduce) {
    /* Suppressed outright, not merely damped. A repeated full-screen brightness
       spike is a strobe, and reduced-motion is the closest signal the platform
       gives us for photosensitivity. The hit still reports through audio and
       haptics, so no feedback is actually lost. */
    .po-impact-flash { opacity: 0 !important; }}

/* ===== Reduced motion ======================================================
 * Mirrors the existing blocks in app.css / game-shell.css / games.css. Motion
 * is removed; the audio-reactive *colour* channels are kept, since they are not
 * movement and carry real information about the mix.
 */
@media (prefers-reduced-motion: reduce) { .fx-pop{
        animation: none !important;
    }}
/* ===== Scene-composited glass =======================================
 * glassFx.js captures the live game canvas into a small blurred thumbnail and
 * hands it to the panel as --glass-capture. It paints UNDER the panel's own
 * translucent tint, so the blur reads as refraction of the scene rather than a
 * flat smear. Without the attribute (low tier, reduced motion, tainted canvas)
 * the panel keeps its plain backdrop-filter look — same family, no capture.
 */
[data-glass-live] {
    /* !important, and that is the fix: every panel that opts in is styled by a scoped rule
       whose `background` shorthand outranks this selector and resets the image to none, so
       the capture was computed four times a second and never painted. The tint is drawn
       over it here, because an opaque capture hides the panel's own translucent fill. A
       panel that is not a dark over-scene HUD sets --glass-capture-tint to its own colour. */
    background-image:
        linear-gradient(var(--glass-capture-tint, rgba(12, 16, 28, 0.58)), var(--glass-capture-tint, rgba(12, 16, 28, 0.58))),
        var(--glass-capture, none) !important;
    background-size: cover !important;
    background-position: center !important;
}

/* ===== Audio-reactive chrome ===================================
 * visualRuntime.js publishes --audio-bass/mid/treble/peak (0..1) on <html> while a
 * page asks for them: the catalog and the lobbies do (Index.razor, LobbyPanel.razor).
 * Cues (a tap, a seat filling) and pointer speed drive them, not the soundtrack.
 * Everywhere else they stay 0 and these rules are the resting look.
 *
 * Two readers here, both cheap, both on --audio-pulse (the beat, declared above): an
 * opacity on one pseudo-element under the top bar, and `scale` on the catalog's card
 * icons (no layout). The lobby card's glow lives in
 * LobbyPanel.razor.css. Low tier and reduced motion get neither: the icons are
 * movement, and the low tier is exactly the machine that cannot spare the repaint.
 */
:root:not([data-gfx='low']):not([data-motion='reduce']) .gl-top-bar::after {
    content: '';
    position: absolute;
    inset: auto 0 -1px 0;
    height: 2px;
    pointer-events: none;
    background: linear-gradient(90deg, transparent, var(--fx-accent, #6366f1) 30%, var(--fx-accent-2, #22d3ee) 70%, transparent);
    opacity: var(--audio-pulse, 0);
    will-change: opacity;
}

:root:not([data-gfx='low']):not([data-motion='reduce']) .home-card-icon {
    display: inline-block;
    scale: calc(1 + var(--audio-pulse, 0) * 0.1);
    transform-origin: 50% 80%;
}

/* The end-of-game card is read, not glanced at: the scene only tints it. */
dialog.gps-modal[data-glass-live] { --glass-capture-tint: color-mix(in srgb, var(--color-surface) 86%, transparent); }

/* ===== Hub glass ==============================================================
 * glassFx.ambient() (high tier, no reduced motion) shares one blurred thumbnail of the
 * catalog's particle field as --hub-glass on the field's parent, marks it
 * data-hub-glass-live, and gives each card the slice behind it (--hub-glass-pos/-size).
 * The card paints that slice under a theme tint, plus a soft specular spot that follows
 * the pointer (fxTilt.js writes --fx-mx/--fx-my, glare only, no tilt). The card keeps its
 * own background-color underneath; this only swaps the image layers.
 */
:root[data-gfx='high']:not([data-motion='reduce']) [data-hub-glass-live] :is(.home-card, [data-hub-glass]) {
    --hub-glass-tint: light-dark(rgba(255, 255, 255, 0.62), rgba(15, 23, 42, 0.5));
    background-image:
        radial-gradient(circle at calc(50% + var(--fx-mx, 0) * 50%) calc(50% + var(--fx-my, 0) * 50%),
            rgba(255, 255, 255, calc(var(--hub-spec, 0) * 0.16)), transparent 60%),
        linear-gradient(var(--hub-glass-tint), var(--hub-glass-tint)),
        var(--hub-glass, none);
    background-position: 0 0, 0 0, var(--hub-glass-pos, 0 0);
    background-size: auto, auto, var(--hub-glass-size, cover);
    background-repeat: no-repeat;
}

:root[data-gfx='high']:not([data-motion='reduce']) [data-hub-glass-live] :is(.home-card, [data-hub-glass]):hover {
    --hub-spec: 1;
}

/* Specular gleam highlight tracking cursor on tilt & glass elements */
[data-fx-tilt], [data-magnetic], .poeco-btn, .poeco-pill, .poeco-tab {
    transition: translate 0.14s cubic-bezier(0.2, 0.9, 0.3, 1), rotate 0.14s cubic-bezier(0.2, 0.9, 0.3, 1);
    will-change: translate, rotate;
}

[data-fx-tilt]::after, [data-magnetic]::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: inherit;
    pointer-events: none;
    background: radial-gradient(circle at calc(50% + var(--fx-mx, 0) * 45%) calc(50% + var(--fx-my, 0) * 45%),
                rgba(255, 255, 255, 0.18) 0%,
                transparent 65%);
    opacity: 0;
    transition: opacity 0.2s ease;
}

[data-fx-tilt]:hover::after, [data-magnetic]:hover::after {
    opacity: 1;
}

/* ===== Weather overlay ==============================================
 * The overlay canvas is positioned inline by weather.js; this only sets the
 * blend so rain/snow read as light in the scene instead of grey scribbles.
 * Fog type keeps its own normal-blend gradient (inline) for the milky look.
 */
.po-weather-canvas { mix-blend-mode: screen; }

/* ── Route-transition wipe ─────────────────────────────────────────────────
   Driven by js/routeFx.js, tinted to the DESTINATION route's accent pair so the
   transition introduces the page it reveals. See that file for why this is a DOM
   overlay rather than a post-process pass.

   Colours arrive as --wipe-a / --wipe-b set inline by routeFx (runtime values in
   custom properties — the permitted use of inline style), with the neutral
   accent tokens as the fallback so a wipe that fires before paletteBus has
   written anything is still themed rather than black. */
.po-route-wipe {
    position: fixed;
    inset: 0;
    z-index: 9000;
    /* Never an obstacle: it covers the viewport and must not eat a click, and it
       is removed from the a11y tree by aria-hidden in routeFx.js. */
    pointer-events: none;
    opacity: 0;
    background: linear-gradient(
        115deg,
        var(--wipe-a, var(--fx-accent, #818cf8)) 0%,
        var(--wipe-b, var(--fx-accent-2, #c084fc)) 100%);
    /* Painted on its own layer so the animation below never triggers layout or a
       repaint of the page underneath — the whole point of animating only opacity
       and transform. */
    will-change: opacity, transform;
    transform: translate3d(-101%, 0, 0);
}

.po-route-wipe.is-wiping {
    animation: po-route-wipe 260ms cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

/* Sweeps across and off, peaking at partial opacity: a wipe that reached 1 would
   be a full flash, which is both harsher and hides the destination's first paint
   rather than revealing it. */
@keyframes po-route-wipe {
    0%   { opacity: 0;    transform: translate3d(-101%, 0, 0) skewX(-8deg); }
    35%  { opacity: 0.55; }
    100% { opacity: 0;    transform: translate3d(101%, 0, 0) skewX(-8deg); }
}

/* The wipe IS motion, so it has nothing to degrade to. routeFx.js already
   refuses to run under reduced motion; this is the belt-and-braces half, for the
   case where the OS preference flips after the module decided. */
@media (prefers-reduced-motion: reduce) {
    .po-route-wipe.is-wiping {
        animation: none;
        opacity: 0;
    }
}
