/* ============================================
   PrecisaX NMS — Mobile Responsive Stylesheet
   ============================================
   Loaded after all other stylesheets.
   Provides smartphone-optimized overrides.
   ============================================ */

html,
body {
    max-width: 100%;
    overflow-x: hidden;
}

img,
svg,
canvas,
video {
    max-width: 100%;
}

/* ---- Hamburger Menu Button ---- */
.mobile-menu-btn {
    display: none;
    background: none;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 22px;
    padding: 6px 10px;
    cursor: pointer;
    line-height: 1;
    transition: background 0.2s, color 0.2s;
}

.mobile-menu-btn:hover,
.mobile-menu-btn:active {
    background: var(--accent-color);
    color: #fff;
}

/* ---- Mobile Nav Overlay ---- */
.mobile-nav-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 9998;
    opacity: 0;
    transition: opacity 0.25s ease;
}

.mobile-nav-overlay.active {
    opacity: 1;
}

/* ---- Mobile Nav Drawer ---- */
.mobile-nav {
    display: none;
    position: fixed;
    top: 0;
    right: -280px;
    width: 280px;
    height: 100vh;
    background: var(--bg-panel);
    border-left: 1px solid var(--border-color);
    z-index: 9999;
    transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: auto;
    padding: 0;
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.4);
}

.mobile-nav.open {
    right: 0;
}

.mobile-nav-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}

.mobile-nav-header h2 {
    margin: 0;
    font-size: 1rem;
    color: var(--text-primary);
    font-weight: 600;
}

.mobile-nav-close {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 22px;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    transition: color 0.2s;
}

.mobile-nav-close:hover {
    color: var(--accent-danger, #dc3545);
}

.mobile-nav a {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 20px;
    color: var(--text-primary);
    text-decoration: none;
    font-size: 15px;
    border-bottom: 1px solid var(--border-color);
    transition: background 0.15s;
}

.mobile-nav a:hover,
.mobile-nav a:active {
    background: rgba(88, 166, 255, 0.1);
}

.mobile-nav a i {
    width: 22px;
    text-align: center;
    color: var(--accent-color);
    font-size: 16px;
}

.mobile-nav-section {
    padding: 10px 20px 6px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-secondary);
}

/* ========================================
   TABLET — max-width: 768px
   ======================================== */
@media (max-width: 768px) {

    html,
    body {
        overflow-x: hidden !important;
    }

    body {
        width: 100%;
    }

    .content,
    .container,
    .main-content,
    .customer-shell,
    .customer-page {
        width: 100% !important;
        max-width: 100% !important;
        margin-left: 0 !important;
        margin-right: 0 !important;
        padding-left: 12px !important;
        padding-right: 12px !important;
        min-width: 0 !important;
        overflow-x: hidden !important;
    }

    .card,
    .metric-card,
    .module-card,
    .customer-panel,
    .customer-watch-card,
    .customer-candidate,
    .ai-card {
        min-width: 0 !important;
        max-width: 100% !important;
    }

    .top-nav,
    .header,
    .topbar {
        width: 100% !important;
        max-width: 100% !important;
    }

    .nav-left,
    .nav-right,
    .header-left,
    .header-right,
    .topbar-info,
    .topbar-actions,
    .topbar-utility-links {
        min-width: 0 !important;
        max-width: 100% !important;
        flex-wrap: wrap !important;
    }

    .header-right,
    .topbar-utility-links {
        justify-content: flex-start !important;
    }

    .metrics-row,
    .modules-grid,
    .dashboard-grid,
    .customer-inline-summary,
    .customer-search-form,
    .customer-watch-grid,
    .customer-summary-grid {
        grid-template-columns: 1fr !important;
        min-width: 0 !important;
    }

    .customer-tab-bar,
    .tabs {
        overflow-x: auto;
        white-space: nowrap;
        padding-bottom: 6px;
    }

    /* Show hamburger, hide desktop nav links */
    .mobile-menu-btn {
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .mobile-nav,
    .mobile-nav-overlay {
        display: block;
    }

    .header-left a {
        display: none !important;
    }

    /* Touch-friendly buttons */
    button,
    .btn,
    .btn-primary,
    .btn-secondary,
    .btn-icon,
    .tab-btn,
    .ai-btn {
        min-height: 42px;
    }

    /* Fix tables that force horizontal scroll */
    .monitor-table {
        min-width: 0 !important;
        font-size: 0.82rem;
    }

    .protocol-monitor .monitor-table {
        min-width: 0 !important;
    }

    /* Table scroll container polish */
    .table-wrapper,
    .protocol-monitor {
        -webkit-overflow-scrolling: touch;
        position: relative;
    }

    /* Cards — ensure single column */
    .device-grid {
        grid-template-columns: 1fr !important;
        gap: 10px;
    }

    .switch-grid {
        grid-template-columns: 1fr !important;
    }

    /* Header adjustments */
    header {
        gap: 8px;
    }

    .header-left h1 {
        font-size: 1rem;
    }

    .header-right {
        gap: 6px;
    }

    /* AI button — compact */
    .ai-btn .ai-btn-text {
        display: none;
    }

    .ai-btn {
        padding: 8px 12px;
    }

    /* Watchdog banner — full width under header */
    #watchdog-banner {
        margin: 0 !important;
        padding: 6px 12px !important;
        border-radius: 6px !important;
    }

    /* Modal full screen on mobile */
    .modal-content {
        width: calc(100vw - 12px) !important;
        max-width: calc(100vw - 12px) !important;
        max-height: 92vh;
        margin: 4vh auto;
        border-radius: 10px;
    }

    .modal-header h2 {
        font-size: 1rem;
    }

    /* Charts — reduce height */
    .chart-container {
        min-height: 160px !important;
    }

    .chart-container-secondary {
        min-height: 120px !important;
    }

    /* Metrics grid — 2 columns on tablet */
    .metrics-grid {
        grid-template-columns: repeat(2, 1fr) !important;
    }

    .rich-kpi-grid {
        grid-template-columns: 1fr !important;
    }

    /* Details panel — single column */
    .details-panel {
        grid-template-columns: 1fr !important;
    }

    /* Tabs — horizontal scroll */
    .tabs {
        gap: 6px;
        padding-bottom: 8px;
    }

    .tab-btn {
        padding: 8px 12px;
        font-size: 13px;
    }

    /* Toast notifications — bottom full-width */
    .toast {
        left: 8px !important;
        right: 8px !important;
        bottom: 8px !important;
    }

    /* Hide non-essential on mobile */
    .hide-mobile {
        display: none !important;
    }
}

/* ========================================
   OLT/ONU — TABLET/MOBILE OVERRIDES
   ======================================== */
@media (max-width: 768px) {
    /* OLT hero — single column */
    .olt-hero {
        grid-template-columns: 1fr !important;
    }

    .olt-hero-copy h1 {
        font-size: 24px !important;
    }

    .olt-hero-actions {
        flex-direction: column;
    }

    .olt-primary-link,
    .olt-secondary-link {
        width: 100%;
        justify-content: center;
        text-align: center;
    }

    /* ONU table rows — card layout on mobile */
    .olt-module-body .table-head {
        display: none;
    }

    .olt-module-body .row {
        display: flex;
        flex-direction: column;
        padding: 12px;
        margin-bottom: 8px;
        border: 1px solid var(--border-color);
        border-radius: 8px;
        gap: 6px;
        background: var(--bg-panel);
    }

    .olt-module-body .row .cell {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .olt-module-body .row .cell::before {
        font-weight: 600;
        color: var(--text-secondary);
        font-size: 0.8rem;
    }

    .olt-module-body .meta-grid {
        grid-template-columns: 1fr !important;
    }

    .olt-module-body .port-stats {
        grid-template-columns: 1fr !important;
    }

    .olt-module-body .summary {
        grid-template-columns: 1fr !important;
    }

    .olt-hint-card,
    .olt-module-frame {
        padding: 14px !important;
    }

    .ops-toolbar {
        flex-direction: column;
        gap: 8px;
    }

    .ops-toolbar button,
    .ops-toolbar select {
        width: 100%;
        min-height: 42px;
    }
}

/* ========================================
   DFM-DDM — MOBILE OVERRIDES
   ======================================== */
@media (max-width: 768px) {
    .dfm-grid,
    .ddm-grid,
    .dfm-ddm-grid {
        grid-template-columns: 1fr !important;
    }

    .dfm-card,
    .ddm-card {
        padding: 12px;
    }

    .dfm-chart-container,
    .ddm-chart-container {
        min-height: 150px !important;
    }
}

/* ========================================
   QFI — MOBILE OVERRIDES (sidebar already collapses)
   ======================================== */
@media (max-width: 768px) {
    .qfi-grid,
    .qfi-stats-grid {
        grid-template-columns: 1fr !important;
    }

    .qfi-card {
        padding: 12px;
    }
}

/* ========================================
   PORTAL/LOGIN — MOBILE OVERRIDES
   ======================================== */
@media (max-width: 768px) {
    .portal-grid {
        grid-template-columns: 1fr !important;
    }

    .portal-card {
        padding: 16px;
    }

    .login-card {
        width: calc(100vw - 24px) !important;
        max-width: 100% !important;
        margin: 10px auto;
        padding: 20px;
    }

    .login-card h1 {
        font-size: 1.3rem;
    }

    .login-card input {
        min-height: 44px;
        font-size: 16px; /* Prevents iOS zoom on focus */
    }

    .login-card button {
        min-height: 48px;
        font-size: 16px;
    }
}

/* ========================================
   QFI MAP — COLLAPSIBLE ONU PANEL (mobile)
   ======================================== */
.onu-map-control-toggle {
    display: none;
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 1001;
    background: #0f172a;
    border: 1px solid #334155;
    border-radius: 8px;
    color: #e2e8f0;
    padding: 8px 12px;
    font-size: 13px;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.4);
    transition: background 0.2s;
}

.onu-map-control-toggle:active {
    background: #1e293b;
}

.onu-map-control-toggle i {
    margin-right: 6px;
    color: #8b5cf6;
}

@media (max-width: 768px) {
    /* Hide ONU panel by default on mobile */
    .onu-map-control {
        display: none !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        width: 85vw !important;
        max-width: 320px !important;
        height: 100vh !important;
        max-height: 100vh !important;
        border-radius: 0 !important;
        z-index: 9999 !important;
        overflow-y: auto !important;
        padding: 16px !important;
        box-shadow: 4px 0 24px rgba(0,0,0,0.5) !important;
    }

    .onu-map-control.mobile-open {
        display: block !important;
    }

    /* Show toggle button on mobile */
    .onu-map-control-toggle {
        display: flex;
        align-items: center;
    }

    /* QFI topbar — compact */
    .topbar {
        padding: 8px 10px !important;
        flex-wrap: wrap;
        gap: 6px;
    }

    .topbar-info h2 {
        font-size: 0.9rem;
    }

    .topbar-subtitle {
        display: none;
    }

    .topbar-actions {
        gap: 8px;
    }

    .topbar-actions .topbar-time {
        display: none;
    }

    /* Sidebar footer — hide on mobile (system status) */
    .sidebar-footer {
        display: none;
    }

    /* QFI sidebar nav buttons — larger touch targets */
    .nav-btn {
        min-height: 44px;
        padding: 10px 12px;
    }

    /* Fullscreen button compact */
    .topbar a[href="/portal"],
    .topbar a[href="/"] {
        font-size: 11px;
    }
}

/* ========================================
   SMALL PHONE — max-width: 480px
   ======================================== */
@media (max-width: 480px) {
    .content,
    .container,
    .main-content,
    .customer-shell {
        padding-left: 10px !important;
        padding-right: 10px !important;
    }

    header {
        padding: 8px !important;
    }

    .header-left h1 {
        font-size: 0.9rem;
    }

    .dashboard-container {
        padding: 6px;
    }

    .metrics-grid {
        grid-template-columns: 1fr !important;
    }

    .cgnat-insights-grid {
        grid-template-columns: 1fr !important;
    }

    .device-card {
        padding: 10px;
    }

    .device-card h3 {
        font-size: 0.85rem;
    }

    /* Font reduction on very small screens */
    body {
        font-size: 13px;
    }

    .monitor-table th,
    .monitor-table td {
        padding: 6px 8px;
        font-size: 0.78rem;
    }

    .tab-btn {
        padding: 6px 10px;
        font-size: 12px;
    }
}

/* ========================================
   GLOBAL — TOUCH IMPROVEMENTS
   ======================================== */
@media (pointer: coarse) {
    /* Devices with coarse pointer (touch screens) */
    button,
    .btn,
    a.btn,
    .tab-btn,
    .switch-card,
    select {
        min-height: 44px;
    }

    input[type="text"],
    input[type="email"],
    input[type="password"],
    input[type="search"],
    input[type="number"],
    textarea,
    select {
        min-height: 44px;
        font-size: 16px; /* Prevents iOS zoom on input focus */
    }

    /* Increase tap targets for close buttons */
    .btn-close,
    .modal-close {
        min-width: 40px;
        min-height: 40px;
        font-size: 20px;
    }

    /* Prevent text selection on rapid taps */
    .device-card,
    .switch-card,
    .tab-btn,
    .olt-primary-link,
    .olt-secondary-link {
        -webkit-tap-highlight-color: transparent;
        user-select: none;
    }

    /* Scroll momentum for tables */
    .table-wrapper,
    .protocol-monitor,
    .tabs {
        -webkit-overflow-scrolling: touch;
    }
}

/* ========================================
   SAFE AREA (iPhone notch)
   ======================================== */
@supports (padding: env(safe-area-inset-top)) {
    header {
        padding-top: max(10px, env(safe-area-inset-top));
    }

    .mobile-nav {
        padding-top: env(safe-area-inset-top);
    }

    .toast {
        bottom: max(8px, env(safe-area-inset-bottom));
    }
}
