Teenagers chain-smoke in the village square in Ripoll, a tidy Catalan town in the foothills of the Pyrenees in northeast Spain. They're trying to process what happened over summer vacation.

Theirs is one of those towns, population about 10,000, where everyone seems to know everyone. There's a Benedictine monastery, window boxes bursting with geraniums and almost zero crime.

But this was the home of 12 terrorism suspects, including three sets of brothers, who last month attacked Barcelona, 60 miles to the south, and Cambrils, 130 miles to the south. Eight of them were killed. Two are in jail, and two others are under house arrest.

"A week before the attacks, we were hanging out with them on this same square," says Paula Lopez, 17. She pulls out her cellphone to reveal a photo of herself with one of the terrorists at a school dance last year. They're beaming, their cheeks pressed together.

As Spanish police investigate last month's terror attacks, for which ISIS claimed responsibility, they have realized the suspects don't fit the usual profile. They were secular, beer-drinking youths — all in their teens and early 20s, from Moroccan immigrant families who seemed to thrive in small-town Spain.

Almost all of them had jobs or were still in school. They didn't grow up in an impoverished ghetto and there were very few — in some cases no — signs of radicalization. Ripoll is about 9 percent immigrant, the majority of them — some 600 to 700 people — are Muslims from Morocco, who are well-integrated into the local Catalan community.

All of which sends chills down the spines of authorities working to prevent these kinds of attacks.

Friends of the suspects are scouring their memories for possible signs of radicalization they might have missed. In recent months, two of the suspects — brothers — had started wearing flashy clothes and bought a new car, says Maria Garcia, another childhood friend. She thought maybe the money came from dealing drugs.

"But smoking pot isn't a sign of terrorism," Garcia says. "In fact, it's the opposite! People who are radicalized don't do drugs. They're religious. Our friends never went to the mosque. So we didn't think anything of it."

Investigators are looking into whether a radical imam who moved to Ripoll two years ago plied the young men with gifts in secret. Either they led double lives or they were radicalized so recently that no one noticed.

"I think ISIS chose them because they were so integrated and smart — and not despite the fact that they lived in a small town, but because of it. They knew their plans would go unnoticed," says Nuria Perpinya, a psychologist and community liaison who works for the municipality. "Only in retrospect can we see some red flags. Maybe they withdrew slightly in the past month. But it was summer vacation. When you know people for life, you give them the benefit of the doubt."

Perpinya knew all the young suspects. Since the attacks, she has been counseling their families. They are ashamed and grieving, she says.

"We need an expert in cults to come explain to us how young brains can change so quickly," Perpinya says.

One such expert is Lorenzo Vidino, director of the Program on Extremism at George Washington University, who has studied jihadism in Spain's northeast region of Catalonia, where Ripoll is located. Vidino says the Ripoll suspects were likely radicalized in a group. Since they grew up together, he says, they had pre-existing social ties and embraced a radical ideology together. Since the terror cell included three sets of brothers, family bonds helped.

Their method of avoiding detection may have originally come from an extremist group from 1970s Egypt called Takfir wal-Hijra,Vidino says.

"Takfir wal-Hijra was the extreme fringe of jihadism. They basically advocated that it's legitimate to act like non-pious Muslims — which includes drinking, or smoking or dating — to hide their true intentions," he says. "The group was wiped out by Egyptians a long time ago, but the mindset has remained, and a lot of extremists use those tactics still today."

Spanish police are looking into that theory. They're also investigating whether Takfir wal-Hijra's philosophy may have been disseminated in Spain by a handful of ultraconservative mosques on the northeast coast. Mohamed Atta — who went on to become one of the hijackers in the Sept. 11, 2001, attacks in the U.S. — visited one of those mosques in early 2001, according to Spanish officials.

The northeast Catalonia region accounts for the highest number of jihadist arrests — nearly 200, over the past two years — in all of Spain, says Albert Oliva, a regional police inspector.

"We've dismantled many terror cells already, always by acting on tips: Someone's behavior changes drastically or they start posting extremist things online," Oliva says. "But none of those things happened here. This was not a textbook case. So now we have to go deeper, with the community's help. Police cannot be inside every living room, every mosque and every friendship."

Copyright 2017 NPR. To see more, visit http://www.npr.org/.