Eurozone leaders have reached a deal with Greece to negotiate a third bailout to keep the near-bankrupt country in the eurozone after a whole night of negotiations at an emergency summit.
"Euro summit has unanimously reached agreement. All ready to go for ESM programme for Greece with serious reforms and financial support," European Council President Donald Tusk announced on Twitter.
ESM refers to the European Stability Mechanism bailout fund.
However, the tough conditions imposed by international lenders led by Germany could bring down Prime Minister Alexis Tsipras' leftist government and cause an outcry in Greece.
Even before the final terms were known, his labour minister went on state television to denounce the terms.
"We were able to keep the unity on keeping Greece inside the euro zone," Slovenian Prime Minister Miro Cerar said on his Twitter account.
EU officials said Mr Tsipras finally accepted a compromise on German-led demands for the sequestration of Greek state assets to be sold off to pay down debt.
Finance ministers will as a matter of urgency discuss how to help #Greece meet her financial needs in the short term (bridge financing).— Donald Tusk (@eucopresident) July 13, 2015
The Greek leader also dropped resistance to a full role for the International Monetary Fund in a proposed €86bn bailout, which German Chancellor Angela Merkel has declared essential to win parliamentary backing in Berlin.
However, in a sign of how hard it may be for Mr Tsipras to convince his own Syriza party to accept the deal, Labour Minister Panos Skourletis said the terms were unviable and would lead to new elections this year.
As the hours ticked away overnight, most of the leaders were forced to cool their heels, playing computer games or taking a nap in their delegation offices while Mr Tusk and the leaders of Germany, France and Greece met several times privately to try to cut through the final knots.
Mr Tsipras will now have to rush swathes of legislation through parliament this week to convince his 18 partners to release bridging funds to avert a state bankruptcy and just to begin negotiations on a three-year loan.
If the summit had failed, Greece would have be staring into an economic abyss with its shuttered banks on the brink of collapse and the prospect of having to print a parallel currency and in time exit the European monetary union.
Greece faces implementing six crucial measures
Six sweeping measures including spending cuts, tax hikes and pension reforms must be enacted by Wednesday night and the entire package endorsed by parliament before talks can start, the leaders decided.
In almost the only concession after imposing a tough set of terms on Mr Tsipras, Germany dropped a proposal to make Greece take a "time-out" from the eurozone that many said resembled a forced ejection if it failed to meet the conditions.
Mr Tsipras said on arrival in Brussels yesterday he wanted "another honest compromise" to keep Europe united.
Instead, he was subjected to a 15-hour humiliation by leaders furious that he had spurned their previous bailout offer on more favourable terms in June and held a referendum last week to reject it.
One senior EU official calculated the cost to the Greek state of the last two weeks of political and economic turmoil at €25-30bn.
Ms Merkel, whose country is the biggest contributor to eurozone bailouts, warned from the start that she would drive a hard bargain against a backdrop of mounting opposition at home to more aid for Greece.
"The most important currency has been lost and that is trust," she told reporters.
"That means that we will have tough discussions and there will be no agreement at any price."
If Greece meets the conditions, the German parliament would meet on Thursday to mandate Ms Merkel and Finance Minister Wolfgang Schaeuble to open the talks on a new loan.
Then Eurogroup finance ministers could formally launch the negotiations.
Perhaps the toughest condition for Mr Tsipras to swallow was Germany's insistence that Greek state assets worth up to €50bn be placed in a trust fund beyond government reach to be sold off with proceeds going directly to pay down debt.
Germany initially wanted to use a structure in Luxembourg managed by its own national development bank, KfW, but diplomats said it was flexible on the location.
EU and IMF experts evaluate Greek assets currently earmarked for privatisation at just €7bn.
One diplomat said that was tantamount to turning Greece into a "German protectorate", stripping it of more sovereignty.
But Ms Merkel declared the matter a "red line" for Germany.
For his part, Mr Tsipras demanded a stronger commitment by the creditors to restructure Greek debt to make it sustainable in the medium-term.
That could be his only hope of selling such a deeply unpalatable package to his own supporters and the public.
An EU official said several options were under consideration to give Greece bridging funds once it passed the laws, but no final decision was taken.
They included releasing European Central Bank profits on Greek bonds, tapping an emergency fund run by the European Commission, or bilateral loans from friendly countries such as France.
Two French sources denied any bridging loan was planned.
Finance ministers said Greece needed €7bn of funding by 20 July, when it must make a crucial bond redemption to the European Central Bank, and a total of €12bn euro by mid-August when another ECB payment falls due.
Some diplomats questioned whether it was feasible to rush the package through the Greek parliament in just three days.
Mr Tsipras is set to sack ministers who did not support his negotiating position in a vote last Friday and make dissident politicians in his Syriza party resign their seats, people close to the government said.
Greek sources said Mr Tsipras feared a public backlash in Greece when the terms of the bailout become known.
Even while Mr Tsipras was still at the table in Brussels, one of his ministers went on television to say he could not blame politicans who would find it hard to say 'Yes' to the emerging cash-for-reforms deal.
"It's clear this deal does not represent us," Mr Skourletis said.
Analysis: Economics Correspondent Sean Whelan
This bailout is going to be a pretty tough and bitter pill for the Greek parliament to swallow.
I can see the ancient cemetery in Athens where Socrates is reputed to be buried after he had to swallow a cup of poison. I’m sure many Syriza MPs are feeling pretty much like that right now.
Those MPs are going to have to do things they don’t want to do, and have set their face against doing, such as a streamlining and overhaul of the VAT system and a broadening of the VAT base in order to take more tax out of the economy.
Also, immediate changes will have to be made the pension system in Greece.
Those were the two red-line issues in Greece up until last week and both have now been crossed.