When Turkish military officers set in motion an attempted coup d’état on the night of July 15, lawmakers in the capital, Ankara, were quick to rush to the parliament building. As explosions rocked the capital and President Recep Tayyip Erdogan was nowhere to be seen, it appeared the lawmakers might be making their last stand in defense of Turkey’s democracy. Before heading to government headquarters, opposition lawmaker Mahmut Tanal brought a copy of the Turkish constitution. Another, Orhan Atalay from Erdogan’s ruling Justice and Development Party (AKP), grabbed his gun.

That might have been the better choice. Turkish and U.S. officials now believe the attempted coup was a serious and well-armed bid for power that came within a hairbreadth of succeeding. The coup plotters commanded tanks, helicopters and F-16s. They opened fire on crowds, invaded television studios and shelled the main parliament building. By his own account, Erdogan narrowly avoided capture and possibly execution, fleeing minutes before troops raided his hotel in the seaside town of Marmaris, where he had been on vacation. It wasn’t until the next morning, after Erdogan had rallied his supporters to the streets, that it became clear the attempt had failed. Some 280 people were killed, according to the President, including 100 he called coup participants.

The attempted coup came after a year in which Turkey’s reputation as an oasis of stability in the chaotic Middle East has crumbled. Conflict reignited between the government and Kurdish separatists in July 2015. The country has been rocked by a devastating series of attacks against civilians blamed on both ISIS and Kurdish militants, culminating in the ISIS assault on Istanbul’s main airport in June. The traumas deepened political divisions in Turkey, where rival camps already viewed each other with increasing distrust as Erdogan gathered more and more power. Now, as he clamps down further in an effort to reassert control, the coup attempt has deepened anxieties about the suddenly uncertain future of this NATO member and longtime U.S. ally.

On the night of the coup attempt, Tanal was at a bar-association office in Ankara when he heard fighter jets flying low over the capital. He checked social media and saw that soldiers had blocked bridges in Istanbul. He soon heard of an attack on the intelligence headquarters in Ankara and thought he stood a better chance of avoiding arrest at the fortified parliament complex with other MPs. Still, he brought a bag of items he might need in prison–including that copy of the constitution. “I’m a lawyer and a member of the human-rights committee in parliament,” he says. “I know what’s needed in jail.”

In a different part of the city, Atalay of the ruling AKP was at home when he heard an explosion. After making calls, he learned that a coup attempt was under way. He took his gun and, as he got into his car, heard yet another explosion, this time at a nearby police headquarters. “I saw the flames with my own eyes,” he says.

Lawmakers from three parties soon gathered in the main parliament hall while leaders of the fourth, the pro-Kurdish Peoples’ Democratic Party, sent messages expressing solidarity. Speaker Ismail Kahraman delivered a defiant address, declaring the parliament still in session. But even as he spoke, lawmakers heard an explosion, an apparent bombing nearby. Ten minutes later there was a second blast, this time a direct attack on the parliament building, prompting shouting and chaos inside the chamber.

At about the time the MPs were evacuating the main parliament chamber, Erdogan made his first public statement since the coup attempt had gotten under way. He appeared not in a TV studio or a government office but via video chat on a newscaster’s phone, in a moment that suggested a tenuous grasp on power. He nevertheless struck a defiant tone, urging the Turkish public to take to the streets to oppose the coup. “I am also on my way,” he said.

Crowds of protesters soon responded. In Ankara, they assembled on the street outside the parliament, facing tanks. In Istanbul, Turkey’s largest city, 270 miles northwest of the capital, the coup plotters deployed tanks blocking two key bridges spanning the Bosporus Strait that separates the European and Asian sides of the city. Hundreds of protesters gathered in the vast plaza of Istanbul’s Taksim Square. In the center of the square, soldiers ringed a statue, facing out toward protesters who jeered in their faces. With sporadic gunfire in the background, the crowd chanted a universal Muslim call and response: “Takbir–Allahu akbar!” or “God is great! The greatest!”

After evading the coup plotters, Erdogan flew not to the capital but to Istanbul, where he served as mayor from 1994 to 1998 and where he enjoys a broad base of popular support. Landing at Istanbul’s Ataturk International Airport between 4:00 and 4:30 in the morning, Erdogan delivered another fiery address, confirming that he remained in power and denouncing the coup as an act of treason–one he would not forgive. “They will pay a heavy price for this,” he said. “This uprising is a gift from God to us because this will be a reason to cleanse our army.”

Though U.S. Secretary of state John Kerry initially responded cautiously to the early reports of the coup attempt, President Obama later issued a statement saying Washington “agreed that all parties in Turkey should support the democratically elected government of Turkey, show restraint and avoid any violence or bloodshed.” A senior State Department official said later that the decision on the U.S. response had been made quickly. “It was not a hard call for us to stand up for Turkish democracy and rule of law and against the notion of use of force to take power.”

But the aftermath of the insurrection threatens to worsen long-standing disagreements between Ankara and Washington over democratic reform in Turkey and tactics in the fight against ISIS across the border in Iraq and Syria. Following the coup attempt, Turkish authorities hastily restricted airspace around Incirlik air base, where a tanker took off that refueled planes commandeered by the coup plotters, apparently without the knowledge of the U.S. forces also stationed there. Some pro-government media and officials have advanced conspiracy theories that the U.S. had covertly or overtly backed the coup–charges Washington denies.

The airspace restrictions briefly halted operations in the U.S.-led bombing campaign against ISIS. Though U.S. operations have since resumed, American officials privately worry about future access to the base in southern Turkey, which is also where NATO’s largest nuclear arsenal is stored. And Washington is concerned that Erdogan may grow increasingly illiberal in the aftermath of the coup. “We have an interest in Turkish democracy writ large,” says the State Department official.

Those fears are already coming true. Erdogan has begun purging the state of thousands of soldiers, police, judges and other civil servants accused of links to the coup plot. But he went beyond those involved with the insurrection–the government also sacked some 20,000 employees of the Ministry of Education and demanded the resignation of 1,577 university deans. Academics have been barred from traveling abroad, according to state media. And on July 20, Erdogan declared a three-month state of emergency.

The government blames the coup on Fethullah Gulen, an influential Turkish religious leader who has lived in self-imposed exile in Pennsylvania since 1999 and fell out with Erdogan in 2013. The government says Gulen’s followers established a vast network within state institutions, which it is now trying to eliminate. Erdogan has demanded that the U.S. extradite Gulen, who denies any role in the coup plot. White House officials say they are skeptical of the accusations but are not ruling them out.

Erdogan’s harsh reaction is perhaps to be expected given how close he came to losing his office–and potentially his life. But the latest measures have fueled fears that he is using the coup attempt to suppress critics who had no connection to the insurrection. “I hope and I wish that the party in power learns a lesson,” says Tanal, the opposition lawmaker. “The regime they are running has made a lot of mistakes.”

As the sun rose on July 16, silence fell over Ankara and the sheltering MPs began to peer out of the parliament headquarters. Everywhere in the compound there were security men, including some the lawmakers recognized and others they didn’t. “Everybody was scared of each other,” says Tanal. Some police he had previously known walked him to his car. To his surprise, the vehicle was intact, unscathed by the shelling. He drove home in the morning sun.

The coup attempt has yielded a political paradox. It prompted a moment of unprecedented unity as the vast majority of Turks–including some opposed to the government–came together over the course of a long night to resist an attack on democracy. But unless Erdogan joins them in that unity, Turkey’s divisions will only deepen. “The people did what they needed to. They stood by the government. They showed political maturity,” says Nigar Goksel, a senior analyst on Turkey at the International Crisis Group in Istanbul. “Now the ball is in the court of the government to reciprocate.”

–With reporting by MASSIMO CALABRESI/WASHINGTON

Contact us at editors@time.com.

This appears in the August 01, 2016 issue of TIME.

SPONSORED FINANCIAL CONTENT

Read More From TIME

EDIT POST