LONDON (CNNMoney) -- Europe has offered Greece a new $96 billion bailout after its government agreed to enact deep economic reforms under close supervision by its creditors.
The rescue -- Greece's third since 2010 - should secure its place in the euro, for now. If the country had crashed out of the currency union, the unprecedented event would have shaken Europe to its core.
"Eurozone leaders have agreed in principle that they are ready to start negotiations on a [new bailout]," said Donald Tusk, who chaired an emergency summit of all 19 nations that use the euro.
The leaders hammered out the agreement at the marathon overnight meeting in Brussels, after weeks of frantic diplomacy sparked by Greece walking away from a previous bailout program.
That decision left it without the cash to make a payment to the International Monetary Fund, triggered the closure of its banks, and sent the economy into free fall.
Fast running out of money, Greece faced an awful choice: Accept the conditions demanded by the only people willing to lend it money, or leave the euro.
Speculation about the currency should now fade, but it's not clear how soon the money the country desperately needs will flow, and when its banks will reopen.
Finance officials will reconvene later Monday to talk about how to support Greece while the details of the bailout are being negotiated. That process could take weeks.
The agreement means the Greek government must now go way beyond a reform proposal it submitted last week, by making much more profound changes to pensions, energy, labor and product markets, and by scaling up a program of privatization. It also has to overhaul its public administration and justice system.
The conditions are particularly tough for two main reasons.
First, the economy has deteriorated sharply in recent weeks, damaging Greece's already fragile finances still further.
Second, there was a complete breakdown in trust between Greek Prime Minister Alexis Tsipras and other European leaders. That was largely because of a series of U-turns he performed, and his decision to call a referendum to reject reforms he then signed up to days later.
That made for hard talking this weekend. The summit ran for 17 hours, and that was after finance officials had spent 14 hours preparing the ground.
Under pressure from skeptical voters, some European leaders wanted ironclad guarantees that they wouldn't be throwing good money after bad. Europe and the IMF have already lent Greece about 233 billion euros since 2010.
Monday's deal requires Greece to give access to bailout monitors on the ground in Athens -- including officials from the IMF, a point Tsipras resisted to the last.
He was elected on promises to reverse austerity and end intrusive monitoring, and the agreement will raise questions about whether he can continue in his post.
But without a new bailout, Tsipras knew Greece's descent into economic chaos would accelerate, bringing the country ever close to exit from the euro.
Greece's banks have been shut for two weeks, and cash withdrawals are capped. The vital tourism industry is suffering. People are spending less, some public services have stopped charging, and the healthcare system is running out of imported medicines.
Greece needs to pay pensions and wages this week, and make a big debt repayment to the European Central Bank next week. Without an injection of funds fast, it would have to issue IOUs, a first step to printing its own currency.
The country isn't home and dry yet. Tsipras has to push a first package of reforms -- including tax changes and pension reforms -- through parliament by Wednesday.
And it's not clear whether that will be enough to persuade the ECB to ease restrictions on funding for Greek banks imposed after the government walked out of talks in late June.
Assuming Greece accepts a broader overhaul of the economy, there are still other potential roadblocks standing in the way of a third bailout.
A handful of other eurozone countries, including Germany and Finland, will need to win votes in their parliaments this week on a new rescue. Only then can formal negotiations begin.