/**
 * NeuroGen Platform - Mobile & Responsive Styles
 * Centralized mobile CSS loaded LAST to override desktop styles.
 *
 * Breakpoints:
 *   480px  — Small phones (iPhone SE, Galaxy S)
 *   640px  — Large phones / card-layout threshold
 *   768px  — Tablets portrait / primary mobile breakpoint
 *   992px  — Tablets landscape / small desktop
 *   1200px — Desktop
 *
 * Created: March 18, 2026
 * Version: 1.0
 */

/* ==========================================
   CSS CUSTOM PROPERTIES — MOBILE OVERRIDES
   ========================================== */
:root {
    --mobile-bottom-nav-height: 64px;
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
}

/* ==========================================
   iOS ZOOM PREVENTION
   All form inputs must be >= 16px to prevent
   Safari auto-zoom on focus
   ========================================== */
@media (max-width: 768px) {
    input, select, textarea, .form-control, .form-select,
    .ng-input, .mod-search, .module-search-input {
        font-size: 16px !important;
    }
}

/* ==========================================
   TOUCH TARGET ENFORCEMENT
   Apple HIG: 44x44px minimum tap targets
   ========================================== */
@media (max-width: 768px) {
    .btn, .ng-btn-primary, .ng-btn-outline, .mod-btn,
    .comm-tab, .module-tab, .nav-link, .dropdown-item,
    .btn-close, .form-check-input, .accordion-button {
        min-height: 44px;
        min-width: 44px;
    }

    /* Ensure buttons in tight spaces have proper padding */
    .btn-sm, .ng-btn-sm {
        min-height: 36px;
        padding: 8px 16px !important;
    }
}

/* ==========================================
   SIDEBAR MOBILE IMPROVEMENTS
   ========================================== */

/* Clamp sidebar to 80% of viewport width */
@media (max-width: 768px) {
    #sidebar.admin-sidebar {
        width: min(260px, 80vw) !important;
        will-change: transform;
        -webkit-overflow-scrolling: touch;
    }

    /* Add close button area at top of sidebar on mobile */
    .sidebar-brand {
        position: relative;
    }

    /* Ensure sidebar overlay is above all content */
    .sidebar-overlay {
        z-index: 1045 !important;
    }

    /* Full-width content when sidebar is hidden */
    .admin-content {
        margin-left: 0 !important;
        width: 100% !important;
        padding: 12px !important;
    }

    /* Module card reduced padding on mobile */
    .module-card {
        padding: 14px 12px !important;
        border-radius: 10px !important;
    }

    .module-content-wrapper {
        padding: 0 12px 12px !important;
    }

    /* Master admin header responsive */
    .master-admin-header h1 {
        font-size: 1.3rem !important;
    }

    .master-admin-header p {
        font-size: 0.82rem !important;
    }

    /* Hide prefetch on mobile to save bandwidth */
    link[rel="prefetch"] {
        /* CSS can't actually prevent prefetch — handled by JS */
    }
}

/* Tablet intermediate: icons-only sidebar hint */
@media (min-width: 769px) and (max-width: 992px) {
    #sidebar.admin-sidebar {
        width: 200px !important;
    }
    .admin-content {
        margin-left: 200px !important;
    }
    .sidebar-nav-text {
        font-size: 0.8rem;
    }
}

/* ==========================================
   STAT CARD GRID — RESPONSIVE CASCADE
   6 → 3 → 2 → 1 columns
   ========================================== */
@media (max-width: 992px) {
    .ng-stat-grid--6, .ng-stat-grid--5, .ng-stat-grid--4,
    .processing-stats, .comm-stat-card-grid {
        grid-template-columns: repeat(3, 1fr) !important;
    }
}

@media (max-width: 768px) {
    .ng-stat-grid--6, .ng-stat-grid--5, .ng-stat-grid--4,
    .ng-stat-grid--3, .processing-stats, .comm-stat-card-grid,
    .org-stat-grid, .am-stat-grid {
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 8px !important;
    }

    .ng-stat-card, .stat-card {
        padding: 12px !important;
    }

    .ng-stat-card .stat-value, .stat-card .stat-value {
        font-size: 1.3rem !important;
    }

    .ng-stat-card .stat-label, .stat-card .stat-label {
        font-size: 0.72rem !important;
    }
}

@media (max-width: 480px) {
    .ng-stat-grid--6, .ng-stat-grid--5, .ng-stat-grid--4,
    .ng-stat-grid--3, .ng-stat-grid--2,
    .processing-stats, .comm-stat-card-grid,
    .org-stat-grid, .am-stat-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ==========================================
   TAB NAVIGATION — SCROLLABLE STRIP
   ========================================== */
@media (max-width: 768px) {
    .module-tabs, .nav-tabs, .comm-tabs, .fn-tabs,
    .sm-tabs, .am-tabs {
        display: flex !important;
        flex-wrap: wrap !important;
        overflow-x: visible !important;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none; /* Firefox */
        gap: 4px !important;
        padding-bottom: 4px;
        position: relative;
    }

    /* Hide scrollbar but keep functionality */
    .module-tabs::-webkit-scrollbar,
    .nav-tabs::-webkit-scrollbar,
    .comm-tabs::-webkit-scrollbar,
    .fn-tabs::-webkit-scrollbar,
    .sm-tabs::-webkit-scrollbar,
    .am-tabs::-webkit-scrollbar {
        display: none;
    }

    /* Tab items don't shrink */
    .module-tabs .nav-item, .module-tabs .nav-link,
    .comm-tab, .fn-tab, .sm-tab, .am-tab,
    .nav-tabs .nav-item, .nav-tabs .nav-link {
        flex-shrink: 0 !important;
        white-space: nowrap !important;
        font-size: 0.78rem !important;
        padding: 8px 14px !important;
    }

    /* Scroll fade indicators */
    .module-tabs-wrapper, .tab-navigation {
        position: relative;
    }

    .module-tabs-wrapper::after, .tab-navigation::after {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        width: 40px;
        height: 100%;
        background: linear-gradient(to left, var(--neural-void, #0f1419), transparent);
        pointer-events: none;
        z-index: 1;
    }
}

/* ==========================================
   TABLE — MOBILE CARD LAYOUT
   On small screens, convert table rows to
   stacked cards using data-label attributes
   ========================================== */
@media (max-width: 640px) {
    .module-table-responsive, .module-table-wrapper {
        overflow-x: visible !important;
    }

    .module-table-refined, .module-table, .comm-table,
    .fn-table, .sm-table, .am-table {
        border: none !important;
    }

    .module-table-refined thead, .module-table thead,
    .comm-table thead, .fn-table thead,
    .sm-table thead, .am-table thead {
        display: none !important;
    }

    .module-table-refined tbody tr, .module-table tbody tr,
    .comm-table tbody tr, .fn-table tbody tr,
    .sm-table tbody tr, .am-table tbody tr {
        display: block !important;
        background: var(--neural-deep, #15202b) !important;
        border: 1px solid var(--neural-border, rgba(136,153,166,0.15)) !important;
        border-radius: 10px !important;
        margin-bottom: 8px !important;
        padding: 12px !important;
    }

    .module-table-refined tbody td, .module-table tbody td,
    .comm-table tbody td, .fn-table tbody td,
    .sm-table tbody td, .am-table tbody td {
        display: flex !important;
        justify-content: space-between;
        align-items: center;
        padding: 6px 0 !important;
        border: none !important;
        border-bottom: 1px solid var(--neural-divider, rgba(136,153,166,0.1)) !important;
        font-size: 0.85rem !important;
        text-align: right !important;
    }

    .module-table-refined tbody td:last-child, .module-table tbody td:last-child,
    .comm-table tbody td:last-child, .fn-table tbody td:last-child,
    .sm-table tbody td:last-child, .am-table tbody td:last-child {
        border-bottom: none !important;
    }

    /* Show data-label as row header */
    .module-table-refined tbody td::before, .module-table tbody td::before,
    .comm-table tbody td::before, .fn-table tbody td::before,
    .sm-table tbody td::before, .am-table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.78rem;
        color: var(--text-secondary, #8b98a5);
        text-align: left;
        flex-shrink: 0;
        margin-right: 12px;
    }

    /* Hide data-label pseudo-element when no label set */
    .module-table-refined tbody td[data-label=""]::before,
    .module-table tbody td[data-label=""]::before {
        display: none;
    }
}

/* ==========================================
   MODAL — MOBILE BOTTOM SHEET
   ========================================== */
@media (max-width: 640px) {
    .modal-dialog, .module-modal .modal-dialog,
    .mod-modal .modal-dialog {
        margin: 0 !important;
        max-width: 100vw !important;
        min-height: auto !important;
        align-self: flex-end;
    }

    .modal-content, .module-modal .modal-content,
    .mod-modal .modal-content {
        border-radius: 16px 16px 0 0 !important;
        border: none !important;
        max-height: 90vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .modal.show .modal-dialog {
        transform: none !important;
    }

    /* Full-screen for large modals */
    .modal-lg .modal-dialog, .modal-xl .modal-dialog {
        max-height: 100vh !important;
    }

    .modal-lg .modal-content, .modal-xl .modal-content {
        border-radius: 0 !important;
        min-height: 100vh;
    }
}

/* ==========================================
   FORM INPUTS — MOBILE FULL-WIDTH
   ========================================== */
@media (max-width: 768px) {
    .mod-search, .module-search-input, .form-control,
    .form-select, .ng-input {
        min-width: 0 !important;
        width: 100% !important;
    }

    /* Toolbar wraps on mobile */
    .module-toolbar, .mod-toolbar {
        flex-wrap: wrap !important;
        gap: 8px !important;
    }

    .module-toolbar > *, .mod-toolbar > * {
        flex: 1 1 auto !important;
        min-width: 0 !important;
    }

    /* Search bars take full width */
    .module-toolbar .mod-search,
    .module-toolbar .module-search-input {
        flex: 1 1 100% !important;
        order: -1;
    }
}

/* ==========================================
   CARDS & GRIDS — RESPONSIVE LAYOUT
   ========================================== */
@media (max-width: 768px) {
    /* Agent cards, integration cards, etc. */
    .agent-cards-grid, .integration-grid,
    .template-grid, .workflow-grid {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }
}

@media (max-width: 992px) {
    .agent-cards-grid, .integration-grid,
    .template-grid, .workflow-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
}

/* ==========================================
   CHART CONTAINERS — RESPONSIVE
   ========================================== */
@media (max-width: 768px) {
    .chart-container, .analytics-chart,
    .revenue-chart, .credit-chart {
        min-height: 200px !important;
        max-height: 300px !important;
        overflow-x: auto;
    }

    canvas {
        max-width: 100% !important;
    }
}

/* ==========================================
   HEADER & NAVIGATION — MOBILE
   ========================================== */
@media (max-width: 768px) {
    /* Module header actions stack */
    .master-admin-header .d-flex {
        flex-wrap: wrap;
        gap: 8px;
    }

    /* Notification/settings dropdowns */
    .ng-notif-panel {
        width: min(360px, calc(100vw - 24px)) !important;
        right: -60px !important;
    }

    /* Settings dropdown */
    .ng-settings-panel {
        width: min(300px, calc(100vw - 24px)) !important;
    }

    /* d-md-none toggle button */
    .d-md-none.mb-3 {
        position: fixed;
        top: 12px;
        left: 12px;
        z-index: 1040;
        margin-bottom: 0 !important;
    }
}

/* ==========================================
   BOTTOM NAVIGATION BAR
   Shows on mobile < 768px, replaces sidebar
   as primary navigation
   ========================================== */
.mobile-bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: var(--mobile-bottom-nav-height);
    padding-bottom: var(--safe-area-bottom);
    background: rgba(15, 20, 25, 0.98);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-top: 1px solid var(--neural-border, rgba(136,153,166,0.15));
    z-index: 1060;
    justify-content: space-around;
    align-items: center;
}

@media (max-width: 768px) {
    .mobile-bottom-nav {
        display: flex;
    }

    /* Add padding to body so content isn't hidden behind bottom nav */
    body {
        padding-bottom: calc(var(--mobile-bottom-nav-height) + var(--safe-area-bottom)) !important;
    }

    /* Move socket status indicator above bottom nav */
    .socket-status-indicator {
        bottom: calc(var(--mobile-bottom-nav-height) + var(--safe-area-bottom) + 8px) !important;
    }
}

.mobile-bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    height: 100%;
    color: var(--text-muted, #6e767d);
    text-decoration: none;
    font-size: 10px;
    font-weight: 500;
    gap: 2px;
    transition: color 0.2s ease;
    -webkit-tap-highlight-color: transparent;
    cursor: pointer;
}

.mobile-bottom-nav-item i {
    font-size: 20px;
    line-height: 1;
}

.mobile-bottom-nav-item.active,
.mobile-bottom-nav-item:active {
    color: var(--module-cyan, #22d3ee);
}

.mobile-bottom-nav-item:hover {
    color: var(--text-primary, #e7e9ea);
}

/* Bottom sheet for "More" menu */
.mobile-bottom-sheet {
    display: none;
    position: fixed;
    bottom: var(--mobile-bottom-nav-height);
    left: 0;
    right: 0;
    max-height: 60vh;
    background: var(--neural-deep, #15202b);
    border-top: 1px solid var(--neural-border, rgba(136,153,166,0.15));
    border-radius: 16px 16px 0 0;
    z-index: 1059;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px 0;
    padding-bottom: var(--safe-area-bottom);
    transform: translateY(100%);
    transition: transform 0.3s ease;
}

.mobile-bottom-sheet.open {
    display: block;
    transform: translateY(0);
}

.mobile-bottom-sheet-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1058;
}

.mobile-bottom-sheet-overlay.open {
    display: block;
}

.mobile-bottom-sheet-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    color: var(--text-primary, #e7e9ea);
    text-decoration: none;
    font-size: 0.9rem;
    transition: background 0.15s ease;
}

.mobile-bottom-sheet-item:hover,
.mobile-bottom-sheet-item:active {
    background: var(--neural-elevated, #22303c);
    color: var(--text-primary);
}

.mobile-bottom-sheet-item i {
    width: 24px;
    text-align: center;
    color: var(--text-secondary, #8b98a5);
    font-size: 1rem;
}

.mobile-bottom-sheet-item.active i {
    color: var(--module-cyan, #22d3ee);
}

.mobile-bottom-sheet-divider {
    height: 1px;
    background: var(--neural-border, rgba(136,153,166,0.15));
    margin: 8px 0;
}

.mobile-bottom-sheet-header {
    padding: 8px 20px 12px;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-muted, #6e767d);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

/* ==========================================
   MISCELLANEOUS MOBILE FIXES
   ========================================== */
@media (max-width: 768px) {
    /* Prevent horizontal overflow */
    html, body {
        overflow-x: hidden;
    }

    /* Word break for long text */
    .module-card, .ng-card, .stat-card {
        word-break: break-word;
    }

    /* Responsive images */
    img {
        max-width: 100%;
        height: auto;
    }

    /* Dropdown menus don't overflow */
    .dropdown-menu {
        max-width: calc(100vw - 24px);
    }

    /* Alert/toast sizing */
    .toast, .alert {
        max-width: calc(100vw - 24px) !important;
    }

    /* Code blocks scroll */
    pre, code {
        max-width: 100%;
        overflow-x: auto;
    }

    /* Dialer keypad touch targets */
    .dialer-key, .comm-dialer-key {
        min-width: 60px !important;
        min-height: 60px !important;
        font-size: 1.2rem !important;
    }
}

@media (max-width: 480px) {
    /* Extra small phone adjustments */
    .module-card {
        padding: 10px 8px !important;
        border-radius: 8px !important;
    }

    .master-admin-header h1 {
        font-size: 1.1rem !important;
    }

    /* Stack header actions vertically */
    .master-admin-header .d-flex.align-items-center.gap-2 {
        flex-direction: column;
        align-items: flex-start !important;
    }

    .ng-notif-panel, .ng-settings-panel {
        position: fixed !important;
        left: 8px !important;
        right: 8px !important;
        width: auto !important;
        max-height: 70vh;
    }
}

/* ==========================================
   MODULE-SPECIFIC OVERRIDES
   Responsive rules for modules without their
   own media queries, centralized here.
   ========================================== */

/* --- Assistant Builder --- */
@media (max-width: 768px) {
    .assistants-grid {
        grid-template-columns: 1fr !important;
    }
    .analytics-grid, .analytics-stat-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    #api-config-panel .apc-body {
        grid-template-columns: 1fr !important;
    }
    .quality-tier-cards {
        flex-direction: column !important;
    }
    .category-pills {
        gap: 5px !important;
    }
}

/* --- Communications: Dialer & Contacts --- */
@media (max-width: 768px) {
    .cd-layout {
        grid-template-columns: 1fr !important;
    }
    .cx-grid {
        grid-template-columns: 1fr !important;
    }
    .cd-field-row {
        grid-template-columns: 1fr !important;
    }
    .comm-stats-grid, #comm-stats {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    .cal-week-grid {
        grid-template-columns: 40px repeat(7, 1fr) !important;
        font-size: 0.7rem !important;
    }
    .cal-month-grid {
        font-size: 0.7rem !important;
    }
    .appt-datetime-grid {
        grid-template-columns: 1fr !important;
    }
}

/* --- Social Media: Composer & YouTube --- */
@media (max-width: 768px) {
    .sm-composer-grid,
    .sm-yt-upload-grid {
        grid-template-columns: 1fr !important;
    }
    .sm-yt-stats-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    .sm-connect-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }
    .sm-cal-month-grid-wrap {
        font-size: 0.7rem !important;
    }
}

/* --- Integrations (Chat) --- */
@media (max-width: 768px) {
    .integration-card {
        padding: 1rem !important;
    }
    .btn-platform {
        min-width: 100px !important;
        padding: 0.75rem 1rem !important;
    }
    .btn-platform i {
        font-size: 1.8rem !important;
    }
    .setup-panel {
        padding: 1.2rem !important;
    }
}

/* --- Legal Discovery --- */
@media (max-width: 768px) {
    .risk-indicator {
        flex-direction: column !important;
        text-align: center;
        gap: 8px;
    }
}

/* --- Agent Create/Edit --- */
@media (max-width: 768px) {
    .template-grid {
        grid-template-columns: 1fr !important;
    }
    .kb-stats-grid {
        grid-template-columns: 1fr !important;
    }
}

/* --- Workflow Create/Edit --- */
@media (max-width: 768px) {
    .wf-editor-layout {
        flex-direction: column !important;
    }
    .wf-sidebar, .wf-canvas {
        width: 100% !important;
        max-width: 100% !important;
    }
}

/* ==========================================
   TOUCH DEVICE — Disable hover effects
   ========================================== */
@media (hover: none) {
    .module-card:hover::before {
        opacity: 0;
    }
    .integration-card:hover {
        transform: none !important;
    }
    .btn-platform:hover {
        transform: none !important;
    }
}

/* ==========================================
   AUTH PAGES — Login / Register
   ========================================== */
@media (max-width: 768px) {
    .auth-card-neural {
        margin: 0 12px;
        padding: 32px 20px !important;
    }
    .auth-title {
        font-size: 1.5rem !important;
    }
    .auth-subtitle {
        font-size: 0.9rem !important;
    }
    .input-group-text {
        min-height: 44px;
        min-width: 44px;
    }
    .auth-neural .container {
        padding: 0 !important;
    }
}

@media (max-width: 480px) {
    .auth-card-neural {
        margin: 0 8px;
        padding: 24px 16px !important;
        border-radius: 12px !important;
    }
    .register-card {
        padding: 24px 16px !important;
    }
}

/* ==========================================
   LANDING PAGE — Small phone adjustments
   ========================================== */
@media (max-width: 480px) {
    .neural-hero h1 {
        font-size: 1.8rem !important;
    }
    .neural-hero p {
        font-size: 0.95rem !important;
    }
    .neural-cta-btn, .btn-neural-primary, .btn-neural-secondary {
        width: 100% !important;
        text-align: center;
    }
    .pricing-grid, .features-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ==========================================
   SMALL PHONE — Two-column stacking
   ========================================== */
@media (max-width: 480px) {
    .sm-connect-grid {
        grid-template-columns: 1fr !important;
    }
    .analytics-grid, .analytics-stat-grid {
        grid-template-columns: 1fr !important;
    }
    .sm-yt-stats-grid {
        grid-template-columns: 1fr !important;
    }
}

/* ==========================================
   PRINT STYLES — Hide mobile nav when printing
   ========================================== */
@media print {
    .mobile-bottom-nav,
    .mobile-bottom-sheet,
    .mobile-bottom-sheet-overlay,
    .sidebar-toggle-mobile,
    .sidebar-overlay {
        display: none !important;
    }
}
