<?php
/**
 * Carrega configurações do sistema e aplica CSS dinâmico
 * Este arquivo deve ser incluído no início do <head> do dashboard
 */

if (!isset($pdo)) {
    require __DIR__ . '/../../config/config_db.php';
}

require_once __DIR__ . '/../../backend/settings_helper.php';

// Gerar e exibir CSS dinâmico
echo getDynamicCSS();

// Carregar logos para uso no dashboard (variáveis globais)
$logo_sidebar_admin = getSystemSetting('logo_sidebar_admin', '');
$logo_sidebar_user = getSystemSetting('logo_sidebar_user', '');
$logo_header = getSystemSetting('logo_header', '');
$logo_favicon = getSystemSetting('logo_favicon', '');

// Aplicar favicon se configurado
if (!empty($logo_favicon)) {
    echo '<link rel="icon" type="image/x-icon" href="' . htmlspecialchars($logo_favicon) . '">';
}

// Carregar cores (modo claro)
$sidebar_bg = getSystemSetting('color_sidebar_bg', '#1e3a8a');
$sidebar_text = getSystemSetting('color_sidebar_text', '#e0e7ff');
$header_bg = getSystemSetting('color_header_bg', '#ffffff');
$header_text = getSystemSetting('color_header_text', '#1f2937');
$body_bg = getSystemSetting('color_body_bg', '#f3f4f6');
$card_bg = getSystemSetting('color_card_bg', '#ffffff');
$text_main = getSystemSetting('color_text_main', '#1f2937');
$text_muted = getSystemSetting('color_text_muted', '#6b7280');
$border = getSystemSetting('color_border', '#e5e7eb');

// Carregar configurações de gradiente (modo claro)
$bg_body_type = getSystemSetting('bg_body_type', 'solid');
$bg_sidebar_type = getSystemSetting('bg_sidebar_type', 'solid');
$bg_header_type = getSystemSetting('bg_header_type', 'solid');

// Gradientes do Body
$bg_body_gradient_start = getSystemSetting('bg_body_gradient_start', '#f3f4f6');
$bg_body_gradient_end = getSystemSetting('bg_body_gradient_end', '#e5e7eb');
$bg_body_gradient_direction = getSystemSetting('bg_body_gradient_direction', '135deg');

// Gradientes da Sidebar
$bg_sidebar_gradient_start = getSystemSetting('bg_sidebar_gradient_start', '#1e3a8a');
$bg_sidebar_gradient_end = getSystemSetting('bg_sidebar_gradient_end', '#4c1d95');
$bg_sidebar_gradient_direction = getSystemSetting('bg_sidebar_gradient_direction', '180deg');

// Gradientes do Header
$bg_header_gradient_start = getSystemSetting('bg_header_gradient_start', '#ffffff');
$bg_header_gradient_end = getSystemSetting('bg_header_gradient_end', '#f9fafb');
$bg_header_gradient_direction = getSystemSetting('bg_header_gradient_direction', 'to right');

// Função auxiliar para gerar valor de fundo (sólido ou gradiente)
function getBackgroundValue($type, $solid_color, $gradient_start, $gradient_end, $gradient_direction) {
    if ($type === 'gradient') {
        return "linear-gradient({$gradient_direction}, {$gradient_start}, {$gradient_end})";
    }
    return $solid_color;
}

// Carregar cores dark mode (valores padrão otimizados)
$header_bg_dark = getSystemSetting('color_header_bg_dark', '#0f172a');
$header_text_dark = getSystemSetting('color_header_text_dark', '#ffffff');
$body_bg_dark = getSystemSetting('color_body_bg_dark', '#020617');
$card_bg_dark = getSystemSetting('color_card_bg_dark', '#0f172a');
$text_main_dark = getSystemSetting('color_text_main_dark', '#ffffff');
$text_muted_dark = getSystemSetting('color_text_muted_dark', '#94a3b8');
$border_dark = getSystemSetting('color_border_dark', 'rgba(255,255,255,0.08)');
$table_header_bg_dark = getSystemSetting('color_table_header_bg_dark', '#1e293b');
$table_header_text_dark = getSystemSetting('color_table_header_text_dark', '#e2e8f0');
$table_row_hover_dark = getSystemSetting('color_table_row_hover_dark', '#334155');
$message_success_bg_dark = getSystemSetting('color_message_success_bg_dark', 'rgba(34, 197, 94, 0.2)');
$message_success_text_dark = getSystemSetting('color_message_success_text_dark', '#86efac');
$message_error_bg_dark = getSystemSetting('color_message_error_bg_dark', 'rgba(239, 68, 68, 0.2)');
$message_error_text_dark = getSystemSetting('color_message_error_text_dark', '#fca5a5');
$modal_bg_dark = getSystemSetting('color_modal_bg_dark', '#0f172a');
$modal_header_bg_dark = getSystemSetting('color_modal_header_bg_dark', '#1e293b');
$modal_header_text_dark = getSystemSetting('color_modal_header_text_dark', '#ffffff');

// Gerar valores de fundo (sólido ou gradiente) usando a função auxiliar
$sidebar_bg_value = getBackgroundValue($bg_sidebar_type, $sidebar_bg, $bg_sidebar_gradient_start, $bg_sidebar_gradient_end, $bg_sidebar_gradient_direction);
$header_bg_value = getBackgroundValue($bg_header_type, $header_bg, $bg_header_gradient_start, $bg_header_gradient_end, $bg_header_gradient_direction);
$body_bg_value = getBackgroundValue($bg_body_type, $body_bg, $bg_body_gradient_start, $bg_body_gradient_end, $bg_body_gradient_direction);

// Aplicar CSS customizado para cores do sistema
echo '<style>
    /* Aplicar cores customizadas do White Label - MODO CLARO */
    :root {
        /* Cores da Sidebar (suporta gradiente) */
        --bg-sidebar: ' . htmlspecialchars($sidebar_bg_value) . ' !important;
        --text-sidebar: ' . htmlspecialchars($sidebar_text) . ' !important;
        
        /* Cores do Header (suporta gradiente) */
        --bg-header: ' . htmlspecialchars($header_bg_value) . ' !important;
        
        /* Cores do Body (suporta gradiente) */
        --bg-body: ' . htmlspecialchars($body_bg_value) . ' !important;
        --bg-dashboard: ' . htmlspecialchars($body_bg_value) . ' !important; /* Variável específica para dashboard-container */
        --bg-card: ' . htmlspecialchars($card_bg) . ' !important;
        
        /* Cores de Texto */
        --text-main: ' . htmlspecialchars($text_main) . ' !important;
        --text-muted: ' . htmlspecialchars($text_muted) . ' !important;
        
        /* Cor das Bordas */
        --border-color: ' . htmlspecialchars($border) . ' !important;
        
        /* Cor Primária (para abas, botões principais, etc.) */
        --primary-color: ' . htmlspecialchars(getSystemSetting('color_button_primary', '#009846')) . ' !important;
        --primary-color-hover: ' . htmlspecialchars(getSystemSetting('color_button_primary_hover', '#007635')) . ' !important;
    }
    
    /* Aplicar cores customizadas do White Label - MODO ESCURO */
    [data-theme="dark"] {
        /* Cores da Sidebar - manter cor customizada ou usar padrão dark (gradiente também funciona no dark) */
        --bg-sidebar: ' . htmlspecialchars($sidebar_bg_value) . ' !important;
        --text-sidebar: ' . htmlspecialchars($sidebar_text) . ' !important;
        
        /* Cores do Header - CORREÇÃO: usar valor calculado que respeita gradiente se configurado */
        --bg-header: ' . htmlspecialchars($header_bg_value) . ' !important;
        
        /* Cores do Body - CORREÇÃO: usar valor calculado que respeita gradiente se configurado */
        --bg-body: ' . htmlspecialchars($body_bg_value) . ' !important;
        --bg-dashboard: ' . htmlspecialchars($body_bg_value) . ' !important; /* Variável específica para dashboard-container - CORREÇÃO: usa gradiente se configurado */
        --bg-card: ' . htmlspecialchars($card_bg_dark) . ' !important;
        
        /* Cores de Texto - dark mode usa texto claro */
        --text-main: ' . htmlspecialchars($text_main_dark) . ' !important;
        --text-muted: ' . htmlspecialchars($text_muted_dark) . ' !important;
        
        /* Cor das Bordas - dark mode */
        --border-color: ' . htmlspecialchars($border_dark) . ' !important;
        
        /* Cor Primária - dark mode (usa versão clara para melhor contraste) */
        --primary-color: ' . htmlspecialchars(getSystemSetting('color_button_primary_dark', getSystemSetting('color_button_primary', '#22c55e'))) . ' !important;
        --primary-color-hover: ' . htmlspecialchars(getSystemSetting('color_button_primary_hover_dark', getSystemSetting('color_button_primary_hover', '#16a34a'))) . ' !important;
    }
    
    /* Aplicar cores diretamente nos elementos */
    #sidebar {
        background: var(--bg-sidebar) !important;
    }
    
    .nav-link {
        color: var(--text-sidebar) !important;
    }
    
    #topbar {
        background: var(--bg-header) !important;
    }
    
    /* Aplicar cor do texto do header - modo claro */
    #topbar,
    #topbar .page-title h1,
    #topbar .user-badge span,
    #topbar .toggle-btn,
    #topbar .theme-toggle {
        color: ' . htmlspecialchars($header_text) . ' !important;
    }
    
    /* CORREÇÃO: Remover efeito de gradiente do título quando cor customizada for aplicada */
    #topbar .page-title h1 {
        background: none !important;
        -webkit-background-clip: unset !important;
        -webkit-text-fill-color: unset !important;
        background-clip: unset !important;
        color: ' . htmlspecialchars($header_text) . ' !important;
    }
    
    /* Aplicar cor do texto do header - modo escuro */
    [data-theme="dark"] #topbar,
    [data-theme="dark"] #topbar .user-badge span,
    [data-theme="dark"] #topbar .toggle-btn,
    [data-theme="dark"] #topbar .theme-toggle {
        color: ' . htmlspecialchars($header_text_dark) . ' !important;
    }
    
    /* CORREÇÃO: Remover efeito de gradiente do título no modo escuro quando cor customizada for aplicada */
    [data-theme="dark"] #topbar .page-title h1 {
        background: none !important;
        -webkit-background-clip: unset !important;
        -webkit-text-fill-color: unset !important;
        background-clip: unset !important;
        color: ' . htmlspecialchars($header_text_dark) . ' !important;
    }
    
    body {
        background: var(--bg-body) !important;
        color: var(--text-main) !important;
    }
    
    /* Aplicar cor dos cards */
    .stat-card,
    .table-container,
    .card,
    [class*="card"] {
        background: var(--bg-card) !important;
    }
    
    /* Aplicar cor das bordas */
    .stat-card,
    .table-container,
    .card,
    [class*="card"],
    input,
    select,
    textarea {
        border-color: var(--border-color) !important;
    }
    
    /* Garantir que tabelas tenham cores corretas */
    [data-theme="dark"] table td {
        color: var(--text-main) !important;
        border-color: var(--border-color) !important;
    }
    
    [data-theme="dark"] table {
        color: var(--text-main) !important;
    }
    
    /* Aplicar cores de botões - modo claro */
    .btn-primary,
    button.btn,
    .action-buttons a[class*="btn-primary"],
    [class*="btn-primary"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_primary', '#009846')) . ' !important;
        color: #ffffff !important;
    }
    
    .btn-primary:hover,
    button.btn:hover,
    .action-buttons a[class*="btn-primary"]:hover {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_primary_hover', '#007635')) . ' !important;
        color: #ffffff !important;
    }
    
    /* Botões de ação - modo claro */
    .btn-edit,
    .action-buttons a[href*="editar"],
    [class*="btn-edit"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_edit', '#2196F3')) . ' !important;
        color: #ffffff !important;
    }
    
    .btn-view,
    .action-buttons a[href*="visualizar"],
    [class*="btn-view"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_view', '#17a2b8')) . ' !important;
        color: #ffffff !important;
    }
    
    .btn-delete,
    .action-buttons a[href*="excluir"],
    [class*="btn-delete"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_delete', '#dc3545')) . ' !important;
        color: #ffffff !important;
    }
    
    .btn-save,
    button[type="submit"].btn {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_save', '#28a745')) . ' !important;
        color: #ffffff !important;
    }
    
    /* Aplicar cores de botões - modo escuro (ajustadas para melhor contraste) */
    [data-theme="dark"] .btn-primary,
    [data-theme="dark"] button.btn,
    [data-theme="dark"] .action-buttons a[class*="btn-primary"],
    [data-theme="dark"] [class*="btn-primary"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_primary_dark', '#22c55e')) . ' !important;
        color: #ffffff !important;
    }
    
    [data-theme="dark"] .btn-primary:hover,
    [data-theme="dark"] button.btn:hover,
    [data-theme="dark"] .action-buttons a[class*="btn-primary"]:hover {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_primary_hover_dark', '#16a34a')) . ' !important;
        color: #ffffff !important;
    }
    
    /* Botões de ação - modo escuro */
    [data-theme="dark"] .btn-edit,
    [data-theme="dark"] .action-buttons a[href*="editar"],
    [data-theme="dark"] [class*="btn-edit"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_edit_dark', '#3b82f6')) . ' !important;
        color: #ffffff !important;
    }
    
    [data-theme="dark"] .btn-view,
    [data-theme="dark"] .action-buttons a[href*="visualizar"],
    [data-theme="dark"] [class*="btn-view"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_view_dark', '#06b6d4')) . ' !important;
        color: #ffffff !important;
    }
    
    [data-theme="dark"] .btn-delete,
    [data-theme="dark"] .action-buttons a[href*="excluir"],
    [data-theme="dark"] [class*="btn-delete"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_delete_dark', '#ef4444')) . ' !important;
        color: #ffffff !important;
    }
    
    [data-theme="dark"] .btn-save,
    [data-theme="dark"] button[type="submit"].btn {
        background-color: ' . htmlspecialchars(getSystemSetting('color_button_save_dark', '#22c55e')) . ' !important;
        color: #ffffff !important;
    }
    
    /* Cores de status - modo claro */
    .status-success,
    [class*="success"]:not(.badge):not(.btn) {
        color: ' . htmlspecialchars(getSystemSetting('color_status_success', '#28a745')) . ' !important;
    }
    
    .status-warning,
    [class*="warning"]:not(.badge):not(.btn) {
        color: ' . htmlspecialchars(getSystemSetting('color_status_warning', '#ffc107')) . ' !important;
    }
    
    .status-danger,
    [class*="danger"]:not(.badge):not(.btn) {
        color: ' . htmlspecialchars(getSystemSetting('color_status_danger', '#dc3545')) . ' !important;
    }
    
    .status-info,
    [class*="info"]:not(.badge):not(.btn) {
        color: ' . htmlspecialchars(getSystemSetting('color_status_info', '#17a2b8')) . ' !important;
    }
    
    /* Cores de status - modo escuro */
    [data-theme="dark"] .status-success,
    [data-theme="dark"] [class*="success"]:not(.badge):not(.btn) {
        color: ' . htmlspecialchars(getSystemSetting('color_status_success_dark', '#4ade80')) . ' !important;
    }
    
    [data-theme="dark"] .status-warning,
    [data-theme="dark"] [class*="warning"]:not(.badge):not(.btn) {
        color: ' . htmlspecialchars(getSystemSetting('color_status_warning_dark', '#fbbf24')) . ' !important;
    }
    
    [data-theme="dark"] .status-danger,
    [data-theme="dark"] [class*="danger"]:not(.badge):not(.btn) {
        color: ' . htmlspecialchars(getSystemSetting('color_status_danger_dark', '#f87171')) . ' !important;
    }
    
    [data-theme="dark"] .status-info,
    [data-theme="dark"] [class*="info"]:not(.badge):not(.btn) {
        color: ' . htmlspecialchars(getSystemSetting('color_status_info_dark', '#22d3ee')) . ' !important;
    }
    
    /* Cores de tabelas - modo claro */
    table th {
        background-color: ' . htmlspecialchars(getSystemSetting('color_table_header_bg', '#f8f9fa')) . ' !important;
        color: ' . htmlspecialchars(getSystemSetting('color_table_header_text', '#495057')) . ' !important;
    }
    
    table tr:hover {
        background-color: ' . htmlspecialchars(getSystemSetting('color_table_row_hover', '#f5f5f5')) . ' !important;
    }
    
    /* Cores de tabelas - modo escuro */
    [data-theme="dark"] table th {
        background-color: ' . htmlspecialchars($table_header_bg_dark) . ' !important;
        color: ' . htmlspecialchars($table_header_text_dark) . ' !important;
    }
    
    [data-theme="dark"] table tr:hover {
        background-color: ' . htmlspecialchars($table_row_hover_dark) . ' !important;
    }
    
    /* Cores de links - modo claro */
    a:not(.nav-link):not(.btn):not(.action-buttons a) {
        color: ' . htmlspecialchars(getSystemSetting('color_link', '#007bff')) . ' !important;
    }
    
    a:not(.nav-link):not(.btn):not(.action-buttons a):hover {
        color: ' . htmlspecialchars(getSystemSetting('color_link_hover', '#0056b3')) . ' !important;
    }
    
    /* Cores de links - modo escuro */
    [data-theme="dark"] a:not(.nav-link):not(.btn):not(.action-buttons a) {
        color: ' . htmlspecialchars(getSystemSetting('color_link_dark', '#60a5fa')) . ' !important;
    }
    
    [data-theme="dark"] a:not(.nav-link):not(.btn):not(.action-buttons a):hover {
        color: ' . htmlspecialchars(getSystemSetting('color_link_hover_dark', '#93c5fd')) . ' !important;
    }
    
    /* Cores de badges - modo claro */
    .badge.ativo,
    .badge[class*="success"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_badge_success', '#28a745')) . ' !important;
        color: #ffffff !important;
    }
    
    .badge.bloqueado,
    .badge[class*="danger"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_badge_danger', '#dc3545')) . ' !important;
        color: #ffffff !important;
    }
    
    .badge.admin,
    .badge[class*="info"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_badge_info', '#17a2b8')) . ' !important;
        color: #ffffff !important;
    }
    
    .badge.usuario,
    .badge[class*="warning"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_badge_warning', '#ffc107')) . ' !important;
        color: #000000 !important;
    }
    
    /* Cores de badges - modo escuro (ajustadas para melhor contraste) */
    [data-theme="dark"] .badge.ativo,
    [data-theme="dark"] .badge[class*="success"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_badge_success', '#22c55e')) . ' !important;
        color: #ffffff !important;
    }
    
    [data-theme="dark"] .badge.bloqueado,
    [data-theme="dark"] .badge[class*="danger"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_badge_danger', '#ef4444')) . ' !important;
        color: #ffffff !important;
    }
    
    [data-theme="dark"] .badge.admin,
    [data-theme="dark"] .badge[class*="info"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_badge_info', '#06b6d4')) . ' !important;
        color: #ffffff !important;
    }
    
    [data-theme="dark"] .badge.usuario,
    [data-theme="dark"] .badge[class*="warning"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_badge_warning', '#fbbf24')) . ' !important;
        color: #000000 !important;
    }
    
    /* Cores de inputs - modo claro */
    input:focus,
    select:focus,
    textarea:focus {
        border-color: ' . htmlspecialchars(getSystemSetting('color_input_focus', '#009846')) . ' !important;
        box-shadow: 0 0 0 0.2rem ' . htmlspecialchars(getSystemSetting('color_input_focus_shadow', 'rgba(0, 152, 70, 0.25)')) . ' !important;
    }
    
    /* Cores de inputs - modo escuro */
    [data-theme="dark"] input:focus,
    [data-theme="dark"] select:focus,
    [data-theme="dark"] textarea:focus {
        border-color: ' . htmlspecialchars(getSystemSetting('color_input_focus_dark', '#22c55e')) . ' !important;
        box-shadow: 0 0 0 0.2rem ' . htmlspecialchars(getSystemSetting('color_input_focus_shadow_dark', 'rgba(34, 197, 94, 0.3)')) . ' !important;
    }
    
    /* Inputs e selects - modo escuro */
    [data-theme="dark"] input,
    [data-theme="dark"] select,
    [data-theme="dark"] textarea {
        background-color: var(--bg-card) !important;
        color: var(--text-main) !important;
        border-color: var(--border-color) !important;
    }
    
    /* Cores de mensagens - modo claro */
    .alert-success,
    .message-success,
    [class*="alert-success"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_message_success_bg', '#d4edda')) . ' !important;
        color: ' . htmlspecialchars(getSystemSetting('color_message_success_text', '#155724')) . ' !important;
    }
    
    .alert-error,
    .alert-danger,
    .message-error,
    [class*="alert-error"],
    [class*="alert-danger"] {
        background-color: ' . htmlspecialchars(getSystemSetting('color_message_error_bg', '#f8d7da')) . ' !important;
        color: ' . htmlspecialchars(getSystemSetting('color_message_error_text', '#721c24')) . ' !important;
    }
    
    /* Cores de mensagens - modo escuro */
    [data-theme="dark"] .alert-success,
    [data-theme="dark"] .message-success,
    [data-theme="dark"] [class*="alert-success"] {
        background-color: ' . htmlspecialchars($message_success_bg_dark) . ' !important;
        color: ' . htmlspecialchars($message_success_text_dark) . ' !important;
    }
    
    [data-theme="dark"] .alert-error,
    [data-theme="dark"] .alert-danger,
    [data-theme="dark"] .message-error,
    [data-theme="dark"] [class*="alert-error"],
    [data-theme="dark"] [class*="alert-danger"] {
        background-color: ' . htmlspecialchars($message_error_bg_dark) . ' !important;
        color: ' . htmlspecialchars($message_error_text_dark) . ' !important;
    }
    
    /* Cores de modais - modo claro */
    .modal-content {
        background-color: ' . htmlspecialchars(getSystemSetting('color_modal_bg', '#ffffff')) . ' !important;
    }
    
    .modal-header {
        background-color: ' . htmlspecialchars(getSystemSetting('color_modal_header_bg', '#f8f9fa')) . ' !important;
        color: ' . htmlspecialchars(getSystemSetting('color_modal_header_text', '#333333')) . ' !important;
    }
    
    /* Cores de modais - modo escuro */
    [data-theme="dark"] .modal-content {
        background-color: ' . htmlspecialchars($modal_bg_dark) . ' !important;
    }
    
    [data-theme="dark"] .modal-header {
        background-color: ' . htmlspecialchars($modal_header_bg_dark) . ' !important;
        color: ' . htmlspecialchars($modal_header_text_dark) . ' !important;
    }
</style>';

