/* ═══════════════════════════════════════════════════════════════════
   MCM Square — Mobile Responsive & Overflow Prevention
   ═════════════════════════════════════════════════════════════════ */

/* ── Global overflow lock — the single most important rule ── */
html, body {
  overflow-x: hidden !important;
  max-width: 100vw;
}

/* Every element respects the viewport width */
*, *::before, *::after {
  max-width: 100%;
}

/* Images never overflow */
img { max-width: 100%; height: auto; display: block; }

/* ── Mobile Drawer Navigation ─────────────────────────────── */
#mobileNavDrawer {
  position: fixed !important;
  top: 68px !important;
  left: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  background: rgba(0,0,0,0.5) !important;
  z-index: 998 !important;
  opacity: 0 !important;
  visibility: hidden !important;
  transition: all 0.3s ease !important;
  backdrop-filter: blur(4px) !important;
}

#mobileNavDrawer.open {
  opacity: 1 !important;
  visibility: visible !important;
}

#mobileNavDrawer > div {
  background: white;
  padding: 0 !important;
  display: flex !important;
  flex-direction: column !important;
  gap: 0 !important;
  max-width: 280px;
  width: 85vw; /* never wider than 85% of viewport */
  height: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  box-shadow: 4px 0 16px rgba(0,0,0,0.15);
  animation: slideInLeft 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"] #mobileNavDrawer > div {
  background: #1e1e1e;
  color: #f0f0f0;
}

@keyframes slideInLeft {
  from { transform: translateX(-100%); opacity: 0; }
  to   { transform: translateX(0);     opacity: 1; }
}

#mobileNavDrawer .nav-link {
  display: flex !important;
  align-items: center !important;
  padding: 14px 20px !important;
  border-radius: 0 !important;
  margin-bottom: 0 !important;
  border-bottom: 1px solid var(--border) !important;
  font-size: 0.95rem !important;
  min-height: 48px !important;
  white-space: nowrap !important;
  transition: all 0.2s ease !important;
  background: transparent !important;
}
#mobileNavDrawer .nav-link:active,
#mobileNavDrawer .nav-link:hover {
  background: var(--primary-pale) !important;
  color: var(--primary) !important;
}
#mobileNavDrawer .nav-link.danger { color: var(--danger) !important; }
#mobileNavDrawer .divider {
  margin: 8px 0 !important;
  height: 1px;
  background: var(--border) !important;
}

/* Hamburger button */
.nav-mobile-toggle {
  width: 48px; height: 48px;
  min-width: 48px; min-height: 48px;
  flex-shrink: 0;
}

/* ── Tablet (≤ 768px) ─────────────────────────────────────── */
@media (max-width: 768px) {

  /* Navbar */
  .nav-search { display: none !important; }
  .nav-links  { display: none !important; }
  .nav-mobile-toggle { display: flex !important; }
  #navAvatarWrapper { display: none !important; }

  /* Page content */
  .page-content {
    padding-top: calc(var(--nav-height) + 16px);
    padding-bottom: 32px;
  }

  /* Container padding tighter */
  .container { padding: 0 16px; }

  /* Modals — full width, no side overflow */
  .modal {
    max-width: calc(100vw - 32px) !important;
    margin: 0 !important;
    border-radius: var(--radius) !important;
  }
  .modal-lg { max-width: calc(100vw - 32px) !important; }
  .modal-body { padding: 14px !important; }
  .modal-header { padding: 14px 16px 10px !important; }

  /* Detail grid stacks */
  .detail-grid { grid-template-columns: 1fr !important; gap: 16px !important; }

  /* Form rows stack */
  .form-row { grid-template-columns: 1fr !important; }

  /* Filters bar */
  .filters-bar {
    flex-direction: column;
    gap: 10px;
    padding: 12px;
    align-items: stretch;
  }
  .filter-chips { flex-wrap: wrap; gap: 6px; }
  .filter-chip { font-size: 0.78rem; padding: 5px 12px; }
  /* Sort/price row — full width, wraps */
  .filters-bar > div[style*="margin-left"] {
    margin-left: 0 !important;
    width: 100%;
    justify-content: flex-start;
  }
  .filter-select { flex: 1; min-width: 0; }

  /* Listings grid — 2 columns like Jiji */
  .listings-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }
  .listing-card-body { padding: 8px 8px 10px !important; }
  .listing-title { font-size: 0.8rem !important; line-height: 1.3 !important; }
  .listing-price { font-size: 0.92rem !important; }
  .listing-meta { font-size: 0.65rem !important; gap: 4px !important; flex-direction: column !important; align-items: flex-start !important; }
  .listing-category { font-size: 0.62rem !important; }
  .listing-seller { display: none !important; } /* hide seller row on mobile to save space */
  .listing-wishlist { width: 26px !important; height: 26px !important; font-size: 13px !important; }
  .listing-badge { font-size: 0.62rem !important; padding: 3px 7px !important; }

  /* Profile */
  .profile-header {
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 20px 16px;
  }
  .profile-stats { justify-content: center; gap: 16px; }
  .profile-tabs { overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* Chat */
  .chat-container { height: calc(100vh - var(--nav-height) - 32px); }
  .chat-sidebar { display: none; width: 100% !important; max-width: 100% !important; }
  .chat-sidebar.visible { display: flex !important; }
  .chat-main { width: 100%; min-width: 0; }

  /* Admin */
  .admin-layout { flex-direction: column; }
  .admin-sidebar {
    width: 100% !important;
    position: static !important;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 12px;
  }
  .admin-sidebar h4 { width: 100%; margin-bottom: 4px; }
  .admin-nav-item { flex: 1; justify-content: center; min-width: 80px; font-size: 0.8rem; }

  /* Tables — horizontal scroll inside container only */
  .table-container { overflow-x: auto; -webkit-overflow-scrolling: touch; max-width: 100%; }
  .data-table { min-width: 600px; }

  /* Stats grid */
  .stats-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px; }
  .stat-card .stat-number { font-size: 1.5rem; }

  /* Footer */
  .footer-grid { grid-template-columns: 1fr !important; gap: 24px; }
  .footer-brand-col { grid-column: 1 !important; }
  .footer-bottom { flex-direction: column; gap: 8px; text-align: center; }
  .footer-bottom-links { justify-content: center; }

  /* About */
  .about-grid { grid-template-columns: 1fr !important; gap: 28px; }
  .about-stats { gap: 16px; }

  /* Hero (marketplace) */
  .hero { padding: 20px 0; }
  .hero h1 { font-size: 1.3rem; margin-bottom: 6px; }
  .hero p { font-size: 0.85rem; margin-bottom: 14px; }
  .hero-search { flex-direction: column; gap: 8px; }
  .hero-search input { width: 100%; }
  .hero-stats { gap: 12px; flex-wrap: wrap; margin-top: 16px; }
  .hero-stat strong { font-size: 1.3rem; }
  .hero-stat span { font-size: 0.75rem; }

  /* Boost plan */
  .boost-plan { flex-direction: column; align-items: flex-start; gap: 8px; }
  .boost-plan-price { align-self: flex-end; }

  /* Landing page — clean, welcoming, not too tall on mobile */
  .landing-hero { grid-template-columns: 1fr !important; gap: 20px; }
  /* Show preview below text on mobile instead of hiding it */
  .landing-visual {
    display: flex !important;
    justify-content: center;
  }
  .market-preview-wrap {
    max-width: 100% !important;
    width: 100%;
  }
  .market-preview {
    border-radius: 14px !important;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2) !important;
  }
  .market-preview-glow { display: none; } /* hide glow on mobile */
  .landing-hero-wrap { padding: 28px 0 36px; }
  .hero-badge {
    font-size: 0.78rem !important;
    padding: 7px 14px !important;
    margin-bottom: 14px !important;
    font-weight: 700 !important;
  }
  .hero-title {
    font-size: 1.85rem !important;
    line-height: 1.1 !important;
    margin-bottom: 12px !important;
    letter-spacing: -0.02em !important;
  }
  .hero-subtitle {
    font-size: 0.92rem !important;
    line-height: 1.65 !important;
    margin-bottom: 20px !important;
  }
  .landing-ctas { flex-direction: column; gap: 10px; margin-bottom: 0 !important; }
  .btn-hero {
    width: 100%; justify-content: center;
    padding: 14px 20px !important;
    font-size: 0.95rem !important;
    font-weight: 700 !important;
    border-radius: 12px !important;
    min-height: 48px !important;
  }
  .trust-banner .container { flex-direction: column; gap: 12px; }
  .trust-divider { width: 100%; height: 1px; }
  .how-grid { grid-template-columns: 1fr; gap: 16px; }
  .why-grid { grid-template-columns: 1fr; }
  .categories-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .final-cta-section { padding: 48px 0; }
  .final-cta-inner h2 { font-size: 1.5rem; }
  .final-ctas { flex-direction: column; gap: 10px; }
  .btn-xl { width: 100%; }

  /* Lost & Found */
  .lf-header { flex-direction: column; align-items: stretch; gap: 12px; margin-bottom: 16px; }
  .lf-header h1 { font-size: 1.4rem; }
  .lf-header > button { width: 100%; }
  .lf-tabs { gap: 6px; margin-bottom: 16px; }
  .lf-tab { flex: 1; text-align: center; padding: 8px 6px; font-size: 0.8rem; }
  .lf-type-btn { min-height: 48px; }

  /* Touch targets */
  .btn { min-height: 44px; }
  .btn-sm { min-height: 36px; }
  .filter-chip { min-height: 36px; }
  .nav-link { min-height: 44px; display: flex; align-items: center; }
}

/* ── Small phones (≤ 480px) ───────────────────────────────── */
@media (max-width: 480px) {

  .container { padding: 0 12px; }

  /* Single column listings on very small screens */
  .listings-grid {
    grid-template-columns: repeat(2, 1fr) !important; /* keep 2 cols even on small phones */
    gap: 6px !important;
  }

  /* Navbar brand */
  .nav-brand { font-size: 1.1rem !important; gap: 6px !important; }
  .nav-brand .brand-icon { width: 30px !important; height: 30px !important; font-size: 15px !important; }

  /* Drawer full width */
  #mobileNavDrawer > div { max-width: 100%; width: 100%; }

  /* Headings */
  h1 { font-size: 1.4rem !important; }
  h2 { font-size: 1.2rem !important; }

  /* Profile tabs */
  .profile-tab { padding: 8px 10px !important; font-size: 0.78rem !important; }

  /* My listings stats — 1 column */
  #myListingsStats { flex-direction: column !important; }
  #myListingsStats .stat-card { width: 100%; }

  /* Boost modal */
  .boost-plan-name { font-size: 0.88rem; }
  .boost-plan-desc { font-size: 0.72rem; }
  .boost-plan-price { font-size: 1rem; }

  /* Auth */
  .auth-form-side { padding: 24px 14px !important; }

  /* Chat input */
  .chat-input-area { padding: 10px 12px; gap: 8px; }
  .chat-input-area input { font-size: 0.85rem; }

  /* Toast */
  #toast-container { left: 8px !important; right: 8px !important; bottom: 8px !important; }
  .toast { font-size: 0.82rem; padding: 10px 12px; }

  /* Lost & Found */
  .lf-tab { padding: 7px 6px; font-size: 0.75rem; }
  .lf-type-btn { font-size: 0.82rem; padding: 10px 10px; }

  /* Modal */
  .modal-body { padding: 12px !important; }
  .modal-header { padding: 12px 14px 10px !important; }
  .modal-close { width: 28px !important; height: 28px !important; font-size: 15px !important; }

  /* Forms */
  .form-group { margin-bottom: 14px; }
  .form-label { font-size: 0.8rem; }
  .form-control { padding: 10px 12px; font-size: 0.85rem; }
  .upload-area { padding: 20px 12px; }

  /* Landing */
  .hero-title { font-size: 1.65rem !important; line-height: 1.1 !important; }
  .hero-subtitle { font-size: 0.88rem !important; }
  .btn-hero { padding: 13px 16px !important; font-size: 0.9rem !important; }
  .categories-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .category-tile { font-size: 0.72rem; padding: 10px 6px; }
  .final-cta-section { padding: 40px 0; }
  .final-cta-inner h2 { font-size: 1.3rem; }
  .btn-xl { padding: 12px 16px; font-size: 0.9rem; }
}

/* ── Jiji-style 2-column card images on mobile ─────────── */
@media (max-width: 768px) {
  .listing-card-img {
    aspect-ratio: 1 / 1 !important; /* square thumbnails like Jiji */
  }
}

/* ── Light mode mobile — darker text for readability ─────── */
@media (max-width: 768px) {

  /* Hero section — force dark text on light mode */
  [data-theme="light"] .hero-title {
    color: #111111 !important;
    text-shadow: none !important;
  }
  [data-theme="light"] .hero-subtitle {
    color: rgba(0,0,0,0.75) !important;
  }
  [data-theme="light"] .hero-badge {
    background: rgba(0,0,0,0.08) !important;
    border-color: rgba(0,0,0,0.18) !important;
    color: #111111 !important;
  }
  [data-theme="light"] .text-gradient {
    background: linear-gradient(120deg, #C44A18, #E85D26) !important;
    -webkit-background-clip: text !important;
    -webkit-text-fill-color: transparent !important;
    background-clip: text !important;
  }

  /* CTA buttons on light mode hero */
  [data-theme="light"] .btn-hero.btn-primary {
    background: var(--primary) !important;
    color: white !important;
  }
  [data-theme="light"] .btn-hero.btn-outline {
    border-color: var(--primary) !important;
    color: var(--primary) !important;
    background: transparent !important;
  }
  [data-theme="light"] .btn-hero.btn-ghost {
    color: #333333 !important;
    border-color: rgba(0,0,0,0.2) !important;
  }
  [data-theme="light"] .btn-arrow {
    color: inherit !important;
  }

  /* Quick stats on hero */
  [data-theme="light"] .stat-number { color: var(--primary) !important; }
  [data-theme="light"] .stat-label  { color: rgba(0,0,0,0.6) !important; }
  [data-theme="light"] .hero-quick-stats {
    border-top-color: rgba(0,0,0,0.15) !important;
  }

  /* General page text — make sure it's dark enough */
  [data-theme="light"] .listing-title  { color: #111111 !important; }
  [data-theme="light"] .listing-price  { color: var(--primary) !important; }
  [data-theme="light"] .listing-meta   { color: #555555 !important; }
  [data-theme="light"] .listing-category { color: var(--primary) !important; }

  /* Cards in light mode */
  [data-theme="light"] .card { background: #ffffff !important; }
  [data-theme="light"] .listing-card-body { background: #ffffff !important; }

  /* Filter chips */
  [data-theme="light"] .filter-chip { color: #333333 !important; }
  [data-theme="light"] .filter-chip.active { color: white !important; }

  /* Nav in light mode */
  [data-theme="light"] .navbar { background: #ffffff !important; }
  [data-theme="light"] .nav-brand { color: #111111 !important; }
}

/* ── Prevent iOS zoom on input focus ────────────────────── */
/* iOS Safari zooms when font-size < 16px on any input      */
@media (max-width: 768px) {
  input, textarea, select {
    font-size: 16px !important;
  }
}
