I studied logic and philosophy at University, but I don't recommend any texts in that area because empirical studies don't. Studies have shown that taking logic or philosophy courses didn't help students think through situations nearly as well as taking, say, medicine. It turned out that encountering real world examples of good vs bad thinking helped more than being able to name or categorize particular kinds of fallacies after the fact.

I will say, that for my part Earle Stanley Gardner taught me best; to use what I'd term my "systematic imagination" and leave no possibility unconsidered. Useful for invention, but also for coding and algorithm design, of course. He did a pretty good job of cataloging the limits of most human (associative) thinking.

And it's not just me! Crime fiction had a significant influence on academic philosophy last century through such as Wittgenstein; but many others as well. There's a good article detailing that history but I can't find a URL for it just now.

If you aren't sure what "first principles" thinking is, Gardner offers a post-graduate course with interesting examples. I prefer the term "systematic imagination" to "first principles thinking" because some people tend to flip the meaning of the latter phrase on it's head and narrow their thinking to straightforward derivations from a few axioms; as if they were medieval theologians. That's not at all what Elon Musk has in mind when he talks about "first principles thinking." He means, discarding everything except the most basic principles of physics, and not neglecting any possibility that those laws allow. In other words "systematic (exhaustive) imagination."

In the same way, reading Elon's precepts (or mine) doesn't really reform your brain; but going through a lot of examples of how 'twas done right, and done wrong is really helpful. The scientific history of medicine offers plenty of (mostly horrifying) examples.

Histories of technology and technological and engineering blunders can perform the same function nicely, too. Watt saw the blunder that Newcomen had made by repeatedly heating and cooling the same chamber. Crazy inefficient. However Watt later assumed that his small-scale tests of high-pressure steam engines showed that the concept couldn't work. Unconsciously, he seems to have assumed that physics scaled (linearly) even though Galileo had shown it doesn't. Blunder. Since he held the key patent, Watt tragically blocked all development of high-pressure steam engines until his patent expired.

My own thinking has also been much improved through reading many excellent military histories that focused on decisions: because you know (nearly) everyone is really trying to do their best thinking in a war, they aren't usually being merely slovenly; yet amazing blunders happen. Such as the Germans putting their bridging equipment at the back of their columns during the Bastogne offensive in WWII. That works in a desert, but it's the wrong way to get through a forest (Ardennes) with rivers. Once trapped at the back of a narrow forest road, the German portable bridges were useless. They didn't think that one through (didn't deploy their imagination, sufficiently.)