By Jan Strupczewski and Alastair Macdonald
BRUSSELS (Reuters) - A last-minute deal to meet Spanish demands for a say on the future of Gibraltar after Britain leaves the EU salvaged a summit which will go ahead as planned on Sunday to deliver a Brexit accord to British Prime Minister Theresa May.
With May due to meet EU chief executive Jean-Claude Juncker and summit chair Donald Tusk at 6 p.m. (1700 GMT) and 7:15 p.m. respectively to receive final assurances that all will go smoothly, 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," Sanchez told reporters in Madrid after talks dragged on through the night in Brussels.
The British government wrote to Tusk's European Council 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.
At the summit on Sunday morning, 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.
Saturday's agreements may comfort Spain's hopes that the EU will, once Britain is no longer a member, swing behind its 300-year-old claim to recover sovereignty over "The Rock", home to a historically strategic British naval base and 30,000 people.
Sanchez's brinkmanship, however, left some in Brussels uneasy at how he jeopardised a tightly choreographed three hours on Sunday morning for what many saw as domestic political purposes -- he faces a regional election next weekend in Andalusia, the province abutting the Gibraltar peninsula.
AFTER SUMMIT, PARLIAMENTARY BATTLE
After a little more than an hour of talks among themselves, the 27 EU leaders will meet around 11 a.m. to share with May two key documents -- a treaty setting terms for an orderly British withdrawal on March 29 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.
She 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. Tusk said in a letter on Saturday 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."
That is also how May is trying to present her deal. But, keeping up pressure within her own Conservative party, her Brexit-supporting rival and former foreign minister Boris Johnson called on Saturday for the accord to be renegotiated.
The deputy leader of the Northern Irish party which May's minority government relies on for help in parliament as the withdrawal agreement would leave Britain in a "pitiful and pathetic place ... locked into an EU straitjacket".
The DUP, along with many of May's Conservatives and the Labour opposition, all threaten to vote down the package.
EU officials are unclear what might happen if parliament rejects May's package and, like London, are making arrangements to manage the disruption of Britain crashing out without a deal.
The Gibraltar agreement with Spain provided a reminder of the complexities that Britons have found since voting narrowly in a 2016 referendum to try and unravel 45 years of membership of what is now the world's biggest free trade bloc.
As with long months spent trying to square the circle of pulling its troubled province of Northern Ireland out of the EU without creating a disruptive customs border with EU member Ireland, the Gibraltar dispute reflected problems with its European neighbours dating back to Britain's imperial past.
(Additional reporting by Sam Edwards in Madrid and Gabriela Baczynska and Elizabeth Piper in Brussels; Writing by Alastair Macdonald; Editing by Keith Weir)