Kevin McCarthy has some explaining to do.

As the California Republican prepares to lead the House GOP in the minority, there’s an unavoidable irony that Republicans in his state took the biggest beating in the election.

In one of the last races of 2018 midterms to be called, California Republican Rep. David Valadao lost reelection to Democrat T.J. Cox this week, becoming the 40th Republican to see their seat flip this election cycle — and seven of those flips were in California.

It was a shellacking, and McCarthy played a pivotal role. For the past two years, he cultivated a close relationship with President Donald Trump. As the House majority leader, McCarthy encouraged even the most vulnerable California Republicans to stay in lock step with the party.

California Republicans overwhelmingly voted for the House Obamacare repeal bill that would have devastated the state’s health care system, and the GOP tax law that disadvantages taxpayers in states like California the most. McCarthy allowed Trump’s hardline immigration agenda to wash over the party, even as California Republicans watched their districts diversify.

In May, I wrote that McCarthy’s allegiance to Trump and Trump’s agenda would likely stand between him and the speakership. At the time, Kurt Bardella, a former Republican congressional aide and Breitbart News staffer who now identifies as a Democrat, made a prediction:

“The next speaker might be from California, but it’s not going to be Kevin McCarthy,” Bardella told me.

Seven months later, McCarthy is the face of crushing defeat. Democrat Nancy Pelosi is poised for the speakership, and he’s the minority leader.

McCarthy was bullish about Republicans chances in the midterms. He was wrong.

The signs of a blue wave crashing on California came early, when Reps. Darrell Issa and Ed Royce, Republicans in districts Hillary Clinton won, announced their retirements. But McCarthy refused to acknowledge it.

“You know, when I look at retirement, I look at retirements for the Republicans who retired in seats that Hillary Clinton carried and Democrats who retired in seats that Donald Trump carried — you know what that number is? Five to four,” McCarthy said in an interview with GOP strategist Frank Luntz at a Milken Institute event in May, predicting House Republicans would keep the majority. “We’ll pick up two seats in Minnesota.”

Republicans did end up flipping two seats in Minnesota, but Democrats did too. The slew of Republicans retiring in 2018 weren’t just blips on the radar; they opened doors for Democrats across the country, including in California, where Democrats Mike Levin and Gil Cisneros were able to pick off retiring Issa and Royce’s seats in suburban southern California.

It didn’t stop there. Only seven of the 14 Republicans in the California congressional delegation will be returning to the House in January. Democrats flipped districts that Trump won, campaigning on health care, immigration and against the Republicans’ tax bill.

McCarthy has had to change his tune.

“There is no question there is work to be done after these election results and we intend to grow,” McCarthy said in a statement.

McCarthy is a Trump man. Trump’s agenda is really bad for California.

Three policy fights — health care, tax reform, and immigration — have defined Trump’s first two years in office. McCarthy has championed the party’s position on all of them and worked to keep his Republican Californian colleagues in line.

All 14 California Republicans voted in favor of the House’s Obamacare repeal bill, a proposal that would have cut $880 billion from Medicaid, a program that covers 13.4 million Californians in the nation’s largest Medicaid program. To put that in context, 50 percent of Republican Rep. David Valadao’s constituents rely on the program, in a Central Valley district that is centered in the San Joaquin Valley and more than 70 percent Hispanic. Valadao lost reelection.

All but two — Issa and Rep. Dana Rohrabacher (R-CA) — voted for the GOP tax law. The law caps the income and property state and local deduction at $10,000 and lowers the mortgage interest deduction for newly issued loans from $1 million to $750,000.

In places like Rep. Mimi Walters’s affluent suburban Orange County district surrounding Irvine, for example, 46 percent of residents take the state and local tax deduction, according to the Tax Policy Center. The median home price in her district is $685,800. Only 36 percent of likely California voters approve of the Republican tax law, whereas 58 percent disapprove. Walters and Rohrabacher both lost reelection. Issa’s seat also went to a Democrat.

The dangers of sticking with Trump’s agenda were perhaps most visible on immigration, an issue on which Trump has pulled the Republican Party far to the right and that has seen substantial pushback from California’s state government.

Growing minority populations in historically white regions like Orange County substantially narrowed Republican advantages in traditionally more conservative districts. In the Central Valley, deportations and the fear of deportation have threatened local economies. According to an October poll by the Public Policy Institute of California, 52 percent of California’s likely voters support local and state government policies to protect the legal rights of undocumented immigrants.

“Immigration is their Achilles’ heel; it’s why [Republicans] will never be able to win elections,” Bardella told me in May. “It is a measurable trajectory on how the demographics have changed in California. Particularly on immigration ... it confounds me how unwilling they are to change their politics.”

Both Valadao and Rep. Jeff Denham, who represent largely Hispanic districts in the Central Valley, where deportations are highly visible, tried to hold more moderate positions on immigration, but neither were able to actually push policy, which Democrats saw as an act of deference to Republican leadership. Both lost reelection.

McCarthy’s tainted ascension

McCarthy easily won reelection in his conservative stronghold centered in Bakersfield, California. He has always been more of a party man than a policy mastermind; a prolific fundraiser, who in the final weeks of the election transferred $5.7 million to party groups to get Republicans over the finish line.

It wasn’t enough.

For the past decade, McCarthy, who has had clear personal ambitions for the speakership, has seen the Republican delegation from California shrink from 19 seats in 2010 to just seven.

The state’s demographics are shifting further and further away from Republicans, and the state is becoming more and more entrenched in a culture of partisanship.

The outgrowth is a GOP that clings to the poles of the party, a reality McCarthy very clearly represents. Trump still has an 80 percent approval rating among California Republicans, despite sitting at around 36 percent approval statewide as of October.

Democrats control every statewide office and have supermajorities in both the chambers of the state legislature. A 2014 study from the American Political Science Association showed California is by far the most polarized state legislature in the country. And because of the open primary system, California Republicans were unable to field a Senate candidate in the general election in 2018.

McCarthy may have been picked to be the top House Republican. But his state played a big role in making him the minority leader.