/* ========================================
   MOBILE ENHANCEMENTS FOR RAGHAD HEALTH
   Comprehensive Mobile-First Responsive CSS
   ======================================== */

/* ==================== Base Mobile Optimizations ==================== */

/* Safe Area for Notch Devices */
:root {
    --safe-area-inset-top: env(safe-area-inset-top, 0px);
    --safe-area-inset-right: env(safe-area-inset-right, 0px);
    --safe-area-inset-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-inset-left: env(safe-area-inset-left, 0px);
}

/* Prevent Horizontal Scroll */
html, body {
    overflow-x: hidden;
    width: 100%;
}

/* Better Touch Targets (Minimum 44x44px) */
@media (max-width: 768px) {
    button, a, input[type="button"], input[type="submit"] {
        min-height: 44px;
        min-width: 44px;
    }
}

/* ==================== Navigation & Header Mobile ==================== */

@media (max-width: 768px) {
    /* Mobile Header */
    header {
        padding: 0.75rem 1rem !important;
    }
    
    /* Logo Size */
    header h1 {
        font-size: 1.25rem !important;
    }
    
    header h1 i {
        font-size: 1.5rem !important;
    }
    
    /* Sidebar Mobile Overlay */
    aside {
        position: fixed !important;
        left: -100%;
        top: 0;
        bottom: 0;
        width: 80% !important;
        max-width: 320px;
        height: 100vh;
        overflow-y: auto;
        z-index: 999;
        transition: left 0.3s ease;
        box-shadow: 4px 0 12px rgba(0, 0, 0, 0.15);
    }
    
    aside.mobile-menu-open {
        left: 0;
    }
    
    /* Mobile Menu Overlay */
    .mobile-menu-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 998;
        backdrop-filter: blur(2px);
    }
    
    .mobile-menu-overlay.active {
        display: block;
    }
    
    /* Navigation Links - Bigger Touch Targets */
    nav a {
        padding: 1rem 1.5rem !important;
        font-size: 1rem !important;
        border-radius: 0.5rem;
        margin: 0.25rem 0.5rem;
    }
    
    nav a i {
        font-size: 1.25rem !important;
    }
}

/* ==================== Content Area Mobile ==================== */

@media (max-width: 768px) {
    main {
        padding: 1rem !important;
        margin-right: 0 !important;
        width: 100% !important;
    }
    
    #contentArea {
        padding: 0.5rem !important;
    }
    
    /* Page Titles */
    h1, h2 {
        font-size: 1.5rem !important;
    }
    
    h3 {
        font-size: 1.25rem !important;
    }
    
    h4 {
        font-size: 1.125rem !important;
    }
}

/* ==================== Cards & Boxes Mobile ==================== */

@media (max-width: 768px) {
    /* Stats Cards */
    .grid {
        grid-template-columns: 1fr !important;
        gap: 0.75rem !important;
    }
    
    /* Responsive Grid - 2 columns for small cards */
    .grid-cols-2,
    .grid-cols-3,
    .grid-cols-4,
    .md\:grid-cols-2,
    .md\:grid-cols-3,
    .md\:grid-cols-4 {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    .lg\:grid-cols-3,
    .lg\:grid-cols-4 {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    
    /* Card Padding */
    .bg-white, .card {
        padding: 1rem !important;
        border-radius: 0.75rem !important;
    }
    
    /* Stats Card Icon - Smaller on Mobile */
    .stat-card-icon {
        font-size: 2rem !important;
    }
}

/* ==================== Tables Mobile - Card View ==================== */

/* ==================== Table Mobile View ==================== */

/* Desktop: Hide mobile cards, show tables */
@media (min-width: 769px) {
    .table-mobile-cards {
        display: none !important;
    }
    
    table {
        display: table !important;
    }
}

/* Mobile: Hide tables, show cards */
@media (max-width: 768px) {
    /* Hide table and show card view */
    .data-table,
    table.mobile-converted {
        display: none !important;
    }
    
    /* Mobile Card View for Tables */
    .table-mobile-cards {
        display: flex !important;
        flex-direction: column;
        gap: 0.75rem;
    }
    
    .table-card {
        background: white;
        border-radius: 0.75rem;
        padding: 1rem;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        border-right: 4px solid #667eea;
    }
    
    .table-card-row {
        display: flex;
        justify-content: space-between;
        padding: 0.5rem 0;
        border-bottom: 1px solid #f3f4f6;
    }
    
    .table-card-row:last-child {
        border-bottom: none;
    }
    
    .table-card-label {
        font-weight: 600;
        color: #6b7280;
        font-size: 0.875rem;
    }
    
    .table-card-value {
        font-weight: 500;
        color: #111827;
        text-align: left;
    }
    
    /* Table Actions */
    .table-card-actions {
        display: flex;
        gap: 0.5rem;
        margin-top: 0.75rem;
        padding-top: 0.75rem;
        border-top: 1px solid #f3f4f6;
    }
    
    .table-card-actions button,
    .table-card-actions a {
        flex: 1;
        padding: 0.5rem;
        font-size: 0.875rem;
        text-align: center;
    }
}

/* ==================== Forms & Inputs Mobile ==================== */

@media (max-width: 768px) {
    /* Form Containers */
    form {
        padding: 0 !important;
    }
    
    .form-group {
        margin-bottom: 1rem !important;
    }
    
    /* Labels */
    label {
        font-size: 0.9375rem !important;
        margin-bottom: 0.5rem !important;
        display: block;
    }
    
    /* Inputs */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="tel"],
    input[type="date"],
    input[type="time"],
    input[type="datetime-local"],
    textarea,
    select {
        width: 100% !important;
        padding: 0.875rem !important;
        font-size: 1rem !important;
        border-radius: 0.5rem !important;
        border: 1.5px solid #e5e7eb !important;
        min-height: 44px;
    }
    
    /* Textarea */
    textarea {
        min-height: 120px !important;
    }
    
    /* Select Dropdowns */
    select {
        appearance: none;
        background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%236b7280'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
        background-repeat: no-repeat;
        background-position: left 0.75rem center;
        background-size: 1.25rem;
        padding-left: 2.5rem !important;
    }
    
    /* Checkboxes & Radio */
    input[type="checkbox"],
    input[type="radio"] {
        width: 20px !important;
        height: 20px !important;
        min-width: 20px;
        min-height: 20px;
    }
    
    /* Form Grid - Stack on Mobile */
    .form-grid,
    .grid-cols-2,
    .md\:grid-cols-2 {
        display: flex !important;
        flex-direction: column !important;
    }
}

/* ==================== Buttons Mobile ==================== */

@media (max-width: 768px) {
    /* Primary Buttons */
    button,
    .btn,
    .btn-primary,
    .btn-secondary,
    .btn-success {
        width: 100% !important;
        padding: 0.875rem 1.5rem !important;
        font-size: 1rem !important;
        border-radius: 0.75rem !important;
        min-height: 48px;
        font-weight: 600;
    }
    
    /* Button Groups - Stack Vertically */
    .flex.gap-2,
    .flex.gap-3,
    .flex.gap-4 {
        flex-direction: column !important;
        gap: 0.75rem !important;
    }
    
    /* Icon Buttons */
    .btn i {
        font-size: 1.125rem !important;
    }
    
    /* Floating Action Button */
    .fab-mobile {
        position: fixed;
        bottom: 5rem;
        left: 1rem;
        width: 56px;
        height: 56px;
        border-radius: 50%;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
        z-index: 900;
    }
}

/* ==================== Modals & Popups Mobile ==================== */

@media (max-width: 768px) {
    /* Modal Container */
    .fixed.inset-0 {
        padding: 0 !important;
    }
    
    /* Modal Content */
    .fixed.inset-0 > div {
        width: 100% !important;
        max-width: 100% !important;
        height: 100vh !important;
        max-height: 100vh !important;
        margin: 0 !important;
        border-radius: 0 !important;
        overflow-y: auto;
    }
    
    /* Modal Header */
    .modal-header,
    .sticky.top-0 {
        position: sticky !important;
        top: 0 !important;
        z-index: 50 !important;
        padding: 1rem !important;
        background: white !important;
        border-bottom: 1px solid #e5e7eb;
    }
    
    /* Modal Body */
    .modal-body,
    .p-6 {
        padding: 1rem !important;
    }
    
    /* Modal Title */
    .modal-header h2,
    .modal-header h3 {
        font-size: 1.25rem !important;
    }
    
    /* Close Button */
    .modal-header button {
        width: 40px !important;
        height: 40px !important;
        padding: 0 !important;
    }
}

/* ==================== Filters & Search Mobile ==================== */

@media (max-width: 768px) {
    /* Filter Container */
    .filter-container {
        background: white;
        padding: 1rem;
        border-radius: 0.75rem;
        margin-bottom: 1rem;
    }
    
    /* Search Input */
    .search-input {
        width: 100% !important;
        padding: 0.875rem 1rem !important;
        padding-right: 2.5rem !important;
        font-size: 1rem !important;
        border-radius: 0.75rem !important;
    }
    
    /* Filter Chips */
    .filter-chips {
        display: flex;
        gap: 0.5rem;
        overflow-x: auto;
        padding-bottom: 0.5rem;
        -webkit-overflow-scrolling: touch;
    }
    
    .filter-chip {
        padding: 0.5rem 1rem;
        background: #f3f4f6;
        border-radius: 9999px;
        font-size: 0.875rem;
        white-space: nowrap;
        flex-shrink: 0;
    }
    
    .filter-chip.active {
        background: #667eea;
        color: white;
    }
}

/* ==================== Charts Mobile ==================== */

@media (max-width: 768px) {
    .chart-container {
        height: 250px !important;
        padding: 0.5rem;
    }
    
    /* Chart Canvas */
    canvas {
        max-height: 250px !important;
    }
    
    /* Chart Grid - Stack */
    .chart-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ==================== Maps Mobile ==================== */

@media (max-width: 768px) {
    /* Map Container */
    #map {
        height: 400px !important;
        border-radius: 0.75rem !important;
    }
    
    /* Map Popup */
    .leaflet-popup-content {
        font-size: 0.875rem !important;
        width: 280px !important;
    }
    
    /* Map Controls */
    .leaflet-control-zoom {
        display: none !important;
    }
    
    .leaflet-touch .leaflet-bar {
        border: 2px solid rgba(0,0,0,0.2) !important;
    }
}

/* ==================== Tabs Mobile ==================== */

@media (max-width: 768px) {
    /* Tab Container */
    .tabs-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin-bottom: 1rem;
    }
    
    /* Tabs */
    .tabs {
        display: flex;
        gap: 0.5rem;
        border-bottom: 2px solid #e5e7eb;
        padding-bottom: 0;
    }
    
    .tab {
        padding: 0.75rem 1.25rem;
        font-size: 0.9375rem;
        white-space: nowrap;
        flex-shrink: 0;
        border-bottom: 3px solid transparent;
        transition: all 0.2s;
    }
    
    .tab.active {
        border-bottom-color: #667eea;
        color: #667eea;
        font-weight: 600;
    }
}

/* ==================== Toast Notifications Mobile ==================== */

@media (max-width: 768px) {
    .toast {
        top: auto !important;
        bottom: 1rem !important;
        right: 1rem !important;
        left: 1rem !important;
        width: auto !important;
        font-size: 0.9375rem !important;
    }
}

/* ==================== Loading States Mobile ==================== */

@media (max-width: 768px) {
    .spinner {
        width: 2.5rem !important;
        height: 2.5rem !important;
    }
    
    .loading-overlay {
        backdrop-filter: blur(4px);
    }
}

/* ==================== Utility Classes Mobile ==================== */

@media (max-width: 768px) {
    /* Spacing */
    .mobile-mt-0 { margin-top: 0 !important; }
    .mobile-mb-1 { margin-bottom: 0.25rem !important; }
    .mobile-mb-2 { margin-bottom: 0.5rem !important; }
    .mobile-mb-3 { margin-bottom: 0.75rem !important; }
    .mobile-mb-4 { margin-bottom: 1rem !important; }
    
    .mobile-p-2 { padding: 0.5rem !important; }
    .mobile-p-3 { padding: 0.75rem !important; }
    .mobile-p-4 { padding: 1rem !important; }
    
    /* Display */
    .mobile-hidden { display: none !important; }
    .mobile-block { display: block !important; }
    .mobile-flex { display: flex !important; }
    
    /* Text */
    .mobile-text-sm { font-size: 0.875rem !important; }
    .mobile-text-base { font-size: 1rem !important; }
    .mobile-text-center { text-align: center !important; }
    
    /* Width */
    .mobile-w-full { width: 100% !important; }
    .mobile-w-auto { width: auto !important; }
}

/* ==================== Sticky Elements Mobile ==================== */

@media (max-width: 768px) {
    .sticky-header {
        position: sticky;
        top: 0;
        z-index: 40;
        background: white;
        padding: 0.75rem 1rem;
        border-bottom: 1px solid #e5e7eb;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }
    
    .sticky-footer {
        position: sticky;
        bottom: 0;
        z-index: 40;
        background: white;
        padding: 1rem;
        border-top: 1px solid #e5e7eb;
        box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.05);
    }
}

/* ==================== Scrollable Sections Mobile ==================== */

@media (max-width: 768px) {
    .scroll-x {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .scroll-y {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Hide scrollbar but keep functionality */
    .scroll-x::-webkit-scrollbar,
    .scroll-y::-webkit-scrollbar {
        display: none;
    }
}

/* ==================== Safe Areas for iOS ==================== */

@supports (padding-top: env(safe-area-inset-top)) {
    @media (max-width: 768px) {
        header {
            padding-top: calc(0.75rem + var(--safe-area-inset-top)) !important;
        }
        
        aside {
            padding-top: var(--safe-area-inset-top) !important;
            padding-bottom: var(--safe-area-inset-bottom) !important;
        }
        
        .mobile-menu-toggle {
            bottom: calc(1rem + var(--safe-area-inset-bottom)) !important;
        }
        
        .sticky-footer {
            padding-bottom: calc(1rem + var(--safe-area-inset-bottom)) !important;
        }
    }
}

/* ==================== Accessibility - Larger Touch Targets ==================== */

@media (max-width: 768px) {
    /* Ensure all interactive elements are at least 44x44 */
    a, button, input, select, textarea, 
    [role="button"], [role="link"], [role="checkbox"] {
        min-height: 44px;
    }
    
    /* Better focus visible for touch */
    a:focus-visible,
    button:focus-visible,
    input:focus-visible {
        outline: 3px solid #667eea;
        outline-offset: 2px;
    }
}

/* ==================== Print Styles ==================== */

@media print {
    .mobile-menu-toggle,
    .mobile-menu-overlay,
    aside,
    .no-print {
        display: none !important;
    }
    
    main {
        margin: 0 !important;
        width: 100% !important;
    }
}

/* ==================== Landscape Orientation ==================== */

@media (max-width: 768px) and (orientation: landscape) {
    /* Adjust heights for landscape */
    .modal-content {
        max-height: 85vh !important;
    }
    
    #map {
        height: 300px !important;
    }
    
    .chart-container {
        height: 200px !important;
    }
}

/* ==================== Small Mobile (< 375px) ==================== */

@media (max-width: 374px) {
    html {
        font-size: 14px;
    }
    
    .grid-cols-2 {
        grid-template-columns: 1fr !important;
    }
    
    button, .btn {
        padding: 0.75rem 1rem !important;
        font-size: 0.9375rem !important;
    }
}

/* ==================== Dark Mode Support (Future) ==================== */

@media (prefers-color-scheme: dark) {
    /* Will be implemented when dark mode is added */
}

/* ==================== Ripple Effect for Touch ==================== */

.ripple-effect {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.6);
    transform: scale(0);
    animation: ripple-animation 0.6s ease-out;
    pointer-events: none;
}

@keyframes ripple-animation {
    to {
        transform: scale(4);
        opacity: 0;
    }
}

button, .btn {
    position: relative;
    overflow: hidden;
}

/* ==================== Pull to Refresh Indicator ==================== */

#pull-indicator {
    padding: 0.5rem 1rem;
    background: white;
    border-radius: 9999px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    transition: opacity 0.3s, transform 0.3s;
}

/* ==================== Mobile Table Cards - Enhanced Styling ==================== */

@media (max-width: 768px) {
    .table-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        transition: all 0.2s ease;
    }
    
    .table-card-value strong {
        color: #667eea;
    }
    
    /* Status Badges in Cards */
    .table-card .badge,
    .table-card .status-badge {
        display: inline-block;
        padding: 0.25rem 0.75rem;
        border-radius: 9999px;
        font-size: 0.75rem;
        font-weight: 600;
    }
}

/* ==================== Bottom Navigation (Alternative) ==================== */

@media (max-width: 768px) {
    .bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        border-top: 1px solid #e5e7eb;
        padding: 0.5rem;
        display: flex;
        justify-content: space-around;
        z-index: 900;
        box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.1);
    }
    
    .bottom-nav-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 0.5rem;
        color: #6b7280;
        font-size: 0.75rem;
        transition: color 0.2s;
        text-decoration: none;
        min-width: 60px;
    }
    
    .bottom-nav-item i {
        font-size: 1.25rem;
        margin-bottom: 0.25rem;
    }
    
    .bottom-nav-item.active {
        color: #667eea;
    }
    
    .bottom-nav-item:active {
        transform: scale(0.95);
    }
}

/* ==================== Swipeable Cards ==================== */

@media (max-width: 768px) {
    .swipeable-card {
        position: relative;
        transition: transform 0.3s ease;
    }
    
    .swipeable-card.swiping {
        transition: none;
    }
    
    .swipe-action {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 80px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 1.5rem;
    }
    
    .swipe-action.delete {
        right: 0;
        background: #ef4444;
    }
    
    .swipe-action.edit {
        left: 0;
        background: #3b82f6;
    }
}

/* ==================== Loading Skeleton Mobile ==================== */

@media (max-width: 768px) {
    .skeleton {
        background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
        background-size: 200% 100%;
        animation: loading 1.5s ease-in-out infinite;
        border-radius: 0.5rem;
    }
    
    @keyframes loading {
        0% {
            background-position: 200% 0;
        }
        100% {
            background-position: -200% 0;
        }
    }
    
    .skeleton-card {
        height: 120px;
        margin-bottom: 0.75rem;
    }
    
    .skeleton-text {
        height: 1rem;
        margin-bottom: 0.5rem;
    }
    
    .skeleton-title {
        height: 1.5rem;
        width: 60%;
        margin-bottom: 1rem;
    }
}

/* ==================== Mobile-Only Classes ==================== */

@media (max-width: 768px) {
    .mobile-only {
        display: block !important;
    }
    
    /* Hide converted tables on mobile, show cards instead */
    table.mobile-converted {
        display: none !important;
    }
    
    .table-mobile-cards {
        display: flex !important;
    }
}

@media (min-width: 769px) {
    .mobile-only {
        display: none !important;
    }
    
    .mobile-menu-toggle {
        display: none !important;
    }
    
    aside.mobile-menu-open {
        left: 0 !important;
    }
}
