/**
 * Material Design 3 Mobile Utilities
 * Comprehensive mobile-first CSS utilities for touch, typography, and responsive layout
 * Ensures WCAG 2.1 AA compliance and MD3 design token standards
 */

/* ==================== DESIGN TOKENS ==================== */

:root {
    /* Touch Target Minimums */
    --md3-touch-min: 44px;              /* iOS minimum */
    --md3-touch-target: 48px;           /* MD3 recommended */
    --md3-touch-comfortable: 56px;      /* Comfortable */
    
    /* Typography Minimums */
    --md3-text-min: 14px;               /* Absolute minimum */
    --md3-text-body: 16px;              /* Body text */
    --md3-text-label: 12px;             /* Small labels only */
    
    /* Spacing Scale */
    --md3-space-xs: 4px;
    --md3-space-sm: 8px;
    --md3-space-md: 16px;
    --md3-space-lg: 24px;
    --md3-space-xl: 32px;
    
    /* Mobile Breakpoints */
    --md3-mobile-sm: 375px;             /* Small phone */
    --md3-mobile-md: 414px;             /* Standard phone */
    --md3-tablet: 768px;                /* Tablet */
}

/* ==================== TOUCH TARGET UTILITIES ==================== */

/**
 * Ensures minimum 44x44px touch targets for all interactive elements
 * Usage: Add .md3-touch to buttons, links, checkboxes, radio buttons
 */
.md3-touch {
    min-width: var(--md3-touch-min);
    min-height: var(--md3-touch-min);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

/* MD3 Recommended 48px touch target */
.md3-touch-48 {
    min-width: var(--md3-touch-target);
    min-height: var(--md3-touch-target);
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

/* Comfortable 56px touch target for primary actions */
.md3-touch-comfortable {
    min-width: var(--md3-touch-comfortable);
    min-height: var(--md3-touch-comfortable);
}

/* Icon-only buttons with proper touch padding */
.md3-icon-button {
    min-width: var(--md3-touch-target);
    min-height: var(--md3-touch-target);
    padding: 12px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    cursor: pointer;
    transition: background-color 0.2s;
}

.md3-icon-button:hover {
    background-color: rgba(0, 0, 0, 0.04);
}

.md3-icon-button:active {
    background-color: rgba(0, 0, 0, 0.12);
}

/* Checkboxes and radio buttons with proper touch area */
.md3-checkbox-wrapper,
.md3-radio-wrapper {
    display: inline-flex;
    align-items: center;
    gap: var(--md3-space-sm);
    min-height: var(--md3-touch-min);
    cursor: pointer;
}

.md3-checkbox-wrapper input[type="checkbox"],
.md3-radio-wrapper input[type="radio"] {
    width: 20px;
    height: 20px;
    cursor: pointer;
    flex-shrink: 0;
}

/* ==================== FORM INPUT UTILITIES ==================== */

/**
 * Mobile-optimized form inputs with proper sizing and accessibility
 * Prevents iOS auto-zoom by ensuring minimum 16px font size
 */
.md3-input {
    width: 100%;
    padding: 14px 16px;
    font-size: 16px;                    /* Prevents iOS zoom */
    line-height: 1.5;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    transition: border-color 0.2s;
    box-sizing: border-box;
    min-height: var(--md3-touch-target);
}

.md3-input:focus {
    outline: none;
    border-color: #1976d2;
}

.md3-input::placeholder {
    color: #9e9e9e;
}

/* Textarea specific */
.md3-textarea {
    min-height: 120px;
    resize: vertical;
    font-family: inherit;
}

/* Select dropdown */
.md3-select {
    background: white;
    background-image: url('data:image/svg+xml;charset=UTF-8,%3csvg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="%23666"%3e%3cpath d="M7 10l5 5 5-5z"/%3e%3c/svg%3e');
    background-repeat: no-repeat;
    background-position: right 12px center;
    background-size: 24px;
    padding-right: 40px;
    cursor: pointer;
}

/* Input with icon (e.g., search, email) */
.md3-input-icon-wrapper {
    position: relative;
    width: 100%;
}

.md3-input-icon-wrapper .material-icons {
    position: absolute;
    left: 16px;
    top: 50%;
    transform: translateY(-50%);
    color: #666;
    pointer-events: none;
}

.md3-input-icon-wrapper .md3-input {
    padding-left: 48px;
}

/* Password visibility toggle */
.md3-password-toggle {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    background: none;
    border: none;
    padding: 8px;
    cursor: pointer;
    color: #666;
    display: flex;
    align-items: center;
    justify-content: center;
}

.md3-password-toggle:hover {
    color: #333;
}

/* ==================== BUTTON UTILITIES ==================== */

.md3-button {
    min-height: var(--md3-touch-target);
    padding: 12px 24px;
    font-size: 16px;
    font-weight: 500;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--md3-space-sm);
    transition: background-color 0.2s, box-shadow 0.2s;
    text-decoration: none;
}

.md3-button-primary {
    background: #1976d2;
    color: white;
}

.md3-button-primary:hover {
    background: #1565c0;
}

.md3-button-primary:active {
    background: #0d47a1;
}

.md3-button-secondary {
    background: #e0e0e0;
    color: #333;
}

.md3-button-secondary:hover {
    background: #d5d5d5;
}

.md3-button-text {
    background: transparent;
    color: #1976d2;
}

.md3-button-text:hover {
    background: rgba(25, 118, 210, 0.08);
}

/* ==================== TYPOGRAPHY UTILITIES ==================== */

.md3-text-body {
    font-size: var(--md3-text-body);
    line-height: 1.5;
}

.md3-text-body-small {
    font-size: var(--md3-text-min);
    line-height: 1.4;
}

.md3-text-label {
    font-size: var(--md3-text-label);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #666;
}

.md3-text-headline {
    font-size: 24px;
    font-weight: 600;
    line-height: 1.3;
}

/* Ensure minimum readable text sizes */
@media (max-width: 768px) {
    /* Never allow text below 14px on mobile */
    body, p, span, div, td, th, li {
        font-size: max(14px, 1em);
    }
    
    /* Buttons and inputs must be at least 16px to prevent iOS zoom */
    button, input, select, textarea {
        font-size: 16px !important;
    }
}

/* ==================== RESPONSIVE TABLE UTILITIES ==================== */

.md3-table-wrapper {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.md3-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 600px;                   /* Ensures horizontal scroll instead of column squish */
}

.md3-table th,
.md3-table td {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid #e0e0e0;
    min-width: 80px;
    font-size: var(--md3-text-min);
}

.md3-table th {
    font-weight: 600;
    background: #f5f5f5;
    position: sticky;
    top: 0;
    z-index: 10;
}

/* Mobile card layout alternative for tables */
@media (max-width: 768px) {
    .md3-table-cards {
        display: block;
    }
    
    .md3-table-cards thead {
        display: none;
    }
    
    .md3-table-cards tbody {
        display: block;
    }
    
    .md3-table-cards tr {
        display: block;
        margin-bottom: var(--md3-space-md);
        border: 1px solid #e0e0e0;
        border-radius: 8px;
        padding: var(--md3-space-md);
        background: white;
    }
    
    .md3-table-cards td {
        display: flex;
        justify-content: space-between;
        padding: 8px 0;
        border: none;
        min-width: 0;
    }
    
    .md3-table-cards td:before {
        content: attr(data-label);
        font-weight: 600;
        margin-right: var(--md3-space-md);
        color: #666;
    }
}

/* ==================== MODAL/DIALOG UTILITIES ==================== */

.md3-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: var(--md3-space-md);
}

.md3-modal {
    background: white;
    border-radius: 16px;
    padding: var(--md3-space-lg);
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

@media (max-width: 768px) {
    .md3-modal {
        max-width: 100%;
        max-height: 95vh;
        border-radius: 16px 16px 0 0;
        margin-top: auto;
    }
    
    .md3-modal-overlay {
        align-items: flex-end;
        padding: 0;
    }
}

/* ==================== RESPONSIVE GRID UTILITIES ==================== */

.md3-grid {
    display: grid;
    gap: var(--md3-space-md);
}

.md3-grid-2 {
    grid-template-columns: repeat(2, 1fr);
}

.md3-grid-3 {
    grid-template-columns: repeat(3, 1fr);
}

.md3-grid-4 {
    grid-template-columns: repeat(4, 1fr);
}

@media (max-width: 768px) {
    .md3-grid-2,
    .md3-grid-3,
    .md3-grid-4 {
        grid-template-columns: 1fr;
    }
    
    .md3-grid-2.md3-keep-cols {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ==================== SPACING UTILITIES ==================== */

.md3-m-0 { margin: 0 !important; }
.md3-mt-xs { margin-top: var(--md3-space-xs); }
.md3-mt-sm { margin-top: var(--md3-space-sm); }
.md3-mt-md { margin-top: var(--md3-space-md); }
.md3-mt-lg { margin-top: var(--md3-space-lg); }
.md3-mt-xl { margin-top: var(--md3-space-xl); }

.md3-mb-xs { margin-bottom: var(--md3-space-xs); }
.md3-mb-sm { margin-bottom: var(--md3-space-sm); }
.md3-mb-md { margin-bottom: var(--md3-space-md); }
.md3-mb-lg { margin-bottom: var(--md3-space-lg); }
.md3-mb-xl { margin-bottom: var(--md3-space-xl); }

.md3-p-xs { padding: var(--md3-space-xs); }
.md3-p-sm { padding: var(--md3-space-sm); }
.md3-p-md { padding: var(--md3-space-md); }
.md3-p-lg { padding: var(--md3-space-lg); }
.md3-p-xl { padding: var(--md3-space-xl); }

/* ==================== LOADING SKELETON UTILITIES ==================== */

.md3-skeleton {
    background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
    background-size: 200% 100%;
    animation: md3-skeleton-loading 1.5s ease-in-out infinite;
    border-radius: 4px;
}

@keyframes md3-skeleton-loading {
    0% {
        background-position: 200% 0;
    }
    100% {
        background-position: -200% 0;
    }
}

.md3-skeleton-text {
    height: 16px;
    margin-bottom: 8px;
}

.md3-skeleton-title {
    height: 24px;
    width: 60%;
    margin-bottom: 16px;
}

.md3-skeleton-button {
    height: 48px;
    width: 120px;
}

.md3-skeleton-card {
    height: 200px;
    width: 100%;
}

/* ==================== RESPONSIVE IMAGE UTILITIES ==================== */

.md3-img-responsive {
    max-width: 100%;
    height: auto;
    display: block;
}

.md3-img-cover {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.md3-img-contain {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

/* ==================== ACCESSIBILITY UTILITIES ==================== */

/* Screen reader only */
.md3-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* Focus visible for keyboard navigation */
.md3-focusable:focus-visible {
    outline: 2px solid #1976d2;
    outline-offset: 2px;
}

/* ==================== MOBILE-SPECIFIC UTILITIES ==================== */

@media (max-width: 768px) {
    /* Hide on mobile */
    .md3-hide-mobile {
        display: none !important;
    }
    
    /* Full width on mobile */
    .md3-full-mobile {
        width: 100% !important;
    }
    
    /* Stack on mobile */
    .md3-stack-mobile {
        flex-direction: column !important;
    }
    
    /* Reduce padding on mobile */
    .md3-compact-mobile {
        padding: var(--md3-space-sm) !important;
    }
}

@media (min-width: 769px) {
    /* Hide on desktop */
    .md3-hide-desktop {
        display: none !important;
    }
}
