“If it starts with a four, I’ll be happy with that,” said one of Nicola Sturgeon’s aides at the launch of the SNP manifesto last month.

At that point, with the polls positive but the Scottish Tories looking tougher to remove than expected, the party was simply anxious to break the psychological threshold of 40 MPs.

Anything below that was seen as depressingly lukewarm, hardly the springboard to another independence referendum Ms Sturgeon needed.

In the closing week, the party revised down its expectations. Canvassers reported the Tory vote was holding up and suddenly just two or three gains on top of the 35 seats won in 2017 became the projection.

Strategists discounted taking Jo Swinson’s Dunbartonshire East seat, for instance, reckoning decapitating the LibDems, while enjoyable, was an ask too far.

The BBC’s exit poll with its prediction of 55 seats was therefore met with extreme scepticism.

The FM’s aide sent a one-word text to those asking about it: “b****cks.” But after a round of frantic phone calls to activists and organisers on the ground, slowly the picture at SNP HQ began to change.

One of the key factors was turnout. “That’s what we underestimated,” said a source.

“We knew our vote was motivated, but turnout was still better than we expected.”

While turnout was down in England, Wales and Northern Ireland, it was up in Scotland, rising 1.6 points to 68.1 per cent. Across the UK it fell 1.5 points to 67.7%.

The SNP’s final tally of 48 MPs – actually 47 plus one suspended member who still appeared on the ballot as SNP – was therefore far better than Ms Sturgeon had hoped.

She immediately put it to use, declaring it was a renewed, refreshed and strengthened mandate for holding Indyref2 in 2020. The SNP improved on every metric.

Besides gaining 13 seats, the party increased its share of the vote from 36.9 to 45%, and gained 264,811 votes, with a total of 1.24m.

It was a victory made all the sweeter by reversals for the party most opposed to Indyref2.

The Scottish Tories had also misread their data and had over-estimated their support rather than under-estimating it.

They lost seven of their 13 MPs, and saw their vote share fall from 28.6 to 25.1%.

Their vote total fell 65,010 to 692,939. Yet, on the eve of polling, the party had been confidently talking up gains to the media.

At a joint event with her successor Jackson Carlaw, former leader Ruth Davidson had said she wanted to see her party take seats off the SNP.

“I’ve not just been spending time in the seats that we hold trying to defend them, I’ve been spending time in seats that we’re trying to take,” she said.

In the end, the party went backwards in 10 of its 13 seats.

The Tories did better on the offensive, increasing their vote in Lanark & Hamilton East, Ayrshire Central and Argyll & Bute, but by small or pitiful amounts.

They slumped in the ultra-marginal target of Perth & North Perthshire, where the SNP’s Pete Wishart had a majority of 21. He was returned with a majority of 7,550.

The SNP increased their vote across the board.

It was a stark contrast to 2017, when Scotland had been the one bright spot for the Tories.

Then, Scotland had been the only source of Tory gains, as the party under Ms Davidson picked up 12 seats and gave

Theresa May the numbers she needed to stay in power. The party’s vote share increased by 13.7%, double the rise across the UK.

But at this election, Scotland provided the worst Tory result. The only nation in which the party lost MPs or vote share was Scotland.

The Scottish campaign was based on making Ms Sturgeon and the SNP synonymous with Indyref2 in the belief Labour voters would vote tactically to protect the Union.

But it turned out Labour voters preferred voting tactically to help the SNP get rid of Tories.

The party knew Boris Johnson and Brexit weren’t going down well – they latterly airbrushed the PM out of their campaign – but thought Ms Sturgeon was even less popular.

Their own animosity to “that effing woman” proved a blindspot. A big headache for the Scottish Tories is that this election was supposed to be the prelude to their Holyrood campaign of 2021.

The party was hoping to build up support and convert it into gains on the regional lists. But if it starts losing constituencies, those MSPs will wash up on the list as consolation wins under the proportional voting system and make net gains will be harder.

One bleak omen for Mr Carlaw is that the Tory share of the vote fell almost 5 points in avowedly Remain East Renfrewshire, while the SNP’s rose 13.7 points. Now a born-again Leaver, Mr Carlaw represents the equivalent seat at Holyrood.

A similar swing to the SNP in 2021 would overturn his 1,611-vote majority, sending him back to the West of Scotland list. The one possible upside for the Scottish Tories is the election may lead to an injection of new blood at Holyrood, as defeated MPs look to Edinburgh for a new berth, clearing out some of the dead timber.

But if the results were bad for the Tories and their campaign against Indyref2, they were truly atrocious for Scottish Labour. Despite its precarious hold on the electoral map – four of the seven seats it was defending had majorities below 1,000 – the party was predicting it would return nine MPs just hours before the polls closed.

It was over-confidence based on positive canvas returns. In Glasgow North East, Paul Sweeney publicly predicted he would increase his 242-vote majority on the back of glowing canvas data.

After the massive YouGov poll on Tuesday predicting a Tory landslide, the party’s digital chief Aidan Kerr tweeted: “Glasgow North East is not going to the SNP. And if the pollsters think this, then God knows what else they have got wrong.”

Mr Sweeney lost to the SNP’s Anne McLaughlin by 2,548 votes. It was a profound failure of the party machine that points to deep, systemic problems. The party reckoned it was making a comeback in its old heartlands of Glasgow and North Lanarkshire.

But when the results came in, Labour lost vote share in every Glasgow seat and in all its North Lanarkshire targets as well. In fact, Labour lost a bigger slice of its vote share in Scotland than anywhere else in the UK, down 8.5 points to a wretched 18.5%, thanks to the loss of 200,000 votes.

Its sole MP north of the Border is Ian Murray in Edinburgh South, who did not hesitate to blame Mr Corbyn for the party’s desperate decline. Mr Leonard has now lost two elections in a year.

Last night he vowed to lead his party into the 2021 election as well. His opponents will be chuffed.

The LibDems, despite losing Ms Swinson, were the steadiest party of the night. It gained North East Fife to stay on four MPs, increasing its share of the vote by 2.8 points.

The upshot of it all is Ms Sturgeon will ramp up demands for Indyref2. Mr Johnson called her last night to get his refusal in early. Because what’s in it for him? If he granted a Section 30 order, despite his “guarantee” not do so, it would cause turmoil in his party.

He also knows referendums bring down leaders. Ask David Cameron, Theresa May and Alex Salmond. Mr Johnson has no appetite to bring his premiership to an early end by granting and then losing Indyref2.

Perhaps most compelling for a politician who seems to care for little but himself, defeat would mean going down in the history books as the last Prime Minister of the UK, the man who made himself Minister for the Union then oversaw its break-up. It is not a legacy he would savour.