According to more than one member of Gibraltar’s business community, it felt “like someone had died”. The day after Britain’s Brexit vote, this outpost of Albion on Spain’s southern tip was in shock.

The Rock’s residents voted 96% to remain. Its flourishing economy, built around financial services, e-commerce and online gaming, is based on the single market and free movement: every day, nearly half its entire workforce – 12,000 Spanish and other EU nationals – commute across the border.

Worse, Madrid has made it clear that it views the UK’s eventual departure from the European Union as its best chance in three centuries to reclaim sovereignty over a territory it has regretted ceding to Britain ever since 1713.

Alarmed by the government’s apparent preference for a hard Brexit, prioritising immigration controls and judicial sovereignty over single market access, Gibraltar’s leaders – and its businesses – are scrambling to devise a Plan B.

“I think a hard Brexit would be very, very challenging to the economic model that has been the source of our prosperity for 30 years,” Gibraltar’s chief minister, Fabian Picardo, said in an interview.

So while Theresa May talks of a great repeal bill to end the authority of EU law in the UK, Picardo is working on what he terms a great continuation bill – aiming to preserve as many of Gibraltar’s EU advantages as he can.

Gibraltar’s chief minister Fabian Picardo ‘will be looking for a new relationship with the EU’. Photograph: Sean Gallup/Getty Images

That will not, he concedes, be easy, and may even entail trying to negotiate a separate relationship with the EU. “Our reality is different: we are part of the physical continent of Europe,” the chief minister said. “So yes, we’ll be looking for a relationship with the EU that may differ from the UK’s, perhaps an associate-style status. There are models: Andorra, Greenland, Liechtenstein. As part of the UK’s exit, we’d like an agreement to continue to give us single market access, and freedom of movement.”

Drawn by the territory’s position in the EU, a 10% corporate tax rate and ready access to regulators, nearly 500 financial services firms have set up shop in Gibraltar, including subsidiaries of major British insurers such as Hastings and Admiral. The territory has also carved itself a niche in the online gaming business: it hosts 35 big-name operators, employing more than 3,500 people. All that has helped the Rock’s economy to near double-digit growth – even during the euro crisis – and boosted employment by more than 50% in a decade.

Sitting in the handsome government office building off Main Street, Picardo said the greatest single threat would be if Spain closed the border, as it did in 1969 during the Franco dictatorship, leaving residents effectively stranded unless they used boats or planes. (It was not fully reopened until 1985, when Spain wanted to join the EU.)

But this is not the only way in which Spain could make life difficult. Three years ago, during a particularly bitter row over territorial waters, Spanish authorities intensified border checks, creating traffic jams hours long at the crossing. Eventually the European commission had to step in.

“What the UK calls uncontrolled immigration, Gibraltar calls the essential flow of workers,” Picardo said. So what we are working on now is how we can ensure our job market stays as open as it is now, for people who have the right, as EU nationals, to work in Gibraltar.”

Would Spain take that step? Picardo said he could not imagine that “in the 21st century, a democratic Spanish government is seriously going to say, that if Gibraltar leaves the EU it will shut the gates”. The foreign secretary, Boris Johnson, has insisted Britain will maintain “an implacable, marmoreal and rock-like resistance” to any change in Gibraltar’s sovereignty.

Spanish Foreign Minister Jose Garcia-Margallo ays Gibraltar’s access to the EU will come only via shared sovereignty. Photograph: Jon Nazca/Reuters

But Madrid may have other ideas. Johnson’s equally bombastic counterpart, José García-Margallo, who earlier this month threatened to “plant his flag” on the enclave, has repeatedly said Gibraltar’s only post-Brexit access to the single market will come through shared sovereignty. Spain, he also likes to recall, can veto any trade deal between the UK and the EU. The prime minister, Mariano Rajoy, reportedly told May in Madrid last week that once the UK abandons free movement and the single market, Gibraltar will have to do so too – unless it agrees to joint rule.

The nightmare, many here agree, would be if, some years down the line, Madrid were to force London to choose between Gibraltar’s sovereignty and – for example – enhanced access for UK firms to the EU single market.

“That’s the $64,000 question,” said Marcus Killick of leading Gibraltar law firm Isolas. “If there’s a deal that’s pivotal to the City of London and requires Spanish support, how far will the UK government go to defend Gibraltar? How deep is that loyalty, really?”

For the time being, such concerns seem some way off. “Things are going to get tough,” said Edward Macquisten of the Gibraltar chamber of commerce, in his office above the main Casemates Square. “But look, this is a small, agile, adaptable jurisdiction: we can change direction fast. The people here are not unused to uncertainty – we’ve been living with it for 300 years. If one market closes, we’ll find others.”

Killick, who has set up a Brexit support unit for his clients, was similarly optimistic: “A shock followed by a reinvention can be a powerful thing. When the meteorite hit the earth, it was the dinosaurs that died out, not the mice.”

So businesses based on the Rock are urgently exploring alternatives. The online gaming industry will not be hit: it has never benefited from a single market as the EU requires companies to negotiate national licences. Others, though, may now switch their focus back to the UK – in effect reinventing themselves as entry points for European firms keen to access the post-Brexit UK market via Gibraltar’s attractive tax and regulatory regime and lower start-up costs. That will require new laws in Gibraltar and Britain, but looks as though it can be done.

A second option could be to move some operations to another EU state. Steve Quinn, who founded Quest, now Gibraltar’s largest insurance management firm, said his clients who operate mainly in the UK market “will probably not be affected even by a hard Brexit, as long as bilateral trade is formalised”. But those selling into the EU will be looking for solutions. “Some are already talking to authorities in places like Malta, Dublin, Luxembourg, the Baltic states: places that are tax-benign and in the EU.”

Nick Cowan, managing director of the fast-growing Gibraltar Stock Exchange, which has helped borrowers raise €400m of investment capital so far this year, said its business model had been “built on passporting – being able to sell, say, a UK carpet manufacturer’s bonds into every country across the EEA”.

Brexit would force the GSX to reconfigure, Cowan said, possibly by opening a subsidiary in another EU state – he’s already sounded out Malta – and almost certainly by passporting into the UK.

“Post-Brexit,” Cowan said, “if you’re a German corporate wanting to market to the UK, you’ll have to either seek approval from the FCA in London – or come to us. As a jurisdiction, Gibraltar is likely to be faster, more user-friendly and more competitive on price. We sit down the corridor from the regulator. Issues can be resolved fast.”

Many here believe there is broad support for Gibraltar’s cause in Brussels and other EU capitals – and mounting frustration with Spain for seeking to profit from Brexit. Macquisten said: “Gibraltar is almost a model of what the EU set out to achieve: cross-border cooperation, jobs for EU citizens, economic growth, full compliance … Surely the EU won’t want that to end, Gibraltar to be punished? Especially when we showed our commitment to the EU ideal so overwhelmingly last June?”

But it may not be down to Gibraltar, or to London. “Spain,” García-Margallo reiterated last week, “will never give up its claim” to the Rock – and free movement across the border would “automatically end” unless Gibraltarians accepted shared sovereignty.

The meaning of leaving: on life, visas and health

British expats in Benalmadena, Spain: no one yet knows yet quite how daily life there will be affected. Photograph: David Ramos/Getty Images

Brexit could affect the residence rights of the 1.2 million British citizens who live in other EU countries, as well as their ability to work visa-free and access free healthcare – but no one yet knows how.

With article 50 yet to be triggered and the UK government refusing to reveal its hand, all that can be said with certainty is that issue will form a important part of the negotiations due to begin before April next year.

Lawyers say it’s unlikely anyone already living abroad will be kicked out, particularly if they’ve lived there legally for five years (the point at which they can become long-term residents, a status granted to non-EU citizens, which British nationals will be). Whatever is agreed will mostly be reciprocal, so if Britain requires residence permits, proof of income and work visas for new EU migrants, this will almost certainly apply to British citizens moving to the EU.

Inheritance and tax laws may change, although existing property rights and UK pensions – which are paid anywhere in the world – are unlikely to be affected. But British workers may be hit by rules in more than half of EU states that say a non-EU citizen can be hired only if no EU candidate is found.

The Spanish government, for example, has already suggested that after Brexit, the UK will have to pay the healthcare costs of Brits living in Spain. For the time being, almost nothing is certain – which explains the huge rise in Britons abroad seeking EU citizenship.