BRUSSELS (AP) -- After months of acrimony, Greece finally clinched a bailout agreement with its European creditors on Monday that will, if implemented, secure the country's place in the euro and avoid financial collapse.
The terms of the deal, however, will be painful both for Greeks and their radical left-led government, which since its election in January had vowed to stand up to the creditors and reject the budget cuts they have been demanding.
Before it can get 85 billion euros ($95.07 billion) in bailout cash and support for its banks to reopen, the Greek government will have to pass a raft of austerity measures that include sales tax increases, reforms to pensions, and labor market reforms.
Greece will be on a tight timetable to implement its reforms -- a reflection of how little its creditors trust the government to honor a deal. Greek Prime Minister Alexis Tsipras infuriated his European partners last month when he called for a popular vote against economic reforms the creditors has proposed.
The Greek people voted against those proposals, but will be horrified to see that they now face even tougher measures.
Both sides acknowledged the bitterness that marked their negotiations and kept them negotiating nine hours past a Sunday midnight deadline.
"Trust needs to be rebuilt," said German Chancellor Angela Merkel, adding that with the deal, "Greece has a chance to return to the path of growth."
In a first step toward getting its bailout loans, the Greek government has to pass a set of measures into law by Wednesday.
Measures include an increase in the sales tax and reform of the pension system. In later weeks, Greece will have to open to competition industries that have long been protected, such as the energy sector. Labor laws will be made more flexible.
If it meets these requirements, Greece will get a three-year rescue program and a commitment to restructure its debt, which is unsustainably high at around 320 billion euros, or around 180 percent of annual GDP.
Tsipras argues that because of these concessions Monday's deal is, despite the tough austerity, actually better for Greece than the proposals Greeks voted down just a week ago.
"We managed to avoid the most extreme measures," Tsipras said. "Greece will fight to return to growth and to reclaim its lost sovereignty."
He said he had managed to avoid a demand by some creditors to transfer Greek assets abroad as a form of collateral and to avoid the collapse of the banking sector.
Greeks seemed mainly relieved that the country was not facing financial collapse.
Kostas Lambos, a pensioner, said things would be "difficult in the beginning" but people had to understand the severity of the situation.
"This was a necessary step for the country to emerge from the dead ends that had been created in the last few years," he said.
Greece's banks, which have been shut for two weeks, were still closed on Monday and limits remained on cash withdrawals. Without a deal, they faced the prospect of collapse within days as they are steadily drained of money.
When the banks will be able to reopen will depend on whether the European Central Bank decides to increase emergency credit to Greek banks now that a bailout deal with Greece has been clinched in principle. It was unclear whether the ECB would make such a decision on Monday or after Greece passes its first batch of reforms.
French President Francois Hollande said the Greek parliament would convene within hours to adopt the reforms called for in the plan and he celebrated Greece's continued membership in the euro.
Losing Greece, he said, would have been akin to losing "the heart of our civilization."
Other European officials were less emotive.
"The Greeks have to show they're credible, show that they mean it," said Jeroen Dijsselbloem, president of the eurogroup of eurozone finance ministers and a longtime critic of the Tsipras government.
The creditors said they would help Greece in the short term deal with its debt repayments since any bailout agreement was not imminent. Greece will need help making a 4.2 billion euro debt repayment on July 20. It is also in arrears on about 1.5 billion euros owed to the International Monetary Fund since June 30.
If the talks had failed, Greece could have faced bankruptcy and a possible exit from the euro, the European single currency that the country has been a part of since 2002. No country has ever left the joint currency, which launched in 1999, and there is no mechanism in place for one to do so.
Greece had requested a three-year, 53.5 billion-euro ($59.5 billion) financial package, but that number grew larger by tens of billions as the negotiations dragged on and the leaders calculated how much Greece will need to stay solvent.
Greece has received two previous bailouts, totaling 240 billion euros ($268 billion), in return for deep spending cuts, tax increases and reforms from successive governments. Although the country's annual budget deficit has come down dramatically, Greece's debt burden has increased as the economy has shrunk by a quarter.