CGLearn: Consistent Gradient-Based Learning for Out-of-Distribution Generalization