Media playback is unsupported on your device Media caption "I was with the Taliban for one year"

In a dusty courtyard, behind a tall mesh fence, a group of teenagers are playing a frenetic game of football, while others stand around watching from the sidelines.

These are some of Afghanistan's most vulnerable and most troubled children. Inmates of Kabul's Juvenile Rehabilitation Centre.

A small, excited crowd of boys press their faces up to the fence to talk to us.

"Some of us killed people… some of us are thieves," one lanky teenager tells me when I ask what they are accused of.

They're smiling and laughing but they aren't the most serious cases here. The centre generally holds approximately 300 inmates, aged between 12 and 18. On average, around half are accused of being in militant groups: the Islamic State (IS) or the Taliban.

The Taliban control or contest around half of Afghanistan, they've been waging a deadly war against the Afghan government and US-led international forces since being overthrown in 2001.

The staff try and keep the categories of detainees separate, to avoid fights between those linked to the Taliban and to the Islamic State group - which have clashed with each other in the outside world - and to prevent the boys accused of "general crimes" from becoming radicalised.

The boys talking to me in the courtyard all live on the ground floor of the bright green accommodation block. The Taliban and IS members are held in the two floors above them.

"They're terrorists, we are not," says one dismissively.

'Kill to survive'

The BBC was given rare access to the centre, surrounded by high concrete walls, with watchtowers and armed guards dotted around the periphery. In a quiet corner, a social worker introduces us to three alleged IS members. The group is newer, and far less powerful in Afghanistan than the Taliban, but has been responsible for dozens of deadly suicide bombings.

One, wearing an electric blue salwar kameez, and with a glare in his eyes, claims he was wrongly arrested. Many of the boys in the centre are wary about revealing their past activities, even though we are keeping their identities anonymous. Nevertheless, he gradually opens up about his extreme views.

"The foreigners haven't done anything wrong to me, but the Prophet has said jihad against them must continue until the Day of Judgement."

The extent of his radicalisation becomes even clearer as he explains why he hates the Afghan government - allied to US-led international forces.

"This is an Islamic country, but look at Kabul, it's full of brothels. There is so much adultery and homosexuality. There are musicians and, god forbid, there are even people having sex with their own mothers and sisters."

He's from a province in the east of the country, and his description of the Afghan capital Kabul as a pit of debauchery bears no resemblance to reality. Yet it's clear it's something he deeply believes.

At the same time, there's a softer side to him too. Despite only having the beginnings of a beard growing on his face, he's a father to a one-year-old girl.

"I don't want her to see me here, she will cry," he says.

As the interview ends and we walk off, we pass the cells of the accommodation block. In one, a group of boys are dangling their legs out between the blue metal bars of the window, singing a song and hammering out a beat on a small plastic jug.

They giggle as they tell me the song is about "love", but I notice many of them have tattoos scratched onto their wrists, arms or ankles.

Some are crude images of guns or daggers, one - in Persian - reads "have a bad life, and an even worse future"; another, "kill to survive".

Kabul suffers from high levels of crime, with kidnappings and violent robberies common. Unemployment amongst young people is at more than 30% according to the World Bank.

"Kill to survive?" I ask. "Is that what you feel life is like?"

The mood has momentarily grown more serious.

"If you work as a labourer you only earn 200 Afghani (£2) a day, how can you feed your family?" one asks. "You have to steal and rob."

Then, another strikes up his song again, and we leave them giggling in their cell.

'I will join the Taliban again'

The following day, we meet a dizzying stream of boys, an IS recruit who joined up after seeing their propaganda on social media, a 17-year-old Taliban fighter who smilingly asserts that he has killed "lots" of American soldiers - a claim that's almost definitely not true.

One of the boys that stands out is Zakir, a tall, gangly 14-year-old. He looks like a rather dopey teenager, whose deep voice seems too old for his youthful face. But he's proud of his links to the Taliban.

"My job was to plant explosives," he tells me. "The Taliban used to come to the village at night time and I would give them reports about what was going on."

Zakir says he joined the group after seeing "how they prayed and looked after people" when visiting relatives in areas under their control, and watching Taliban propaganda videos.

He tells me he joined "secretly" and that his family never knew. In fact, his father and elder brother were fighting on the other side of the conflict, as members of the Afghan intelligence services.

Was he never worried the explosives he planted could end up killing his own father? He insisted he operated in areas his father never visited, targeting instead local pro government militias who he accused of "raping and stealing".

Zakir, like many of the boys, seemed to have a good relationship with the staff at the centre. But he was clear that he wanted to join the Taliban again.

"Here, we pray five times a day, and have lessons. But when I'm free I will look after my parents, and then I will join the Taliban again. They are fighting for the truth."

Some of his words sounded as if they were copied from propaganda videos. But there were also glimpses of how child-like he was.

What would he do if his parents forbade him from rejoining the Taliban?

"Then I won't… but when they die, I will."

Fighting back

The staff try to deradicalise the boys. There's a school in the centre, with lessons taking place behind thick metal doors, in what used to be cells.

One of the most popular subjects is Islamic studies.

Mubashir Nazari is a heavy set man, with a cropped beard. He's leading a class of around 25 boys - many accused of being in the Taliban or IS - who are reciting Quranic verses.

He admits it's not easy to change their minds, but says the lessons do make a difference.

"When they first come to the centre, and they hear that there's been a suicide attack in Kabul, they are happy and say, 'do you see how many people we have killed? This is jihad'.

"Then we ask them, 'what is the sin of those children killed in the blast? Or the people just going to work?'. And we say, 'where in Islam does it permit you to kill innocent people?'. By the end, when they hear of a suicide attack they get upset."

There are also lessons in academic subjects, with basic literacy taught for those who have never been to school before.

Abdullah, a slim 16-year-old, with bright eyes, and a small traditional cap perched atop his head, tells me he spent two months with the Taliban, and was arrested on his way to a shootout between the group and security forces in western Afghanistan.

He was inspired to sign up when he and his friends began attending a madrassa, or religious school, in his area, which was under the control of the Taliban.

"The preacher said it's your duty to fight and die in the name of Allah," he says.

But now, Abdullah is one of the centre's best students, despite having little to no formal education in the past.

"I'm in Class 10, and waiting for my results so I can move into Class 11," he says with pride. "The Taliban just put ammunition in your hand and tell you to fight, but when I am released from here I want to keep studying."

Traumatised

For staff trying to form relationships with boys like Abdullah, one of the biggest problems is the high turn-over in inmates.

Detainees are regularly released or transferred, whilst there's a constant stream of fresh arrivals.

One morning, we watch as the intelligence services drop off a small boy, his head shaved, wrists still in handcuffs.

He looks extremely nervous as he's taken to the medical centre to be registered. In an almost inaudible voice, he tells them he's suffering from psychological problems.

The doctor taking down his details says mental health issues are common.

He tells me he often sees boys who have self harmed, cutting their bodies or faces.

"Every week there's two or three cases…. They do it with whatever they can find - pieces of glass or metal."

"I think almost all of them have gone through something traumatic and haven't had the ability to process it, so their response to it is fighting," Lyla Schwartz, a foreign psychologist with the Etidal Foundation, explains. "Finding something to be a part of in order to get their anger and frustration out, instead of actually acknowledge how much loss they've experienced."

Media playback is unsupported on your device Media caption Secunder Kermani reports from one of the country's busiest hospitals in the southern city of Kandahar

Ms Schwartz tries to get the boys to address their underlying trauma, but she's stretched for time.

"We always address the crises first - suicide attempts, fighting, riots - but there are some kids that we really have built relationships with. If we had the support and resources, we could go more in depth."

'Killing children'

Fourteen-year-old Mohammad looks particularly nervous

He led a small group of friends running away from an IS camp they were forced into, and is worried about other boys - with extremist views - overhearing us.

His parents both died, and he ended up in the care of his sister and his brother-in-law. The brother-in-law joined IS, and took Mohammad with him to the group's stronghold in the east of Afghanistan.

There, he enrolled him in a madrassa.

"They pretend to be good Muslims," he says, "but they are not doing good things."

"Like what?" I ask.

"Killing children," he replies. It's not clear what he witnessed himself, and what he's simply heard about - he doesn't want to go into details - but later in the conversation, he again refers to IS "killing kids".

Mohammed says he tried to run away with a handful of others. "But we were arrested by IS and put in jail… They beat us and said, 'you want to join the infidels'."

When they could, they tried to escape again.

"We knew if we were discovered we would be killed… We walked all night through the hills and the next day we handed ourselves in."

But while Mohammad is relieved he's no longer in the madrassa, he's frustrated that he's being kept in the detention centre. "We thought we would go home, but instead they put us here."

'My father was wrong'

Aside from lessons in the morning, and a short period in the courtyard, the boys spend their days in their rooms.

Zakir, the 14-year-old Taliban member, shares his cell with nine others, all accused of being in the militant group.

Their favourite pastime? Beadwork.

Zakir shows me a tiny pair of sandals he's made for his younger sister, using red and white beads that are stuck together, others have decorated pens or made prayer beads for their parents.

I use the opportunity to ask Zakir more about life growing up in a warzone.

"I've been seeing dead bodies ever since I can remember," he tells me in a flat tone.

One episode stands out for him. When he was eight, one of his friends was killed by his own parents, for getting involved with local criminals. It's a reminder that the violence in Afghanistan isn't just restricted to the war.

How and why Zakir ended up joining the Taliban is confusing.

His father, whilst working for the intelligence services, was injured in an attack by the group when Zakir was 13. He remembers his parents trying to hide the fact from him. "My father said he was bitten by a dog, but later on I found out that wasn't true."

But despite what happened to his father, Zakir says he began to support the Taliban. "I knew my father was in the wrong, for supporting the foreigners," he tells me.

One of the main reasons he joined the group, he says, was because he was being harassed by local pro-government militia forces.

"If you are dressed like a religious Muslim with a skullcap, they arrest you, beat you and humiliate you."

Once with the Taliban, he felt "safer".

"Before joining the Taliban, I would get upset when I was beaten and ask why is this happening to me? But when I was with the Taliban, it was better, because at least then there was reason - we were enemies."

It's hard to know if all that is true, but Zakir seems genuine. He's conflicted about what he wants to do with his future. He's "proud" of his time with the Taliban, but adds: "Perhaps I won't do it again, as there are other fighters… I want to continue my jihad, but not if my parents are unhappy."

The centre does try and prepare the boys for life on the outside - there's a tailoring workshop for example.

But once they've been released there's no system for checking up on them.

It's a source of frustration for Ms Schwartz.

"There are supposed to be social workers who follow up with them," she says, but security concerns make it difficult for that to happen. "Support, school, jobs, alternatives to what they've come from, is extremely lacking."

Justice Minister Abdul Baseer Anwar - who ultimately has control of the centre - recognises the need to rehabilitate the boys. "The psychologists," he says, "are even more important than the clerics" in fighting against the "brainwashing" done by militants.

Mr Anwar adds that he's looking at introducing new regulations that would make it easier to help find the detainees stable jobs after release. He hopes the international community can help both with more funding and expertise.

'I'll slit your throat'

Abdullah, the former Taliban member who has excelled in his studies, is due for release and worries about what will happen next.

"I won't go back to my village," he tells me. "The Taliban would kill me."

But others in the centre show little sign of changing their views. On our final day, one peering out of the window of his cell on the third floor, points to me and runs his fingers across his neck.

"I'll slit your throat," he shouts down.

It's a reminder of the challenges faced by the Afghan authorities in reintegrating those with extremist views into the rest of society.

The Taliban and US signed an agreement in February aimed at beginning to bring an end to the conflict. In the next stage of the peace process, talks between the insurgents and the government about the future of the country are due to begin, though fighting between them continues.

The boys in the centre linked to the Taliban celebrated the deal as a victory, staying awake late at night offering special prayers. Many hope now to eventually be released as part of a prisoner exchange that belatedly began earlier this month, although both sides have accused each other of not fully abiding by its terms.

Zakir, with a mixture of stubbornness and innocence, insists that the war in Afghanistan will end when American troops leave as Afghans are all "brothers". But he acknowledges he wants the Taliban's interpretation of an Islamic state established in the country, despite the fact many other Afghans clearly don't. Other boys I speak to make clear they believe their "jihad" will continue until sharia law is established.

When we ask Zakir what he thinks about democracy, he's instantly dismissive.

"Nonsense," he replies.

All the teenagers' names have been changed