/**
 * LexoSign Mobile Fixes
 * Comprehensive mobile compatibility fixes for iOS Safari, Android Chrome, etc.
 *
 * Fixes:
 * - 100vh issues on mobile browsers with dynamic address bars
 * - Touch drag-drop visual feedback
 * - Virtual keyboard conflicts with fixed elements
 * - Responsive layouts for narrow screens
 *
 * @version 1.0.0
 */

/* ═══════════════════════════════════════════════════════════════════════════
   VIEWPORT HEIGHT FIXES
   iOS Safari and mobile browsers have dynamic address bars that break 100vh
   ═══════════════════════════════════════════════════════════════════════════ */

:root {
    /* Set by touch-support.js - fallback to 100vh */
    --vh: 1vh;
    --app-height: 100vh;

    /* Safe area insets for notched devices */
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
}

/* Use dynamic viewport height where supported */
@supports (height: 100dvh) {
    :root {
        --app-height: 100dvh;
    }
}

/* Fix body/html height on mobile */
@media (max-width: 1024px) {
    html, body {
        min-height: var(--app-height, 100vh);
        /* NOTE: Do NOT set overflow: hidden here - it blocks scrolling on all pages! */
    }

    /* Prevent overscroll bounce on iOS but allow scrolling */
    body {
        overscroll-behavior-y: contain;
        -webkit-overflow-scrolling: touch;
    }

    /* Only lock scroll on specific full-screen app pages */
    body.esign-canvas-page,
    body.pdf-editor-page,
    body.canvas-page {
        height: var(--app-height, 100vh);
        overflow: hidden;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   ESIGN CANVAS MOBILE FIXES
   ═══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 1024px) {
    /* Fix esign-canvas-page using 100vh */
    body.esign-canvas-page {
        height: var(--app-height, 100vh);
        min-height: -webkit-fill-available;
    }

    /* Fix esign layout height */
    .esign-canvas-layout {
        height: calc(var(--app-height, 100vh) - var(--header-height, 56px));
        min-height: calc(var(--app-height, 100vh) - var(--header-height, 56px));
    }

    /* Fix document viewport */
    .document-viewport {
        height: 100%;
        min-height: 0;
        max-height: none;
    }

    /* Fix PDF container */
    .pdf-container {
        height: 100%;
        min-height: 0;
        padding: 16px;
        -webkit-overflow-scrolling: touch;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   PDF EDITOR MOBILE FIXES
   ═══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 1024px) {
    /* Fix pdf-editor full height */
    .pdf-editor {
        height: var(--app-height, 100vh);
        min-height: -webkit-fill-available;
    }

    /* Fix main layout */
    .pdf-editor__main,
    .main-layout {
        height: calc(var(--app-height, 100vh) - 112px); /* header + toolbar */
        min-height: 0;
    }

    /* Fix viewer area */
    .pdf-editor__viewer,
    .viewer-area {
        height: 100%;
        min-height: 0;
        padding: 1rem;
        -webkit-overflow-scrolling: touch;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   TOUCH DRAG-DROP VISUAL FEEDBACK
   ═══════════════════════════════════════════════════════════════════════════ */

/* Pressing state (before drag starts) */
.touch-pressing {
    transform: scale(0.98);
    opacity: 0.9;
    transition: transform 0.1s ease, opacity 0.1s ease;
}

/* Active dragging state */
.touch-dragging {
    opacity: 0.5 !important;
    transform: scale(0.95);
}

/* Body state during drag */
body.touch-drag-active {
    cursor: grabbing !important;
    user-select: none;
    -webkit-user-select: none;
}

body.touch-drag-active * {
    cursor: grabbing !important;
}

/* Drop target hover state */
.touch-drag-over {
    background: rgba(59, 130, 246, 0.15) !important;
    border-color: #3b82f6 !important;
    box-shadow: inset 0 0 0 2px #3b82f6 !important;
}

/* Drag clone styling */
.touch-drag-clone {
    background: linear-gradient(135deg, #3b82f6, #1d4ed8);
    border-radius: 8px;
    padding: 10px 16px;
    box-shadow:
        0 10px 40px rgba(59, 130, 246, 0.4),
        0 4px 12px rgba(0, 0, 0, 0.3);
    animation: dragCloneAppear 0.15s ease-out;
}

.touch-drag-clone-inner {
    display: flex;
    align-items: center;
    gap: 8px;
    color: white;
}

.touch-drag-clone-icon {
    font-size: 16px;
}

.touch-drag-clone-label {
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
}

@keyframes dragCloneAppear {
    from {
        transform: translate(-50%, -50%) scale(0.8);
        opacity: 0;
    }
    to {
        transform: translate(-50%, -50%) scale(1.1);
        opacity: 1;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   VIRTUAL KEYBOARD FIXES
   Prevent fixed elements from being pushed up on iOS
   ═══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 768px) {
    /* When keyboard is open (detected via visualViewport API in JS) */
    body.keyboard-open .esign-canvas-header,
    body.keyboard-open .pdf-editor__header {
        position: absolute;
    }

    body.keyboard-open .signing-sidebar,
    body.keyboard-open .advanced-panel {
        position: absolute;
        max-height: 50vh;
    }

    body.keyboard-open .zoom-controls,
    body.keyboard-open .floating-controls {
        display: none;
    }

    /* Ensure inputs are visible when focused */
    input:focus,
    textarea:focus,
    [contenteditable]:focus {
        scroll-margin-bottom: 100px;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   RESPONSIVE WIDTH FIXES
   Replace hardcoded pixel widths with responsive values
   ═══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 768px) {
    /* Sidebar widths */
    .signing-sidebar,
    .thumbnail-sidebar {
        width: 100% !important;
        max-width: 300px;
    }

    /* Modal widths */
    .modal-who-sign,
    .modal-add-receiver,
    .modal-signature,
    .modal-signature-v2 {
        width: calc(100% - 32px) !important;
        max-width: 100% !important;
        margin: 16px !important;
    }

    /* Tool cards grid */
    .tools-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }

    /* Placed field minimum size for touch targets */
    .placed-field {
        min-width: 100px;
        min-height: 44px; /* iOS minimum touch target */
    }

    /* Larger touch targets for buttons */
    .placed-field-delete,
    .placed-field-resize {
        width: 32px;
        height: 32px;
        opacity: 1; /* Always visible on touch */
    }
}

@media (max-width: 480px) {
    /* Single column for very narrow screens */
    .tools-grid {
        grid-template-columns: 1fr;
    }

    /* Smaller paddings */
    .pdf-container {
        padding: 8px;
    }

    /* Stack receiver inputs */
    .receiver-row,
    .receiver-row-v2 {
        flex-direction: column;
        gap: 8px;
    }

    .receiver-input,
    .receiver-input-v2 {
        width: 100%;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   TOUCH-FRIENDLY INTERACTIVE ELEMENTS
   ═══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 1024px) {
    /* Larger click targets */
    .tool-item,
    .tool-card,
    .ribbon-btn,
    .type-btn,
    button {
        min-height: 44px;
        min-width: 44px;
    }

    /* Remove hover states that interfere with touch */
    .tool-card:hover,
    .tool-item:hover,
    .draggable-field:hover {
        transform: none;
    }

    /* Active states instead of hover */
    .tool-card:active,
    .tool-item:active,
    .draggable-field:active {
        transform: scale(0.98);
        opacity: 0.9;
    }

    /* Prevent text selection during drag */
    .draggable-field,
    .draggable-field-v2 {
        user-select: none;
        -webkit-user-select: none;
        -webkit-touch-callout: none;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   SAFE AREA PADDING FOR NOTCHED DEVICES
   ═══════════════════════════════════════════════════════════════════════════ */

@supports (padding-top: env(safe-area-inset-top)) {
    .esign-canvas-header,
    .pdf-editor__header,
    .header {
        padding-top: max(env(safe-area-inset-top), 0px);
    }

    .sidebar-footer,
    .modal-footer,
    .zoom-controls,
    .floating-controls {
        padding-bottom: max(env(safe-area-inset-bottom), 16px);
    }

    .signing-sidebar,
    .advanced-panel {
        padding-right: max(env(safe-area-inset-right), 0px);
    }

    .thumbnail-sidebar {
        padding-left: max(env(safe-area-inset-left), 0px);
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   UPLOAD DROP ZONE MOBILE IMPROVEMENTS
   ═══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 768px) {
    .upload-drop-zone,
    #upload-drop-zone,
    .upload-area {
        min-height: 200px;
        padding: 24px;
    }

    .upload-drop-zone .upload-icon,
    .upload-placeholder svg {
        width: 48px;
        height: 48px;
    }

    /* Make file input easier to tap */
    input[type="file"] {
        min-height: 44px;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   SCROLL BEHAVIOR IMPROVEMENTS
   ═══════════════════════════════════════════════════════════════════════════ */

@media (max-width: 1024px) {
    /* Smooth scrolling for containers */
    .pdf-container,
    .document-viewport,
    .sidebar-scroll-content,
    .pdf-editor__panel-content {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }

    /* Momentum scrolling */
    .thumbnail-list {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    /* Hide scrollbars on mobile but keep scrollability */
    .thumbnail-list::-webkit-scrollbar,
    .sidebar-scroll-content::-webkit-scrollbar {
        width: 0;
        height: 0;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   ORIENTATION-SPECIFIC FIXES
   ═══════════════════════════════════════════════════════════════════════════ */

/* Landscape on phones */
@media (max-height: 500px) and (orientation: landscape) {
    .esign-canvas-header,
    .pdf-editor__header {
        height: 44px;
        padding: 0 12px;
    }

    .esign-canvas-layout {
        height: calc(var(--app-height, 100vh) - 44px);
    }

    .pdf-container {
        padding: 8px;
    }

    /* Hide less important elements */
    .compliance-badge,
    .onboarding-tooltip {
        display: none;
    }

    /* Smaller modals */
    .modal-content {
        max-height: 90vh;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   PRINT STYLES
   ═══════════════════════════════════════════════════════════════════════════ */

@media print {
    /* Reset mobile-specific styles for printing */
    body {
        height: auto;
        overflow: visible;
    }

    .touch-drag-clone,
    .mobile-sidebar-toggle {
        display: none !important;
    }
}


/* ═══════════════════════════════════════════════════════════════════════════
   REDUCED MOTION PREFERENCES
   ═══════════════════════════════════════════════════════════════════════════ */

@media (prefers-reduced-motion: reduce) {
    .touch-pressing,
    .touch-dragging,
    .touch-drag-clone {
        transition: none;
        animation: none;
    }
}
