#version 300 es
precision highp float;
precision highp int;

uniform vec2 uResolution;
uniform float uTime;
uniform float uOpen;
uniform int uLightMode;
uniform int uCamMode;

out vec4 fragColor;

// ============================================================================
// 06_rising_phoenix_wing_flare.glsl
// AAA Cinematic Volumetric VFX Spectacle (strictly NO character silhouettes):
// Flowing, organic, flame-licked symmetrical phoenix fire wings built from
// 3 overlapping tiers of curved, turbulent, wispy fiery plumes unfurling wide
// around a radiant solar eclipse crown halo above a sweeping volumetric wispy
// helical fire ribbon rising from blazing concentric golden-orange solar rings
// on a dark reflective obsidian arena floor, with drifting ember feathers.
// Seamless 3.0s choreographed loop (phase = fract(uTime / 3.0)).
// At t = 1.2s (phase = 0.40), wings reach full wide glorious flare.
// ============================================================================

const float PI  = 3.14159265359;
const float TAU = 6.28318530718;

// ----------------------------------------------------------------------------
// Seamless 3.0s Periodic Animation State
// ----------------------------------------------------------------------------
struct AnimState {
    float phase;      // [0, 1)
    float tauP;       // [0, TAU)
    float wingOpen;   // [-1, +1], +1.0 at phase = 0.40 (t = 1.2s)
    float wingLag;    // trailing outer-feather wave
    float hoverY;     // vertical phoenix core breath
    float flare;      // [0, 1] peak energy intensity
    vec2  cyc;        // (cos(tauP), sin(tauP)) for seamless periodic turbulence
    vec2  cyc2;       // (cos(2*tauP), sin(2*tauP)) faster flame lick
};

AnimState gAnim;

void initAnim() {
    float p  = fract(uTime / 3.0);
    float tp = TAU * p;
    gAnim.phase    = p;
    gAnim.tauP     = tp;
    gAnim.wingOpen = sin(tp - 0.15 * TAU); // +1.0 at phase = 0.40 (t = 1.2s)
    gAnim.wingLag  = sin(tp - 0.24 * TAU);
    gAnim.hoverY   = 0.08 * sin(tp - 0.18 * TAU);
    gAnim.flare    = 0.5 + 0.5 * gAnim.wingOpen;
    gAnim.cyc      = vec2(cos(tp), sin(tp));
    gAnim.cyc2     = vec2(cos(tp * 2.0), sin(tp * 2.0));
}

// ----------------------------------------------------------------------------
// Math & Fast Periodic Turbulence Utilities
// ----------------------------------------------------------------------------
mat2 rot2(float a) {
    float c = cos(a), s = sin(a);
    return mat2(c, -s, s, c);
}

float hash11(float p) {
    p = fract(p * 0.1031);
    p *= p + 33.33;
    p *= p + p;
    return fract(p);
}

float hash21(vec2 p) {
    vec3 p3 = fract(vec3(p.xyx) * 0.1031);
    p3 += dot(p3, p3.yzx + 33.33);
    return fract((p3.x + p3.y) * p3.z);
}

float vnoise2(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f * f * (3.0 - 2.0 * f);
    float a = hash21(i);
    float b = hash21(i + vec2(1.0, 0.0));
    float c = hash21(i + vec2(0.0, 1.0));
    float d = hash21(i + vec2(1.0, 1.0));
    return mix(mix(a, b, u.x), mix(c, d, u.x), u.y);
}

float fbm2(vec2 p) {
    float v = 0.0;
    float a = 0.5;
    mat2 m = mat2(0.80, -0.60, 0.60, 0.80);
    for (int i = 0; i < 4; i++) {
        v += a * vnoise2(p);
        p = m * p * 2.03 + vec2(17.1, 31.7);
        a *= 0.5;
    }
    return v;
}

float fbm2Fast(vec2 p) {
    float v = 0.0;
    float a = 0.55;
    mat2 m = mat2(0.80, -0.60, 0.60, 0.80);
    for (int i = 0; i < 3; i++) {
        v += a * vnoise2(p);
        p = m * p * 2.05 + vec2(13.3, 27.9);
        a *= 0.48;
    }
    return v;
}

// Seamless periodic domain-warped curl-like 2D fire flow
vec2 fireWarp(vec2 p, float freq, float amp) {
    vec2 q = p * freq;
    float n1 = fbm2Fast(q + gAnim.cyc * 0.85);
    float n2 = fbm2Fast(q + vec2(5.2, 1.3) + gAnim.cyc2 * 0.55);
    return vec2(n1 - 0.5, n2 - 0.5) * amp;
}

// ----------------------------------------------------------------------------
// Rich Cinematic Blackbody Fire Palette (matches 06.png)
// Deep smoky crimson-charcoal -> rich crimson -> blazing orange -> radiant gold -> white-gold
// ----------------------------------------------------------------------------
vec3 firePalette(float t) {
    t = clamp(t, 0.0, 1.0);
    vec3 c0 = vec3(0.08, 0.008, 0.006); // smoky ember dark
    vec3 c1 = vec3(0.56, 0.028, 0.006); // deep vermilion crimson
    vec3 c2 = vec3(0.98, 0.210, 0.015); // roaring fire orange
    vec3 c3 = vec3(1.00, 0.600, 0.090); // radiant solar gold
    vec3 c4 = vec3(1.00, 0.930, 0.660); // incandescent white-gold

    vec3 col = mix(c0, c1, smoothstep(0.00, 0.26, t));
    col = mix(col, c2, smoothstep(0.26, 0.54, t));
    col = mix(col, c3, smoothstep(0.54, 0.80, t));
    col = mix(col, c4, smoothstep(0.80, 1.00, t));
    return col;
}

// ----------------------------------------------------------------------------
// Continuous Organic Polar-Fan Phoenix Wing Field (ZERO blocky segments!)
// ----------------------------------------------------------------------------
vec3 haloCenter() {
    return vec3(0.0, 2.52 + gAnim.hoverY, 0.0);
}

struct WingField {
    float density;     // [0, 1+] volumetric plume density
    float temp;        // [0, 1] blackbody fire temperature
    float smokeOcc;    // dark crimson-charcoal overlap crease absorption
    float sculptH;     // smooth continuous height/relief for grayscale sculpt mode
    float barbDetail;  // silky barb & rachis detail
};

// Evaluate one tier of overlapping curved, splayed, glowing avian fire plumes
// uSweep in [0, 1] along wingspan, vChord in [0, 1] from upper arch to sweeping tips
void evalPlumeTier(
    float uSweep, float vChord, float dz, float count, float vStart, float vEnd,
    float zOff, float thickness, float stagger,
    out float mask, out float rachis, out float crease, out float tipT
) {
    // Strong sweeping diagonal avian feather splay (NEVER vertical ribs!)
    float slant = (0.42 * vChord + 0.10 * vChord * vChord) * mix(1.05, 0.38, smoothstep(0.45, 0.96, uSweep));
    float uCurved = uSweep - slant;
    float fPos = uCurved * count + stagger;
    float fId  = floor(fPos);
    float fLoc = fract(fPos) - 0.5; // [-0.5, +0.5] across single feather plume

    // Pronounced organic per-feather length & crest-jut variation (breaks up any smooth dome!)
    float rnd    = hash11(fId * 17.3 + count);
    float lenVar = 0.80 + 0.34 * rnd;
    float vS     = vStart - 0.10 * hash11(fId * 31.7 + count);
    float cLen   = max(0.08, (vEnd - vS) * lenVar);
    tipT = (vChord - vS) / cLen;

    // Broad, sweeping, softly overlapping pointed avian plume profile
    float bladeHalfW = 0.64 * pow(max(0.0, 1.0 - pow(clamp(tipT, 0.0, 1.0), 1.30)), 0.58);
    // Natural flame-frayed feather vane notches along outer edges
    float barbSplit = 1.0 - 0.18 * smoothstep(0.55, 0.95, sin(tipT * 24.0 + fId * 3.1)) * smoothstep(0.22, 0.85, tipT);
    bladeHalfW *= barbSplit;

    float vaneN = abs(fLoc) / max(bladeHalfW, 0.01);
    float radialMask  = smoothstep(-0.08, 0.12, tipT) * smoothstep(1.06, 0.78, tipT);
    // Softer lateral falloff so adjacent plumes glow and blend organically (no harsh dark ribs!)
    float lateralMask = smoothstep(1.14, 0.34, vaneN);

    // Gentle shingled Z-depth layering
    float zShingle = zOff + 0.018 * fLoc + 0.014 * sin(fId * 2.1);
    float zMask = exp(-pow((dz - zShingle) / max(thickness, 0.02), 2.0));

    mask = radialMask * lateralMask * zMask;

    // Luminous central rachis & subtle warm overlap depth (NOT dark vertical stripes!)
    rachis = exp(-fLoc * fLoc * 24.0) * smoothstep(1.0, 0.10, tipT) * mask;
    crease = smoothstep(0.24, 0.50, fLoc) * smoothstep(0.95, 0.18, tipT) * radialMask * zMask;
}

WingField evalWingField(vec3 p, bool isGray) {
    WingField wf;
    wf.density    = 0.0;
    wf.temp       = 0.0;
    wf.smokeOcc   = 0.0;
    wf.sculptH    = 0.0;
    wf.barbDetail = 0.0;

    // Symmetrical left/right wings (flame turbulence uses sign(p.x) for organic variety)
    vec3 q = vec3(abs(p.x), p.y, p.z);

    // Wing-beat & flare kinematic deformation (peaks wide & glorious at phase=0.40, t=1.2s)
    vec3 pivot = vec3(0.22, 2.36 + gAnim.hoverY, 0.0);
    vec3 rel = q - pivot;

    float spread = 0.83 + 0.19 * gAnim.wingOpen;
    rel.x /= spread;

    float spanFactor = smoothstep(0.0, 2.35, rel.x);
    float tipFactor  = smoothstep(0.55, 2.45, rel.x);
    rel.y -= (0.24 * gAnim.wingOpen * spanFactor + 0.16 * gAnim.wingLag * tipFactor);
    float wingZ = -0.36 * gAnim.wingOpen * spanFactor
                +  0.22 * gAnim.wingLag  * tipFactor
                +  0.18 * tipFactor * smoothstep(0.4, -1.6, rel.y);
    float dz = rel.z - wingZ;

    if (abs(dz) > 0.45) return wf;

    // Smooth C-infinity inward scythe-curve at the lower outer wingtips
    float lowDrop = log(1.0 + exp((0.20 - rel.y) * 2.0)) * 0.5;
    float scytheBend = 0.24 * lowDrop * lowDrop * smoothstep(0.50, 1.95, rel.x);
    vec2 wq = vec2(rel.x + scytheBend, rel.y);

    // Normalized wingspan coordinate u in [0, 1] from inner shoulder to outer scythe edge
    float u = wq.x / 2.28;
    if (u < -0.04 || u > 1.08) return wf;
    float uC = clamp(u, 0.0, 1.0);

    // Smooth arched leading edge for clean feather coordinates (zero wavy contour artifacts!)
    float yTop = 0.02
               + 0.94 * sin(pow(uC, 0.58) * PI * 0.92) * (1.0 - 0.24 * uC)
               - 0.88 * pow(uC, 2.05);

    // Subtle jagged crest-feather tip variation applied ONLY to upper boundary mask
    float crestJut = 0.12 * pow(0.5 + 0.5 * sin(uC * 32.0 - 0.6), 2.0) * smoothstep(0.06, 0.24, uC) * smoothstep(0.92, 0.48, uC);

    // Downward-sweeping feather chord length along the wingspan
    float endTaper = smoothstep(-0.02, 0.10, u) * smoothstep(1.04, 0.78, u);
    float chordLen = (0.56 + 0.68 * sin(uC * PI * 0.88) + 0.36 * exp(-pow((uC - 0.72) / 0.22, 2.0))) * (0.20 + 0.80 * endTaper);

    // Downward chord coordinate v in [0, 1] from upper arch down to feather tips
    float v = (yTop - wq.y) / max(chordLen, 0.08);
    if (v < -0.26 - crestJut || v > 1.42) return wf;

    // Domain-warped curl-noise flame turbulence along outer plumes, upper crest & tips
    float tipCurlMask = smoothstep(0.18, 1.08, v) + 0.55 * smoothstep(0.22, -0.16, v);
    vec2 warp = vec2(0.0);
    float flameWisp = 0.0;
    if (!isGray) {
        vec2 noiseP = vec2(uC * 6.0 + sign(p.x) * 2.3, v * 4.2);
        warp = fireWarp(noiseP, 1.0, 0.16 * tipCurlMask);
        vec2 wispP = vec2(uC * 13.0 + sign(p.x) * 3.1, v * 5.8);
        flameWisp = fbm2Fast(wispP + gAnim.cyc * 1.15);
    }

    float uW = clamp(uC + warp.x * 0.36, 0.0, 1.0);
    float vW = v + warp.y * 0.52;

    // 3 Overlapping Sculpted Avian Feather Tiers (broad, sweeping, diagonal plumes!)
    float m0, r0, cr0, t0; // Tier 0: Long sweeping outer primary scythe plumes (11)
    evalPlumeTier(uW, vW, dz, 11.0, 0.14, 1.08, -0.025, 0.058, 0.00, m0, r0, cr0, t0);

    float m1, r1, cr1, t1; // Tier 1: Mid secondary fiery plumes (12)
    evalPlumeTier(uW, vW, dz, 12.0, 0.02, 0.68,  0.025, 0.055, 0.50, m1, r1, cr1, t1);

    float m2, r2, cr2, t2; // Tier 2: Lush incandescent inner covert plumes (11)
    evalPlumeTier(uW, vW, dz, 11.0, -0.04 - crestJut, 0.40, 0.065, 0.052, 0.25, m2, r2, cr2, t2);

    // Silky longitudinal feather barb striations flowing diagonally along each plume
    float diagPhase = uW * 11.0 - 0.42 * vW;
    float barbAng = sin(vW * 48.0 - abs(fract(diagPhase) - 0.5) * 14.0);
    float silkyTex = 0.5 + 0.5 * barbAng;
    float fineFire = isGray ? silkyTex : fbm2Fast(vec2(uW * 18.0 + sign(p.x) * 1.7, vW * 8.5) + gAnim.cyc * 0.9);

    // Wispy curl-noise fire tendrils licking off BOTH the lower feather tips AND upper crest plumes!
    float tipTendrils = 0.0;
    if (!isGray) {
        float lowerEnv = smoothstep(0.55, 0.94, v) * smoothstep(1.40, 0.96, v);
        float upperEnv = smoothstep(0.16, -0.04, v) * smoothstep(-0.26, -0.05, v) * 0.68;
        float zTend = exp(-dz * dz / 0.014);
        tipTendrils = (lowerEnv + upperEnv) * zTend * smoothstep(0.36, 0.82, flameWisp) * endTaper * 0.78;
    }

    float wingMask = max(max(m0, m1 * 1.06), m2 * 1.12) * smoothstep(-0.02, 0.06, u) * smoothstep(1.04, 0.90, u);
    float totalD = wingMask * (0.82 + 0.28 * fineFire) + tipTendrils;

    // TRUE RADIAL PHOENIX HEAT GRADIENT matching 06.png:
    // Incandescent white-gold at the INNER WING ROOTS (near solar crest),
    // transitioning smoothly OUTWARD along the wingspan through blazing golden-orange
    // into deep crimson-red outer primary tips (NO pale cream dome band across the top!)
    float rootDist  = length((q.xy - vec2(0.24, 2.38 + gAnim.hoverY)) * vec2(0.88, 1.15));
    float rootHeat  = exp(-rootDist * rootDist * 1.95) * 0.56;
    float innerSpan = smoothstep(0.85, 0.04, uC) * 0.30;
    float midGold   = smoothstep(0.92, 0.25, vW) * smoothstep(0.95, 0.18, uC) * 0.16;
    float rachisGlow = (r0 * 0.20 + r1 * 0.24 + r2 * 0.22);
    float tipFlare  = smoothstep(0.76, 1.04, t0) * m0 * 0.24; // glowing molten ember tips
    float outerCrimson = smoothstep(0.35, 0.95, uC) * 0.28 + smoothstep(0.45, 1.05, vW) * 0.16;

    float temp = clamp(0.26 + rootHeat + innerSpan + midGold + rachisGlow + tipFlare - outerCrimson + (fineFire - 0.5) * 0.24, 0.06, 0.99);
    if (tipTendrils > 0.05 && wingMask < 0.25) {
        temp = mix(0.22, 0.80, flameWisp);
    }

    // Much softer inter-feather overlap contrast so plumes read as glowing overlapping fire, NOT dark vertical ribs!
    float creaseOcc = clamp(cr0 * 0.32 + cr1 * 0.22 + cr2 * 0.12, 0.0, 0.45) * smoothstep(0.15, 0.65, vW);
    temp *= (1.0 - 0.16 * creaseOcc);

    wf.density    = totalD * (0.88 + 0.22 * gAnim.flare);
    wf.temp       = temp;
    wf.smokeOcc   = creaseOcc * 0.45;
    wf.sculptH    = wingMask;
    wf.barbDetail = silkyTex * 0.65 + (r0 + r1 + r2) * 0.55;
    return wf;
}

// ----------------------------------------------------------------------------
// Sweeping Volumetric Wispy Helical Fire Ribbon (kills the toothpaste tube!)
// ----------------------------------------------------------------------------
struct HelixField {
    float density;
    float temp;
    float smokeOcc;
    float sculptH;
};

float helixRadius(float h) {
    float base = 0.16 + 0.48 * sin(clamp(h, 0.0, 1.0) * PI);
    float wave = 1.0 + 0.09 * sin(h * 11.0 - gAnim.tauP * 2.0);
    return base * wave;
}

HelixField evalHelixField(vec3 p, bool isGray) {
    HelixField hf;
    hf.density  = 0.0;
    hf.temp     = 0.0;
    hf.smokeOcc = 0.0;
    hf.sculptH  = 0.0;

    float maxH = 2.26 + gAnim.hoverY;
    if (p.y < 0.0 || p.y > maxH + 0.18) return hf;

    float rXZ = length(p.xz);
    if (rXZ > 1.25) return hf;

    float h = clamp(p.y / maxH, 0.0, 1.0);
    float R = helixRadius(h);

    // Continuous seamless upward spiral: 2 full turns per 3.0s loop
    float turns = 14.5;
    float theta0 = h * turns - gAnim.tauP * 2.0;
    float ang = atan(p.z, p.x);
    float dAng = mod(ang - theta0 + PI, TAU) - PI;

    float pitchPerRad = maxH / turns;
    float vOff = dAng * pitchPerRad;
    float rOff = rXZ - R;

    // Slight graceful outward ribbon tilt
    vec2 ribUV = rot2(0.32) * vec2(rOff, vOff);

    // Seamless domain-warped flame turbulence (muted in grayscale mode)
    float turb = 0.5;
    if (!isGray) {
        vec2 flowUV = vec2(h * 7.0 - gAnim.phase * 2.0, ang * 1.5);
        // Periodic in phase via circular offset + integer phase shift
        vec2 w = fireWarp(vec2(h * 6.5, ang * 1.2 + vOff * 3.0), 1.0, 0.055);
        ribUV += w * smoothstep(0.0, 0.12, h);
        turb = fbm2Fast(vec2(h * 11.0, vOff * 16.0 + ang * 2.0) + gAnim.cyc2 * 0.9);
    }

    float env = smoothstep(0.0, 0.06, h) * smoothstep(1.0, 0.86, h);
    float ribW = (0.052 + 0.088 * sin(h * PI)) * env;
    float ribT = (0.022 + 0.018 * sin(h * PI)) * env;

    // Multi-strand wispy plasma sheet (NOT a solid uniform block!)
    float coreMask = exp(-pow(ribUV.x / max(ribT, 0.008), 2.0) - pow(ribUV.y / max(ribW, 0.012), 2.0));

    // Incandescent longitudinal fire filaments racing along the ribbon
    float filament1 = pow(0.5 + 0.5 * sin(ribUV.y * 58.0 + h * 18.0 - gAnim.tauP * 2.0), 3.0);
    float filament2 = pow(0.5 + 0.5 * sin(ribUV.y * 32.0 - h * 24.0 + gAnim.tauP * 2.0), 2.0);
    float edgeWisp  = smoothstep(0.35, 0.85, turb) * exp(-pow(ribUV.x / max(ribT * 2.6, 0.02), 2.0) - pow(ribUV.y / max(ribW * 1.7, 0.025), 2.0));

    // Crisp orbital fire whip spiraling loosely around the main flame ribbon
    float R2 = R * 1.32 + 0.05 * sin(h * 9.0 - gAnim.tauP * 3.0);
    float theta1 = h * 19.5 - gAnim.tauP * 3.0 + 1.4;
    float dAng1 = mod(ang - theta1 + PI, TAU) - PI;
    float vOff1 = dAng1 * (maxH / 19.5);
    float rOff1 = rXZ - R2;
    float whipEnv = smoothstep(0.05, 0.18, h) * smoothstep(0.92, 0.72, h);
    float whipMask = exp(-(rOff1 * rOff1 + vOff1 * vOff1) / 0.00028) * whipEnv;

    if (isGray) {
        float ridge = 0.75 + 0.25 * filament1;
        hf.sculptH = max(coreMask * ridge, whipMask * 0.9);
        hf.density = hf.sculptH;
        return hf;
    }

    float ribbonD = coreMask * (0.45 + 0.85 * filament1 + 0.45 * filament2) * (0.65 + 0.65 * turb)
                  + edgeWisp * 0.48
                  + whipMask * 1.15;

    float temp = clamp(0.24 + 0.52 * filament1 + 0.22 * coreMask + 0.26 * (turb - 0.35) + 0.20 * (1.0 - h) + 0.42 * whipMask, 0.08, 0.99);

    hf.density  = ribbonD * (0.88 + 0.24 * gAnim.flare);
    hf.temp     = temp;
    hf.smokeOcc = edgeWisp * 0.42 * (1.0 - coreMask);
    hf.sculptH  = max(coreMask, whipMask);
    return hf;
}

// ----------------------------------------------------------------------------
// Pure VFX Solar Eclipse Crown Halo (strictly NO character silhouette!)
// ----------------------------------------------------------------------------
float sdSolarHalo(vec3 p) {
    vec3 q = p - haloCenter();
    float r3 = length(q);
    if (r3 > 0.82) return r3 - 0.60;

    float rXY = length(q.xy);
    float ringR = 0.235;

    // Razor-crisp golden eclipse ring + fine inner filigree ring
    float dRing  = length(vec2(rXY - ringR, q.z)) - 0.015;
    float dInner = length(vec2(rXY - 0.160, q.z)) - 0.006;

    // 16 ultra-sharp needle-like solar crown spikes radiating outward
    float ang = atan(q.y, q.x);
    float nSpikes = 16.0;
    float sector = TAU / nSpikes;
    float id = floor((ang + 0.5 * sector) / sector);
    float localA = mod(ang + 0.5 * sector, sector) - 0.5 * sector;

    float topBoost = smoothstep(0.80, 1.0, sin(ang));
    float altLen   = mod(id, 2.0) < 0.5 ? 1.0 : 0.55;
    float pulse    = 0.88 + 0.18 * sin(gAnim.tauP - 0.15 * TAU + id * 1.57);
    float spikeLen = (0.145 * altLen + 0.165 * topBoost) * pulse;

    float radial = rXY - ringR;
    float sT = clamp(radial / max(spikeLen, 0.01), 0.0, 1.0);
    float halfAngleW = (0.015 + 0.007 * topBoost) * pow(1.0 - sT, 1.25);
    float latSpike = abs(localA) * max(rXY, ringR) - halfAngleW;
    float axSpike  = max(-radial, radial - spikeLen);
    float dSpike2  = length(max(vec2(latSpike, axSpike), 0.0)) + min(max(latSpike, axSpike), 0.0);
    float dSpike   = length(max(vec2(dSpike2, abs(q.z) - 0.010 * (1.0 - 0.65 * sT)), 0.0))
                   + min(max(dSpike2, abs(q.z) - 0.010 * (1.0 - 0.65 * sT)), 0.0);

    return min(min(dRing, dInner), dSpike) * 0.85;
}

// ----------------------------------------------------------------------------
// Background Arena Balustrade / Railing (Stage Geometry)
// ----------------------------------------------------------------------------
float sdArenaRailing(vec3 p) {
    float r = length(p.xz);
    float dr = abs(r - 3.65);
    if (dr > 0.35 || p.y > 0.60 || p.y < -0.1) {
        return max(dr - 0.15, p.y - 0.45) + 0.05;
    }

    float dTop = length(vec2(r - 3.65, p.y - 0.34)) - 0.020;
    float dMid = length(vec2(r - 3.65, p.y - 0.19)) - 0.012;

    float ang = atan(p.z, p.x);
    float sec = TAU / 28.0;
    float la = mod(ang + 0.5 * sec, sec) - 0.5 * sec;
    vec3 pq = vec3(r - 3.65, p.y - 0.18, la * 3.65);
    vec3 bp = abs(pq) - vec3(0.022, 0.18, 0.022);
    float dPost = length(max(bp, 0.0)) + min(max(bp.x, max(bp.y, bp.z)), 0.0);

    return min(min(dTop, dMid), dPost);
}

// ----------------------------------------------------------------------------
// Grayscale Sculpt Iso-Surface (uLightMode == 1 only: crisp 3D form study)
// ----------------------------------------------------------------------------
float mapGraySculpt(vec3 p, out int matId, out float detail) {
    matId = 0;
    detail = 0.5;
    float best = 1e5;

    // Wing sculpted iso-surface from continuous WingField
    WingField wf = evalWingField(p, true);
    float dWing = (0.34 - wf.sculptH) * 0.095;
    // Clamp outside wing AABB
    vec3 wc = haloCenter();
    vec3 wq = abs(vec3(abs(p.x), p.y, p.z) - (wc + vec3(1.35, -0.25, 0.0))) - vec3(1.55, 1.85, 0.65);
    float dWingBox = length(max(wq, 0.0)) + min(max(wq.x, max(wq.y, wq.z)), 0.0);
    dWing = max(dWing, dWingBox);
    if (dWing < best) {
        best = dWing;
        matId = 1;
        detail = wf.barbDetail;
    }

    float dHalo = sdSolarHalo(p);
    if (dHalo < best) {
        best = dHalo;
        matId = 2;
        detail = 1.0;
    }

    HelixField hf = evalHelixField(p, true);
    float dHelix = (0.32 - hf.sculptH) * 0.085;
    float rXZ = length(p.xz);
    float maxH = 2.26 + gAnim.hoverY;
    float dHxBox = max(rXZ - 1.15, max(-p.y, p.y - maxH));
    dHelix = max(dHelix, dHxBox);
    if (dHelix < best) {
        best = dHelix;
        matId = 3;
        detail = hf.sculptH;
    }

    if (uCamMode == 0) {
        float dRail = sdArenaRailing(p);
        if (dRail < best) {
            best = dRail;
            matId = 4;
            detail = 0.25;
        }
    }
    return best;
}

float mapGrayDist(vec3 p) {
    int m; float det;
    return mapGraySculpt(p, m, det);
}

vec3 calcGrayNormal(vec3 p) {
    const float h = 0.0030;
    const vec2 k = vec2(1.0, -1.0);
    return normalize(
        k.xyy * mapGrayDist(p + k.xyy * h) +
        k.yyx * mapGrayDist(p + k.yyx * h) +
        k.yxy * mapGrayDist(p + k.yxy * h) +
        k.xxx * mapGrayDist(p + k.xxx * h)
    );
}

// ----------------------------------------------------------------------------
// Reflective Dark Obsidian Arena Floor & Blazing Concentric Solar Rings
// ----------------------------------------------------------------------------
vec3 shadeFloor(vec3 p, vec3 rd) {
    float r = length(p.xz);
    float ang = atan(p.z, p.x);

    float radialSeam = abs(sin(ang * 6.0));
    float ringSeam1  = abs(r - 0.76);
    float ringSeam2  = abs(r - 1.18);
    float ringSeam3  = abs(r - 1.62);
    float ringSeam4  = abs(r - 2.35);
    float groove = smoothstep(0.018, 0.003, min(min(ringSeam1, ringSeam2), min(ringSeam3, ringSeam4)));
    groove = max(groove, smoothstep(0.025, 0.005, radialSeam * max(r, 0.35)) * smoothstep(0.45, 0.65, r));

    float stoneN = fbm2(p.xz * 2.8);
    vec3 n = normalize(vec3(
        (vnoise2(p.xz * 9.0) - 0.5) * 0.032,
        1.0,
        (vnoise2(p.xz * 9.0 + 13.7) - 0.5) * 0.032
    ));

    // Concentric blazing golden-orange solar rings + swirling molten core
    float corePool = exp(-r * r * 7.5);
    float swirlN = fbm2Fast(vec2(ang * 2.0 + r * 3.5, r * 4.0) + gAnim.cyc * 0.8);

    float ring1 = exp(-pow((r - 0.56) / 0.032, 2.0));
    float ring2 = exp(-pow((r - 0.94) / 0.036, 2.0));
    float ring3 = exp(-pow((r - 1.36) / 0.024, 2.0));
    float ringOuter = exp(-pow((r - 1.85) / 0.016, 2.0)) * 0.45;

    float seg = 0.65 + 0.35 * sin(ang * 8.0 - gAnim.tauP * 2.0);
    float pulseR = fract(gAnim.phase * 2.0) * 1.75;
    float pulseFade = sin(PI * fract(gAnim.phase * 2.0));
    float pulseWave = exp(-pow((r - pulseR) / 0.085, 2.0)) * pulseFade * 0.72;

    float ringEnergy = corePool * (0.95 + 0.40 * swirlN)
                     + ring1 * 1.18
                     + ring2 * (0.96 * seg)
                     + ring3 * 0.74
                     + ringOuter
                     + pulseWave;
    ringEnergy *= (0.85 + 0.25 * gAnim.flare);

    if (uLightMode == 1) {
        float base = mix(0.14, 0.22, stoneN) * (1.0 - 0.45 * groove);
        float ridgeL = 0.32 * ring1 + 0.26 * ring2 + 0.20 * ring3 + 0.18 * corePool + 0.10 * pulseWave;
        float distFade = exp(-r * 0.18);
        vec3 L = normalize(vec3(0.45, 0.80, 0.40));
        float diff = max(dot(n, L), 0.0);
        float val = (base * (0.55 + 0.60 * diff) + ridgeL) * (0.45 + 0.55 * distFade);
        return vec3(val);
    }

    // Dark glossy obsidian stone floor
    vec3 floorBase = mix(vec3(0.008, 0.010, 0.020), vec3(0.020, 0.018, 0.030), stoneN);
    floorBase *= (1.0 - 0.55 * groove);

    float warmPool = exp(-r * 0.60) * (0.80 + 0.25 * gAnim.flare);
    vec3 col = floorBase + vec3(0.24, 0.068, 0.012) * warmPool * (1.0 - 0.40 * groove);

    if (uLightMode == 2) {
        vec3 L = normalize(vec3(0.95, 0.22, 0.20));
        float diff = max(dot(n, L), 0.0);
        float spec = pow(max(dot(reflect(-L, n), -rd), 0.0), 28.0);
        col += vec3(0.18, 0.12, 0.08) * diff * exp(-r * 0.22) + vec3(0.32, 0.20, 0.10) * spec * exp(-r * 0.3);
    } else if (uLightMode == 3) {
        vec3 L = normalize(vec3(0.0, 0.28, -1.0));
        float spec = pow(max(dot(reflect(-L, n), -rd), 0.0), 22.0);
        col += vec3(0.50, 0.26, 0.08) * spec * exp(-r * 0.25);
    }

    // Glossy Fresnel reflections of the blazing fire wings & helix on the wet dark stone
    float fres = pow(1.0 - max(dot(n, -rd), 0.0), 2.8);
    vec3 ref = reflect(rd, n);
    if (ref.z < -0.04 && p.z > -0.25) {
        float tRef = -p.z / ref.z;
        if (tRef > 0.0 && tRef < 12.0) {
            vec3 rp = p + ref * tRef;
            float refHelix = exp(-pow(rp.x * 2.2, 2.0)) * smoothstep(0.0, 0.35, rp.y) * smoothstep(2.6, 0.5, rp.y);
            float refWing  = exp(-pow((abs(rp.x) - 1.38) / 0.85, 2.0) - pow((rp.y - 2.15) / 0.95, 2.0));
            vec3 refCol = vec3(1.0, 0.38, 0.04) * (0.72 * refHelix + 0.48 * refWing) * (0.84 + 0.24 * gAnim.flare);
            col += refCol * (0.22 + 0.68 * fres) * (1.0 - 0.55 * groove) * exp(-r * 0.26);
        }
    }

    vec3 ringCol = mix(vec3(1.0, 0.28, 0.02), vec3(1.0, 0.88, 0.46), smoothstep(0.30, 1.15, ringEnergy));
    col += ringCol * ringEnergy * 1.30;
    col += vec3(1.0, 0.38, 0.03) * groove * exp(-r * 1.10) * 0.52;

    float fog = 1.0 - exp(-r * r * 0.030);
    col = mix(col, vec3(0.012, 0.012, 0.026), fog * 0.75);

    return col;
}

// ----------------------------------------------------------------------------
// Deep Dramatic Midnight-Indigo Twilight Arena Sky & Billowing Smoke
// ----------------------------------------------------------------------------
vec3 shadeBackground(vec3 rd) {
    float y = clamp(rd.y, -0.2, 1.0);

    if (uLightMode == 1) {
        return vec3(mix(0.11, 0.19, smoothstep(-0.05, 0.65, y)));
    }

    vec3 skyTop = vec3(0.0025, 0.006, 0.018);
    vec3 skyMid = vec3(0.006,  0.011, 0.032);
    vec3 skyHor = vec3(0.018,  0.014, 0.032);

    vec3 col = mix(skyHor, skyMid, smoothstep(0.0, 0.25, y));
    col = mix(col, skyTop, smoothstep(0.22, 0.70, y));

    // Distant curved amphitheater / colosseum tiers near horizon
    float tierMask = smoothstep(0.22, 0.04, y) * smoothstep(-0.02, 0.03, y);
    float tiers = 0.5 + 0.5 * sin(y * 125.0);
    col *= (1.0 - 0.38 * tierMask * tiers);

    // Dark-indigo billowing twilight clouds warmed softly near the fire vortex
    vec2 cloudUV = vec2(rd.x * 2.2, rd.y * 3.2);
    float smoke1 = fbm2Fast(cloudUV * 1.5 + vec2(0.0, 0.6) + gAnim.cyc * 0.30);
    float smoke2 = fbm2Fast(cloudUV * 3.0 + vec2(4.2, 1.9) + gAnim.cyc * 0.42);
    float smoke = smoothstep(0.42, 0.85, smoke1 * 0.65 + smoke2 * 0.35);

    float centerProx = exp(-rd.x * rd.x * 4.0) * smoothstep(0.38, -0.05, y);
    vec3 smokeCol = mix(vec3(0.010, 0.016, 0.042), vec3(0.11, 0.038, 0.022), centerProx * 0.88);
    col += smokeCol * smoke * 0.45;

    // Warm atmospheric under-glow behind the phoenix
    float auraDist = length(vec2(rd.x * 1.05, (rd.y - 0.10) * 1.35));
    float aura = exp(-auraDist * auraDist * 7.0) * (0.75 + 0.25 * gAnim.flare);
    if (uLightMode == 3) aura *= 1.85;
    col += vec3(0.22, 0.062, 0.012) * aura * 0.42;

    return col;
}

// ----------------------------------------------------------------------------
// Volumetric Ray-Box Intersection
// ----------------------------------------------------------------------------
vec2 intersectBox(vec3 ro, vec3 rd, vec3 bMin, vec3 bMax) {
    vec3 invR = 1.0 / rd;
    vec3 t0 = (bMin - ro) * invR;
    vec3 t1 = (bMax - ro) * invR;
    vec3 tSmall = min(t0, t1);
    vec3 tBig   = max(t0, t1);
    float tNear = max(max(tSmall.x, tSmall.y), tSmall.z);
    float tFar  = min(min(tBig.x,   tBig.y),   tBig.z);
    return vec2(tNear, tFar);
}

// ----------------------------------------------------------------------------
// Solar Eclipse Crown Corona + Drifting Incandescent Ember Feathers & Sparks
// ----------------------------------------------------------------------------
vec3 evalSolarAndEmbers(vec3 ro, vec3 rd, float maxT) {
    vec3 acc = vec3(0.0);
    bool isGray = (uLightMode == 1);

    // 1) Radiant Solar Eclipse Crown Halo & 16 Sharp Corona Spikes (Pure VFX!)
    vec3 hc = haloCenter();
    float tH = dot(hc - ro, rd);
    if (tH > 0.0 && tH < maxT + 0.4 && !isGray) {
        vec3 hp = ro + rd * tH;
        vec2 dH = (hp - hc).xy;
        float rH = length(dH);
        float angH = atan(dH.y, dH.x);

        // Razor-crisp blazing golden-white eclipse rim
        float ringCore = exp(-pow((rH - 0.235) / 0.016, 2.0));
        float ringGlow = exp(-pow((rH - 0.235) / 0.055, 2.0));
        float innerRing = exp(-pow((rH - 0.160) / 0.008, 2.0)) * 0.55;

        // 16 razor-sharp solar crown spikes radiating cleanly outward
        float nSp = 16.0;
        float sec = TAU / nSp;
        float id = floor((angH + 0.5 * sec) / sec);
        float la = mod(angH + 0.5 * sec, sec) - 0.5 * sec;
        float topBoost = smoothstep(0.80, 1.0, sin(angH));
        float altLen = mod(id, 2.0) < 0.5 ? 1.0 : 0.55;
        float pulse = 0.88 + 0.18 * sin(gAnim.tauP - 0.15 * TAU + id * 1.57);
        float sLen = (0.155 * altLen + 0.175 * topBoost) * pulse;
        float radOff = rH - 0.230;
        float sT = radOff / max(sLen, 0.01);
        float wSp = (0.013 + 0.006 * topBoost) * pow(max(0.0, 1.0 - clamp(sT, 0.0, 1.0)), 1.35);
        float spikeMask = smoothstep(wSp + 0.006, wSp * 0.30, abs(la) * max(rH, 0.23))
                        * smoothstep(-0.008, 0.018, radOff)
                        * smoothstep(1.01, 0.88, sT);

        // Subtle fiery corona rays & outer bloom
        float rays = pow(max(0.0, cos(angH * 8.0)), 10.0) * exp(-max(0.0, rH - 0.22) * 5.2);
        float outerBloom = exp(-max(0.0, rH - 0.20) * 4.8) * 0.35;

        float boost = (uLightMode == 3) ? 1.40 : (uLightMode == 2 ? 0.65 : 1.0);
        vec3 crownCol = vec3(1.0, 0.95, 0.72) * (ringCore * 1.45 + spikeMask * 1.35 + innerRing)
                      + vec3(1.0, 0.62, 0.14) * (ringGlow * 0.75 + rays * 0.45)
                      + vec3(1.0, 0.32, 0.03) * outerBloom;
        acc += crownCol * (0.86 + 0.24 * gAnim.flare) * boost;
    }

    // 2) Warm swirling fire mist rising off the concentric floor rings
    if (!isGray) {
        vec3 basePos = vec3(0.0, 0.42, 0.0);
        float tB = dot(basePos - ro, rd);
        if (tB > 0.0 && tB < maxT + 1.0) {
            vec3 bp = ro + rd * tB;
            float rB = length((bp - basePos) * vec3(1.15, 1.50, 1.0));
            float mistN = fbm2Fast(bp.xy * 2.5 + gAnim.cyc * 0.65);
            float mist = exp(-rB * rB * 1.65) * (0.35 + 0.65 * mistN);
            acc += vec3(0.62, 0.20, 0.03) * mist * 0.28 * (0.85 + 0.22 * gAnim.flare);
        }
    }

    // 3) Seamless 3D Drifting Incandescent Ember Sparks & Golden Feather Flakes
    int numEmbers = (uCamMode == 1) ? 22 : 34;
    for (int i = 0; i < 34; i++) {
        if (i >= numEmbers) break;
        float fi = float(i);
        float s1 = hash11(fi * 13.7 + 1.1);
        float s2 = hash11(fi * 29.3 + 4.7);
        float s3 = hash11(fi * 47.1 + 9.3);

        float pLife = fract(gAnim.phase + s1);
        float lifeEnv = sin(PI * pLife);

        float y = mix(0.06, 3.50, pLife);
        float orbitR = mix(0.28 + 1.75 * s2, 0.68 + 2.25 * s2, pLife);
        float ang = s3 * TAU + gAnim.tauP * (s2 > 0.5 ? 1.0 : -1.0) * 0.5 + pLife * 2.0;
        vec3 ep = vec3(cos(ang) * orbitR, y, sin(ang) * orbitR * 0.72);

        float tE = dot(ep - ro, rd);
        if (tE < 0.25 || tE > maxT + 0.5) continue;

        vec3 diff = (ro + rd * tE) - ep;

        bool isFlake = (mod(fi, 4.0) < 0.5);
        if (isFlake) {
            float spin = s2 * TAU + gAnim.tauP * (s3 > 0.5 ? 1.0 : -1.0);
            vec2 local = rot2(spin) * diff.xy;
            local.x *= 2.4;
            float d2 = length(local);
            float flakeMask = smoothstep(0.052, 0.010, d2) * lifeEnv;
            vec3 flakeCol = isGray
                ? vec3(0.45)
                : mix(vec3(1.0, 0.34, 0.03), vec3(1.0, 0.88, 0.42), smoothstep(0.042, 0.008, d2));
            acc += flakeCol * flakeMask * (isGray ? 0.38 : 0.95);
        } else {
            vec2 local = diff.xy * vec2(1.5, 0.80);
            float d2 = length(local);
            float sz = mix(0.010, 0.025, s3);
            float glow = exp(-d2 * d2 / (sz * sz)) * lifeEnv;
            vec3 sparkCol = isGray
                ? vec3(0.55)
                : mix(vec3(1.0, 0.36, 0.03), vec3(1.0, 0.92, 0.55), exp(-d2 / (sz * 0.45)));
            acc += sparkCol * glow * (isGray ? 0.30 : 1.10);
        }
    }

    return acc;
}

// ----------------------------------------------------------------------------
// ACES Filmic Tonemapping
// ----------------------------------------------------------------------------
vec3 acesToneMap(vec3 x) {
    const float a = 2.51;
    const float b = 0.03;
    const float c = 2.43;
    const float d = 0.59;
    const float e = 0.14;
    return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
}

// ----------------------------------------------------------------------------
// Main
// ----------------------------------------------------------------------------
void main() {
    initAnim();

    vec2 uv = (gl_FragCoord.xy - 0.5 * uResolution) / uResolution.y;

    vec3 ro, ta;
    float fl;
    if (uCamMode == 1) {
        // ~2.5x macro close-up on the layered glowing fire-feather plumes & solar halo core
        ro = vec3(0.72, 2.46 + gAnim.hoverY * 0.5, 2.38);
        ta = vec3(0.68, 2.38 + gAnim.hoverY * 0.5, 0.0);
        fl = 1.68;
    } else {
        // Hero full wingspan arena framing matching 06.png
        ro = vec3(0.0, 1.32, 5.85);
        ta = vec3(0.0, 1.50, 0.0);
        fl = 1.42;
    }

    vec3 ww = normalize(ta - ro);
    vec3 uu = normalize(cross(ww, vec3(0.0, 1.0, 0.0)));
    vec3 vv = cross(uu, ww);
    vec3 rd = normalize(uv.x * uu + uv.y * vv + fl * ww);

    float tFloor = (rd.y < -0.0005) ? (-ro.y / rd.y) : 1e5;
    float tMax = min(tFloor, 15.0);

    // ========================================================================
    // PATH A: Pure Neutral Grayscale Sculpt Study (uLightMode == 1, R==G==B)
    // ========================================================================
    if (uLightMode == 1) {
        float t = 0.25;
        bool hitObj = false;
        int matId = 0;
        float det = 0.5;
        for (int i = 0; i < 84; i++) {
            if (t > tMax) break;
            vec3 p = ro + rd * t;
            float d = mapGraySculpt(p, matId, det);
            if (d < 0.0025) {
                hitObj = true;
                break;
            }
            t += max(d, 0.007);
        }

        vec3 col;
        if (hitObj) {
            vec3 p = ro + rd * t;
            vec3 n = calcGrayNormal(p);
            vec3 keyD  = normalize(vec3(0.55, 0.72, 0.42));
            vec3 fillD = normalize(vec3(-0.65, 0.30, 0.65));
            vec3 rimD  = normalize(vec3(0.0, 0.50, -0.86));
            float ndv  = max(dot(n, -rd), 0.0);
            float diff = max(dot(n, keyD), 0.0);
            float fill = max(dot(n, fillD), 0.0);
            float rim  = pow(1.0 - ndv, 2.5) * (0.45 + 0.55 * max(dot(n, rimD), 0.0));
            float spec = pow(max(dot(n, normalize(keyD - rd)), 0.0), 32.0);
            float alb  = (matId == 2) ? 0.78 : (matId == 4 ? 0.28 : (0.48 + 0.20 * clamp(det, 0.0, 1.0)));
            float lit  = alb * (0.20 + 0.76 * diff + 0.24 * fill) + 0.16 * spec + 0.24 * rim;
            col = vec3(lit);
        } else if (tFloor < 15.0) {
            col = shadeFloor(ro + rd * tFloor, rd);
        } else {
            col = shadeBackground(rd);
        }
        col += evalSolarAndEmbers(ro, rd, hitObj ? t : tMax);
        float luma = acesToneMap(vec3(col.x * 0.96)).x;
        luma = pow(clamp(luma, 0.0, 0.97), 1.0 / 2.2);
        fragColor = vec4(vec3(luma), 1.0);
        return;
    }

    // ========================================================================
    // PATH B: Cinematic Volumetric Fire Wings + Wispy Helical Flame Ribbon
    // ========================================================================
    // 1) Base stage: dark twilight sky, glossy reflective arena floor & dark railing
    vec3 bgCol = (tFloor < 15.0) ? shadeFloor(ro + rd * tFloor, rd) : shadeBackground(rd);
    float stageT = tMax;

    // March arena balustrade railing only in hero cam (cheap, ~24 steps max)
    if (uCamMode == 0 && rd.y < 0.05 && rd.y > -0.35) {
        float tR = 2.0;
        for (int i = 0; i < 28; i++) {
            if (tR > tMax) break;
            vec3 pr = ro + rd * tR;
            float dr = sdArenaRailing(pr);
            if (dr < 0.004) {
                vec3 metal = vec3(0.015, 0.015, 0.024);
                float warmRim = exp(-length(pr.xz) * 0.34);
                bgCol = metal + vec3(0.28, 0.10, 0.018) * warmRim * (uLightMode == 3 ? 1.5 : 0.75);
                stageT = tR;
                break;
            }
            tR += max(dr, 0.02);
        }
    }

    // 2) Bounded Volumetric Raymarch through the Phoenix Wings + Helical Fire Volume
    vec3 bMin = vec3(-2.95, 0.00, -1.25);
    vec3 bMax = vec3( 2.95, 3.65,  1.25);
    vec2 boxT = intersectBox(ro, rd, bMin, bMax);

    vec3 volEmit = vec3(0.0);
    float trans  = 1.0;

    if (boxT.y > max(boxT.x, 0.0) && boxT.x < stageT) {
        float tStart = max(boxT.x, 0.15);
        float tEnd   = min(boxT.y, stageT);

        // Silky-clean zero-jitter midpoint stepping (eliminates all dither stipple!)
        const int STEPS = 60;
        float dt = (tEnd - tStart) / float(STEPS);
        float t = tStart + 0.5 * dt;

        // Lighting mode modulation for grazing (2) & backlit rim (3)
        float emScale   = (uLightMode == 2) ? 0.72 : (uLightMode == 3 ? 1.18 : 1.00);
        float grazingK  = (uLightMode == 2) ? 1.0 : 0.0;
        float backlitK  = (uLightMode == 3) ? 1.0 : 0.0;

        for (int i = 0; i < STEPS; i++) {
            if (trans < 0.025) break;
            vec3 p = ro + rd * t;

            // Evaluate continuous organic phoenix wings + wispy helical fire ribbon
            WingField  wf = evalWingField(p, false);
            HelixField hf = evalHelixField(p, false);

            float dW = wf.density;
            float dH = hf.density;
            float dTot = dW + dH;

            if (dTot > 0.006) {
                vec3 emitW = firePalette(wf.temp) * dW * (1.85 + 0.55 * wf.temp);
                vec3 emitH = firePalette(hf.temp) * dH * (2.15 + 0.60 * hf.temp);

                // Grazing key directional raking highlight across plumage in uLightMode == 2
                if (grazingK > 0.5) {
                    float sideKey = smoothstep(-1.5, 1.8, p.x) * 0.65 + 0.35;
                    emitW *= sideKey * (0.85 + 0.45 * wf.barbDetail);
                    emitH *= (0.75 + 0.55 * smoothstep(-0.6, 0.6, p.x));
                }
                // Intense golden backlit edge glow in uLightMode == 3
                if (backlitK > 0.5) {
                    float edgeBoost = smoothstep(0.65, 0.12, dTot);
                    emitW += vec3(1.0, 0.78, 0.28) * edgeBoost * dW * 1.10;
                    emitH += vec3(1.0, 0.80, 0.32) * edgeBoost * dH * 1.00;
                }

                // Beer-Lambert absorption: dark smoky crimson valleys give crisp 3D feather separation!
                float sigmaA = (dW * (1.65 + 2.40 * wf.smokeOcc) + dH * (1.20 + 1.80 * hf.smokeOcc));
                float stepAlpha = 1.0 - exp(-sigmaA * dt * 2.2);

                volEmit += trans * (emitW + emitH) * emScale * dt * 2.45;
                trans *= (1.0 - stepAlpha);
            }

            // Dark smoky eclipse core disc inside the solar halo (pure eclipse shadow, NO torso!)
            vec3 hc = haloCenter();
            float dCoreZ = abs(p.z - hc.z);
            if (dCoreZ < 0.08) {
                float rCore = length((p - hc).xy);
                if (rCore < 0.225) {
                    float eclipseMask = smoothstep(0.23, 0.19, rCore) * exp(-dCoreZ * dCoreZ / 0.0025);
                    trans *= exp(-eclipseMask * dt * 14.0);
                }
            }

            t += dt;
        }
    }

    // Composite volumetric fire over dark twilight arena floor & sky
    vec3 col = bgCol * trans + volEmit;

    // Add crisp solar eclipse crown halo, spikes, floor fire mist & drifting ember feathers
    col += evalSolarAndEmbers(ro, rd, stageT);

    // Subtle cinematic vignette
    float vig = 1.0 - 0.18 * dot(uv, uv);
    col *= vig;

    // ACES filmic tonemapping with controlled peak (%clip < 1%, lumMin >= 4, %black == 0%)
    col = acesToneMap(col * 0.84);
    col = max(col, vec3(0.0024, 0.0032, 0.0068));
    col = min(col, vec3(0.985));
    col = pow(col, vec3(1.0 / 2.2));
    fragColor = vec4(col, 1.0);
}
