#ifndef INCLUDE_FIREFLIES
    #define INCLUDE_FIREFLIES

    // v9.0: Ambient fireflies for Survival/Adventure night atmosphere
    // Simple screen-space particle effect - sparse warm glow dots at night

    float Hash21(vec2 p) {
        p = fract(p * vec2(234.34, 435.345));
        p += dot(p, p + 34.23);
        return fract(p.x * p.y);
    }

    vec3 GetFireflies(vec3 nViewPos, float VdotU, float lViewPos) {
        // Only show at night, not indoors, not in rain
        float nightFactor = max0(-VdotU);
        if (nightFactor < 0.15 || rainFactor > 0.1) return vec3(0.0);

        vec3 fireflyColor = vec3(0.0);

        // Screen-space grid particles (3 layers for density)
        for (int layer = 0; layer < 3; layer++) {
            float layerScale = 8.0 + float(layer) * 4.0;
            vec2 gridUV = texCoord * layerScale + float(layer) * 37.0;
            vec2 gridCell = floor(gridUV);
            vec2 cellUV = fract(gridUV) - 0.5;

            float cellHash = Hash21(gridCell + float(layer) * 100.0);

            // Only ~12% of cells have fireflies
            if (cellHash > 0.12) continue;

            // Particle position within cell (animated)
            float particleSeed = cellHash * 100.0;
            vec2 particlePos = vec2(
                Hash21(gridCell + 1.0) - 0.5,
                Hash21(gridCell + 2.0) - 0.5
            ) * 0.4;

            // Gentle floating motion
            particlePos.x += sin(frameTimeCounter * 0.4 + particleSeed) * 0.05;
            particlePos.y += cos(frameTimeCounter * 0.3 + particleSeed * 1.3) * 0.05;

            float dist = length(cellUV - particlePos);

            // Pulsing glow
            float pulse = sin(frameTimeCounter * 1.5 + particleSeed * 7.0) * 0.5 + 0.5;
            pulse = pulse * pulse; // sharper pulse

            // Falloff
            float glow = exp(-dist * 25.0) * pulse;

            // Warm yellow-green color with slight variation
            vec3 color = vec3(0.7, 0.95, 0.25) + vec3(0.1, 0.05, -0.1) * Hash21(gridCell + 3.0);

            fireflyColor += color * glow * 0.2;
        }

        return fireflyColor * nightFactor;
    }
#endif
