This is a tricky one. If you are hidden in a dark location and fire a bow beyond a creatures normal vision (18m or 60ft) then you wouldn't be seen or leave stealth BUT the creature if not killed would investigate in the direction he/it was hit following a perception check. Casting a loud arsed spell going "Wisawizawizawooo" at the top of your lungs while creating a visually obvious bolt spell wouldn't require the creature to make a perception ck he/it would just run over and bite you.

This is a problem in turn based combat, the stealth system in BG3 is pretty close to D&D, probably the only mechanic that is. Currently a wizard can move after casting a spell into an out of sight area and sneak. I would say remove the ability to sneak after casting a spell completely that turn. Unless and this is a biggy, the arcane caster uses silent spell which if I remember correctly requires a resourse from a Sorcerer, not sure if in 5e wizards can cast silent spell anymore?

Whatever, my point being weapon attacks should be treated differently than spells in stealth. You cannot cast stealth spells with few exceptions like greater invis and silent spell. Classes like thief and assassin need these stealth mechanics to function as a class. Stealth checks on the rogue (for ex) only happen in visual range (18m or 60ft) otherwise they remain hidden and they can go back into stealth without check outside visual range with a bonus action.

Sneaking in visual range depends on many factors, light level, creatures vison and skills like "hide in plain sight". You cannot hide in visual range if relative to the creature you are seen in full daylight, so in a half lit room vs a goblin with darkvision. If you are not seen in relative daylight and in visual range you would make a stealth ck based on dex/skill + light level before hiding/not.