Saturday night had turned into Sunday morning and four days of talks over Iran's nuclear programme had already gone so far over schedule that the Geneva Intercontinental Hotel had been given over to another event.
A black tie charity ball was ending and singers with an after-party band at a bar above the lobby were crooning out the words to a Johnny Cash song - "I fell into a burning ring of fire." In nearby conference rooms, weary diplomats tried to polish off the last touches of an accord. Negotiators emerged, complaining that the hotel lobby smelled like beer.
At around 2am, US Secretary of State John Kerry and counterparts from Britain, China, France, Germany and Russia were ushered to a conference room to approve a final text of the agreement which would provide limited sanctions relief on Iran in return for curbs to its nuclear programme.
At the last minute, with the ministers already gathered, an Iranian official sought changes. Negotiators for the global powers refused. Finally, the ministers were given the all clear. With the interim deal signed, talks are still not over as the parties work towards a final accord that would settle all doubts about Iran's nuclear programme.
"Now the really hard part begins," Kerry told reporters.
The deal represents the most important thaw between the United States and Iran in more than three decades since Iranian revolutionaries stormed the US embassy in Tehran and seized 52 American hostages. Yet it very nearly did not happen.
Officials from several of the countries doubted that a deal would be reached. European diplomats groused that their foreign minister bosses had not wanted to come unless a final text was on the table, but had felt obliged to show when Russia's Sergei Lavrov appeared Friday. Some junior diplomats and journalists were evicted from their hotel rooms to clear space for the VIPs.
After his trans-Atlantic flight on Saturday morning, Kerry met his Iranian counterpart, Mohammad Zarif, with European Union foreign policy chief Catherine Ashton, who has led negotiations on behalf of the powers.
According to a US State Department official, Kerry told Zarif there could be no more delays. President Barack Obama's administration would call for even tighter restrictions on Iran unless they reached a deal immediately. Congress members were demanding new sanctions and the White House would join them.
Kerry made the case that "there would be no way to hold back new sanctions … and we would lead the charge for more sanctions if we did not come to agreement," the State Department official said.
By Saturday evening, the final language was personally approved by Obama in Washington. In a sign of how big a risk the Obama administration was taking, the main US ally in the Middle East, Israel, decried what it called an "historic mistake", easing sanctions without dismantling Iran's nuclear programme.
But Obama said the deal put limits on Iran's nuclear programme that would make it harder for Tehran to build a weapon and easier for the world to find out if it tried.
"Simply put, they cut off Iran's most likely paths to a bomb," Obama said after the deal was reached.
Obama was not the only one taking a risk. Iran's new president, the relative moderate Hassan Rowhani was elected in June and inaugurated in August promising to ease the crippling sanctions. But Iran has invested billions of dollars in a nuclear programme, which its clerical and military establishment believes is a point of national pride.
Before Zarif was sent to Geneva, he and Rowhani had a meeting with Iran's supreme leader, Ayatollah Ali Khamenei, whose approval was absolutely required for any deal.
"The leader's main concern is his core supporters, who truly believe that there should be no deal with America, and are closely watching the developments to find a weak point or a failure to blame on the negotiators for betraying the leadership," said a former Iranian official, a relative of Khamenei.
The deal was in part the result of months of secret talks held with Iran in such places as Oman, with US officials using military planes, side entrances and service elevators to avoid giving the game away.
The talks were the most important contacts in more than three decades, during a time when Iran branded the US the "Great Satan" and the US described Iran a part of an "axis of evil". The negotiations included Iraq and North Korea.
They illustrate a US desire, dating to the start of Obama's administration in January 2009, to explore whether there might be a way to reconcile two nations that have been hostile since 1979 but were once allies.
According to a US official, who spoke on condition of anonymity, key Americans involved in the effort were William Burns, the US deputy secretary of state, and Jake Sullivan, the national security adviser to US Vice President Joe Biden.
The two men, at times with other officials such as White House national security staff member Puneet Talwar, met Iranian officials at least five times this year, the official said.
Burns, Sullivan and technical experts arrived in Muscat, Oman, in March on a military plane - a way to preserve secrecy - to meet Iranians, the official added.
That was months before the election of Rowhani, a sign that Iranian officials were already coming round to the idea of talks before he took power.
Rowhani defeated more hardline candidates based partly on hopes that he would ease sanctions that had taken an increasing severe toll on the Iranian economy since they were tightened by the US and European Union to hit Iran's crucial oil exports since 2011.
A former nuclear negotiator, Rowhani replaced the combative Mahmoud Ahmadinejad. But ultimately no negotiations would have been possible without a nod from the supreme leader, Khamenei.
"The leader gave the green light but was not optimistic about the result," said a former Iranian official, who participated in one round of the secret talks. He said the hardest meeting was the first one because of Khamenei's scepticism.
The Oman channel had been nurtured by Kerry, who, as chairman of the US Senate Foreign Relations Committee before he became secretary of state, made an unannounced trip to the Gulf state to meet Omani officials.
After Kerry replaced Hillary Clinton as the top US diplomat on February 1, it was decided that the Oman channel would continue to help feed into multi-lateral talks led by the EU's Ashton on behalf of the five permanent UN Security Council members plus Germany.
Around the time that Kerry was taking over the State Department, Zarif's predecessor, Ali Akbar Salehi - then serving as foreign minister under Ahmadinejad - sent an extraordinary three-page, hand-written letter to Khamenei, calling for "broad discussions" with the US.
The supreme leader, though cautious about the prospect, sent a reply to Salehi and the rest of the cabinet: he was not optimistic but would not oppose them if they pursued the initiative, several sources said.
The senior US official said that four of the secret American-Iranian meetings took place since Rowhani's August inauguration, a sign that Washington was trying to exploit the opportunity presented by the Iranian official's ascent.
Kerry met Iran's foreign minister at the UN General Assembly in September and, soon thereafter, Obama and Rowhani spoke by telephone, marking the highest-level contact between the US and Iran since the 1979 Islamic Revolution.
Kerry also spoke to the Iranian foreign minister by telephone on October 25 and November 2 - discussions that were not revealed by the State Department at the time.
Before heading to Geneva, Zarif had a crucial meeting with Khamenei in the presence of Rowhani, a senior member of the Iranian delegation said.
"The leader underlined the importance of respecting Iran's right to enrich uranium and that he was backing the delegation as long as they respected this red line," said the delegate.
Iranian officials can point to the mention of an enrichment programme as a victory that shows they will be allowed to keep it. Western officials say it means no such thing and emphasise all the limits described in the text.
The interpretive differences underscore how difficult it may be to move towards a final deal that would resolve differences once and for all. Progress could easily be stymied.
Still, for those on both sides committed to the agreement, it represented an historic victory.
"We took a risk," said the former Iranian official involved in the secret talks. "But we won."
ASIAN OIL CUSTOMERS: Sanctions on Iran’s oil exports will remain in place during the six-month period covered by the deal, but world powers promise no new economic measures against Tehran as long as compliance proceeds – good news for energy-hungry Asian economies such as India, China and Japan, which have US waivers to continue Iranian oil imports. Further talks could peel back wider oil sale restrictions.
DUBAI: The Gulf city’s ports and air cargo terminals were once brimming with Iran-bound goods. Sanctions have sharply cut into the traditional trade and livelihood of many in the large Iranian expatriate community in Dubai. Anything that brings back Iranian business, even in limited steps, is welcome in Dubai.
IRAN’S PRESIDENT: Rowhani often pitched the nuclear talks as a potential for a “win-win” outcome with the West. On one level, he got his take by securing a deal that allows Iran to maintain uranium enrichment – although at lower levels.
ISRAEL: Jerusalem says the deal is a mistake and puts Israel in greater peril. Many Israeli officials, including Prime Minister Benjamin Netanyahu, see Iran’s ability to enrich uranium as a recipe for potential disaster. Netanyahu must now try to mend relations with Washington and weigh the significant risks of turning his back on the West and considering unilateral military options.
SAUDI ARABIA: The Iran deal favours the oil-rich kingdom’s regional rival and diminishes the Gulf role in US policy. EGYPT: The military-backed leaders in Cairo have rolled back much of the Iran outreach by the Muslim Brotherhood-led government ousted in July. The nuclear deal and the possibility of improved US-Iran dialogue could cut into Egypt’s traditional standing as the West’s guiding force in the region.