/* Estilos base de la app (extraídos de index.html) */
body {
    font-family: 'Inter', sans-serif;
}

/* Evita el flash del login mientras se verifica una sesión previa */
body.auth-checking #login-screen,
body.auth-checking #main-app {
    display: none;
}

#auth-loader {
    display: none;
    position: fixed;
    inset: 0;
    background: #0b1222;
    color: #cbd5e1;
    align-items: center;
    justify-content: center;
    z-index: 999;
    padding: 1rem;
}

body.auth-checking #auth-loader {
    display: flex;
}

.auth-loader__card {
    background: rgba(30, 41, 59, 0.9);
    border: 1px solid #1f2937;
    border-radius: 12px;
    padding: 1.25rem 1.5rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    box-shadow: 0 20px 45px rgba(0, 0, 0, 0.35);
}

.auth-loader__spinner {
    width: 28px;
    height: 28px;
    border-radius: 9999px;
    border: 3px solid #1f2937;
    border-top-color: #3b82f6;
    animation: auth-spin 0.8s linear infinite;
}

@keyframes auth-spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* --- SIDEBAR COLAPSABLE ESTILO SUPABASE --- */
#sidebar {
    width: 4.5rem; /* Colapsado por defecto */
    transition: width 0.2s ease-in-out;
    overflow: hidden;
    z-index: 40;
}

#sidebar:hover {
    width: 16rem; /* Expandido al hover */
}

/* Ocultar etiquetas por defecto, mostrar en hover */
#sidebar .label {
    opacity: 0;
    width: 0;
    overflow: hidden;
    white-space: nowrap;
    transition: opacity 0.15s ease-in-out, width 0.2s ease-in-out;
}

#sidebar:hover .label {
    opacity: 1;
    width: auto;
}

/* Logo más pequeño cuando está colapsado */
#sidebar > .text-center {
    transition: all 0.2s ease-in-out;
}

#sidebar > .text-center img {
    transition: height 0.2s ease-in-out;
    height: 2.5rem; /* Pequeño por defecto */
}

#sidebar:hover > .text-center img {
    height: 5rem; /* Normal al expandir */
}

.sidebar-link {
    display: flex;
    align-items: center;
    padding: 0.75rem;
    border-radius: 0.5rem;
    transition: all 0.2s ease-in-out;
    color: #9ca3af; /* gray-400 */
    justify-content: center; /* Centrar iconos cuando colapsado */
}

#sidebar:hover .sidebar-link {
    justify-content: flex-start;
    padding: 0.75rem 1.25rem;
}

.sidebar-link:hover {
    background-color: #374151; /* gray-700 */
    color: #ffffff;
}
.sidebar-link.active {
    background: linear-gradient(90deg, #1D4ED8, #3B82F6);
    color: #ffffff;
    font-weight: 600;
    box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
}
.sidebar-link svg {
    flex-shrink: 0;
    width: 1.25rem;
    height: 1.25rem;
    margin-right: 0; /* Sin margen cuando colapsado */
    transition: margin 0.2s ease-in-out;
}

#sidebar:hover .sidebar-link svg {
    margin-right: 0.75rem; /* Margen al expandir */
}

.kpi-card {
    background-color: #1F2937; /* gray-800 */
    border-radius: 0.75rem;
    padding: 1.5rem;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    border: 1px solid #374151; /* gray-700 */
}
.kpi-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 10px 15px -3px rgba(59, 130, 246, 0.2), 0 4px 6px -2px rgba(59, 130, 246, 0.1);
}

.chart-container {
    background-color: #1F2937; /* gray-800 */
    padding: 1.5rem;
    border-radius: 0.75rem;
    border: 1px solid #374151; /* gray-700 */
}

/* Altura fija previa de los canvas de charts */
.chart-canvas-320 {
    width: 100%;
    height: auto;
    aspect-ratio: 16 / 9;
    max-height: 380px;
}

/* --- ESTILOS PARA LA VISTA PREVIA DE LA FACTURA --- */
#factura-vista-previa {
    background: white;
    width: 21cm;
    min-height: 29.7cm;
    margin: 2rem auto;
    padding: 1.5cm;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
    font-size: 12pt;
    color: #333;
    box-sizing: border-box;
}

/* --- COTIZACIÓN: reutiliza estilos de factura + marca de agua --- */
.quote-preview {
    background: white;
    width: 21cm;
    min-height: 29.7cm;
    margin: 2rem auto;
    padding: 1.5cm;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
    font-size: 12pt;
    color: #333;
    box-sizing: border-box;
    position: relative;
}
.quote-watermark {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) rotate(-25deg);
    font-size: 120px;
    font-weight: 800;
    color: rgba(29, 78, 216, 0.08); /* Azul muy tenue */
    letter-spacing: 4px;
    pointer-events: none;
    user-select: none;
    white-space: nowrap;
}

#factura-vista-previa p {
    margin: 4px 0;
}

.factura-header {
    display: grid;
    grid-template-columns: 60% 40%;
    gap: 20px;
    padding-bottom: 15px;
    border-bottom: 4px solid #1D4ED8; /* azul corporativo */
}

.logo-empresa {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
}
.logo-empresa img {
    height: 52px;
    width: auto;
    object-fit: contain;
}
.empresa-nombre {
    font-size: 1.4em;
    font-weight: 700;
    color: #1F2937;
}

.info-factura {
    text-align: right;
    border-right: 6px solid #F59E0B; /* naranja corporativo */
    padding-right: 15px;
}

.info-factura h2 {
    font-size: 2.5em;
    margin: 0;
}

.info-cliente-fecha {
    display: grid;
    grid-template-columns: 60% 40%;
    gap: 20px;
    margin-top: 20px;
    padding: 10px;
    background-color: #f9f9f9;
    border: 1px solid #ddd;
}

.fecha-factura {
    text-align: right;
    padding-right: 8px; /* desplazar levemente hacia la izquierda */
}

.tabla-items {
    margin-top: 30px;
}

.items-table {
    width: 100%;
    border-collapse: collapse;
}

.items-table th, .items-table td {
    padding: 10px;
    border: 1px solid #ddd;
}
/* Números/cantidades y montos SIEMPRE en negro para legibilidad */
.items-table td:nth-child(1), /* cantidad */
.items-table td:nth-child(3), /* subtotal item */
.totales span,
#factura-ncf,
#factura-subtotal,
#factura-itbis,
#factura-total {
    color: #000000 !important;
}

.items-table thead {
    background: linear-gradient(90deg, #1D4ED8, #F59E0B);
    color: white;
    text-align: left;
}

.seccion-totales {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
}

.totales {
    width: 40%;
}

.totales p {
    display: flex;
    justify-content: space-between;
    margin: 5px 0;
    padding: 5px;
}

.totales .total-final {
    background: linear-gradient(90deg, #1D4ED8, #F59E0B);
    color: white;
    font-weight: bold;
    font-size: 1.2em;
}

.pie-pagina {
    margin-top: 50px;
    padding-top: 15px;
    border-top: 2px solid #555;
    display: grid;
    grid-template-columns: 1fr 1fr;
    font-style: italic;
}

.pie-pagina .original {
    text-align: right;
}

/* --- ESTILOS DE IMPRESIÓN --- */
@media print {
    body * { visibility: hidden; }
    .no-print, .no-imprimir, .no-print * , .no-imprimir * { display: none !important; }
    #invoice-modal, #invoice-modal * { visibility: visible; }
    #factura-vista-previa, #factura-vista-previa * { visibility: visible; }
    #invoice-modal { position: static !important; inset: auto !important; background: transparent !important; border: 0 !important; box-shadow: none !important; padding: 0 !important; }
    #factura-vista-previa { position: relative; left: 0; top: 0; width: 100%; border: none; box-shadow: none; margin: 0; padding: 0; }
    #quote-modal, #quote-modal * { visibility: visible; }
    #cotizacion-vista-previa, #cotizacion-vista-previa * { visibility: visible; }
    #quote-modal { position: static !important; inset: auto !important; background: transparent !important; border: 0 !important; box-shadow: none !important; padding: 0 !important; }
    #cotizacion-vista-previa { position: relative; left: 0; top: 0; width: 100%; border: none; box-shadow: none; margin: 0; padding: 0; }
}

/* --- OPTIMIZACIONES PARA MÓVIL --- */
@media (max-width: 1024px) {
    /* Reflow del layout: sidebar arriba y contenido debajo */
    #main-app > div.flex.h-screen.bg-gray-900 {
        flex-direction: column;
        height: auto;
        min-height: 100vh;
    }

    /* Sidebar como barra superior deslizable */
    #sidebar {
        width: 100%;
        height: auto;
        flex-direction: row;
        align-items: center;
        gap: 0.5rem;
        border-right: 0;
        border-bottom: 1px solid #1f2937;
        position: sticky;
        top: 0;
        z-index: 50;
        background-color: rgba(31, 41, 55, 0.9);
        backdrop-filter: blur(6px);
    }

    #sidebar:hover {
        width: 100%;
    }

    #sidebar > .text-center {
        display: none;
    }

    #sidebar nav {
        display: flex;
        flex: 1;
        gap: 0.5rem;
        overflow-x: auto;
        padding: 0.25rem;
    }

    .sidebar-link {
        justify-content: center !important;
        flex: 1 0 auto;
        padding: 0.75rem;
        min-width: 3rem;
    }

    /* Ocultar etiquetas en móvil para dejar solo íconos */
    #sidebar .label {
        display: none;
    }

    /* Reducir padding general de contenido */
    main.flex-1 {
        padding: 1rem !important;
    }

    /* Tarjetas y gráficos más compactos */
    .kpi-card {
        padding: 1.1rem;
    }

    .chart-canvas-320 {
        max-height: 260px;
    }

    /* Tablas horizontales scrollables */
    .content-section .bg-gray-800.rounded-lg {
        overflow-x: auto;
    }

    .content-section table {
        min-width: 680px;
    }

    /* Cotizaciones: evitar overflow del header en tablets/móvil */
    #cotizaciones > .flex {
        flex-wrap: wrap;
        gap: 0.75rem;
    }

    #cotizaciones > .flex > div:first-child {
        min-width: 0;
    }

    #cotizaciones > .flex > .flex {
        width: 100%;
        justify-content: flex-start;
        gap: 0.5rem;
    }

    #cotizaciones input#q-search {
        flex: 1 1 220px;
        width: 100%;
        max-width: 320px;
    }

    #cotizaciones button#q-add {
        flex-shrink: 0;
    }
}

@media (max-width: 640px) {
    /* Mejorar tap targets y tipografía */
    .sidebar-link {
        padding: 0.75rem 0.9rem;
    }

    h2.text-3xl {
        font-size: 1.5rem;
    }

    /* Separar más los inputs en modales */
    input, select, button {
        touch-action: manipulation;
    }
}

/* Modal de Cuentas: permitir scroll en pantallas pequeñas */
#case-review-modal > div {
    max-height: 90vh;
    overflow-y: auto;
}

/* Modal de Facturas: permitir scroll en pantallas pequeñas */
#invoice-form-modal > div {
    max-height: 90vh;
    overflow-y: auto;
}

/* Aplicar el mismo comportamiento de scroll a todos los modales */
#quote-form-modal > div,
#invoice-modal > div,
#quote-modal > div,
#plate-modal > div,
#plate-view-modal > div,
#plate-edit-modal > div,
#plate-upgrade-modal > div,
#printer-loading-modal > div,
#print-loading-modal > div,
#printer-error-modal > div,
#ncf-modal > div,
#fin-form-modal > div,
#fin-detail-modal > div,
#fin-schedule-modal > div,
#fin-pay-modal > div,
#fin-history-modal > div {
    max-height: 90vh;
    overflow-y: auto;
    overscroll-behavior: contain;
}

/* --- ESTILOS PARA FINANCIAMIENTOS --- */

/* Tabs del formulario de financiamiento */
.fin-form-tab {
    transition: all 0.2s ease;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
}

.fin-form-tab:hover {
    color: #9ca3af;
}

.fin-form-tab.active {
    color: #ffffff;
    border-bottom-color: #3B82F6;
}

/* Contenido de tabs */
.fin-tab-content {
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Resumen de cálculo en el formulario */
#fin-tab-terminos .bg-blue-900\/30 {
    background: linear-gradient(135deg, rgba(29, 78, 216, 0.15) 0%, rgba(59, 130, 246, 0.1) 100%);
}

/* Estado badges en la tabla */
#fin-tbody .bg-green-500\/20 { background-color: rgba(34, 197, 94, 0.2); }
#fin-tbody .bg-red-500\/20 { background-color: rgba(239, 68, 68, 0.2); }
#fin-tbody .bg-blue-500\/20 { background-color: rgba(59, 130, 246, 0.2); }
#fin-tbody .bg-gray-500\/20 { background-color: rgba(107, 114, 128, 0.2); }
#fin-tbody .bg-purple-500\/20 { background-color: rgba(168, 85, 247, 0.2); }
#fin-tbody .bg-yellow-500\/20 { background-color: rgba(234, 179, 8, 0.2); }

/* Barra de progreso en tabla */
#fin-tbody .bg-gray-700 {
    background-color: #374151;
}

#fin-tbody .bg-blue-500 {
    background-color: #3B82F6;
}

/* Cronograma de pagos - filas con estado */
#fin-schedule-tbody tr.bg-green-900\/20 {
    background-color: rgba(20, 83, 45, 0.2);
}

#fin-schedule-tbody tr.bg-red-900\/20 {
    background-color: rgba(127, 29, 29, 0.2);
}

/* Alertas de pagos atrasados */
#fin-alerts-container .bg-red-900\/30 {
    background-color: rgba(127, 29, 29, 0.3);
}

/* Dashboard KPIs de financiamiento - tamaño compacto */
#financiamientos .kpi-card {
    padding: 1rem;
}

#financiamientos .kpi-card p.text-2xl,
#financiamientos .kpi-card p.text-xl {
    margin-top: 0.25rem;
}

/* Responsive para financiamientos */
@media (max-width: 1024px) {
    #financiamientos > .flex.flex-col.lg\\:flex-row {
        flex-direction: column;
    }
    
    #financiamientos .flex.flex-wrap {
        width: 100%;
    }
    
    #financiamientos input#fin-search {
        flex: 1 1 200px;
        max-width: 100%;
    }
}

/* --- ESTILOS PARA CONTRATO/PAGARÉ --- */
.contract-preview {
    background: white;
    width: 21cm;
    min-height: 29.7cm;
    margin: 1rem auto;
    padding: 2cm 1.5cm;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);
    font-size: 11pt;
    color: #333;
    box-sizing: border-box;
    line-height: 1.6;
}

.contract-preview p {
    margin: 4px 0;
}

.contract-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding-bottom: 20px;
    border-bottom: 3px solid #1D4ED8;
    margin-bottom: 20px;
}

.contract-header .logo-empresa img {
    height: 70px;
    width: auto;
    object-fit: contain;
}

.contract-title {
    text-align: right;
}

.contract-title h1 {
    font-size: 1.5em;
    font-weight: 700;
    color: #1D4ED8;
    margin: 0;
}

.contract-title h2 {
    font-size: 1.1em;
    font-weight: 600;
    color: #F59E0B;
    margin: 0;
}

.contract-title .contract-number {
    font-size: 0.9em;
    margin-top: 5px;
    color: #666;
}

.contract-intro {
    text-align: justify;
    margin-bottom: 20px;
}

.contract-parties {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    margin-bottom: 20px;
}

.contract-parties .party {
    padding: 15px;
    background: #f8f9fa;
    border-left: 4px solid #1D4ED8;
}

.contract-parties .party.garante {
    border-left-color: #F59E0B;
    grid-column: 1 / -1;
}

.contract-parties .party h3 {
    font-size: 0.9em;
    color: #666;
    margin: 0 0 8px 0;
    text-transform: uppercase;
}

.contract-vehicle,
.contract-terms {
    margin-bottom: 20px;
}

.contract-vehicle h3,
.contract-terms h3,
.contract-clauses h3,
.contract-notes h3 {
    font-size: 1em;
    color: #1D4ED8;
    border-bottom: 1px solid #ddd;
    padding-bottom: 5px;
    margin-bottom: 10px;
}

.contract-vehicle table,
.contract-terms table {
    width: 100%;
    border-collapse: collapse;
}

.contract-vehicle td,
.contract-terms td {
    padding: 8px;
    border: 1px solid #ddd;
}

.contract-terms .total-row {
    background: linear-gradient(90deg, #1D4ED8, #3B82F6);
    color: white;
}

.contract-terms .total-row td {
    border-color: #1D4ED8;
}

.contract-clauses ol {
    padding-left: 20px;
}

.contract-clauses li {
    margin-bottom: 10px;
    text-align: justify;
}

.contract-notes {
    margin-bottom: 20px;
    padding: 15px;
    background: #fff8e1;
    border-left: 4px solid #F59E0B;
}

.contract-signatures {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 50px;
    margin-top: 60px;
    padding-top: 20px;
}

.contract-signatures .signature {
    text-align: center;
}

.contract-signatures .garante-signature {
    grid-column: 1 / -1;
    max-width: 300px;
    margin: 0 auto;
}

.signature-line {
    border-top: 1px solid #333;
    width: 80%;
    margin: 0 auto 10px;
    height: 50px;
}

.contract-footer {
    margin-top: 40px;
    padding-top: 15px;
    border-top: 1px solid #ddd;
    text-align: center;
    font-size: 0.85em;
    color: #666;
}

/* Estilos de impresión para contrato */
@media print {
    #fin-contract-modal, #fin-contract-modal * { visibility: visible; }
    #contrato-vista-previa, #contrato-vista-previa * { visibility: visible; }
    #fin-contract-modal { position: static !important; inset: auto !important; background: transparent !important; border: 0 !important; box-shadow: none !important; padding: 0 !important; }
    #contrato-vista-previa { position: relative; left: 0; top: 0; width: 100%; border: none; box-shadow: none; margin: 0; padding: 1cm; }
}