/* --- RESET Y ESTILOS GENERALES --- */
:root {
    --color-primario: #34568B; /* Azul Índigo Formal */
    --color-primario-hover: #2a456e;
    --color-secundario: #6c757d; /* Gris */
    --color-secundario-hover: #5a6268;
    --color-exito: #198754; /* Verde */
    --color-exito-hover: #146c43;
    --color-peligro: #dc3545; /* Rojo */
    --color-fondo: #f8f9fa;
    --color-texto: #212529;
    --color-borde: #dee2e6;
    --sombra-card: 0 4px 12px rgba(0,0,0,0.08);
}

.dark-mode {
    --color-primario: #5a88d1;
    --color-primario-hover: #7ba4e3;
    --color-secundario: #8a939b;
    --color-secundario-hover: #a1aab2;
    --color-exito: #2a9d8f;
    --color-exito-hover: #3ac0b1;
    --color-peligro: #e76f51;
    --color-fondo: #121212;
    --color-texto: #e0e0e0;
    --color-borde: #333333;
    --sombra-card: 0 4px 12px rgba(0,0,0,0.4);

    /* Variables específicas del modal y tabla */
    --color-fondo-modal: #1e1e1e;
    --color-tabla-header: #282828;
    --color-input-disabled: #2a2a2a;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background-color: var(--color-fondo);
    margin: 0;
    padding: 0;
    color: var(--color-texto);
    transition: background-color 0.3s, color 0.3s;
}

* {
    box-sizing: border-box;
}

/* --- HEADER / BARRA DE NAVEGACIÓN --- */
.navbar {
    width: 100%;
    background-color: #ffffff;
    box-shadow: var(--sombra-card);
    padding: 0 2rem;
    height: 70px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.navbar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--color-primario);
    text-decoration: none;
}
.navbar-brand img {
    max-height: 40px;
}
.navbar-nav .btn-link-header {
    display: flex;
    align-items: center;
    gap: 8px;
    background-color: var(--color-secundario);
    color: white;
    padding: 10px 15px;
    border-radius: 5px;
    text-decoration: none;
    font-weight: 500;
    font-size: 0.9rem;
    transition: background-color 0.3s;
}
.navbar-nav .btn-link-header:hover {
    background-color: var(--color-secundario-hover);
}

/* --- CONTENEDOR PRINCIPAL --- */
.container {
    max-width: 1200px;
    margin: 2rem auto;
    padding: 0 1rem;
}

/* --- DISEÑO DE TARJETAS (CARDS) --- */
.card {
    background-color: #ffffff;
    border: 1px solid var(--color-borde);
    border-radius: 8px;
    box-shadow: var(--sombra-card);
    margin-bottom: 2rem;
    transition: background-color 0.3s, box-shadow 0.3s, border-color 0.3s;
}
.card-header {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--color-primario);
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--color-borde);
}
.card-header i {
    font-size: 1rem;
}
.card-body {
    padding: 1.5rem;
}

/* --- FORMULARIO EN GRID (Inspirado en tu imagen) --- */
.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 1.5rem;
}
.form-group {
    display: flex;
    flex-direction: column;
}
label {
    display: block;
    margin-bottom: 8px;
    color: #555;
    font-weight: 500;
}
input[type="number"], input[type="text"] {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--color-borde);
    border-radius: 4px;
    font-size: 16px;
}
input:disabled {
    background-color: #f0f0f0;
    cursor: not-allowed;
}

/* --- ESTILOS DE COSTOS ADICIONALES --- */
.addon-group {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 15px;
    padding: 10px;
    border: 1px solid #eee;
    border-radius: 4px;
}
.addon-group label {
    flex-grow: 1;
    display: flex;
    align-items: center;
    margin-bottom: 0;
}
.addon-group input[type="checkbox"] {
    margin-right: 10px;
    width: 18px;
    height: 18px;
}
.addon-group input[type="number"] {
    width: 100px;
    margin-left: 15px;
}

/* --- BOTONES PRINCIPALES --- */
.btn {
    width: 100%;
    padding: 12px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 18px;
    font-weight: bold;
    transition: background-color 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}
#btnAgregar {
    background-color: var(--color-primario);
    color: white;
    margin-top: 1rem; /* Espacio después del último campo */
}
#btnAgregar:hover {
    background-color: var(--color-primario-hover);
}
#btnImprimir {
    background-color: var(--color-exito);
    color: white;
}
#btnImprimir:hover {
    background-color: var(--color-exito-hover);
}
#btnImprimir:disabled {
    background-color: #a3d9b1;
    cursor: not-allowed;
}

/* --- TABLA DE RESUMEN Y TOTALES --- */

/* ¡NUEVO! Contenedor para el header del resumen y el botón */
.card-header-with-button {
    display: flex;
    justify-content: space-between;
    align-items: center;
    /* Usamos el padding y borde del .card-header original */
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--color-borde);
}
/* Quitamos el padding y borde del .card-header cuando está dentro del nuevo */
.card-header-with-button .card-header {
    padding: 0;
    border-bottom: none;
    margin: 0; /* Aseguramos que no tenga márgenes */
}
/* Ajusta el botón 'Agregar Productos' */
.card-header-with-button #btnAbrirModal {
    width: auto; /* Ancho automático */
    flex-shrink: 0; /* No se encoge */
    margin-left: 1rem;
    /* Hereda el estilo .btn, pero definimos color por si acaso */
    background-color: var(--color-primario);
    color: white;
    font-size: 16px; /* Un poco más pequeño para el header */
    padding: 10px 15px;
}
.card-header-with-button #btnAbrirModal:hover {
    background-color: var(--color-primario-hover);
}

.summary-table-wrapper {
    max-height: 40vh; /* Altura máxima en desktop */
    overflow-y: auto; /* Scroll vertical */
    overflow-x: auto; /* ¡Scroll horizontal para móvil! */
    border: 1px solid var(--color-borde);
    border-radius: 4px;
}
.summary-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
    /* margin-bottom ya no es necesario aquí */
}
.summary-table th, .summary-table td {
    padding: 10px;
    border: 1px solid var(--color-borde);
    text-align: left;
    white-space: nowrap;
}
.summary-table th {
    background-color: #f7f7f7;
    position: sticky; /* Header pegajoso */
    top: 0;
}
.summary-table .text-right { text-align: right; }
.summary-table .action-cell { text-align: center; }
.summary-table button {
    font-size: 14px;
    padding: 5px 10px;
    background-color: var(--color-peligro);
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}
.summary-table button:hover {
    background-color: #b02a37;
}

.grand-totals {
    width: 50%;
    max-width: 400px;
    margin-left: auto;
    margin-top: 20px;
    margin-bottom: 1.5rem; /* Espacio antes del botón Imprimir */
}
.grand-totals .result-item {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
    font-size: 18px;
}
.grand-totals .result-item span { font-weight: bold; }
.grand-totals .total-final strong { font-size: 20px; }
.grand-totals .total-final span {
    color: var(--color-exito);
    font-size: 22px;
}

/* --- ESTILOS DE PÁGINA DE HISTORIAL --- */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}
.page-header h1 {
    margin: 0;
}
.btn-link-volver {
    display: flex;
    align-items: center;
    gap: 8px;
    background-color: var(--color-secundario);
    color: white;
    padding: 10px 15px;
    border-radius: 5px;
    text-decoration: none;
    font-weight: 500;
    font-size: 0.9rem;
    transition: background-color 0.3s;
}
.btn-link-volver:hover {
    background-color: var(--color-secundario-hover);
}

/* --- ESTILOS DE IMPRESIÓN (Sin cambios) --- */
#plantillaCotizacion { display: none; }
@media print {
     body * { visibility: hidden; }
    #plantillaCotizacion, #plantillaCotizacion * { visibility: visible; }
    #plantillaCotizacion { position: absolute; left: 0; top: 0; width: 100%; font-size: 12pt; display: block !important; }
    /* Estilos de la plantilla que faltaban */
    .header-cotizacion { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; margin-bottom: 40px; }
    #logoCotizacion { max-height: 80px; margin-right: 20px; }
    .header-cotizacion div { text-align: left; }
    .header-cotizacion h1 { width: 100%; text-align: center; margin-top: 20px; margin-bottom: 0; }
    .header-cotizacion h2, .header-cotizacion p { margin: 0; }
    .info-cotizacion { display: flex; justify-content: space-between; margin-bottom: 30px; }
    .tabla-cotizacion { width: 100%; border-collapse: collapse; margin-bottom: 30px; }
    .tabla-cotizacion th, .tabla-cotizacion td { border: 1px solid #ccc; padding: 10px; text-align: left; }
    .tabla-cotizacion th { background-color: #f2f2f2; }
    .tabla-cotizacion .text-right { text-align: right; }
    .totales-cotizacion { width: 50%; margin-left: auto; }
    .footer-cotizacion { margin-top: 50px; text-align: center; font-style: italic; font-size: 12px; }
}


/* --- ESTILOS DEL MODAL FLOTANTE --- */

/* El fondo oscuro semi-transparente */
.modal-overlay {
    position: fixed; /* Se queda fijo en la pantalla */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6); /* Fondo negro al 60% */
    z-index: 1000; /* Se pone encima de todo */
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0; /* Inicia invisible */
    pointer-events: none; /* Inicia sin poder clickearse */
    transition: opacity 0.3s ease;
}
/* La ventana blanca */
.modal-content {
    background-color: var(--color-fondo); /* Usa el color de fondo del body */
    border-radius: 8px;
    width: 90%;
    max-width: 800px; /* Ancho máximo del modal */
    max-height: 90vh; /* Altura máxima */
    display: flex;
    flex-direction: column;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    transform: scale(0.9); /* Inicia un poco pequeño */
    transition: transform 0.3s ease;
}

/* ¡ARREGLO PARA SCROLL! (Punto 2) */
.modal-content .card {
    margin-bottom: 0;
    border: none;
    box-shadow: none;
    flex-grow: 1; /* Permite que el card crezca */
    display: flex;
    flex-direction: column;
    overflow: hidden; /* ¡CLAVE! Fuerza al card a contener a sus hijos */
}
.modal-content .card-header {
    flex-shrink: 0; /* Evita que el header se encoja */
}
.modal-content .card-body {
    overflow-y: auto; /* ¡El scroll ahora está DENTRO del modal! */
    flex-grow: 1; /* ¡CLAVE! Hace que el body ocupe el espacio restante */
}

/* Botón de cerrar (la 'X') */
.modal-close {
    position: absolute;
    top: 10px;
    right: 15px;
    font-size: 2rem;
    color: #aaa;
    cursor: pointer;
    line-height: 1;
    transition: color 0.2s;
}
.modal-close:hover {
    color: #333;
}

/* --- CLASES PARA MOSTRAR/OCULTAR EL MODAL --- */
.modal-overlay.visible {
    opacity: 1;
    pointer-events: auto; /* Se puede clickear */
}
.modal-overlay.visible .modal-content {
    transform: scale(1); /* Tamaño normal */
}

/* --- ESTILOS PARA DARK MODE TOGGLE --- */
.navbar-nav {
    display: flex;
    align-items: center;
    gap: 1.5rem; /* Añade espacio entre el link y el toggle */
}
.switch { 
    position: relative; 
    display: inline-block; 
    width: 40px; 
    height: 20px;
}
.switch input { opacity: 0; width: 0; height: 0; }
.slider { 
    position: absolute; 
    cursor: pointer; 
    top: 0; left: 0; right: 0; bottom: 0; 
    background-color: var(--color-secundario); 
    transition: 0.4s; 
    border-radius: 20px;
}
.slider:before { 
    position: absolute; 
    content: ""; 
    height: 16px; width: 16px; 
    left: 2px; bottom: 2px; 
    background-color: white; 
    transition: 0.4s; 
    border-radius: 50%;
    /* Iconos de Sol/Luna */
    display: flex; 
    align-items: center; 
    justify-content: center; 
    font-family: "Font Awesome 6 Free"; 
    font-weight: 900; 
    color: #ffc107; 
    content: "\f185"; /* Sol */
    font-size: 10px;
}
input:checked + .slider { 
    background-color: var(--color-primario); 
}
input:checked + .slider:before { 
    transform: translateX(20px); 
    color: var(--color-primario); 
    content: "\f186"; /* Luna */
}

/* --- REGLAS DE DARK MODE --- */
.dark-mode .card,
.dark-mode .navbar,
.dark-mode .summary-table th,
.dark-mode .modal-content {
    background-color: var(--color-fondo-modal);
}
.dark-mode input[type="number"], 
.dark-mode input[type="text"] {
    background-color: var(--color-fondo);
    color: var(--color-texto);
    border-color: var(--color-borde);
}
.dark-mode input:disabled {
    background-color: var(--color-input-disabled);
}
.dark-mode label {
    color: var(--color-texto); /* Texto blanco/legible */
}
.dark-mode .modal-close {
    color: #888;
}
.dark-mode .modal-close:hover {
    color: #eee;
}

/* --- RESPONSIVIDAD --- */
@media (max-width: 768px) {
    
    /* --- Arreglos del Modal en Móvil --- */
    .modal-content {
        width: 95%; /* Ocupa el 95% del ancho */
        max-height: 95vh; /* Ocupa el 95% de la altura */
        padding: 0;
    }
    .modal-content .card-body {
        padding: 1.5rem 1rem; /* Menos padding interno */
    }
    .modal-content .card-header {
        padding: 1rem 1.5rem;
    }
    .modal-close {
        top: 12px;
        right: 12px;
        font-size: 1.8rem;
    }

    /* --- Arreglos del Formulario en Modal --- */
    .modal-content .form-row {
        gap: 1rem; /* Reduce el espacio */
    }

    /* --- Arreglos del Resumen en Pág. Principal --- */
    /* Ajusta el nuevo header de resumen en móvil */
    .card-header-with-button {
        flex-direction: column;
        align-items: flex-start; /* Alinea todo a la izquierda */
        gap: 1rem;
    }
    .card-header-with-button #btnAbrirModal {
        width: 100%; /* El botón ocupa todo el ancho en móvil */
    }

    /* Ajusta el footer del resumen */
    .summary-footer {
        flex-direction: column; /* Apila los totales y el botón */
        align-items: stretch; /* Ocupan todo el ancho */
        gap: 1rem;
    }
    .summary-footer #btnImprimir {
        flex-basis: auto; /* El botón se ajusta */
    }
    .summary-footer .grand-totals {
        max-width: none; /* Los totales ocupan todo el ancho */
        margin-bottom: 1rem;
    }

    /* Ajuste del contenedor principal */
    .container {
        margin: 1rem auto;
        padding: 0 0.5rem;
    }
    .navbar {
        padding: 0 1rem;
    }
}
.filter-buttons {
    display: flex;
    gap: 10px;
    margin-top: 1.5rem;
}
.btn-filter-search, .btn-filter-clear {
    padding: 10px 15px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background-color 0.3s;
}
.btn-filter-search {
    background-color: var(--color-primario);
    color: white;
}
.btn-filter-search:hover {
    background-color: var(--color-primario-hover);
}
.btn-filter-clear {
    background-color: var(--color-secundario);
    color: white;
}
.btn-filter-clear:hover {
    background-color: var(--color-secundario-hover);
}

/* --- ESTILOS PARA ACCIONES DE TABLA --- */
.action-cell-buttons {
    display: flex;
    justify-content: center;
    gap: 10px;
}
.btn-history, .btn-delete {
    padding: 5px 10px;
    border: none;
    border-radius: 4px;
    color: white;
    font-size: 14px;
    text-decoration: none;
    cursor: pointer;
}
.btn-history {
    background-color: var(--color-exito);
}
.btn-history:hover {
    background-color: var(--color-exito-hover);
}
.btn-delete {
    background-color: var(--color-peligro);
}
.btn-delete:hover {
    background-color: #b02a37;
}
