“MARINE, prosecutor, patriot, Catholic. Democratic nominee for Congress in the #PA18 special election on March 13th, 2018.” Conor Lamb’s description of himself on Twitter sums up neatly why the 33-year-old newcomer to local politics is the perfect candidate for a congressional district in south-western Pennsylvania that is overwhelmingly white, socially conservative and working-and middle class. His background, youth and soft-spoken demeanour seem to be straight out of central casting for a successful congressman in an area that used to vote for the Democrats but fell out of love with a party seemingly out of touch with voters’ concerns when in 2002 it voted for Tim Murphy, a socially conservative but otherwise moderate Republican, who was re-elected seven times.

With nearly 100% of votes counted at close to midnight locally Mr Lamb was clinging to a 579-vote lead over Rick Saccone, his Republican opponent. But a few thousand absentee ballots were still outstanding, which means that no winner will be declared until March 14th, at the earliest. And the trailing candidate can request a recount, according to state law.

The special election was watched all over the country and even internationally. It matters so much because a Democratic victory in an area that is considered Trump country is seen as a bellwether for the midterm elections in November. If Democrats can win in a district where President Donald Trump won with almost 20 percentage points over Hillary Clinton, they can also hope to win on April 24th in Arizona’s 8th district (which Mr Trump won with 21 points) and on May 8th in Ohio’s 12th district (which Mr Trump won with 11 points), the next special elections for a House seat. It also matters as a lesson for the Democrats: voters in the heartland of America flock to centrist candidates who talk about their pensions, health insurance and gun rights and who demonstrate granular knowledge of their concerns.

Mr Lamb’s success is an embarrassing setback for the GOP. Since Mr Trump won the presidency Republicans have lost a gubernatorial election in Virginia and a race for a Senate seat in Alabama. But they expected the race in Virginia to be tight and Roy Moore, their candidate for the Senate, faced multiple credible accusations of molesting teenagers. State legislative elections get far less attention than national races, which is why the 39 red-to-blue flips in state elections in Florida, Missouri and Wisconsin had not set the GOP’s alarm bells ringing yet. This changed with Mr Lamb’s unexpected surge.

What happened? “Candidates matter,” says Robert Speel of Penn State University. The Democrats had a telegenic candidate who played a good ground game, knocking on doors in particular in areas with undecided voters (with his clean-cut looks the practising Catholic was at times mistaken for a Jehovah’s Witness). He made savvy use of social media with, for instance, well-curated video clips on Facebook. He was careful not to make the vote a referendum on Mr Trump, who is still popular in the district. And he turned out to be a formidable fund-raiser, outraising his opponent by a margin of six to one with a haul of some $5m.

The Democrats’ other great strength was newly energised support from the unions. Around 86,000 union members live in the 18th district; union leaders say one in five district votes comes from a labour household. Many of them voted for Mr Trump, who did 13 points better in union households than Mitt Romney, the Republican presidential candidate in 2012. And they used to vote for Mr Murphy, who had moderate views on organised labour and was even endorsed by several unions. But Mr Saccone fell out with union leaders: he is a fervent backer of right-to-work legislation, which prevents unions from requiring workers to pay union dues, thus weakening them financially and politically.

Mr Lamb spent the last days of his campaign at rallies with unions. On March 9th he rallied with steelworkers in Pittsburgh (he embraces the proposed tariffs on imports of steel). On March 11th he rallied with coalminers of United Mine Workers in Greene county, which Mr Trump won by a whopping 40%. Mr Lamb promised to protect pensions of union members as well as Social Security and Medicare benefits for all Americans. “We are ready to step up and keep the promises that we made,” he said. Union members responded with a fervour they never showed for Mrs Clinton. Mr Lamb was “a God-fearing, union-supporting, gun-owning, job-protecting, pension-defending, Social-Security-believing, sending-drug-dealers-to-jail Democrat,” enthused Cecil Roberts, the boss of the union.

As Mr Lamb's campaign kept gaining momentum, out-of-state Republican groups came to Mr Saccone’s rescue, funnelling as much as $10m into his lacklustre one. The Congressional Leadership Fund, a PAC with ties to Paul Ryan, the Speaker of the House, forked out $3.5m, and the National Republican Congressional Committee nearly matched that sum. The leadership fund also hired 50 door knockers through a private contractor. But the state legislator, who claims to have been a diplomat in North Korea, has the charisma of a phone book. He failed to show up for door-knocking rounds, seemed ill at ease at public gatherings and made at times amateurish use of social media. One video posted on Facebook showed him at a Christmas party at a bar in Moon Township rambling about the wings, pizza and zucchini on offer.

Most of Mr Saccone’s sparse appearances over the last weeks were closed to the public, such as his meeting with campaign volunteers on March 9th in tandem with Kellyanne Conway, an adviser to Mr Trump. Those that were open to the public attracted only smallish groups of supporters. Mr Trump travelled a second time to Pennsylvania to prop up Mr Saccone on March 10th. During a 75-minute freewheeling speech he talked mostly about himself, insulted several of his adversaries and mentioned Mr Saccone only in passing. (“Personally, I like Rick. I think he’s handsome”.)

As his lead in the race disappeared, Mr Saccone grew increasingly strident. Appearing next to Donald Trump junior, the president’s son, on the eve of the election, he claimed that “the other side” had hatred for the president and the country. “And I tell you some more,” he said. “They have a hatred for God”.

Republicans could have had a stronger candidate than Mr Saccone, says Kevan Yenerall of Clarion University of Pennsylvania. After Mr Murphy, a vocal opponent of abortion, was compelled to resign last autumn in the aftermath of revelations that he encouraged his mistress to abort their unborn child, both parties picked their candidates for the newly vacant House seat through a party committee. Guy Reschenthaler, a 34-year-old state senator who is also a veteran, was expected to get the nomination. But on the second ballot of the committee vote one of the three front-runners, Kim Ward, another state senator, threw her support behind Mr Saccone, who subsequently won handily. Mr Reschenthaler could have matched Mr Lamb in youth and energy—and he has more legislative experience than Mr Lamb does. Still, says Mr Yenerall, even with Mr Saccone the Republican thought they could not lose.

While the Pennsylvania election is likely to be one of the most important races of this year, it is also probably one of the least consequential. The Supreme Court of Pennsylvania imposed new congressional-district lines for the primaries in May and the general election in November. The current 18th district will mostly be split between a new 14th district and the 17th district. Mr Lamb will presumably run again in the more liberal, suburban 17th district where he would face Keith Rothfus, the incumbent Republican. Mr Saccone had said that he would seek re-election in the strongly Republican 14th district if he won. By the start of 2019 both men (or neither) could be a member of the House. Mr Lamb will not have much time to celebrate. The deadline for applications to run in November is March 20th—and they need to gather at least 1,000 signatures.