On Wednesday morning, as Mohamed Morsi sat discussing his plight with a small coterie of aides at a base in the east of Cairo, a senior adviser reassured him that the presidential guard would protect him no matter what.

But, as the Egyptian troops moved in on the base following the orders of army chief Abdul Fattah al-Sissi, even this elite unit slipped away, so Morsi could be easily detained. As with so many of the political errors that dogged his presidency, Morsi hadn't seen it coming.

The 3 July coup may have been executed by the military, but its roots lie in a civilian movement.

On the evening of 15 April, Mohammed Abdul Aziz and five other friends sat down in Borsa coffee shop in central Cairo to plot ways to invigorate Egypt's tired civil opposition.

According to Aziz, the group's aims were simple at first; to reignite support for a movement that had ground to a halt almost a year into the increasingly unpopular presidency of Morsi.

"In the beginning all we wanted to do was gather petitions to renounce Morsi," he said. But the group soon got a name, Tamarod (Rebel). Within weeks it had also gained a momentum that propelled it to centre stage of a defining period in Egypt's modern history – the ousting of the country's first democratically elected leader.

"I was sure by the number of petitions flowing that Tamarod was going to transform the Egyptian political scene," said Aziz.

The means seemed simple enough, not dissimilar to the campaign that led to the toppling of the previous president, Hosni Mubarak, 30 months ago. Smartphones, Facebook and other forms of social media were critical organising tools, but this time the boot leather of volunteers and old fashioned petitions also played a pivotal role.

"We had a website with an electronic petition and a space for people to put their name down and fill the form out," said Aziz. "They would then print the form out and give it to a volunteer."

By mid-May, he said, there were 8,000 volunteers in 15 of Egypt's 22 governorates.

"That's when it became a popular movement. That's when the idea became a reality."

Egypt's problems had been piling up since November, little more than three months into the four-year term of Morsi's government. Morsi had enjoyed the briefest of political honeymoons. The economy was in torpor, the body politic barely functioning and society deeply polarised.

On one side of a by now gaping divide was the Muslim Brotherhood, the powerful Islamic group that had largely been responsible for sweeping Morsi to power in elections last June.

On the other was the rest of the country — about 48% of voters, according to the poll, which gave Morsi the presidency with close to 52% of the popular vote.

The disaffected included a band of unlikely allies, who sit uneasily even now; at one end were the leftists and secularists, who had been squeezed in January 2011 by the Islamists, at the other those who resented the toppling of Mubarak.

The latter had been a formidable foe-in-waiting. Away from the sweeping scenes of Tahrir Square in January 2011, many millions of Egyptians were uncomfortable with Mubarak's demise. They had been safe under the dictator and some of them had prospered.

The 17 months after his ignominious exit had been unsettling for the Mubarak faithful. But the year since Morsi's inauguration had been even worse.

"It was becoming clear that everything that the state had built, everything that it had stood on, was coming crumbling down," said Ahmed Badawi, a mid-ranking police officer who was unhappy to see Mubarak go. "It was a case of 'my enemy's enemy is my friend, so we joined them in Tahrir Square this time'," he said of this week's revolts.

A senior western diplomat who had spent time with Morsi, his inner court and Brotherhood leaders said the writing was on the wall for his presidency by early this year.

"We had noticed particularly in the past nine months that they had become increasingly disconnected from reality. The army had become more and more worried by the [Brotherhood].

"The economy was being wrecked by the movement. They were spending at least $1.5bn per month more than they should have. They were using months and months of reserves at a critical level. You couldn't deny the underlying trend that the government was heading for bankruptcy.

"Whatever mess they had created was going to lead to civil revolt. Soon they wouldn't have been able to pay for civil servants' salaries."

By March, serious diplomatic efforts had started to convince Morsi to form a government of national unity.

"We were trying to convince them to broaden the base of political participation," said the diplomat. "After much negotiation, they declined and then went about making it even worse by maintaining a technocratic government run by newly promoted lower-grade officials with bad ideas. What did it for me was the appointment of the culture minister."

The nomination of Alaa Abdul Aziz led to the sacking of five key cultural figures, including the head of the opera house and the National Library and Archives, and a view that he was trying to impose an Islamist agenda on cultural institutions which had always been avowedly secular.

From every angle, Morsi was increasingly being seen as, a captive of his constituency. "By that time, the Tamarod movement was really becoming something," said the diplomat. "And that added a dynamism and sheer scope to what had been taking place."

By mid-June, with other state institutions now sharing the military's alarm, the tide was clearly turning against Morsi. Tamarod claimed to have received more than 20m petition signatures.

Within a week, citizens experienced shortages of essentials, especially food and fuel. Long queues for fuel are rare in Egypt, where the military has a significant stake in the gas and oil sector and is usually a guarantor of supply. But in the leadup to the first anniversary of Morsi's swearing in – June 30 – the date chosen by Tamarod for a march en masse to the place where it all began, Tahrir Square, the shortages seemed specially severe.

By then, the army had given Morsi the first ultimatum: find ways to end the crisis within a week. Unable to deliver, Morsi watched as the large crowds hoped for by the born-again opposition materialised.

The army posted statements on its Facebook site acknowledging "huge crowds of protesters" on the streets. Things were moving quickly now; when the first deadline expired, the Egyptian military chief, Abdul Fattah al-Sissi gave Morsi another deadline, this time 48 hours. It was to be his last as leader.

Last Saturday, with his political legacy crumbling, Morsi cut a serene figure when the Guardian met him in his office in Quba Palace, Cairo.

The streets of the capital were tense, but Morsi appeared cocooned, even oblivious to what had begun to take shape.

"How confident are you in the army?" the Guardian asked him. "Very," he replied. How wrong he was.