wrong rotation transform on rig
It's a bone roll problem.
The bone rolls differ in your rig. Bones have 3 axes and a roll value. These values determine in which direction for example a limb bends when the bones are rotated around one of the axes in a positive or negative direction.
Select the armature, go in Edit mode, and check the bone roll values. Activate the display of the bone axes to see what happens.
You can select a several bones and use Recalculate Roll (Shift+N) + Active Bone to assign the roll value to all bones. Blender takes the value of the active bone, the last one you selected.

Important! Note that a roll of 180 is not the same as -180. For centered bones make sure the X and Y positions of the head and tail are 0, not -0. I had a blender version (2.91?) that used -0 and the (calculated) roll then was -180 but for 0 it was 180. The result is a mess when several bones have different signed values (+/-).
Here is an example in Pose mode of roll 0 (top) and roll -180 (bottom). For both, the local rotation is in the positive direction of the x-axis of the bones. In the first example, it bends forward, while in the second it bends backward. With a roll value of 90, it would bend to the right.