California’s primary elections on June 5 will be one of the most consequential — and bizarre — contests of the entire primary season.

The Golden State is crucial to Democrats’ effort to retake the House — Republicans currently hold a whopping seven districts Hillary Clinton won, and Democrats are eager to pry them away.

The bizarreness, though, stems from California’s extremely unusual “top two” primary system — which pits all candidates of all parties against each other and lets only the first- and second-place finishers move on to the general election.

Often, the top two finishers are one Republican and one Democrat, setting up a normal partisan general election contest. But the top two can also be two candidates from the same party, which would lock in the partisan outcome of a race months in advance.

This year, Democrats are anxious that they will end up shut out of several key House races where they have multiple candidates running — which could badly hurt their chances of retaking the chamber. Republicans, meanwhile, dread being locked out of the governor’s race, an open contest now that Gov. Jerry Brown is term-limited out. They fear that would depress their voters’ turnout this fall.

So naturally, both parties are furiously trying to game the system. Each is trying to make sure its votes are as concentrated as possible while the other party’s are divided. But those pesky candidates have minds of their own and often defy their own parties’ wishes.

“It has the feeling of one of those civil wars in the Middle Ages, where the king is fighting against barons and there’s multiple alliances that form and collapse,” says Eric McGhee, a research fellow at the Public Policy Institute of California. “It’s a lot less straightforward than just you got your Democrat, you got your Republican. It’s sort of organized chaos.”

The top-two primary system, explained

Californians voted to establish the top-two system by ballot initiative in 2010. They’ve used it since for all elections for Congress, statewide offices, and the state legislature.

The system’s advocates hoped to break up the party-dominated primary process, which, in their view, too often led to two extreme candidates being nominated and being the only realistic choices in the general election.

To understand their thinking, consider that classic political science concept: the median voter theorem. Democrats running in their party’s primary, we’d think, would target the median Democratic voter to try to win. Republicans would do the same for the median Republican voter. But at that stage of the process, no one is incentivized to target the median voter in the state overall — they’re purely concerned with their own parties.

Moderate Republican state Sen. Abel Maldonado, who championed the reform proposal, argued that if all voters were voting in the same primary, candidates would have more of an incentive to moderate, and the actual median voter in the state would have more of a say.

Another potential virtue of top two is in bringing competition even to “safe” seats one party is usually sure to win — pitting, say, two Democrats against each other in Democratic-leaning districts, so that the incumbent doesn’t get too comfortable and voters have some choice in the fall.

Has it worked? Well, a lot of other things happened at the same time — the state adopted a new redistricting system and new state legislature term limit rules, and the California Republican Party broadly declined — so it’s tough to say. McGhee and Boris Shor found in a 2017 paper that Democrats in the state legislature (but not Republicans) did tend to be more moderate, but they weren’t sure top two had caused that.

Additionally, 2016 brought a new top-two milestone, as Republicans failed to put up a serious candidate for the open US Senate seat, and two Democrats, Kamala Harris and Loretta Sanchez, advanced to the general election. In a state with far more Democrats than Republicans, it makes sense that this would happen some of the time, and it’s not an obviously “wrong” outcome — just 29 percent of votes in the primary went to the various Republican candidates, after all.

How top two can go wrong

But there is a way the top-two system can go quite awry. Sometimes, in districts that are politically divided, two candidates from the same party can end up making it through the primary, due to unusual vote splits.

Let’s say there’s a congressional primary where about 51 percent of the vote goes to Republicans and 49 percent to Democrats. One would think a Republican versus Democrat top-two matchup would best represent voters’ preferences in this swing district.

But think of the total primary vote as a pie. The candidates are all trying to get slices of that pie, and whoever gets the two biggest slices wins. How much of the pie each party gets doesn’t matter.

So if that 51 percent for the GOP is split among two candidates and that 49 percent for Democrats is split among, say, four candidates — then the first- and second-place finishers could well be the two Republicans.

This isn’t just hypothetical — it happened in California’s 31st District in 2012. The two Republicans got 26.7 percent and 24.8 percent of the vote. The top Democrat got 22.6 percent, with the rest of the vote (totaling 25.9 percent) being split among three other Democrats, so the party was shut out of the general election for the House in a district Barack Obama ended up winning handily.

When the chances of a top-two lockout grow

Now, a major party being locked out of a swing district isn’t a common outcome — in fact, the above example is the only time it’s happened in a competitive congressional district this decade.

But the chances of a swing district lockout tend to be highest in two types of races. The first type is open-seat contests, in which the vote on either side could be split among multiple candidates. The second type is races with troubled incumbents who’ve drawn a significant challenger from their own party — because, after all, there needs to be a second candidate of note in the race for a party to win both top slots.

Even then, a lockout is only likely when the race happens to develop in a very particular way — in which one or both parties end up having too many evenly matched candidates, so that no one candidate in a party manages to get a big enough slice of the pie for him- or herself. (This issue is often shorthanded as “too many candidates,” but the real issue is whether the candidates are evenly matched — they won’t affect things if they can’t draw much of the vote.)

Think of the pie again, in a district with about 50 percent Democratic voters and 50 percent Republican (that is, each party has about half the pie).

If each party has two evenly matched candidates, the vote would be split something like 25 percent for each Democrat and 25 percent for each Republican. A lockout on either side is possible, depending on which two candidates emerge with narrow leads.

But if the Democrats instead have three evenly matched candidates, their 50 percent would be split so that they get 16.6 percent each — meaning they’d be locked out if the two Republicans still got 25 percent each.

As the discrepancy between evenly matched candidates on each sides rises, so do the chances of a lockout. Four evenly matched candidates in a party would get 12.5 percent each. Five would get 10 percent each. Six would get 8 percent each.

Actual vote results are of course messier and unlikely to be so exactly matched, but this gives you a sense of the math of how problematic it can be for one party to have more evenly matched candidates on its side than the other.

The biggest risks of House swing district lockouts

Last week, I asked a Democratic operative focused on these contests to rank the races in which the party was most afraid of being locked out. Speaking on background, the operative said there’s one race in particular where the danger seems highest, two more in which it also seems serious, and then two others where it’s a more remote possibility.

1) The 48th District (Rep. Dana Rohrabacher): Hillary Clinton won this Orange County district by 1.7 percentage points, and the 30-year GOP incumbent, Rohrabacher, is plagued with scandal over his ties to Russia, so this seems like a top pickup opportunity for Democrats.

But the operative told me this race has “by far the most danger” of a lockout. That’s because of Rohrabacher’s very weakness — he’s drawn one serious Republican challenger, former state Assembly member Scott Baugh. The worse Rohrabacher does and the better Baugh does, the better the chances of a Republican-versus-Republican general election.

Several Democrats, meanwhile, are splitting their party’s section of the pie into small slices. Real estate investor Harley Rouda and stem cell researcher Hans Keirstead appear to be the leaders in a nasty primary, and several other Democrats are in the race too.

2) The 39th District (retiring Rep. Ed Royce): Royce’s retirement set off a stampede of candidates into this other Orange County district Hillary Clinton won by 8.6 points, on both sides — there are six Democrats, seven Republicans, and four independents on the ballot.

On the Republican side, Young Kim is generally believed to be the frontrunner and likely to win one of the top two spots. But Democrats appear to be split among self-funding lottery winner Gil Cisneros, self-funding retired insurance executive Andy Thorburn, pediatrician Mai Khanh Tran, and former Obama administration official Sam Jammal.

3) The 49th District (retiring Rep. Darrell Issa): The Democratic operative I spoke to thought the chance of a Democratic lockout was comparable here to in the 39th, but that a Republican lockout was possible as well in this San Diego-area Clinton +7.5 district.

There are four prominent Democrats in this expensive contest — real estate investor Paul Kerr, nonprofit CEO (and billionaire’s granddaughter) Sara Jacobs, environmental lawyer Mike Levin, and retired Marine Col. Doug Applegate. And none of them seems to have clearly broken away from the pack yet.

Yet there are also eight Republicans on the ballot and no obvious frontrunner or fundraising star among them — in fact, each Democrat is better funded than any of the Republicans running. So while there’s a risk here for Democrats, there’s also a potential upside.

4) The 10th District (Rep. Jeff Denham): This Clinton +3 district in central California is a top-tier target for Democrats in the general election. But it hasn’t gotten too much attention in discussions of potential lockouts, since Denham appears likely to get the support of most GOP voters.

Still, the Democratic operative told me, it’s “not without risk of a lockout. The chance is there.” That’s because there is one Republican on the ballot in addition to Denham — Ted Howze, a former city council member. Democrats, meanwhile, have six candidates on the ballot, meaning there’s at least a remote chance Howze could get more than any one of them (though it doesn’t appear likely).

5) The 50th District (Rep. Duncan Hunter): This is a much more Republican-leaning district than those above (Trump won this suburban San Diego district by 15 points), so it’s not as high a priority for Democrats. But the scandal-plagued Hunter is under FBI investigation for potentially misusing campaign funds and his fundraising has been dismal. And he’s drawn two Republican challengers, El Cajon Mayor Bill Wells and business exec Shamus Sayed.

Meanwhile, 28-year-old public affairs consultant Ammar Campa-Najjar and former Navy SEAL Josh Butner are in a nasty Democratic primary, with realtor Patrick Malloy also in the race. But if one of Hunter’s GOP challengers can make serious gains among Republican voters in this red district, an all-GOP general election is a possibility.

Parties and candidates are furiously trying to game the system

Democratic strategists tasked with retaking the House have been worried about lockouts for months — and have been taking steps to try to stop it from happening.

The strategy for a party trying to avoid a lockout is relatively simple — they want to concentrate their own voters while dividing their opponents’ voters.

So, for instance, in the 48th District race, Democrats pressured three of their eight candidates on the ballot to withdraw, so the Democratic vote would be less divided. Then just recently, the Democratic Congressional Campaign Committee threw its backing behind one candidate running, Harley Rouda, hoping to concentrate the party’s support further and lock down one of those slots.

In the 39th District open seat contest in Orange County, there’s even more intrigue. Not only did Democrats recently broker a truce of sorts between two of their top candidates who’d been nastily squabbling, Cisneros and Thorburn, but the party is deliberately trying to divide GOP voters as well:

Democrats are ignoring the frontrunner Republican candidate, Young Kim, believing she’s assured one of the top two slots.

Instead, they’re airing ads attacking the apparent second- and third-place Republican candidates, Bob Huff and Shawn Nelson, from the right. The ads call them tax hikers in an attempt to scare GOP voters away from them. “The strategy is to drive down the second and third Republicans,” the Democratic operative told me.

Meanwhile, one Democratic group even sent mailers attacking the apparent fourth-place Republican, Phil Liberatore, from the left — saying Liberatore loves President Trump and wants a border wall. The goal here, it seems, is to drive GOP voters toward Liberatore rather than his second- or third-place rivals.

The problem for a party trying to game the system in its favor is that candidates often have minds of their own — and interests that differ from those of the party. Several Democratic candidates evidently view 2018 as a golden opportunity to win political office for themselves, and have resisted the party’s entreaties to quit the race for the partisan greater good.

Meanwhile, national Democrats focused on taking the House would dearly love it if two Democrats advanced in the governor’s race — they think GOP voters would be less likely to turn out in November if they have no governor candidate on the ballot. And with both Lt. Gov. Gavin Newsom and former Los Angeles Mayor Antonio Villaraigosa in the race, a Democrat-on-Democrat runoff seems like a real possibility.

Yet Newsom, the frontrunner, has other ideas. He’s lately been airing ads saying that John Cox, the apparent top Republican in a weak GOP field, “stands with Donald Trump and the NRA.” This is pretty obviously an attempt to help Cox advance — attacking him from the left, so as to unite the GOP vote around him. Newsom evidently thinks he’d be assured of winning in the fall against a Republican and that his prospects would be less certain against a Democrat like Villaraigosa.

All of this goes to show that there’s a lot of opportunity for mischief in the top-two system. So much can hinge on whether, say, the fourth-place Republican draws 5 percent of the vote or 10 percent of the vote. Or whether a Democrat and Republican who aren’t even competing for the same voters rank second or third, or third and second. The parties are spending millions to try to get the outcomes they want, or at least to try to avoid disaster — but their best-laid plans could go very awry indeed.