:root {
    --primary-color: #004785; /* Azul institucional */
    --secondary-color: #f8f9fa;
    --text-color: #333;
    --border-color: #ddd;
    --accent-color: #2c5282;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    margin: 0;
    padding: 0;
    background-color: #f0f2f5;
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

header {
    background-color: white;
    border-bottom: 4px solid var(--primary-color);
    padding: 1rem 2rem;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    max-width: 1200px;
    margin: 0 auto;
    gap: 20px;
}

.header-actions {
    display: flex;
    align-items: center;
}

.btn-upload {
    background-color: #fff;
    color: var(--primary-color);
    padding: 8px 16px;
    border-radius: 6px;
    text-decoration: none;
    font-weight: 600;
    font-size: 0.9rem;
    transition: all 0.3s ease;
    border: 1px solid transparent;
}

/* =========================================
   ESTILOS PARA CARGA DE DATOS (UPLOAD)
   ========================================= */

.upload-card {
    background: #fff;
    padding: 2.5rem;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
    width: 100%;
    max-width: 100%; /* Asegurar que no se limite por nada */
    box-sizing: border-box;
    margin: 0 auto;
}

.instructions {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    padding: 1.5rem;
    border-radius: 8px;
    margin-bottom: 2rem;
    width: 100%;
    box-sizing: border-box;
}

.textarea-data {
    width: 100%;
    height: 400px;
    font-family: 'Courier New', Courier, monospace;
    padding: 1.5rem;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    margin-bottom: 1.5rem;
    resize: none;
    white-space: pre;
    overflow: auto;
    background-color: #ffffff;
    display: block;
    box-sizing: border-box;
    font-size: 0.95rem;
    line-height: 1.5;
    transition: border-color 0.2s;
}

.textarea-data:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.alert {
    padding: 1.2rem;
    border-radius: 8px;
    margin-bottom: 1.5rem;
    font-size: 0.95rem;
    border: 1px solid transparent;
}

.alert-success { 
    background-color: #d4edda; 
    color: #155724; 
    border: 1px solid #c3e6cb; 
}

.alert-error { 
    background-color: #f8d7da; 
    color: #721c24; 
    border: 1px solid #f5c6cb; 
}

.nav-links a {
    display: inline-block;
    margin-bottom: 1rem;
    color: var(--primary-color);
    text-decoration: none;
    font-weight: 600;
}

.nav-links a:hover {
    text-decoration: underline;
}

.logo-img {
    height: 60px; /* Ajustar según la imagen real */
    width: auto;
}

.header-title h1 {
    margin: 0;
    font-size: 1.5rem;
    color: var(--primary-color);
    text-transform: uppercase;
}

.header-title p {
    margin: 0;
    font-size: 0.9rem;
    color: #666;
}

main {
    flex: 1;
    padding: 2rem;
    max-width: 1400px; /* Aumentado para permitir tablas y cuadros de carga anchos */
    margin: 0 auto;
    width: 100%;
    box-sizing: border-box;
}

.search-card {
    background: white;
    border-radius: 8px;
    padding: 2rem;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    margin-bottom: 2rem;
    text-align: center;
}

.search-card h2 {
    margin-top: 0;
    color: var(--primary-color);
}

.form-group {
    margin-bottom: 1.5rem;
    width: 100%;
    text-align: left;
}

.upload-card .form-group {
    max-width: none; /* Eliminar restricción para la carga masiva */
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: bold;
    color: #555;
}

.form-control {
    width: 100%;
    padding: 10px;
    font-size: 1rem;
    border: 1px solid #ccc;
    border-radius: 4px;
    box-sizing: border-box;
    text-align: left; /* Forzar alineación a la izquierda */
    direction: ltr;   /* Forzar dirección de texto de izquierda a derecha */
}

.btn-submit {
    background-color: var(--primary-color);
    color: white;
    border: none;
    padding: 12px 30px;
    font-size: 1rem;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.3s;
}

.btn-submit:hover {
    background-color: var(--accent-color);
}

.results-section {
    background: white;
    border-radius: 8px;
    padding: 2rem;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    animation: fadeIn 0.5s ease-in;
}

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

.section-title {
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 0.5rem;
    margin-bottom: 1rem;
    color: var(--primary-color);
    font-size: 1.2rem;
}

.data-grid {
    display: grid !important;
    grid-template-columns: 120px 1fr 60px 1fr !important; /* RUT fijo, Nombre y Repartición comparten espacio, Sexo fijo */
    gap: 1.5rem !important;
    width: 100% !important;
    margin-bottom: 2rem;
    align-items: start !important;
}

.data-item {
    display: flex !important;
    flex-direction: column !important;
    gap: 0.25rem !important;
    min-width: 0 !important;
    align-items: flex-start !important; /* Alineación a la izquierda por defecto */
    text-align: left !important;
}

/* Ajustes específicos para Desktop */
.data-item:nth-child(1) { width: auto; }
.data-item:nth-child(3) { width: auto; }
.data-item:nth-child(4) { width: auto; }

.data-item strong {
    display: block;
    color: #666;
    font-size: 0.85rem;
    margin-bottom: 0.2rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.data-item span {
    font-size: 1.1rem;
    font-weight: 600;
    color: #2d3748;
}

.table-responsive {
    overflow-x: auto;
}

.custom-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
}

.custom-table th, .custom-table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #eee;
}

.custom-table th {
    background-color: #f8f9fa;
    color: var(--primary-color);
    font-weight: 600;
}

.custom-table tr:hover {
    background-color: #f9f9f9;
}

footer {
    background-color: var(--primary-color);
    color: white;
    text-align: center;
    padding: 1.5rem;
    margin-top: auto;
}

.footer-content p {
    margin: 5px 0;
    font-size: 0.9rem;
    opacity: 0.9;
}

/* Modal Styles */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0,0,0,0.4);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.modal-overlay.show {
    display: flex;
}

.modal-card {
    background: #fff;
    width: 90%;
    max-width: 600px;
    border-radius: 10px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.15);
    overflow: hidden;
}

.modal-topbar {
    height: 6px;
    background: #22c55e;
}

.modal-content {
    padding: 2rem;
    text-align: center;
}

.modal-icon {
    font-size: 32px;
    color: #22c55e;
    margin-bottom: 0.5rem;
}

.modal-title {
    margin: 0.5rem 0 1rem;
    color: #0d47a1;
    font-weight: 700;
    font-size: 1.4rem;
}

.modal-body {
    color: #555;
    margin-bottom: 1.5rem;
    line-height: 1.5;
}

.modal-actions {
    display: flex;
    justify-content: center;
}

.btn-primary {
    background-color: var(--primary-color);
    color: #fff;
    border: none;
    padding: 10px 24px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1rem;
}

.btn-primary:hover {
    background-color: var(--accent-color);
}

/* Estilos específicos para la tabla de resultados en el modal */
.results-table-container {
    max-height: 400px;
    overflow-y: auto;
    margin: 1.5rem 0;
    border: 1px solid #eee;
    border-radius: 6px;
}

.modal-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.modal-table th, .modal-table td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #eee;
}

.modal-table th {
    background-color: #f8f9fa;
    position: sticky;
    top: 0;
    color: var(--primary-color);
    font-weight: 700;
}

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

.modal-card.modal-large {
    max-width: 800px;
}

/* Colores para filas del modal */
.row-duplicate {
    background-color: #fff5f5;
    color: #c53030;
}

.row-duplicate td {
    border-bottom: 1px solid #feb2b2;
}

.row-success {
    background-color: #f0fff4;
    color: #276749;
}

/* =========================================
   ESTILOS PARA AUDITORÍA (AUDIT)
   ========================================= */

.audit-reparticion {
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    margin-bottom: 2.5rem;
    overflow: hidden;
}

.reparticion-title {
    background-color: #f8f9fa;
    padding: 1.2rem 1.5rem;
    margin: 0;
    border-bottom: 2px solid var(--primary-color);
    color: var(--primary-color);
    font-size: 1.3rem;
    display: flex;
    align-items: center;
    gap: 15px;
}

.reparticion-title .badge {
    background-color: var(--primary-color);
    color: #fff;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.9rem;
}

.audit-table-container {
    overflow-x: auto;
}

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

.audit-table th {
    background-color: #f1f3f5;
    color: #495057;
    font-weight: 700;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.5px;
    padding: 12px 15px;
    text-align: left;
    border-bottom: 1px solid #dee2e6;
}

.audit-table td {
    padding: 12px 15px;
    border-bottom: 1px solid #eee;
    font-size: 0.95rem;
}

.row-titular {
    background-color: #f1f5f9;
    border-left: 4px solid var(--primary-color);
}

.row-titular td {
    border-bottom: 1px solid #e2e8f0;
    font-weight: 600;
}

.row-carga {
    background-color: #ffffff;
}

.row-carga td {
    color: #64748b;
    font-size: 0.9rem;
}

.row-carga td.padding-left {
    padding-left: 50px;
    position: relative;
}

.row-carga td.padding-left::before {
    content: "";
    position: absolute;
    left: 25px;
    top: 0;
    bottom: 0;
    width: 2px;
    background-color: #e2e8f0;
}

.row-carga td.padding-left::after {
    content: "";
    position: absolute;
    left: 25px;
    top: 50%;
    width: 15px;
    height: 2px;
    background-color: #e2e8f0;
}

/* Botones de navegación */
.admin-nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid #e2e8f0;
}

.admin-nav .btn-primary,
.admin-nav .btn-secondary {
    padding: 8px 16px;
    font-size: 0.85rem;
    border-radius: 4px;
    font-weight: 600;
}

.btn-secondary {
    background-color: transparent;
    color: #64748b;
    border: 1px solid #cbd5e1;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    transition: all 0.2s;
    cursor: pointer;
}

.btn-secondary:hover {
    background-color: #f1f5f9;
    color: #334155;
    border-color: #94a3b8;
}

.label {
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
}

.label-titular {
    background-color: #ebf8ff;
    color: #2b6cb0;
    border: 1px solid #bee3f8;
}

.label-carga {
    background-color: #fefcbf;
    color: #744210;
    border: 1px solid #faf089;
}

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

/* =========================================
   MEDIA QUERIES PARA RESPONSIVIDAD (MOBILE)
   ========================================= */

@media screen and (max-width: 600px) {
    /* 1. Header más compacto */
    header {
        padding: 0.8rem 1rem;
    }
    
    .header-content {
        flex-direction: column;
        text-align: center;
        gap: 15px; /* Aumentado un poco el gap para que respire */
    }
    
    .logo-img {
        height: auto; /* Permitir que la altura sea automática */
        max-width: 180px; /* Limitar el ancho máximo del logo en móviles */
        max-height: 60px; /* Mantener una altura razonable */
    }
    
    .header-title h1 {
        font-size: 1.1rem; /* Reducir tamaño de fuente significativamente */
        line-height: 1.3;
        margin-bottom: 5px;
        word-wrap: break-word; /* Asegurar que no desborde */
    }
    
    .header-title p {
        font-size: 0.85rem;
    }
    
    /* 2. Contenedores con menos padding */
    main {
        padding: 1rem;
    }
    
    .search-card, .results-section {
        padding: 1.5rem 1rem;
    }
    
    /* 3. Data Grid a una columna (Stack) */
    .data-grid {
        display: flex !important;
        flex-direction: column !important;
        gap: 1.2rem !important;
    }
    
    .data-item {
        width: 100% !important;
        align-items: flex-start !important;
        border-bottom: 1px solid #eee;
        padding-bottom: 0.8rem;
    }
    
    .data-item:last-child {
        border-bottom: none;
        padding-bottom: 0;
    }
    
    /* 4. Tabla de Cargas transformada a Tarjetas */
    .custom-table, 
    .custom-table tbody, 
    .custom-table tr, 
    .custom-table td {
        display: block;
        width: 100%;
    }
    
    /* Ocultar encabezados originales */
    .custom-table thead {
        display: none;
    }
    
    .custom-table tr {
        margin-bottom: 1.5rem;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        box-shadow: 0 1px 3px rgba(0,0,0,0.05);
        background-color: white;
        overflow: hidden;
    }
    
    .custom-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        text-align: right;
        padding: 12px 15px;
        border-bottom: 1px solid #f0f0f0;
        font-size: 0.95rem;
    }
    
    .custom-table td:last-child {
        border-bottom: none;
    }
    
    /* Etiquetas para cada celda (Pseudo-elementos) */
    .custom-table td::before {
        content: attr(data-label);
        font-weight: 700;
        color: #4a5568;
        text-align: left;
        margin-right: 15px;
        font-size: 0.85rem;
        text-transform: uppercase;
    }
    
    /* Inyectar textos específicos para las columnas conocidas */
    /* Columna 1: RUT Carga */
    .custom-table td:nth-of-type(1)::before {
        content: "RUT";
    }
    /* Columna 2: Nombre */
    .custom-table td:nth-of-type(2)::before {
        content: "Nombre";
    }
    /* Columna 3: Sexo */
    .custom-table td:nth-of-type(3)::before {
        content: "Sexo";
    }
    /* Columna 4: Repartición */
    .custom-table td:nth-of-type(4)::before {
        content: "Repartición";
    }
    
    /* Estilo especial para el nombre (negrita) */
    .custom-table td:nth-of-type(2) {
        background-color: #f8fafc;
        color: var(--primary-color);
        font-weight: 600;
        justify-content: flex-start;
        flex-direction: column;
        align-items: flex-start;
        text-align: left;
        gap: 5px;
    }
    
    .custom-table td:nth-of-type(2)::before {
        margin-bottom: 2px;
        color: #718096;
    }
}

/* =========================================
   ESTILOS PARA FILTROS Y RESUMEN (AUDIT)
   ========================================= */

.summary-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 2rem;
}

.summary-card {
    flex: 1;
    min-width: 200px;
    background: white;
    padding: 1.2rem;
    border-radius: 6px;
    border: 1px solid #e2e8f0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    transition: all 0.2s;
}

.summary-card:hover {
    border-color: var(--primary-color);
}

.summary-icon {
    display: none; /* Minimalismo: eliminamos iconos grandes */
}

.summary-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.summary-label {
    font-size: 0.75rem;
    color: #64748b;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.summary-value {
    font-size: 1.5rem;
    font-weight: 700;
    color: #1e293b;
}

/* Filtros Minimalistas */
.filter-section {
    background: #f8fafc;
    padding: 1.2rem;
    border-radius: 6px;
    border: 1px solid #e2e8f0;
    margin-bottom: 2rem;
}

.filter-form {
    display: flex;
    flex-wrap: wrap;
    gap: 1.2rem;
    align-items: flex-end;
}

.filter-form .form-group {
    margin: 0;
    flex: 1;
    min-width: 180px;
}

.filter-form label {
    font-size: 0.75rem;
    font-weight: 700;
    color: #475569;
    margin-bottom: 6px;
    text-transform: uppercase;
}

.filter-form select, 
.filter-form input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid #cbd5e1;
    border-radius: 4px;
    font-size: 0.9rem;
    color: #334155;
    background-color: white;
    transition: border-color 0.2s;
}

.filter-form select:focus, 
.filter-form input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.filter-form .form-actions {
    display: flex;
    gap: 8px;
}

.reparticion-stats {
    margin-left: auto;
    display: flex;
    gap: 20px;
    font-size: 0.85rem;
    color: #64748b;
}

.reparticion-stats span {
    background: #f1f5f9;
    padding: 2px 10px;
    border-radius: 4px;
    border: 1px solid #e2e8f0;
}

.reparticion-stats strong {
    color: var(--primary-color);
}

@media screen and (max-width: 768px) {
    .reparticion-title {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }
    
    .reparticion-stats {
        margin-left: 0;
        width: 100%;
        justify-content: space-between;
    }
}

/* Estilos para colapsar reparticiones */
.toggle-reparticion {
    cursor: pointer;
    user-select: none;
    transition: background-color 0.2s;
}

.toggle-reparticion:hover {
    background-color: #edf2f7 !important;
}

.toggle-icon {
    display: inline-block;
    width: 20px;
    font-size: 0.8rem;
    color: var(--primary-color);
    transition: transform 0.2s;
}

.collapsible-content {
    max-height: 5000px; /* Suficiente para tablas largas */
    overflow: hidden;
    transition: max-height 0.3s ease-in-out, opacity 0.3s ease-in-out;
    opacity: 1;
}

.audit-reparticion.collapsed .collapsible-content {
    max-height: 0;
    opacity: 0;
    border: none;
}

.audit-reparticion.collapsed {
    margin-bottom: 1rem;
}
