Chemical Equilibrium and Stable Stratification of a Multicomponent Fluid: Thermodynamics and Application to Neutron Stars