#version 330 core
precision highp float;

uniform vec3 iResolution;
uniform float iTime;
uniform float alpha;

in vec2 fragCoord;
out vec4 fragColor;

// Serpent in Paradise — abstract backdrop: jungle-green gradient, Copacabana-style wave
// bands, a big bossa sun and faint drifting triangles. Brazil flag palette.
const vec3 deepGreen = vec3(0.012, 0.10, 0.065);
const vec3 leafGreen = vec3(0.0, 0.38, 0.18);
const vec3 sunYellow = vec3(1.0, 0.87, 0.05);
const vec3 seaBlue = vec3(0.0, 0.15, 0.46);
const vec3 cream = vec3(0.99, 0.97, 0.88);

float sdEquilateralTriangle(vec2 p, float r)
{
	const float k = sqrt(3.0);
	p.x = abs(p.x) - r;
	p.y = p.y + r / k;
	if (p.x + k * p.y > 0.0) {
		p = vec2(p.x - k * p.y, -k * p.x - p.y) / 2.0;
	}
	p.x -= clamp(p.x, -2.0 * r, 0.0);
	return -length(p) * sign(p.y);
}

mat2 rot(float a)
{
	float c = cos(a);
	float s = sin(a);
	return mat2(c, -s, s, c);
}

// One layer of Copacabana boardwalk waves: soft stripes along a meandering sine curve
float waveBand(vec2 p, float rows, float amp, float freq, float phase, float width)
{
	float y = p.y * rows + amp * sin(p.x * freq + phase);
	float d = abs(fract(y) - 0.5);
	return smoothstep(width, width - 0.09, d);
}

void main()
{
	vec2 uv = fragCoord / iResolution.xy;
	vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y; // centered, aspect-corrected
	float t = iTime;

	// Vertical jungle gradient
	vec3 col = mix(deepGreen, leafGreen * 0.55, smoothstep(-0.65, 0.75, p.y));

	// Staged intro: first wave layer, then the second, then the sun drops in fast,
	// then the ribbon snakes arrive (their timing lives in the demo json).
	float in1 = smoothstep(0.5, 4.0, t);
	float in2 = smoothstep(4.5, 8.0, t);
	float inTri = smoothstep(10.0, 14.0, t);

	// Copacabana wave bands, two parallax layers swaying gently
	float w1 = waveBand(p, 4.0, 0.35, 2.6, t * 0.10, 0.16);
	float w2 = waveBand(p + vec2(0.4, 0.13), 6.5, 0.30, 3.4, -t * 0.14 + 1.3, 0.12);
	col = mix(col, seaBlue, w1 * 0.30 * in1);
	col = mix(col, cream, w2 * 0.10 * in2);

	// Sun: a big soft circle with a halo, dropping quickly into place at ~8 s
	float drop = smoothstep(8.0, 10.5, t);
	float sunY = mix(1.0, 0.26, drop);

	// Outro: the sun falls off-screen under gravity once the credits have had a
	// moment fully on screen; the end fade-to-black part takes over from 65 s
	float fallT = max(t - 63.0, 0.0);
	sunY -= 0.29 * fallT * fallT;
	vec2 sunPos = vec2(0.58, sunY + 0.015 * sin(t * 0.4));
	float sunDist = length(p - sunPos);
	float sun = smoothstep(0.30, 0.29, sunDist);
	float halo = smoothstep(0.62, 0.30, sunDist);
	col = mix(col, sunYellow * 0.85, halo * 0.18);
	col = mix(col, sunYellow, sun * 0.9);

	// Faint drifting triangles (the snake's zigzag motif, scattered in the air)
	for (int i = 0; i < 5; ++i) {
		float fi = float(i);
		vec2 tp = p - vec2(sin(fi * 2.4 + t * 0.07) * 0.75, cos(fi * 1.7 + t * 0.05) * 0.42);
		tp = rot(t * 0.06 + fi * 1.3) * tp;
		float d = sdEquilateralTriangle(tp, 0.10 + 0.05 * sin(fi * 3.1));
		float edge = smoothstep(0.012, 0.0, abs(d));
		col = mix(col, cream, edge * 0.10 * inTri);
	}

	// Vignette
	float vig = smoothstep(1.25, 0.45, length(p));
	col *= mix(0.72, 1.0, vig);

	fragColor = vec4(col * alpha, alpha);
}
