Two-phase magnetohydrodynamics: Theory and applications to planetesimal cores