It isn’t every day that a federal court cites Ex parte Endo, the 1944 US supreme court decision which invalidated the detention of loyal, law-abiding Japanese-Americans during the second world war. But these aren’t ordinary times.

Shortly after taking office, President Donald J Trump unleashed pandemonium by suddenly announcing a temporary ban on travel into the United States from seven Muslim-majority nations, in addition to a temporary ban on all refugees. Experts cried foul, warning that Trump’s order violated the constitution and made America less safe.

Amid vigils and protests, federal courts issued a flurry of rulings against Trump’s order. The broadest ruling was issued by Judge James Robart, who Trump promptly denounced on Twitter.

On Tuesday, the US court of appeals for the ninth circuit heard oral argument on an emergency motion to overturn Judge Robart. More than 130,000 people live-streamed the hearing.

The Department of Justice represented Trump in the court of appeals and took several astonishing positions. Most remarkably, it warned that “judicial second-guessing of the President’s national security determination in itself imposes substantial harm on the federal government and the nation at large.”

Trump (through his tweets) and his lawyers (in their briefs) thus argued not only that Trump should win on appeal, but that judges would cause grave harm merely by questioning his order.

On Thursday night, a three-judge panel of the ninth circuit unanimously ruled against Trump in a powerful, well-reasoned opinion.

The panel held that Trump’s demand for judicial abdication “runs contrary to the fundamental structure of our constitutional democracy”. As the court explained, “it is the role of the judiciary to interpret the law”, even in cases involving national security.

Indeed, that is a settled, fundamental principle of our separation of powers: “Courts routinely review the constitutionality of – and even invalidate – actions taken by the executive to promote national security, and have done so even in times of conflict.”

It was here that the panel cited Endo, a case imbued with tragic, enduring significance. To cite Endo is to invoke the memory of Japanese-American internment – a dark chapter from our past, in which the political branches cloaked bigotry in baseless claims of lurking peril, and in which the judiciary abandoned for too long its obligation to stand sentinel against tyranny.

The ninth circuit amplified Endo’s message by linking it to cases from the civil war, cold war and Guantánamo Bay, where judges kept faith with the constitution against claims of exigency.

All of these citations led the ninth circuit to Justice Sandra Day O’Connor’s ringing declaration in Hamdi v Rumsfeld (2004) that the constitution “most assuredly envisions a role for all three branches when individual liberties are at stake”.

The ninth circuit’s reasoning is forceful, elegant and necessary. It provides a respectful answer to Trump’s call for judicial silence, and teaches that checks and balances are essential to freedom. This is a lesson that Trump, a former business tycoon, must come to appreciate as he wields the awesome power of the presidency.

Turning to the underlying question, the panel first addressed whether Trump is likely to prove that his order is constitutional. It concluded that he is not likely to do so, at this preliminary stage.

Much of the court’s reasoning focused on whether the executive order complies with the fifth amendment, which requires due process of law (such as fair notice and a hearing before suffering travel restrictions). The Department of Justice largely based its defense on the ground that persons covered by the order – foreign nationals located abroad – lack constitutional rights.

But as the panel observed, there are gaping holes in this defense. Most notably, the order applies to lawful permanent residents, or green card holders, who are protected by the constitution. While the White House counsel has purported to exempt green card holders, the court noted that his interpretation is neither authoritative nor binding.

In addition, even if lawful permanent residents were not covered, Trump’s order might well violate the due process rights of other groups, including unlawful migrants present in the United States, non-immigrant visa holders who have been in the United States but temporarily departed or wish to temporarily depart, and visa applicants who have a protected relationship with a US resident.

Turning to the claim that Trump’s order discriminates on the basis of religion – thereby violating the Establishment Clause and Equal Protection Clause – the panel noted only that there are “serious allegations” and “significant constitutional questions”. Based on the oral argument, this non-answer probably reflects a compromise among the judges, who may have different views.

But still, the panel made clear that claims of religious discrimination can rest upon evidence of improper purpose. This ruling will come back to haunt the Trump administration, which has made anti-Muslim and pro-Christian comments in spades.

The panel then considered whether blocking Trump’s order would cause irreparable injury. Here it tackled Trump’s most alarmist statements, emphasizing that “the Government has pointed to no evidence that any alien from any of the countries named in the Order has perpetrated a terrorist attack in the United States.”

In fact, the government had presented no evidence at all, relying instead on broad and non-specific assertions of national security. The only relevant evidence was an affidavit by a bipartisan group of former officials, warning that the order made us less safe. This was a striking departure from the executive’s standard practice of offering factual support for its security claims.

Finally, the panel saw public interests pointing both ways: “On the one hand, the public has a powerful interest in national security and in the ability of an elected president to enact policies. And on the other, the public also has an interest in free flow of travel, in avoiding separation of families, and in freedom from discrimination.”

Taking all of these considerations into account, the panel refused to reverse Judge Robart’s decision blocking Trump’s order.

Trump has spent the past few days railing against, bullying and seeking to delegitimize the federal courts. True to form, within minutes of the ninth circuit’s opinion, he tweeted, “SEE YOU IN COURT, THE SECURITY OF OUR NATION IS AT STAKE!”

The contrast between Trump’s tweets and the ninth circuit’s 30-page reasoned opinion speaks for itself.

Trump now has three options. He can allow his executive order to remain stayed while proceedings continue in the trial court. He can seek immediate rehearing before the full ninth circuit. Or he can file an emergency application for relief at the supreme court. If Trump pursues this last route, he must convince five justices in order to prevail. That may prove challenging.

In all events, while Trump’s order will probably remain blocked for the time being, the path ahead is long and steep. The ninth circuit’s order is admirable, but preliminary. Legal battles will rage in courts across the land. Further fact-finding and real-world developments may change hearts and minds. And Trump could decide to revoke, revise or replace the executive order.

Still, the ninth circuit has rendered the nation a great service. Its hearing exemplified the rule of law in a democratic society. And its fine opinion displays the judicial courage our era requires.