/* ==========================================================================
   Botifex - Shared Mobile / Responsive Layer
   --------------------------------------------------------------------------
   Loaded by the core authenticated dashboard pages (index, inventory,
   alerts, messages, settings, profile, analytics). Acts on the shared
   shell classes that every dashboard template duplicates:
     .sidebar / .sidebar-inner / .sidebar-nav / .sidebar-footer / .main
   plus the new mobile chrome:
     .mobile-topbar / .sidebar-scrim
   Desktop styles defined inline in each template are NOT modified.
   ========================================================================== */

/* The mobile topbar + scrim are hidden on desktop. */
.mobile-topbar { display: none; }
.sidebar-scrim { display: none; }

/* Prevent horizontal page scroll caused by stray wide elements on phones. */
@media (max-width: 900px) {
    html, body {
        max-width: 100%;
        overflow-x: hidden;
    }

    button,
    a,
    input,
    select,
    textarea {
        touch-action: manipulation;
    }
}

/* ==========================================================================
   TABLET / SMALL-LAPTOP (<= 900px) - sidebar becomes off-canvas drawer
   ========================================================================== */
@media (max-width: 900px) {
    body {
        display: block !important;
        flex-direction: column !important;
        min-height: 100vh;
    }

    /* Mobile topbar (injected just before .sidebar in each template) */
    .mobile-topbar {
        display: flex !important;
        align-items: center;
        gap: 12px;
        position: sticky;
        top: 0;
        left: 0;
        right: 0;
        z-index: 1200;
        height: 56px;
        padding: 0 14px;
        background: linear-gradient(180deg, rgba(10, 12, 24, 0.96), rgba(10, 12, 24, 0.88));
        border-bottom: 1px solid rgba(99, 102, 241, 0.22);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        color: #f8fafc;
        cursor: pointer;
        border: none;
        border-left: 0;
        border-right: 0;
        border-top: 0;
        width: 100%;
        font: inherit;
        text-align: left;
    }
    .mobile-topbar:focus-visible {
        outline: 2px solid #4a9eff;
        outline-offset: -2px;
    }
    .mobile-hamburger {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 48px;
        height: 48px;
        border-radius: 10px;
        background: rgba(74, 158, 255, 0.12);
        border: 1px solid rgba(74, 158, 255, 0.28);
        color: #e2e8f0;
        font-size: 18px;
        flex-shrink: 0;
    }
    .mobile-topbar-logo {
        height: 30px;
        width: auto;
    }
    .mobile-topbar-title {
        font-size: 15px;
        font-weight: 600;
        letter-spacing: 0.2px;
        color: #f1f5f9;
        margin-left: 4px;
    }
    .mobile-topbar-spacer { flex: 1; }

    /* Sidebar -> off-canvas drawer */
    .sidebar {
        position: fixed !important;
        top: 0;
        left: 0;
        bottom: 0;
        width: 84vw !important;
        max-width: 320px !important;
        min-width: 0 !important;
        height: 100vh;
        height: 100dvh;
        z-index: 1300;
        transform: translateX(-105%);
        transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
        border-radius: 0 22px 22px 0;
        box-shadow: 18px 0 48px rgba(0, 0, 0, 0.55);
        padding: 18px 18px 22px !important;
        overflow-y: auto;
        will-change: transform;
    }
    .sidebar.is-open {
        transform: translateX(0);
    }

    /* Scrim */
    .sidebar-scrim {
        display: block !important;
        position: fixed;
        inset: 0;
        background: rgba(4, 6, 14, 0.55);
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
        z-index: 1250;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.22s ease;
    }
    .sidebar-scrim.is-open {
        opacity: 1;
        pointer-events: auto;
    }
    /* When drawer is open, lock body scroll */
    body.sidebar-open {
        overflow: hidden;
    }

    /* Main content takes full width */
    .main, .main-content, main.main {
        width: 100% !important;
        margin-left: 0 !important;
        padding: 18px 14px !important;
        min-height: calc(100vh - 56px);
    }

    /* Tighten common dashboard chrome */
    .dashboard-page-header,
    .page-header {
        flex-direction: column !important;
        align-items: flex-start !important;
        gap: 12px !important;
    }
    .dashboard-greeting,
    .page-title {
        font-size: 22px !important;
        line-height: 1.25 !important;
    }
    .dashboard-subtitle,
    .page-subtitle {
        font-size: 13px !important;
    }
    .dashboard-page-header-right,
    .page-header-right {
        width: 100%;
        flex-wrap: wrap;
        gap: 8px;
    }

    /* Wide tables get horizontal scroll */
    .table-scroll,
    .responsive-table-wrap {
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    table.responsive-table {
        min-width: 560px;
    }

    /* Forms stack to single column */
    .form-grid,
    .two-col,
    .grid-2 {
        grid-template-columns: 1fr !important;
    }

    /* Tap targets */
    .sidebar-link,
    .sidebar-section-title,
    .btn,
    button,
    .pill,
    .chip {
        min-height: 48px;
    }
    .sidebar-link {
        padding: 10px 12px !important;
    }

    a:focus-visible,
    button:focus-visible,
    input:focus-visible,
    select:focus-visible,
    textarea:focus-visible {
        outline: none;
        box-shadow: 0 0 0 3px rgba(74, 158, 255, 0.45);
    }

    @media (hover: none) {
        .feature-card:hover,
        .pricing-card:hover,
        .stat-card:hover {
            transform: none !important;
        }
    }

    /* ROI dashboard metrics — responsive column layout */
    #roi-stats-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    }

    @media (min-width: 768px) {
        #roi-stats-grid {
            grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
        }
    }

    @media (min-width: 1025px) {
        #roi-stats-grid {
            grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
        }
    }

    /* Modals - cap to viewport, allow internal scroll */
    .modal,
    .modal-content,
    .listing-modal,
    .listing-modal-content,
    .dm-modal,
    .dialog,
    .popup-content {
        max-width: calc(100vw - 24px) !important;
        max-height: 92vh !important;
        max-height: 92dvh !important;
        overflow-y: auto;
    }
}

/* ==========================================================================
   PHONE (<= 600px) - tighter spacing + iOS-zoom-prevention on inputs
   ========================================================================== */
@media (max-width: 600px) {
    .main, .main-content, main.main {
        padding: 14px 12px !important;
    }

    /* Stop iOS Safari from zooming when focusing form fields */
    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="number"],
    input[type="search"],
    input[type="tel"],
    input[type="url"],
    input[type="date"],
    input[type="datetime-local"],
    input[type="time"],
    select,
    textarea {
        font-size: 16px !important;
        min-height: 48px;
    }

    .form-grid,
    .field-grid,
    .form-field,
    .input-group {
        gap: 12px;
    }

    .form-grid,
    .field-grid {
        display: grid;
        gap: 12px;
    }

    button,
    .btn,
    input[type="submit"],
    input[type="button"] {
        min-height: 48px;
    }

    .field-error {
        color: #ff6b6b;
        font-size: 0.875rem;
        line-height: 1.4;
        margin-top: 0.35rem;
    }

    input[aria-invalid="true"],
    select[aria-invalid="true"],
    textarea[aria-invalid="true"] {
        border-color: #ff6b6b !important;
    }

    /* Buttons go full-width inside grouped action rows */
    .form-actions .btn,
    .modal-actions .btn,
    .listing-modal-actions .btn,
    .stack-on-mobile .btn {
        width: 100%;
    }

    /* Notifications shouldn't take 300px on a 360px phone */
    .notification {
        left: 12px !important;
        right: 12px !important;
        max-width: none !important;
        top: 70px !important;
    }

    /* Common card grids collapse to 1 column */
    .feed-grid,
    .stats-grid,
    .cards-grid,
    .grid-3,
    .grid-4,
    .grid-auto {
        grid-template-columns: 1fr !important;
    }

    /* Tab pills become horizontally scrollable */
    .tabs,
    .tab-list,
    .pill-tabs,
    .nav-pills {
        display: flex !important;
        overflow-x: auto !important;
        flex-wrap: nowrap !important;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }
    .tabs::-webkit-scrollbar,
    .tab-list::-webkit-scrollbar,
    .pill-tabs::-webkit-scrollbar,
    .nav-pills::-webkit-scrollbar {
        display: none;
    }
    .tabs > *,
    .tab-list > *,
    .pill-tabs > *,
    .nav-pills > * {
        flex: 0 0 auto;
    }

    /* Hide footer logout / subscription label crowding under <=360 */
    .sidebar-tagline,
    .sidebar-credits-foot .sidebar-credits-link span:not(.icon) {
        font-size: 12px;
    }
}

/* ==========================================================================
   PRINT / REDUCED MOTION
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
    .sidebar,
    .sidebar-scrim {
        transition: none !important;
    }
}
