#version 330 core
in vec2 vUV;
out vec4 FragColor;
uniform float uTime;
uniform float uAlpha;
uniform int uBarCount;
uniform float uBarThickness;

void main() {
    vec2 uv = vUV;
    float barCount = float(max(uBarCount, 1));
    float barThickness = max(uBarThickness, 0.0);
    vec3 final = vec3(0.0);

    for (float i = 0.0; i < barCount; i += 1.0) {
        float phase = i * 0.785398;
        float timeOffset = uTime * (1.0 - i * 0.08);
        float offsetY = sin(timeOffset * 1.2 + phase) * 0.35;
        float barY = i / barCount + offsetY;
        float distY = abs(uv.y - barY);
        float curvature = sin(uv.x * 3.14159 + timeOffset * 0.7 + i) * 0.04;
        float waveDist = distY - curvature;

        if (waveDist < barThickness && waveDist > -barThickness * 0.3) {
            float frontFade = 1.0 - (waveDist / barThickness);
            float edgeFadeFront = smoothstep(0.0, 0.1, frontFade);
            float h = mod(i * 1.5 + timeOffset * 0.2, 1.0);
            vec3 rgb = vec3(cos(h * 6.28318), cos(h * 6.28318 - 2.09), cos(h * 6.28318 - 4.18)) * 0.5 + 0.5;
            rgb *= (0.9 + 0.2 * sin(timeOffset + i));
            rgb *= edgeFadeFront * (0.8 + 0.3 * sin(timeOffset * 0.5 - i));
            final = max(final, rgb);
        }

        float offsetBack = sin(timeOffset * 0.9 - phase) * 0.3;
        float backY = (i + 0.3) / barCount + offsetBack;
        float distYBack = abs(uv.y - backY);
        float waveDistBack = distYBack - curvature * 0.5;

        if (waveDistBack < barThickness && waveDistBack > -barThickness * 0.2) {
            float backFade = 1.0 - (waveDistBack / barThickness);
            float edgeFadeBack = smoothstep(0.0, 0.15, backFade);
            float h = mod(i + 3.0 + timeOffset * 0.15, barCount) / barCount;
            vec3 rgb = vec3(cos(h * 6.28318), cos(h * 6.28318 - 2.09), cos(h * 6.28318 - 4.18)) * 0.5 + 0.5;
            rgb *= 0.5;
            rgb *= edgeFadeBack * (0.6 + 0.2 * sin(timeOffset * 0.3 + i));
            final = max(final, rgb);
        }
    }

    final += vec3(0.03);
    FragColor = vec4(final, uAlpha);
}
