First minister loses seat and resigns as Welsh Labour leader