I notice that when I'm tired, dehydrated, or starving, my mind gets razor sharp and I become hyper focused.
I used to think that made me think better, but over time I've realised that sharpness =/= smarter.
Whatever logic I was holding in my head at 2am that I needed to put to screen actually wasn't as amazing as I thought it was, but my tired yet focused brain convinced me that it was genius.
Once I made that connection, I just started leaving TODOs in my comments of roughly what I was going for, and then go to sleep, reassuring my panicked brain that its "genius" can genuinely wait until morning for a clearer perspective.
I liken the experience to potheads who reach some half-baked inspiration that they can no longer put to words once cogent. It was never a great idea, it just interested you a lot in that heightened state.
In short: let go of your ideas. If they're truly amazing they'll still be there in the morning with a fresh pair of eyes.