/* ============================================
   INSTANET CONTRATOS - CSS DE IMPRESIÓN v2.0
   ============================================
   Ruta: wp-content/themes/wellonly/contratos/assets/css/contrato-print.css
   
   REGLAS CRÍTICAS:
   1. NO espacios en blanco bajo ningún contexto
   2. Firma SIEMPRE en la misma hoja que el bloque de datos
   3. Tablas y secciones importantes no se cortan
   ============================================ */

/* === CONFIGURACIÓN DE PÁGINA A4 === */
@page {
    size: A4 portrait;
    margin: 15mm 10mm 20mm 10mm;
}

@page :first {
    margin-top: 25mm;
}

/* === RESET PARA IMPRESIÓN === */
@media print {
    *,
    *::before,
    *::after {
        box-sizing: border-box;
    }

    html, body {
        width: 210mm;
        height: auto;
        margin: 0 !important;
        padding: 0 !important;
        background: white !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
        color-adjust: exact !important;
    }

    body {
        font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
        font-size: 10pt;
        line-height: 1.4;
        color: #000 !important;
    }

    /* === OCULTAR ELEMENTOS NO IMPRIMIBLES === */
    .no-print,
    .instc-topbar,
    .instc-stepper,
    .instc-user-pill,
    .instc-search-container,
    .instc-btn,
    button,
    nav,
    header:not(.header),
    footer:not(#footer-fijo),
    .instc-signature-actions,
    .instc-preview-tools,
    [onclick],
    script,
    style:not([media="print"]) {
        display: none !important;
    }

    /* === CONTENEDOR PRINCIPAL === */
    #wizard-preview,
    .instc-contrato-firma,
    .contract-container {
        width: 100% !important;
        max-width: none !important;
        margin: 0 !important;
        padding: 0 !important;
        border: none !important;
        box-shadow: none !important;
        background: white !important;
    }

    /* === SALTOS DE PÁGINA === */
    
    /* Forzar salto de página */
    .page-break,
    .salto-pagina {
        page-break-before: always !important;
        break-before: page !important;
        display: block !important;
        height: 1px !important;
        clear: both !important;
    }

    /* Evitar saltos dentro de elementos */
    .no-break,
    .firma-section,
    .firmas-bloque,
    .tabla-firmas-completa,
    .anexo2-section,
    .consent-item,
    table,
    tr,
    th,
    td,
    h3,
    h4,
    .block-title,
    .data-table,
    .contract-table {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
    }

    /* === SECCIÓN DE FIRMAS - CRÍTICO === */
    .firma-section,
    .firmas-bloque,
    .instc-firma-section {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
        page-break-before: auto !important;
        margin-top: 20px;
    }

    /* Si la firma + 150px de contenido previo no cabe, mover todo a nueva página */
    .pre-firma-content {
        page-break-after: avoid !important;
        break-after: avoid !important;
    }

    /* Tabla de firmas completa */
    .tabla-firmas-completa {
        page-break-inside: avoid !important;
        break-inside: avoid !important;
        min-height: 120px;
    }

    .tabla-firmas-completa td {
        vertical-align: bottom;
        height: 100px;
        padding: 10px;
    }

    .tabla-firmas-completa img,
    .signature-img,
    .firma-img {
        max-height: 80px !important;
        width: auto !important;
        display: block;
        margin: 0 auto;
    }

    /* === EVITAR LÍNEAS HUÉRFANAS/VIUDAS === */
    p, li, dd {
        orphans: 3;
        widows: 3;
    }

    h1, h2, h3, h4, h5, h6 {
        page-break-after: avoid !important;
        break-after: avoid !important;
        orphans: 3;
        widows: 3;
    }

    /* === TABLAS === */
    table {
        width: 100% !important;
        border-collapse: collapse !important;
        page-break-inside: avoid !important;
    }

    /* Tablas largas que deben dividirse */
    table.allow-break {
        page-break-inside: auto !important;
    }

    table.allow-break tr {
        page-break-inside: avoid !important;
    }

    thead {
        display: table-header-group;
    }

    tfoot {
        display: table-footer-group;
    }

    tr {
        page-break-inside: avoid !important;
    }

    td, th {
        padding: 6px 8px !important;
        font-size: 9pt !important;
        border: 1px solid #333 !important;
    }

    th {
        background: #f0f0f0 !important;
        font-weight: bold !important;
    }

    /* === ENCABEZADOS === */
    .header {
        border-bottom: 2px solid #ff7a1a !important;
        margin-bottom: 15px;
        padding-bottom: 10px;
    }

    .logo {
        max-width: 70px !important;
        height: auto !important;
    }

    h2 {
        font-size: 12pt !important;
        text-align: center;
        margin: 5px 0;
        text-transform: uppercase;
    }

    h3 {
        font-size: 11pt !important;
        border-left: 4px solid #ff7a1a !important;
        padding: 6px 8px;
        margin: 15px 0 8px;
        background: #f5f5f5 !important;
    }

    h4, .block-title {
        font-size: 10pt !important;
        margin: 10px 0 6px;
        border-bottom: 1px dashed #ccc;
        padding-bottom: 4px;
    }

    /* === PÁRRAFOS Y TEXTO === */
    p {
        margin: 5px 0;
        text-align: justify;
        font-size: 9.5pt;
    }

    /* === CHECKBOXES === */
    input[type="checkbox"] {
        -webkit-appearance: none;
        appearance: none;
        width: 12px !important;
        height: 12px !important;
        border: 1px solid #333 !important;
        background: white !important;
        display: inline-block;
        vertical-align: middle;
        margin: 0 4px;
    }

    input[type="checkbox"]:checked {
        background: #333 !important;
        position: relative;
    }

    input[type="checkbox"]:checked::after {
        content: "✓";
        color: white;
        font-size: 10px;
        position: absolute;
        top: -2px;
        left: 1px;
    }

    /* === ANEXO 2 - PROTECCIÓN DE DATOS === */
    .anexo2-section {
        border: 1px solid #22c55e !important;
        padding: 10px !important;
        margin: 15px 0 !important;
        page-break-inside: avoid !important;
    }

    .consent-item {
        padding: 8px !important;
        margin: 5px 0 !important;
        border: 1px solid #ddd !important;
        page-break-inside: avoid !important;
    }

    .metadata-box {
        background: #f5f5f5 !important;
        padding: 8px !important;
        font-size: 8pt !important;
        border: 1px solid #ccc !important;
    }

    /* === COLORES DE ESTADO === */
    .consent-granted {
        color: #000 !important;
        font-weight: bold !important;
    }

    .consent-granted::before {
        content: "✓ ";
    }

    .consent-denied {
        color: #666 !important;
    }

    .consent-denied::before {
        content: "✗ ";
    }

    /* === FECHA Y META === */
    .meta-date {
        background: #fff8e0 !important;
        border: 1px solid #e0c000 !important;
        padding: 6px 10px;
        display: inline-block;
        font-weight: bold;
    }

    /* === FOOTER FIJO EN CADA PÁGINA === */
    #footer-fijo {
        display: block !important;
        position: fixed;
        bottom: 0;
        left: 10mm;
        right: 10mm;
        height: 12mm;
        border-top: 1px solid #ccc;
        font-size: 7pt;
        color: #666;
        background: white;
        padding-top: 3mm;
        text-align: center;
    }

    /* === LINKS === */
    a {
        color: #000 !important;
        text-decoration: none !important;
    }

    a[href]::after {
        content: none !important;
    }

    /* === NOTAS Y ADVERTENCIAS === */
    .note {
        font-size: 8pt !important;
        color: #555 !important;
        font-style: italic;
    }

    /* === SECCIÓN CONSENTIMIENTOS LOPDP === */
    .instc-consentimientos-section {
        border: 1px solid #333 !important;
        padding: 10px !important;
        margin: 15px 0 !important;
        page-break-inside: avoid !important;
    }

    .instc-consentimientos-section h3 {
        border-bottom: 1px solid #333 !important;
        margin-bottom: 10px !important;
        padding-bottom: 8px !important;
    }

    /* === IMÁGENES === */
    img {
        max-width: 100% !important;
        height: auto !important;
        page-break-inside: avoid !important;
    }

    /* === ESPACIADO ENTRE SECCIONES === */
    .content {
        padding-top: 0 !important;
    }

    /* Evitar padding excesivo después de salto de página */
    .page-break + .content,
    .page-break + div {
        padding-top: 10px !important;
        margin-top: 0 !important;
    }

    /* === AJUSTES FINALES PARA EVITAR ESPACIOS EN BLANCO === */
    
    /* Llenar espacio si hay poco contenido antes del final de página */
    .fill-space {
        flex-grow: 1;
    }

    /* Forzar contenido a ocupar espacio disponible */
    .stretch-content {
        display: flex;
        flex-direction: column;
        min-height: 100%;
    }

    /* === DEBUGGING (comentar en producción) === */
    /*
    * {
        outline: 1px solid rgba(255,0,0,0.1);
    }
    */
}

/* === PREVIEW EN PANTALLA (no impresión) === */
@media screen {
    .print-only {
        display: none !important;
    }
}
