She was ill-tempered, moody, vain and insecure.

She berated her famous husband in public, went on shopping binges and made enemies. She was called “the hell cat” by some of the staff at the White House.

First lady Mary Todd Lincoln, who also experienced the deaths of three of her children and the assassination of her husband, was a troubled figure in 19th-century American history. Later in life, she was temporarily institutionalized.

On Tuesday, a California cardiologist advanced what he said was a fresh theory to explain some of her behavior: She suffered from an ailment called “pernicious anemia,” which is related to a vitamin B-12 deficiency.

John G. Sotos, who has also studied the medical history of Abraham Lincoln, detailed his theory in the journal Perspectives in Biology and Medicine, published by the Johns Hopkins University Press.

Mary Todd Lincoln, wife of President Abraham Lincoln, wore this dress for his inauguration in 1861. (Library of Congress/Getty Images)

Mary Lincoln’s litany of ailments and complaints was often thought to be, in part, those of a hypochondriac, Sotos wrote.

“A diagnosis of chronic multisystem pernicious anemia would clarify [her conduct] as First Lady and widow, and illuminate challenges faced by her husband,” he wrote.

The ailment begins with an autoimmune attack on the stomach, which then stops making the chemical that helps absorb ­dietary B-12, Sotos explained in an email.

“The symptoms of deficiency appear months to years later, after the body’s storehouse of B12 is depleted, and over time, symptoms arise from more and more organs,” he said.

It’s not clear how Mary Lincoln got it.

“It is likely she was predisposed to it genetically,” Sotos said. “Some people have suspected that the same bacterium that causes ulcers . . . also triggers the autoimmune attack on the stomach, but the question is far from settled.”

Mary’s physical afflictions included fevers, headaches, fatigue, a rapid heart rate, progressive weakness, a sallow complexion and tingling of the skin, he wrote. All are consistent with vitamin B-12 deficiency.

And her irritability, delusions and hallucinations also are in line with such a deficiency, he argued.

“Mary Todd Lincoln ranks among the most detested public women in American history,” ­biographer Jean H. Baker wrote in 1987. “And Americans . . . have unshakable opinions about Mary Lincoln’s failings.”

But Sotos contended that “she was simply a woman with a biochemically injured mind struggling in a complicated, ­relentlessly demanding environment.”

A native of Lexington, Ky., Mary Todd and the budding politician and lawyer Abraham Lincoln were married in 1842 in Springfield, Ill., according to Baker’s biography.

She and her family lived briefly in Washington after her husband was elected to his single term in Congress in 1846 and returned after he was elected president in 1860.

By then, one of her children had died in Springfield, and another would die in the White House in 1862.

It was while she was first lady that her questionable conduct emerged publicly.

She spent lavishly on White House furnishings, shelling out huge sums for china, wallpaper, carpets and furniture. She spent the government’s $20,000 four-year allowance for White House refurbishing in one year, Baker wrote.

She also spent heavily on clothing and jewelry, seeking to emulate European royalty, even as the United States was in the grip of the Civil War.

After her 11-year-old son, ­Willie, died on Feb. 20, 1862, Mary took to her bed for three weeks, overcome with grief, ­Baker wrote.

Later, she dabbled in spiritualism and attended seances in the White House to try to contact her departed son. She believed that he and his deceased brother, ­Eddie, appeared to her in her room at night.

Meanwhile, her jealousies tormented her.

Three weeks before her husband was assassinated, she attended an Army review with him in Virginia. Arriving late in a wagon, she became enraged that the wife of a prominent general was riding on horseback beside the president.

Mary confronted her husband and began to rail at him in front of Army officers, according to Baker.

“He bore it as Christ might have done, with an expression of pain and sadness that cut one to the heart,” an officer wrote later. “He pleaded with eyes and tones, till she turned on him like a tigress, and then he walked away.”

She demanded that the general be fired for his wife’s offense. He wasn’t.

Calamity struck again when Lincoln was assassinated while sitting beside his wife in Ford’s Theatre on April 14, 1865, and she was unhinged anew.

Sotos wrote that although Mary Lincoln left few surviving medical records, he amassed a compendium of hundreds of letters and historical sources related to her health.

After the assassination, she moved to Chicago with her surviving sons, Robert and Tad, ­Sotos wrote. But in 1871, Tad Lincoln died there at the age of 18, further afflicting his mother.

In the years after that, Mary suffered from auditory hallucinations, paranoia and delusions.

In 1875, Robert, worried about her well-being, sought a warrant for her arrest on the grounds of insanity. After a court hearing, she was ruled insane and committed to an asylum outside Chicago, where she was confined for three months.

After her release, she went to France for four years, then moved in with her sister in Springfield. She died there on July 16, 1882, at the age of 63.

Doctors in those times knew almost nothing about pernicious anemia, and until the early 20th century it was frequently fatal, according to Sotos.

Nowadays it can be spotted early through routine blood tests, he said. It is treated with B-12 pills or shots.

Mary Lincoln’s misfortune “was living before treatment was available,” Sotos wrote, “and before physicians could make a diagnosis that would have prevented 150 years of misunderstanding about her.”