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

Sixteen years after they were ousted in the US-led invasion, the Taliban have fought their way back to control swathes of Afghanistan. The country remains mired in conflict, and recent months have seen a series of bloody attacks. In the south, key towns are now Taliban territory. The BBC's Auliya Atrafi was invited by the militants to spend four days behind the front line in Helmand province witnessing life under their control.

In the town of Sangin, two dozen men sat cross-legged inside a huge mud compound. Under the full moon, their black turbans cast deep shadows over their sunburned features.

These were the Taliban's special forces; the Red Unit. They sat quietly as they listened to their commander Mullah Taqi telling war stories, gently cradling their M4 machine guns. The M4s, with their night-vision scopes, were one of the main reasons they had captured nearly 85% of Helmand province from less-well-armed Afghan forces.

But these victories had presented Taliban leaders with an unexpected challenge.

The people they now ruled had lived with government services for more than a decade. Schools, hospitals, development - residents had become accustomed to them. So how could a group entirely focused on taking territory evolve into one that could attempt to run it?

Image caption Men at the market in Musa Qala - women are rarely seen in public

Who are the Taliban?

The hardline Islamic Taliban movement swept to power in Afghanistan in 1996 after the civil war which followed the Soviet-Afghan war, and were ousted by the US-led invasion five years later

In power, they imposed a brutal version of Sharia law, such as public executions and amputations, and banned women from public life

Men had to grow beards and women to wear the all-covering burka; television, music and cinema were banned

They sheltered al-Qaeda leaders before and after being ousted - since then they have fought a bloody insurgency which continues today

In 2016, Afghan civilian casualties hit a new high - a rise attributed by the UN largely to the Taliban

Setting up our visit to Taliban territory took months. It had been years since a journalist with international media had secured such access. But in mid-May, we crossed the frontline in Gereshk, following a boy on a motorbike. We drove on the main Kabul-Herat highway towards Kandahar.

Just by an Afghan National Army post, the boy suddenly turned left, leaving the highway behind, and rode into scattered settlements. He handed us over to two Taliban guards who were manning a makeshift base. One sat with us in the car, while the other led us on a motorbike towards the Zanbulai area.

There, waiting for us. was Mullah Taqi, the head of Taliban special forces. He stood with a group of his men, all nursing sophisticated weaponry.

Throughout the visit we were accompanied by a Taliban media team who controlled what we saw.

We were not allowed to film anything to do with opium. The opium trade is synonymous with this region - Afghanistan produces about 90% of the world's opium - and helps fund the Taliban.

I tried to explain to their media head, Asad Afghan, the English concept of "an elephant in the room". He put his hand on my shoulder and said: "Opium is our economic necessity, but we hate it as much as you do."

The fact is the Taliban need the money they get from drugs - it buys arms and helps fund their fight.

Image caption Taliban special forces are well armed

Image caption Bomb damage at what's left of the police station in Sangin

Our first encounter with Taliban governance came in the market. Sangin has been fiercely contested for more than a decade - hundreds of UK, US and Afghan troops lost their lives here - and finally fell to the Taliban in March this year.

The old Sangin bazaar had been flattened in the battle for the city. We walked through its makeshift replacement, a sea of tarpaulin and boxes. Two men were arguing by a food stall.

"I can't read!" shouted shopkeeper Haji Saifullah. "How was I supposed to know the biscuits were out of date?" He fidgeted with his turban, pushing it to one side nervously.

Image caption The Taliban mayor of Sangin, clutching petrol jugs

The other man was the Taliban mayor of Sangin, Noor Mohammad. He ordered Haji Saifullah to be imprisoned for three days and to pay a fine.

Next on the mayor's list was inspecting petrol containers to see if they had been altered to pour under the promised gallon. After that came examinations for people who claimed to be doctors, but who he suspected were lying.

Later we drove to Musa Qala, the Taliban's de facto capital. Just short of the town, we stopped at a travelling bazaar set up on a dry riverbed.

Image caption Young traders in Musa Qala

Musa Qala is famous for the opium trade but it is also a commercial lifeline for the district. Traders come here all the way from the Afghan-Pakistan border areas.

At the bazaar you could buy motorbikes, cows, ice-cream - and less conventional commodities such as ammunition.

Bullets for an AK47 were 25 cents (15p) each. Bullets for a Russian machine-gun used to be 40 cents each, but were reduced to 15 cents because - according to the shopkeeper - too many of them had been captured from the Afghan security forces.

Image caption Bullets on sale in the market in Musa Qala

While the Taliban focus on health, safety and trading standards in Sangin was surprising, more discoveries awaited us in Musa Qala. Despite it being the Taliban capital, the school and hospital were still being funded by the government in Kabul.

Read more about the conflict:

"The government recently did their inspections; our schools were officially registered; our salaries that were locked for a year were later released," said Abdul Rahim, the government's head of education for Musa Qala.

He said the Taliban did not have any problem with government inspectors, and that the system was working.

"The government give us stationery and everything else, we implement the government syllabus and the Taliban don't have a problem with it," he said.

Image caption Girls get primary education but after that schools cater for boys under the Taliban

But not everything was running smoothly. Across Afghanistan, about 40% of pupils enrolled in schools are female, according to US Aid. Not in Musa Qala, however. No girls over the age of about 12 were being educated in the Taliban capital. But girls were deprived of education here even before the Taliban took hold, because it is a very conservative area.

For the boys, meanwhile, there were not enough basic supplies.

"The way our school is run is good, as in security, but we have one problem and that's we don't have enough books," said one student, Dadul-Haq. "One student will be missing maths, the other chemistry - not all pupils have the same books."

It struck me that in education, at least, the Taliban are tentatively experimenting by allowing wider access to education - at least for boys - than during their earlier regime. Under them, before 2001, many fewer boys went to school in the countryside. But experiences like Haji Saifullah's - the biscuit seller in Sangin - have made rural Afghans realise that education and literacy are essential. They will not turn you into an infidel, as their forefathers feared.

Now the Taliban appear to have realised that they cannot fight the modern world forever, so some have opted to join it on their own terms.

Image caption Electricity is something people in Helmand are now used to

Asad Afghan, the Taliban's media co-ordinator, used a proverb to make his point. "The fire may have burnt our house, but it made our walls stronger," he said. He meant that the Taliban had learned from the past mistake of isolating themselves from modernisation.

Many say the Taliban have brought some security - albeit with limited freedoms - to the countryside they control. Areas used to years of fighting between troops and militants are now seeing a dramatic rise in trade. Many people say they prefer the Taliban's swift - but flawed - system of justice to the previous administration, which they say was riddled with corruption and patronage.

We visited the district hospital which, like the school, was funded by the government but run by the Taliban. It is meant to serve 120,000 people, but lacked many basic facilities. There was not one female doctor; neither was there a paediatric specialist. It wasn't even possible to get a chest X-ray.

To cater for women the Taliban had built a separate facility next door, run by female staff.

One doctor said the dual system had created a responsibility vacuum and opened the door to corruption. "I haven't been paid in the past six months - not only me but also the entire staff of the hospital," he said.

"[Government] supervisors write things on paper that don't turn into reality. Our medicine for three months doesn't last us more than a month and half… this is because sometimes the Taliban come and want medicine for themselves."

We asked the Taliban's supervisor for health services, Attaullah, if we could interview a female nurse, but he refused.

Her husband told him that he had no problem with the interview, but Attaullah said: "It is your right to allow the interview and my responsibility to stop it.

"What would be the difference between us and the government if we allowed interviews with women?"

During the four days I was in Taliban territory, I only saw women in clinics and being transported around by their male relatives. But men here have always preferred women to stay at home out of sight. Even if the Taliban were not here, it is unlikely things would be very different.

Some activities were limited. In Musa Qala, using mobile phones and the internet was banned for security and religious reasons - our Taliban media handlers communicated via walkie-talkies. Filming and playing musical instruments are also not allowed. One young man told me he was given 40 lashes for watching a Bollywood film.

The Taliban have cracked down on bachabaze - dance parties involving teenage boys that can often end in sexual abuse. They also come down hard on homosexuality, although it appears the Taliban legal process can be influenced with a mixture of pulling strings and bribes.

Image caption You just didn't see posters like this under the Taliban in the past

There are contradictions. We were allowed in to film, for example. And we passed billboards that featured pictures of Western women advertising dental clinics - a far cry from the days when the Taliban banned such images.

Despite the internet ban, there are wi-fi hotspots providing a connection to the outside world. A few dedicated fans of Turkish and Indian soap operas have televisions connected to small satellite dishes.

"Aren't you scared the Taliban will find out?" I asked one teenager. "They know about our TV and the wi-fi," he said. "But I think they are just watching and waiting, to see what happens."

During our visit, we were aware that the Taliban were treating us carefully, mindful of creating a good impression. Equally, Sangin and Musa Qala are important to them, so keeping local people happy matters. We heard reports that Taliban control in other places was more rigid.

For the Taliban, beginning to adapt in the face of modernity seems to be a painful dilemma: embrace it and you lose control and religious legitimacy; reject it and you become an island.

When it comes to governance, the Taliban's Achilles heel is thought to be their political philosophy, or rather the lack of it. From the start, their focus has been on war and there has been little scope for political thinking to evolve. Their success has become their greatest nemesis.

As a white-bearded school headmaster put it: "The Taliban see everything through the prism of war, and they see winning wars as their sole purpose in life."

Image caption What does the future hold for these Musa Qala boys growing up under militant rule?

I reminded him that the Taliban also had a culture of obedience and were disciplined, so didn't he think they would be able to direct their devotion to war into the art of politics? He dropped his head, thought for a moment and shook his head doubtfully. He didn't think so.

At night, we would dine with local Taliban leaders and discuss these themes.

One evening a Taliban leader strove to convince us of the benefits of life under the Taliban by contrasting it with the failings of the Afghan government. But it struck me that the world they wanted to create was too absolute for a human society.

I suggested that society was messy, complicated and always in transition, and wondered how successful any government would be trying put it in a fixed framework.

The leader, Musavir Sahib, was a tiny man, with long beard and blue eyes. He was adamant: "Our governance is based on sacred scripture; it is the best solution for any human society.

"Afghans are adaptable people," he added. "When we took over the country for the first time, very soon people started dressing up like us. And then when the Americans came, they started dressing up like the Americans. So surely they will adopt our governance again."

He could not conceive that people could oppose Taliban rule and were coerced by them into doing what they wanted.

Image caption The police headquarters in Sangin bears the name of the UK regiment that was based in the area

Back inside government-held territory, I realised that describing the insurgent group had become less straightforward and full of contradictions. The Taliban have changed significantly while at the same time they are stuck in their past; they feel they have to adapt to the modern world while thinking theirs is the best way of governance.

In the areas they hold, they seem to be trying to provide a peaceful existence, but elsewhere they continue carrying out their deadly bombings. Their goal to create their specific kind of extreme Islamic state is unchanged and they are still fighting because they see themselves as winning.

But they now face a new challenge. In the areas they control, people now expect life-changing improvements like healthcare and electricity - a lasting legacy of the billions of dollars that poured in with foreign forces to rebuild Afghanistan in the years after 9/11. How will the Taliban cope with that shift?

The BBC team in Helmand province were Auliya Atrafi, Ali Hussaini and Attahullah Safi.