A stunning setback in a reliably Republican Pennsylvania congressional district has unnerved many GOP elected officials, who are bracing for a potential Democratic wave in the midterm elections without any proven strategy to fight back.

Publicly, party leaders tried to cast the special election as a fluke, won by an idiosyncratic Democrat who ran to the right. But privately, strategists, donors and elected officials expressed alarm, with the November midterms looming. “Horrid” was how one Republican in frequent contact with lawmakers described the political outlook.

“Denial ain’t just a river in Egypt. This is a real problem,” said Rep. Charlie Dent (R-Pa.), who has chosen to retire. “You better have your own brand, your own image, and you better be able to sell it. You can’t count on somebody else dragging you across the finish line in this type of environment.”

As of late Wednesday, Democrat Conor Lamb was the apparent winner, holding a lead of 627 votes over state Sen. Rick Saccone after more than 224,000 ­people cast ballots Tuesday. The race, in a district President Trump won by nearly 20 points in 2016, has not been officially called because state officials continue to count provisional and military ballots, a process that could last until at least March 21.

Republican officials, who are looking for irregularities, have written to each of the counties in the district to demand that they preserve all ballot boxes for a potential recount, as required under state law.

Pennsylvania provides no mechanism for parties or candidates to directly request a recount in this kind of election. Absent a court order, a full recount would need to be requested by a petition from three voters in each of the district’s 593 precincts, said Wanda Murren, a spokeswoman for the Pennsylvania secretary of state.

Republicans had worked for months to turn the House race in Pennsylvania’s 18th Congressional District into a demonstration of GOP staying power, deploying their best messaging ideas, spending more than $10 million and receiving the full backing of Trump in a district where he polls over 50 percent.

None of it worked, and the apparent loss has left the party without a clear Plan B just eight months before midterm elections that threaten to give Democrats control of the House, if not the Senate as well.

“What changes for our strategy? Nothing,” said Corry Bliss, executive director of the Congressional Leadership Fund, a Republican super PAC that spent more than $3.4 million on ads in the race as part of a 2018 campaign budget of more than $100 million. “However, everyone has to do better.”

The dynamics in Pennsylvania continue a clear pattern that began in elections last year, including statewide Democratic victories in Virginia and Alabama. In the era of Trump, Democrats have shown they can drive suburban voters to the polls and generate real liberal enthusiasm for moderate candidates.

Meanwhile, Republicans have found themselves unable to replicate the exuberance that propelled Trump to the White House in 2016, let alone the off-year elections during the presidency of Barack Obama. Now, strategists worry that the outcome will prompt further congressional retirements while driving more money into the coffers of Democratic election efforts this fall.

But in public comments, party leaders played down the significance of the loss, arguing that Lamb ran to the right of most Democratic candidates who are nominated through normal primaries.

“I don’t know that there’s a big surprise,” House Speaker Paul D. Ryan (R-Wis.) said at a news conference. “This is something that you are not going to see repeated.”

Republicans had hoped to demonstrate to their donors the efficacy of a twin messaging strategy that can be employed in other races in the fall. Starting in January, they committed millions to largely unanswered television ads that sought to tie Lamb to House Minority Leader Nancy Pelosi (Calif.), the Democrat who polls showed was poorly regarded in the state. They also blanketed the district with spots promoting the Republican tax cuts passed in December. They also sent dozens of field staffers to the state to turn out the party’s base.

Republican internal polling showed that the popularity of the tax cut rose over the last two months in the district, outstripping gains elsewhere in the country, and Pelosi remained far less popular than Trump in the final weeks of the race. But the strategy did not prevent a massive swing in support to the Democratic candidate.

“We spent an awful lot of money,” said one Republican strategist involved in the race who spoke on the condition of anonymity to talk candidly. “It’s difficult to spin this one.”

Ultimately, Republicans said, it was the effectiveness of Lamb as a candidate and the struggles of the Saccone campaign that doomed the strategy. Lamb, who said early that he did not support Pelosi as a Democratic leader, cast himself as palatable to conservatives, saying he personally opposed abortion and supported Trump’s plan for new trade tariffs. In one campaign ad, he fired a semiautomatic gun.

Trump appeared to reference Lamb in a speech at a fundraiser in Missouri on Wednesday.

“He said very nice things about me. I kept saying, is he a Republican? Sounded like a Republican to me,” Trump said.

“It’s very confusing to voters when Democrats run on traditionally Republican platforms, and I think Saccone’s opponent did that pretty darn effectively,” Rep. Scott Perry (R-Pa.) said.

Ohio Rep. Steve Stivers, head of the House GOP campaign arm, briefed Republican members in a closed-door meeting Wednesday morning, encouraging them to define themselves and their opponents more effectively than Saccone did.

With outside groups included, Republicans spent more on the race than Democrats, but Saccone struggled to raise money.

“This is a wake-up call. If you’re getting outraised, this is a wake-up call. Prepare to bear down,” Stivers said, according to a Republican present for his remarks, who spoke on the condition of anonymity to describe the private meeting.

Stanley Hubbard, a Minnesota broadcasting executive who donated $70,000 to the Congressional Leadership Fund in January, pinned the Pennsylvania result mostly on Lamb being young and telegenic and Saccone looking like “an old-timer.” Still, he said, he also sees a political template Democrats can use effectively in future contests.

“I think the chance the Democrats have is to come back to the middle,” Hubbard said. “If they come back to the middle the way this guy did, they have a chance.”

The district, a stretch of suburbs and small towns that was drawn to elect a Republican has not been competitive in recent years. Obama won just over 40 percent of its voters in 2012, and Hillary Clinton received only 38 percent support in 2016.

The Cook Political Report, which ranks the partisan bias of congressional districts, lists 114 Republican-held seats as more competitive than the district where Lamb appears to have won, a fact that Democrats have seized upon to suggest an electoral wave is coming that will wipe out the 24-seat advantage Republicans hold in the House.

The White House tried to capitalize on Trump’s relative support in the district by sending him in twice for rallies, as well as deploying Vice President Pence, White House adviser Kellyanne Conway and the president’s son, Donald Trump Jr., to tour the district.

“The Economy is raging, at an all time high, and is set to get even better,” the president tweeted Tuesday morning. “Jobs and wages up. Vote for Rick Saccone and keep it going!”

Despite the result, White House aides argued that Trump had a positive effect on the race, citing internal Republican polls that showed Saccone losing by a greater margin than the vote count.

“The Democrat in the race really embraced the president’s policies and his vision, whereas he didn’t really embrace Nancy Pelosi, the Democratic leader,” White House spokesman Raj Shah said.

Mike DeBonis, David Weigel, Seung Min Kim and Erica Werner contributed to this report.