Improved window resize handler for performance and reliability.