/**
 * Apple-Inspired Responsive Design System
 * 12.1 - Responsive Design Enhancements
 * Mobile-first approach with Apple design principles
 */

/* ============================================
   MOBILE-FIRST BREAKPOINTS (Apple Standard)
   ============================================ */
:root {
    --breakpoint-xs: 375px;   /* iPhone SE */
    --breakpoint-sm: 414px;   /* iPhone Plus */
    --breakpoint-md: 768px;   /* iPad */
    --breakpoint-lg: 1024px;  /* iPad Pro */
    --breakpoint-xl: 1280px;  /* Desktop */
    --breakpoint-2xl: 1440px; /* Large Desktop */
}

/* ============================================
   BASE MOBILE STYLES (Mobile-First)
   ============================================ */

/* Touch-friendly button sizes (minimum 44x44px Apple standard) */
.apple-button-primary,
.apple-button-secondary,
.btn {
    min-height: 44px;
    min-width: 44px;
    padding: 12px 20px;
    touch-action: manipulation;
    -webkit-tap-highlight-color: rgba(0, 122, 255, 0.1);
}

/* Touch-friendly form controls */
.form-control,
.form-select {
    min-height: 44px;
    font-size: 16px; /* Prevents zoom on iOS */
    -webkit-appearance: none;
    appearance: none;
}

/* Touch-friendly navigation */
.navbar-nav .nav-link {
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 12px 16px;
}

/* ============================================
   RESPONSIVE NAVIGATION
   ============================================ */

/* Mobile navbar styles moved to navbar-apple.css for better organization */

/* ============================================
   RESPONSIVE TYPOGRAPHY
   ============================================ */

@media (max-width: 767px) {
    html {
        font-size: 14px;
    }
    
    h1 { font-size: 2rem; }
    h2 { font-size: 1.75rem; }
    h3 { font-size: 1.5rem; }
    h4 { font-size: 1.25rem; }
    h5 { font-size: 1.1rem; }
    h6 { font-size: 1rem; }
    
    /* Improved line height for mobile readability */
    p, li, span {
        line-height: 1.6;
    }
}

/* Font size scaling for accessibility */
@media (min-width: 768px) {
    :root {
        font-size: 16px;
    }
}

@media (min-width: 1024px) {
    :root {
        font-size: 16px;
    }
}

/* User preference font scaling */
@media (prefers-contrast: high) {
    :root {
        --text-dark: #000000;
        --text-medium: #333333;
        --bg-white: #ffffff;
    }
}

/* ============================================
   RESPONSIVE TABLES (Mobile Tables)
   ============================================ */

@media (max-width: 767px) {
    /* Card-based table for mobile */
    .table-responsive {
        border: none;
    }
    
    .table {
        display: block;
        width: 100%;
    }
    
    .table thead {
        display: none;
    }
    
    .table tbody,
    .table tr,
    .table td {
        display: block;
        width: 100%;
    }
    
    .table tr {
        margin-bottom: 16px;
        background: white;
        border-radius: 12px;
        padding: 16px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
        border: 1px solid rgba(0, 0, 0, 0.06);
    }
    
    .table td {
        border: none;
        padding: 8px 0;
        text-align: left !important;
        position: relative;
        padding-left: 40% !important;
    }
    
    .table td::before {
        content: attr(data-label);
        position: absolute;
        left: 0;
        width: 35%;
        font-weight: 600;
        color: var(--text-medium);
        font-size: 0.875rem;
    }
    
    /* Alternative: Horizontal scroll with snap */
    .table-scroll {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scroll-snap-type: x mandatory;
    }
    
    .table-scroll .table {
        min-width: 600px;
    }
    
    .table-scroll .table th,
    .table-scroll .table td {
        scroll-snap-align: start;
    }
}

/* ============================================
   RESPONSIVE FORMS (Mobile Forms)
   ============================================ */

@media (max-width: 767px) {
    .form-group,
    .mb-3,
    .mb-4 {
        margin-bottom: 20px;
    }
    
    /* Full-width inputs on mobile */
    .form-control,
    .form-select,
    .form-check-input {
        width: 100%;
    }
    
    /* Larger touch targets */
    .form-check {
        min-height: 44px;
        padding-left: 50px;
        display: flex;
        align-items: center;
    }
    
    .form-check-input {
        width: 22px;
        height: 22px;
        margin-top: 0;
        margin-left: -50px;
    }
    
    /* Mobile-friendly file upload */
    .form-control[type="file"] {
        padding: 12px;
        font-size: 16px;
    }
    
    /* Improved date/time inputs */
    input[type="date"],
    input[type="time"],
    input[type="datetime-local"] {
        min-height: 44px;
        font-size: 16px;
    }
}

/* ============================================
   RESPONSIVE CARDS AND LAYOUTS
   ============================================ */

@media (max-width: 767px) {
    /* Stack cards vertically on mobile */
    .row > [class*="col-"] {
        margin-bottom: 16px;
    }
    
    .apple-settings-card {
        margin-bottom: 16px;
        padding: 20px 16px;
    }
    
    /* Reduce padding on mobile */
    .container,
    .container-fluid {
        padding-left: 16px;
        padding-right: 16px;
    }
    
    /* Full-width buttons on mobile */
    .d-grid .btn,
    .btn-group-vertical .btn {
        width: 100%;
        margin-bottom: 8px;
    }
}

/* ============================================
   RESPONSIVE CHARTS (Mobile Charts)
   ============================================ */

@media (max-width: 767px) {
    .chart-container {
        position: relative;
        height: 300px !important;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    .chart-container canvas {
        max-width: 100%;
        height: auto !important;
    }
    
    /* Simplified chart legends on mobile */
    .chartjs-legend {
        flex-direction: column;
        font-size: 0.875rem;
    }
    
    .chartjs-legend li {
        margin-bottom: 8px;
    }
}

/* ============================================
   RESPONSIVE IMAGES (Mobile Image Optimization)
   ============================================ */

@media (max-width: 767px) {
    img {
        max-width: 100%;
        height: auto;
        display: block;
    }
    
    /* Lazy loading for images */
    img[loading="lazy"] {
        opacity: 0;
        transition: opacity 0.3s;
    }
    
    img[loading="lazy"].loaded {
        opacity: 1;
    }
    
    /* Responsive images with srcset */
    img.responsive {
        width: 100%;
        height: auto;
        object-fit: cover;
    }
}

/* WebP support detection */
@supports (background-image: url('image.webp')) {
    .webp-image {
        background-image: url('image.webp');
    }
}

/* ============================================
   TOUCH-FRIENDLY INTERFACES
   ============================================ */

/* Larger touch targets */
@media (max-width: 1024px) {
    /* Buttons */
    button,
    .btn,
    a.btn {
        min-height: 44px;
        min-width: 44px;
        padding: 12px 20px;
    }
    
    /* Icon buttons */
    .btn-icon,
    button[class*="icon"] {
        width: 44px;
        height: 44px;
        padding: 0;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    /* Links */
    a:not(.btn) {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        padding: 8px 0;
    }
    
    /* Checkboxes and radios */
    input[type="checkbox"],
    input[type="radio"] {
        width: 22px;
        height: 22px;
        margin: 0;
    }
    
    /* Switches */
    .form-switch .form-check-input {
        width: 44px;
        height: 24px;
    }
}

/* Touch gestures */
@media (hover: none) and (pointer: coarse) {
    /* Active state instead of hover for touch devices */
    button:active,
    .btn:active,
    a:active {
        transform: scale(0.98);
        opacity: 0.8;
    }
}

/* ============================================
   RESPONSIVE GRID SYSTEM (Enhanced)
   ============================================ */

/* Extra small devices (phones, < 576px) */
@media (max-width: 575.98px) {
    .container {
        max-width: 100%;
        padding-left: 12px;
        padding-right: 12px;
    }
    
    .row {
        margin-left: -8px;
        margin-right: -8px;
    }
    
    .row > * {
        padding-left: 8px;
        padding-right: 8px;
    }
}

/* Small devices (landscape phones, >= 576px) */
@media (min-width: 576px) and (max-width: 767.98px) {
    .container {
        max-width: 540px;
    }
}

/* Medium devices (tablets, >= 768px) */
@media (min-width: 768px) and (max-width: 991.98px) {
    .container {
        max-width: 720px;
    }
    
    /* Two column layout on tablets */
    .col-md-6,
    .col-lg-4 {
        flex: 0 0 50%;
        max-width: 50%;
    }
}

/* Large devices (desktops, >= 992px) */
@media (min-width: 992px) {
    .container {
        max-width: 960px;
    }
}

/* Extra large devices (large desktops, >= 1200px) */
@media (min-width: 1200px) {
    .container {
        max-width: 1140px;
    }
}

/* ============================================
   MOBILE ACCESSIBILITY
   ============================================ */

/* High contrast mode support */
@media (prefers-contrast: high) {
    .apple-settings-card,
    .card {
        border: 2px solid var(--text-dark);
    }
    
    .apple-button-primary {
        border: 2px solid currentColor;
    }
}

/* Reduced motion support */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ============================================
   MOBILE SPECIFIC UTILITIES
   ============================================ */

/* Safe area insets for notched devices (iOS) */
@supports (padding: max(0px)) {
    .safe-top {
        padding-top: max(20px, env(safe-area-inset-top));
    }
    
    .safe-bottom {
        padding-bottom: max(20px, env(safe-area-inset-bottom));
    }
    
    .safe-left {
        padding-left: max(16px, env(safe-area-inset-left));
    }
    
    .safe-right {
        padding-right: max(16px, env(safe-area-inset-right));
    }
}

/* Landscape orientation optimizations */
@media (orientation: landscape) and (max-height: 500px) {
    .navbar {
        padding: 0.5rem 0;
    }
    
    .container-fluid {
        padding-top: 1rem;
        padding-bottom: 1rem;
    }
}

/* Portrait orientation optimizations */
@media (orientation: portrait) {
    .chart-container {
        height: 400px;
    }
}

/* ============================================
   PRINT OPTIMIZATION
   ============================================ */

@media print {
    .navbar,
    .sidebar,
    .btn,
    button,
    .no-print {
        display: none !important;
    }
    
    .container,
    .apple-settings-card {
        box-shadow: none;
        border: 1px solid #ddd;
    }
    
    .table {
        border-collapse: collapse;
    }
    
    .table th,
    .table td {
        border: 1px solid #ddd;
        padding: 8px;
    }
    
    /* Force page breaks */
    .page-break {
        page-break-before: always;
    }
    
    /* Avoid breaks inside */
    .no-break {
        page-break-inside: avoid;
    }
}

/* ============================================
   PERFORMANCE OPTIMIZATIONS
   ============================================ */

/* GPU acceleration for transforms */
.will-change-transform {
    will-change: transform;
}

/* Contain layout for performance */
.isolated {
    contain: layout style paint;
}

/* ============================================
   APPLE-SPECIFIC MOBILE ENHANCEMENTS
   ============================================ */

/* iOS specific fixes */
@supports (-webkit-touch-callout: none) {
    /* iOS Safari specific styles */
    input,
    textarea,
    select {
        font-size: 16px; /* Prevents zoom */
        -webkit-appearance: none;
        appearance: none;
        border-radius: 8px;
    }
    
    /* iOS safe scrolling */
    body {
        -webkit-overflow-scrolling: touch;
    }
    
    /* iOS momentum scrolling */
    .overflow-auto,
    .overflow-scroll {
        -webkit-overflow-scrolling: touch;
    }
}

/* Android Chrome specific */
@media screen and (-webkit-min-device-pixel-ratio: 0) {
    select,
    textarea,
    input[type="text"],
    input[type="password"],
    input[type="datetime"],
    input[type="datetime-local"],
    input[type="date"],
    input[type="month"],
    input[type="time"],
    input[type="week"],
    input[type="number"],
    input[type="email"],
    input[type="url"] {
        font-size: 16px;
    }
}
