HONG KONG — After four days of hearings, dozens of weary pro-democracy activists charged with subversion under the national security law found themselves back where they started: in detention.

In a case that has emerged as the clearest illustration yet of the pressures on Hong Kong’s courts as Beijing cracks down on the territory, lawyers were unable to secure even temporary freedom for the 47 pro-democracy activists charged Sunday with conspiracy to commit subversion, which is punishable by up to life in prison.

Although a Hong Kong judge granted bail to 15 defendants, the government’s justice department immediately appealed the decision, sending them back into detention immediately. More than 30 others must remain in jail, the judge said, a departure from the right to bail under Hong Kong’s common law courts. This group includes a 23-year-old democracy activist, a 64-year old former elected lawmaker with heart disease, a medical worker and a lawyer.

Their crime, prosecutors said, is organizing or participating in a primary vote in July. The defendants, they said, plotted to disrupt the functioning of Hong Kong’s government and would abuse their powers if elected. The primary was held to select pro-democracy candidates for elections that were ultimately postponed because of the coronavirus pandemic, and many of the primary’s winners were barred from running anyway.

The defendants span the gamut of Hong Kong’s civil society, including activists who have fought not only for democracy but for LGBT rights, environmental protections and workers’ rights. The bail hearings were so grueling that at least eight defendants were hospitalized, and their trials could drag on for years.

Human rights groups and lawyers immediately condemned the detention decision, which they say represents an unprecedented attack on free expression in the city and severely undermines the independent judiciary.

“None of them have committed a recognized crime, but they have fallen victim to a national security law that deems people a ‘threat’ simply for the peaceful expression of political views,” Amnesty International Hong Kong said in a statement. The denial of bail, Amnesty added, was “essentially a foregone conclusion.”

Jerome Cohen, a leading expert on Chinese law at New York University School of Law wrote that the process has made Hong Kong’s “formerly revered judicial system look like the willing instrument of the police and prosecution.”

Since anti-government protests erupted n 2019, Beijing has moved swiftly to curtail Hong Kong’s freedoms and re-engineer its core institutions, hoping to decisively end opposition sentiment and unrest. After reining in schools, the legislature and the media, Chinese officials say those serving in all of Hong Kong’s institutions must meet the Communist Party’s definition of a “patriot,” including the courts.

China will begin the annual meeting of the National People’s Congress this week, which is the country’s most important political event. Less than an hour after the bail decision, Chinese state media said it would put forward a resolution to “improve” Hong Kong’s electoral system.

Defendants, who were first arrested in January and released shortly after, were asked to report to the police on Sunday, weeks earlier than scheduled. Prosecutors, meanwhile, say they need three more months to prepare their case for trial.

Since the start of the bail hearings on Monday, hundreds of supporters have turned up to the West Kowloon court in solidarity. People handed out free drinks outside the courts, held banners that read, “Free all political prisoners” and broke into protest chants. Family members watched the proceedings from a separate courtroom, unable to see their loved ones.

“It’s bizarre that I can only see if my child is doing well or not from the screen,” said the mother of a defendant, who spoke on the condition of anonymity, citing security concerns. She has not slept for days, she said, and did not have any appetite for dinner before the bail decision was announced.

Upon hearing that they would be sent back to detention centers, most defendants were stoic. Some waved, while others thanked lawyers. And before the microphones were turned off, one shouted: “Hong Kongers, never die.”