/* ==============================================================================
   ERP INTERNO DE COMPRAS - COMPONENTS & UI ELEMENTS
   Tablas, Tarjetas KPI, Modales, Badges, Timeline y Formularios
   ============================================================================== */

/* TARJETAS KPI DASHBOARD */
.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 18px;
    margin-bottom: 24px;
}

.kpi-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 20px;
    box-shadow: var(--shadow-sm);
    transition: all var(--transition-normal);
    position: relative;
    overflow: hidden;
}

.kpi-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-md);
    border-color: var(--primary-border);
}

.kpi-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.kpi-title {
    font-size: 12.5px;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.kpi-icon-wrap {
    width: 38px;
    height: 38px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
}

.kpi-val {
    font-size: 26px;
    font-weight: 800;
    color: var(--text-primary);
    line-height: 1.1;
    letter-spacing: -0.5px;
}

.kpi-sub {
    font-size: 11.5px;
    color: var(--text-muted);
    margin-top: 6px;
}

/* GRÁFICOS Y PANELES */
.dashboard-grid {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 20px;
    margin-bottom: 24px;
}

.panel {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
}

.panel-header {
    padding: 18px 22px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.panel-title {
    font-size: 15px;
    font-weight: 700;
    color: var(--text-primary);
}

.panel-body {
    padding: 20px;
    flex: 1;
}

/* TABLAS DE DATOS */
.table-container {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

.table-toolbar {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    background: #ffffff;
}

.filter-group {
    display: flex;
    align-items: center;
    gap: 10px;
}

.form-select, .form-control {
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: #ffffff;
    font-size: 13px;
    color: var(--text-primary);
    outline: none;
    transition: all var(--transition-fast);
}

.form-select:focus, .form-control:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.erp-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
    text-align: left;
}

.erp-table th {
    background: #f8fafc;
    padding: 12px 16px;
    font-size: 11.5px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
    border-bottom: 1px solid var(--border-color);
    white-space: nowrap;
}

.erp-table td {
    padding: 14px 16px;
    border-bottom: 1px solid var(--border-subtle);
    vertical-align: middle;
}

.erp-table tr:hover {
    background-color: #f8fafc;
}

.erp-table tr:last-child td {
    border-bottom: none;
}

/* BADGES DE ESTADOS */
.status-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border-radius: var(--radius-full);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.3px;
    text-transform: uppercase;
    white-space: nowrap;
}

.badge-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
}

.status-borrador { background: var(--status-draft-bg); color: var(--status-draft); }
.status-borrador .badge-dot { background: var(--status-draft); }

.status-pendiente { background: var(--status-pending-bg); color: #b45309; }
.status-pendiente .badge-dot { background: #d97706; }

.status-en-revision { background: var(--status-review-bg); color: #1d4ed8; }
.status-en-revision .badge-dot { background: #2563eb; }

.status-en-aprobacion { background: #fef3c7; color: #b45309; }
.status-en-aprobacion .badge-dot { background: #f59e0b; }

.status-aprobada { background: var(--status-approved-bg); color: #065f46; }
.status-aprobada .badge-dot { background: #10b981; }

.status-rechazada { background: var(--status-rejected-bg); color: #991b1b; }
.status-rechazada .badge-dot { background: #ef4444; }

.status-oc-generada { background: #ecfdf5; color: #047857; }
.status-oc-generada .badge-dot { background: #059669; }

.status-enviada-al-proveedor { background: #e0e7ff; color: #3730a3; }
.status-enviada-al-proveedor .badge-dot { background: #4f46e5; }

.status-factura-recibida { background: #ede9fe; color: #5b21b6; }
.status-factura-recibida .badge-dot { background: #8b5cf6; }

.status-validada { background: #e0f2fe; color: #0369a1; }
.status-validada .badge-dot { background: #0ea5e9; }

.status-pagada { background: #d1fae5; color: #065f46; }
.status-pagada .badge-dot { background: #10b981; }

.status-cerrada { background: #f1f5f9; color: #475569; }
.status-cerrada .badge-dot { background: #64748b; }

.status-cancelada { background: #fee2e2; color: #991b1b; }
.status-cancelada .badge-dot { background: #ef4444; }

.status-recepcionada { background: #dcfce7; color: #15803d; }
.status-recepcionada .badge-dot { background: #16a34a; }

.status-recepcion-parcial { background: #e0f2fe; color: #0369a1; }
.status-recepcion-parcial .badge-dot { background: #0284c7; }

.status-alerta-80 { background: #fef3c7; color: #b45309; }
.status-alerta-80 .badge-dot { background: #f59e0b; }

.status-excedido { background: #fee2e2; color: #b91c1c; }
.status-excedido .badge-dot { background: #dc2626; }

.status-normal { background: #ecfdf5; color: #047857; }
.status-normal .badge-dot { background: #10b981; }

.status-sin-cupo { background: #f1f5f9; color: #64748b; }
.status-sin-cupo .badge-dot { background: #94a3b8; }

/* PRIORIDAD BADGES */
.priority-badge {
    font-size: 11px;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: var(--radius-sm);
}

.priority-normal { background: #f1f5f9; color: #475569; }
.priority-alta { background: #fff7ed; color: #c2410c; }
.priority-urgente { background: #fef2f2; color: #b91c1c; font-weight: 800; }

/* TIMELINE VISUAL TRAZABILIDAD */
.timeline {
    position: relative;
    padding-left: 28px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.timeline::before {
    content: '';
    position: absolute;
    left: 8px;
    top: 6px;
    bottom: 6px;
    width: 2px;
    background: #e2e8f0;
}

.timeline-item {
    position: relative;
}

.timeline-dot {
    position: absolute;
    left: -28px;
    top: 2px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #ffffff;
    border: 3px solid var(--primary);
    box-shadow: 0 0 0 3px #eff6ff;
}

.timeline-dot.success { border-color: #10b981; box-shadow: 0 0 0 3px #d1fae5; }
.timeline-dot.danger { border-color: #ef4444; box-shadow: 0 0 0 3px #fee2e2; }

.timeline-content {
    background: #ffffff;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 12px 16px;
}

.timeline-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 4px;
}

.timeline-title {
    font-size: 13px;
    font-weight: 700;
    color: var(--text-primary);
}

.timeline-time {
    font-size: 11px;
    color: var(--text-muted);
}

.timeline-desc {
    font-size: 12.5px;
    color: var(--text-secondary);
}

.timeline-meta {
    font-size: 11px;
    color: var(--primary);
    margin-top: 4px;
    font-weight: 600;
}

/* MODALES */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(15, 23, 42, 0.6);
    backdrop-filter: blur(4px);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 20px;
}

.modal-overlay.open {
    display: flex;
    animation: modalFadeIn 0.2s ease forwards;
}

@keyframes modalFadeIn {
    from { opacity: 0; transform: scale(0.97); }
    to { opacity: 1; transform: scale(1); }
}

.modal-dialog {
    background: #ffffff;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    width: 100%;
    max-width: 900px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.modal-dialog > form {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.modal-header {
    padding: 18px 24px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #f8fafc;
    flex-shrink: 0;
}

.modal-title {
    font-size: 17px;
    font-weight: 800;
    color: var(--text-primary);
}

.modal-close {
    background: none;
    border: none;
    font-size: 20px;
    color: var(--text-muted);
    cursor: pointer;
}

.modal-body {
    padding: 24px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    flex: 1;
    min-height: 0;
}

.modal-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
    background: #f8fafc;
    flex-shrink: 0;
}

/* FORM GRID ROWS (DESKTOP & RESPONSIVE) */
.form-grid-row {
    display: grid;
    gap: 16px;
    margin-bottom: 16px;
}

.form-grid-2 {
    grid-template-columns: 1fr 1fr;
}

.form-grid-3 {
    grid-template-columns: 2fr 1fr 1fr;
}

.form-grid-summary {
    grid-template-columns: 1.2fr 0.8fr;
    gap: 20px;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 16px;
}

.form-grid-4 {
    grid-template-columns: repeat(4, 1fr);
}

/* CONTENEDOR CON SCROLL HORIZONTAL PARA TABLA DE ÍTEMS */
.items-editor-scroll-container {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    background: #ffffff;
    margin-top: 8px;
    margin-bottom: 16px;
}

.mobile-table-hint {
    display: none;
}

/* TABLA DINÁMICA DE ITEMS DE COMPRA */
.items-editor-table {
    width: 100%;
    min-width: 780px;
    border-collapse: collapse;
    font-size: 12.5px;
}

.items-editor-table th {
    background: #f1f5f9;
    padding: 8px 10px;
    font-size: 11px;
    text-transform: uppercase;
    color: #475569;
    border: 1px solid var(--border-color);
}

.items-editor-table td {
    padding: 6px;
    border: 1px solid var(--border-color);
}

.items-editor-table input, .items-editor-table select {
    width: 100%;
    padding: 6px 8px;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    font-size: 12.5px;
    outline: none;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.items-editor-table input:focus, .items-editor-table select:focus {
    border-color: #3b82f6;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.15);
}

/* Eliminar flechas / spinners en inputs numéricos de la tabla de ítems */
.items-editor-table input[type=number]::-webkit-inner-spin-button,
.items-editor-table input[type=number]::-webkit-outer-spin-button,
input.no-spinner::-webkit-inner-spin-button,
input.no-spinner::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.items-editor-table input[type=number],
input.no-spinner {
    -moz-appearance: textfield;
    appearance: textfield;
}


/* EXPEDIENTE DIGITAL CARDS */
.dossier-grid {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 16px;
    margin-bottom: 24px;
}

.dossier-doc-card {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: var(--radius-md);
    padding: 16px;
    text-align: center;
    transition: all var(--transition-fast);
}

.dossier-doc-card.active {
    background: #ffffff;
    border-color: var(--primary);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.1);
}

.dossier-icon {
    width: 44px;
    height: 44px;
    margin: 0 auto 10px;
    background: #eff6ff;
    color: var(--primary);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* NOTIFICACIONES DRAWER */
.notifications-drawer {
    position: absolute;
    top: 60px;
    right: 28px;
    width: 360px;
    background: #ffffff;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    display: none;
    flex-direction: column;
    z-index: 50;
    max-height: 480px;
}

.notifications-drawer.open {
    display: flex;
}

.notif-header {
    padding: 14px 18px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.notif-list {
    overflow-y: auto;
    max-height: 380px;
}

.notif-item {
    padding: 12px 18px;
    border-bottom: 1px solid var(--border-subtle);
    cursor: pointer;
    transition: background var(--transition-fast);
}

.notif-item.unread {
    background: #f0fdf4;
}

.notif-item:hover {
    background: #f8fafc;
}

/* TOAST NOTIFICATIONS */
.toast-container {
    position: fixed;
    bottom: 24px;
    right: 24px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    z-index: 200;
}

.toast {
    padding: 12px 18px;
    background: #0f172a;
    color: white;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    font-size: 13px;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 10px;
    animation: toastIn 0.3s ease;
}

.toast.success { background: #065f46; border-left: 4px solid #10b981; }
.toast.error { background: #7f1d1d; border-left: 4px solid #ef4444; }

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

/* ========================================================================== */
/* CONSTRUCTOR INTERACTIVO DE ORDEN DE COMPRA (FINANZAS)                      */
/* ========================================================================== */
.product-search-container {
    position: relative;
    margin-bottom: 12px;
}

.product-autocomplete-dropdown {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    max-height: 250px;
    overflow-y: auto;
    background: #ffffff;
    border: 1.5px solid #cbd5e1;
    border-radius: 8px;
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.16);
    z-index: 1050;
}

.product-autocomplete-item {
    padding: 9px 14px;
    border-bottom: 1px solid #f1f5f9;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background 0.15s ease, border-left 0.15s ease;
}

.product-autocomplete-item:last-child {
    border-bottom: none;
}

.product-autocomplete-item:hover,
.product-autocomplete-item.is-selected {
    background: #eff6ff !important;
    border-left: 4px solid #2563eb;
    padding-left: 10px;
}

.product-autocomplete-item .item-sku {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-weight: 700;
    color: #2563eb;
    font-size: 12px;
}

.product-autocomplete-item .item-name {
    font-weight: 600;
    color: #0f172a;
    margin-left: 8px;
    font-size: 13px;
}

.product-autocomplete-item .item-price {
    font-weight: 800;
    color: #059669;
    font-size: 13px;
    white-space: nowrap;
}

.supplier-filter-toggle {
    display: inline-flex;
    background: #f1f5f9;
    padding: 3px;
    border-radius: 8px;
    gap: 4px;
}

.supplier-filter-toggle button {
    border: none;
    background: transparent;
    padding: 5px 12px;
    font-size: 12px;
    font-weight: 600;
    border-radius: 6px;
    cursor: pointer;
    color: #64748b;
    transition: all 0.15s ease;
}

.supplier-filter-toggle button.is-active {
    background: #ffffff;
    color: #1e293b;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    font-weight: 700;
}

.supplier-chips-container {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
    margin-bottom: 12px;
}

.supplier-quick-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #f8fafc;
    border: 1px solid #cbd5e1;
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 11.5px;
    color: #334155;
    cursor: pointer;
    transition: all 0.15s ease;
}

.supplier-quick-chip:hover {
    background: #e0f2fe;
    border-color: #7dd3fc;
    color: #0369a1;
    transform: translateY(-1px);
}

.supplier-quick-chip .chip-price {
    font-weight: 700;
    color: #059669;
}

/* Indicador de Auto-Actualización en Bandeja */
.auto-refresh-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 11.5px;
    color: #475569;
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 20px;
    padding: 4px 11px;
    font-weight: 500;
    user-select: none;
}

.refresh-pulse-dot {
    width: 8px;
    height: 8px;
    background-color: #10b981;
    border-radius: 50%;
    display: inline-block;
    box-shadow: 0 0 0 rgba(16, 185, 129, 0.4);
    animation: pulseDotLive 2s infinite;
}

@keyframes pulseDotLive {
    0% {
        box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
    }
    70% {
        box-shadow: 0 0 0 6px rgba(16, 185, 129, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(16, 185, 129, 0);
    }
}

/* ==============================================================================
   RESPONSIVE DESIGN & MOBILE OPTIMIZATION (MODALES, TABLAS Y CAPTURA TÁCTIL)
   ============================================================================== */
@media (max-width: 768px) {
    .modal-overlay {
        padding: 0 !important;
        align-items: flex-start !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    .modal-dialog {
        max-width: 100% !important;
        width: 100% !important;
        height: 100% !important;
        max-height: 100% !important;
        min-height: 100vh;
        border-radius: 0 !important;
        margin: 0 !important;
    }

    .modal-dialog > form {
        display: flex;
        flex-direction: column;
        flex: 1;
        min-height: 0;
        height: 100%;
    }

    .modal-header {
        padding: 12px 16px !important;
        position: sticky;
        top: 0;
        z-index: 25;
        background: #ffffff;
        box-shadow: 0 1px 3px rgba(0,0,0,0.06);
    }

    .modal-body {
        padding: 14px 12px !important;
        overflow-y: auto !important;
        -webkit-overflow-scrolling: touch !important;
        flex: 1;
        min-height: 0;
    }

    .modal-footer {
        padding: 12px 14px !important;
        position: sticky;
        bottom: 0;
        z-index: 25;
        background: #ffffff;
        box-shadow: 0 -2px 12px rgba(0,0,0,0.08);
        flex-direction: column-reverse !important;
        gap: 8px !important;
    }

    .modal-footer .btn {
        width: 100% !important;
        padding: 11px 16px !important;
        font-size: 14px !important;
        justify-content: center;
    }

    /* Colapso de Grids a 1 Columna en Móvil */
    .form-grid-row,
    .form-grid-2,
    .form-grid-3,
    .form-grid-4,
    .form-grid-summary {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    /* Contenedor de scroll táctil horizontal en tabla de ítems */
    .items-editor-scroll-container {
        border: 1px solid #cbd5e1;
        border-radius: 8px;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        box-shadow: inset 0 1px 3px rgba(0,0,0,0.04);
        margin-bottom: 12px;
    }

    .items-editor-table {
        min-width: 780px !important;
        margin-top: 0;
    }

    .items-editor-table input,
    .items-editor-table select {
        font-size: 14px !important; /* Previene auto-zoom en iOS Safari */
        padding: 8px 6px !important;
    }

    .mobile-table-hint {
        display: inline-block !important;
        font-size: 10.5px !important;
        color: #0284c7 !important;
        background: #f0f9ff;
        border: 1px solid #bae6fd;
        padding: 2px 6px;
        border-radius: 4px;
        margin-top: 3px;
        font-weight: 600;
    }

    /* Adaptación para Modal de Aprobaciones / Confección OC en Móvil */
    #modalApproval .modal-footer {
        flex-direction: column-reverse !important;
        gap: 8px !important;
    }

    #modalApproval .modal-footer > div {
        display: flex;
        flex-direction: column !important;
        width: 100% !important;
        gap: 8px !important;
    }

    #modalApproval .modal-footer .btn {
        width: 100% !important;
    }

    .approval-tools-grid {
        grid-template-columns: 1fr !important;
        gap: 8px !important;
    }

    .approval-guide-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }

    .approval-guide-grid > div:first-child {
        border-right: none !important;
        border-bottom: 1px solid #e2e8f0;
        padding-right: 0 !important;
        padding-bottom: 12px;
    }
}


