How to connect bluetooth magic mouse and keyboard on thunderbolt display, if they don't show up?
Your iMac is acts as a screen only, not as a bluetooth hub. You need to pair your devices to your main laptop only, and get a cheap keyboard for your iMac who's only purpose will be to kick it in target display mode as you described.

To re-pair Apple input devices: 

Mouse / Magic Trackpad: hold "clicked" while turning on 10- 15 seconds

BT keyboard: hold power for 15 seconds.