Modelling of thermal stratification at the top of a planetary core: Application to the cores of Earth and Mercury and the thermal coupling with their mantles