<?php
require_once __DIR__ . '/../config/config.php';
header('Content-Type: text/css; charset=UTF-8');

$db = getDB();
$stmt = $db->query("SELECT * FROM configuracion LIMIT 1");
$config = $stmt->fetch();

$colorPrimario = $config['color_primario'] ?? '#1a237e';
$colorSecundario = $config['color_secundario'] ?? '#4caf50';
$temaOscuro = isset($_COOKIE['tema_oscuro']) ? $_COOKIE['tema_oscuro'] == '1' : ($config['tema_oscuro'] ?? 0);

// Generar variaciones de color
function adjustBrightness($hex, $percent) {
    $hex = str_replace('#', '', $hex);
    $r = hexdec(substr($hex, 0, 2));
    $g = hexdec(substr($hex, 2, 2));
    $b = hexdec(substr($hex, 4, 2));
    
    $r = max(0, min(255, $r + ($r * $percent / 100)));
    $g = max(0, min(255, $g + ($g * $percent / 100)));
    $b = max(0, min(255, $b + ($b * $percent / 100)));
    
    return '#' . str_pad(dechex(round($r)), 2, '0', STR_PAD_LEFT) 
              . str_pad(dechex(round($g)), 2, '0', STR_PAD_LEFT) 
              . str_pad(dechex(round($b)), 2, '0', STR_PAD_LEFT);
}

function hexToRgb($hex) {
    $hex = str_replace('#', '', $hex);
    return [
        hexdec(substr($hex, 0, 2)),
        hexdec(substr($hex, 2, 2)),
        hexdec(substr($hex, 4, 2))
    ];
}

$colorPrimarioClaro = adjustBrightness($colorPrimario, 30);
$colorPrimarioOscuro = adjustBrightness($colorPrimario, -25);
$colorSecundarioClaro = adjustBrightness($colorSecundario, 20);
$rgbPrimario = hexToRgb($colorPrimario);
?>

/* ===== TEMA PERSONALIZADO - LABPRO ===== */

:root {
    --color-primario: <?= $colorPrimario ?>;
    --color-primario-claro: <?= $colorPrimarioClaro ?>;
    --color-primario-oscuro: <?= $colorPrimarioOscuro ?>;
    --color-secundario: <?= $colorSecundario ?>;
    --color-secundario-claro: <?= $colorSecundarioClaro ?>;
    --color-primario-rgb: <?= implode(',', $rgbPrimario) ?>;
}

<?php if ($temaOscuro): ?>
/* ===== TEMA OSCURO ===== */
body {
    background-color: #1a1a1a !important;
    color: #e0e0e0 !important;
}

.card {
    background-color: #2d2d2d !important;
    border-color: #404040 !important;
    color: #e0e0e0 !important;
}

.card-header {
    background-color: #363636 !important;
    border-color: #404040 !important;
    color: #ffffff !important;
}

.table {
    color: #e0e0e0 !important;
    border-color: #404040 !important;
}

.table thead th {
    background-color: #363636 !important;
    border-color: #404040 !important;
    color: #ffffff !important;
}

.table-striped tbody tr:nth-of-type(odd) {
    background-color: rgba(255, 255, 255, 0.03) !important;
}

.table-hover tbody tr:hover {
    background-color: rgba(255, 255, 255, 0.07) !important;
}

.form-control, .form-select {
    background-color: #2d2d2d !important;
    border-color: #404040 !important;
    color: #e0e0e0 !important;
}

.form-control:focus, .form-select:focus {
    background-color: #363636 !important;
    border-color: var(--color-primario) !important;
    color: #e0e0e0 !important;
}

.modal-content {
    background-color: #2d2d2d !important;
    color: #e0e0e0 !important;
}

.modal-header {
    background-color: #363636 !important;
    border-color: #404040 !important;
}

.modal-footer {
    border-color: #404040 !important;
}

.list-group-item {
    background-color: #2d2d2d !important;
    border-color: #404040 !important;
    color: #e0e0e0 !important;
}

.sidebar {
    background: linear-gradient(180deg, #111827 0%, #1a1a2e 50%, #0d0d1a 100%) !important;
    border-right-color: #2a2a3e !important;
}

.sidebar .nav-link {
    color: rgba(255,255,255,0.5) !important;
}

.sidebar .nav-link:hover {
    background-color: rgba(255, 255, 255, 0.08) !important;
    color: #ffffff !important;
}

.sidebar .nav-link.active {
    background: var(--color-primario-claro) !important;
    color: #ffffff !important;
    box-shadow: 0 4px 18px rgba(0,0,0,0.3) !important;
}

.sidebar .nav-section-label {
    color: rgba(255,255,255,0.2) !important;
}

.sidebar-toggle-btn {
    border-color: #1a1a1a !important;
}

.theme-toggle {
    background: #2d2d2d !important;
    border: 1px solid #404040 !important;
}

.theme-toggle:hover {
    background: #363636 !important;
}

.mobile-menu-btn {
    background: #2d2d2d !important;
    border: 1px solid #404040 !important;
}

.alert-info {
    background-color: #1e3a5f !important;
    border-color: #2c5282 !important;
    color: #90cdf4 !important;
}

.alert-success {
    background-color: #1e4620 !important;
    border-color: #2d5f30 !important;
    color: #9ae6b4 !important;
}

.alert-warning {
    background-color: #5a4a1a !important;
    border-color: #7a6a2a !important;
    color: #f6e05e !important;
}

.alert-danger {
    background-color: #5a1a1a !important;
    border-color: #7a2a2a !important;
    color: #fc8181 !important;
}

.text-muted {
    color: #909090 !important;
}

.border {
    border-color: #404040 !important;
}

.dropdown-menu {
    background-color: #2d2d2d !important;
    border-color: #404040 !important;
}

.dropdown-item {
    color: #e0e0e0 !important;
}

.dropdown-item:hover {
    background-color: #363636 !important;
}

input[type="date"]::-webkit-calendar-picker-indicator,
input[type="time"]::-webkit-calendar-picker-indicator {
    filter: invert(1);
}

<?php endif; ?>

/* ===== COLORES PERSONALIZADOS ===== */

/* Sidebar usa color primario */
.sidebar {
    background: linear-gradient(180deg, 
        var(--color-primario) 0%, 
        var(--color-primario-oscuro) 40%, 
        #0c1222 100%) !important;
}

.sidebar .nav-link.active {
    background: var(--color-primario-claro) !important;
    box-shadow: 0 4px 18px rgba(var(--color-primario-rgb), 0.3);
}

.sidebar .nav-link:hover i {
    color: var(--color-primario-claro) !important;
}

.logo-text i {
    color: var(--color-primario-claro) !important;
    -webkit-text-fill-color: var(--color-primario-claro) !important;
}

.sidebar-toggle-btn {
    background: var(--color-primario-claro) !important;
}

.sidebar-toggle-btn:hover {
    background: var(--color-primario) !important;
}

.user-avatar {
    background: var(--color-primario-claro) !important;
}

/* Botones */
.btn-primary {
    background: var(--color-primario) !important;
    border: none !important;
}

.btn-primary:hover {
    background: var(--color-primario-oscuro) !important;
    border: none !important;
    box-shadow: 0 4px 15px rgba(var(--color-primario-rgb), 0.3);
}

.btn-success {
    background-color: var(--color-secundario) !important;
    border-color: var(--color-secundario) !important;
}

/* Botones flotantes */
.mobile-menu-btn {
    background: var(--color-primario) !important;
}

.theme-toggle {
    background: var(--color-primario) !important;
}

.theme-toggle:hover {
    background: var(--color-primario-oscuro) !important;
}

/* Acentos */
.bg-primary {
    background-color: var(--color-primario) !important;
}

.text-primary {
    color: var(--color-primario) !important;
}

.border-primary {
    border-color: var(--color-primario) !important;
}

.page-header h2 i {
    color: var(--color-primario) !important;
}

.form-control:focus, .form-select:focus {
    border-color: var(--color-primario) !important;
    box-shadow: 0 0 0 0.2rem rgba(var(--color-primario-rgb), 0.15) !important;
}

/* Stat card primaria usa color del sistema */
.stat-card.orders {
    background: linear-gradient(135deg, var(--color-primario) 0%, var(--color-primario-claro) 100%) !important;
    box-shadow: 0 8px 32px rgba(var(--color-primario-rgb), 0.25) !important;
}

/* ===== RESPONSIVE DESIGN ===== */

/* Tablets (768px - 1024px) */
@media (max-width: 1024px) and (min-width: 768px) {
    .sidebar {
        width: 200px !important;
    }
    
    .main-content {
        margin-left: 200px !important;
    }

    body.sidebar-collapsed .main-content {
        margin-left: 72px !important;
    }
    
    .card-body {
        padding: 1rem !important;
    }
    
    table {
        font-size: 0.9rem !important;
    }
    
    .btn {
        font-size: 0.85rem !important;
        padding: 0.4rem 0.8rem !important;
    }
}

/* Teléfonos (menos de 768px) */
@media (max-width: 767px) {
    .sidebar {
        left: -260px;
        width: 260px !important;
        transition: left 0.3s ease, width 0.3s ease;
    }
    
    .sidebar.show {
        left: 0;
    }

    .sidebar.collapsed {
        width: 260px !important;
    }

    .sidebar-toggle-btn {
        display: none !important;
    }
    
    .main-content {
        margin-left: 0 !important;
        padding: 60px 12px 12px !important;
    }

    body.sidebar-collapsed .main-content {
        margin-left: 0 !important;
    }
    
    .mobile-menu-btn {
        display: block !important;
        position: fixed;
        top: 12px;
        left: 12px;
        z-index: 1040;
        width: 44px;
        height: 44px;
        background: var(--color-primario) !important;
        border: none;
        border-radius: 12px;
        color: white;
        font-size: 1.3rem;
        box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    }
    
    .card {
        margin-bottom: 1rem !important;
    }
    
    .card-header h5 {
        font-size: 1rem !important;
    }
    
    h2 {
        font-size: 1.5rem !important;
    }
    
    h3 {
        font-size: 1.25rem !important;
    }
    
    .table {
        font-size: 0.8rem !important;
    }
    
    .table thead {
        display: none;
    }
    
    .table tr {
        display: block;
        margin-bottom: 1rem;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 0.5rem;
    }
    
    .table td {
        display: flex;
        justify-content: space-between;
        padding: 0.5rem !important;
        border: none !important;
    }
    
    .table td:before {
        content: attr(data-label);
        font-weight: bold;
        margin-right: 1rem;
    }
    
    .btn {
        font-size: 0.8rem !important;
        padding: 0.4rem 0.6rem !important;
    }
    
    .btn-group {
        display: flex;
        flex-wrap: wrap;
        gap: 0.25rem;
    }
    
    .d-flex.gap-2 {
        flex-direction: column !important;
    }
    
    .d-flex.gap-2 > * {
        width: 100% !important;
    }
    
    .form-control, .form-select {
        font-size: 16px !important; /* Evita zoom en iOS */
    }
    
    .modal-dialog {
        margin: 0.5rem !important;
    }
}

/* Computadores grandes (más de 1400px) */
@media (min-width: 1400px) {
    .container-fluid {
        max-width: 1600px;
        margin: 0 auto;
    }
    
    .card-body {
        padding: 2rem;
    }
}

/* Mejoras generales responsive */
.table-responsive {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

img {
    max-width: 100%;
    height: auto;
}

/* Botón de tema oscuro */
.theme-toggle {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 1030;
    width: 46px;
    height: 46px;
    border-radius: 14px;
    background: var(--color-primario);
    border: none;
    color: white;
    font-size: 1.2rem;
    box-shadow: 0 4px 18px rgba(0,0,0,0.2);
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.theme-toggle:hover {
    transform: translateY(-2px) scale(1.05);
    box-shadow: 0 8px 25px rgba(0,0,0,0.3);
}

@media (max-width: 767px) {
    .theme-toggle {
        bottom: 15px;
        right: 15px;
        width: 42px;
        height: 42px;
        font-size: 1.1rem;
        border-radius: 12px;
    }
}

/* Overlay para menú móvil */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1040;
    backdrop-filter: blur(2px);
}

@media (max-width: 767px) {
    .sidebar-overlay.show {
        display: block;
    }
}

/* Ocultar botón de menú móvil en desktop */
.mobile-menu-btn {
    display: none;
}

/* Animaciones suaves */
* {
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

button, a, input, select, textarea {
    transition: all 0.2s ease;
}
