First of all if you are just fantasizing and not using porn in my humble opinion you are doing two different things. I don't know how G-d views them, but it seems to me that they are quite different.
One simple reason to focus on is that there is a rabbinic decree about touching yourself (a Talmud chatham please jump in here, machloches Abaye and Rava in Niddah about whether you are allowed to touch it with a cloth or a rigid object?)
But I would focus on a practical reason. The practical reason is that you have an automatic thought, a belief, that you are powerful over lust, and as long as you keep masturbating you are not doing anything to disprove this belief. The belief is bad because you also have a belief that being a kosher yid is a Good Thing (tm) and these two beliefs together make you depressed, because you don't feel you are a kosher yid right now (hence your post.)
You can fix it. You can develop the belief that you are not powerless.
Thinking clearly at bedtime is a battle best avoided, because you are tired (in aa they have the halt acronym, don't get too hungry, angry, lonely, or tired, people like that don't think clearly.)
If you do not have a belief that you are powerless over alcohol, how about a drink before bed?
Or it might be enough to plan to masturbate in the morning after your coffee. Then you might think more clearly.
You might want to read Feeling Good by David Burns.