INITIALLY the mood at Doug Jones’s election-night party was genial but uneasy. Guests knew Mr Jones was closer to winning one of Alabama’s Senate seats than any Democrat in a quarter-century; they also knew that Mr Trump won the state by 28 points, and the last two Republican Senate candidates won 63.9% and 97.3% of the vote. So they smiled, and made all the right hopeful noises, but around the corners of their eyes you could see them bracing for disappointment.

Rural votes came back first; they solidly favoured Roy Moore, the Republican candidate (pictured above, to the right). But as the night wore on, Mr Moore’s lead shrunk. As votes from Alabama’s cities returned, it vanished. When networks finally called the race for Mr Jones, at around 9.30pm local time, the crowd’s scream was almost primal, as much disbelief as joy. Mr Jones captured the crowd’s mood in the first sentence of his victory speech: “I think I’ve been waiting my whole life, and now I don’t know what the hell to say.”

Mr Jones’s victory was narrow—he took 49.9% of the vote to Mr Moore’s 48.4%, with the remaining 1.7% going to write-in votes—but decisive. He flipped every one of the counties that Mr Trump won by 10 points or less last year, banking large numbers of votes in the counties housing Alabama’s five biggest cities, and running up sizable margins in Alabama’s majority African-American “black belt”. Mr Moore, meanwhile, underperformed Mr Trump’s results from November 2016 in every one of Alabama’s 67 counties, faring especially poorly in those with large numbers of educated voters.

The two candidates this time have markedly different demeanours and backgrounds. Mr Jones is genial and steady, Mr Moore preening, sour and angry. Mr Jones ran on policy, talking about “kitchen-table issues” such as health care and education; aside from hating abortion and same-sex marriage, Mr Moore talked about little other than his own piety. Mr Jones was a federal prosecutor who successfully convicted domestic terrorists: two Klansmen who killed four black girls in an infamous church bombing in 1963. Mr Moore was twice removed from his post as Alabama’s chief justice for flouting federal law. He believes homosexuality should be illegal, Muslims should not be allowed to serve in Congress, the Constitution exists to foster Christianity and America was last great “at the time when families were united—even though we had slavery.” Several women have accused him of offences ranging from sexual misconduct to assault; most were teenagers at the time of the alleged incidents.

When those accusations first emerged, other Republicans distanced themselves from him. Mitch McConnell, the Senate majority leader, said he believed the women. Mr Trump’s press secretary called on Mr Moore to “do the right thing and step aside…if those allegations are true.” But as Mr Moore’s polling numbers began to creep back up, Mr Trump, also multiply accused of sexual offences ranging from harassment to assault, endorsed him. Other Republicans crept back into the fold. One notable exception was Richard Shelby, Alabama’s senior senator. Two days before the election he went on a prominent talk show just to say, “I wouldn’t vote for Roy Moore…The state of Alabama deserves better.”

At a rally in south-eastern Alabama the night before the election, Steve Bannon, Mr Trump’s former chief strategist and the architect of his presidential campaign, headlined a motley crew of far-right Republicans who offered a cavalcade of bilious, resentment-filled speeches promoting Mr Moore while pandering to Alabamians’ prickliness. “Nobody comes down here and tells Alabamians what to do,” said Mr Bannon, a Virginian, speaking after a Texan and several Midwesterners. Other speakers attacked George Soros, Islam and “the lynch-mob media”. No name got longer and more sustained boos than Mr Shelby’s. Mr Moore’s wife defended her husband against charges of bigotry by revealing that “one of our attorneys is a Jew.”

That rally was one of Mr Moore’s few public appearances in the campaign’s last days. He did not campaign for votes as Mr Jones did, through handshakes and persuasion. Instead he restricted himself to friendly audiences and hand-picked media, the better to avoid difficult questions. On Monday Mr Jones attacked him for “fleeing the state”, saying it showed “he has no sense of duty to the people.”

The results suggest Mr Jones threaded a difficult needle. Black voters turned out in droves: they usually comprise around one-quarter of Alabama’s electorate; on Tuesday they made up 28%, and Mr Jones won 96% of their votes. His strong performance in the state’s most populous counties suggest that he also flipped some white suburbanites.

White evangelicals—Mr Moore’s core supporters—comprised a smaller share of the electorate this year than in past elections. Some of them stayed home, or even voted for Mr Jones, despite vehemently disagreeing with his pro-choice position on abortion. Rushton Mellen Waltchack, a Christian and lifelong Republican from Birmingham, compared Mr Moore to “a televangelist who falls from grace,” and said she could not bring herself to vote for him. “He makes statements that to me don’t represent Jesus in the Bible…What does it say about us as a party if we continue to choose policy over character?”

Statements like that from voters like Mrs Waltchack should terrify Republicans. Their Senate majority is now down to one seat, and the prospect of losing the chamber in next year’s midterms now looms. In Mr Moore they may have found partisanship’s outer limit. Mr Trump has now backed three consecutive losers: Mr Moore; Luther Strange, whom Mr Moore beat in the primary; and Ed Gillespie, who lost the race to be Virginia’s governor.

But establishment Republicans can also take comfort in Mr Moore’s loss. It weakens Mr Bannon and his plan for next year to back primary challengers to Senate Republicans whom he deems insufficiently loyal to Mr Trump. Mississippi remains friendly territory for him, but after that the map looks unwelcoming. And donors—seeing one of his candidates lose a winnable seat—may begin taking their money elsewhere.

As for Mr Jones, he will face long odds when he runs again in three years, presuming Republicans learn their lessons and nominate a less divisive character. But on Tuesday he showed the world an Alabama that rejected hatred in favour of decency and competence.