/*
 * Touch Optimization CSS
 * WCAG 2.1 AA Compliance: Minimum 44x44px touch targets
 * Target: Mobile (< 768px) and Tablet (768px - 1024px)
 *
 * P2 Task #22: Touch Optimization
 * Created: 2025-01-30
 */

/* ============================================
   WCAG 2.1 AA Touch Target Standards
   ============================================
   - Minimum: 44x44px for all interactive elements
   - Recommended: 48x48px for primary actions
   - Spacing: 8px minimum between touch targets
   ============================================ */

/* ============================================
   1. GLOBAL TOUCH TARGET ENFORCEMENT
   ============================================ */

/* Mobile devices (< 768px) - STRICT 48x48px */
@media (max-width: 767.98px) {
    /* All buttons - Primary touch targets */
    .btn,
    button:not(.unstyled) {
        min-height: 48px;
        min-width: 48px;
        padding: 12px 16px;
        font-size: 16px; /* Prevents iOS zoom */
        touch-action: manipulation; /* Disable double-tap zoom */
    }

    /* Small buttons - OVERRIDE to meet WCAG */
    .btn-sm,
    .btn-small,
    .small-action-btn {
        min-height: 48px !important; /* Force compliance */
        min-width: 48px !important;
        padding: 10px 14px !important;
        font-size: 14px;
    }

    /* Icon-only buttons - Square 48x48px */
    .btn-icon,
    .icon-button,
    [class*="icon-btn"] {
        min-height: 48px !important;
        min-width: 48px !important;
        width: 48px;
        height: 48px;
        padding: 12px !important;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* Form controls */
    .form-control,
    .form-select,
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="password"],
    select,
    textarea {
        min-height: 48px;
        padding: 12px 16px;
        font-size: 16px; /* Prevents iOS zoom */
        touch-action: manipulation;
    }

    /* Checkbox and radio - Larger tap area */
    .form-check-input {
        min-width: 24px;
        min-height: 24px;
        margin-top: 0;
    }

    .form-check {
        min-height: 48px;
        padding: 12px 0;
        display: flex;
        align-items: center;
    }

    .form-check-label {
        padding-left: 12px;
        cursor: pointer;
        user-select: none;
        -webkit-tap-highlight-color: transparent;
    }

    /* Links and nav items */
    .nav-link,
    a.nav-item {
        min-height: 48px;
        padding: 14px 16px;
        display: flex;
        align-items: center;
        touch-action: manipulation;
    }

    /* Dropdown items */
    .dropdown-item {
        min-height: 48px;
        padding: 12px 16px;
        font-size: 14px;
        touch-action: manipulation;
    }

    /* Badge buttons (e.g., notification badges) */
    .badge-btn,
    .badge.clickable {
        min-height: 48px;
        min-width: 48px;
        padding: 10px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* Touch spacing - Minimum 8px between interactive elements */
    .btn + .btn,
    button + button {
        margin-left: 8px;
    }

    .btn-group .btn,
    .btn-group button {
        margin-left: 0; /* Button groups are exempt */
    }
}

/* Tablet devices (768px - 1024px) - WCAG Minimum 44x44px */
@media (min-width: 768px) and (max-width: 1024px) {
    .btn,
    button:not(.unstyled) {
        min-height: 44px;
        min-width: 44px;
        padding: 10px 16px;
        touch-action: manipulation;
    }

    .btn-sm,
    .btn-small {
        min-height: 44px !important;
        min-width: 44px !important;
        padding: 8px 12px !important;
    }

    .btn-icon,
    .icon-button {
        min-height: 44px !important;
        min-width: 44px !important;
        width: 44px;
        height: 44px;
    }

    .form-control,
    input,
    select,
    textarea {
        min-height: 44px;
        padding: 10px 14px;
    }

    .nav-link {
        min-height: 44px;
        padding: 12px 16px;
    }

    .dropdown-item {
        min-height: 44px;
        padding: 10px 14px;
    }
}

/* ============================================
   2. MUDBLAZOR COMPONENT OVERRIDES
   ============================================ */

/* Mobile: MudBlazor components */
@media (max-width: 767.98px) {
    /* MudButton - All variants */
    .mud-button-root {
        min-height: 48px !important;
        min-width: 48px !important;
        padding: 12px 16px !important;
        touch-action: manipulation;
    }

    /* MudIconButton - Icon only buttons */
    .mud-icon-button {
        min-height: 48px !important;
        min-width: 48px !important;
        width: 48px !important;
        height: 48px !important;
        padding: 12px !important;
    }

    /* MudFab - Floating action button */
    .mud-fab {
        min-height: 56px !important;
        min-width: 56px !important;
        width: 56px !important;
        height: 56px !important;
    }

    /* MudChip - Chip buttons */
    .mud-chip {
        min-height: 48px !important;
        padding: 0 16px !important;
        touch-action: manipulation;
    }

    /* MudToggleIconButton */
    .mud-toggle-icon-button {
        min-height: 48px !important;
        min-width: 48px !important;
        width: 48px !important;
        height: 48px !important;
    }

    /* MudSwitch - Switch toggle */
    .mud-switch {
        min-height: 48px !important;
        padding: 8px 0 !important;
    }

    .mud-switch-root {
        min-height: 28px;
        min-width: 48px;
    }

    /* MudRadio and MudCheckbox */
    .mud-radio,
    .mud-checkbox {
        min-height: 48px !important;
        padding: 12px 0 !important;
    }

    .mud-radio-root,
    .mud-checkbox-root {
        min-height: 24px;
        min-width: 24px;
    }

    /* MudNavLink */
    .mud-nav-link {
        min-height: 48px !important;
        padding: 14px 16px !important;
        touch-action: manipulation;
    }

    /* MudMenu and MudMenuItem */
    .mud-menu-item {
        min-height: 48px !important;
        padding: 12px 16px !important;
    }

    /* MudListItem */
    .mud-list-item {
        min-height: 48px !important;
        padding: 12px 16px !important;
        touch-action: manipulation;
    }

    /* MudTabs */
    .mud-tab {
        min-height: 48px !important;
        padding: 0 16px !important;
        touch-action: manipulation;
    }

    /* MudPagination */
    .mud-pagination-item {
        min-height: 48px !important;
        min-width: 48px !important;
        touch-action: manipulation;
    }
}

/* Tablet: MudBlazor components */
@media (min-width: 768px) and (max-width: 1024px) {
    .mud-button-root {
        min-height: 44px !important;
        min-width: 44px !important;
    }

    .mud-icon-button {
        min-height: 44px !important;
        min-width: 44px !important;
        width: 44px !important;
        height: 44px !important;
    }

    .mud-fab {
        min-height: 52px !important;
        min-width: 52px !important;
        width: 52px !important;
        height: 52px !important;
    }

    .mud-chip {
        min-height: 44px !important;
    }

    .mud-nav-link,
    .mud-menu-item,
    .mud-list-item {
        min-height: 44px !important;
    }

    .mud-tab {
        min-height: 44px !important;
    }
}

/* ============================================
   3. PRODUCT GRID TOUCH OPTIMIZATION
   ============================================ */

/* Product cards - Critical for NewOrder.razor */
@media (max-width: 767.98px) {
    .product-card {
        min-height: 140px !important;
        min-width: 100px !important;
        padding: 12px !important;
        cursor: pointer;
        touch-action: manipulation;
        -webkit-tap-highlight-color: rgba(102, 126, 234, 0.2);
        transition: all 0.2s ease;
    }

    /* Touch feedback */
    .product-card:active {
        transform: scale(0.98);
        background-color: rgba(102, 126, 234, 0.1);
    }

    /* Product card internal spacing */
    .product-card img,
    .product-placeholder {
        min-height: 80px;
        margin-bottom: 8px;
    }

    .product-name {
        font-size: 14px;
        line-height: 1.3;
        margin-bottom: 4px;
        min-height: 36px; /* 2 lines minimum */
    }

    .product-price {
        font-size: 16px;
        font-weight: 600;
        margin-bottom: 4px;
    }

    /* Product grid spacing */
    .products-section .row {
        gap: 12px; /* Minimum 8px spacing */
    }

    .products-section .col {
        padding: 6px; /* Additional touch spacing */
    }
}

/* Tablet: Product cards */
@media (min-width: 768px) and (max-width: 1024px) {
    .product-card {
        min-height: 130px !important;
        min-width: 100px !important;
        padding: 10px !important;
        cursor: pointer;
        touch-action: manipulation;
    }

    .product-card:active {
        transform: scale(0.98);
    }
}

/* ============================================
   4. TABLE CARD TOUCH OPTIMIZATION
   ============================================ */

/* Table preview cards - For Tables/Manage.razor */
@media (max-width: 767.98px) {
    .table-preview,
    .table-card {
        min-height: 100px !important;
        min-width: 100px !important;
        padding: 12px !important;
        touch-action: manipulation;
        -webkit-tap-highlight-color: rgba(102, 126, 234, 0.2);
    }

    .table-preview:active,
    .table-card:active {
        transform: scale(0.98);
    }

    /* Drag handles - Larger touch area */
    .drag-handle,
    .move-handle {
        min-height: 48px !important;
        min-width: 48px !important;
        padding: 12px !important;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: grab;
        touch-action: none; /* Enable dragging */
    }

    .drag-handle:active,
    .move-handle:active {
        cursor: grabbing;
    }
}

@media (min-width: 768px) and (max-width: 1024px) {
    .table-preview,
    .table-card {
        min-height: 90px !important;
        min-width: 90px !important;
        touch-action: manipulation;
    }

    .drag-handle,
    .move-handle {
        min-height: 44px !important;
        min-width: 44px !important;
    }
}

/* ============================================
   5. ORDER ITEM TOUCH OPTIMIZATION
   ============================================ */

/* Order items in lists - Swipe gesture ready */
@media (max-width: 767.98px) {
    .order-item,
    .order-summary-item,
    [class*="order-item"] {
        min-height: 72px; /* Enough for 2 lines + padding */
        padding: 12px 16px;
        touch-action: pan-y; /* Enable vertical scroll, horizontal swipe */
        cursor: pointer;
        -webkit-tap-highlight-color: rgba(102, 126, 234, 0.2);
        transition: transform 0.2s ease;
    }

    /* Swipe visual feedback */
    .order-item.swiping {
        transition: transform 0.1s linear;
    }

    .order-item.swipe-left {
        background: linear-gradient(to left, #ef4444 0%, transparent 100%);
    }

    .order-item.swipe-right {
        background: linear-gradient(to right, #10b981 0%, transparent 100%);
    }

    /* Order action buttons within items */
    .order-item .btn,
    .order-summary-item .btn {
        min-height: 44px;
        min-width: 44px;
        padding: 8px 12px;
    }
}

@media (min-width: 768px) and (max-width: 1024px) {
    .order-item,
    .order-summary-item {
        min-height: 64px;
        padding: 10px 14px;
        touch-action: pan-y;
    }
}

/* ============================================
   6. MODAL AND DIALOG TOUCH OPTIMIZATION
   ============================================ */

@media (max-width: 767.98px) {
    /* Modal close buttons */
    .modal-header .btn-close,
    .mud-dialog-title .mud-icon-button {
        min-height: 48px !important;
        min-width: 48px !important;
        width: 48px !important;
        height: 48px !important;
    }

    /* Modal action buttons */
    .modal-footer .btn,
    .mud-dialog-actions .mud-button-root {
        min-height: 48px !important;
        width: 100%; /* Full width on mobile */
        margin: 4px 0; /* Vertical spacing */
    }

    /* Dialog content minimum touch spacing */
    .modal-body,
    .mud-dialog-content {
        padding: 20px 16px;
    }
}

@media (min-width: 768px) and (max-width: 1024px) {
    .modal-header .btn-close,
    .mud-dialog-title .mud-icon-button {
        min-height: 44px !important;
        min-width: 44px !important;
    }

    .modal-footer .btn,
    .mud-dialog-actions .mud-button-root {
        min-height: 44px !important;
    }
}

/* ============================================
   7. ACCESSIBILITY ENHANCEMENTS
   ============================================ */

/* Remove 300ms tap delay on mobile */
a, button, input, select, textarea {
    touch-action: manipulation;
}

/* Improve tap highlight color */
* {
    -webkit-tap-highlight-color: rgba(102, 126, 234, 0.2);
}

/* Focus visible for keyboard navigation - Only interactive elements */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[role="button"]:focus-visible,
[role="link"]:focus-visible,
[role="menuitem"]:focus-visible,
[role="tab"]:focus-visible,
[tabindex]:not([tabindex="-1"]):focus-visible {
    outline: 3px solid #667eea;
    outline-offset: 2px;
}

/* Remove focus outline from non-interactive elements */
div:focus,
section:focus,
article:focus,
main:focus,
header:focus,
footer:focus,
nav:focus,
span:focus,
p:focus,
h1:focus,
h2:focus,
h3:focus,
h4:focus,
h5:focus,
h6:focus {
    outline: none;
}

/* Disable text selection on interactive elements */
.btn,
button,
.product-card,
.table-card,
.order-item,
.nav-link,
[role="button"] {
    user-select: none;
    -webkit-user-select: none;
    -moz-user-select: none;
}

/* ============================================
   8. TOUCH GESTURE PREPARATION
   ============================================ */

/* Swipeable elements - Visual indicators */
.swipeable {
    position: relative;
    overflow: hidden;
}

.swipeable::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
}

.swipeable.swipe-hint-left::before {
    background: linear-gradient(to left, rgba(239, 68, 68, 0.1) 0%, transparent 100%);
    opacity: 1;
}

.swipeable.swipe-hint-right::before {
    background: linear-gradient(to right, rgba(16, 185, 129, 0.1) 0%, transparent 100%);
    opacity: 1;
}

/* Pull-to-refresh indicator placeholder */
.pull-to-refresh-indicator {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s;
    transform: translateY(-60px);
}

.pull-to-refresh-indicator.visible {
    opacity: 1;
    transform: translateY(0);
}

/* ============================================
   9. PERFORMANCE OPTIMIZATIONS
   ============================================ */

/* GPU acceleration for touch interactions */
.product-card,
.table-card,
.order-item,
.swipeable {
    will-change: transform;
    transform: translateZ(0); /* Force GPU layer */
}

/* Prevent scrolling lag on touch devices */
.products-scroll,
.orders-scroll,
.tables-scroll {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
}

/* ============================================
   10. UTILITY CLASSES
   ============================================ */

/* Touch-friendly utility classes */
.touch-target {
    min-height: 44px;
    min-width: 44px;
}

.touch-target-lg {
    min-height: 48px;
    min-width: 48px;
}

.touch-spacing {
    margin: 4px;
}

.touch-spacing-lg {
    margin: 8px;
}

.no-tap-highlight {
    -webkit-tap-highlight-color: transparent;
}

/* Desktop: Reset to normal */
@media (min-width: 1025px) {
    /* Most touch optimizations don't apply to desktop */
    /* But keep basic accessibility standards */
    .btn,
    button {
        min-height: 36px;
        min-width: auto;
    }

    .btn-sm {
        min-height: 32px !important;
        min-width: auto !important;
    }

    .product-card,
    .table-card,
    .order-item {
        will-change: auto; /* Reduce GPU usage on desktop */
    }
}
