Click HERE if you’re having trouble viewing the gallery on your mobile device.

OAKLAND — An Alameda County judge has ruled Oakland officials had a “mandatory duty” to ensure safety at the doomed Ghost Ship warehouse, leaving the city potentially liable for a fire where 36 people died.

Attorneys for the dozens of victims and their families, as well as reports by this news agency, have detailed numerous visits by police, firefighters and building officials in the years before the deadly Dec. 2, 2016, fire. Yet the maze-like Fruitvale district warehouse with a shoddy electrical system continued to house more than 20 people and host concerts.

Alameda County Judge Brad Seligman’s decision Wednesday pierces broad immunity afforded to cities to protect workers for failing to inspect buildings.

“I think the court was correct,” said attorney Tom Brandi, one of several lawyers representing victims and victims’ families. “The real issue is what did they know, when did they know it, and why didn’t they do the right thing. I’m confident we are going to be able to establish the facts we need to establish.”

The judge’s ruling comes as attorneys representing the plaintiffs and defendants enter into the evidence gathering phase of the massive lawsuit. While Oakland still has the ability to make a summary judgment motion — a request for the court to drop the case — the finding of “mandatory duty” for now leaves the city potentially liable for the death and displacement of partygoers and Ghost Ship tenants.

Oakland also is facing a suit in Seligman’s courtroom over a deadly March 2017 San Pablo Avenue fire. Public records show firefighters had sent warnings to fire administrators about dangerous conditions at the halfway house in the months before the blaze.

Ray Marshall, an attorney representing the city, argued that “mandatory duty” can only be applied if a qualified city official makes a determination that a building is in violation of codes and if there is sufficient supporting facts.

“It doesn’t happen by observation … not by the police but someone with proper expertise,” Marshall told Seligman. “Not every city official is capable of making that determination, because they’re complex.”

Seligman was unmoved, reaffirming his November ruling on the same issue. Until all the evidence arrives and arguments are made, the judge said he has to take the allegations in the lawsuit as true.

“The complaint says there were very serious conditions and the city knew about it and did nothing,” Seligman said.

On Sunday, the Bay Area News Group reported officers had been inside the building on at least seven occasions, and one officer called it “a huge fireplace,” according to obtained police body camera footage.

Oakland firefighters had also visited Ghost Ship and, according to multiple witnesses, went inside during a party in September 2014. Those visits were not inspections, which are largely protected in civil suits, and show officials had knowledge of the potential for disaster, said Bobby Thompson, another attorney for the plaintiffs.

The immunity provided to government agencies, Brandi added, “sadly is designed to conceal their conduct and exclude responsibility.”

Besides Oakland, PG&E, landlord Chor Ng, master tenant Derick Almena, party promoters and others are listed as defendants in the case. Almena and his second-in-command, Max Harris, each have been charged with 36 counts of involuntary manslaughter and are expected to go to trial in July.