TUNIS/ALGIERS The signal to attack came from the mosque, sending dozens of Islamist fighters storming through the Tunisian town of Ben Guerdan to hit army and police posts in street battles that lit the dawn sky with tracer bullets.
Militants used a megaphone to chant "God is Great," and reassure residents they were Islamic State, there to save the town near the Libyan border from the "tyrant" army. Most were Tunisians themselves, with local accents, and even some familiar faces, officials and witnesses to Monday's attack said.
Hours later, 36 militants were dead, along with 12 soldiers and seven civilians, in an assault authorities described as an attempt by Islamic State to carve out terrain in Tunisia.
Whether Islamic State aimed to hold territory as they have in Iraq, Syria and Libya, or intended only to dent Tunisia's already battered security, is unclear and the group has yet to officially claim the attack.
But as fuller details of the Ben Guerdan fighting emerge, the incident highlights the risk Tunisia faces from home-grown jihadists drawn to Iraq, Syria and Libya, and who have threatened to bring their war back home.
Despite Tunisian forces' preparations to confront returning fighters, and their defeat of militants in Ben Guerdan, Monday's assault shows how the country is vulnerable to violence spilling over from Libya as Islamic State expands there.
Authorities are still investigating the Ben Guerdan attack. But most of the militants appear to have been already in the town, with a few brought in from Libya. Arms caches were deposited around the city before the assault.
"Most of them were from Ben Guerdan, we know their faces. They knew where to find the house of the counter-terrorist police chief," one witness, Sabri Ben Saleh, told Reuters. "They were driving round in a car filled with weapons, my neighbors said they knew some of them."
Troops have killed 14 more militants around Ben Guerdan since Monday. Others have been arrested and more weapons seized.
Officials say they are still determining if the militants had been in Libya before or had returned from fighting with Islamic State overseas. But that such a large number of militants and arms were in Tunisia is no surprise.
After its revolt in 2011 to topple Zine El-Abidine Ben Ali, Tunisia has struggled with growing Islamic militancy.
More than 3,000 Tunisians have left to fight with Islamic State in Iraq and Syria, according to government estimates. Tunisian security sources say many are with Islamic State in Libya.
Gunmen trained in Libya were blamed for attacks on tourists at the Bardo Museum in Tunis a year ago and at a beach hotel in Sousse in June.
Tunisians also play a major role in Islamic State in Libya where they run training camps, according to Tunisian security sources.
But the scale of Monday's attack was unprecedented. The militants were well-organized, handing out weapons to their fighters from a vehicle moving through the city, with knowledge of the town and its military barracks.
"We came across a group of terrorists with their Kalashnikovs, and they told us: 'Don't worry we are not here to target you. We are the Islamic State and we are here for the tyrants in the army,'" said Hassein Taba, a local resident.
The attack tests Tunisia at a difficult time. After Islamic State violence last year, the tourism industry that represents 7 percent of the economy is struggling to tempt visitors to return.
With its new constitution, free elections and secular history, Tunisia is a target for jihadists looking to upset a young democracy just five years after the overthrow of dictator Ben Ali.
"The battle of Ben Guerdane in Tunisia, 20 miles from the Libyan border ... is proof enough that the Islamic State has cells far and wide," said Geoff Porter, at North Africa Risk Consulting. "But what these cells can reliably do ... and how they are directed by Islamic State leadership in Sirte, let alone in Iraq and Syria, is not known."
Islamic State has grown in Libya over the past year and half, coopting local fighters, battling with rivals and taking over the town of Sirte, now its main base.
That has worried Tunisian authorities, who have built a border trench and tightened controls along nearly 200-km (125 miles) of the frontier with Libya.
Western military experts are training Tunisians to protect a porous border where smuggling has been a long tradition. Ben Guerdan is well-known as a smuggling town.
"There are still some blind spots in intelligence, but they are advancing with the cooperation of neighboring countries and with the West," said Ali Zarmdini, a Tunisian military analyst.
But Tunisia's North African neighbors worry about the spill over impact of any further Western air strikes and military action against Islamic State in Libya.
After a U.S. air strike killed 40 mostly Tunisian militants in the Libyan town of Sabratha last month, Tunisian forces went on alert for any cross-border incursions.
Just days before the Ben Guerdan attack, Tunisian troops killed five militants who tried to cross from Libya.
But the fact that even after that setback, militants mustered a force of 50 fighters to strike the town shows the group's ability to keep testing the Tunisian military.
(Writing by Patrick Markey; Editing by Giles Elgood)