Christian and purity

Something I’ve never understood about the Christian faith is waiting until marriage to not do anything sexual(even if it’s masturbation and that can be just for yourself) and to stay pure and if you have any sexual thoughts or very lustful thoughts, that’s seen as a sin. But yet, when you get married, it’s totally fine to have sex and do other sexual things.

I grew up in the Christian faith up until I was in my late teens, and when I learned about what sex was, I always wondered about this because it just simply doesn’t make that much sense to me and I still wonder why it’s seen this way/is this way.