/**
 * Modal de perspective crop — paleta clara, header/footer fijos, body scrolleable.
 * Sin dependencias externas. SVG + Canvas nativo.
 *
 * El color del header y de los botones principales lo personaliza
 * wp-creador-fondos-negros.php con los CSS vars del admin.
 */

.cfn-pc-modal {
    /* visibility en vez de display:none → el modal SIEMPRE tiene layout,
       getBoundingClientRect funciona desde el primer momento. */
    visibility: hidden;
    opacity: 0;
    pointer-events: none;
    display: flex;
    position: fixed;
    inset: 0;
    z-index: 999999;
    align-items: center;
    justify-content: center;
    /* Padding para que el dialog no toque los bordes de la ventana.
       env(safe-area-inset-*) respeta notch/barras dinámicas en iOS. */
    padding: 24px 16px;
    padding-top: max(24px, env(safe-area-inset-top));
    padding-bottom: max(24px, env(safe-area-inset-bottom));
    box-sizing: border-box;
    transition: opacity 0.15s ease-out;
}

.cfn-pc-modal.is-open {
    visibility: visible;
    opacity: 1;
    pointer-events: auto;
}

.cfn-pc-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
}

.cfn-pc-dialog {
    position: relative;
    background: #ffffff;
    color: #2c3e50;
    border-radius: 10px;
    box-shadow: 0 30px 80px rgba(0, 0, 0, 0.4);
    width: min(100%, 1000px);
    max-height: 100%; /* padding del modal define el margen exterior */
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.cfn-pc-header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 18px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #fff;
}

.cfn-pc-header h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: #fff;
}

.cfn-pc-close {
    background: transparent;
    border: 0;
    color: rgba(255, 255, 255, 0.85);
    font-size: 24px;
    line-height: 1;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}

.cfn-pc-close:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
}

/* Body = solo el stage + hint. Scrolleable si la altura aprieta. */
.cfn-pc-body {
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding: 12px;
    gap: 10px;
    overflow-y: auto;
    background: #f5f6fa;
}

.cfn-pc-hint {
    flex: 0 0 auto;
    margin: 0;
    font-size: 13px;
    color: #5a6e85;
    text-align: center;
    line-height: 1.4;
}

.cfn-pc-stage {
    position: relative;
    flex: 1 1 auto;
    /* Stage oscuro: hace que los handles brillantes y la imagen contrasten bien. */
    min-height: 260px;
    background: #1a1d28;
    border-radius: 8px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    touch-action: none;
    user-select: none;
}

.cfn-pc-canvas {
    display: block;
    max-width: 100%;
    max-height: 100%;
}

.cfn-pc-svg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    /* El SVG sí recibe eventos; los hijos individuales deciden cuáles. */
    pointer-events: auto;
}

.cfn-pc-poly {
    fill: rgba(102, 126, 234, 0.18);
    stroke: #8aa6ff;
    stroke-width: 2;
    stroke-linejoin: round;
    pointer-events: none;
}

.cfn-pc-handle {
    fill: #fff;
    stroke: #4a6cff;
    stroke-width: 3;
    cursor: grab;
    pointer-events: all;
    transition: fill 0.1s, stroke 0.1s;
}

.cfn-pc-handle:hover {
    fill: #ffe888;
    stroke: #ffae00;
}

.cfn-pc-handle.is-dragging {
    cursor: grabbing;
    fill: #ffe888;
    stroke: #ffae00;
}

/* Círculo transparente más grande para hit area en touch */
.cfn-pc-handle-hit {
    fill: rgba(255, 255, 255, 0.001); /* casi transparente pero pintado → captura events */
    cursor: grab;
    pointer-events: all;
}

.cfn-pc-handle-label {
    fill: rgba(255, 255, 255, 0.95);
    font-size: 11px;
    font-weight: 700;
    text-anchor: middle;
    pointer-events: none;
    user-select: none;
}

/* Lupa real: canvas DOM con zoom de la imagen alrededor del handle activo.
   Se posiciona absolutamente dentro del stage. JS controla left/top/display. */
.cfn-pc-magnifier {
    position: absolute;
    display: none;
    pointer-events: none;
    border-radius: 50%;
    border: 3px solid #4a6cff;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.5);
    background: #fff;
    z-index: 5;
}

/* Footer = controles fijos siempre visibles */
.cfn-pc-footer {
    flex: 0 0 auto;
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: center;
    padding: 12px 14px;
    background: #ffffff;
    border-top: 1px solid #e1e5ec;
}

.cfn-pc-rotation {
    flex: 1 1 280px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 200px;
}

.cfn-pc-rotation-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.cfn-pc-rotation-label {
    font-size: 13px;
    color: #5a6e85;
    font-weight: 500;
}

.cfn-pc-rotation-val {
    font-variant-numeric: tabular-nums;
    color: #2c3e50;
    font-weight: 600;
}

.cfn-pc-rotation input[type="range"] {
    width: 100%;
    accent-color: #667eea;
    margin: 0;
}

.cfn-pc-rotation-quick {
    display: flex;
    gap: 4px;
    justify-content: center;
    flex-wrap: wrap;
}


.cfn-pc-btn-quick {
    background: #ffffff;
    border: 1px solid #d0d7e2;
    color: #2c3e50;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 12px;
    cursor: pointer;
    font-weight: 500;
    transition: background 0.15s, border-color 0.15s;
}

.cfn-pc-btn-quick:hover {
    background: #f5f6fa;
    border-color: #a8b3c5;
}

.cfn-pc-actions {
    display: flex;
    gap: 8px;
    margin-left: auto;
}

.cfn-pc-btn {
    padding: 10px 16px;
    border-radius: 6px;
    border: 0;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, opacity 0.15s, transform 0.05s;
}

.cfn-pc-btn:active {
    transform: scale(0.97);
}

.cfn-pc-btn-secondary {
    background: #e1e5ec;
    color: #2c3e50;
}

.cfn-pc-btn-secondary:hover {
    background: #d0d7e2;
}

.cfn-pc-btn-primary {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #fff;
    box-shadow: 0 2px 8px rgba(102, 126, 234, 0.35);
}

.cfn-pc-btn-primary:hover {
    opacity: 0.92;
}

.cfn-pc-btn-primary:disabled {
    background: #999;
    cursor: not-allowed;
    box-shadow: none;
    opacity: 0.6;
}

.cfn-pc-busy {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: none;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: #fff;
    gap: 14px;
    z-index: 10;
}

.cfn-pc-busy.is-active {
    display: flex;
}

.cfn-pc-busy-spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.18);
    border-top-color: #fff;
    border-radius: 50%;
    animation: cfn-pc-spin 0.8s linear infinite;
}

@keyframes cfn-pc-spin {
    to {
        transform: rotate(360deg);
    }
}

/* Botón "Ajustar perspectiva" en las cards de preview */
.cfn-pc-trigger {
    position: absolute;
    top: 10px;
    right: 56px;
    background: rgba(255, 255, 255, 0.95);
    border: 0;
    border-radius: 50%;
    width: 36px;
    height: 36px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #2c3e50;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.25);
    transition: background 0.15s, transform 0.1s;
    z-index: 5;
}

.cfn-pc-trigger:hover {
    background: #fff;
    transform: scale(1.08);
}

.cfn-pc-trigger svg {
    width: 18px;
    height: 18px;
}

/* Móvil portrait — compactamos para que TODO quepa con margen visible.
   El padding del .cfn-pc-modal ya garantiza margen exterior. El dialog
   ocupa el espacio restante con border-radius mantenido para verse "elevado". */
@media (max-width: 640px) {
    .cfn-pc-modal {
        padding: 12px;
        padding-top: max(12px, env(safe-area-inset-top));
        padding-bottom: max(12px, env(safe-area-inset-bottom));
    }

    .cfn-pc-dialog {
        width: 100%;
        max-width: 100%;
        max-height: 100%;
        height: 100%;
        border-radius: 8px;
    }

    .cfn-pc-header {
        padding: 10px 14px;
    }

    .cfn-pc-body {
        padding: 8px;
        gap: 6px;
    }

    .cfn-pc-hint {
        font-size: 12px;
    }

    .cfn-pc-stage {
        min-height: 200px;
    }

    .cfn-pc-footer {
        padding: 10px;
        gap: 8px;
        flex-direction: column;
        align-items: stretch;
    }

    .cfn-pc-actions {
        margin-left: 0;
        display: grid;
        grid-template-columns: 1fr 1fr 1fr;
        gap: 6px;
    }

    .cfn-pc-btn {
        padding: 11px 8px;
        font-size: 13px;
    }

    .cfn-pc-rotation-quick {
        justify-content: space-between;
    }
}
