/* ============================================================
   APEX • Responsive Layer
   ============================================================
   One sheet to make every page work on phones, tablets and desktops.

   Breakpoints:
     ≥ 1280px  desktop                (default styles)
     ≤ 1280px  laptop / small desktop
     ≤ 1024px  tablet landscape
     ≤  900px  tablet portrait
     ≤  768px  large phone / phablet
     ≤  640px  phone
     ≤  420px  small phone
   ============================================================ */

/* ---------- Universal safety ---------- */
html { -webkit-text-size-adjust: 100%; }
body { overflow-x: hidden; }
img, video, svg { max-width: 100%; height: auto; }

/* ---------- Tap targets (touch-friendly) ---------- */
@media (hover: none) {
    .btn, .filter-tab, .nav-item, .stat-card--link,
    button:not(.btn-icon-sm) {
        min-height: 40px;
    }
    .btn-icon { min-width: 40px; min-height: 40px; }
}

/* ===========================================================
   LAPTOP  (≤ 1280px)
   =========================================================== */
@media (max-width: 1280px) {
    .stats-grid {
        grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    }
    .form-container {
        max-width: 100%;
    }
}

/* ===========================================================
   TABLET LANDSCAPE  (≤ 1024px)
   =========================================================== */
@media (max-width: 1024px) {
    /* Page paddings tighten */
    .page-content,
    .main-content > .container,
    .main-content > section,
    .main-content > div:not(.main-header):not(.flash-messages) {
        padding-left: 1rem !important;
        padding-right: 1rem !important;
    }

    /* Cards/grids collapse to two columns where they were many */
    .dashboard-grid,
    .ticket-layout,
    .settings-grid,
    .reports-grid {
        grid-template-columns: 1fr !important;
    }

    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }

    /* Two-column forms become one */
    .form-grid {
        grid-template-columns: 1fr !important;
    }

    /* Sidebar in ticket detail goes above */
    .ticket-sidebar { order: -1; }

    /* Filter tabs scroll horizontally rather than wrap awkwardly */
    .filter-tabs {
        max-width: 100%;
        overflow-x: auto;
        scrollbar-width: none;
    }
    .filter-tabs::-webkit-scrollbar { display: none; }
}

/* ===========================================================
   TABLET PORTRAIT  (≤ 900px)
   =========================================================== */
@media (max-width: 900px) {
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.75rem;
    }
    .stat-card .stat-icon {
        width: 44px; height: 44px; font-size: 1.25rem;
    }
    .stat-content h3 { font-size: 1.45rem; }

    /* KB articles 1 column */
    .kb-articles { gap: 0.75rem; }

    /* Bigger touch padding on table rows */
    .data-table td, .tickets-table td { padding: 0.85rem 0.65rem; }
}

/* ===========================================================
   PHABLET  (≤ 768px)
   =========================================================== */
@media (max-width: 768px) {
    html { font-size: 13.5px; }

    /* --- Sidebar slides in from the right (RTL) --- */
    .sidebar {
        position: fixed;
        top: 0; right: 0; bottom: 0;
        width: 86vw;
        max-width: 320px;
        z-index: 1100;
        transform: translateX(100%);
        transition: transform 0.28s cubic-bezier(0.22, 1, 0.36, 1);
        box-shadow: -16px 0 40px rgba(0, 0, 0, 0.35);
    }
    .sidebar.open { transform: translateX(0); }

    /* Hamburger only on small screens */
    .mobile-menu-btn {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        width: 40px; height: 40px;
        border-radius: 10px;
        background: var(--surface-muted, rgba(255, 255, 255, 0.08));
        color: var(--text-color, #fff);
        border: 1px solid var(--border, rgba(255, 255, 255, 0.12));
        cursor: pointer;
    }
    .mobile-menu-btn:active { transform: scale(0.96); }

    /* Backdrop when sidebar is open */
    body.sidebar-open::before {
        content: "";
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.55);
        backdrop-filter: blur(2px);
        z-index: 1090;
        animation: apex-fade-in 0.2s ease both;
    }
    @keyframes apex-fade-in { from { opacity: 0; } to { opacity: 1; } }

    /* Main content takes full width */
    .main-content { margin-right: 0 !important; width: 100%; }

    /* Header compresses */
    .main-header {
        padding: 0.6rem 0.85rem !important;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .page-title { font-size: 1.1rem !important; }
    .user-name { display: none; }   /* show only avatar */
    .header-left { gap: 0.4rem; }

    /* Page headers stack */
    .page-header,
    .page-header > .header-content {
        flex-direction: column;
        align-items: stretch !important;
        gap: 0.75rem !important;
    }

    /* Cards full-width */
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.65rem;
    }
    .stat-card { padding: 0.85rem; }
    .stat-card__arrow { display: none; }
    .stat-content h3 { font-size: 1.25rem; margin: 0; }
    .stat-content p { font-size: 0.78rem; }
    .stat-icon { width: 38px; height: 38px; font-size: 1.1rem; }

    /* Tables: horizontal scroll with a visual hint */
    .tickets-table,
    .data-table-wrap,
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .tickets-table table,
    .data-table {
        min-width: 720px;
    }

    /* Filter / search bar stacking */
    .header-filters,
    .filter-bar {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    .search-box,
    .search-box input { width: 100%; min-width: 0; }

    /* Forms */
    .form-grid,
    .form-row-2,
    .grid-2,
    .grid-3 { grid-template-columns: 1fr !important; gap: 1rem; }

    .form-card { padding: 1.25rem !important; }
    .form-actions { flex-direction: column; align-items: stretch; }
    .form-actions .btn { width: 100%; justify-content: center; }

    /* --- Modal: edge-to-edge on phones --- */
    .apex-modal-overlay { padding: 0.5rem; }
    .apex-modal {
        width: 100%;
        max-width: 100%;
        max-height: 92vh;
        border-radius: 14px;
    }

    .print-modal-overlay { padding: 0; align-items: stretch; }
    .print-modal {
        width: 100%; max-width: 100%;
        max-height: 100vh; height: 100vh;
        border-radius: 0;
        display: flex; flex-direction: column;
    }
    .print-modal-body { flex: 1; overflow-y: auto; }
    .print-modal-footer { flex-wrap: wrap; }
    .print-modal-footer .btn { flex: 1 1 100px; justify-content: center; }
    .print-row,
    .print-columns-grid { grid-template-columns: 1fr !important; }
    .print-options-row,
    .print-format-options { gap: 0.5rem; }
    .format-label { min-width: 0; padding: 0.75rem; flex: 1 1 30%; }

    /* Bulk action bar wraps */
    .bulk-action-bar { flex-direction: column; align-items: stretch; gap: 0.6rem; padding: 0.75rem; }
    .bulk-actions { flex-wrap: wrap; gap: 0.45rem; }

    /* Notification dropdown becomes drawer */
    .notification-menu,
    .user-menu {
        position: fixed !important;
        top: auto !important; bottom: 0 !important;
        right: 0 !important; left: 0 !important;
        width: 100% !important;
        max-width: 100% !important;
        border-radius: 18px 18px 0 0 !important;
        max-height: 70vh;
    }
}

/* ===========================================================
   PHONE  (≤ 640px)
   =========================================================== */
@media (max-width: 640px) {
    html { font-size: 13px; }

    .stats-grid { grid-template-columns: 1fr 1fr; }

    /* KB editor: a bit more breathing room */
    .editor-wrapper { border-radius: 12px; }
    .ql-editor { padding: 1.1rem !important; min-height: 300px !important; }
    .ql-toolbar.ql-snow { padding: 0.5rem !important; }
    .ql-toolbar .ql-formats { margin: 4px !important; }

    /* Big hero blocks lighter */
    .article-hero { padding: 1.4rem !important; }
    .article-hero h1 { font-size: 1.25rem !important; }
    .category-header { padding: 1.4rem !important; }
    .category-header h1 { font-size: 1.25rem; }

    /* KB index / category list spacing */
    .kb-category-card { padding: 1rem; }
    .kb-categories { gap: 0.65rem; grid-template-columns: 1fr 1fr; }
    .article-card { padding: 1rem; }
    .article-content { padding: 1.2rem !important; }

    /* Media pages */
    .media-grid { grid-template-columns: 1fr !important; }
    .media-hero { padding: 1.4rem 1rem !important; }
    .media-view__share { flex-wrap: wrap; }
    .media-view__player { border-radius: 10px; }
    video.media-video, .media-player video { max-height: 60vh; }

    /* Landing hero */
    .landing-hero,
    .hero-section {
        padding: 2rem 1rem !important;
        text-align: center;
    }
    .landing-hero h1, .hero-section h1 { font-size: 1.7rem !important; line-height: 1.4; }
    .landing-hero .subtitle, .hero-description { font-size: 0.95rem !important; }
    .landing-hero-actions, .hero-actions {
        flex-direction: column; gap: 0.6rem; align-items: stretch;
    }
    .landing-hero-actions .btn,
    .hero-actions .btn { width: 100%; justify-content: center; }

    /* Auth (login/register/forgot) cards */
    .auth-card, .auth-wrap {
        width: 100% !important;
        max-width: 100% !important;
        margin: 0 !important;
        border-radius: 0 !important;
        min-height: 100vh;
    }

    /* Settings tabs scroll horizontally */
    .settings-tabs, .tabs {
        overflow-x: auto;
        flex-wrap: nowrap !important;
        scrollbar-width: none;
    }
    .settings-tabs::-webkit-scrollbar,
    .tabs::-webkit-scrollbar { display: none; }
    .settings-tab, .tab-button { flex-shrink: 0; }

    /* Ticket detail messages */
    .message-content { padding: 0.7rem 0.85rem !important; }
    .message-body { font-size: 0.95rem; }
    .message-avatar { width: 32px !important; height: 32px !important; }

    /* Toast notifications full-width on phones */
    .apex-toast-stack {
        left: 0.5rem !important;
        right: 0.5rem !important;
        bottom: 0.5rem !important;
        top: auto !important;
        width: auto !important;
    }
    .apex-toast { width: 100%; }

    /* Filter chips smaller */
    .filter-tab { padding: 0.45rem 0.75rem; font-size: 0.85rem; }
}

/* ===========================================================
   SMALL PHONE  (≤ 420px)
   =========================================================== */
@media (max-width: 420px) {
    .stats-grid { grid-template-columns: 1fr; }
    .kb-categories { grid-template-columns: 1fr; }

    .page-title { font-size: 1rem !important; }
    .notification-btn,
    .user-btn { padding: 0.35rem !important; }

    .stat-content h3 { font-size: 1.5rem; }
    .stat-card { padding: 1rem; }
    .stat-icon { width: 44px; height: 44px; }
}

/* ===========================================================
   Landscape phone — keep height usable
   =========================================================== */
@media (max-width: 920px) and (orientation: landscape) and (max-height: 480px) {
    .main-header { position: sticky; top: 0; z-index: 50; }
    .auth-card .auth-hero, .auth-hero { display: none; }
}

/* ===========================================================
   Reduced-motion users
   =========================================================== */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.001s !important;
        transition-duration: 0.001s !important;
    }
}
