<?php
defined('BASEPATH') or exit('No direct script access allowed');
$brandName = config_item('company_name') ?: (config_item('website_name') ?: (config_item('copyright_name') ?: 'ERP'));
$assetUrl = static function ($path) {
	$path = ltrim((string) $path, '/');
	$file = FCPATH . $path;
	$version = is_file($file) ? filemtime($file) : time();
	return base_url($path) . '?v=' . $version;
};
?>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><?php echo html_escape(isset($title) ? $title : $brandName . ' | Comprehensive Business Suite'); ?></title>
<meta name="color-scheme" content="light dark" />
<style>
	:root {
		--background: 220 14% 96%;
		--foreground: 224 71.4% 4.1%;
		--card: 0 0% 100%;
		--card-foreground: 224 71.4% 4.1%;
		--popover: 0 0% 100%;
		--popover-foreground: 224 71.4% 4.1%;
		--primary: 248 100% 72%;
		--primary-foreground: 210 40% 98%;
		--secondary: 220 14.3% 95.9%;
		--secondary-foreground: 220.9 39.3% 11%;
		--muted: 220 13% 91.1%;
		--muted-foreground: 220 8.9% 46.1%;
		--success: 142 56.1% 52.1%;
		--success-foreground: 110 40% 98%;
		--danger: 0 100% 67.1%;
		--danger-foreground: 210 40% 98%;
		--info: 205 100% 70%;
		--info-foreground: 0 0 100%;
		--accent: 220 14.3% 95.9%;
		--accent-foreground: 220.9 39.3% 11%;
		--destructive: 0 84.2% 60.2%;
		--destructive-foreground: 210 40% 98%;
		--border: 220 13% 91%;
		--border-foreground: 220 8.9% 46.1%;
		--input: 0 0 100%;
		--input-foreground: 0 0 100%;
		--ring: 248 100% 77%;
		--ring-foreground: 210 40% 98%;
		--sidebar-background: 0 0% 100%;
		--sidebar-foreground: 240 5.3% 26.1%;
		--sidebar-active: 248 100% 77%;
		--sidebar-active-foreground: 210 40% 98%;
		--sidebar-border: 220 13% 91%;
		--sidebar-border-foreground: 220 8.9% 46.1%;
	}

	.dark {
		--background: 222 47% 11%;
		--foreground: 210 40% 98%;
		--card: 215 28% 17%;
		--card-foreground: 210 40% 98%;
		--popover: 222 47% 11%;
		--popover-foreground: 210 40% 98%;
		--primary: 248 100% 72%;
		--primary-foreground: 210 40% 98%;
		--secondary: 217 22% 29.1%;
		--secondary-foreground: 210 40% 98%;
		--muted: 222 19% 27%;
		--muted-foreground: 215 20.2% 65.1%;
		--success: 142 56.1% 52.1%;
		--success-foreground: 110 40% 98%;
		--danger: 0 100% 67.1%;
		--danger-foreground: 210 40% 98%;
		--info: 205 100% 70%;
		--info-foreground: 0 0 100%;
		--accent: 217.2 32.6% 17.5%;
		--accent-foreground: 210 40% 98%;
		--destructive: 0 84.2% 60.2%;
		--destructive-foreground: 210 40% 98%;
		--border: 216 18% 28.1%;
		--border-foreground: 216 18% 28.1%;
		--input: 221 39% 12.3%;
		--input-foreground: 210 40% 98%;
		--ring: 263.4 70% 50.4%;
		--ring-foreground: 210 40% 98%;
		--sidebar-background: 215 28% 17%;
		--sidebar-foreground: 220 14.29% 95.88%;
		--sidebar-active: 263.4 70% 50.4%;
		--sidebar-active-foreground: 210 40% 98%;
		--sidebar-border: 217.2 32.6% 17.5%;
		--sidebar-border-foreground: 215 20.2% 65.1%;
	}
</style>

<!-- Set theme ASAP to avoid flash -->
<script>
	(function () {
		try {
			var stored = localStorage.getItem('theme');
			var preferred = stored || (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
			document.documentElement.classList.toggle('dark', preferred === 'dark');
			document.documentElement.setAttribute('data-theme', preferred);
			document.documentElement.style.colorScheme = preferred;
		} catch (e) {
			document.documentElement.classList.remove('dark');
			document.documentElement.setAttribute('data-theme', 'light');
			document.documentElement.style.colorScheme = 'light';
		}
	})();
</script>

<?php $useDataTables = !empty($dataTables) || !empty($datatable); ?>
<!-- Styles -->
<link rel="stylesheet" href="<?php echo $assetUrl('assets/css/tailwind.generated.css'); ?>" />
<?php
$styles = isset($styles) ? $styles : array('assets/css/app.css');
foreach ($styles as $cssPath): ?>
	<link rel="stylesheet" href="<?php echo $assetUrl($cssPath); ?>" />
<?php endforeach; ?>
<script src="<?php echo base_url('assets/vendor/jquery/jquery-3.7.1.min.js'); ?>"></script>
<?php if ($useDataTables): ?>
	<link rel="stylesheet" href="<?php echo base_url('assets/plugins/dataTables/css/dataTables.tailwindcss.min.css'); ?>" />
	<link rel="stylesheet" href="<?php echo base_url('assets/plugins/dataTables/css/responsive.dataTables.min.css'); ?>" />
	<link rel="stylesheet" href="<?php echo base_url('assets/plugins/dataTables/css/buttons.dataTables.min.css'); ?>" />
<?php endif; ?>
<script>
	let base_url = "<?php echo base_url(); ?>";
	let search = "<?php echo lang('search'); ?>...";
	window.appConfig = {
		base_url: "<?php echo base_url(); ?>",
		baseUrl: "<?php echo base_url(); ?>",
		theme: "<?php echo isset($theme_class) ? $theme_class : ''; ?>",
	};
</script>
