Detailed chemical composition of classical Cepheids in the LMC cluster NGC 1866 and in the field of the SMC