#version 440

layout(location = 0) in vec2 qt_TexCoord0;
layout(location = 0) out vec4 fragColor;

layout(std140, binding = 0) uniform buf {
    mat4 qt_Matrix;
    float qt_Opacity;
    float time;
    float amplitude; // Audio amplitude 0.0 to 1.0
};

// Smooth rainbow gradient
vec3 rainbow(float t) {
    t = fract(t);
    float r = abs(t * 6.0 - 3.0) - 1.0;
    float g = 2.0 - abs(t * 6.0 - 2.0);
    float b = 2.0 - abs(t * 6.0 - 4.0);
    return clamp(vec3(r, g, b), 0.0, 1.0);
}

// Smooth waveform function
float wave(float x, float t, float amp) {
    // Multiple sine waves for organic movement
    float w1 = sin(x * 3.0 + t * 2.0) * 0.3;
    float w2 = sin(x * 5.0 - t * 1.5) * 0.2;
    float w3 = sin(x * 7.0 + t * 3.0) * 0.1;
    return (w1 + w2 + w3) * amp;
}

void main() {
    vec2 uv = qt_TexCoord0;
    vec2 p = uv * 2.0 - 1.0; // Center coordinates
    
    float t = time * 0.1;
    float amp = amplitude * 1.92 + 0.1; // Reduced by another 20% from 2.4 to 1.92
    
    // Calculate distance to waveform (dual waves, mirrored)
    float wave1 = wave(uv.x * 3.14159, t, amp);
    float wave2 = -wave1; // Mirror
    
    float dist1 = abs(p.y - wave1);
    float dist2 = abs(p.y - wave2);
    float dist = min(dist1, dist2);
    
    // Wide cinematic glow
    float glow = 1.0 / (dist * 20.0 + 1.0);
    glow = pow(glow, 1.5); // Sharpen the glow
    
    // Rainbow color based on position and time
    float colorPos = uv.x + t * 0.2;
    vec3 color = rainbow(colorPos);
    
    // EVEN longer fade zones for maximum gradual edges (0-45% and 55-100%)
    float fadePos = uv.x;
    float leftFade = smoothstep(0.0, 0.45, fadePos);
    float rightFade = smoothstep(1.0, 0.55, fadePos);
    float fade = leftFade * rightFade;
    
    // Combine everything
    vec3 finalColor = color * glow * fade;
    float alpha = glow * fade * qt_Opacity;
    
    fragColor = vec4(finalColor, alpha);
}
