shader_type canvas_item;
render_mode unshaded;

// ✅ Godot 4 (nuevo): declarar screen texture como uniform
uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, repeat_disable, filter_linear_mipmap;

// --- Color / ambiente ---
uniform float red_strength : hint_range(0.0, 1.0) = 0.35;
uniform float darkness     : hint_range(0.0, 1.0) = 0.35;
uniform float contrast     : hint_range(0.5, 2.0) = 1.10;
uniform float vignette     : hint_range(0.0, 1.0) = 0.35;
uniform float grain        : hint_range(0.0, 0.3) = 0.06;

// --- Ceniza ---
uniform float ash_density  : hint_range(0.0, 1.0) = 0.35;
uniform float ash_speed    : hint_range(0.0, 1.0) = 0.35;
uniform float ash_scale    : hint_range(0.5, 6.0) = 2.2;
uniform float ash_soft     : hint_range(0.0, 1.0) = 0.35;
uniform vec2  ash_dir = vec2(-0.35, -1.0);

// --- Relámpagos ---
uniform float lightning_rate      : hint_range(0.0, 1.0) = 0.10;
uniform float lightning_intensity : hint_range(0.0, 3.0) = 1.35;
uniform float lightning_height    : hint_range(0.05, 0.8) = 0.25;
uniform float bolt_width          : hint_range(0.001, 0.05) = 0.012;

// -------------------- Noise helpers --------------------
float hash11(float p){ return fract(sin(p)*43758.5453123); }
float hash21(vec2 p){ return fract(sin(dot(p, vec2(127.1,311.7)))*43758.5453123); }

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

float fbm(vec2 p){
	float v=0.0, a=0.5;
	for(int i=0;i<4;i++){ v+=a*noise2(p); p*=2.0; a*=0.5; }
	return v;
}

vec3 apply_color_grade(vec3 col, vec2 uv){
	col *= (1.0 - darkness);
	col = (col - 0.5) * contrast + 0.5;

	vec3 red_tint = vec3(1.0, 0.15, 0.10);
	col = mix(col, col * red_tint, red_strength);

	vec2 d = uv - 0.5;
	float v = smoothstep(0.9, 0.2, dot(d,d)*2.0);
	col *= mix(1.0 - vignette, 1.0, v);

	return col;
}

float ash_layer(vec2 uv){
	vec2 dir = normalize(ash_dir);
	vec2 p = uv * ash_scale * 12.0 + dir * TIME * ash_speed;

	float n = fbm(p);
	float m = noise2(p * 2.5 + 10.0);

	float specks = smoothstep(1.0 - ash_density, 1.0, n*0.75 + m*0.25);
	specks = mix(specks, smoothstep(0.5, 1.0, specks), 1.0 - ash_soft);

	float center_fade = smoothstep(0.95, 0.2, distance(uv, vec2(0.5)));
	return specks * center_fade;
}

float lightning_mask(vec2 uv){
	float tick = floor(TIME * 4.0);
	float event_r = hash11(tick * 12.345);
	float active = step(1.0 - lightning_rate, event_r);

	float top = smoothstep(lightning_height, 0.0, uv.y);

	float bolt = 0.0;
	for(int i=0;i<3;i++){
		float x0 = hash11(tick*(31.7+float(i)*9.13));
		float w  = bolt_width*(0.7+0.8*hash11(tick*(77.1+float(i)*3.2)));

		float jag = fbm(vec2(uv.y*18.0 + TIME*2.0, x0*10.0)) - 0.5;
		float dx  = abs(uv.x - (x0 + jag*0.03));
		float core = exp(-dx / max(w, 0.0001));

		float falloff = pow(top, 1.5);
		bolt += core * falloff;
	}

	float flash = 0.35 + 0.65 * hash11(tick * 91.9);
	return active * clamp(bolt + flash * top * 0.35, 0.0, 1.0);
}

void fragment(){
	vec2 uv = SCREEN_UV;

	// ✅ ahora sí: sampleamos la captura de pantalla
	vec4 base = texture(SCREEN_TEXTURE, uv);

	vec3 col = apply_color_grade(base.rgb, uv);

	float g = (hash21(uv*vec2(1200.0,700.0) + TIME*60.0) - 0.5) * grain;
	col += g;

	float ash = ash_layer(uv);
	col = mix(col, col + vec3(ash)*0.30, ash);

	float lm = lightning_mask(uv) * lightning_intensity;
	col += vec3(1.0, 0.65, 0.55) * lm;

	COLOR = vec4(clamp(col, 0.0, 1.0), base.a);
}
