/* ============================================================
 * v56c Mobile responsive layer — properly mobile-first UX
 *
 * Loaded LAST so it overrides existing mobile rules.
 *
 * Major fixes:
 *   - Sidebar becomes a slide-out drawer on mobile (overlay, not pushed below)
 *   - Backdrop dimming on drawer open
 *   - Touch-friendly tap targets (44px minimum)
 *   - Bottom-nav style for primary nav on phones (optional)
 *   - Card grids collapse cleanly
 *   - Tables become readable on narrow screens
 *   - Topbar with hamburger that's always visible on mobile
 *   - Form fields scale up to prevent iOS zoom-on-focus (16px minimum)
 *   - Public site hero/sections collapse properly
 * ============================================================ */

/* ============================================================
 * BREAKPOINT: Tablet and below (≤960px)
 * Sidebar becomes a drawer
 * ============================================================ */
@media (max-width: 960px) {

  /* App shell — single column, sidebar overlays */
  .app-shell {
    display: block !important;
    grid-template-columns: 1fr !important;
  }

  /* Sidebar as drawer */
  .app-side {
    position: fixed !important;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    max-width: 85vw;
    height: 100vh !important;
    border-right: 1px solid var(--line) !important;
    border-bottom: 0 !important;
    background: var(--bg-2);
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
    overflow-y: auto;
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.4);
    display: flex !important;
    flex-direction: column;
  }
  .app-side.open {
    transform: translateX(0);
    display: flex !important;
  }

  /* Backdrop when sidebar open */
  body.side-open::before {
    content: "";
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    z-index: 99;
    animation: backdrop-fade 0.2s ease;
  }
  @keyframes backdrop-fade {
    from { opacity: 0; }
    to { opacity: 1; }
  }

  /* Main content full width */
  .app-main {
    margin-left: 0 !important;
    width: 100% !important;
  }

  /* Topbar — sticky, with hamburger always visible */
  .app-topbar {
    position: sticky;
    top: 0;
    z-index: 50;
    background: rgba(11, 14, 28, 0.92);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    padding: 12px 16px !important;
  }
  .app-topbar h1 {
    font-size: 1.05rem !important;
    margin: 0;
    line-height: 1.2;
  }
  .menu-toggle-app {
    display: flex !important;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    flex-shrink: 0;
  }

  /* Content padding */
  .app-content {
    padding: 16px 14px 80px !important;
  }

  /* User chip on mobile — compact */
  .user-chip {
    padding: 4px 10px !important;
  }
  .user-chip .role { display: none; }

  /* Sidebar links — bigger touch targets */
  .app-nav a {
    padding: 12px 16px !important;
    font-size: 0.94rem !important;
    border-radius: 10px;
    margin: 1px 8px;
    min-height: 44px;
  }
  .app-nav .group-label {
    padding: 14px 16px 6px !important;
    font-size: 0.7rem !important;
  }
}

/* ============================================================
 * BREAKPOINT: Phone (≤640px)
 * Tighter spacing, larger touch targets, simpler layouts
 * ============================================================ */
@media (max-width: 640px) {

  /* Reduce panel padding */
  .panel-head { padding: 14px 16px !important; }
  .panel-body { padding: 16px !important; }
  .panel-head h2, .panel-head h3 { font-size: 0.95rem !important; }

  /* KPI grid: 2 columns then 1 */
  .kpi-grid { gap: 10px !important; }
  .kpi {
    padding: 14px !important;
    border-radius: 12px;
  }
  .kpi .lbl {
    font-size: 0.65rem !important;
    margin-bottom: 5px !important;
  }
  .kpi .val {
    font-size: 1.4rem !important;
  }
  .kpi .sub {
    font-size: 0.72rem !important;
  }

  /* Welcome banner */
  .welcome-banner {
    padding: 16px !important;
    border-radius: 12px !important;
    text-align: left;
  }
  .welcome-banner h3 {
    font-size: 1.05rem !important;
    line-height: 1.3;
  }
  .welcome-banner p { font-size: 0.84rem !important; }
  .welcome-banner .btn {
    width: 100%;
    margin-top: 4px;
  }

  /* Buttons — touch friendly */
  .btn {
    padding: 12px 18px !important;
    font-size: 0.92rem !important;
    min-height: 44px;
  }
  .btn-sm {
    padding: 8px 14px !important;
    font-size: 0.84rem !important;
    min-height: 38px;
  }
  .btn-lg {
    padding: 14px 22px !important;
    font-size: 1rem !important;
    min-height: 50px;
  }

  /* Forms — prevent iOS zoom-on-focus */
  input[type="text"], input[type="email"], input[type="password"],
  input[type="number"], input[type="search"], input[type="url"],
  input[type="tel"], input[type="date"], textarea, select {
    font-size: 16px !important;
  }

  /* Tables — horizontal scroll wrap */
  .panel { overflow: hidden; }
  .panel-body { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .tbl {
    min-width: 100%;
  }
  .tbl th, .tbl td {
    padding: 10px 8px !important;
    font-size: 0.84rem !important;
    white-space: nowrap;
  }

  /* Status badges smaller */
  .status-badge {
    font-size: 0.66rem !important;
    padding: 2px 7px !important;
  }

  /* Toolbar wrap */
  .toolbar {
    gap: 8px !important;
  }
  .toolbar .filters {
    width: 100%;
    flex-wrap: wrap;
  }
  .toolbar .filters select,
  .toolbar .filters input {
    flex: 1;
    min-width: 0;
  }

  /* Page header */
  .page-head {
    flex-direction: column;
    align-items: flex-start !important;
    gap: 12px !important;
  }
  .page-head h1, .page-head h2 {
    font-size: 1.4rem !important;
  }

  /* Feed view — narrower padding */
  .feed-page { padding: 12px 12px 60px !important; }
  .feed-item, article.feed-item {
    border-radius: 12px;
    padding: 14px !important;
  }
  .feed-body { font-size: 0.9rem !important; }
  .feed-act {
    padding: 8px 12px !important;
    font-size: 0.78rem !important;
    min-height: 36px;
  }

  /* Pulse view */
  .pls-compose, .pls-post {
    border-radius: 12px;
    padding: 14px !important;
  }
}

/* ============================================================
 * BREAKPOINT: Small phone (≤480px)
 * Single column everything
 * ============================================================ */
@media (max-width: 480px) {
  .kpi-grid { grid-template-columns: 1fr !important; }
  .split { grid-template-columns: 1fr !important; }

  /* Hide brand text in topbar — only show logo */
  .app-topbar .brand-mark span:not(.brand-mark-dot) {
    display: none;
  }

  /* Page title — smaller */
  .app-topbar h1 { font-size: 0.95rem !important; }

  /* Hero on public site */
  .hero h1 { font-size: 1.7rem !important; }
  .hero-sub { font-size: 0.95rem !important; }
  .hero-cta { flex-direction: column; gap: 10px; width: 100%; }
  .hero-cta .btn { width: 100%; justify-content: center; }
  .hero-stats {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
  }
  .hero-stats .stat strong { font-size: 1.3rem !important; }
}

/* ============================================================
 * Public site mobile fixes
 * ============================================================ */
@media (max-width: 800px) {
  /* Public hero — stack columns */
  .hero-grid {
    grid-template-columns: 1fr !important;
    gap: 30px !important;
  }
  .hero-visual { display: none; } /* hide on tablet for performance */

  /* Header nav — hamburger pattern */
  .site-header nav.main-nav {
    /* if exists, will be handled by existing rules */
  }
}

/* ============================================================
 * Touch device — disable hover-lift effects
 * ============================================================ */
@media (hover: none) and (pointer: coarse) {
  .btn:hover { transform: none; }
  .kpi:hover { transform: none; }
  .feed-act:hover { transform: none; }
  .panel:hover { transform: none; }
  .adm-tile:hover { transform: none; }
}

/* ============================================================
 * Admin dashboard tiles — mobile responsive
 * ============================================================ */
@media (max-width: 900px) {
  .adm-grid-6 { grid-template-columns: repeat(2, 1fr) !important; }
  .adm-grid-4 { grid-template-columns: repeat(2, 1fr) !important; }
  .adm-grid-3 { grid-template-columns: 1fr !important; }
}
@media (max-width: 480px) {
  .adm-grid-6, .adm-grid-4 { grid-template-columns: 1fr !important; }
  .adm-tile {
    padding: 12px !important;
  }
  .adm-tile-val { font-size: 1.4rem !important; }
}

/* ============================================================
 * Footer — mobile collapse
 * ============================================================ */
@media (max-width: 680px) {
  .site-footer {
    padding: 40px 0 24px !important;
  }
  .site-footer .footer-grid {
    gap: 24px !important;
  }
  .site-footer .footer-strip {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px !important;
  }
  .site-footer .footer-bottom {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  .site-footer .footer-legal {
    gap: 6px 12px;
  }
}

/* ============================================================
 * Safe area insets (iOS notch)
 * ============================================================ */
@supports (padding: env(safe-area-inset-bottom)) {
  .app-content {
    padding-bottom: calc(80px + env(safe-area-inset-bottom)) !important;
  }
  .app-topbar {
    padding-top: calc(12px + env(safe-area-inset-top));
  }
}

/* ============================================================
 * Image safety — never overflow
 * ============================================================ */
img {
  max-width: 100%;
  height: auto;
}

/* ============================================================
 * Body — disable horizontal scroll on mobile
 * ============================================================ */
@media (max-width: 960px) {
  body { overflow-x: hidden; }
}
