LAUSANNE, Switzerland (Reuters) - Iran and the West could not have agreed a framework nuclear deal this week without the backing of Supreme Leader Ayatollah Ali Khamenei, who set aside his deep mistrust of the United States to end decades of Iranian isolation.
Iran and world powers reached the framework agreement on Thursday on curbing Iran's nuclear programme for at least a decade, a step towards a final pact following 12 years of brinkmanship, threats and confrontation.
President Hassan Rouhani, a relative moderate elected in 2013, has led the efforts to normalise relations with the outside world. But under Iran's political system, it is the 75-year-old conservative cleric Khamenei who is the ultimate power in the country.
Despite frequently declaring in public that he believed the negotiations would fail, Khamenei gave Rouhani crucial political cover to pursue long and arduous talks against the interests of powerful domestic opponents.
Khamenei became supreme leader in 1989 following the death of Ayatollah Ruhollah Khomeini, who led the revolution to overthrow the pro-Western Shah ten years earlier. Despite lacking Khomeini's revolutionary authority and charisma, Khamenei has held on to power for 25 years by deftly balancing the interests of numerous factions.
In allowing a deal with enemies, he is taking a step similar to his predecessor's agreement to a 1988 cease-fire with Iraq after an eight-year war, which Khomeini compared at the time to drinking a cup of poison.
Iran has suffered under economic sanctions for decades, especially over the last three years when much tighter U.S. and EU measures sharply curtailed the oil exports that are the foundation of its economy.
Throughout, Khamenei championed the idea of a "resistance economy" and framed Tehran's nuclear programme as a symbol of sovereignty that it could never abandon at any cost.
His speeches are still larded with denunciations of the "Great Satan", the United States.
Nevertheless, when the public elected Rouhani in a landslide two years ago, he gave his blessing to negotiations. The supreme leader may genuinely have come to see engagement as the best option for Iran as international pressure mounted, despite his rhetoric to the contrary and instinctive mistrust of the West.
Even as the negotiations entered the final stretch, Khamenei continuously stressed that he expected them to fail. The talks were a distraction, he said; a thin veil draped over implacable U.S. enmity.
"They know that we are not pursuing nuclear weapons, rather they use it as an excuse to pressure the Iranian people," he said in a speech last month, to cries of "Death to America" from the crowd.
When Republican senators wrote a letter to Iran in March warning that they could undercut any deal struck by President Barack Obama, Khamenei took this as further evidence of the futility of the talks.
"Every time we reach a stage where the end of the negotiations is in sight, the tone of the other side, specifically the Americans, becomes harsher, coarser and tougher. This is the nature of their tricks and deceptions," he said.
Diplomats speculate that Khamenei and other conservative figures went along with Rouhani largely due to the president's strong electoral mandate for international engagement and public anger over years of economic mismanagement.
In 2009, the largest opposition demonstrations since the 1979 Islamic Revolution filled the streets. While that uprising was swiftly put down, it rattled conservatives.
Khamenei's decision to support the diplomatic process, even tentatively, was a remarkable turnaround for a man who has disdained the United States and its allies, which he calls "the arrogant powers", throughout his life.
He was a teenager when Western intelligence agencies backed a coup against the nationalist prime minister Mohammad Mosaddegh in 1953.
Ten years later Khamenei, by then a student of Khomeini and a young agitator against the Shah's rule, served the first of many prison terms that would see him put under "severe torture", according to his official biography.
After the fall of the Shah in 1979, Khamenei took up several posts in the new Islamic government. He was elected president in 1981, promising in his inaugural address to eliminate "deviation, liberalism, and American-influenced leftists".
Scholars outside Iran describe the supreme leader as a secretive ideologue fearful of betrayal, an anxiety fuelled by an assassination attempt in June 1981 that paralysed his right arm.
He became close to the military, in particularly the elite Revolutionary Guards, a relationship that paid off when the Guards crushed the 2009 protests.
Khamenei derived further power by expanding Setad, a holding organisation founded by Khomeini, into a vast financial empire with assets worth tens of billions of dollars.
Editing by Peter Graff