Image copyright Getty Images Image caption The election was held in Trump country

A special congressional election in Pennsylvania has given President Donald Trump a bloody nose and offers some key lessons for both parties.

The final results aren't in yet - Democrat Conor Lamb clings to a lead of around 500 votes out of more than 200,000, with a few thousand absentee ballots left to count.

But the massive swing against the Republicans - Trump won here with ease in 2016 - means we can learn plenty.

Both parties are closely watching the results to glean any insight into the mood of the electorate with the November mid-terms looming.

Here are five key takeaways:

Image copyright Getty Images

A coming Democratic wave

The party that holds the White House usually suffers at the ballot box, particularly if the incumbent president is unpopular.

Only two times in modern recent history has this not been the case - 2002, just a year after the September 11 terrorist attacks, and 1998, when congressional Republicans were in the midst of their drive to impeach President Bill Clinton.

It's looking increasingly like, despite a growing economy and relative calm overseas, Democrats are going to have a very, very good autumn.

Whatever the outcome of the Pennsylvania race, the simple fact that it was close is stunning. This was a congressional district drawn by state Republicans in 2011 to be a safe seat for their party. Donald Trump won there by nearly 20 points in the 2016 presidential election. In 2014 and 2016 Democrats didn't even field anyone.

According to the political website FiveThirtyEight.com, the Pennsylvania results fit with a Trump-era trend of Democratic candidates outperforming the partisan "lean" in their races by an average of 17 points.

If you're a Republican, that is very concerning news.

The skinny version: There are more than a hundred Republican-held congressional districts across the country that have a narrower margin than 17. If seats that look like this one in Pennsylvania are toss-ups in November, it's going to be a bloodbath.

Trump-haters are motivated

The president campaigned for Republican Rick Saccone twice, including just a few days before the election, but if it helped at all, it didn't help enough.

This was a district where the president is favourably viewed. As in the Alabama Senate special election last December, however, he wasn't able to pull his chosen candidate across the finish line.

Mr Trump, in fact, wasn't much of a topic of discussion in this race. Mr Lamb largely avoided direct criticism of the president, instead focusing on local issues and, in particular, the economy.

Where Mr Trump may have had an influence in this race was in motivating Democrats to head to the polls. In Democratic areas of the district, closer to the suburbs of Pittsburgh, turnout was significantly up - and that, in the end, was a key reason why this race was close.

The skinny: Since Mr Trump's inauguration, Republicans have been losing seats - on the state and federal level - even where the president is moderately popular. If he can't help on friendly terrain, that doesn't bode well for Republicans running in places where the president is a political liability.

Image copyright Getty Images

A candidate that fits

Mr Lamb, an ex-Marine and federal prosecutor, campaigned as a straight-talking moderate who supported gun rights, steel tariffs and law and order.

He also opposed the Republican-backed tax cuts passed at the end of 2017, and was for abortion rights and Barack Obama's healthcare reforms.

He successfully appealed to the white working-class voters who had switched from Mr Obama to Mr Trump in 2016, energised Democrats and won over suburban Republicans.

If Mr Lamb seems like a candidate who was hand-selected to cobble together a winning coalition in this district, it's because he was. The former prosecutor was selected to be the party nominee at a state convention, rather than by voters in a primary process.

The Democrats have spent a lot of time looking for Lamb-like candidates - political novices with military backgrounds or other compelling personal stories.

But many of them will have to face primaries where they will be challenged by more liberal candidates.

The skinny: Mr Lamb was Democratic insiders' view of the right kind of candidate in the right place. In the coming months, will Democratic primary voters agree?

A changing of the guard?

Another interesting thing about Mr Lamb was his campaign pledge not to support Nancy Pelosi as Democratic leader in the House of Representatives.

If the party wins a majority of seats in the House come November, that means Mr Lamb wouldn't back the San Francisco Democrat's bid to replace Republican Paul Ryan as speaker of the House - the third-highest position in the US government.

Media playback is unsupported on your device Media caption This is why US gun laws probably won't change

Ms Pelosi, who has long been a villain for Republicans and unpopular among some independent voters, brushed aside a modest challenge to her leadership in January 2017.

Republicans tried to link Mr Lamb to her, but his disavowal of the Democratic leader may have immunised him.

Will other Democratic challengers in Republican-leaning districts follow his lead? If they do, it could put Ms Pelosi on shaky ground even if her party triumphs in the mid-term elections.

The skinny: Democrats have been criticised for the age of their party's leadership. Ms Pelosi, along with potential presidential front-runners Bernie Sanders and Joe Biden, are all in their late 70s. Mr Lamb, at 33, could represent a new generation flexing its muscles.

Republicans feel the heat

Republicans have been scrambling to explain the outcome in Pennsylvania - blaming a less-than-compelling candidate, saying Mr Lamb actually campaigned as a Republican, and explaining low turnout by boundaries being redrawn.

But behind closed doors, Republicans are very concerned. House Speaker Ryan, for instance, reportedly warned his colleagues that they would have to fight for their political lives in November.

But how many will decide they want to? Dozens of prominent Republicans have opted to retire rather than face an uphill re-election battle.

David Jolly, a former Republican congressman from Florida, said he had been contemplating a political revival, but Pennsylvania had put him off.

Eight months until the mid-terms, Republicans are debating their strategy and message.

Do they go all-in on Trumpism and seek to make cultural and social issues a central focus? Do they try to tout the economy and the benefits of their one major legislative accomplishment, the tax law?

The skinny: History is not on the Republican Party's side. In 2010, Democrats - watching their own set of special election defeats - saw a wave coming and scrambled to find a way to protect themselves. They ended up losing 63 House seats, six Senate seats and six governorships.