That is a big question indeed - chord substitution - all about tension (diatonic or out of the key). pulling (resolving) back into the key. Obviously you are correct with the initial answer... study music theory.

But if you want a quick forum answer here is one way I attempt to handle the question that may or may not work for you.

https://www.scalerplugin.com/

Or is you are a reader, this can help you fall asleep at night with some sweet dreams.
https://rickbeato.com/products/the-beato-book-4-0-pdf

Or, if you prefer video, watch/listen to everything this guy talks about...



BIAB – 2025, Reaper (current), i7-12700F Processor, 32GB DDR4-3200MHz RAM, Motu Audio Express 6x6 - My SoundCloud (Tip: No need to create a SC account to hear music - just hit ESC ).