BRUSSELS (Reuters) - The deal David Cameron has asked Britons to vote on to remain in what he calls a "reformed EU" has been years in the making -- and its making says much about how today's European Union works.
For EU chief executive Jean-Claude Juncker, speaking after Friday's agreement that Cameron said granted Britain "special status" in the group, a key moment came half a world away in the presence of koala bears, when he persuaded the British prime minister to stop seeking rapid revisions to treaties that voters elsewhere in Europe would not ratify.
Juncker urged Cameron at the G20 Brisbane summit in November 2014 to push for a binding agreement pledging to amend EU treaties in the future -- what has become known as the "Danish solution" -- instead of an outright treaty change.
"I simply told him I would not play that game," Juncker said last week of the conversation in the lobby of the summit venue. "That helped convince him."
The story of the "Brexit-buster" deal illustrates how public disillusion with the EU, fueled by economic malaise and disquiet at migration and globalization, has both raised pressure for change and made it harder for governments to revamp the bloc.
It shows, too, the degree of legal creativity -- trickery to its detractors -- that sits in the backrooms of Brussels and the willingness, or maybe desperation, of EU leaders to do whatever it takes to stop its second-ranked economy from leaving, potentially setting off an unstoppable rush for the exit.
Cameron had promised a referendum in 2013 thinking he could piggy-back his demands for safeguards for sterling and British sovereignty onto EU treaty amendments that Angela Merkel had told him she planned to stabilize the euro.
But the German chancellor lost heart in early 2014, one of several negotiators and diplomats who spoke to Reuters said.
"What changed was her failure ... and the fact it was obviously intractable inside the euro zone," one senior EU diplomat said.
President Francois Hollande, not the most sympathetic to what many in the EU saw as a rash gamble by Cameron to hold his own party together, saw no chance of euro crisis-weary French voters ratifying anything they saw as coming out of Brussels.
Success for the far-right, anti-immigrant French National Front and UK Independence Party at the European Parliament election of May 2014 raised both the pressure on Cameron to make good on his promises as he fought for re-election in 2015 and reduced the chance of Hollande and other leaders delivering it.
There would also be other critical moments, notably when two weeks after the Brisbane meeting Cameron made a pre-election pledge to cut welfare for EU migrant workers.
That seemed hopelessly incompatible with treaty rights to free movement and non-discrimination of European citizens. Yet the deal agreed unanimously on Friday includes a promise of EU legislation to that effect.
The legally binding decision also grants Britain an explicit exemption from the founding goal of "ever closer union" and safeguards for the City of London financial center. Cameron on Saturday set the date for a referendum for June 23.
That a deal was done at all testifies to the creativity of Brussels lawyers, to the fear of EU leaders that a Brexit could wreck a Union deep in crisis and, the deal's architects say, to an evolution in Cameron's relations around the summit table.
The prime minister has traveled from a lonely summit in late 2011, when he outraged fellow leaders by vetoing a plan to fix the euro crisis, to one before last Christmas, when a diplomatic tour de force won their blessing to bend EU rules and help him keep his promise to deter Europeans moving to Britain.
"There was a very near moment of rupture in December 2011 and it could easily have lurched from there into exit," the senior EU diplomat said. "It's been an evolution. Cameron's fundamental thinking hasn't changed very much on the European Union. But ... he has changed the way he interacts with others."
Cameron was temporarily isolated at a June 2014 summit which made Juncker head of the European Commission. Hurt by British media headlines alleging he "drank cognac for breakfast", the Luxemburger could have blocked Cameron's quest for reform.
But the pair quickly made up and Juncker handed the key banking portfolio in his team to Cameron ally Jonathan Hill.
"That sealed an alliance," a senior EU official said. "Cameron knows he cannot get the 27 to support him if the Commission is against him. That was decisive."
Juncker was at the 1992 Edinburgh summit that devised a way to overcome Danish rejection of an EU treaty and he gave the prime minister an impassioned vision of how the same solution could work for Britain.
"If Juncker and Cameron had not spoken about Edinburgh in Brisbane we would not have got here," the EU official said, describing the scene that morning when the pair talked while IMF chief Christine Lagarde, deep in conversation with Angela Merkel, petted a tame koala nearby.
The whole idea of a referendum might have lapsed had Cameron lost in May 2015. In the event, he won big and, untrammeled by Europhile former coalition partners, he was free, perhaps even to his own surprise, to demand all he had promised.
Quickly, both sides put together negotiating teams.
Cameron mandated his Europe "sherpa", Tom Scholar, and Ivan Rogers, the British ambassador in Brussels.
Juncker put a Commission veteran, British lawyer Jonathan Faull, in charge of a task force. The president's chief-of-staff, German lawyer Martin Selmayr, led Commission negotiators, backed by the new Dutch head of the EU executive's administration, Alexander Italiener.
Tusk, as president of the European Council, was the man who would have to broker a deal with the 27 other leaders. Acting for him were his chief-of-staff, fellow Pole Piotr Serafin, backed by the Council's French chief legal officer Hubert Legal and the head of its administration, Jeppe Tranholm-Mikkelsen.
A graduate of the London School of Economics, Tranholm-Mikkelsen was until June Copenhagen's EU envoy. Familiar with Denmark's own skeptical relationship with Brussels, he was the patiently creative key force behind the deal, according to others involved in the months of backroom talks.
The brief, from both London and Brussels, was to give Cameron "a chance to offer something he could win with", another official said. "If he didn't it would be terrible for everyone."
Cameron delivered demands to Tusk on Nov. 10. There were no surprises for Brussels -- both sides had worked to ensure that whatever he asked for would be just about feasible.
But the request to discriminate and curb free movement was tricky. And officials and diplomats recall a wobble when London raised the pressure by warning it might yet press for full treaty change.
"It would have wrecked the process," one of the senior EU officials said.
Though the legal experts were still unsure it could work, an EU offer to give Britain the right to deny benefits to newly arrived EU workers for their first four years to protect the welfare system -- an "emergency brake" -- was leaked to the press in a bid to staunch more talk of treaty change.
At the December 17-18 summit, it was still not clear the idea would fly. But Cameron, who since his re-election had visited most European capitals including some that few if any of his predecessors had ever seen, delivered a bravura performance over dinner, asking leaders to help him solve his dilemmas.
He won them over.
"He convinced the others this was not just his internal problem but about the future of Europe," one participant said.
"History was clearly present in the room," said another.
There was plenty of haggling and brinkmanship to come. At last week's summit of late nights, one EU official likened the text to a painting by Belgian surrealist Rene Magritte -- a work of art reconciling legal reality with political image.
While Cameron's critics, at home and abroad, dismiss the deal as "hollow" and a "repetition of tautologies" already found in EU law, its architects insisted its selective readings of existing jurisprudence will give Britain the protections Cameron had wanted -- if he can win over voters on June 23.
There is little more Brussels can do to help him now: "It's not my concern," a weary Juncker said. "That's his problem."
Additional reporting by Francois Aulner; Editing by Sonya Hepinstall