/**
 * Mobile Precision Editor Styles
 * Optimized for touch interaction and mobile displays
 */

/* Base canvas setup with touch-action control */
#pdfCanvas,
#textOverlay {
    touch-action: auto; /* FIXED: Allow pinch-zoom and normal touch gestures */
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* Text selection layers */
.text-selection-layer {
    position: absolute;
    border: 2px solid transparent;
    transition: all 0.2s ease;
    cursor: pointer;
    word-wrap: break-word;
    overflow-wrap: break-word;
    padding: 2px;
    box-sizing: border-box;
    pointer-events: auto; /* Enable touch interaction */
    
    /* Expand touch target */
    padding: 10px;
    margin: -10px;
}

.text-selection-layer:active {
    border-color: rgba(102, 126, 234, 0.5);
    background: rgba(102, 126, 234, 0.1);
}

/* Selected state */
.text-selection-layer.mobile-selected {
    border-color: #667eea;
    background: rgba(102, 126, 234, 0.15);
    box-shadow: 0 0 10px rgba(102, 126, 234, 0.5);
    z-index: 100;
}

/* Highlight animation */
.text-selection-layer.mobile-highlight {
    animation: highlightPulse 0.3s ease;
}

@keyframes highlightPulse {
    0%, 100% {
        transform: scale(1);
        box-shadow: 0 0 0 rgba(102, 126, 234, 0);
    }
    50% {
        transform: scale(1.05);
        box-shadow: 0 0 20px rgba(102, 126, 234, 0.8);
    }
}

/* Floating Contextual Toolbar */
.mobile-floating-toolbar {
    position: fixed;
    display: none;
    flex-direction: row;
    gap: 8px;
    background: white;
    border-radius: 12px;
    padding: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
    z-index: 10000;
    opacity: 0;
    transform: translateY(-10px);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.mobile-floating-toolbar.active {
    opacity: 1;
    transform: translateY(0);
}

.mobile-floating-toolbar .toolbar-btn {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 10px 15px;
    border: none;
    background: #f8f9fa;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    color: #333;
    transition: all 0.2s ease;
    white-space: nowrap;
    min-width: 44px; /* iOS minimum tap target */
    min-height: 44px;
    justify-content: center;
}

.mobile-floating-toolbar .toolbar-btn:active {
    background: #667eea;
    color: white;
    transform: scale(0.95);
}

/* Hide text labels on very small screens */
@media (max-width: 360px) {
    .mobile-floating-toolbar .toolbar-btn span {
        display: none;
    }
}

/* Modal Input System */
.mobile-input-modal {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: white;
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.2);
    z-index: 10001;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    max-height: 40vh;
    min-height: 250px;
    display: none;
    flex-direction: column;
}

.mobile-input-modal.active {
    display: flex;
    transform: translateY(0);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #e0e0e0;
}

.modal-title {
    font-size: 18px;
    font-weight: 600;
    color: #333;
}

.modal-close {
    width: 32px;
    height: 32px;
    border: none;
    background: #f0f0f0;
    border-radius: 50%;
    cursor: pointer;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #666;
    transition: all 0.2s ease;
}

.modal-close:active {
    background: #e0e0e0;
    transform: scale(0.95);
}

.modal-body {
    padding: 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 15px;
    overflow-y: auto;
}

#modalTextInput {
    width: 100%;
    padding: 12px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    font-size: 16px;
    font-family: inherit;
    resize: vertical;
    min-height: 100px;
    transition: border-color 0.2s ease;
}

#modalTextInput:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.modal-actions {
    display: flex;
    gap: 10px;
}

.modal-actions button {
    flex: 1;
    padding: 14px;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 48px;
}

.btn-cancel {
    background: #f0f0f0;
    color: #666;
}

.btn-cancel:active {
    background: #e0e0e0;
    transform: scale(0.98);
}

.btn-apply {
    background: #667eea;
    color: white;
}

.btn-apply:active {
    background: #5568d3;
    transform: scale(0.98);
}

/* Color Picker */
.mobile-color-picker {
    position: fixed;
    display: flex;
    gap: 10px;
    padding: 15px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    z-index: 10002;
    animation: popIn 0.2s ease;
}

@keyframes popIn {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.color-btn {
    width: 44px;
    height: 44px;
    border: 3px solid white;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    transition: all 0.2s ease;
}

.color-btn:active {
    transform: scale(0.9);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

/* Font Picker */
.mobile-font-picker {
    position: fixed;
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    z-index: 10002;
    padding: 15px;
    max-width: 90%;
    animation: popIn 0.2s ease;
}

.font-family-section,
.font-size-section {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 10px;
}

.font-btn,
.size-btn {
    padding: 12px 16px;
    border: 2px solid #e0e0e0;
    background: white;
    border-radius: 8px;
    cursor: pointer;
    font-size: 16px;
    text-align: left;
    transition: all 0.2s ease;
    min-height: 44px;
}

.font-btn:active,
.size-btn:active {
    background: #667eea;
    color: white;
    border-color: #667eea;
    transform: scale(0.98);
}

/* Visual Viewport Adjustments */
body.keyboard-visible {
    padding-bottom: env(safe-area-inset-bottom);
}

/* Safe area padding for notched devices */
.mobile-floating-toolbar {
    padding-left: max(10px, env(safe-area-inset-left));
    padding-right: max(10px, env(safe-area-inset-right));
}

.mobile-input-modal {
    padding-bottom: max(0px, env(safe-area-inset-bottom));
}

/* Loading state */
.text-selection-layer.loading {
    opacity: 0.5;
    pointer-events: none;
}

.text-selection-layer.loading::after {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border: 2px solid #667eea;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

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

/* Zoom mode indicator */
.zoom-indicator {
    position: fixed;
    top: 20px;
    right: 20px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 600;
    z-index: 9999;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
}

.zoom-indicator.active {
    opacity: 1;
}

/* Touch feedback ripple */
.text-selection-layer::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(102, 126, 234, 0.3);
    transform: translate(-50%, -50%);
    opacity: 0;
    transition: all 0.3s ease;
    pointer-events: none;
}

.text-selection-layer:active::before {
    width: 100px;
    height: 100px;
    opacity: 1;
    transition: all 0s;
}

/* Performance optimization */
.text-selection-layer,
.mobile-floating-toolbar,
.mobile-input-modal {
    will-change: transform;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

/* Prevent text selection during drag */
body.dragging {
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .text-selection-layer.mobile-selected {
        border-width: 3px;
    }
    
    .mobile-floating-toolbar {
        border: 2px solid #000;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    .text-selection-layer,
    .mobile-floating-toolbar,
    .mobile-input-modal,
    .mobile-color-picker,
    .mobile-font-picker {
        transition: none;
        animation: none;
    }
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    .mobile-floating-toolbar,
    .mobile-input-modal,
    .mobile-color-picker,
    .mobile-font-picker {
        background: #1a1a1a;
        color: #ffffff;
    }
    
    .mobile-floating-toolbar .toolbar-btn {
        background: #2a2a2a;
        color: #ffffff;
    }
    
    .mobile-floating-toolbar .toolbar-btn:active {
        background: #667eea;
    }
    
    .modal-header {
        border-bottom-color: #3a3a3a;
    }
    
    #modalTextInput {
        background: #2a2a2a;
        border-color: #3a3a3a;
        color: #ffffff;
    }
    
    .btn-cancel {
        background: #2a2a2a;
        color: #ffffff;
    }
    
    .font-btn,
    .size-btn {
        background: #2a2a2a;
        border-color: #3a3a3a;
        color: #ffffff;
    }
}

/* Orientation change handling */
@media (orientation: landscape) {
    .mobile-input-modal {
        max-height: 40vh;
    }
}

/* Small phones (< 375px) */
@media (max-width: 374px) {
    .mobile-floating-toolbar {
        gap: 5px;
        padding: 8px;
    }
    
    .mobile-floating-toolbar .toolbar-btn {
        padding: 8px 10px;
        font-size: 12px;
    }
    
    .modal-body {
        padding: 15px;
    }
}

/* Large phones (> 414px) */
@media (min-width: 415px) {
    .mobile-floating-toolbar .toolbar-btn {
        padding: 12px 18px;
        font-size: 15px;
    }
}

/* Tablets (> 768px) */
@media (min-width: 769px) and (max-width: 1024px) {
    .mobile-floating-toolbar {
        gap: 12px;
        padding: 12px;
    }
    
    .mobile-floating-toolbar .toolbar-btn {
        padding: 14px 20px;
        font-size: 16px;
    }
    
    .mobile-input-modal {
        max-width: 600px;
        left: 50%;
        transform: translateX(-50%) translateY(100%);
        border-radius: 20px;
    }
    
    .mobile-input-modal.active {
        transform: translateX(-50%) translateY(0);
        bottom: 20px;
    }
}

/* Accessibility: Focus visible */
.mobile-floating-toolbar .toolbar-btn:focus-visible,
.modal-close:focus-visible,
.btn-cancel:focus-visible,
.btn-apply:focus-visible,
#modalTextInput:focus-visible {
    outline: 3px solid #667eea;
    outline-offset: 2px;
}

/* Hide on desktop */
@media (min-width: 1025px) and (hover: hover) and (pointer: fine) {
    .mobile-floating-toolbar,
    .mobile-input-modal,
    .mobile-color-picker,
    .mobile-font-picker {
        display: none !important;
    }
}

/* Hide modal completely - using direct editing instead */
.mobile-input-modal {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
}

.mobile-input-modal.active {
    display: none !important;
}
