Bad thoughts come from ego but Allah shows a beautiful way to fight them. A bad thought held in the mind is not a sin. However, when it is spoken about or acted upon it becomes a sin. To fight the ego is difficult but winning against it makes things easy and Allah rewards this.