vec3 GetSkyColor(vec3 lightCol, float NdotU, vec3 nViewPos, bool isReflection) {
    float timeBrightnessInv = 1.0 - timeBrightness;
    float timeBrightnessInv2 = timeBrightnessInv * timeBrightnessInv;
    float timeBrightnessInv4 = timeBrightnessInv2 * timeBrightnessInv2;
    float timeBrightnessInv8 = timeBrightnessInv4 * timeBrightnessInv4;

    float NdotSp = clamp(dot(nViewPos, sunVec) * 0.5 + 0.5, 0.001, 1.0);
    float NdotS = NdotSp * NdotSp;
    NdotS *= NdotS;

    float absNdotU = abs(NdotU);

    vec3 skyColor2 = skyColor * skyColor;

    vec3 sky = mix(sqrt1(skyColor) * 0.47, skyColor2 * 0.9, absNdotU);
    sky = sky * (0.5 + 0.5 * sunVisibility) * skyMult;

    #ifdef ONESEVEN
        sky = vec3(0.812, 0.741, 0.674) * 0.5;
    #endif

    float horizon = 1.0 - max(NdotU + 0.1, 0.0) * (1.0 - 0.25 * NdotS * sunVisibility);
    horizon = min(horizon, 0.9);
    horizon *= horizon;
    
    float lightmix = NdotS * max(1.0 - absNdotU * 2.0, 0.0) * 0.5 + horizon + 0.05;
    lightmix *= sunVisibility * (1.0 - rainStrengthS) * timeBrightnessInv8;

    sky *= 2.0 - 0.5 * timeBrightnessInv4;
    sky *= mix(SKY_NOON, SKY_DAY, timeBrightnessInv4);
    
    float mult = 0.1 * (1.0 + rainStrengthS) + horizon * (0.3 + 0.1 * sunVisibility);
	
    vec3 finalSky = mix(sky * (1.0 - pow2(lightmix)), lightCol * sqrt(lightCol), lightmix);
    
    vec3 nightSky = ambientNight * ambientNight * (3.25 + 2.25 * max(sqrt1(NdotU), 0.0));
    nightSky *= mix(SKY_NIGHT, 1.0, sunVisibility);
    finalSky += nightSky;


    // Event Horizon atmosphere: a broad golden/red aureole around the low sun,
    // a subtle violet anti-twilight arc, and a genuinely dark midnight zenith.
    float sunHeight = dot(sunVec, upVec);
    float daylight = smoothstep(-0.08, 0.12, sunHeight);
    float twilight = smoothstep(-0.16, -0.015, sunHeight)
                    * (1.0 - smoothstep(0.075, 0.34, sunHeight))
                    * (1.0 - rainStrengthS);
    float sunFacing = max(dot(nViewPos, sunVec), 0.0);
    float antiSunFacing = max(-dot(nViewPos, sunVec), 0.0);
    float horizonBand = pow(clamp(1.0 - absNdotU, 0.0, 1.0), 1.35);
    float wideAureole = pow(sunFacing, 4.0);
    float tightAureole = pow(sunFacing, 24.0);
    vec3 twilightColor = mix(vec3(1.00, 0.34, 0.075),
                              vec3(1.00, 0.16, 0.045), mefade);
    finalSky += twilightColor * twilight * horizonBand * TWILIGHT_GLOW
              * (0.028 + 0.13 * wideAureole + 0.20 * tightAureole);
    vec3 antiTwilight = mix(vec3(0.10, 0.025, 0.12),
                             vec3(0.16, 0.018, 0.09), mefade);
    finalSky += antiTwilight * twilight * horizonBand * TWILIGHT_GLOW
              * pow(antiSunFacing, 5.0) * 0.10;

    float nightAmount = 1.0 - daylight;
    vec3 midnightFloor = mix(vec3(0.00013, 0.00024, 0.00062),
                             vec3(0.00042, 0.00068, 0.00155),
                             pow(clamp(1.0 - absNdotU, 0.0, 1.0), 1.5));
    midnightFloor *= SKY_NIGHT;
    finalSky = mix(finalSky, midnightFloor, nightAmount * 0.72);

	float reflectionSkyMask = 1.0;
	if (isReflection) {
        float invNdotU = max(-NdotU, 0.0);
        float groundFactor = 0.5 * (11.0 * rainStrengthS + 1.0) * (-5.0 * sunVisibility + 6.0);
        float ground = exp(-groundFactor / (invNdotU * 6.0));
        ground = smoothstep(0.0, 1.0, ground);
        reflectionSkyMask = 1.0 - ground;
        mult *= reflectionSkyMask;
    }

    // Keep midnight readable at the zenith without lifting the reflected
    // ground hemisphere or flattening the naturally brighter horizon.
    float nightHemisphere = smoothstep(-0.02, 0.10, NdotU);
    float nightSkyLift = nightAmount * nightHemisphere * reflectionSkyMask * NIGHT_SKY_LIFT
                       * mix(0.22, 0.34, horizon);
    mult = max(mult, nightSkyLift);

    //duplicate 98765
    vec3 weatherSky = weatherCol * weatherCol;
    weatherSky *= GetLuminance(ambientCol / (weatherSky)) * 1.4;
    weatherSky *= mix(SKY_RAIN_NIGHT, SKY_RAIN_DAY, sunVisibility);
    weatherSky = max(weatherSky, skyColor2 * 0.75); // Lightning Sky Color
	weatherSky *= rainStrengthS;
    finalSky = mix(finalSky, weatherSky, rainStrengthS) * mult;

    finalSky = pow(finalSky, vec3(1.125));

    return finalSky;
}
