/**
 * LengSpurs Dairy — Global Mobile Responsive Enhancements
 * Loaded on every page via components/pwa-head.php
 * Adds safe-area insets, tap-friendly controls, table reflow and
 * form sizing that prevents iOS zoom-on-focus.
 */

/* ---------- Base mobile-first resets ---------- */
html {
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

body {
    overflow-x: hidden;
}

/* Honour iOS safe areas (notch / home bar) */
@supports (padding: max(0px)) {
    .top-bar,
    .sidebar-header {
        padding-top: max(16px, env(safe-area-inset-top));
    }
    .main-content,
    .scroll-content {
        padding-bottom: max(24px, env(safe-area-inset-bottom));
    }
    .sidebar {
        padding-left: env(safe-area-inset-left);
    }
}

/* Prevent iOS zoom on focus by enforcing 16px minimum on form fields */
@media (max-width: 768px) {
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="tel"],
    input[type="number"],
    input[type="date"],
    input[type="search"],
    input[type="url"],
    input:not([type]),
    select,
    textarea {
        font-size: 16px !important;
    }
}

/* ---------- Tap targets ---------- */
@media (max-width: 768px) {
    button,
    .btn,
    .btn-primary,
    .btn-secondary,
    .nav-item,
    a.menu-toggle {
        min-height: 44px;
    }
}

/* ---------- Responsive tables (card-style stacking on very small screens) ---------- */
@media (max-width: 640px) {
    .table-responsive,
    .data-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    table.stack-mobile {
        display: block;
        width: 100%;
    }
    table.stack-mobile thead {
        display: none;
    }
    table.stack-mobile tr {
        display: block;
        margin-bottom: 14px;
        border: 1px solid #e2e8e2;
        border-radius: 14px;
        padding: 10px 14px;
        background: #fff;
        box-shadow: 0 2px 6px rgba(0,0,0,0.04);
    }
    table.stack-mobile td {
        display: flex;
        justify-content: space-between;
        gap: 12px;
        padding: 8px 0;
        border: none;
        font-size: 14px;
    }
    table.stack-mobile td::before {
        content: attr(data-label);
        font-weight: 700;
        color: #2e7d32;
        flex: 0 0 45%;
    }
}

/* ---------- Grid & card responsive fallback ---------- */
@media (max-width: 900px) {
    .stats-grid,
    .summary-grid,
    .card-grid {
        grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)) !important;
        gap: 12px !important;
    }
}

@media (max-width: 480px) {
    .stats-grid,
    .summary-grid,
    .card-grid {
        grid-template-columns: 1fr !important;
    }

    .card {
        padding: 16px !important;
        border-radius: 14px !important;
    }

    .page-title {
        font-size: 18px !important;
    }
}

/* ---------- Horizontal-scroll charts / wide elements ---------- */
@media (max-width: 768px) {
    .chart-container,
    .wide-scroll {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* ---------- Buttons adapt to small screens ---------- */
@media (max-width: 480px) {
    .btn-primary,
    .btn-secondary {
        padding: 12px 18px !important;
        font-size: 14px !important;
        border-radius: 10px !important;
    }

    .action-buttons,
    .btn-group {
        flex-wrap: wrap;
        gap: 8px;
    }
}

/* ---------- Modals & dialogs fill screen on mobile ---------- */
@media (max-width: 480px) {
    .modal-content,
    .dialog-content {
        width: 94% !important;
        max-width: 100% !important;
        border-radius: 14px !important;
        padding: 18px !important;
    }
}

/* ---------- Top bar tweaks for mobile ---------- */
@media (max-width: 480px) {
    .top-bar {
        padding: 12px 14px !important;
    }
    .page-title {
        font-size: 16px !important;
    }
    .user-info {
        padding: 6px 10px !important;
        gap: 6px !important;
    }
    .user-info i { font-size: 24px !important; }
}

/* ---------- Install banner button (optional, manual trigger) ---------- */
.pwa-floating-install {
    position: fixed;
    right: 16px;
    bottom: max(16px, env(safe-area-inset-bottom));
    z-index: 990;
    background: linear-gradient(135deg, #2e7d32 0%, #1b5e20 100%);
    color: #fff;
    border: none;
    border-radius: 28px;
    padding: 12px 18px;
    font-weight: 700;
    box-shadow: 0 10px 24px rgba(46,125,50,0.4);
    cursor: pointer;
    display: none;
    align-items: center;
    gap: 8px;
    font-size: 14px;
}
.pwa-floating-install.visible { display: inline-flex; }
.pwa-floating-install:hover { transform: translateY(-2px); }

/* ---------- Printable / PWA standalone tweaks ---------- */
@media (display-mode: standalone) {
    .pwa-hide-in-app { display: none !important; }
    body { user-select: none; -webkit-user-select: none; }
    input, textarea, [contenteditable] { user-select: auto; -webkit-user-select: auto; }
}

/* ---------- Reduce motion for accessibility ---------- */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

/* ---------- Landscape small-screen spacing ---------- */
@media (max-height: 520px) and (orientation: landscape) {
    .sidebar-header { padding: 12px 16px; }
    .nav-item { padding: 10px 18px; }
}
