Congenital myotonia is primarily caused by mutations in genes encoding chloride channels, such as the CLCN1 gene. These channels are crucial for maintaining the electrical stability of muscle cells. A defect in the CLCN1 gene disrupts the normal chloride ion flow, leading to prolonged muscle contraction.