NSE has released a detailed article on margins and how they are calculated. While some elements here are mathematical you can understand how margins work in general.
The one thing you must remember is: Margins are recomputed regularly, a number of times a day. Given that, if you run out of cash to support your margins due to even one bad position, the brokers will square off your positions. You can’t just put on a trade (say an intraday position or an F&O position), check the margin required at that time and then forget about it. Margin recomputations can change a lot of things.