(Changes Iraq to Iran in 46th paragraph)
By Louis Charbonneau, Parisa Hafezi and Arshad Mohammed
GENEVA, Nov 25 (Reuters) - 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 finishing up 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" - while weary diplomats in nearby conference rooms were trying to polish off the last touches of an accord. Negotiators emerged complaining that the hotel lobby smelled like beer.
At around 2:00 a.m., U.S. Secretary of State John Kerry and counterparts from Britain, China, France, Germany and Russia were brought to a conference room to approve a final text of the agreement which would provide limited relief of sanctions on Iran in return for curbs to its nuclear programme.
At the last minute, with the ministers already gathered in the room, an Iranian official called seeking changes. Negotiators for the global powers refused. Finally the ministers were given the all clear. The deal, a decade in the making, would be done at last.
Now that the interim deal is signed, talks are far from over as the parties work towards a final accord that would lay to rest all doubts about Iran's nuclear programme.
"Now the really hard part begins," Kerry told reporters. "We know this."
The deal, which represents the most important thaw between the United States and Iran in more than three decades since Iranian revolutionaries held 52 American hostages in the U.S. embassy in Tehran, very nearly did not happen.
There was still ample ground to cover on the final day, when U.S. Secretary of State John Kerry arrived, joining foreign ministers from Britain, China, France, Germany and Russia.
Officials from several of the countries were doubtful that a deal would be reached. Resentful-sounding European diplomats said their foreign minister bosses had not wanted to come unless a final text was on the table, but had felt obliged to come anyway when Russia's Sergei Lavrov showed up on Friday.
When the foreign ministers arrived, 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 opposite number Mohammad Zarif, with European Union foreign policy chief Catherine Ashton, who has led negotiations on behalf of the powers.
According to a senior U.S. State Department official, Kerry told Zarif there could be no more delay. President Barack Obama's administration would call for even tighter sanctions on Iran unless a deal was reached now. 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 to give room for (a) new round 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 U.S. 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 down 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 in a late-night appearance at the White House after the deal was reached.
Obama was not the only one taking a risk. Iran's new president, the relative moderate Hassan Rouhani, 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 cornerstone of national pride.
Before Zarif was sent to Geneva, he and Rouhani 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 out-of-the-way places as Oman, with U.S. officials using military planes, side entrances and service elevators to avoid giving the game away.
The talks, the most important contacts in more than three decades during which Iran branded the United States the "Great Satan" and the United States described Iran a part of an "axis of evil" that also included Iraq and North Korea, were confirmed by U.S. officials and a former Iranian official.
They illustrate a U.S. 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 the U.S. official, who spoke on condition of anonymity, key Americans involved in the effort were William Burns, the U.S. deputy secretary of state, and Jake Sullivan, the national security adviser to U.S. 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 Rouhani, a sign that Iranian officials were already coming round to the idea of talks before he took power.
Rouhani defeated more hardline candidates based in part on hopes he would ease sanctions that had taken an increasing severe toll on the Iranian economy since they were sharply tightened by the United States and European Union to hit Iran's crucial oil exports since 2011.
A former nuclear negotiator, Rouhani 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 itself had been nurtured by Kerry, who, as chairman of the U.S. Senate Foreign Relations Committee before he took over as Secretary of State, made an unannounced trip to the Gulf state to meet Omani officials.
After Kerry replaced Hillary Clinton as the top U.S. diplomat on Feb. 1, it was decided the Oman channel would continue to help feed into multi-lateral talks led by the EU's Ashton on behalf of the five permanent U.N. Security Council members plus Germany, the P5+1. Kerry visited Oman himself in May for talks with Omani officials.
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 United States".
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.
"Salehi endangered his career - and even his security," said a source who knows Salehi and saw the letter. "But he said this letter will be registered in history." In August, Rouhani put Salehi in charge of Iran's nuclear agency.
The senior U.S. official said that four of the secret U.S.-Iranian meetings took place since Rouhani's August inauguration, a sign that the United States was trying to exploit the opportunity presented by the Iranian official's ascent.
Kerry met Iran's foreign minister at the U.N. General Assembly in September and, soon thereafter, Obama and Rouhani spoke by telephone, marking the highest-level contact between the United States and Iran since the 1979 Islamic Revolution.
Kerry also spoke to the Iranian foreign minister by telephone on Oct. 25 and Nov. 2 - discussions that were not revealed by the State Department at the time.
In recent months there has been noticeable change in body language when diplomats from the United States and Iran are in the same room. Whatever the relations between their countries, officials from both sides now appear - normal.
During talks in Geneva earlier this month, Reuters spotted U.S. Under-Secretary of State for Political Affairs Wendy Sherman chatting alone in a hotel lobby with Iran's Deputy Foreign Minister Abbas Araqchi. Such casual, cordial meetings in public would have been unthinkable just months ago.
Nevertheless, the United States was so eager to keep the role of Burns and Sullivan secret that it brought them to Geneva twice this month for wider talks between Iran and the major powers but left their names off the official delegation list and made them use hotel side entrances and service elevators to keep the secret.
When the time came for the final push in Geneva, diplomats expected their bosses would not show up until the text was nearly complete. Journalists waited drinking $9 capuccinos and $29 bloody marys at the Intercontinental.
Even after the foreign ministers arrived, officials sounded downbeat about the prospected of a deal on the final day.
"It's not a done deal. There's a realistic chance but there's a lot of work to do," said German Foreign Minister Guido Westerwelle.
One final bone of contention was the Iranian heavy water reactor at Arak, where Western countries suspect Tehran could one day make plutonium for a bomb.
"Defining limits on that and what should take place there in this six month period has proved to be quite a task," British Foreign Secretary William Hague said. "However, that has now been agreed. It was the resolution of that problem that helped unlock the agreement."
French officials had been holding out in public for a tough line on Iran, although several Western diplomats said the French were more flexible behind closed doors.
The Arak issue was tough, but it wasn't the toughest. Iran and the powers would still have to find language that both sides could find acceptable over what Iran considers its fundamental right to enrich uranium.
Before heading to Geneva, Zarif had a crucial meeting with Khamenei in the presence of Rouhani, 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.
According to another source in Iran, Zarif and Rouhani, along with their top allies, later held a three-hour meeting and discussed various "face-saving solutions" of wording designed to be acceptable to both sides.
Sunday's agreement said Iran and the major powers aimed to reach a final deal that would "involve a mutually defined enrichment programme with mutually agreed parameters consistent with practical needs, with agreed limits on scope and level of enrichment activities, capacity, where it is carried out, and stocks of enriched uranium, for a period to be agreed upon."
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 differences in interpretation 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 who participated in the secret talks with the United States. "But we won." (Additional reporting by John Irish and Justyna Pawlak; Writing by Peter Graff; Editing by Grant McCool)