/* ───────────────────────────────────────────────────────────────────
   Spirit Sight — Mobile & Responsive Enhancements
   Touch-optimized controls, responsive layouts, and mobile breakpoints
   for field operations (barrel scanning, sample collection, gauging).
   ─────────────────────────────────────────────────────────────────── */

/* ── Touch-friendly tap targets (minimum 44px per WCAG 2.5.5) ───── */
@media (pointer: coarse) {
    .ss-btn,
    .ss-tab,
    button,
    a.nav-link,
    .sidebar-nav-item {
        min-height: 44px;
        min-width: 44px;
        padding: 0.625rem 1rem;
    }

    .ss-table td,
    .ss-table th {
        padding: 0.75rem 0.5rem;
    }

    input, select, textarea {
        min-height: 44px;
        font-size: 16px; /* Prevents iOS zoom */
    }

    .ss-badge {
        padding: 0.375rem 0.75rem;
    }
}

/* ── Small screens (phones) ─────────────────────────────────────── */
@media screen and (max-width: 768px) {
    /* Sidebar, overlay, and main content handled by app.css */

    /* Module header stacks on mobile */
    .ss-module-header {
        flex-direction: column;
        gap: 0.75rem;
        align-items: flex-start;
    }

    .ss-module-header__actions {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .ss-module-header__actions .ss-btn {
        flex: 1 1 auto;
        text-align: center;
    }

    /* Stat cards single column on narrow screens */
    .ss-metric-row {
        grid-template-columns: 1fr 1fr;
    }

    /* Tables scroll horizontally */
    .ss-table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .ss-table thead {
        white-space: nowrap;
    }

    /* Tabs scroll horizontally */
    .ss-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        flex-wrap: nowrap;
        scrollbar-width: none;
    }

    .ss-tabs::-webkit-scrollbar {
        display: none;
    }

    .ss-tab {
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* Legacy modals full-width on mobile. The design-system .ss-modal is
       intentionally excluded here: it renders as a bottom sheet via modal.css
       (this !important block would otherwise force a centered 95vw card). */
    .modal-content,
    .ss-modal-content {
        width: 95vw !important;
        max-width: none !important;
        margin: 1rem auto !important;
        max-height: 90vh;
        overflow-y: auto;
    }

    /* Form fields full width */
    .form-group,
    .ss-form-group {
        width: 100%;
    }

    .form-row,
    .ss-form-row {
        flex-direction: column;
    }

    /* Hide non-essential columns on mobile */
    .hide-mobile {
        display: none !important;
    }
}

/* ── Medium screens (tablets) ────────────────────────────────────── */
@media screen and (min-width: 769px) and (max-width: 1024px) {
    .ss-metric-row {
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    }

    .ss-module-header__actions {
        flex-wrap: wrap;
    }
}

/* ── Small phones ────────────────────────────────────────────────── */
@media screen and (max-width: 480px) {
    /* Single-column KPIs so cards stay legible on a phone */
    .ss-metric-row {
        grid-template-columns: 1fr;
    }
    /* Tighter content gutters to reclaim width */
    .module-content {
        padding-left: var(--spacing-3);
        padding-right: var(--spacing-3);
    }
}

/* ── Ultra-wide / 4K: keep dense dashboards from stretching too far ── */
@media screen and (min-width: 2200px) {
    .ss-metric-row {
        grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    }
}

/* ── PWA install banner ──────────────────────────────────────────── */
.pwa-install-banner {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--color-surface-elevated, #2a2a4a);
    color: var(--color-text-primary);
    padding: 1rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    z-index: 10000;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.3);
    transform: translateY(100%);
    transition: transform 0.3s ease;
}

.pwa-install-banner.visible {
    transform: translateY(0);
}

.pwa-install-banner__text {
    flex: 1;
}

.pwa-install-banner__text strong {
    display: block;
    margin-bottom: 0.25rem;
}

.pwa-install-banner__text small {
    color: var(--color-text-muted);
}

/* ── Offline indicator ───────────────────────────────────────────── */
.offline-indicator {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: var(--color-warning, #f59e0b);
    color: #000;
    text-align: center;
    padding: 0.375rem;
    font-size: 0.8125rem;
    font-weight: 600;
    z-index: 10001;
    /* visibility gate too: translateY(-100%) alone leaves a 1px sliver of
       the accent bar at the viewport top edge (subpixel rounding). */
    transform: translateY(-100%);
    visibility: hidden;
    transition: transform 0.3s ease, visibility 0.3s;
}

.offline-indicator.visible {
    transform: translateY(0);
    visibility: visible;
}

/* ── Landscape phone adjustments ─────────────────────────────────── */
@media screen and (max-height: 500px) and (orientation: landscape) {
    .ss-module-header {
        padding: 0.5rem 0;
    }

    .ss-metric-row {
        grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        gap: 0.5rem;
    }

    .ss-metric {
        padding: 0.5rem;
    }
}

/* ═══════════════════════════════════════════════════════════════════
   MOBILE CONTROL COLLAPSING (shared; see utils/mobileControls.js)
   Desktop defaults keep the transform invisible: the injected toggle +
   More trigger are hidden and the moved buttons lay out inline via
   display:contents, so >768px renders exactly as before.
   ═══════════════════════════════════════════════════════════════════ */

/* Desktop (default): transform is presentation-neutral. */
.ss-mobile-filters-toggle { display: none; }
.js-mobile-collapsible { /* no collapse above the mobile breakpoint */ }
.ss-mobile-more { display: contents; }        /* wrapper vanishes from layout */
.ss-mobile-more__trigger { display: none; }
.ss-mobile-more__menu { display: contents; }  /* moved buttons stay inline */

@media screen and (max-width: 768px) {
    /* Header "More" overflow: primary stays inline, rest behind a menu. */
    .ss-mobile-more { display: inline-flex; position: relative; }
    .ss-mobile-more__trigger {
        display: inline-flex;
        align-items: center;
        gap: 4px;
    }
    .ss-mobile-more__menu {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: var(--spacing-1);
        position: absolute;
        top: calc(100% + 4px);
        left: 0;
        min-width: 200px;
        max-width: 70vw;
        z-index: var(--z-dropdown);
        padding: var(--spacing-2);
        background: var(--color-bg-primary);
        border: 1px solid var(--color-border-primary);
        box-shadow: var(--shadow-lg);
    }
    .ss-mobile-more__menu[hidden] { display: none; }
    .ss-mobile-more__menu > button,
    .ss-mobile-more__menu > a { width: 100%; justify-content: flex-start; }

    /* Filters: collapsed by default, revealed by the toggle. Collapse via
       max-height so the inner grid/flex layout is preserved when open. */
    .ss-mobile-filters-toggle {
        display: flex;
        align-items: center;
        gap: var(--spacing-2);
        width: 100%;
        padding: var(--spacing-2) var(--spacing-3);
        margin-bottom: var(--spacing-2);
        background: var(--color-bg-primary);
        border: 1px solid var(--color-border-primary);
        border-radius: 2px;
        color: var(--color-text-primary);
        font-size: var(--font-size-sm);
        font-weight: var(--font-weight-semibold);
        cursor: pointer;
    }
    .ss-mobile-filters-toggle__count {
        margin-left: auto;
        min-width: 20px;
        padding: 0 6px;
        text-align: center;
        background: var(--color-bg-tertiary);
        border-radius: 10px;
        font-size: var(--font-size-2xs);
        color: var(--color-text-secondary);
    }
    .ss-mobile-filters-toggle.is-open svg { transform: rotate(180deg); }
    .js-mobile-collapsible {
        max-height: 0;
        overflow: hidden;
        /* Zero the container's own box so padding/border leave no residual
           height while collapsed (max-height caps content, not padding). */
        padding-top: 0 !important;
        padding-bottom: 0 !important;
        margin-top: 0 !important;
        margin-bottom: 0 !important;
        border-top-width: 0 !important;
        border-bottom-width: 0 !important;
        transition: max-height 0.2s ease;
    }
    .js-mobile-collapsible.is-open {
        max-height: 1200px;
        padding-top: revert !important;
        padding-bottom: revert !important;
    }

    /* When open, stack the controls full width for tap targets. */
    .js-mobile-collapsible.is-open > * { margin-bottom: var(--spacing-2); }
}
