/* ================================================================
   GLOBAL RESPONSIVE — Mobile & iPad fixes for ALL CRM pages
   Loaded from just-application-base.html so every page benefits.
   Breakpoints: 1024px (tablet), 768px (portrait), 640px (mobile),
                480px (small mobile)
   ================================================================ */

/* ──────────────────────────────────────────────────────────────
   1. MODALS — Ensure all modals fit on mobile screens
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 767px) {
  /* Bootstrap modals */
  .modal-dialog {
    margin: 0.5rem !important;
    max-width: calc(100vw - 1rem) !important;
  }
  .modal-dialog.modal-lg,
  .modal-dialog.modal-xl {
    max-width: calc(100vw - 1rem) !important;
  }
  .modal-body {
    max-height: 70vh;
    overflow-y: auto;
  }

  /* Custom PM modals (former_tenants, landlords, prospects) */
  .pm-modal {
    width: calc(100vw - 2rem) !important;
    max-width: calc(100vw - 2rem) !important;
    margin: 1rem !important;
    padding: 1rem !important;
  }
  .pm-modal-lg {
    width: calc(100vw - 2rem) !important;
    max-width: calc(100vw - 2rem) !important;
  }

  /* IAM modals */
  .iam-modal,
  [class*="iam-"] .modal-content,
  .iam-overlay > div,
  .edit-role-modal,
  .invite-modal {
    max-width: calc(100vw - 2rem) !important;
    width: calc(100vw - 2rem) !important;
    margin: 1rem auto !important;
  }

  /* Generic fixed-width modal content */
  .modal-content {
    border-radius: 16px !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   2. KANBAN / PIPELINE BOARDS — Horizontal scroll on mobile
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 1024px) {
  /* Pipeline kanban columns */
  .kanban-container,
  .kanban-board,
  .pipeline-board {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    padding-bottom: 1rem;
  }
  .kanban-column {
    min-width: 280px !important;
    max-width: 280px !important;
    scroll-snap-align: start;
  }
}
@media screen and (max-width: 640px) {
  .kanban-column {
    min-width: 85vw !important;
    max-width: 85vw !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   3. LEAD STAGES — Fixed 320px columns
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 1024px) {
  .lead-stage {
    flex: 0 0 280px !important;
    min-width: 280px !important;
  }
  .lead-stage-content {
    max-height: calc(100vh - 200px) !important;
  }
}
@media screen and (max-width: 640px) {
  .lead-stage {
    flex: 0 0 85vw !important;
    min-width: 85vw !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   4. TASK COLUMNS — Fixed 300px columns
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 1024px) {
  .tm-column {
    min-width: 280px !important;
    max-width: 280px !important;
  }
  .tm-board {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
  }
}
@media screen and (max-width: 640px) {
  .tm-column {
    min-width: 85vw !important;
    max-width: 85vw !important;
    scroll-snap-align: start;
  }
  .tm-stats-grid {
    grid-template-columns: 1fr !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   5. TABLES — Ensure all tables scroll horizontally
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 1024px) {
  table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }
  table thead, table tbody, table tr {
    display: table;
    width: 100%;
    table-layout: auto;
  }
  /* Undo display:block where it breaks layout */
  .table-responsive table,
  table.dataTable {
    display: table;
  }
}

/* ──────────────────────────────────────────────────────────────
   6. GRIDS — Stats grids, filter grids, info grids
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  /* 4-column stats → 2 columns */
  .pm-stats,
  .stats-grid-4,
  [style*="grid-template-columns: repeat(4"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Filter bars → stack */
  .pm-filter-grid,
  .filter-grid {
    grid-template-columns: 1fr !important;
    gap: 0.5rem !important;
  }

  /* Info grids in detail pages */
  .info-grid {
    grid-template-columns: 1fr 1fr !important;
  }
}
@media screen and (max-width: 480px) {
  .pm-stats,
  .stats-grid-4,
  .info-grid,
  [style*="grid-template-columns: repeat(4"] {
    grid-template-columns: 1fr !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   7. CONTACTS GRID — Min-width 280px cards
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 640px) {
  .contacts-grid {
    grid-template-columns: 1fr !important;
  }
  .contact-card {
    min-width: 0 !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   8. SEARCH & FILTER BARS — Don't overflow on mobile
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .search-input,
  .search-box input,
  [class*="search-"] input {
    min-width: 0 !important;
    width: 100% !important;
  }
  .filter-dropdown-menu {
    min-width: 0 !important;
    width: calc(100vw - 2rem) !important;
    max-width: 300px !important;
  }
  .contacts-toolbar,
  .toolbar,
  .page-toolbar,
  .filter-toolbar {
    flex-wrap: wrap !important;
    gap: 0.5rem !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   9. TOOLBARS & HEADERS — Wrap actions on small screens
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .box-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 0.75rem !important;
    padding: 1rem !important;
  }
  .box-header-actions {
    flex-wrap: wrap !important;
    gap: 0.5rem !important;
    width: 100% !important;
  }
  .box-header-left {
    width: 100% !important;
  }
  .box-title h1 {
    font-size: 1.125rem !important;
  }

  /* Pipeline header */
  .pipeline-header,
  .page-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 0.75rem !important;
  }

  /* Stage dropdown */
  .stage-dropdown-wrapper {
    width: 100% !important;
  }
  .stage-dropdown-wrapper .btn-stage-select {
    width: 100% !important;
    justify-content: space-between !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   10. CALL CENTER — Dial pad & sidebar
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 1024px) {
  /* Stack call center layout vertically */
  .call-center-layout,
  .call-center-container {
    flex-direction: column !important;
  }
  /* Right sidebar becomes full-width */
  .call-center-layout .w-96,
  .call-center-layout > div:last-child,
  .call-center-container .w-96 {
    width: 100% !important;
    max-width: 100% !important;
    border-left: none !important;
    border-top: 1px solid #e5e7eb !important;
  }
}
@media screen and (max-width: 640px) {
  .dial-pad {
    max-width: 200px !important;
  }
  .dial-btn {
    width: 50px !important;
    height: 50px !important;
    font-size: 1.125rem !important;
  }
  .call-btn {
    width: 56px !important;
    height: 56px !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   11. COMMUNICATION PAGES — Voicemail, Recordings
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .communication-page {
    height: auto !important;
    min-height: calc(100vh - 64px);
  }
  .voicemail-card,
  .recording-card {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 0.75rem !important;
  }
  .voicemail-card audio,
  .recording-card audio {
    width: 100% !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   12. CALENDAR — FullCalendar responsive
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .fc .fc-toolbar {
    flex-direction: column !important;
    gap: 0.5rem !important;
  }
  .fc .fc-toolbar-title {
    font-size: 1rem !important;
  }
  .fc .fc-button {
    padding: 0.25rem 0.5rem !important;
    font-size: 0.75rem !important;
  }
  /* Calendar stats grid */
  .calendar-stats,
  [class*="grid-cols-4"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}
@media screen and (max-width: 480px) {
  .fc .fc-toolbar-chunk {
    width: 100%;
    justify-content: center;
    display: flex;
  }
  .calendar-stats,
  [class*="grid-cols-4"] {
    grid-template-columns: 1fr !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   13. TIMELINE / ACTIVITY — Reduce padding on mobile
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .timeline-container {
    padding-left: 1rem !important;
  }
  .timeline-item::before {
    left: -1rem !important;
  }
  .activity-page {
    padding: 0.75rem !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   14. DETAIL PAGES — 2-column → single column
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 1024px) {
  /* Box detail: sidebar + main → stacked */
  .box-content {
    display: flex !important;
    flex-direction: column !important;
  }
  .box-content > .box-main {
    width: 100% !important;
  }
  .box-content > .box-sidebar,
  .box-content > div:last-child {
    width: 100% !important;
  }

  /* Contact / Company detail sidebars */
  .detail-layout,
  .detail-content {
    flex-direction: column !important;
  }
  .detail-sidebar,
  .detail-main {
    width: 100% !important;
    max-width: 100% !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   15. CRM NAV — Horizontal scroll on small screens
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .crmnav-container {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .crmnav-container::-webkit-scrollbar {
    display: none;
  }
  .crmnav-tabs {
    flex-wrap: nowrap !important;
    min-width: max-content;
  }
  .crmnav-link {
    white-space: nowrap;
    padding: 0.5rem 0.75rem !important;
    font-size: 0.8125rem !important;
  }
  .crmnav-link span {
    display: none;
  }
  .crmnav-divider {
    display: none !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   16. DASHBOARD — KPI cards & chart grids
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .dash-grid,
  .kpi-grid,
  .dashboard-grid {
    grid-template-columns: 1fr !important;
  }
  .dash-card,
  .kpi-card {
    min-width: 0 !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   17. FORECASTING & REPORTS — Chart containers
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .forecast-grid,
  .report-grid {
    grid-template-columns: 1fr !important;
  }
  .chart-container,
  .report-chart {
    min-height: 200px !important;
    height: auto !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   18. SMS PAGE — Chat layout
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 1024px) {
  .sms-layout,
  .sms-container {
    flex-direction: column !important;
  }
  .sms-sidebar,
  .sms-conversation-list {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 40vh !important;
    border-right: none !important;
    border-bottom: 1px solid #e5e7eb !important;
  }
  .sms-chat,
  .sms-messages {
    width: 100% !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   19. DROPDOWNS & MENUS — Don't overflow screen
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 640px) {
  [class*="dropdown-menu"],
  .dropdown-menu {
    min-width: 0 !important;
    max-width: calc(100vw - 2rem) !important;
  }
  /* Variables menu in inbox compose */
  .variables-menu,
  .merge-fields-menu {
    min-width: 0 !important;
    width: calc(100vw - 2rem) !important;
    max-width: 320px !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   20. GLASS CARDS — Padding reduction on mobile
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 640px) {
  .glass-card,
  .glassmorphic-card {
    padding: 0.75rem !important;
    border-radius: 12px !important;
  }
  .glass-card .card-header,
  .glassmorphic-card .card-header {
    padding: 0.5rem 0.75rem !important;
  }
  .glass-card .card-body,
  .glassmorphic-card .card-body {
    padding: 0.5rem 0.75rem !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   21. TEAM PAGES — Member cards, IAM console
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .team-grid,
  .member-grid,
  .member-cards {
    grid-template-columns: 1fr !important;
  }
  .team-sidebar {
    width: 100% !important;
    max-width: 100% !important;
  }
  .iam-layout {
    flex-direction: column !important;
  }
  .iam-sidebar {
    width: 100% !important;
    border-right: none !important;
    border-bottom: 1px solid #e5e7eb !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   22. NOTIFICATION SETTINGS
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .notification-settings-container {
    max-width: 100% !important;
    padding: 0.75rem !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   23. EMAIL ACCOUNT SETUP — Wide forms
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 640px) {
  .setup-container,
  .email-setup-container {
    max-width: 100% !important;
    padding: 1rem !important;
  }
  .setup-step-content,
  .oauth-buttons {
    flex-direction: column !important;
    gap: 0.5rem !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   24. SEQUENCES & CAMPAIGNS — Step editors
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .sequence-steps,
  .campaign-steps {
    padding: 0 !important;
  }
  .sequence-step,
  .campaign-step {
    padding: 0.75rem !important;
  }
  .sequence-stats,
  .campaign-stats {
    grid-template-columns: repeat(2, 1fr) !important;
  }
}
@media screen and (max-width: 480px) {
  .sequence-stats,
  .campaign-stats {
    grid-template-columns: 1fr !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   25. IMPORT/EXPORT — AI mapping tables & wide containers
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .import-export-container {
    padding: 0.75rem !important;
  }
  #aiMappingTable,
  #aiSampleTable,
  .mapping-table {
    display: block !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }
}

/* ──────────────────────────────────────────────────────────────
   26. PROSPECT / LANDLORD / TENANT GRIDS
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .prospect-grid,
  .landlord-grid,
  .tenant-grid,
  .former-tenant-grid {
    grid-template-columns: 1fr !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   27. TEXT & TYPOGRAPHY — Scale down on mobile
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 640px) {
  h1, .text-3xl, .text-2xl {
    font-size: 1.25rem !important;
    line-height: 1.3 !important;
  }
  h2, .text-xl {
    font-size: 1.0625rem !important;
    line-height: 1.3 !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   28. BUTTONS — Touch-friendly sizes on mobile
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 640px) {
  .btn, button[class*="btn-"] {
    min-height: 40px;
    padding: 0.5rem 0.875rem;
  }
  /* Keep icon-only buttons small */
  .btn-icon, .btn-sm {
    min-height: 32px;
    padding: 0.25rem 0.5rem;
  }
}

/* ──────────────────────────────────────────────────────────────
   29. GENERAL OVERFLOW PREVENTION
   ────────────────────────────────────────────────────────────── */
body {
  overflow-x: hidden;
}
img, video, iframe, canvas, svg {
  max-width: 100%;
  height: auto;
}

/* ──────────────────────────────────────────────────────────────
   30. DARK MODE — Ensure dark borders match on mobile layouts
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 1024px) {
  html.dark .call-center-layout .w-96,
  html.dark .call-center-layout > div:last-child {
    border-top-color: #334155 !important;
  }
  html.dark .sms-sidebar,
  html.dark .sms-conversation-list {
    border-bottom-color: #334155 !important;
  }
  html.dark .iam-sidebar,
  html.dark .team-sidebar {
    border-bottom-color: #334155 !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   31. FORMER TENANT / LANDLORD CARD GRIDS — minmax too large
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 768px) {
  .former-grid,
  .landlord-card-grid,
  .prospect-card-grid {
    grid-template-columns: 1fr !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   32. FORM GRIDS — 2-col forms on mobile
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 480px) {
  .form-grid-2,
  .grid.grid-cols-2.gap-4 {
    grid-template-columns: 1fr !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   33. COMMUNICATION NAV — Tab bar responsive
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 640px) {
  .communication-nav {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .communication-nav::-webkit-scrollbar { display: none; }
  .communication-nav .flex {
    flex-wrap: nowrap !important;
    min-width: max-content;
  }
}

/* ──────────────────────────────────────────────────────────────
   34. PAGE PADDING — Reduce on mobile
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 640px) {
  .p-6 { padding: 0.75rem !important; }
  .p-8 { padding: 1rem !important; }
  .px-6 { padding-left: 0.75rem !important; padding-right: 0.75rem !important; }
  .px-8 { padding-left: 1rem !important; padding-right: 1rem !important; }
}

/* ──────────────────────────────────────────────────────────────
   iPad-specific (portrait: 768–834px, landscape: 1024–1194px)
   ────────────────────────────────────────────────────────────── */
@media screen and (min-width: 768px) and (max-width: 1024px) {
  /* Pipeline/lead columns slightly smaller */
  .kanban-column,
  .lead-stage {
    min-width: 260px !important;
    max-width: 260px !important;
    flex: 0 0 260px !important;
  }

  /* 2-up grids for stats */
  .pm-stats,
  .stats-grid-4 {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Contact cards: 2-up */
  .contacts-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Detail page sidebar narrower */
  .box-content {
    grid-template-columns: 1fr 260px !important;
  }
}

/* iPad Pro landscape (1024–1194px) */
@media screen and (min-width: 1024px) and (max-width: 1194px) {
  .box-content {
    grid-template-columns: 1fr 280px !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   35. LARGE SCREEN ENHANCEMENTS — Use space wisely on 1600px+
   ────────────────────────────────────────────────────────────── */
@media screen and (min-width: 1600px) {
  /* Wider columns for kanban/leads */
  .kanban-column,
  .lead-stage {
    min-width: 340px !important;
    max-width: 340px !important;
  }
  /* Wider detail sidebar */
  .box-content {
    grid-template-columns: 1fr 380px !important;
  }
  /* Larger stat grids */
  .pm-stats,
  .stats-grid-4 {
    grid-template-columns: repeat(4, 1fr) !important;
  }
}

@media screen and (min-width: 1920px) {
  .kanban-column,
  .lead-stage {
    min-width: 360px !important;
    max-width: 360px !important;
  }
  .box-content {
    grid-template-columns: 1fr 420px !important;
  }
}

/* ──────────────────────────────────────────────────────────────
   36. LOGO — Constrain brokerage logos on mobile
   ────────────────────────────────────────────────────────────── */
@media screen and (max-width: 1023px) {
  #mobileHeader img {
    max-height: 32px !important;
    max-width: 120px !important;
    object-fit: contain !important;
    width: auto !important;
  }
  .brand-logo {
    width: 40px !important;
    height: 40px !important;
  }
}
