LONDON (Reuters) - When Prime Minister David Cameron sealed a deal designed to keep Britain in the European Union after two days of talks in Brussels, his relief was short-lived.
Within hours of Friday's agreement, one of Cameron's closest allies, Justice Secretary Michael Gove, and five other ministers declared they would campaign against him in a June 23 referendum on whether Britain should stay in the bloc.
It was the first blow in what could be a new "civil war" in Cameron's Conservative Party over Europe. Divisions over Britain's place in Europe contributed to the downfall of two of his predecessors, John Major and Margaret Thatcher.
It is a war Cameron tried hard to avoid when he came to power in 2010. The following year he ordered his party in the strictest terms to vote down a bill suggesting a referendum on membership of the EU, saying it was the "wrong answer for Britain".
But within two years, he had changed his mind, paving the way to a membership referendum, by declaring: "I believe in confronting this issue - shaping it, leading the debate. Not simply hoping a difficult situation will go away."
Cameron, 49, now finds himself fighting a referendum which will determine Britain's future in world affairs and shape the future EU - Britain is the bloc's second-largest economy and one of its two main military powers.
In a quirk of the British political system, the prime minister is in the unusual position of being more sure of the backing of the opposition Labour Party than of his own party.
"He didn't want a referendum, he was bounced into doing it," said Douglas Carswell, a Conservative Party member until he defected to the eurosceptic UK Independence Party (UKIP) in 2014. "He's been the actor in this production, he is not writing the script, or directing it, or producing it."
As a eurosceptic, Carswell has an axe to grind, but his view that Cameron has almost sleep walked into such a crucial referendum is shared by some of the prime minister's allies and supporters as well as other critics.
"I think it's pretty clear ... there was no plan," said Jonathan Portes, principal research fellow at the National Institute of Economic and Social Research who specialises in immigration issues.
The deal reached on Friday followed weeks of negotiations across Europe in which Cameron tried to win better terms for Britain if it remains in the EU, hoping to win over sceptical voters including many in his own party.
He said he had won his country a "special status" from the agreement, which excludes Britain from the founding goal of "ever closer union" and hands the government welfare curbs to try to tackle concerns over high levels of migration.
A spokesman for Cameron said the prime minister had always focused on winning "the best deal for the British people" and denied the negotiations had been about "party management".
But Cameron's path to the referendum shows he is clearly sensitive to the opinions in his party.
He initially steered clear of discussing the EU with party eurosceptics after coming to power and avoided meetings with them and, to reduce their hold, portrayed them "as a crackpot minority banging on about Europe", one Conservative lawmaker said.
Allies say Cameron knew he had to counter the threat of rebellion and took a calculated risk by calling a referendum he expects to win.
But as the prime minister tried to ignore the issue, Britain's largely eurosceptic press kept the issue alive.
Over decades Britain's press has fuelled suspicion of a bloc praised by many Europeans for ensuring peace after World War Two, but derided in Britain for wanting to ban curved bananas and rename sausages.
A natural, if not passionate, europhile, Cameron increasingly seemed out of step with many in his party as they opposed moves towards further integration in Europe and saw a referendum as the logical next step.
In 2011, when Cameron ordered Conservative members of parliament to vote down a bill suggesting a membership referendum, about 80 rebelled.
John Baron was one of them. Immediately afterwards, Baron said he and at least eight rallied more than 100 lawmakers to force Cameron to abandon his no-referendum policy and, after they wrote letters and lobbied fiercely, more than a year later he relented. He then made holding a referendum a central policy.
Baron told Reuters that Cameron was "ultimately confronted with logic but also a realisation that one, we weren't going to go away, and two, we were much more sizeable than the spin operation portrayed".
Baron is keen to take credit for his role in forcing the prime minister into holding the referendum.
But bullied into the vote, Cameron hastily had to come up with policy on the hoof, sources said.
Portes, who as a civil servant helped negotiate Britain's opt-out from the Maastricht treaty which set up the euro currency, said Cameron's decision to focus on winning concessions over "in-work benefits" - social payments for low-paid workers - had underlined the lack of strategy.
The focus on a specific, and otherwise little-known, element of Britain's welfare system maddened some EU officials, who saw it as a direct attack on the EU's fundamental principles and discriminatory. It almost derailed the negotiations.
Pat McFadden, Labour's former Europe minister, says this was down to Cameron's poor understanding of the EU.
"He doesn't always do his homework on a European level," McFadden told Reuters.
Cameron aides say the prime minister has plenty of experience of EU affairs, visiting 20 member states for talks on the bloc since he was re-elected last year.
Cameron was forced to focus on in-work benefits after concerns over immigration grew among voters over an influx of migrants from eastern European member states. His move was also partly meant to counter a growth in the popularity of UKIP.
In 2014, he said he would "not take no for an answer and when it comes to free movement I will get what Britain needs". But EU officials swiftly made clear they would not allow this, saying it would undermine one of the EU's founding principles.
Struggling to find a way to include immigration in the renegotiation, Cameron's eye fell on a report by the Open Europe think tank which suggested that curbing the benefits to EU workers would reduce migration and would avoid troublesome change to the bloc's founding treaties.
"And they just lifted it, without testing it, without doing any serious analysis at all, either legal or economic," said Portes.
Open Europe's co-director, Stephen Booth, said that his study was quickly taken up by the government and advisers did not ask legal questions. Cameron's officials say the prime minister has formed policy in response to public concerns over high immigration.
Although agreement was reached on Friday, the decision to include gestures for Britain on welfare reform still rankles in EU capitals.
One diplomat involved in the talks said Britain's EU partners were "rather irritated ... that here we are dealing with some rather obscure issues... while we have ... major issues we should really be on instead of this".
For eurosceptics, the curbs on in-work benefits and restrictions to payments to EU migrants with an annual income below a certain level for their children who remain abroad do not go far enough and accuse Cameron of buckling under pressure from the EU.
"Britain banged the table and aggressively demanded the status quo," Daniel Hannan, a Conservative member of the European Parliament, wrote on Twitter. "The EU, after some mandatory faux-agonising, agreed."
"He's tinkering because he knows he cannot question one of the founding principles of the EU which is freedom of movement," said Baron.
Baron said the result of the referendum would be close and "lance the boil" in the party for good.
But others disagree, seeing little chance of 'the Europe issue' ever going away - something that may come to hurt Cameron whichever way Britain votes.
"The European issue has split it from top to bottom, ever since we went in," said Jeffrey Archer, a former conservative lawmaker and an author.
"It's a divisive issue and if we do come out, it will be divisive. If we do stay in, it will be divisive."
additional reporting by Gabriela Baczynska, editing by Janet McBride