/**
 * Mobile Optimizations for iOS and Android
 * Advanced responsive CSS for touch devices
 */

/* ========== iOS Specific ==========*/
@supports (-webkit-touch-callout: none) {
    body {
        /* Prevent selection on long press */
        -webkit-user-select: none;
        user-select: none;
    }

    /* Allow text selection in input fields */
    input, textarea, [contenteditable] {
        -webkit-user-select: text;
        user-select: text;
    }

    /* Smooth scrolling */
    html {
        scroll-behavior: smooth;
        -webkit-font-smoothing: antialiased;
        -webkit-text-size-adjust: 100%;
    }

    /* Fix input zoom on iOS */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="search"],
    textarea,
    select {
        font-size: 16px !important;
    }

    /* Safe area support for notch */
    body {
        padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    }
}

/* ========== Android Specific ==========*/
@supports (padding: max(0px)) {
    .navbar {
        padding: max(0.5rem, env(safe-area-inset-top)) 0.5rem 0.5rem 0.5rem;
    }

    .sidebar {
        padding-top: max(0.5rem, env(safe-area-inset-top));
        padding-left: max(0.5rem, env(safe-area-inset-left));
    }
}

/* ========== Touch Friendly Elements ==========*/

/* Increase touch target size */
button, a, input[type="button"], input[type="submit"],
[role="button"], .btn {
    min-height: 48px;
    min-width: 48px;
    padding: 12px 16px;
}

/* Form inputs */
input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
input[type="search"],
input[type="date"],
input[type="time"],
select,
textarea {
    min-height: 48px;
    padding: 12px;
    font-size: 16px;
    border-radius: 8px;
    border: 1px solid #ddd;
}

/* Remove focus outline on touch (will add it back for keyboard) */
button:focus,
input:focus,
select:focus,
textarea:focus {
    outline: none;
    box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.25);
}

/* ========== Landscape Mode ==========*/
@media (max-height: 500px) and (orientation: landscape) {
    .navbar {
        height: 45px;
        padding: 0.25rem 0.5rem;
    }

    .navbar-brand {
        font-size: 1rem;
    }

    .sidebar {
        position: fixed;
        top: 45px;
        height: calc(100vh - 45px);
        z-index: 1000;
    }

    .main-content {
        margin-top: 45px;
    }

    .stat-card {
        padding: 10px;
        margin: 5px;
    }

    .stat-card h5 {
        font-size: 0.75rem;
    }

    .stat-card p {
        font-size: 0.65rem;
    }
}

/* ========== Portrait Mode ==========*/
@media (max-height: 600px) and (orientation: portrait) {
    .navbar {
        padding: 0.5rem 0.25rem;
    }

    .stat-card {
        padding: 12px;
        margin: 8px 0;
    }
}

/* ========== Small Screens (<480px) ==========*/
@media (max-width: 479px) {
    :root {
        font-size: 13px;
    }

    body {
        font-size: 13px;
        line-height: 1.4;
    }

    .container, .container-fluid {
        padding: 8px;
    }

    .navbar-brand {
        font-size: 1rem;
    }

    h1 {
        font-size: 1rem;
        margin: 8px 0;
    }

    h2 {
        font-size: 0.95rem;
        margin: 8px 0;
    }

    h3, h4, h5, h6 {
        font-size: 0.85rem;
        margin: 6px 0;
    }

    .btn, button {
        font-size: 0.75rem;
        padding: 8px 12px;
        min-height: 40px;
    }

    .btn-sm {
        font-size: 0.65rem;
        padding: 4px 8px;
        min-height: 32px;
    }

    .form-control, input, select, textarea {
        font-size: 0.85rem;
        padding: 8px;
        min-height: 40px;
    }

    .table {
        font-size: 0.75rem;
    }

    .table td, .table th {
        padding: 0.35rem;
    }

    .card {
        margin-bottom: 8px;
    }

    .card-body {
        padding: 8px;
    }

    .navbar-nav .nav-link {
        padding: 0.5rem 0.25rem !important;
        font-size: 0.85rem;
    }

    .sidebar-menu li {
        margin: 2px 0;
    }

    .sidebar-menu a {
        padding: 8px 10px;
        font-size: 0.85rem;
    }

    .stat-card {
        flex: 0 0 calc(50% - 8px);
        margin: 4px;
    }
}

/* ========== Media Query for Very Small Phones (320-414px) ==========*/
@media (max-width: 414px) {
    .navbar {
        height: auto;
    }

    .sidebar {
        width: 75vw;
        max-width: 200px;
    }

    .table {
        display: block;
        width: 100%;
        overflow-x: auto;
    }

    .table thead {
        display: none;
    }

    .table tbody,
    .table tr,
    .table td {
        display: block;
        width: 100%;
    }

    .table tr {
        margin-bottom: 15px;
        border: 1px solid #ddd;
        border-radius: 4px;
        padding: 10px;
    }

    .table td {
        padding: 8px 0;
        border: none;
        position: relative;
        padding-left: 50%;
    }

    .table td::before {
        content: attr(data-label);
        position: absolute;
        left: 0;
        width: 45%;
        padding-left: 10px;
        font-weight: bold;
        text-align: left;
    }

    /* Modal adjustments */
    .modal {
        padding: 0;
    }

    .modal-dialog {
        margin: 10px;
        max-width: calc(100vw - 20px);
    }

    .modal-header, .modal-body, .modal-footer {
        padding: 10px;
    }
}

/* ========== Pull to Refresh Support ==========*/
body {
    overscroll-behavior-y: contain;
}

/* ========== Network Indicator ==========*/
@media (max-width: 576px) {
    .offline-indicator {
        display: none;
        background: #dc3545;
        color: white;
        padding: 10px;
        text-align: center;
        font-weight: bold;
    }

    body.offline .offline-indicator {
        display: block;
    }
}

/* ========== Tap Highlight Color ==========*/
* {
    -webkit-tap-highlight-color: transparent;
    tap-highlight-color: transparent;
}

a, button, [role="button"] {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.1);
    tap-highlight-color: rgba(0, 0, 0, 0.1);
}

/* ========== Sticky Header ==========*/
.navbar {
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* ========== Bottom Navigation (optional) ==========*/
@media (max-width: 576px) {
    .bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        background: white;
        border-top: 1px solid #eee;
        display: flex;
        justify-content: space-around;
        align-items: center;
        height: 56px;
        box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.1);
    }

    body.has-bottom-nav {
        padding-bottom: 56px;
    }

    .main-content.has-bottom-nav {
        padding-bottom: 60px;
    }
}

/* ========== Accessibility Improvements ==========*/
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* Focus visible for keyboard navigation */
*:focus-visible {
    outline: 2px solid #007bff;
    outline-offset: 2px;
}

/* ========== Dark Mode Support ==========*/
@media (prefers-color-scheme: dark) {
    input, select, textarea {
        background-color: #2d2d2d;
        color: #fff;
        border-color: #444;
    }

    .form-control:focus {
        background-color: #3d3d3d;
        color: #fff;
        border-color: #0d6efd;
        box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);
    }

    .btn-outline-secondary {
        border-color: #666;
        color: #aaa;
    }

    .btn-outline-secondary:hover {
        background-color: #3d3d3d;
        border-color: #888;
    }
}

/* ========== Print Styles ==========*/
@media print {
    .navbar, .sidebar, .bottom-nav, .btn-print {
        display: none;
    }

    body {
        margin: 0;
        padding: 0;
    }

    .main-content {
        margin: 0;
        padding: 0;
    }
}
