Cairo - The head of Egypt's military, General Abdel-Fattah el-Sisi, sat with a polite smile in the front row listening to President Mohammed Morsi give a 2.5-hour speech defending his year in office.
El-Sisi even clapped lightly as the audience of Morsi supporters broke into cheers.
It was a calculating display of cool by an army general plotting the overthrow of his commander in chief. Just over a week later, el-Sisi slid in the knife, announcing Morsi's ousting on state TV on 3 July as troops took the Islamist leader into custody.
The move was the culmination of nearly a year of acrimonious relations between el-Sisi and Egypt's first freely elected - and first civilian - president.
A series of interviews by The Associated Press with defence, security and intelligence officials paint a picture of a president who intended to flex his civilian authority as supreme commander of the armed forces, issuing orders to el-Sisi.
In turn, the military chief believed Morsi was leading the country into turmoil and repeatedly challenged him, defying his orders in at least two cases.
The degree of their differences suggests that the military had been planning for months to take greater control of the political reins in Egypt.
When an activist group named Tamarod began a campaign to oust Morsi, building up to protests by millions nationwide that began 30 June, it appears to have provided a golden opportunity for el-Sisi to get rid of the president.
The military helped Tamarod from early on, communicating with it through third parties, according to the officials.
The reason, the officials said, was because of profound policy differences with Morsi. el-Sisi saw him as dangerously mismanaging a wave of protests early in the year that saw dozens killed by security forces.
More significantly, however, the military also worried that Morsi was giving a free hand to Islamic militants in the Sinai Peninsula, ordering el-Sisi to stop crackdowns on jihadis who had killed Egyptian soldiers and were escalating a campaign of violence.
"I don't want Muslims to shed the blood of fellow Muslims," Morsi told el-Sisi in ordering a halt to a planned offensive in November, retired army General Sameh Seif el-Yazl told AP. Seif el-Yazl remains close to the military and sometimes appears with el-Sisi at public events.
And at root, the military establishment has historically had little tolerance for the Muslim Brotherhood, Morsi's Islamist group. The military leadership has long held the conviction that the group puts its regional Islamist ambitions above Egypt's security interests.
Its alliances with Gaza's Hamas rulers and other Islamist groups alarmed the military, which believed Gaza militants were involved in Sinai violence. The officials said the military leadership also believed the Brotherhood was trying to co-opt commanders to turn against el-Sisi.
The military has been the most powerful institution in Egypt since officers staged a 1952 coup that toppled the monarchy. Except for Morsi, the military has since given Egypt all of its presidents and maintained a powerful influence over policy. Having a civilian leader over the military was entirely new for the country.
The Brotherhood accuses el-Sisi of turning against them and carrying out a coup to wreck democracy. Since being deposed, Morsi is detained by the military at an undisclosed Defense Ministry facility.
The Brotherhood had believed that el-Sisi was sympathetic with their Islamist agenda. A senior Brotherhood official told AP that Morsi installed el-Sisi, then the head of military intelligence, as defence minister and head of the armed forces in August 2012 in part because he had been the contact man between the Brotherhood and the military junta that ruled Egypt for nearly 17 months after the February 2011 fall of autocrat Hosni Mubarak.
El-Sisi spoke of his differences with Morsi for the first time Sunday when he addressed military officers in a meeting that was partially televised.
"I don't want to count to you the number of times that the armed forces showed its reservations on many actions and measures that came as a surprise," el-Sisi said.
Along with the Brotherhood official, eight current senior officials in the military, military intelligence and Interior Ministry - including a top army commander and an officer from el-Sisi's inner circle - spoke to AP on condition of anonymity because they were not authorised to discuss the events between Morsi and the military.
They recounted tense conversations and meetings with a frustrated Morsi frequently reminding the military chief of his rank as supreme commander.
‘The people have demands’
As early as April, the army drew up a contingency plan to assert control of the nation by taking charge of security if street violence escalated out of Morsi's control, the intelligence and defence officials said.
The plan did not entail removing Morsi. Instead, it was an expansion of the role the army took in the Suez Canal city of Port Said, which by that time had seen months of anti-Morsi protests that evolved into an outright revolt.
More than 40 protesters had been killed by police there, as Morsi publicly urged security forces to deal strongly with the protests. The military was deployed in the city, largely welcomed by the residents, who continued protests and strikes.
The military officials said Morsi had ordered the army to get tougher on protesters, but el-Sisi refused, telling him, "The people have demands".
About this time, in April and May, el-Sisi's officials met with commanders of the Republican Guard, the army branch that protects the president. The commanders told them that Morsi's aides were trying to co-opt Guard officers and senior army officers in a move to replace el-Sisi, according to the official in the military chief's staff.
Each side's suspicions were fuelled by leaks in the press with anonymous Brotherhood and military officials quoted as criticising the other. In meetings, Morsi assured el-Sisi that he had no intention of firing him, saying, "These are just rumours," the defence officials said. El-Sisi told Morsi that the military leaks were merely "newspaper talk".
In April, the youth activists of Tamarod, Arabic for "Rebel," began collecting signatures on a petition for Morsi to step down. When it said it had 2 million signatures in mid-May, the military took an interest and worked through third parties that connected the group with liberal and opposition-linked businessmen who would bank it, said two high-ranking Interior Ministry officials.
The campaign claimed in June to have more than 20 million signatures - though the number has never been independently confirmed - and called for mass rallies against Morsi to start 30 June, the anniversary of his inauguration.
El-Sisi issued a statement saying the armed forces would intervene to stop any violence at the protests, particularly to stop Morsi supporters from attacking the rallies. He gave the two sides a week to resolve their differences - with the deadline being 30 June.
The protection plan appeared to be an evolution of the original contingency plan set up in April, and it was widely seen as a show of support for the protesters.
Morsi summoned el-Sisi to explain his statement, and the general reassured him that "this was designed to calm people down," the Brotherhood official said.
"He did not show his true intentions until 1 July when he gave the president a 48-hour ultimatum," said the official, referring to a second ultimatum from el-Sisi that explicitly demanded Morsi find a solution with his opponents or the military would intervene.
Soon after the first deadline was issued, two Morsi aides called the commander of the 2nd Field Army, Major General Ahmed Wasfi, based in the Suez Canal region, and sounded him out about installing him in el-Sisi's place, the military officials said. Wasfi immediately informed el-Sisi of the call, they said.
Seif el-Yazl and the military and intelligence officials said security in the strategic Sinai Peninsula bordering Gaza and Israel was at the heart of the differences. The region plunged into lawlessness after Mubarak's ousting, with Islamic militants gaining increasing power.
Soon after Morsi took office, militants killed 16 Egyptian soldiers in a single attack and smaller-scale shootings on security forces mounted. In May, six policemen and a soldier were kidnapped.
Morsi in each case vowed action, but he and his aides also spoke publicly on the need for restraint and dialogue. At one point, he publicly acknowledged holding the military back from a raid to prevent civilian casualties, and he also spoke of the need not to harm the kidnappers as well as the captives.
Morsi's ultraconservative Salafi allies mediated with militant groups to get them to halt violence, although attacks continued.
In November, Morsi ordered el-Sisi to halt a planned Sinai offensive a day before it was to be launched, and el-Sisi complied, Self el-Yazl said. In May, the kidnappers released their captives after a week, apparently after mediation.
Morsi vowed publicly to track them down, but the military officials said the president ordered el-Sisi to pull his forces out of the area where they were believed to be. Again, the military complied. The kidnappers have not been caught.
The security and intelligence officials said they reported to Morsi about a rising number of foreign jihadis, including Palestinians, entering Sinai. The military identified Gazan militants involved in the killing of the 16 soldiers, but Morsi rejected a request by el-Sisi that he ask Hamas to hand them over for trial, the officials said. Hamas has repeatedly denied any role in the killings.
Morsi instead ordered el-Sisi to meet with Hamas leader Khaled Meshaal to discuss the issue. El-Sisi refused, because of the military's long-time view of Hamas as a threat, said the officials.
The military saw the policy of dialogue as being rooted in the Brotherhood's sympathy to others in the Islamist movement, even ones engaged in violence. Another incident deepened the military's belief that Morsi was more interested in a regional Islamist agenda than what the army saw as Egypt's interests.
During an April visit to Sudan, which has an Islamist government, Morsi showed flexibility over the fate of a border region claimed by both countries. After Morsi's return, el-Sisi sent his chief of staff to Khartoum to "make it crystal clear to the Sudanese that the Egyptian armed forces will never surrender" the territory, one defence official said.