Thoughts in and of themselves are not sins. But they however can easily lead to sin. The idea of thought control is very important in Islam and it begins with being attuned to our ideas and the images , sounds, and casual mixed gender interactions that effect our heart-whether it be in real life or online/social media.
For example if someone was to see an image that was sexually exciting that can lead him or her to have certain thoughts or imaginations of for example committing adultery or obsessing over another man or woman which in could effect harmony of the marriage. We should pay close attention to our daily salaat, give in charity, read Qur'an/Du'a, and try our level best to control our glances. Also and perhaps most importantly, beseech Allah in du'a swt for his divine assistance ins seeking refuge from Shaytan and Satanic whispers while attempting to root out the causes of impure thoughts as mentioned in the question. Thus it requires a deeper investigation into the self to understand what triggers such ideas.
wa Allahu al-'Alim