How on earth did it come to this? Even as the counting started in the early hours of Friday morning, there was still a hope, an expectation even, that the Remain side would win in the end, but as the sun rose, we were greeted instead by the sight of a triumphalist Nigel Farage loudly declaring June 23 as Independence Day.

Later in the day Boris Johnson tried to strike a more sober tone, but for everyone who believed, and still believes, that membership of the EU is in the best interests of Scotland and the UK, for those who want to live in a country that looks out towards its neighbours rather than obsesses about its own selfish interests, for the millions who voted Remain in the hope that the UK could be part of a stable and progressive Europe, yesterday was about disbelief and despair.

The result has also raised the prospect of a second independence referendum, one Nicola Sturgeon would be justified in seeking given the Scottish vote to Remain and the material difference the UK decision to Leave had ushered in. Also, the result vividly exposed the profound divisions within the UK: the fissures between city and country, between the establishment and the electorate, between nation and nation, and particularly between the young and the old. The older the voter, the more likely they were to vote for Brexit, but by opting for an act of national self-mutilation that will damage the British economy and likely bring about another recession, the old will probably deny the young many of the benefits they have received from decades of growth within the EU. Perhaps they will regret it when they see their pension pots begin to atrophy.

The economic consequences do not end there. Four days ago, The Herald argued membership of the EU was good for the economy of Scotland and the UK and that we should stay in. But, in voting to leave, the UK has been diminished economically and politically and faces a significantly poorer future in which businesses look at how they operate or consider relocating; indeed, many already are. Mr Johnson and his allies can strive to paint a Panglossian picture (does he really believe, as he insisted yesterday, that the UK would not be any less united after the vote?), the darkest irony of it all is that those who most enthusiastically supported Brexit will suffer the most. This is especially so in the poorest areas that have benefited from EU investment and public spending. If those voters think a right-wing Tory government will replace that investment with British money, they should think again.

The political consequences are also profound, for the leading players who have taken us to this point and for the UK as a whole. The Prime Minister once said he wanted his party to stop “banging on” about Europe but he fatally undermined his position by allowing the Conservatives to do little else for months. He might well be remembered as the prime minister who broke up two unions: the union of Europe and the union of the UK.

As for Labour leader Jeremy Corbyn, he might also pay the price for a flabby, half-hearted and ineffectual campaign. He maintained he was in favour of Remain and that there was a socialist, progressive case for membership of an organisation he once reviled. But he seemed to be driving the Labour campaign with one foot on the brake; it is also clear Labour has become disconnected from the working class areas of Britain that were once its bulwarks. Faced with the disaster this has brought about, Mr Corbyn could well have to follow Mr Cameron’s example and resign.

The figures who replace them must not repeat the mistakes of the past or ignore the forces that led to Brexit. Labour MP Diane Abbott said the vote was a roar of defiance against the Westminster elite and she was right as, in many ways, the referendum was a proxy for other grievances, mostly caused by the Tories’ austerity programme. The Remain campaign kept talking about the economic benefits of EU membership, but for those in places like Boston and Basildon, and indeed parts of Scotland, that feel they have been denied the economic benefits and felt the worst of austerity, the EU vote became a focus for dissatisfaction. It became an opportunity for the dispossessed to strike back at the entitled.

Many voters also felt ignored on the issue of migrants and when they did raise concerns, they were told immigration brings more advantages than disadvantages or, worse, they were dismissed as bigots or racists. The Leave campaign’s argument that the UK can leave the EU, clamp down on migration and retain preferential access to EU markets remains a fallacy. But part of the reason we are where we are today is because the political class did not listen to voters’ concerns on migration and sought to close down the debate. There will be some who will still view the popular result in those terms and remind us that a crowd once shouted for Barabbas and that electorates do not always make the right choice, but that would be to repeat the mistakes of the past. It is politicians who refused to talk about migration and cried “racist” who are paying the price.

The other consequence of the forces unleashed by the referendum is an existential crisis for the UK and for the EU. For the EU, it looks like it might take a particularly ugly form; just look at the kind of European politicians who have welcomed Brexit. In Northern Ireland too, Deputy First Minister Martin McGuinness has called for a poll on a united Ireland and in Scotland, there is not only a case for another referendum, the SNP potentially also has a much stronger case for independence than two years ago. Indeed, recent political history has created the ideal scenario for Nicola Sturgeon and the SNP: first, a Tory government, then the collapse of Labour, and now a resurgent Conservative right taking us out of the EU.

There is also a genuine democratic grievance in Scotland. All 32 local government areas voted to remain and yet the nation as a whole faces the prospect of that democratic will being ignored. What it means is that the SNP can legitimately say there has been the material change it insisted was needed for a second independence referendum which, combined with the business case for independence based on the economic benefits of being in the EU, creates a powerful momentum behind the SNP.

Mr Johnson and his fellow Brexiters may dismiss all of this, just as Leave campaigners dismissed the concerns of economic experts with a wave of their hands, but that will not do.

Whoever is Prime Minister by the end of the year will have to map out a strategy for unpicking all of the connections between us and the EU as well as attempt to mitigate the economic consequences as they take effect over the coming months. He or she will also have to attempt to find a solution that works for Scotland within the UK but the bleak and desperate result of the EU referendum could mean it is already too late.