<?php
/**
 * MontaLoja - Dynamic Styles
 */

$primaryColor = $primaryColor ?? '#2563EB';
$bgColor = $bgColor ?? '#FAFBFC';
$cardColor = $cardColor ?? '#FFFFFF';
$textColor = $textColor ?? '#111827';
$accentColor = $accentColor ?? '#10B981';
$textSecondary = $textSecondary ?? '#6B7280';

// Hex to RGB
$pHex = ltrim($primaryColor, '#');
$pR = hexdec(substr($pHex, 0, 2));
$pG = hexdec(substr($pHex, 2, 2));
$pB = hexdec(substr($pHex, 4, 2));
$primaryRgbStr = "$pR, $pG, $pB";

$aHex = ltrim($accentColor, '#');
$aR = hexdec(substr($aHex, 0, 2));
$aG = hexdec(substr($aHex, 2, 2));
$aB = hexdec(substr($aHex, 4, 2));
$accentRgbStr = "$aR, $aG, $aB";

// Primary hover (darker)
$hR = max(0, intval($pR * 0.85));
$hG = max(0, intval($pG * 0.85));
$hB = max(0, intval($pB * 0.85));
$primaryHover = sprintf('#%02X%02X%02X', $hR, $hG, $hB);

// Detect dark theme
$bHex = ltrim($bgColor, '#');
$bR = hexdec(substr($bHex, 0, 2));
$bG = hexdec(substr($bHex, 2, 2));
$bB = hexdec(substr($bHex, 4, 2));
$bgLuminance = ($bR * 0.299 + $bG * 0.587 + $bB * 0.114);
$isDarkTheme = $bgLuminance < 128;

if ($isDarkTheme) {
    $borderColorDyn = 'rgba(255,255,255,0.12)';
    $borderLightDyn = 'rgba(255,255,255,0.08)';
    $surfaceHoverDyn = 'rgba(255,255,255,0.05)';
} else {
    $borderColorDyn = '#E5E7EB';
    $borderLightDyn = '#F3F4F6';
    $surfaceHoverDyn = '#F8F9FA';
}
?>
<style>
:root {
    --primary: <?php echo $primaryColor; ?>;
    --primary-rgb: <?php echo $primaryRgbStr; ?>;
    --primary-soft: rgba(<?php echo $primaryRgbStr; ?>, 0.08);
    --primary-hover: <?php echo $primaryHover; ?>;
    --accent: <?php echo $accentColor; ?>;
    --accent-rgb: <?php echo $accentRgbStr; ?>;
    --accent-soft: rgba(<?php echo $accentRgbStr; ?>, 0.1);
    --bg: <?php echo $bgColor; ?>;
    --bg-white: <?php echo $cardColor; ?>;
    --surface: <?php echo $cardColor; ?>;
    --surface-hover: <?php echo $surfaceHoverDyn; ?>;
    --text: <?php echo $textColor; ?>;
    --text-secondary: <?php echo $textSecondary; ?>;
    --border: <?php echo $borderColorDyn; ?>;
    --border-light: <?php echo $borderLightDyn; ?>;
}
</style>
