/**
 * Metricly — phone & tablet layout
 * Loaded after theme-modern.css. Page-inline styles that conflict should
 * also include matching @media blocks so they do not override these rules.
 */

/* —— Shared mobile performance —— */
@media (max-width: 900px), (hover: none) and (pointer: coarse) {
    .login-card {
        /* backdrop-filter is expensive while scrolling under fixed wallpaper */
        backdrop-filter: none !important;
        -webkit-backdrop-filter: none !important;
        background: rgba(10, 10, 10, 0.94) !important;
    }

    .hub-card {
        transition: border-color 0.15s ease, box-shadow 0.15s ease !important;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }

    .hub-card:hover,
    .hub-card:active {
        transform: none !important;
    }

    .tree-dept-row,
    .tree-item,
    .btn-report-action,
    .header-user-trigger,
    .sidebar-toggle-btn {
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }

    .hub-card--branded:hover {
        transition: none !important;
    }

    .hub-bg-layer.has-animated-default .metricly-wallpaper {
        transition: none !important;
    }
}

/* —— Shared header —— */
@media (max-width: 900px) {
    header {
        padding: 0.65rem 0.85rem;
    }

    .header-container {
        gap: 0.5rem;
    }

    .header-logo-image {
        max-height: 40px !important;
        max-width: min(160px, 42vw) !important;
    }

    .header-actions {
        gap: 0.5rem !important;
    }

    .header-link {
        font-size: 0.8125rem !important;
        margin-right: 0.35rem !important;
    }

    .header-user-wrap {
        margin-left: 0.25rem;
    }

    .header-user-name {
        display: none;
    }

    .header-user-trigger--labeled {
        max-width: none;
    }
}

@media (max-width: 640px) {
    .header-actions .header-link {
        display: none;
    }

    .header-logo-image {
        max-height: 36px !important;
        max-width: min(140px, 48vw) !important;
    }

    .header-user-dropdown {
        min-width: 180px;
        right: -0.25rem;
    }
}

/* —— Login / password pages —— */
@media (max-width: 480px) {
    .login-page {
        padding: 1rem 0.75rem 1.5rem;
        align-items: flex-start;
        padding-top: max(1.25rem, env(safe-area-inset-top));
    }

    .login-card {
        padding: 1.5rem 1.15rem 1.35rem !important;
        border-radius: 14px !important;
    }

    .login-logo {
        max-height: 56px;
        margin-bottom: 1rem;
    }
}

/* —— Hub (home / favourites) —— */
@media (max-width: 900px) {
    .hub-content {
        padding: 1.25rem 1rem !important;
    }

    .hub-grid {
        grid-template-columns: repeat(auto-fill, minmax(min(100%, 180px), 1fr)) !important;
        gap: 1rem !important;
    }

    .hub-card {
        min-height: 100px;
        padding: 1.25rem 1rem;
    }

    .hub-empty-state {
        padding: 3rem 1rem 1.5rem;
    }
}

@media (max-width: 480px) {
    .hub-grid {
        grid-template-columns: 1fr !important;
    }

    .hub-card {
        min-height: 88px;
    }
}

/* —— Report layout: drawer sidebar on tablet/phone —— */
.mobile-sidebar-open-btn {
    display: none;
}

.sidebar-backdrop {
    display: none;
}

@media (max-width: 900px) {
    body.reports-page:has(.report-layout) {
        height: 100dvh;
        min-height: 100dvh;
        max-height: 100dvh;
        overflow: hidden;
    }

    .reports-page:has(.report-layout) .reports-main {
        overflow: hidden;
        min-height: 0;
    }

    .report-layout {
        position: relative;
        flex: 1;
        min-height: 0;
        overflow: hidden;
    }

    .reports-sidebar {
        position: fixed !important;
        top: 0;
        left: 0;
        bottom: 0;
        width: min(300px, 86vw) !important;
        min-width: 0 !important;
        max-width: 300px;
        z-index: 200;
        border-right: 1px solid var(--border-color);
        box-shadow: 8px 0 32px rgba(0, 0, 0, 0.45);
        transform: translate3d(0, 0, 0);
        transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
        padding-top: env(safe-area-inset-top);
        will-change: transform;
        -webkit-overflow-scrolling: touch;
        backface-visibility: hidden;
        display: flex;
        flex-direction: column;
        overflow: hidden;
    }

    .reports-sidebar .tree-menu {
        flex: 1 1 auto;
        min-height: 0;
        overflow-x: hidden;
        overflow-y: auto;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }

    .report-layout.sidebar-collapsed .reports-sidebar {
        width: min(300px, 86vw) !important;
        min-width: 0 !important;
        transform: translate3d(-105%, 0, 0);
        visibility: hidden;
        pointer-events: none;
        box-shadow: none;
        will-change: auto;
    }

    /* Drawer always shows full nav (ignore desktop rail collapse) */
    .report-layout.sidebar-collapsed .reports-sidebar-head {
        justify-content: flex-start;
        padding: 0.75rem 0.65rem 0.75rem 1rem;
    }

    .report-layout.sidebar-collapsed .reports-sidebar-logo {
        flex: 1;
        width: auto;
        max-width: none;
        opacity: 1;
        pointer-events: auto;
    }

    .report-layout.sidebar-collapsed .reports-sidebar .tree-menu {
        opacity: 1;
        pointer-events: auto;
        overflow-y: auto;
    }

    .report-layout.sidebar-collapsed .sidebar-toggle-btn svg {
        transform: none;
    }

    .sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        z-index: 190;
        background: rgba(0, 0, 0, 0.55);
        opacity: 1;
        transition: opacity 0.2s ease;
        border: none;
        padding: 0;
        cursor: pointer;
        -webkit-tap-highlight-color: transparent;
    }

    .report-layout.sidebar-collapsed .sidebar-backdrop {
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
    }

    .mobile-sidebar-open-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        gap: 0.4rem;
        position: absolute;
        top: 0.65rem;
        left: 0.65rem;
        z-index: 5;
        padding: 0.45rem 0.7rem;
        border: 1px solid var(--border-color);
        border-radius: 8px;
        background: rgba(10, 10, 10, 0.92);
        color: var(--text-primary);
        font-family: inherit;
        font-size: 0.8125rem;
        font-weight: 600;
        cursor: pointer;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
        /* Solid fill — blur over iframe/wallpaper stutters on mobile GPUs */
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        -webkit-tap-highlight-color: transparent;
        touch-action: manipulation;
    }

    .mobile-sidebar-open-btn svg {
        width: 18px;
        height: 18px;
        flex-shrink: 0;
    }

    .report-layout:not(.sidebar-collapsed) .mobile-sidebar-open-btn {
        display: none;
    }

    .report-iframe-wrap {
        flex: 1;
        min-width: 0;
        width: 100%;
        position: relative;
    }

    .report-iframe-wrap iframe {
        min-height: 0 !important;
        height: 100%;
    }

    .report-iframe-wrap > .hub-content {
        padding-top: 3.25rem !important;
    }

    .report-iframe-wrap > .report-placeholder {
        padding-top: 3.5rem;
    }

    .report-favorite-bar {
        flex-wrap: wrap;
        padding: 0.5rem 0.75rem 0.5rem 3.25rem;
        gap: 0.4rem;
    }

    .report-favorite-bar .btn-report-action span {
        display: none;
    }

    .report-favorite-bar .btn-report-action {
        padding: 0.45rem;
    }

    body.sidebar-drawer-open {
        overflow: hidden;
    }

    .report-placeholder {
        padding: 1.5rem 1rem;
        font-size: 0.9375rem;
    }

    .feedback-modal-box {
        padding: 1.15rem;
        max-height: min(90vh, 640px);
        overflow-y: auto;
    }

    .feedback-modal-box .modal-actions {
        flex-wrap: wrap;
    }

    .feedback-modal-box .modal-actions .btn-primary,
    .feedback-modal-box .modal-actions .btn-secondary {
        flex: 1 1 auto;
        min-width: 7rem;
    }
}

@media (max-width: 640px) {
    .report-favorite-bar {
        padding-left: 3rem;
    }

    .tree-item-report-wrap {
        padding-left: 2.5rem;
        padding-right: 0.75rem;
    }

    .tree-item-report {
        padding-left: 2.5rem;
    }
}

/* —— Admin —— */
@media (max-width: 900px) {
    .admin-main {
        padding: 1rem 0.85rem 2rem !important;
        max-width: 100%;
    }

    .admin-header {
        gap: 0.75rem;
    }

    .admin-header h1 {
        font-size: 1.25rem !important;
    }

    .admin-tabs,
    .admin-subnav,
    .admin-section-nav {
        flex-wrap: nowrap !important;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
        gap: 0 !important;
        margin-left: -0.85rem;
        margin-right: -0.85rem;
        padding-left: 0.85rem;
        padding-right: 0.85rem;
        border-bottom: 1px solid var(--border-color);
    }

    .admin-tabs a,
    .admin-subnav a,
    a.admin-tab,
    .admin-section-nav a {
        flex: 0 0 auto;
        white-space: nowrap;
        padding: 0.65rem 0.75rem !important;
        font-size: 0.8125rem !important;
    }

    .admin-users-table-card,
    .admin-data-table-card {
        overflow-x: auto !important;
        -webkit-overflow-scrolling: touch;
    }

    .admin-table,
    .admin-users-table,
    .admin-data-table,
    .admin-reports-table {
        min-width: 520px;
    }

    .admin-table th,
    .admin-table td,
    .admin-users-table th,
    .admin-users-table td,
    .admin-data-table th,
    .admin-data-table td {
        padding: 0.65rem 0.75rem;
    }

    .user-activity-dl {
        grid-template-columns: 1fr !important;
        gap: 0.15rem 0 !important;
    }

    .user-activity-dl dt {
        margin-top: 0.5rem;
    }

    .user-activity-dl dt:first-child {
        margin-top: 0;
    }

    .modal-box {
        max-width: 100%;
        max-height: min(92vh, 720px);
        overflow-y: auto;
        padding: 1.15rem;
    }

    .modal-box .modal-actions {
        flex-wrap: wrap;
    }

    .email-template-preview-box,
    #email-template-preview-iframe {
        min-height: 280px !important;
        max-height: 50vh;
    }
}

@media (max-width: 640px) {
    .admin-main {
        padding: 0.85rem 0.65rem 1.75rem !important;
    }

    .admin-tabs,
    .admin-subnav,
    .admin-section-nav {
        margin-left: -0.65rem;
        margin-right: -0.65rem;
        padding-left: 0.65rem;
        padding-right: 0.65rem;
    }

    .admin-header {
        flex-direction: column;
        align-items: stretch;
    }

    .admin-header .btn,
    .admin-header .btn-primary,
    .admin-header a.btn-primary {
        width: 100%;
        justify-content: center;
    }

    .admin-form .form-row input,
    .admin-form .form-row select,
    .admin-form .form-row textarea {
        font-size: 16px; /* avoid iOS zoom on focus */
    }

    .entra-section-nav {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* Safe areas for notched phones */
@supports (padding: max(0px)) {
    @media (max-width: 900px) {
        header {
            padding-left: max(0.85rem, env(safe-area-inset-left));
            padding-right: max(0.85rem, env(safe-area-inset-right));
        }

        .reports-sidebar {
            padding-bottom: env(safe-area-inset-bottom);
        }
    }
}
