Even before Mr. Trump began thundering against an “invasion” from Latin America — spurning with finality the moderate swing voters that Republicans desperately needed — his party’s facade of election-year unity was crumbling.

Panic mounting, Republican factions began looking out for themselves: Mr. McCarthy, currying favor with the right as he positioned himself to succeed Mr. Ryan, revived a push to fund Mr. Trump’s border wall. On the weekend before the election, Representative Jim Jordan of Ohio, a far-right rival of Mr. McCarthy, began calling colleagues to gauge their interest in supporting him for minority leader in a Democratic-led House.

As Mr. Trump swallowed the already-faint Republican economic message with deafening appeals to right-wing nationalism, Republican campaign chiefs began expressing discomfort with his turn toward raw racial politics. Senator Cory Gardner of Colorado, head of the Republican Senate committee, expressed anxiety to allies that Mr. Trump’s obsession with a migrant caravan could harm his Senate re-election in 2020.

Among White House aides, improvisation and favor-trading overrode any semblance of political strategy. Even Vice President Mike Pence, aides noted, appeared less than deliberate in his approach: When conservative media began rippling with interest in John James, a long-shot Senate candidate in Michigan, Mr. Pence phoned the president from the state and urged him to campaign there. The same day, a public poll found Mr. James losing by 17 points to Senator Debbie Stabenow.

Johnny DeStefano, counselor to the president, told an associate he used “every ounce of capital” to keep Mr. Trump from visiting Michigan and other states where his party was sure to lose.

And in a move that baffled their political peers, both Mr. Pence and Mr. McCarthy visited Kansas City on the Friday before the election to campaign with Mr. Yoder, among other Republicans — more than a month after the N.R.C.C. concluded Mr. Yoder could not win.

Last weekend, the gulf between the two parties — in strategy and message, in political tenacity and internal cohesion — could not have been greater. Meeting with campaign donors in San Francisco, Ms. Pelosi declared confidently that the House majority would soon be in Democratic hands. Next up, she said, would be a “huge amount of work to repair the fabric of the country,” according to a person in attendance who paraphrased her comments.