Strange mesh twist after appying armature.

I am working on 4 legged robot. I have mesh and armature prepared, but the issue is when I am applying it to the model i have very strange twist near the robot foot. It’s like something is upside down. The mesh is correct when i change the rotation of bones (i point the z axis to the opposite direction) and the “knee” is bending down. I would like knee to bend up and still have the mesh to deform correctly. How to do this?

Opening your file causes Blender to freeze, so I have no idea.

I downloaded it and opened without any issues.

Bump! Srsly i need help!

select the leg you have a prob with (I think it’s Armature upper_leg.l) and in the constraint make the pole vector @ 0 degree it should solve your prob, but one thing that I would advice you is that when you know you are going to use some IK, it’s better to model your object with a bit of rotation and not having it into a straight line. This way Blender might understand a bit more the way it would bend.