Which rules wins is the rule that is the most specific. This article explains it as having 'the most hits'. Whatever that means.
Not just most specific, though. IDs are weighted higher than classes, so if there's a tie between 2 rules, an ID wins over a class, even if it comes first. If 2 classes (or things weighted the same as classes) tie, the later rule wins. https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
.foo.foo { color: blue; }
.bar { color: red; }
I’m blue