Women are far more likely than men to get autoimmune diseases, when an out-of-whack immune system attacks their own bodies — and new research may finally explain why.

It’s all about how the body handles females’ extra X chromosome, Stanford University researchers reported Thursday — a finding that could lead to better ways to detect a long list of diseases that are hard to diagnose and treat.

“This transforms the way we think about this whole process of autoimmunity, especially the male-female bias,” said University of Pennsylvania immunologist E. John Wherry, who wasn’t involved in the study.

More than 24 million Americans, by some estimates up to 50 million, have an autoimmune disorder — diseases such as lupus, rheumatoid arthritis, multiple sclerosis and dozens more. About 4 of every 5 patients are women, a mystery that has baffled scientists for decades.

One theory is that the X chromosome might be a culprit. After all, females have two X chromosomes while males have one X and one Y.

The new research, published in the journal Cell, shows that extra X is involved — but in an unexpected way.