Media playback is unsupported on your device Media caption Is peace with the Taliban possible?

The US and the Taliban have signed an agreement aimed at paving the way towards peace in Afghanistan after more than 18 years of conflict.

Under the deal, the US and its Nato allies will withdraw all their troops from the country in 14 months if the hardline Islamic movement upholds its commitments to stop attacks.

But why is the US fighting a war in Afghanistan and why has it lasted so long?

On 11 September 2001, attacks in America killed nearly 3,000 people. Osama Bin Laden, the head of Islamist terror group al-Qaeda, was quickly identified as the man responsible.

The Taliban, radical Islamists who ran Afghanistan and protected Bin Laden, refused to hand him over. So, a month after 9/11, the US launched air strikes against Afghanistan.

As other countries joined the war, the Taliban were quickly removed from power. But they didn't just disappear - their influence grew back and they dug in.

Since then, the US and its allies have struggled to stop Afghanistan's government collapsing, and to end deadly attacks by the Taliban.

"We did not ask for this mission, but we will fulfil it," US President George W Bush said when he announced the first air strikes against Afghanistan on 7 October, 2001. The raids were in response to the 9/11 attacks, which killed 2,977 people in New York, Washington and Pennsylvania.

The mission, he said, was "to disrupt the use of Afghanistan as a terrorist base of operations and to attack the military capability of the Taliban regime".

The first targets were military sites belonging to the hardline Taliban group who ruled the country. Training camps for al-Qaeda, the terror group run by 9/11 plotter Osama Bin Laden, were also hit.

But 18 years on, it's hard to argue the US mission has been fulfilled - the Taliban may play a part in ruling Afghanistan again if peace talks do eventually succeed.

Media playback is unsupported on your device Media caption The BBC gained rare access to see life under the Taliban in 2017

The Taliban first took control of the capital Kabul in 1996, and ruled most of the country within two years. They followed a radical form of Islam and enforced punishments like public executions.

Within two months of the US and its international and Afghan allies launching their attacks, the Taliban regime collapsed and its fighters melted away into Pakistan.

A new US-backed government took over in 2004, but the Taliban still had a lot of support in areas around the Pakistani border, and made hundreds of millions of dollars a year from the drug trade, mining and taxes.

As the Taliban carried out more and more suicide attacks, international forces working with Afghan troops struggled to counter the threat the re-energised group posed.

In 2014, at the end of what was the bloodiest year in Afghanistan since 2001, Nato's international forces - wary of staying in Afghanistan indefinitely - ended their combat mission, leaving it to the Afghan army to fight the Taliban.

But that gave the Taliban momentum, as they seized territory and detonated bombs against government and civilian targets. In 2018, the BBC found the Taliban was openly active across 70% of Afghanistan.

Where did the Taliban come from?

Afghanistan had been in a state of almost constant war for 20 years even before the US invaded.

In 1979, a year after a coup, the Soviet army invaded Afghanistan to support its communist government. It fought a resistance movement - known as the mujahideen - that was supported by the US, Pakistan, China and Saudi Arabia, among other countries.

In 1989, Soviet troops withdrew but the civil war continued. In the chaos that followed, the Taliban (which means "students" in the Pashto language) sprang up.

Media playback is unsupported on your device Media caption Tens of thousands of Afghan soldiers have been killed and injured. This is their story

They first rose to prominence in the border area of northern Pakistan and south-west Afghanistan in 1994. They promised to fight corruption and improve security and, at that time, many Afghans were tired of the excesses and infighting of the mujahideen during the civil war.

It's thought the Taliban first appeared in religious schools, mostly funded by Saudi Arabia, which preached a hardline form of Islam.

They enforced their own austere version of Sharia, or Islamic law, and introduced brutal punishments. Men were made to grow beards and women had to wear the all-covering burka.

The Taliban banned television, music and cinema and disapproved of girls' education.

And because the Taliban gave shelter to militants from the al-Qaeda group, it made them an immediate target for an attack by US, Afghan and international forces in the wake of 9/11.

Why has the war lasted so long?

There are many reasons for this. But they include a combination of fierce Taliban resistance, the limitations of Afghan forces and governance, and other countries' reluctance to keep their troops for longer in Afghanistan.

At times over the past 18 years, the Taliban have been on the back foot. In late 2009, US President Barack Obama announced a troop "surge" that saw the number of American soldiers in Afghanistan top 100,000.

The surge helped drive the Taliban out of parts of southern Afghanistan, but it was never destined to last for years.

As a result, the Taliban were able to regroup. When international forces withdrew from fighting, Afghan forces left to lead the charge were easily overwhelmed. To make matters worse, Afghanistan's government, that is full of tribal division, is often hamstrung.

Media playback is unsupported on your device Media caption The BBC was given exclusive access to spend a week with ambulance workers in Afghanistan.

The BBC World Service's Dawood Azami says there are five main reasons the war is still going on now. They include:

a lack of political clarity since the invasion began, and questions about the effectiveness of the US strategy over the past 18 years;

the fact each side is trying to break what has become a stalemate - and that the Taliban have been trying maximise their leverage during peace negotiations

an increase in violence by Islamic State militants in Afghanistan - they've been behind some of the bloodiest attacks recently

There's also the role played by Afghanistan's neighbour, Pakistan.

There's no question the Taliban have their roots in Pakistan, and that they were able to regroup there during the US invasion. But Pakistan has denied helping or protecting them - even as the US demanded it do more to fight militants.

How have the Taliban managed to stay so strong?

The group could be making as much as $1.5bn (£1.2bn) a year, a huge increase even within the past decade. Some of this is through drugs - Afghanistan is the world's largest opium producer, and most opium poppies - used for heroin - are grown in Taliban-held areas.

But the Taliban also make money by taxing people who travel through their territory, and through businesses like telecommunications, electricity and minerals.

Foreign countries, including Pakistan and Iran, have denied funding them, but private citizens from the region are thought to have done so.

How costly has the war been?

Extremely.

It's difficult to say how many Afghan troops have died - the numbers are no longer published. But in January 2019, Afghan President Ashraf Ghani said 45,000 members of the security forces had been killed since 2014.

Nearly 3,500 members of the international coalition forces have died since the 2001 invasion, more than 2,300 of them American.

The figures for Afghan civilians are more difficult to quantify. A UN report in February 2019 said more than 32,000 civilians had died. The Watson Institute at Brown University says 42,000 opposition fighters have died.

The same institute says conflicts in Iraq, Syria, Afghanistan and Pakistan have cost the US $5.9 trillion since 2001.

The US is still conducting air strikes against the Taliban, instigated by the third president to oversee the war, Donald Trump. But he is keen to reduce troop numbers before he faces another election in November 2020.

The Taliban now control much more territory than they did when international troops left Afghanistan in 2014.

Many in Washington and elsewhere fear that a full US troop pull-out would leave a vacuum that could be filled by militant groups seeking to plot attacks in the West.

The Afghan people, meanwhile, continue to bear the brunt of the long and bloody conflict.