BARCELONA • For more than a decade, Spain seemed immune to the steady eruptions of violence by Islamic militants that rocked other European countries where radicalised young men, often from poor immigrant backgrounds, carried out devastating attacks.
Spain's security forces carved out a reputation as some of Europe's most dogged counter-terrorism operators, fighting an intense battle beneath a deceptively placid surface in a country whose long coastline, just across from Morocco, makes it an inviting entry point into Europe for extremists.
But the illusion of a Spanish haven was shattered last Thursday when a van swerved down Las Ramblas, Barcelona's famous pedestrian boulevard, leaving behind a trail of bodies including 13 dead and more than 100 injured. Another person was killed in a second attack at a seaside resort that ended when the police shot dead five suspects.
Now, the authorities are rushing to answer how a group that included teenagers was able to carry out a well-planned and coordinated attack in a country that has parried danger for so long, despite being such a tempting and symbolic target for Islamic militants.
"For the jihadis, Spain is still very much a front-line country," said Georgetown University's Professor Bruce Hoffman, who has advised the US government on terrorism.
Since 2004, when bombs ripped through trains in Madrid, killing 192 people, Spain has foiled a long list of plots by Islamic militants.
More than 700 suspected extremists have been arrested since 2004, according to the Interior Ministry. Last year, a possible plot to drive a truck through crowds was quashed.
"The Spanish authorities are at the top of the game in Europe," said Mr Matthew Olsen, a former director of the National Counter-terrorism Centre in the United States.
The Spaniards built on their decades of experience in fighting ETA, the Basque separatist movement, and introduced a raft of laws that helped to disrupt radical cells.
But counter-terrorism is partly a matter of luck, and last Thursday, Spain's ran out. Some of the young men involved were teenagers, and investigators believe they had lived double lives as they planned the attacks for a year at a house in Alcanar, a town about 190km from Barcelona.
It was a stark reminder of the challenge in Catalonia, which has a reputation as a burgeoning hub of Islamic extremism. The economic boom in Catalonia, home to just over a quarter of Spain's almost two million Muslim residents, drew many Muslim migrants from Morocco, Algeria and South Asia. But many struggled to integrate and were drawn to clusters of radical Salafi mosques that sprang up near some smaller towns.
Spain has not seen the emergence of hardline, anti-Muslim political movements as elsewhere in Europe, despite high unemployment since the 2008 financial crisis. But Spain has struggled with extremist currents in its Moroccan communities. This is linked to its close, if sometimes testy, relationship with Morocco, which accounts for much of its Muslim migration. Nearly all the dozen suspects in the latest attacks have been identified as Moroccans or of Moroccan origin.