Both sentences are grammatically correct, but they have slightly different meanings due to the tense.
* A. "I thought something bad had happened to you" (Past Perfect) indicates that at the time you thought this, the bad thing was already completed in the past. This tense emphasizes that the event (the bad thing) happened before your thinking.
* B. "I thought something bad happened to you" (Past Simple) is more straightforward and can be used when you don't emphasize the timing of the bad event as much. It suggests that the bad thing and your thought about it might have occurred around the same time.
In general, the Past Perfect in Sentence A is more formal and specific when you're clearly talking about something that happened before another past action (your thinking).