/* ============================================
   CONTENEDOR PRINCIPAL (CAJA FUERTE)
   ============================================ */
#contenedor-principal {
    width: 100%;
    max-width: 100% !important;
    box-sizing: border-box !important;
    padding: 15px;
    overflow: hidden !important; /* NADA sale de aquí */
}

/* ============================================
   SECCIÓN TABLA (ENVOLTORIO)
   ============================================ */
.seccion-tabla {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    margin-bottom: 30px;
    width: 100% !important;
    max-width: 100% !important;
    overflow: hidden !important; /* CLAVE: Contiene la tabla */
    box-sizing: border-box !important;
}

/* ============================================
   CONTENEDOR TABLA JQUERY (SCROLL)
   ============================================ */
.contenedor-tabla-jquery {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 70vh;
    overflow-x: auto !important; /* Scroll horizontal si la tabla es muy ancha */
    overflow-y: auto !important; /* Scroll vertical si hay muchas filas */
    border: 1px solid #cbd5e1;
    border-top: none;
    border-radius: 0 0 8px 8px;
    position: relative;
    background: #fff;
    box-sizing: border-box !important;
}

/* ============================================
   TABLA GRID PURO (ENCERRADA)
   ============================================ */
.tabla-grid-puro {
    table-layout: fixed !important; /* OBLIGA a respetar los anchos */
    width: 100% !important;        /* Ocupa el 100% y ni un pixel más */
    min-width: 600px;              /* Si hay pocas columnas, se ve decente. Si hay muchas, el scroll-x hace su trabajo */
    border-collapse: separate !important;
    border-spacing: 0 !important;
    font-size: 13px;
}

/* ============================================
   HEADER TH (FIJO ARRIBA) - SIN OVERFLOW:HIDDEN
   ============================================ */
.tabla-grid-puro thead th {
    position: sticky !important;
    top: 0;
    z-index: 10 !important;
    background: #1e293b !important; 
    padding: 10px 12px !important;
    font-weight: 600 !important;
    color: #ffffff !important;
    border-bottom: 2px solid #334155 !important;
    border-right: 1px solid #334155 !important;
    user-select: none !important;
    text-align: left !important;
    /* ✅ QUITAMOS overflow: hidden y white-space: nowrap de aquí */
}
/* ✅ NUEVA REGLA: El texto se corta, pero el TH permite que el resizer se vea */
.titulo-texto {
    display: block !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
    white-space: nowrap !important;
    width: 100% !important;
}


/* ✅ REGLA RECUPERADA: Para que el resizer se vea y funcione */
.th-resizer {
    position: absolute !important;
    right: 0 !important;
    top: 0 !important;
    width: 6px !important;
    height: 100% !important;
    cursor: col-resize !important;
    z-index: 20 !important; /* Mayor que el z-index del th */
    background: transparent !important;
    transition: background 0.2s !important;
}

.th-resizer:hover,
.th-resizer.resizing {
    background-color: #3b82f6 !important;
    opacity: 0.8 !important;
}
/* ============================================
   CELDAS TD (PROTEGIDAS CONTRA DESBORDAMIENTO)
   ============================================ */
.tabla-grid-puro tbody td {
    padding: 0 !important; /* El input tendrá el padding */
    border-bottom: 1px solid #e2e8f0 !important;
    border-right: 1px solid #e2e8f0 !important;
    vertical-align: middle !important;
    background: #ffffff !important;
    overflow: hidden !important; /* CLAVE: Que la celda no se estire por su contenido */
    word-break: break-word !important; /* Rompe palabras largas */
    box-sizing: border-box !important;
}

/* Efecto cebra */
.tabla-grid-puro tbody tr:nth-child(even) td {
    background-color: #f8fafc !important;
}

/* Hover */
.tabla-grid-puro tbody tr:hover td {
    background-color: #e0f2fe !important;
}

/* Fila seleccionada */
.fila-seleccionada td {
    background-color: #bfdbfe !important;
}

/* ============================================
   INPUTS DENTRO DEL GRID (CONTROLADOS)
   ============================================ */
.input-grid {
    width: 100% !important;
    max-width: 100% !important; /* No crecer más que la celda */
    box-sizing: border-box !important;
    border: 1px solid #e2e8f0 !important; 
    outline: none !important;
    background: #ffffff !important;
    padding: 8px 10px !important;
    font: inherit !important;
    color: #334155 !important;
    font-size: 13px !important;
    transition: all 0.2s !important;
    border-radius: 0 !important; /* Cuadrados para ocupar toda la celda */
    display: block !important;
}

.input-grid:hover {
    border-color: #94a3b8 !important;
}

.input-grid:focus {
    border-color: #3b82f6 !important;
    background-color: #ffffff !important;
    box-shadow: inset 0 0 0 1px #3b82f6 !important;
}

/* Fecha */
.input-grid-fecha {
    /* width lo da el th */
}

/* Checkbox */
.input-grid-check {
    width: 16px !important;
    height: 16px !important;
    cursor: pointer !important;
    accent-color: #3b82f6 !important;
    margin: 0 auto !important;
    display: block !important;
}

/* Celda HTML (spans) */
.celda-html {
    display: block !important;
    width: 100% !important;
    padding: 8px 10px !important;
    box-sizing: border-box !important;
    overflow: hidden !important;
}

/* Textarea */
.textarea-grid {
    min-height: 38px !important;
    resize: none !important;
    overflow: hidden !important;
    line-height: 1.4 !important;
    border: 1px solid #e2e8f0 !important;
    padding: 8px 10px !important;
}

.textarea-grid:focus {
    border-color: #3b82f6 !important;
    box-shadow: inset 0 0 0 1px #3b82f6 !important;
}

/* Select */
.input-grid.select-grid {
    appearance: auto !important; /* Flecha nativa del navegador */
}

/* ============================================
   BOTÓN ABRIR SUBGRID
   ============================================ */
.btn-abrir-subgrid {
    display: block !important;
    width: auto !important;
    margin: 4px auto !important; /* Centrado */
    align-items: center !important;
    justify-content: center !important;
    font-size: 12px !important;
    font-weight: 600 !important;
    padding: 6px 12px !important;
    border-radius: 4px !important;
    background-color: #ffffff !important;
    color: #3b82f6 !important;
    border: 1px solid #3b82f6 !important;
    cursor: pointer !important;
    transition: all 0.2s ease-in-out !important;
    white-space: nowrap !important;
}

.btn-abrir-subgrid:hover {
    background-color: #3b82f6 !important;
    color: #ffffff !important;
}

/* ============================================
   PESTAÑA TOGGLE (CLIENTES, ETC)
   ============================================ */
.grid-header-toggle {
    background-color: #334155 !important;
    border: 1px solid #1e293b !important;
    border-bottom: none !important;
    padding: 10px 14px !important;
    cursor: pointer !important;
    font-family: 'Segoe UI', sans-serif !important;
    font-size: 14px !important;
    font-weight: 700 !important;
    color: #ffffff !important;
    border-radius: 6px 6px 0 0 !important;
    display: flex !important;
    align-items: center !important;
    user-select: none !important;
}

.grid-header-toggle:hover {
    background-color: #475569 !important;
}

.toggle-flecha {
    margin-right: 8px;
}

/* ============================================
   BARRA DE BÚSQUEDA
   ============================================ */
.barra-busqueda-grid {
    display: flex !important;
    gap: 10px !important;
    align-items: center !important;
    padding: 10px 12px !important;
    background: #f1f5f9 !important;
    border: 1px solid #cbd5e1 !important;
    border-bottom: none !important;
}

.selector-columna-busqueda {
    padding: 6px 10px !important;
    border: 1px solid #cbd5e1 !important;
    border-radius: 4px !important;
    background: #fff !important;
}

.contenedor-busqueda-input {
    position: relative !important;
}

.input-busqueda-grid {
    padding: 6px 30px 6px 10px !important;
    width: 220px !important;
    border: 1px solid #cbd5e1 !important;
    border-radius: 4px !important;
    outline: none !important;
}

.btn-limpiar-busqueda {
    display: none !important;
    position: absolute !important;
    right: 6px !important;
    top: 50% !important;
    transform: translateY(-50%) !important;
    border: none !important;
    background: none !important;
    cursor: pointer !important;
    font-size: 16px !important;
    color: #94a3b8 !important;
}
.btn-limpiar-busqueda.visible { display: block !important; }

.contador-resultados {
    font-size: 12px !important;
    color: #64748b !important;
}

.btn-agregar-fila {
    margin-left: auto !important;
    padding: 6px 14px !important;
    background: #3b82f6 !important;
    color: #fff !important;
    border: none !important;
    border-radius: 4px !important;
    font-size: 13px !important;
    font-weight: 600 !important;
    cursor: pointer !important;
}
.btn-agregar-fila:hover { background: #2563eb !important; }

/* ============================================
   INDICADOR DE CONTEXTO
   ============================================ */
.indicador-fila-activa {
    display: flex !important;
    justify-content: space-between !important;
    padding: 6px 12px !important;
    background: #fffbeb !important;
    border: 1px solid #fde68a !important;
    font-size: 12px !important;
    color: #92400e !important;
}
/* ============================================
   ORDENAMIENTO DE COLUMNAS (FLECHAS)
   ============================================ */
.tabla-grid-puro thead th {
    cursor: pointer; /* Manito al pasar por el título */
    position: relative; /* Para posicionar la flecha */
    padding-right: 20px !important; /* Espacio para la flecha */
}

/* Flecha por defecto (sutil) */
.tabla-grid-puro thead th::after {
    content: '';
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-bottom: 6px solid #94a3b8; /* Gris */
    opacity: 0.4;
}

/* Flecha Ascendente (Arriba) */
.tabla-grid-puro thead th.sort-asc::after {
    border-bottom: 6px solid #ffffff; /* Blanco */
    opacity: 1;
}

/* Flecha Descendente (Abajo) */
.tabla-grid-puro thead th.sort-desc::after {
    border-bottom: none;
    border-top: 6px solid #ffffff; /* Blanco */
    opacity: 1;
}

/* ============================================
   RESIZE DE COLUMNAS (CONTROL DE ARRASTRE)
   ============================================ */
.resize-handle {
    position: absolute;
    top: 0;
    right: 0;
    width: 6px;
    height: 100%;
    cursor: col-resize;
    z-index: 5;
    background: transparent;
}

.resize-handle:hover {
    background: rgba(255, 255, 255, 0.3); /* Línea blanca tenue al pasar */
}

/* Cuando está arrastrando, evita seleccionar texto en toda la página */
body.resizing-column {
    cursor: col-resize !important;
    user-select: none !important;
}
body.resizing-column * {
    cursor: col-resize !important;
}