/**
 * EDVC Mapper - Touch Device Optimizations
 * Handles mobile/tablet specific interactions
 */

/* ==========================================================================
   Touch Device Detection
   Use: @media (hover: none) or @media (pointer: coarse)
   ========================================================================== */

/* Touch-optimized Card Edit Buttons */
@media (hover: none) and (pointer: coarse) {
    
    /* 1. CARD EDIT BUTTON - Always visible on touch devices */
    .edvc-edit-trigger {
        opacity: 1 !important; /* Always visible, not just on hover */
    }
    
    /* Larger touch target for edit button */
    .edvc-edit-trigger-btn {
        width: 44px;  /* Minimum 44px for good touch target */
        height: 44px;
        padding: 0.75rem;
    }
    
    .edvc-edit-trigger-btn img {
        width: 24px;
        height: 24px;
    }
    
    /* 2. MAPPING TOOLS - Toggle behavior instead of hover */
    .edvc-mapping-tools {
        /* Don't show on hover anymore */
        pointer-events: none;
        opacity: 0;
    }
    
    /* Show mapping tools when active class is added */
    .edvc-mapping-tools.active {
        opacity: 1 !important;
        pointer-events: all !important;
    }
    
    /* Prevent hover effects on touch devices */
    .edvc-edit-trigger:hover + .edvc-mapping-tools {
        opacity: 0;
        pointer-events: none;
    }
    
    /* Larger touch targets for tool buttons */
    .edvc-tool-btn {
        width: 44px;
        height: 44px;
        padding: 0.75rem;
    }
    
    .edvc-tool-btn img {
        width: 24px;
        height: 24px;
    }
    
    /* 3. PHASE MENU BUTTON - Always visible on touch devices */
    .edvc-phase-menu {
        opacity: 1 !important; /* Always visible */
    }
    
    /* Larger touch target for phase menu */
    .edvc-phase-tab {
        min-height: 44px; /* Good touch target size */
    }
    
    .edvc-phase-menu {
        padding: 8px;
        font-size: 18px;
        margin: 0;
    }
    
    /* Phase dropdown positioning adjustment for touch */
    .edvc-phase-dropdown {
        margin-top: 8px; /* More spacing for finger taps */
    }
    
    .edvc-dropdown-item {
        min-height: 44px; /* Good touch target */
        padding: 12px;
    }
    
    .edvc-dropdown-item img {
        width: 24px;
        height: 24px;
    }
    
    /* 4. CARD INTERACTIONS - Prevent hover states */
    .edvc-card:hover {
        transform: none; /* No transform on touch */
    }
    
    /* Remove hover effects that don't work on touch */
    .edvc-card:hover .edvc-edit-trigger {
        /* Already visible, no change needed */
    }
    
    /* 5. RATING BUTTONS - Larger touch targets */
    .edvc-rating-btn {
        padding: 12px;
        min-width: 44px;
        min-height: 44px;
    }
    
    .edvc-rating-btn img {
        width: 48px;
        height: 48px;
    }
    
    /* 6. TOOLTIPS - Don't show on touch (can't hover) */
    .edvc-tooltip {
        display: none !important;
    }
    
    /* 7. CANVAS ZOOM CONTROLS - Better touch targets */
    .edvc-center-btn {
        width: 44px;
        height: 44px;
    }
    
    /* 8. MENU ITEMS - Larger touch areas */
    .edvc-menu-item {
        padding: 1.25rem 2rem;
        min-height: 60px;
    }
    
    /* 9. PHASE TAB INTERACTIONS */
    .edvc-phase-tab {
        padding: 0.75rem 1rem;
        touch-action: manipulation; /* Prevent double-tap zoom */
    }
    
    /* 10. BUTTONS - All buttons get better touch targets */
    .edvc-btn-primary,
    .edvc-btn-secondary,
    .edvc-btn-warning,
    .edvc-btn-add {
        min-height: 44px;
        padding: 12px 24px;
        touch-action: manipulation;
    }
}

/* ==========================================================================
   Tablet-specific (larger touch devices)
   ========================================================================== */

@media (hover: none) and (pointer: coarse) and (min-width: 768px) {
    
    /* Tablets can have slightly smaller touch targets */
    .edvc-edit-trigger-btn,
    .edvc-tool-btn {
        width: 40px;
        height: 40px;
    }
    
    .edvc-phase-tab {
        min-height: 40px;
    }
    
    .edvc-dropdown-item {
        min-height: 40px;
        padding: 10px;
    }
}

/* ==========================================================================
   Phone-specific (smaller touch devices)
   ========================================================================== */

@media (hover: none) and (pointer: coarse) and (max-width: 767px) {
    
    /* Phones need even larger touch targets */
    .edvc-edit-trigger-btn,
    .edvc-tool-btn {
        width: 48px;
        height: 48px;
        padding: 1rem;
    }
    
    .edvc-tool-btn img {
        width: 28px;
        height: 28px;
    }
    
    /* Phase tabs take full width on phones */
    .edvc-phase-tab {
        min-width: 120px;
        padding: 1rem;
    }
    
    /* Mapping tools container adjustment */
    .edvc-tools-container {
        transform: translateX(60px); /* Less offset on small screens */
    }
}

/* ==========================================================================
   Active States for Touch
   ========================================================================== */

@media (hover: none) and (pointer: coarse) {
    
    /* Visual feedback for touch */
    .edvc-edit-trigger-btn:active,
    .edvc-tool-btn:active,
    .edvc-phase-menu:active,
    .edvc-menu-item:active,
    button:active {
        transform: scale(0.95);
        transition: transform 0.1s ease;
    }
    
    /* Active state for cards */
    .edvc-card:active {
        opacity: 0.9;
    }
    
    /* Highlight active mapping tools */
    .edvc-mapping-tools.active .edvc-tools-container {
        box-shadow: 0 4px 16px rgba(0, 124, 186, 0.3);
        border: 2px solid var(--edvc-highlight);
    }
}

/* ==========================================================================
   Prevent Unwanted Behaviors on Touch
   ========================================================================== */

@media (hover: none) and (pointer: coarse) {
    
    /* Prevent text selection during interactions */
    .edvc-card,
    .edvc-phase-tab,
    .edvc-edit-trigger-btn,
    .edvc-tool-btn {
        -webkit-user-select: none;
        user-select: none;
        -webkit-tap-highlight-color: transparent;
    }
    
    /* Prevent pull-to-refresh on canvas */
    .edvc-canvas-container {
        overscroll-behavior: contain;
        touch-action: pan-x pan-y;
    }
    
    /* Prevent zoom on double-tap for buttons */
    button,
    .edvc-card,
    .edvc-phase-tab {
        touch-action: manipulation;
    }
}

/* ==========================================================================
   Accessibility - High Contrast Mode on Touch
   ========================================================================== */

@media (hover: none) and (pointer: coarse) and (prefers-contrast: high) {
    
    .edvc-edit-trigger-btn,
    .edvc-tool-btn,
    .edvc-phase-menu {
        border: 2px solid currentColor;
    }
    
    .edvc-mapping-tools.active .edvc-tools-container {
        border: 3px solid var(--edvc-highlight);
    }
}

/* ==========================================================================
   Reduced Motion on Touch
   ========================================================================== */

@media (hover: none) and (pointer: coarse) and (prefers-reduced-motion: reduce) {
    
    .edvc-edit-trigger-btn:active,
    .edvc-tool-btn:active,
    button:active {
        transform: none;
        transition: none;
    }
}

/* ==========================================================================
   Pinch Zoom Enhancements
   ========================================================================== */

/* Canvas container must allow touch manipulation */
.edvc-canvas-container {
    touch-action: none; /* Required for pinch zoom */
    overflow: hidden;
    position: relative;
}

/* Canvas smooth transform transitions */
.edvc-canvas {
    transform-origin: 0 0;
    will-change: transform; /* Performance optimization */
    /* Note: transition is removed during pinch for smoothness */
}

/* Smooth transition when not actively pinching */
.edvc-canvas:not(.pinching) {
    transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* Card state transitions for zoom */
.edvc-card {
    /* Keep existing transition but ensure it includes height */
    transition: height 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94), 
                min-height 0.5s cubic-bezier(0.25, 0.46, 0.45, 0.94),
                opacity 0.3s ease !important;
}

/* Collapsed state (zoom < 0.8) */
.edvc-card.collapsed {
    height: 140px !important;
    min-height: 140px !important;
}

.edvc-card.collapsed .edvc-card-summary {
    opacity: 0 !important;
    transform: translateY(-20px) !important;
    pointer-events: none !important;
    height: 0 !important;
    overflow: hidden !important;
}

/* Expanded state (zoom >= 0.8) */
.edvc-card.expanded {
    height: 220px !important;
    min-height: 220px !important;
}

.edvc-card.expanded .edvc-card-summary {
    opacity: 1 !important;
    transform: translateY(0) !important;
    pointer-events: auto !important;
    height: auto !important;
    transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1) 0.1s, 
                transform 0.3s cubic-bezier(0.4, 0, 0.2, 1) 0.1s !important;
}

/* Prevent layout shift during zoom */
.edvc-card-row {
    min-height: 200px;
    position: relative;
}

/* ==========================================================================
   Touch-Specific Zoom Indicators
   ========================================================================== */

/* Optional: Add a zoom level indicator */
.edvc-zoom-indicator {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.7);
    color: white;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 12px;
    z-index: 1000;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.edvc-zoom-indicator.visible {
    opacity: 1;
}

/* ==========================================================================
   Performance Optimizations for Touch Zoom
   ========================================================================== */

/* GPU acceleration for canvas */
.edvc-canvas {
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
}

/* Optimize card rendering during zoom */
@media (hover: none) and (pointer: coarse) {
    .edvc-card {
        backface-visibility: hidden;
        -webkit-backface-visibility: hidden;
        transform: translateZ(0);
        -webkit-transform: translateZ(0);
    }
}

/* ==========================================================================
   Mobile-Specific Zoom States
   ========================================================================== */

@media (max-width: 768px) {
    /* Adjust collapsed height for mobile */
    .edvc-card.collapsed {
        height: 120px !important;
        min-height: 120px !important;
    }
    
    /* Adjust expanded height for mobile */
    .edvc-card.expanded {
        height: 200px !important;
        min-height: 200px !important;
    }
}

@media (max-width: 480px) {
    /* Further adjustments for small phones */
    .edvc-card.collapsed {
        height: 100px !important;
        min-height: 100px !important;
    }
    
    .edvc-card.expanded {
        height: 180px !important;
        min-height: 180px !important;
    }
}

/* ==========================================================================
   Accessibility
   ========================================================================== */

/* Respect reduced motion preference */
@media (prefers-reduced-motion: reduce) {
    .edvc-canvas,
    .edvc-card,
    .edvc-card-summary {
        transition: none !important;
    }
}

/* ==========================================================================
   Debug Mode (optional)
   ========================================================================== */

/* Add class 'debug-zoom' to body to see zoom information */
body.debug-zoom .edvc-canvas::before {
    content: 'Scale: ' attr(data-scale);
    position: fixed;
    top: 10px;
    right: 10px;
    background: rgba(255, 0, 0, 0.8);
    color: white;
    padding: 5px 10px;
    border-radius: 4px;
    font-size: 12px;
    z-index: 10000;
    font-family: monospace;
}

body.debug-zoom .edvc-card.collapsed::after {
    content: 'COLLAPSED';
    position: absolute;
    top: 5px;
    right: 5px;
    background: rgba(255, 165, 0, 0.8);
    color: white;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 10px;
    font-weight: bold;
}

body.debug-zoom .edvc-card.expanded::after {
    content: 'EXPANDED';
    position: absolute;
    top: 5px;
    right: 5px;
    background: rgba(0, 128, 0, 0.8);
    color: white;
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 10px;
    font-weight: bold;
}