I admittedly don't have a perfect solution, but I think the status quo is fairly extreme either way. I see your point that ball security should matter more near the goalline and just giving them the ball back at the spot where the ball went out of bounds would probably have the exact opposite effect.
The Ringer has a few suggestions
here
a) The opposing teams gets the ball at the spot where the ball went out
b) Offense retains possession, but gets the ball at the XX yard line (25? 30?)
c) One untimed down from the spot where it went out, if the offense gets into the EZ, they get the ball at the spot where it went out, if the defense keeps them out they get the ball at the 20/25