What can I do to make the hand hold the grip of the gun even while the character is bending downward or upward?

Overall hand IK is a suggested destination.
There is no guarantee what so ever that a given skeleton is able to reach the goal accurately all the time.

You probably want to make a better aim offset animation for grip like weapons so as to mitigate the issue a bit.

But the most important part is likely going to be (like IRL) to tailor or fit the stock of the weapon so that the grip can easily be reached by the skeleton/structure of the specific character.

If you are looking to make characters modular, you probably need to shrink down the weapon size to some arbitrary number or make the grip be a lot less forward than you’d need for larger (longer arms wide shoulder) characters.

Also fwiw an aim offset tracks all bones in an animation and interpolates mathematically the position of the different poses you set. Not “just the spine”.

Last but not least.

With first person you need to look down sight and make AO poses that are precise before you can run any IK.
With third person no one cares. Simply adjust the weapon to be in the hands at all times (socket it to the forward grip hand not the stock). And you are probably fine to always have the back hand (right for normal people) use IK to adjust in all skeleton sizes.