Magnetism and superconductivity driven by identical 4f states in a heavy-fermion metal