Bereishit 4:7 and Yirmiyahu 4:14 both frame sin as something that lurks and must be mastered or washed away even at the level of the heart, with the prophet demanding that Yerushalayim cleanse its heart of wickedness and stop harboring evil designs within itself.
Yoma 29a:1, Mesillat Yesharim 11:51, and Derashot HaRan 6:21 all transmit and build on the teaching that thoughts of transgression are worse than the transgression itself, with the Mesillat Yesharim adding the drashah that one should not bring himself to impure thoughts at all, and the Derashot HaRan explaining that habitual sinners without teshuvah harbor some rotten root of thought sustaining their repeated failures even when the Rambam's distinction between sinning in the intellect versus the body is granted.
The Tanya passages—Tanya, Part I; Likkutei Amarim 12:9-11, Tanya, Part I; Likkutei Amarim 27:1-2, Tanya, Part I; Likkutei Amarim 28:1, and Tanya, Part I; Likkutei Amarim 29:9-10—develop a psychological account for the benoni (intermediate person), teaching that foreign thoughts of sin inevitably arise from the animal soul but that one earns credit precisely by diverting the mind from them rather than entertaining them, while cautioning that the mere capacity to desire the forbidden, even without craving its fulfillment, still distances a person from Hashem to some degree.
Rabbeinu Bahya, Devarim 29:18 confronts directly why the Torah punishes mere hirhur, answering that just as the choice of deeds is placed in a person's hand, so too is the choice of thought after proper preparation, since one is commanded to cultivate good thoughts and guard against evil ones; sudden involuntary stray thoughts that arise before such preparation, however, are treated differently.
Shulchan Arukh, Even HaEzer 23:3-4 codifies concrete halachic restrictions meant to prevent a person from bringing himself to improper thoughts, forbidding deliberately inducing an erection or lustful thinking and prescribing practical safeguards such as sleeping position and where one may place his hands.
Nefesh HaChayim, Gate I 12:6-8 and Tomer Devorah 1:4 describe the mystical consequence of sin as self-generating: every forbidden act or sin creates a destructive spirit or defect recorded above at its root, drawing power from the person's own vitality, which is why sin carries consequence beyond any external punishment.
Mishneh Torah, Human Dispositions 1:4 teaches that a person must constantly monitor and calibrate his dispositions along the balanced middle path, implying an ongoing duty to attend to one's inner character traits and not merely one's deeds.
Likutei Moharan 49:1 teaches that the two inclinations, good and evil, are essentially constituted by a person's thoughts, so that thinking evil thoughts itself clogs and defiles the inner spiritual space, independent of any subsequent action.
Sefat Emet, Shemot, Parashat Parah 2:3 offers a kabbalistic reading in which even a foreign thought that rises in a person is meant to be elevated and clarified through calm reflection, until truth is sorted out within him.
Beit Yaakov, Metzora 23 is a scanned source whose reading we have, and it distinguishes inadvertent wrongdoing from deliberate sin, describing far greater compassion shown toward one who stumbles unintentionally than toward one who sins knowingly out of desire and pleasure.
Chochma uMusar 1 p. 225 is a scanned source whose reading we have, and it holds that a person must actively train his thoughts, since disciplined thinking runs against natural tendency, and neglecting reflection on wisdom and morality can readily cascade into a chain of sins beginning from an initial lapse.