CHOPIN played in the background and, as night fell, the crowd on the square in front of the Supreme Court in Warsaw sang the Polish national anthem. Someone projected “This is our court” onto the building’s wall. Two weeks earlier, in the same square, Donald Trump had hailed Poland’s role in the defence of Western values. But for the demonstrators who turned out on July 16th to protest against changes to the judicial system by the governing Law and Justice (PiS) party, it was those very values that were under threat.

Since taking power in 2015, PiS has set about dismantling the country’s checks and balances. It has reduced the public broadcaster to a propaganda organ, packed the civil service with loyalists and purged much of the army’s leadership. It has undermined the independence of the judiciary by stacking the Constitutional Tribunal with its cronies. In response, the European Commission warned Poland’s government last year that such changes pose “a systemic risk to the rule of law”.

On July 12th PiS stepped up its effort to subjugate the legal system to politicians’ control with two new laws. Members of the National Judicial Council, the body that chooses judges, will henceforth be selected by parliament instead of by other judges. The minister of justice can now appoint and dismiss the heads of lower courts. A third bill, if signed into law, would allow the minister to sack every member of the Supreme Court. Among other responsibilities, that court rules on the validity of elections. Unexpectedly, Andrzej Duda, Poland’s president, threatened to veto the bill, but with a few amendments it is now likely to pass.

Jaroslaw Kaczynski, PiS’s boss and the country’s de facto leader, accuses Poland’s courts of being “subordinated to foreign forces” and beset by a “collapse of moral principles”. He also calls them a “stronghold of post-communists”, referring to PiS’s claim that Polish liberals have secret ties to the former communist regime. The reforms, he said, were needed to speed up proceedings and restore public confidence.

In fact, Polish courts are not especially slow. Critics believe PiS simply wants to stuff them with judges who will rubber-stamp its policies. From now on, judges will owe their careers to the governing party. “It’s shockingly brazen,” says Kim Lane Scheppele, a sociologist at Princeton University who has analysed similar changes in Hungary.

Polls show that 76% of Poles oppose a politicised judiciary, as the protests in Warsaw and other cities attested. The front page of Dziennik Gazeta Prawna, a daily, pictured Mr Kaczynski and two shadows; the title read: “The three branches of government”. Outside parliament last weekend, one man brandished a handmade placard quoting Montesquieu. Another carried a copy of the constitution. “It has no meaning now,” said his wife. State television, meanwhile, described the protests as a “coup”. In a rant in parliament on July 18th, Mr Kaczynski even accused the opposition of murdering his brother, who died in a plane crash in 2010.

The European Commission dutifully expressed concern over the new laws. There is some talk of imposing sanctions on Poland. But Mr Kaczynski has drawn lessons from Hungary, where Viktor Orban, the autocratic prime minister, has rewritten the constitution and tightened the screws on civil society with little trouble from the European Union. “Kaczynski has learned from Orban that if you change facts on the ground, the commission can’t get its head around it in time,” says Ms Scheppele. The EU has launched infringement procedures against Hungary, but the most serious sanctions, contained in Article 7 of the EU treaty, require a unanimous vote in the European Council. Poland would probably veto any effort to invoke them against Hungary, and vice versa.

Yet unlike Hungary, where Mr Orban’s party enjoys a crushing majority, Poland is politically divided. PiS won just 37.5% of the vote in 2015. Civil society remains strong, and the government responds to public pressure: last year it backed down from a strict abortion law when faced with massive protests. The independence of Poland’s judiciary may depend on how strongly Poles want to keep it.