I know marathon sessions are part of coding culture, probably because that goes with the complex territory.
I've always thought 'coders insomia' must be very common as a result. I'd be surprised if anyone that codes for a living hasn't experienced this.
The challenge is how to get back in control of your brain and switch it off.
I think the only way is to force yourself to do something more mundane for at least several minutes before sleep, but that's a real wrench I know.
At least you're not alone... as I'm sure the rest of the comments will reveal.
Yeah, doing other stuff doesn't keep me up so late lol