Judge Stephen Reinhardt, a liberal stalwart on the Ninth U.S. Circuit Court of Appeals in San Francisco who wrote the ruling that ultimately legalized same-sex marriage in California, died Thursday. He was 87.

Court spokesman David Madden said Reinhardt died suddenly during a medical appointment in his hometown f Los Angeles.

“We have lost a wonderful colleague and friend,” said Sidney Thomas, chief judge of the Ninth Circuit, which oversees federal courts in California and eight other Western states. “As a judge, he was deeply principled, fiercely passionate about the law and fearless in his decisions. He will be remembered as one of the giants of the federal bench.”

He may also have been the nation’s most-reversed judge, writing most of his important decisions while the Supreme Court has had a conservative majority. Among his rulings that the high court overturned were decisions that would have struck down Washington state’s ban on doctors providing aid in dying and a federal law prohibiting a type of midterm abortion that opponents labeled partial-birth abortion.

“If you are really faithful to the law, you’re likely to get reversed because (the Supreme Court) has cut back on rights,” Reinhardt said in an interview published in 2011.

But one of his most important rulings withstood appeals: a decision in February 2012 that struck down Proposition 8, the ban on same-sex marriage that California voters had approved in November 2008. Rather than declare a constitutional right for gays and lesbians to wed their chosen partners, Reinhardt crafted a narrower decision, based on past Supreme Court rulings that said a state could not withdraw rights in order to harm a politically unpopular group. Same-sex couples had briefly been allowed to marry in California, based on a state Supreme Court ruling that was repealed by Prop. 8.

“The people may not employ the initiative power to single out a disfavored group for unequal treatment and strip them, without a legitimate justification, of a right as important as the right to marry,” Reinhardt wrote for a 2-1 majority.

The high court reached the same conclusion in 2013 on even narrower grounds, declaring that after Gov. Jerry Brown and the state attorney general’s office refused to defend Prop. 8, the measure’s private sponsors had no right to represent the public and appeal a federal judge’s ruling that declared the measure unconstitutional. Two years later, the court issued a broad ruling legalizing same-sex marriage nationwide.

Reinhardt also wrote a ruling that barred lawyers from removing prospective jurors because of their sexual orientation, a ban that already had applied to categories such as race and religion.

Hector Villagra, executive director of the ACLU Foundation of Southern California and a former clerk of Reinhardt’s, called the judge “a great legal mind and writer.”

“He worked seven days a week, completely committed to doing justice,” Villagra said.

Reinhardt grew up in Los Angeles and attended Pomona College and Yale Law School. After graduation, he served in the Air Force as an attorney, then practiced law privately, mostly at a firm that specialized in labor law. He also served on the U.S. Civil Rights Commission and was a member of the Democratic National Committee before President Jimmy Carter appointed him to the appeals court in 1980.

It was one of a series of Carter appointments that transformed the Ninth Circuit into perhaps the nation’s most liberal federal appeals court, with Reinhardt one of its leaders.

A prototype Reinhardt opinion was the March 2016 ruling that briefly struck down a federal law that barred unauthorized immigrants from challenging their deportation if they were “habitual drunkards.” Alcoholism, Reinhardt said, is an illness, not a moral defect.

“Like any other medical condition, alcoholism is undeserving of punishment,” he wrote for a 2-1 majority. “We are well past the point where it is rational to link a person’s medical disability with his character.”

But the full appeals court granted a rehearing to the government, and a larger panel of the court upheld the law, which the Supreme Court recently declined to review.

Reinhardt is survived by his wife, Ramona Ripston, former executive director of the American Civil Liberties Union of Southern California. Backers of Prop. 8 tried to remove him from their case because of the ACLU’s support of same-sex marriage, but Reinhardt refused, noting that Ripston’s organization was not a party to the case and that spouses are individuals with their own independent views.

He is also survived by his adult children: Mark, a professor of political science at Williams College, Justin, a professional musician and Dana, a novelist.

The family suggests that donations in Reinhardt's honor may be made to the ACLU.