A last-minute deal to meet Spanish demands for a say on the future of Gibraltar after Britain leaves the EU has salvaged Sunday’s EU summit to deliver a Brexit accord to British Prime Minister Theresa May.

Mrs May has met EU chief executive Jean-Claude Juncker and summit chair Donald Tusk this evening to receive final assurances that all will go smoothly as Spanish Prime Minister Pedro Sanchez lifted his threat of an effective veto after Britain and EU officials provided written guarantees to Madrid.

"We have received sufficient guarantees to be able to reach a solution to a conflict that has lasted more than 300 years between the United Kingdom and Spain," Mr Sanchez told reporters in Madrid after talks dragged on through the night in Brussels.

Mr Juncker's spokesman said the negotiated deal was "fair for the UK, fair for the EU", while Mr Tusk quoted the British rock band Queen as a motto for the Sunday summit in saying: "Friends will be friends right till the end."

The British government wrote to Mr Tusk to say that it would not interpret its withdrawal treaty, due to be endorsed on Sunday, as meaning that a future EU-UK trade treaty would automatically apply to Gibraltar - though London's envoy to the EU said it would seek the best deal for its territory.

"We will always negotiate on behalf of the whole UK family, including Gibraltar... We have worked through the withdrawal issues for Gibraltar in a constructive and sensible way," Mrs May said in Brussels this evening.

At the summit, the 27 EU leaders will be asked to approve a declaration specifically excluding Gibraltar from future EU-UK treaties - while leaving open the possibility that London could negotiate with Madrid on deals for Gibraltar.

Read more:

DUP to 'revisit' support for UK govt if Brexit deal passed - Foster

Spanish PM warns of no EU summit without Gibraltar deal

Analysis: Brexit and the DUP - Triumph or disaster?

After a little more than an hour of talks among themselves, the 27 EU leaders will meet to share with Mrs May two key documents - a treaty setting terms for an orderly British withdrawal on 29 March and an outline of how Britain can keep close to its biggest market by following some EU rules after a status-quo transition period ends in two to four years.

Mrs May will, however, face an uphill struggle to have the agreement accepted by her own party and the British parliament.

"We were all looking for a good and fair agreement," Mr Tusk said in a letter to the 27 other national leaders confirming that the summit will go ahead.

"And I believe that we have finally found the best possible compromise."

It comes as former Brexit secretary Dominic Raab, a committed Leaver who quit last week over the Withdrawal Agreement, said he believed the terms were so bad the UK would be better off remaining in the EU.

"I'm not going to advocate staying in the EU," he told the BBC Radio 4 Today programme.

"But if you just presented me terms, this deal or EU membership, because we would effectively be bound by the same rules but without the control or voice over them, yes, I think this would be even worse than that."

With more than 80 Conservative MPs - from both the Leave and Remain sides - threatening to vote against the agreement, Mr Raab warned it was unlikely to get through the Commons and said ministers should consider leaving without a deal.

But if it did pass the Commons, the repercussions could bring down Mrs May's Government, with the DUP hinting at withdrawing the support of its MPs.

Meanwhile, the country's finance minister said that Britain's draft deal to leave the European Union is not just the best solution to political division but also for the economy.

"This deal is a way of Britain leaving the European Union... with minimum negative impact on our economy," Philip Hammond said.

"But economics is not the only consideration. We also have to look at the political healing process, bringing our country back together, because (...) countries that are disunited and divided are not successful countries," he said.

Despite the turmoil, the Prime Minister again insisted that as far as she was concerned, Brexit would go ahead next year as planned.

"Personally, there is no question of no Brexit, because the Government needs to deliver on what people voted for in the referendum in 2016," she said.

"As far as I am concerned, the UK is leaving the European Union on March 29 2019."

Additional reporting: PA