/* ============================================================
   RESPONSIVE LAYOUT — Phase R (Mobile + PWA)
   3 breakpoints: Mobile (≤768px), Tablet (769-1024px), Desktop (>1024px)
   ============================================================ */

/* ── Overlay for mobile sidebar ─────────────────────────────── */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
    backdrop-filter: blur(2px);
    transition: opacity var(--transition-normal);
}
.sidebar-overlay.active {
    display: block;
    animation: fadeIn 0.2s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ═══════════════════════════════════════════════════════════════
   TABLET (≤1024px) — Collapse sidebar to icons only
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 1024px) {
    :root {
        --sidebar-width: 64px;
    }

    .sidebar {
        padding: var(--space-4) var(--space-2);
        align-items: center;
        overflow: hidden;
    }

    .logo-text,
    .nav-label,
    .sidebar-bottom .btn-pause span,
    .sidebar-bottom .status-pill-text {
        display: none;
    }

    .logo {
        justify-content: center;
        padding: 0;
        margin-bottom: var(--space-4);
    }

    .nav-item {
        justify-content: center;
        padding: var(--space-2);
        min-width: 44px;
        min-height: 44px;
    }

    .nav-menu {
        align-items: center;
    }

    .btn-pause {
        width: 44px;
        height: 44px;
        padding: 0;
        border-radius: 50%;
        font-size: 0;
    }
    .btn-pause svg,
    .btn-pause i {
        font-size: var(--text-base);
    }

    /* Dashboard grids */
    .page {
        max-width: 100%;
    }

    .grid-3 {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .flex-row {
        flex-direction: column !important;
    }

    /* Scheduler stats */
    .scheduler-stats {
        flex-wrap: wrap;
    }

    /* Sentiment layout */
    .sentiment-layout {
        grid-template-columns: 1fr !important;
    }
}

/* ═══════════════════════════════════════════════════════════════
   MOBILE (≤768px) — Full overlay sidebar + bottom nav
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {
    :root {
        --sidebar-width: 280px;
        --header-height: 56px;
    }

    /* Sidebar: hidden by default, slides from left */
    .sidebar {
        transform: translateX(-100%);
        padding: var(--space-6) var(--space-4);
        align-items: stretch;
        overflow-y: auto;
        box-shadow: var(--shadow-lg);
        width: var(--sidebar-width);
    }
    .sidebar.open {
        transform: translateX(0);
    }

    /* Show all sidebar labels when open */
    .sidebar.open .logo-text,
    .sidebar.open .nav-label,
    .sidebar.open .btn-pause span {
        display: inline;
    }
    .sidebar.open .nav-item {
        justify-content: flex-start;
        padding: var(--space-3) var(--space-4);
    }
    .sidebar.open .logo {
        justify-content: flex-start;
        padding: 0 var(--space-3);
    }
    .sidebar.open .btn-pause {
        width: 100%;
        height: auto;
        padding: var(--space-3) var(--space-4);
        border-radius: var(--radius-md);
        font-size: var(--text-base);
    }

    /* Content: full width */
    .content-wrap {
        margin-left: 0 !important;
        padding-bottom: 68px; /* Space for bottom nav */
    }

    /* Show hamburger button */
    .hamburger {
        display: flex !important;
    }

    /* Header adjustments */
    .header {
        padding: 0 var(--space-3);
        height: var(--header-height);
    }
    .header-left h1 {
        font-size: var(--text-lg);
    }
    .user-name {
        display: none;
    }
    .user-pill {
        padding: var(--space-1);
    }

    /* Show bottom nav */
    .bottom-nav {
        display: block !important;
    }

    /* Hide footer on mobile (replaced by bottom nav) */
    .footer {
        display: none;
    }

    /* Main content padding */
    .main {
        padding: var(--space-3);
    }

    /* Dashboard KPIs: 2 columns */
    #dash-kpis {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: var(--space-2) !important;
    }

    /* Dashboard: Single column layout */
    .page > div > div[style*="grid-template-columns: 2fr 1fr"] {
        display: flex !important;
        flex-direction: column !important;
    }

    /* Cards */
    .card {
        border-radius: var(--radius-md);
    }

    /* Grid overrides */
    .grid-3 {
        grid-template-columns: 1fr !important;
    }

    /* Tables: horizontal scroll */
    .tbl-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .tbl {
        min-width: 600px;
    }

    /* Bulk config grid */
    .bulk-cfg-grid {
        grid-template-columns: 1fr !important;
    }

    /* Analytics grid */
    .analytics-grid {
        grid-template-columns: 1fr !important;
    }

    /* Form fields */
    .field-input, .field-select, .field-textarea {
        font-size: 16px !important; /* Prevent iOS zoom on focus */
    }

    /* Buttons: larger touch targets */
    .btn {
        min-height: 44px;
        min-width: 44px;
    }

    /* Page selector dropdown */
    .page-selector-dropdown {
        min-width: 200px;
    }

    /* Modal: full width on mobile */
    .modal-content,
    .sh-modal-content {
        width: 95vw !important;
        max-width: none !important;
        max-height: 85vh !important;
        margin: var(--space-3);
        border-radius: var(--radius-lg);
    }

    /* Scheduler */
    .scheduler-header {
        flex-direction: column;
        gap: var(--space-2);
    }
    .scheduler-tabs {
        overflow-x: auto;
        scrollbar-width: none;
    }
    .scheduler-tabs::-webkit-scrollbar { display: none; }

    /* Calendar grid */
    .cal-grid {
        font-size: var(--text-xs);
    }

    /* Queue items */
    .queue-item {
        grid-template-columns: 1fr !important;
    }

    /* Sentiment layout */
    .sentiment-layout {
        grid-template-columns: 1fr !important;
    }

    /* Listen filters */
    .hist-filters {
        overflow-x: auto;
        flex-wrap: nowrap;
        scrollbar-width: none;
    }
    .hist-filters::-webkit-scrollbar { display: none; }
}

/* ═══════════════════════════════════════════════════════════════
   SMALL MOBILE (≤480px) — Extra compact
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 480px) {
    .header-right .icon-btn:not(#notif-btn):not(#theme-toggle) {
        display: none;
    }

    /* 1 column KPIs */
    #dash-kpis {
        grid-template-columns: 1fr !important;
    }

    /* Smaller headings */
    h1 { font-size: var(--text-lg) !important; }
    h2 { font-size: var(--text-base) !important; }

    /* Stack date range buttons */
    #dash-range-btns {
        flex-wrap: wrap;
    }
    #dash-range-btns button {
        flex: 1;
        min-width: unset;
    }

    /* Compact page padding */
    .main {
        padding: var(--space-2);
    }

    /* Sent summary metrics stack */
    .sent-summary {
        flex-direction: column;
    }
}

/* ═══════════════════════════════════════════════════════════════
   TOUCH-FRIENDLY ENHANCEMENTS
   ═══════════════════════════════════════════════════════════════ */
@media (hover: none) and (pointer: coarse) {
    /* Remove hover effects on touch devices */
    .nav-item:hover {
        background: transparent;
    }
    .nav-item.active:hover {
        background: color-mix(in srgb, var(--color-accent) 12%, transparent);
    }

    /* Larger tap targets */
    .nav-item {
        min-height: 48px;
    }
    .icon-btn {
        min-width: 48px;
        min-height: 48px;
    }

    /* Smooth scrolling */
    .nav-menu,
    .main,
    .tbl-wrap {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }
}

/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE HARDENING — Phone + Tablet full-visibility pass
   Phase A: Auto-Reply page + page/account scope selector
   (fix clipping & horizontal overflow on iPhone/Android/tablet)
   ═══════════════════════════════════════════════════════════════ */

/* ── Global overflow guard: nothing should force the page wider than the
   viewport on touch devices. Tables keep their own .tbl-wrap scroll. ── */
@media (max-width: 1024px) {
    html, body { overflow-x: hidden; }
    .content-wrap { max-width: 100vw; overflow-x: hidden; }
    .main { max-width: 100%; }
    /* Flex/grid children may overflow unless allowed to shrink */
    .card, .nav-group-body, #auto-reply-container, #hideall-card { min-width: 0; }
    /* Media/preview never push width */
    .main img, .main video, .main canvas, .main iframe { max-width: 100%; height: auto; }
    /* Long unbreakable strings (page names, URLs) wrap instead of overflowing */
    .ha-pages-grid label span, .nav-label { overflow-wrap: anywhere; }
}

/* ── Phone (<640px) ── */
@media (max-width: 639px) {
    /* Tighter container padding so cards use full width without clipping */
    #auto-reply-container,
    #access-mgmt-container,
    #ao-container,
    #qh-container { padding: 12px !important; }

    /* Page/account selection grid → single column, even boxes */
    .ha-pages-grid { grid-template-columns: 1fr !important; max-height: 50vh !important; }
    .ha-pages-grid label { min-height: 44px; }

    /* Auto-Reply header: stack title + actions; actions wrap full-width so
       "+ Tạo rule mới" is never clipped by the overflow guard */
    .ar-header { flex-direction: column; align-items: stretch; }
    .ar-header-actions { flex-wrap: wrap; width: 100%; gap: 10px 14px; justify-content: space-between; }
    .ar-header-actions #btn-import-rules { margin-right: 0 !important; }
    .ar-header-actions #btn-new-rule { flex: 1 1 auto; }

    /* Scope radios stack vertically with breathing room */
    .ha-scope-radios { flex-direction: column; gap: 10px; }

    /* Auto-reply rule cards: stack content full-width, then an even bottom row
       holding toggle (left) + edit/delete (right) — no more cramped side column. */
    #rules-list .card { overflow: hidden; }
    #rules-list .card > div { flex-direction: column; align-items: stretch; gap: 10px; }
    #rules-list .card > div > div:last-child {
        flex-direction: row !important;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        margin-top: 2px;
        padding-top: 12px;
        border-top: 1px solid var(--color-border);
    }
    /* keep the edit/delete buttons grouped on the right of that bottom row */
    #rules-list .card > div > div:last-child > div { gap: 8px; }
}

/* ── Tablet (640–1023px): 2-col page grid only when it truly fits ── */
@media (min-width: 640px) and (max-width: 1023px) {
    .ha-pages-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)) !important; }
}

/* ═══════════════════════════════════════════════════════════════
   Phase C — Tables / dense lists on phone
   Any table becomes a self-contained horizontal scroller (cells keep
   alignment via anonymous table boxes). This is the prompt's allowed
   fallback when a full card transform isn't feasible across all modules.
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 639px) {
    .main table { display: block; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .main table th, .main table td { white-space: nowrap; }
    /* Card-list grids collapse to one readable column */
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr !important; }
}

/* ═══════════════════════════════════════════════════════════════
   Phase D — Dashboard / Reports KPI + chart cards
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 639px) {
    .kpi-grid { grid-template-columns: repeat(2, 1fr) !important; }
    .exec-kpi-strip { grid-template-columns: repeat(2, 1fr) !important; }
    .main canvas { max-width: 100% !important; }
}
@media (max-width: 400px) {
    .kpi-grid, .exec-kpi-strip { grid-template-columns: 1fr !important; }
}

/* ═══════════════════════════════════════════════════════════════
   Phase B — Smart Inbox: list-first flow on phone
   List on top (readable height), thread detail below; reply controls
   clear of the bottom nav. (JS smooth-scrolls to the thread on select.)
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    .inbox-split { grid-template-columns: 1fr !important; max-height: none !important; }
    .inbox-list { max-height: 46vh !important; }
    .inbox-detail { max-height: none !important; padding-bottom: 76px; }
    .inbox-filters { gap: 8px; }
    .inbox-filters > * { flex: 1 1 auto; min-width: 0; }
}

/* ═══════════════════════════════════════════════════════════════
   Phase E — Floating widgets must not cover content/controls
   Auto-hidden while scrolling or while an input is focused (JS toggles
   body.widgets-hidden / body.input-focused). Also already hidden when
   the sidebar drawer is open.
   ═══════════════════════════════════════════════════════════════ */
@media (max-width: 767px) {
    #presence-bar, #ai-chatbot-widget { transition: opacity .2s ease, transform .2s ease; }
    body.widgets-hidden #presence-bar,
    body.widgets-hidden #ai-chatbot-widget,
    body.input-focused #presence-bar,
    body.input-focused #ai-chatbot-widget {
        opacity: 0; pointer-events: none; transform: translateY(10px);
    }
}

/* ═══════════════════════════════════════════════════════════════
   PWA STANDALONE MODE — When installed as app
   ═══════════════════════════════════════════════════════════════ */
@media (display-mode: standalone) {
    /* Safe area insets for notch devices */
    .header {
        padding-top: env(safe-area-inset-top);
        height: calc(var(--header-height) + env(safe-area-inset-top));
    }
    .bottom-nav {
        padding-bottom: env(safe-area-inset-bottom);
        height: calc(60px + env(safe-area-inset-bottom));
    }
    .sidebar {
        padding-top: calc(var(--space-6) + env(safe-area-inset-top));
    }
}
