Imran Khan has been ousted as Pakistan's prime minister after a vote of no confidence in parliament. But what led to his downfall?
When Imran Khan was elected prime minister in 2018, he seemed to have almost everything in his favour.
A national hero from his cricketing days, he had transformed into a charismatic politician and, after years of struggle, managed to supplant the two rival established political dynasties that had dominated Pakistan for decades.
He emerged as a fresh force, with vibrant rallies full of catchy songs which, along with his huge social media presence, amplified his staunch anti-corruption message. Mr Khan promised to bring "change" to the country, creating a "new Pakistan".
No prime minister has ever completed a full five-year parliamentary tenure in Pakistan, and Imran Khan looked as though he could well be the first.
The reason his position appeared so secure, however, also helps explain his downfall. Both sides deny it, but it's widely acknowledged he came to power with the help of Pakistan's powerful army and intelligence services - and now he has fallen out with them.
Mr Khan undoubtedly had significant, genuine public support in 2018.
But he also had the covert backing of what in Pakistan is referred to as "the establishment" or the military. The army has either directly or indirectly controlled the country for most of its existence, and critics labelled Imran Khan's government a "hybrid regime".
The support for Mr Khan exemplified itself in a host of different ways. During the 2018 election campaign, media outlets reporting sympathetically on his opponents had their distribution curtailed, while some candidates standing for election were either cajoled or coerced into joining his party.
"He was made by them," one defecting member of Imran Khan's party told the BBC, referring to the military. "They were the ones that brought him into power."
His chief opponent, Nawaz Sharif, was first disqualified and then convicted on corruption charges. Many suspected that Mr Sharif had indeed been involved in corruption in the past - but the real reason he was being punished at this stage was his own falling out with the army.
Mr Sharif had started his political career as a protégé of a military dictator before later growing more independent and invoking the ire of the "establishment". He has always denied being involved in corruption, claiming the charges are politically motivated.
After coming to power, Mr Khan, by contrast, proudly proclaimed he and the army were on "one page" when it came to policy decisions.
The results worried civil society activists, with a spate of attacks and abductions targeting journalists and commentators critical of both Mr Khan's government and the intelligence services. They both denied involvement, but no other culprit was ever identified.
Mr Khan has insisted his focus is on improving governance, and he has made some impressive expansions to the social welfare system, introducing a health insurance scheme in large parts of the country, for example.
However, in other areas he has faltered. His decision to appoint an inexperienced and underqualified political newcomer to a key position, chief minister of Punjab, the country's most populous province, was widely ridiculed.
At a loss to explain why Mr Khan refused to replace his appointee, Usman Buzdar, despite overwhelming criticism, rumours spread that the prime minister's wife, a spiritual guide of sorts, had warned him Mr Buzdar was a good omen and - if he were to be sacked - his entire government would collapse.
There were other challenges, too. The cost of living in Pakistan has been rocketing up, with sharp rises in food prices and the rupee falling against the dollar.
Imran Khan's supporters blame global conditions, but public resentment against him has been rising. "The Sharifs might've filled themselves up, but at least they got work done," has become a common grumble.
Still, for a while, the prime minister looked to be the best bet for the military. He cut an impressive figure on the world stage and his decision not to order a complete lockdown during the coronavirus pandemic was vindicated by fewer than expected deaths - though no-one can still be exactly sure why.
Meanwhile, his opponents were becoming increasingly vocal in their opposition to the military, naming the army chief, Gen Bajwa, and the head of the intelligence services (ISI), Lt Gen Faiz Hameed, as being responsible for "selecting" Imran Khan to office.
The dynamic changed dramatically last year. A number of observers told the BBC the army began to grow increasingly frustrated with Mr Khan's failure to deliver good governance, particularly in Punjab, and perhaps at how they were being publicly blamed for bringing him into power by the opposition.
Most crucially, a rift began to appear between Gen Bajwa and Lt Gen Faiz Hameed, who was widely seen as hoping to become the next army chief.
Lt Gen Hameed was apparently so confident of his prospects that he had even previously told officials in neighbouring Afghanistan he would be the next man in charge of the army.
However, one source close to the military said that while Lt Gen Hameed was seen as someone who could handle "dirty jobs" effectively, a reference to manipulating politicians or silencing critics, he was not seen as someone fit "to lead the institution".
The tension between the two powerful figures was noticed during a private interaction with influential commentators last summer. One journalist asked a question, only to be told by the head of the ISI that time had run out.
"I'm the chief," interjected Gen Bajwa curtly, "and I'll decide when we're done," before proceeding to take the question and answer it at leisure.
In October, the dispute escalated and enveloped Imran Khan. Gen Bajwa was understood to want a new man in charge of the intelligence services, and the army announced a change in roles.
Mr Khan, however, who had developed a close relationship with Lt Gen Faiz Hameed, resisted, apparently wanting him to stay on until elections had taken place - the assumption being that Lt Gen Hameed could once again help ensure Mr Khan's victory.
The prime minister held off issuing a formal notification approving the change of posting for nearly three weeks before eventually relenting. The now visible cracks between the military and Imran Khan's government emboldened the opposition.
When they began plotting a vote of no confidence, and sounding out potential defectors from within his party and coalition allies, a number of sources told the BBC, the military made clear that they were going to be "neutral" on this occasion.
One defecting member of Mr Khan's party told the BBC he and other parliamentarians used to receive "calls" from the intelligence services, directing them on what to do.
"We used to be manhandled," he said indignantly. However, once Lt Gen Faiz Hameed left office, the "calls" stopped. "Now the army is not interfering," he added.
Journalist Kamran Yousaf told the BBC that the military had been involved in "managing" Imran Khan's allies and slim majority in government. "Once that support was missing, his downfall was inevitable," he said.
Further differences have emerged between Mr Khan and the army, too, notably on foreign policy. Although he defended visiting Moscow on the day Russian troops crossed into Ukraine and brusquely rejected attempts by Western officials to issue a condemnation of President Vladimir Putin's behaviour, Gen Bajwa said last week that the invasion "must be stopped immediately".
Mr Yousaf said Imran Khan had also earlier "scuttled" an attempt championed by Gen Bajwa to partially restore trade with regional rival India, "because of the political cost". Ironically, previous civilian governments have fallen foul of the Pakistani military because they have been in favour of improving ties with India - but at that stage, the military wasn't.
Mr Khan, however, has repeatedly indicated he will not give up the fight. He alleges he is the victim of a US-led attempt to affect "regime change" in Pakistan, because of the anti-Western tilt in his foreign policy, which has included criticism of America's war in Afghanistan.
Most analysts have dismissed this as a deliberate exaggeration of a diplomatic cable sent by the Pakistani ambassador in Washington.
It's a narrative, however, that does appear to be resonating with Mr Khan's supporters, tapping into a reservoir of anti-Americanism in the country.
Ironically, the Pakistani army has itself fuelled an atmosphere conducive to conspiracy theories, by repeatedly warning of the dangers of vague "foreign plots" or "foreign agendas".
Mr Khan once told his cricket team to "fight like cornered tigers" and he looks set to be a formidable character in opposition.