import './utils/browserCompat';
import { createRoot } from 'react-dom/client';
import App from './App.tsx';
import './index.css';

const rootElement = document.getElementById('root');

if (rootElement) {
  try {
    createRoot(rootElement).render(<App />);
  } catch (err) {
    console.error('SLPTSU Root Render Error:', err);
    const detail = err instanceof Error ? err.message : String(err);
    const safeDetail = detail.replace(/[<>&"]/g, '');
    rootElement.innerHTML =
      '<div style="font-family: \'Noto Sans Sinhala\', \'Noto Sans Tamil\', Arial, sans-serif; max-width: 720px; margin: 40px auto; padding: 24px; border: 1px solid #d4d4d4; border-top: 4px solid #B51224; background: #ffffff; color: #222222; line-height: 1.7;">' +
      '<h1 style="font-size: 20px; margin: 0 0 12px; color: #800D1B;">SLPTSU — ශ්‍රී ලංකා තැපැල් හා විදුලි සංදේශ සේවා සංගමය</h1>' +
      '<p style="margin: 0 0 10px;">වෙබ් අඩවිය ආරම්භ කිරීමේදී දෝෂයක් ඇතිවිය. කරුණාකර පිටුව නැවත පූරණය කරන්න හෝ යාවත්කාලීන බ්‍රවුසරයක් භාවිතා කරන්න. සහාය සඳහා සංගම් මූලස්ථානය අමතන්න.</p>' +
      '<p style="margin: 0 0 10px;">Unable to initialize the SLPTSU web application in this browser. Please reload the page or try an updated browser.</p>' +
      '<p style="margin: 0 0 14px; font-size: 12px; color: #555555;">Details: ' + safeDetail + '</p>' +
      '<button type="button" onclick="window.location.reload()" style="background: #B51224; color: #ffffff; border: 0; padding: 10px 18px; font-weight: 700; cursor: pointer;">Reload / නැවත පූරණය කරන්න</button>' +
      '</div>';
  }
}
