* All-night talks end with deal on Gibraltar
* Gibraltar excluded from future EU-UK trade pact
* EU leaders to meet on Sunday to endorse Brexit deal
* Treaty faces strong opposition in British parliament (Adds May-Juncker meeting, Tusk, May comments)
By Jan Strupczewski and Alastair Macdonald
BRUSSELS, Nov 24 (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.
May met EU chief executive Jean-Claude Juncker and summit chair Donald Tusk on Saturday 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,” Sanchez told reporters in Madrid after talks dragged on through the night in Brussels.
Juncker’s spokesman said the negotiated deal was “fair for the UK, fair for the EU”, while 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 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.
May said in Brussels on Saturday evening: “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.”
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 British naval base and 30,000 people.
Sanchez’s brinkmanship, however, left some in Brussels uneasy at how he jeopardised the Sunday summit for what many saw as domestic political purposes. Sanchez’s party faces a regional election next weekend in Andalusia, the province abutting the Gibraltar peninsula.
After a little more than an hour of talks among themselves, the 27 EU leaders will meet at around 11 a.m. on Sunday 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.
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. 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 Northern Ireland’s Democratic Unionist Party (DUP), which May’s minority government relies on for support in parliament, also criticised the withdrawal agreement. Nigel Dodds said it 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 deal.
EU officials are unclear what might happen if parliament rejects May’s package and, like London, are making arrangements to manage the disruption caused by 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 unravel 45 years of membership in what is now the world’s largest free trade bloc.
As with the challenge of pulling 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 and Jason Neely)