/* ============================================================
   PLI PLATFORM — RESPONSIVE CSS
   Handles all inline styles and mobile layouts
   ============================================================ */

* { box-sizing: border-box; }
img, video { max-width: 100%; height: auto; }

/* Always scrollable tables */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; display: block; }
.table-wrap table { min-width: 500px; }

/* ── TABLET (≤1024px) ── */
@media (max-width: 1024px) {
  [style*="grid-template-columns:repeat(4,1fr)"] { grid-template-columns: repeat(2,1fr) !important; }
  [style*="grid-template-columns:repeat(3,1fr)"] { grid-template-columns: repeat(2,1fr) !important; }
  [style*="grid-template-columns:1fr 1fr 1fr"]   { grid-template-columns: 1fr 1fr !important; }
}

/* ── MOBILE (≤768px) ── */
@media (max-width: 768px) {

  /* ALL inline grids → 1 column */
  [style*="grid-template-columns:1fr 320px"]      { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:1fr 380px"]      { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:1fr 2fr"]        { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:2fr 1fr"]        { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:1fr 1fr 1fr"]    { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:1fr 1fr"]        { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:repeat(4,1fr)"]  { grid-template-columns: 1fr 1fr !important; }
  [style*="grid-template-columns:repeat(3,1fr)"]  { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:300px 1fr"]      { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:320px 1fr"]      { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns: 2fr 1fr 1fr 2fr 1fr auto"] { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:repeat(auto-fill,minmax(260px"] { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:repeat(auto-fill,minmax(300px"] { grid-template-columns: 1fr !important; }
  [style*="grid-template-columns:minmax(280px,320px) 1fr"]       { grid-template-columns: 1fr !important; }

  /* CSS class grids */
  .grid-2, .grid-3, .smtp-grid, .rule-grid, .row-2, .row-3 { grid-template-columns: 1fr !important; }

  /* Auth pages */
  .auth-page { padding: 12px !important; }
  .auth-card, .reg-card { padding: 24px 16px !important; max-width: 100% !important; }

  /* Tables */
  table { min-width: 480px; font-size: 12px; }
  th, td { padding: 8px 10px !important; white-space: nowrap; }
  .actions-col { white-space: normal !important; }
  .actions-col form, .actions-col button, .actions-col a {
    display: inline-block !important; margin: 2px !important;
  }

  /* Modals */
  [style*="position:fixed"][style*="z-index:9999"] > div,
  [style*="position:fixed"][style*="z-index:1000"] > div {
    max-width: calc(100vw - 20px) !important;
    margin: 10px !important;
    max-height: 90vh !important;
    overflow-y: auto !important;
    width: calc(100vw - 20px) !important;
  }

  /* Settings tabs */
  .settings-tabs { overflow-x: auto !important; -webkit-overflow-scrolling: touch; }
  .stab { font-size: 11px !important; padding: 8px 10px !important; white-space: nowrap !important; }

  /* Sidebar compact */
  .sidebar .nav-item { padding: 10px 14px !important; font-size: 13px !important; }
  .sidebar-brand { padding: 16px 14px !important; }
  .sidebar-user  { padding: 10px 14px !important; }
  .nav-section-label { font-size: 9px !important; padding: 4px 14px !important; }

  /* Notification bell */
  #notifDropdown {
    width: calc(100vw - 20px) !important;
    right: -50px !important;
  }

  /* Quiz */
  .quiz-header { flex-direction: column !important; }
  .timer-box { width: 100% !important; text-align: center !important; }
  .option-label { padding: 10px 12px !important; font-size: 13px !important; }

  /* Badges */
  .badge { font-size: 10px !important; padding: 2px 6px !important; }

  /* Learner register */
  .row-2, .row-3 { grid-template-columns: 1fr !important; }
}

/* ── SMALL (≤480px) ── */
@media (max-width: 480px) {
  .auth-card { padding: 18px 12px !important; }
  table { font-size: 11px; }
  th, td { padding: 6px 8px !important; }
  .settings-tabs { flex-wrap: nowrap !important; }
  .neu-input { width: 100% !important; }
  [style*="grid-template-columns:1fr 1fr"] { grid-template-columns: 1fr 1fr !important; }
}

/* ── Print ── */
@media print {
  .sidebar, .hamburger, .theme-toggle, .sidebar-overlay, .desktop-topbar { display: none !important; }
  .app-layout > .main-content { margin: 0 !important; padding: 0 !important; width: 100% !important; }
}

/* ════════════════════════════════════════════════════════
   MOBILE TOP BAR + BOTTOM TAB NAV
   ════════════════════════════════════════════════════════ */

/* Hidden on desktop */
.mobile-topbar,
.mobile-bottom-nav { display: none; }

@media (max-width: 768px) {

  /* ── Mobile Top Bar ── */
  .mobile-topbar {
    display: flex;
    align-items: center;
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 58px;
    background: var(--sidebar);
    border-bottom: 1px solid var(--border);
    z-index: 300;
    padding: 0 14px;
    gap: 0;
    box-shadow: 0 2px 12px rgba(0,0,0,.15);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
  }

  .mobile-hamburger {
    display: none !important;
  }

  .mobile-topbar-left {
    display: flex; align-items: center;
    gap: 9px; flex: 1; min-width: 0;
  }

  .mobile-logo-link {
    display: flex; align-items: center;
    gap: 9px; text-decoration: none;
    transition: opacity 0.15s;
  }
  .mobile-logo-link:active { opacity: 0.7; }

  .mobile-logo-icon {
    width: 32px; height: 32px; border-radius: 9px;
    background: linear-gradient(135deg, var(--accent), var(--accent2));
    display: flex; align-items: center; justify-content: center;
    font-size: 14px; color: #fff;
    box-shadow: 0 2px 8px rgba(99,102,241,0.35);
    flex-shrink: 0;
  }

  .mobile-topbar-brand {
    font-size: 19px; font-weight: 900;
    color: var(--accent);
    font-family: 'DM Serif Display', serif;
    letter-spacing: 0.5px;
    flex-shrink: 0;
  }

  .mobile-topbar-user {
    display: flex; align-items: center;
    gap: 7px; min-width: 0;
    margin-left: 10px;
    padding: 5px 10px;
    background: rgba(99,102,241,0.08);
    border: 1px solid var(--border);
    border-radius: 20px;
    overflow: hidden;
  }

  .mobile-topbar-right {
    display: flex; align-items: center;
    gap: 6px; flex-shrink: 0;
    margin-left: auto;
  }

  .mobile-user-avatar {
    width: 22px; height: 22px; border-radius: 6px;
    background: linear-gradient(135deg, var(--accent), var(--accent2));
    color: #fff; font-size: 11px;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
  }

  .mobile-user-name {
    font-size: 11px; font-weight: 700;
    color: var(--text);
    overflow: hidden; text-overflow: ellipsis;
    white-space: nowrap; max-width: 100px;
    letter-spacing: 0.2px;
  }

  .mobile-logout-btn {
    color: var(--muted); text-decoration: none;
    font-size: 16px; padding: 4px;
    display: flex; align-items: center;
    transition: color 0.2s;
  }
  .mobile-logout-btn:hover { color: var(--danger); }

  /* ── Hide desktop sidebar elements on mobile ── */
  .hamburger { display: none !important; }
  .theme-toggle {
    display: flex !important;
    position: fixed; top: 8px; right: 8px;
    width: 36px; height: 36px; z-index: 310;
  }

  /* ── Main content padding adjustments ── */
  .app-layout > .main-content {
    margin-left: 0 !important;
    width: 100% !important;
    padding: 70px 12px 80px !important;
  }

  /* ── Mobile Bottom Tab Bar ── */
  .mobile-bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: 58px;
    background: var(--sidebar);
    border-top: 1px solid var(--border);
    z-index: 300;
    box-shadow: 0 -2px 8px rgba(0,0,0,.1);
  }
  .mobile-tab {
    flex: 1;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    gap: 3px;
    text-decoration: none;
    color: var(--muted);
    font-size: 9px; font-weight: 700;
    letter-spacing: 0.3px;
    background: none; border: none; cursor: pointer;
    transition: color 0.2s;
    padding: 6px 2px;
    font-family: 'Nunito', sans-serif;
  }
  .mobile-tab i { font-size: 18px; }
  .mobile-tab span { font-size: 9px; }
  .mobile-tab.active { color: var(--accent); }
  .mobile-tab:hover  { color: var(--accent); }

  /* ── Sidebar (slide-in panel on mobile) ── */
  .sidebar {
    position: fixed !important;
    top: 0 !important; left: 0 !important;
    height: 100vh !important;
    width: 220px !important;
    transform: translateX(-100%) !important;
    z-index: 400 !important;
    overflow-y: auto;
    padding-top: 16px !important;
  }
  .sidebar.open { transform: translateX(0) !important; }
  .sidebar .brand-sub  { font-size: 10px !important; }
  .sidebar .nav-item   { padding: 9px 14px !important; font-size: 13px !important; }
  .sidebar .nav-section-label { font-size: 9px !important; padding: 4px 14px !important; }
  .sidebar .sidebar-user { padding: 10px 14px !important; }
  .sidebar .user-name    { font-size: 12px !important; }

  /* Hide bottom nav items that are in sidebar already */
  .sidebar-overlay.open { display: block !important; z-index: 350; }
}

/* ── Hide security/firewall/audit on mobile dashboard ── */
@media (max-width: 768px) {
  .hide-mobile { display: none !important; }
}

/* ── Users page action buttons on mobile ── */
@media (max-width: 768px) {
  .actions-col {
    white-space: normal !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 6px !important;
    align-items: center !important;
    padding: 8px !important;
  }
  .actions-col form,
  .actions-col > a,
  .actions-col > button {
    display: inline-flex !important;
    margin: 0 !important;
  }

  /* Hide old .hamburger class button (theme.js no longer creates it, but safety) */
  .hamburger { display: none !important; }

  /* Mobile topbar z-index above everything */
  .mobile-topbar { z-index: 500 !important; }
  .theme-toggle  { z-index: 501 !important; }

  /* Sidebar panel above overlay */
  #mainSidebar { z-index: 400 !important; }
  #sidebarOverlay { z-index: 350 !important; }
}

/* ════════════════════════════════════════════════════
   TARGETED MOBILE FIXES
   ════════════════════════════════════════════════════ */
@media (max-width: 768px) {

  /* ── Theme toggle — embedded in topbar visually ── */
  .theme-toggle {
    position: fixed !important;
    top: 8px !important;
    right: 8px !important;
    width: 36px !important;
    height: 36px !important;
    background: transparent !important;
    border: none !important;
    box-shadow: none !important;
    color: var(--text) !important;
    font-size: 16px !important;
    z-index: 501 !important;
  }

  /* ── Topbar right — make room for theme toggle ── */
  .mobile-topbar-right {
    padding-right: 44px !important;
  }

  /* ── Notification dropdown — fixed full-width ── */
  #notifDropdown {
    position: fixed !important;
    top: 56px !important;
    right: 8px !important;
    left: 8px !important;
    width: auto !important;
    max-height: calc(100vh - 120px) !important;
    overflow-y: auto !important;
    z-index: 600 !important;
    border-radius: 16px !important;
  }

  /* ── Hide System Admin label in sidebar on mobile ── */
  .sidebar-user .user-role { display: none !important; }

  /* ── Sidebar user section hidden on mobile ── */
  .sidebar .sidebar-user { display: none !important; }

  /* ── Charts grid — stack on mobile, both visible ── */
  #chartsGrid { grid-template-columns: 1fr !important; }
  #chartsGrid > div { display: block !important; }

  /* ── Rule Hits card — show on mobile ── */
  .rule-hits-card { display: block !important; }

  /* ── Back-to-top only (no back button) ── */
  #fabBack { display: none !important; }
  #fabStack { bottom: 70px !important; right: 12px !important; }
}

@media (max-width: 768px) {
  /* Activity chart full width when firewall chart hidden */
  .activity-chart-card {
    grid-column: 1 / -1 !important;
    width: 100% !important;
  }
}

/* ── Collapse all remaining inline 1fr 1fr form-field grids on small screens ── */
@media (max-width: 480px) {
  [style*="grid-template-columns:1fr 1fr"],
  [style*="grid-template-columns: 1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }
}
