It's hard to define because there is no hard dividing line between "annoyingly perfect mary sue", "great, flawed character", "hilariously cringe edgelord" or "justifiable power fantasy".
Batman is a mary sue. He is one of the richest men in the world, AND the "world's greatest detective", AND one of the best martial artists, AND an expert in practically everything. He has a cool secret cave with special tech, he can dress up in a bat costume and nobody laughs; everyone acts like it's genuinely cool looking and intimidating, and sexy women all find him extremely attractive. He's a grumpy guy who pushes people away, but everyone always wants to be friends with him and dedicate themselves to his cause, including his best friend, the super-powered alien guy who invited him to the secret club where they all hang out on a cool space station. He's above the law, and even though his violent crusade against criminals never seems to result in Gotham city being a safe or pleasant place to live, people all seem to agree he's a necessity.
Batman is also one of the world's most beloved and popular characters. People love Batman!
When writing fiction, there's kind of this line that exists between "This character is who I wish I was", and "This character is who you or I would actually be in that scenario", and the line is often simply how much dignity that character is given. The character you wish you were is a power fantasy, and when they wear a cloak, it billows in the wind dramatically and everyone thinks it looks awesome. The character you or I would actually be in that scenario is relateable and when they wear a cloak, it kind of... flops around and gets in the way, and people laugh at them for being pretentious.
So the best general guideline is to aim somewhere in the middle, where the character gets to be cool now and then and have people respect them and find them attractive or think their jokes are funny or their threats are genuinely intimidating rather than a bit dorky.... but then also occasionally let them not quite be as cool as they want; let them be clumsy or awkward, or make a joke that doesn't quite land.
But you need to be aware there's another ingredient: People hate other people's power fantasies.
Part of why Batman, James Bond, Wolverine, Superman are so beloved and not labelled Mary Sues, is that they're the power fantasy of the group that have had dominance over the media and media criticism world for a long time: Cishet White Men. So to the audience whose word is taken the most seriously and who have the most buying power, these power fantasy characters are allowed, because the most dominant group says they're fine, and anyone who says otherwise is marginalised, so their voice is quiet, on the sidelines.
So what happens when somebody takes a chance and makes a character with that level of power and agency and the same biased, respectful treatment from the narrative, but you have it be a woman, or a black person? "UGH. SO cringe! Unrealistic! This character just gets powers from nowhere! You want me to believe everyone finds them attractive? Psh, they just become the world's greatest crime fighter without working for it at all! Total Mary Sue!"
Anakin Skywalker can be so good at mechanics and piloting that he builds and pilots a winning pod-racer at age eight. And a sentient robot! Without any believable source for that level of training. He can then fly a fighter craft well enough to avoid damage and destroy an entire fleet of killer robots....by accident/narrative convenience... and then fly the craft out of a cramped ship, avoiding death and fly and land a space ship home.
But heaven forbid that Rey is good enough at age 19 or so after years of taking space ships apart and piloting them, to be okay at flying and repairing a spaceship! That's unrealistic!
If you're only aiming your work at a very specific audience, you can get away a lot more with making your character a fantasy for that audience, whether they're incredibly powerful, incredibly sexy or always right and always taken seriously. But if it's aimed at a "general audience", if you don't want a character to be called a mary sue, and the character is a woman or PoC, you'll always need to make them extra flawed to compensate. You'll need to show them messing up a lot more, they'll need to work harder to prove how they "earned" any skills or authority they have. Because power fantasies about people like you are cool, but power fantasies about people not like you, especially if on some level you think those people are inferior, weaker, less capable, are "annoying", "unrealistic" and "ridiculous".