I'm exact same way, hit the gym for half a decade 6 days a week right before bed. No issue sleeping, but put me in front of a intense video game before bed time and I'm up for hours.
Many people can exercise directly before bed to good effect (as sibling comments note). Many people can't. The best "sleep hygiene" recommendation is to try it both ways and see which works best for you.
At least 3 hours before bedtime is usually a good practice, as others noticed though it may be possible for one to exercise just before and have a good nights sleep.
Exercising in the evening is optimal for muscle growth as power is maximal[0] and it's better for recovery, though there are other factors such as daily schedule and I've often heard people saying that working out at morning has a good psychological effect.
You hit on it with the last point. The best program is one you will stick to. Mine is 20 minutes of gym time every morning at whatever intensity I can muster, and 20 mins of walk during my commute. Every day. If I’m hungover or feeling down, I dual down the workout. But I do it every day so it’s part of my lifestyle and sustainable.
This applies to diets too. The best one is the one you can stick to.
I tried min maxing working out, but I found that it was much better to just do the schedule that would work best for me instead of the one that would work best for my body. I now exercise first thing in the morning.