ON MARCH 18th, as part of an agreement to stem the influx of refugees from the Middle East, the European Union declared Turkey to be a “safe” country for asylum seekers—a necessary precondition for sending migrants back there under international law. The following day, a suicide bomber killed at least four people, all of them foreigners, on Istanbul’s most popular pedestrian shopping street (pictured). Turkey has now endured five big terror attacks since October, at a cost of nearly 200 lives. Residents of Turkey would therefore disagree with the EU’s assessment: the country no longer feels safe for anyone.

No group has stepped forward to claim responsibility for the latest bombing. The day after the attack, officials identified a Turkish member of Islamic State (IS) as the perpetrator. IS has been quick to take credit for atrocities elsewhere, from Jakarta to San Bernardino. But it has yet to do so for any attack by its sympathisers in Turkey, including a double bombing that killed 102 people in October.

Distressingly, the list of other possible suspects has grown longer over the past year. In neighbouring Syria, Turkish-backed proxies are fighting the regime of President Bashar al-Assad, his Russian allies, and the People’s Protection Units (YPG), a local affiliate of the Kurdistan Workers’ Party (PKK). Turkish forces have shelled the YPG directly. As the terror attacks continue, officials in Ankara have pointed to all of the above as possible culprits. A Turkey that once tried to play firefighter in Syria is slowly being consumed by its flames.

Yet the biggest threat to Turkey’s security is homegrown. An urban insurgency waged by the PKK across the country’s southeast, accompanied by a ruthless crackdown by Turkish security forces, has left more than a thousand people dead, including some 300 civilians. With the army deploying tanks and artillery fire against rebels armed with RPGs and machine guns, entire neighbourhoods have been shelled out of existence.

Losing ground in the southeast, the PKK appears to be turning its firepower on soft targets, including civilians, in the country’s west. In the past two months, terrorist attacks claimed by one of the group’s offshoots have killed 67 people in Ankara, the Turkish capital. A senior PKK commander has warned that “at this point in the struggle” his militants are poised to fight by any means necessary. Amid the blowback from Syria, the increasingly radicalised Kurdish insurgency and a mounting crackdown against academics, journalists and politicians suspected of PKK sympathies, Turkey risks sinking further into a cycle of repression.

The best (if still fragile) hope of escaping chaos may lie in the new deal with Europe. In exchange for a commitment to accept migrants sent back from Greece, Turkey has wrested a number of big concessions from the EU. These include visa-free travel, a substantial political prize, as well as €6 billion ($6.8 billion) in aid for the refugees Turkey harbours. They also include opening a new chapter of Turkey’s long-stalled talks on joining the union. Optimists argue that the strong conditions attached to the promise of visa liberalisation will bring Turkey back into the EU fold and help stem its descent into authoritarianism. There is at least a chance that rebuilding bridges with Europe may rein in the authoritarian instincts of Recep Tayyip Erdogan, Turkey’s president, and embolden the few reformists left in his government.

Yet the EU’s new openness cannot undo the damage done by the recent violence, which may strike a crippling blow to Turkey’s tourism industry by scaring Europeans away. The deal itself still faces legal, political, and logistical challenges that could cause it to unravel. Turkey will find it difficult to meet all of the exacting conditions required for visa liberalisation by this summer, and Greece may find it hard to set up the infrastructure needed to ensure orderly mass returns.

Pessimists fear that Mr Erdogan, who once boasted of being able to flood Greece and Bulgaria with refugees, may use his leverage over the EU to press for still more concessions. The president does not seem terribly malleable; he will heed the advice of international bodies only “as long as it is fair,” he said recently. “If it is not, sorry.” Rather than change course, he could ratchet up military action in the southeast and put the squeeze on any domestic opponents who get in the way. Nevertheless, the renewal of a European connection may be the best chance of keeping the country from going off the rails. “Even some limited engagement has an upside,” says Atilla Yesilada, a political analyst. “Leaving Turkey out in the cold has no benefits."