Question about taxation on equal stock profit and loss

Salam all

I have some question about stocks loss and profit. Say I have a company called X stock that I bought at $50 and I sold it at $55 (profit = $5). Also I have a company called Y stock that I bought also at $50 and I sold it at the same time at $45 (loss = $5). Assume also that all the money get reinvested in other stock, say company called Z, at the same time also so the money does not leave the brokerage account to my bank.

Now the question is, am I still have to pay taxes on my $5 profit from X stock sell although I got at the same time an equivalent loss of $5 from the Y stock sell? basically net profit = 0

Please if you think the answer is YES and you have some thoughts/reasons/justifications why IRS is doing that let me know. Personally if the answer is YES, then I see that is a bit unfair because the taxation should account for everything combined, in that case $5 profit - $5 loss = $0 so intuitively there should be no taxes !!!
Thanks in advance for your answers and thoughts