/**
 * Mobile Enhancements for PDF Editor
 * Optimized touch interactions and responsive layouts
 */

/* ===== MOBILE MENU TOGGLES ===== */
.mobile-menu-toggles {
  display: none;
}

.mobile-toggle-btn {
  position: fixed;
  z-index: 1001;
  background: var(--accent);
  color: white;
  border: none;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  cursor: pointer;
  transition: all 0.3s ease;
  -webkit-tap-highlight-color: transparent;
}

.mobile-toggle-btn:active {
  transform: scale(0.95);
}

#mobile-sidebar-toggle {
  bottom: 80px;
  left: 16px;
}

#mobile-properties-toggle {
  bottom: 80px;
  right: 16px;
}

@media (max-width: 1024px) {
  .mobile-menu-toggles {
    display: block;
  }

  .sidebar-left,
  .sidebar-right {
    position: fixed;
    top: 60px;
    bottom: 0;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    background: var(--sidebar);
  }

  .sidebar-left {
    left: 0;
    width: 280px;
  }

  .sidebar-right {
    right: 0;
    width: 320px;
    transform: translateX(100%);
  }

  .sidebar-left.mobile-visible {
    display: flex;
    transform: translateX(0);
    box-shadow: 2px 0 10px rgba(0,0,0,0.3);
  }

  .sidebar-right.mobile-visible {
    display: flex;
    transform: translateX(0);
    box-shadow: -2px 0 10px rgba(0,0,0,0.3);
  }
}

@media (max-width: 600px) {
  .mobile-toggle-btn {
    width: 44px;
    height: 44px;
  }

  #mobile-sidebar-toggle {
    bottom: 70px;
    left: 12px;
  }

  #mobile-properties-toggle {
    bottom: 70px;
    right: 12px;
  }

  .sidebar-left {
    width: 90vw;
    max-width: 280px;
  }

  .sidebar-right {
    width: 90vw;
    max-width: 320px;
  }
}

/* ===== GLOBAL MOBILE IMPROVEMENTS ===== */

/* Prevent iOS zoom on input focus */
@media (max-width: 1024px) {
    input,
    select,
    textarea,
    button {
        font-size: 16px !important;
    }
}

/* Better touch targets */
@media (max-width: 768px) {
    button,
    .btn,
    .btn-icon,
    a {
        min-height: 44px;
        min-width: 44px;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    /* Improve scrolling on mobile */
    .sidebar-left,
    .sidebar-right,
    .canvas-area,
    .toolbar {
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
    }

    /* Modal improvements for mobile */
    .modal-content {
        width: 95vw;
        max-width: 500px;
        max-height: 90vh;
    }

    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 12px;
    }

    .modal-header h2 {
        font-size: 16px;
    }

    /* Better canvas controls on mobile */
    .page-controls {
        padding: 8px;
        gap: 8px;
        flex-wrap: wrap;
        justify-content: center;
    }

    .zoom-controls {
        gap: 4px;
    }

    .zoom-controls button {
        min-width: 36px;
        min-height: 36px;
        padding: 6px;
    }

    /* Stack page navigation vertically on small screens */
    .page-nav {
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
    }

    /* Better input fields on mobile */
    input[type="number"],
    input[type="text"],
    input[type="range"],
    select {
        height: 44px;
        padding: 8px 12px;
    }

    /* Adjust grid layouts for mobile */
    .action-buttons-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 8px;
    }

    /* Tool buttons in sidebar */
    .tool-btn-wide {
        padding: 12px;
        min-height: 48px;
    }
}

/* ===== EXTRA SMALL SCREENS ===== */
@media (max-width: 480px) {
    .app-header {
        padding: 6px 8px;
    }

    .brand {
        font-size: 14px;
    }

    .brand-icon {
        font-size: 18px;
    }

    /* Single column layout for buttons */
    .action-buttons-grid {
        grid-template-columns: 1fr;
    }

    /* Compact toolbar buttons */
    .toolbar-group {
        gap: 2px;
    }

    .btn-icon {
        min-width: 32px;
        min-height: 32px;
        padding: 4px;
    }

    .btn-icon .material-icons {
        font-size: 18px;
    }

    /* Hide non-essential elements on very small screens */
    .toolbar .divider {
        display: none;
    }

    /* Compact modal on small screens */
    .modal-content {
        width: 98vw;
        max-height: 95vh;
    }

    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 8px;
    }

    /* Smaller fonts for compact display */
    .zoom-value {
        font-size: 12px;
        padding: 4px 8px;
    }

    .page-nav {
        font-size: 12px;
    }

    /* Better spacing for touch on small screens */
    .tool-btn-wide {
        padding: 10px;
        min-height: 44px;
    }
}

/* ===== LANDSCAPE ORIENTATION ===== */
@media (max-width: 768px) and (orientation: landscape) {
    .app-header {
        padding: 6px 12px;
    }

    .canvas-area {
        padding: 4px;
    }

    /* Optimize vertical space in landscape */
    .modal-content {
        max-height: 85vh;
    }

    .page-controls {
        padding: 6px;
    }
}

/* ===== TOUCH GESTURE IMPROVEMENTS ===== */
@media (hover: none) and (pointer: coarse) {
    /* This targets touch devices specifically */
    
    /* Increase hit areas for better touch accuracy */
    button,
    .btn,
    a {
        padding: 12px;
    }

    /* Remove hover effects on touch devices */
    button:hover,
    .btn:hover,
    .btn-icon:hover {
        transform: none;
    }

    /* Add active states for touch feedback */
    button:active,
    .btn:active,
    .btn-icon:active {
        opacity: 0.7;
        transform: scale(0.98);
    }

    /* Improve canvas interactions */
    canvas {
        touch-action: none;
    }

    /* Better scrollable areas */
    .sidebar-left,
    .sidebar-right,
    .toolbar {
        overflow-x: auto;
        overflow-y: auto;
    }
}

/* ===== ACCESSIBILITY IMPROVEMENTS ===== */
@media (max-width: 768px) {
    /* Ensure focus is visible on mobile */
    button:focus-visible,
    input:focus-visible,
    select:focus-visible {
        outline: 2px solid var(--accent);
        outline-offset: 2px;
    }

    /* Better contrast for readability */
    .text-muted,
    .btn-label,
    small {
        color: var(--text);
        opacity: 0.8;
    }
}

/* ===== CANVAS SPECIFIC MOBILE IMPROVEMENTS ===== */
@media (max-width: 768px) {
    #canvas-area,
    .canvas-area {
        max-width: 100%;
        overflow: auto;
        -webkit-overflow-scrolling: touch;
        height: 100%;
        min-height: calc(100vh - 70px);
        background: var(--bg-secondary);
    }

    #canvas-wrapper {
        max-width: 100%;
        overflow: auto;
        -webkit-overflow-scrolling: touch;
    }

    canvas {
        max-width: 100%;
        height: auto;
    }

    .page {
        margin: 8px auto;
        max-width: 100%;
    }

    /* Ensure content is visible */
    .app-main {
        height: calc(100vh - 70px);
        overflow: visible;
    }

    /* Pinch zoom support hint */
    .canvas-area::before {
        content: 'Pinch to zoom';
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: rgba(0, 0, 0, 0.7);
        color: white;
        padding: 8px 16px;
        border-radius: 20px;
        font-size: 12px;
        pointer-events: none;
        opacity: 0;
        transition: opacity 0.3s;
        z-index: 10;
    }

    .canvas-area.show-hint::before {
        opacity: 1;
    }
}

/* ===== PREVENT OVERSCROLL BOUNCE ON iOS ===== */
body {
    overscroll-behavior-y: none;
}

.canvas-area,
.sidebar-left,
.sidebar-right {
    overscroll-behavior: contain;
}

/* ===== IMPROVE SCROLLBAR VISIBILITY ON MOBILE ===== */
@media (max-width: 768px) {
    ::-webkit-scrollbar {
        width: 6px;
        height: 6px;
    }

    ::-webkit-scrollbar-track {
        background: transparent;
    }

    ::-webkit-scrollbar-thumb {
        background: rgba(0, 0, 0, 0.3);
        border-radius: 3px;
    }

    [data-theme="dark"] ::-webkit-scrollbar-thumb {
        background: rgba(255, 255, 255, 0.3);
    }
}

/* ===== SAFE AREA INSETS FOR NOTCHED DEVICES ===== */
@supports (padding: max(0px)) {
    .app-header {
        padding-left: max(12px, env(safe-area-inset-left));
        padding-right: max(12px, env(safe-area-inset-right));
    }

    .modal-content {
        margin-left: env(safe-area-inset-left);
        margin-right: env(safe-area-inset-right);
        margin-bottom: env(safe-area-inset-bottom);
    }
}
