Borrowed from French, the expression esprit de l’escalier, or esprit d’escalier, literally wit of (the) staircase, denotes a retort or remark that occurs to a person after the opportunity to make it has passed.
It originally referred to a witty remark coming to mind on the stairs leading away from a social gathering. The image seems to have originated in Paradoxe sur le Comédien (Paradox of the Actor), an essay on theatre by the French philosopher, writer and critic Denis Diderot.
https://wordhistories.net/2017/05/27/esprit-descalier/