Structural and chemical embrittlement of grain boundaries by impurities: A general theory and first-principles calculations for copper