The cognitive basis of resilience