For all their ills, the Chargers are in first place.

Justin Herbert is why.

Herbert seems to have gone to school on how AFC stars Patrick Mahomes and Josh Allen overcome pass rushers. In his sixth NFL season and second year under ex-NFL quarterback Jim Harbaugh, the quarterback is solving problems with rare anticipation and reactions.

How was Herbert able to defeat a fierce pass rush Sunday and thus enable Ladd McConkey’s long gain, Cameron Dicker’s decisive field goal and the 29-27 road victory over the Miami Dolphins?

Let’s start here: Being 6-foot-6, agile and strong gave him an edge.

Before the ball was snapped, Herbert saw that the Dolphins’ top-two pass rushers were lined up together to his right.

When Herbert took the shotgun snap and dropped back, his height improved his ability to feel the pressure to his right yet also gauge developments upfield.

At the top of Herbert’s drop, the Dolphins’ frontside duo — ends Jaelan Phillips and Chop Robinson — already were closing fast.

If Herbert paused, both men would have a clean shot at him. But, surging forward, he avoided a direct hit while also securing the ball with both hands.

Perhaps Herbert, who was making his 88th career start, made two savvy guesses: that Phillips would win fast against rusty guard Mekhi Becton, and that Robinson would blow by tackle Bobby Hart, who was signed off the street Tuesday.

But Herbert wasn’t safe yet. Phillips made a sharp right turn, putting near Herbert’s right hip.

Lunging, the 6-foot-5, 263-pounder got two long arms around Herbert’s torso.

Herbert shook him off, denying the UCLA alum his 25th career sack, righted himself and flipped a pass to McConkey.

Somehow, Miam had failed to cover McConkey on a shallow crossing route before and after he and rookie tight end Oronde Gadsen criss-crossed.

McConkey was alone near the left sideline when he caught Herbert’s toss.

He evaded a rookie safety, got a downfield block from running back Hassan Haskins and darted out of bounds, gaining 41 yards.

Ballgame.

Dicker’s 33-yard kick, rewarding JK Scott’s reach grab and placement, went through with five seconds left.

“This was a must win for us,” said McConkey.

He wasn’t far off.

Losing to the Dolphins (1-5) before the schedule toughens would’ve stretched the team’s losing streak to three games. The Chargers (4-2) would’ve fallen behind the Broncos (4-2) instead of matching them, while also holding the tiebreaker.

What happens if Herbert, instead of wrecking Phillips’ and Robinson’s sack plan, had been downed on that second-and-10 try from L.A.’s 41 with the Dolphins ahead 27-26?

The Chargers would’ve called their final timeout. About 30 seconds would’ve remained. They would’ve needed 20-plus yards, just to give Dicker a 63-yard try.

Let’s get repetitious here.

In the 2020 NFL draft, Dolphins general manager Chris Grier erred big time, benefiting the Chargers, when he chose Tua Tagovailoa fifth overall.

Tom Telesco and John Spanos didn’t turn down the gift. They took Herbert with the next pick.

Jim Harbaugh was right in believing Justin Herbert could fix many problems within an NFL game. No matter how the Chargers’ job works out for the head coach, who left a plush job at Michigan to take over an underachieving franchise, Harbaugh wasn’t wrong about Herbert.

Much like Herbert demoralized the Dolphines, he zapped Broncos defenders last month in the Kroenke Dome, rallying his team toward the win.

The Broncos, for now, have the better roster. But the Chargers have the better QB and stand to regain two injured stars in left tackle Joe Alt and edge rusher Khalil Mack, plus running back Omarion Hampton.

John Harbaugh doesn’t have it so good as his younger brother. His Ravens lost Sunday at home to the Rams, 17-3. Benefiting the Chargers, the Ravens — an AFC title favorite entering the season — are 1-5.