/* =========================================================
   Zapim — Global Responsive Layer
   Loaded after all other stylesheets. Pure overrides via media
   queries. Does not change visual identity, colors, typography,
   or remove any feature. Adapts existing layouts to small viewports.
   Breakpoints (mobile-first by negation):
     ≤1280px small desktop / large laptop
     ≤1024px tablet
     ≤880px  small tablet  (inbox stacking begins)
     ≤768px  mobile
     ≤480px  small mobile
   ========================================================= */

/* --------------------------------------------------------- */
/* Universal hardening — applies at all sizes                */
/* --------------------------------------------------------- */
html, body { max-width: 100%; overflow-x: hidden; }
.app, .main, .page { min-width: 0; }
.main { min-width: 0; max-width: 100%; }
.app > .main { overflow-x: hidden; }
img, svg, video, canvas { max-width: 100%; height: auto; }
.dt-wrap, .table-wrap, .data-table-wrap, .sp-tablewrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  width: 100%;
}

/* =========================================================
   ≤1280px — laptops, small desktops
   Mostly cosmetic shrink to prevent topbar elbowing
   ========================================================= */
@media (max-width: 1280px) {
  .topbar .tb-search,
  .topbar .search { width: 220px; }
  .wallet-card .wallet-label { display: none; }
  .wallet-card { padding: 6px 10px; }
  .crumbs { font-size: 12.5px; }
  .crumbs > * + * { margin-left: 6px; }
}

/* =========================================================
   ≤1024px — tablets
   - Existing rule already collapses sidebar to 64px icon rail
   - Stack multi-col page layouts to single column where safe
   - Compress topbar further
   ========================================================= */
@media (max-width: 1024px) {
  /* Topbar: shrink + allow shrink */
  .topbar { padding: 0 14px; gap: 8px; }
  .topbar .tb-search,
  .topbar .search { width: 180px; }
  .crumbs { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

  /* Page padding tighter */
  .page, .main > .page, .main-inner { padding-left: 18px; padding-right: 18px; }

  /* KPI / metric grids that were 4-col → 2-col */
  .kpi-grid,
  .bl-kpi-grid,
  .bm-grid,
  .ch-grid,
  .cards-grid,
  .cr-kpis,
  .cmp-kpi-bar { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }

  /* 3-col → 2-col split layouts */
  .split-2-1, .grid-2-1 { grid-template-columns: 1fr !important; }

  /* Wizard 2-col layouts → stack */
  .qc-grid, .qc-cols, .wiz-grid, .wiz-cols,
  .signup-grid, .create-cols { grid-template-columns: 1fr !important; }

  /* Field rows stay 2-col here, become 1-col below */

  /* Filter/toolbar rows allow wrapping */
  .dt-toolbar, .toolbar, .page-head, .pb-tabs,
  .inbox-pagebar, .lc-tabs { flex-wrap: wrap; row-gap: 10px; }

  /* Modals — let max-width breathe */
  .bl-mc { max-width: calc(100vw - 32px); }
}

/* =========================================================
   ≤880px — small tablets
   - Inbox 3-pane already stacks via existing rules; reinforce
   - More tables get x-scroll
   ========================================================= */
@media (max-width: 880px) {
  /* Make existing table wrappers scrollable instead of forcing display:block on
     the table itself (which produces orphaned thead/tbody and a long tail of
     blank space). Tables keep their native layout, parents scroll horizontally. */
  .dt-wrap, .dt-scroll, .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .dt th, .dt td { white-space: nowrap; }

  /* Wider drop to 2-col on KPI grids that still feel cramped */
  .kpi-grid,
  .bl-kpi-grid,
  .bm-grid,
  .ch-grid { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }

  /* Inbox: keep conv-list visible (override existing 900px rule that hides it).
     Detail panel hidden — user opens a conversation to see chat. */
  .inbox-shell.no-rail,
  .inbox-shell.has-cards .inbox-3pane {
    grid-template-columns: var(--inbox-list-w) minmax(0, 1fr) !important;
  }
  .inbox-shell .conv-list { display: flex !important; }
  .inbox-shell.no-rail > .detail,
  .inbox-shell.has-cards .inbox-3pane > .detail { display: none; }

  /* Hide pane-resize handles on stacked layout */
  .pane-resize-handle { display: none; }
}

/* =========================================================
   ≤768px — mobile
   - Sidebar becomes off-canvas drawer (existing rule does this;
     we reinforce + smooth transitions)
   - Topbar compresses; search may collapse to icon
   - Modals near full-screen
   - Field rows stack
   - Inbox: list OR chat (toggle), full-width
   ========================================================= */
@media (max-width: 768px) {
  /* Page padding tightest */
  .page, .main > .page, .main-inner { padding: 14px; }
  .page-head, .topbar { padding-left: 12px; padding-right: 12px; }

  /* Topbar at mobile */
  .topbar { gap: 6px; height: var(--topbar-h); }
  .topbar .tb-search,
  .topbar .search { display: none; } /* search bar hidden — could be re-exposed via icon if implemented */
  .crumbs { max-width: 140px; font-size: 12px; }
  .crumbs > * + * { margin-left: 4px; }
  .wallet-card { padding: 5px 8px; font-size: 12px; }
  .wallet-card .wallet-label { display: none; }
  .tb-icon, .icon-btn { width: 32px !important; height: 32px !important; }
  .tb-user .ar, .tb-user-name { display: none !important; }
  .tb-user { padding: 3px; }

  /* Topbar brand: ensure visible gap between the dark logo box and the
     "Zapim" text on mobile. */
  .topbar .topbar-mobile-brand {
    gap: 10px !important;
  }
  .topbar .topbar-mobile-brand .brand-mark {
    margin-right: 0;
  }

  /* Action icons in tables (Team Members, Custom Roles, etc.) — bump up
     the SVG and tap target so they're readable on a phone. */
  .tm-act button,
  .role-act button,
  .uc-act button,
  .dt-actions button,
  td.dt-actions button {
    padding: 7px !important;
    min-width: 32px;
    min-height: 32px;
  }
  .tm-act button svg,
  .role-act button svg,
  .uc-act button svg,
  td.dt-actions button svg {
    width: 16px !important;
    height: 16px !important;
  }

  /* ---------------------------------------------------------
     MOBILE DRAWER FIX
     The inline JS on every page toggles `.nav-open` on `.app`,
     but the original CSS only watched for `.sidebar.open` — so the
     drawer never slid in. Bridge both classes here, plus make the
     scrim visible (the JS only sets display, not opacity).
     Note: `.scrim` is a SIBLING of `.app` (not a child), so we use
     `:has()` to flip its visibility from the body level.
     --------------------------------------------------------- */
  .app.nav-open .sidebar,
  .app.nav-open aside.sidebar { transform: translateX(0) !important; }

  body:has(.app.nav-open) .scrim,
  body:has(.app.nav-open) #scrim {
    display: block !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    visibility: visible !important;
  }
  /* Lock body scroll while drawer is open */
  body:has(.app.nav-open) { overflow: hidden; }

  /* Sidebar drawer transitions (existing CSS sets fixed, slide via .nav-open) */
  .sidebar {
    transition: transform .25s cubic-bezier(.4, 0, .2, 1);
  }

  /* All KPI / card grids stack to single column on mobile */
  .kpi-grid,
  .bl-kpi-grid,
  .bm-grid,
  .ch-grid,
  .cards-grid,
  .cr-kpis,
  .cmp-kpi-bar,
  .cr-grid-2,
  .am-kpis { grid-template-columns: 1fr !important; }

  /* All field rows stack */
  .field-row { grid-template-columns: 1fr !important; gap: 12px !important; }

  /* Modals fill the screen with safe insets */
  .bl-modal { padding: 12px; align-items: flex-start; padding-top: 5vh; }
  .bl-mc, .bl-mc-xl, .modal-card {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 90vh;
    overflow-y: auto;
    margin: 0;
  }
  .bl-mc-acts, .bl-mc-foot, .bl-mc-foot-acts {
    flex-wrap: wrap;
    gap: 8px;
  }
  .bl-mc-acts .btn,
  .bl-mc-foot-acts .btn { flex: 1 1 auto; min-height: 40px; }

  /* Buttons / interactive controls — minimum touch targets */
  .btn { min-height: 38px; }
  .icon-btn, .tb-icon { min-height: 36px; min-width: 36px; }

  /* Inbox: list OR chat, full-width single column */
  .inbox-shell.no-rail,
  .inbox-shell.has-cards .inbox-3pane {
    grid-template-columns: 1fr !important;
  }
  .inbox-shell .conv-list {
    display: flex !important;
    width: 100%;
    flex-direction: column;
  }
  .inbox-shell .chat,
  .inbox-shell .detail { display: none; }
  /* When a conversation is selected (JS toggles 'show-chat' class on .inbox-shell),
     show the chat full-width and hide the list. Falls back gracefully if JS not present. */
  .inbox-shell.show-chat .conv-list { display: none !important; }
  .inbox-shell.show-chat .chat { display: flex !important; }
  .inbox-shell.has-cards .inbox-cards {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
  }

  /* Page heads: title + actions wrap nicely */
  .page-head { flex-wrap: wrap; row-gap: 10px; }
  .page-head .ph-actions, .page-head .pa { flex-wrap: wrap; }

  /* Toolbars wrap with reasonable gap */
  .dt-toolbar { flex-direction: column; align-items: stretch; gap: 10px; }
  .dt-toolbar > * { width: 100%; }
  .dt-toolbar .filters-btn,
  .dt-toolbar .btn { width: auto; }

  /* Templates / wizard / sign-up centered cards: full-width */
  .qc-card, .signup-card, .wiz-card, .auth-card {
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
  }

  /* Mobile back button (injected by inbox-mobile.js into chat-head) */
  .mobile-back-btn {
    display: grid !important;
    place-items: center;
    width: 32px;
    height: 32px;
    border-radius: 8px;
    background: transparent;
    border: 0;
    color: var(--ink);
    cursor: pointer;
    margin-right: 4px;
    flex-shrink: 0;
  }
  .mobile-back-btn:hover { background: var(--bg-panel); }
  .mobile-back-btn:active { background: var(--bg-muted); }

  /* ---------------------------------------------------------
     BRAND-MARK IMAGE FIX
     On mobile, the inner image needs more breathing room inside
     the dark box so the colorful Z stays clearly visible.
     --------------------------------------------------------- */
  .brand-mark img,
  .hero-logo .lg-mk img,
  .top-brand .bm img,
  .hdr-brand .bm img,
  .hdr-brand .ic img,
  .topbar .topbar-mobile-brand .brand-mark img,
  .lg-mk img,
  .bm img {
    width: 68% !important;
    height: 68% !important;
    object-fit: contain !important;
    margin: auto !important;
  }
  /* Restore "filled" feel on the auth pages that already have larger boxes */

  /* ---------------------------------------------------------
     UNIVERSAL TABLE HORIZONTAL SCROLL
     Wrap any table in a scrolling container so columns don't squish.
     Targets common table classes used across the app.
     --------------------------------------------------------- */
  table.dt,
  table.data-table,
  table.sp-table,
  table.waba-table,
  .waba-page table,
  .wallet-page table,
  .credit-mgmt table,
  .approvals table,
  .api-keys table,
  .webhooks-table,
  table[class*="-table"]:not(.no-scroll) {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    max-width: 100%;
  }
  table.dt thead, table.dt tbody, table.dt tr,
  table.sp-table thead, table.sp-table tbody, table.sp-table tr,
  table.waba-table thead, table.waba-table tbody, table.waba-table tr {
    white-space: nowrap;
  }

  /* Generic table-wrap classes */
  .dt-wrap, .table-wrap, .sp-tablewrap, .waba-tablewrap,
  .data-table-wrap, .table-scroll, [class*="tablewrap"] {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
    width: 100%;
  }

  /* ---------------------------------------------------------
     ANY FIXED-WIDTH ELEMENT inside .main shouldn't push past viewport
     --------------------------------------------------------- */
  .main img, .main video, .main iframe { max-width: 100%; height: auto; }

  /* Filter / action toolbars wrap with reasonable gap so buttons stay visible */
  .filter-bar, .filters-bar, .action-bar, .toolbar-row,
  .pageHeader-actions, .ph-actions, .pa,
  .qr-toolbar, .qr-actions,
  .opt-actions, .mm-actions, .mml-actions,
  .ml-actions, .ml-toolbar {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  .filter-bar > *, .filters-bar > *, .action-bar > *,
  .qr-toolbar > *, .qr-actions > *,
  .opt-actions > *, .mm-actions > *, .mml-actions > * {
    flex-shrink: 0;
  }
}

/* =========================================================
   ≤480px — small mobile
   Tightest compaction. Nothing removed; just denser.
   ========================================================= */
@media (max-width: 480px) {
  .page, .main > .page, .main-inner { padding: 10px; }
  .page-head { padding: 0 4px; }
  .topbar { padding: 0 8px; gap: 4px; }
  .crumbs { max-width: 90px; font-size: 11.5px; }
  .wallet-card { padding: 4px 7px; font-size: 11.5px; }
  .wallet-amount { font-size: 12px; }

  /* Page titles slightly smaller */
  .page-head h1, .page-head .ph-title, .page-h1 {
    font-size: 17px !important;
    line-height: 1.3 !important;
  }

  /* Card paddings */
  .card, .panel, .bl-kpi { padding: 12px !important; }

  /* Tighter modal */
  .bl-modal { padding: 8px; padding-top: 3vh; }
  .bl-mc { border-radius: 12px !important; }

  /* Tabs that overflow → make them scrollable */
  .pb-tabs, .lc-tabs, .nav-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
  }
  .pb-tabs > *, .lc-tabs > *, .nav-tabs > * { flex-shrink: 0; }
}

/* =========================================================
   Touch-device optimizations (any size, hover-incapable)
   ========================================================= */
@media (hover: none) and (pointer: coarse) {
  /* Buttons that revealed on hover stay visible */
  .dt tr .row-actions,
  .dt tr .tm-act,
  .dt tr [data-row-actions] { opacity: 1 !important; }
  /* Slightly larger hit targets for icon-only controls */
  .icon-btn, .tb-icon { min-width: 38px; min-height: 38px; }
}

/* =========================================================
   MOBILE — Page-specific patches (≤768px)
   These target the issues called out in user QA, while
   leaving desktop layouts untouched.
   ========================================================= */
@media (max-width: 768px) {

  /* ----- Brand-mark image sizing on mobile -----
     The `<img>` inside any brand-mark/logo dark box must use ~68%
     of its container so the colorful Z is well-centered on phones. */
  .brand-mark img,
  .topbar-mobile-brand .brand-mark img,
  .top-brand .bm img,
  .hdr-brand .bm img,
  .hdr-brand .ic img,
  .hero-logo .lg-mk img {
    width: 68% !important;
    height: 68% !important;
    object-fit: contain !important;
    margin: auto;
    display: block;
  }
  /* The dark boxes themselves: ensure flex centering so the 68%
     image sits in the middle */
  .brand-mark,
  .top-brand .bm,
  .hdr-brand .bm,
  .hdr-brand .ic,
  .hero-logo .lg-mk {
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    margin-right: 5px;
  }

  /* ----- Universal table horizontal-scroll fallback -----
     For any table whose parent is a card (.bl-card) or a generic
     wrapper, allow horizontal scroll so columns don't compress. */
  .bl-card:has(> table),
  .bl-card:has(> .bl-table) {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  .bl-table,
  .wl-table {
    min-width: 720px !important;
  }
  /* For .dt tables, keep min-width only when inside a scroll wrapper, so they
     scroll horizontally without breaking layouts that don't have a wrapper. */
  .dt-wrap .dt,
  .dt-scroll .dt {
    min-width: 720px !important;
  }
  /* Hide tbody if completely empty (avoids phantom-row blank tail) */
  .dt tbody:empty { display: none; }

  /* ----- My WABAs table: column min-widths so content doesn't squash ----- */
  .wl-table { table-layout: auto; }
  .wl-table th, .wl-table td { white-space: nowrap; padding: 10px 12px !important; }

  /* ----- Wallet page tables ----- */
  .bl-card { max-width: 100%; }
  .bl-card > .bl-table { display: table !important; }

  /* ----- Credit Management & API/Webhook tables: vertical safety -----
     If a card is constrained in height and content overflows,
     allow vertical scroll to the page itself by removing fixed heights. */
  .api-table-card, .webhook-table-card, .cm-table-card {
    max-height: none !important;
    overflow: visible !important;
  }

  /* ----- Approvals page: filters wrap & tables scroll ----- */
  .approvals-filters,
  .ap-filters,
  .approval-filters {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  .approvals-filters > *,
  .ap-filters > *,
  .approval-filters > * { flex: 1 1 calc(50% - 4px); min-width: 0; }

  /* ----- Quick Replies: filter button visibility ----- */
  .qr-filter-btn,
  .qr-toolbar .filters-btn,
  .qr-toolbar .btn-filter,
  [data-page="quick-replies"] .filters-btn { display: inline-flex !important; }

  /* ----- KPI + Filter row pattern (Templates / Agent Status / Quick Replies /
     MM Lite / Chat Tags). The pages place a flex row with KPI cards taking
     flex:1 and the Filters button on the right. On mobile the row needs to
     wrap and the Filters button drops to its own line so it's visible. */
  .kpi-filter-row,
  .kpi-bar {
    flex-wrap: wrap !important;
    align-items: stretch !important;
  }
  .kpi-filter-row > .sp-sumgrid,
  .kpi-bar > .sum-grid6 {
    flex: 1 1 100% !important;
  }
  /* Hide the vertical divider when row wraps */
  .kpi-filter-row > [aria-hidden="true"][style*="width:1px"],
  .kpi-bar .kpi-divider { display: none !important; }
  /* The button container takes its own row */
  .kpi-filter-row > div[style*="display:flex"][style*="align-items:center"]:last-child,
  .kpi-bar .kpi-end {
    flex: 1 1 100% !important;
    justify-content: flex-start !important;
  }
  .kpi-filter-row [id$="FiltersBtn"],
  .kpi-bar #fltBtn {
    width: 100%;
    justify-content: center;
  }

  /* ----- Opt-In page: action buttons (Audit / Upload / Add Number) ----- */
  .opt-actions,
  .optin-actions,
  .opt-toolbar {
    flex-wrap: wrap !important;
    row-gap: 8px;
    column-gap: 8px;
  }
  .opt-actions > .btn,
  .optin-actions > .btn,
  .opt-toolbar > .btn {
    flex: 1 1 auto;
    min-width: max-content;
  }

  /* ----- MM Lite: filter button visibility ----- */
  .mml-filters .filters-btn,
  .mml-toolbar .filters-btn,
  [data-page="mm-lite"] .filters-btn { display: inline-flex !important; }

  /* ----- Media Library: info text overflow ----- */
  .ml-info, .media-info, .ml-banner, .media-library-info,
  .ml-help, .info-line, .helper {
    word-break: break-word;
    overflow-wrap: anywhere;
    white-space: normal !important;
    max-width: 100% !important;
  }

  /* ----- Generate Invoice / Quick Create Invoices: stack email fields ----- */
  .gi-email-row, .qci-email-row, .invoice-email-grid {
    grid-template-columns: 1fr !important;
    flex-direction: column !important;
  }
  .gi-email-row > *, .qci-email-row > *, .invoice-email-grid > * {
    width: 100% !important;
    min-width: 0;
  }

  /* ----- Permissions page: hide duplicate '+' icons in Add Permission row -----
     The Add Permission button often renders an inline svg + a unicode '+'.
     Hide a duplicate '+' that's adjacent to the svg. */
  .perm-add-row .perm-plus-dup,
  .add-permission [data-dup-plus],
  .perm-add .icon-add + .icon-add { display: none !important; }

  /* ----- Custom Roles & Use Cases: trim tail blank space ----- */
  .custom-roles-table-wrap,
  .use-cases-table-wrap,
  .ucs-table-wrap { padding-bottom: 0 !important; }
  .ucs-empty-row,
  .custom-roles-empty-row { display: none !important; }

  /* Use Case Changes: New Use Case form responsive (inline-style 2-col grids) */
  #formView [style*="grid-template-columns:1fr 1fr"],
  #formView [style*="grid-template-columns: 1fr 1fr"] {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }
  /* Country search row inside form should wrap */
  #formView .input-group,
  #formView label[style*="position:relative"] { width: 100%; }
  #formView [style*="display:flex"][style*="margin-bottom:8px"] {
    flex-wrap: wrap;
    gap: 8px !important;
  }
  #formView [style*="display:flex"][style*="margin-bottom:8px"] > label {
    flex: 1 1 100% !important;
    min-width: 0;
  }
  #formView [style*="display:flex"][style*="margin-bottom:8px"] > label input {
    width: 100% !important;
    box-sizing: border-box;
  }
  #formView [style*="display:flex"][style*="margin-bottom:8px"] > .btn {
    flex: 1 1 calc(50% - 4px) !important;
    min-width: 0;
  }

  /* ----- Create Campaign: y-axis right overflow ----- */
  .create-campaign,
  .campaign-create,
  .cc-page { overflow-x: hidden; }
  .create-campaign .cc-grid,
  .campaign-create .cc-grid,
  .cc-grid { grid-template-columns: 1fr !important; }
}

/* =========================================================
   Landscape mobile — keep modals scrollable; topbar fits
   ========================================================= */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  .bl-mc { max-height: 86vh; }
  .bl-modal { padding-top: 4vh; }
}

/* =========================================================
   Page-specific patches for known overflow-prone layouts.
   These target classes that exist in only one or two pages
   but were producing horizontal overflow on small viewports.
   ========================================================= */

/* --- Dashboard (Zapim Portal.html) --- */
@media (max-width: 1000px) {
  .db-grid { grid-template-columns: 1fr !important; }
}
@media (max-width: 768px) {
  .db-page { padding: 16px !important; }
  .db-greeting { flex-wrap: wrap; gap: 12px; }
  .db-actions { width: 100%; }
  .db-actions .btn { width: 100%; justify-content: center; }
  .kpi-row { grid-template-columns: 1fr !important; gap: 10px !important; }
  .cmp-row { grid-template-columns: 1fr 60px 60px !important; gap: 6px !important; }
  /* Hide the 4th metric column (least critical — "Cost") on smallest screens to fit */
  .cmp-row > *:nth-child(5) { display: none; }
  .cmp-head > *:nth-child(5) { display: none; }
  .qa-grid { grid-template-columns: 1fr !important; }
}
@media (max-width: 480px) {
  .db-page { padding: 12px !important; }
  .kpi-card { padding: 14px !important; }
  .kpi-value { font-size: 22px !important; }
}

/* --- Templates (Templates.html) --- */
.sp-tablewrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  width: 100%;
}
@media (max-width: 1024px) {
  .sp-actions { flex-wrap: wrap; gap: 8px; }
}
@media (max-width: 768px) {
  .sp-actions { flex-direction: column; align-items: stretch; }
  .sp-actions > * { width: 100%; }
  /* Don't force a fixed min-width — let the table take exactly the width
     its content needs. That way there's no trailing empty space after the
     last (Actions) column on Chat Tags / Opt-In / etc. */
  .sp-table {
    width: max-content !important;
    min-width: 100% !important;
  }
}

/* --- Quick Create / wizard pages --- */
@media (max-width: 768px) {
  .qc-info-tip,
  .wiz-info-tip { max-width: calc(100vw - 60px) !important; }
  .qc-card, .wiz-card { padding: 16px !important; }
  .wiz-stepper { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .wiz-stepper > * { flex-shrink: 0; }
}

/* --- Generic safety: any element with fixed pixel widths >= 360 must shrink at narrow viewports --- */
@media (max-width: 480px) {
  [style*="width:520px"],
  [style*="width:500px"],
  [style*="width:480px"],
  [style*="width:460px"],
  [style*="width:440px"],
  [style*="width:420px"],
  [style*="width:400px"],
  [style*="width: 520px"],
  [style*="width: 500px"],
  [style*="width: 480px"],
  [style*="width: 460px"],
  [style*="width: 440px"],
  [style*="width: 420px"],
  [style*="width: 400px"] {
    width: 100% !important;
    max-width: 100% !important;
  }
}

/* =========================================================
   PAGE-SPECIFIC MOBILE PATCHES
   Each block targets a specific page's idiosyncratic layout
   issues without changing desktop appearance.
   ========================================================= */

/* --- Media Library: info banner text was overflowing because the icon
   and inline text don't shrink in flex layout. Switch to block-with-
   icon-floated layout. Plus show the asset grid as 3 columns on phones. --- */
@media (max-width: 768px) {
  .sp-info {
    display: block !important;
    position: relative;
    padding: 12px 14px 12px 38px !important;
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  .sp-info .ib-icon {
    position: absolute;
    left: 12px;
    top: 14px;
  }
  /* Asset grid: 3 columns on phones */
  .ml-assets {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 8px !important;
  }
  .asset-card { font-size: 11px; }
  .asset-card .asset-name { font-size: 11px; }

  /* Type tabs (All / Image / Video / Document / Audio): allow horizontal
     scroll instead of clipping when they don't fit on narrow phones. */
  .ml-typetabs {
    display: flex !important;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
  }
  .ml-typetabs::-webkit-scrollbar { display: none; }
  .ml-typetabs .tt-tab { flex-shrink: 0; }
}

/* --- Wallet page: balance card stacks so the Statement + Top Up buttons
   are not pushed off the right edge. --- */
@media (max-width: 768px) {
  .bl-balance {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 14px !important;
    padding: 18px 18px !important;
  }
  .bl-balance .bl-actions {
    width: 100%;
    flex-wrap: wrap;
    gap: 8px !important;
  }
  .bl-balance .bl-actions > button {
    flex: 1 1 calc(50% - 4px);
    justify-content: center;
  }
}

/* --- Register WABA: method-toggle stacks and labels can wrap so
   "Meta Embedded Signup" + "Recommended" pill don't overflow. --- */
@media (max-width: 768px) {
  .method-toggle {
    display: flex !important;
    flex-direction: column;
    width: 100%;
    gap: 4px;
  }
  .method-toggle button {
    width: 100%;
    height: auto !important;
    min-height: 40px;
    padding: 8px 12px !important;
    flex-wrap: wrap;
    justify-content: flex-start;
    white-space: normal;
    text-align: left;
    line-height: 1.3;
  }
  .method-toggle button .tone-pill {
    margin-left: auto !important;
  }
}

/* --- Quick Create Account email chip-input: force the input onto its
   own row inside the chip-input so chips + placeholder don't overlap
   the next field's label. --- */
@media (max-width: 768px) {
  .chip-input { padding: 6px 8px !important; }
  .chip-input input {
    flex: 1 1 100% !important;
    min-width: 100% !important;
    width: 100%;
    padding: 4px !important;
  }
}

/* --- Create Campaign / Create Template: scroll-to-preview fix ---
   Both pages use a desktop-only locked-viewport flex layout so the form
   column scrolls independently of the preview column. On mobile that
   model doesn't work (one column), and it BLOCKS the page from scrolling
   to reveal the preview. Restore the global `.main { overflow-y: auto }`
   scroll model for these pages on mobile. */
@media (max-width: 768px) {
  /* Override inline `style="overflow:hidden;height:calc(100vh-56px)"` on .main
     and the page's inline-style-block setting `display: flex` on .main */
  body .main.main {
    display: block !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    height: calc(100vh - var(--topbar-h)) !important;
  }
  /* Page wrappers: stop trying to be flex columns clipped to viewport */
  body .main > .page,
  body .main > .sp-page,
  body .main .page,
  body .main .sp-page {
    flex: none !important;
    display: block !important;
    overflow: visible !important;
    height: auto !important;
    min-height: 0 !important;
  }
  /* Create Campaign layout */
  body .main > .page > .create-layout,
  body .create-layout {
    flex: none !important;
    overflow: visible !important;
    height: auto !important;
  }
  .create-layout.create-layout {
    grid-template-columns: 1fr !important;
    grid-template-rows: auto auto;
    display: grid !important;
  }
  .create-layout.create-layout .create-form { order: 1; }
  .create-layout.create-layout .create-aside-fixed,
  .create-layout.create-layout .create-aside { order: 2; }
  .create-aside-fixed.create-aside-fixed,
  .create-aside.create-aside {
    position: static !important;
    height: auto !important;
    border-left: 0 !important;
    border-top: 1px solid var(--line);
    width: 100% !important;
    max-width: 100% !important;
    overflow: visible !important;
  }
  .create-form { padding: 14px !important; }

  /* Create Template layout: same model — form first, preview second,
     no per-column overflow scroll */
  body .ct-layout.ct-layout {
    display: grid !important;
    grid-template-columns: 1fr !important;
    grid-template-rows: auto auto;
    flex: none !important;
    overflow: visible !important;
    height: auto !important;
    gap: 14px !important;
  }
  body .ct-layout .ct-form,
  body .ct-form.ct-form {
    overflow: visible !important;
    padding-right: 0 !important;
    min-height: 0 !important;
    order: 1;
  }
  body .ct-layout .ct-preview,
  body .ct-preview.ct-preview {
    overflow: visible !important;
    padding-right: 0 !important;
    min-height: 0 !important;
    order: 2;
  }
}

/* --- Approvals page: filters + tabs wrap, table scrolls --- */
@media (max-width: 768px) {
  .ap-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap !important;
    gap: 4px !important;
  }
  .ap-tabs > * { flex-shrink: 0; }
  .sum-grid { grid-template-columns: 1fr !important; gap: 10px; }
  .sp-head { flex-wrap: wrap !important; gap: 10px; }
  .sp-head .sp-actions { width: 100%; flex-wrap: wrap; }
  .ap-mode { display: block; margin-top: 4px; }

  /* Bulk action bar — on mobile, stack into 2 rows so the buttons don't
     squash on top of the "selected/Total impact" labels (image 12) */
  .bulk-bar {
    flex-wrap: wrap !important;
    align-items: stretch !important;
    gap: 10px !important;
  }
  .bulk-bar .bb-l {
    flex: 1 1 100% !important;
    flex-wrap: wrap;
    row-gap: 4px;
  }
  .bulk-bar .bb-r {
    flex: 1 1 100% !important;
    justify-content: flex-end;
  }
  .bulk-bar .bb-r button { flex: 1 1 auto; justify-content: center; }

  /* Each request row — keep checkbox + content on one row, drop the action
     buttons to a new row that spans full width so View / Reject / Approve
     are readable instead of stacked on top of each other (image 12) */
  .req-row {
    grid-template-columns: 28px 1fr !important;
  }
  .req-row .req-acts {
    grid-column: 1 / -1;
    flex-wrap: wrap;
    gap: 8px !important;
  }
  .req-row .req-acts .ap-btn {
    flex: 1 1 calc(33.33% - 6px);
    justify-content: center;
  }
}

/* --- Webhook Endpoints: <table> sits directly in <div class="sec-card">.
   That parent must scroll horizontally on mobile so the URL column doesn't
   clip (image 14). Same belt-and-braces protection for any other page that
   places an sp-table directly inside a sec-card. --- */
@media (max-width: 768px) {
  .sec-card:has(> table),
  .sec-card:has(> .sp-table),
  .sec-card:has(> .dt) {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }
}

/* --- Issue Rewards modal (Wallet Management): the row uses a 4-col grid
   `32px 1fr auto auto` where the two trailing `auto` cells hold the
   "1,842 sent" / "1,012 ok" stats. On a 390px viewport these spill past
   the modal edge (image 10). Reflow to 2-col, push stats below. --- */
@media (max-width: 768px) {
  .wm-camp-list .wm-camp-row {
    grid-template-columns: 32px 1fr !important;
    row-gap: 6px !important;
  }
  /* All children after the second column drop onto a new row spanning the
     content column. We can't use nth-child reliably without knowing exact
     children counts, but `:nth-child(n+3)` covers 3rd, 4th, 5th… */
  .wm-camp-list .wm-camp-row > :nth-child(n+3) {
    grid-column: 2 !important;
    text-align: left !important;
    font-size: 11.5px;
  }
  /* Modal footer with Cancel + Issue Rewards buttons — let it stack */
  .bl-mc-foot { flex-wrap: wrap; gap: 10px; }
  .bl-mc-foot-acts { width: 100%; justify-content: flex-end; }
}

/* --- Use Case Changes form: stack inline 2-col grids on mobile --- */
@media (max-width: 768px) {
  /* Override inline grid-template-columns: 1fr 1fr */
  .uc-page [style*="grid-template-columns:1fr 1fr"],
  .uc-page [style*="grid-template-columns: 1fr 1fr"],
  .main [style*="grid-template-columns:1fr 1fr"]:not(.kpi-row):not(.field-row),
  .main [style*="grid-template-columns: 1fr 1fr"]:not(.kpi-row):not(.field-row),
  .main [style*="grid-template-columns:1fr 1fr 1fr"],
  .main [style*="grid-template-columns: 1fr 1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }
}

/* --- Custom Roles: remove blank space below 1-row table --- */
@media (max-width: 768px) {
  .dt-wrap { min-height: 0 !important; }
  .dt-scroll { min-height: 0 !important; }
  .dt-empty { padding: 24px 12px !important; }
}

/* --- Quick Replies: ensure filter button stays visible --- */
@media (max-width: 768px) {
  .qr-toolbar, .qr-actions, .qr-head {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  .qr-toolbar .btn, .qr-actions .btn { flex-shrink: 0; }
}

/* --- Opt-In Management: Audit / Upload / Add Number buttons --- */
@media (max-width: 768px) {
  /* sp-actions is already stacked via earlier rule; ensure buttons full-width and centered text */
  .sp-actions .btn {
    justify-content: center;
    text-align: center;
  }
}

/* --- MM Lite: filter button visibility & toolbar wrap --- */
@media (max-width: 768px) {
  .mm-toolbar, .mml-toolbar, .mml-head, .filter-row {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  .filter-panel {
    flex-wrap: wrap !important;
    gap: 10px !important;
  }
  .filter-panel .flt-group {
    width: 100% !important;
    min-width: 0 !important;
  }
}

/* --- Media Library: info text overflow fix --- */
@media (max-width: 768px) {
  .ml-banner, .ml-info, .bl-banner.info,
  .ml-page .info-card, .ml-card .help-text {
    word-break: break-word;
    overflow-wrap: anywhere;
    max-width: 100%;
  }
  .ml-banner > div, .ml-info > div, .bl-banner.info > div {
    min-width: 0;
    flex: 1 1 auto;
  }
}

/* --- Generate Invoice: prevent email field/content overlap --- */
@media (max-width: 768px) {
  .invoice-page .field-row,
  .gen-invoice [style*="grid-template-columns"],
  .gen-invoice-meta {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }
  .invoice-meta-row {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
  }
  .invoice-page input[type="email"],
  .gen-invoice input[type="email"] {
    width: 100% !important;
    min-width: 0;
  }
}

/* --- Wallet / Wallet Management / Credit Management: tables --- */
@media (max-width: 880px) {
  .wallet-page .dt,
  .wallet-page table,
  .credit-mgmt .dt,
  .credit-mgmt table,
  .credits-page .dt,
  .credits-page table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    max-width: 100%;
  }
}

/* --- API Keys / Webhook Endpoints: vertical overflow fix on long content --- */
@media (max-width: 768px) {
  .api-keys-page .key-row,
  .webhooks-page .endpoint-row,
  .api-page .row {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  .api-keys-page .key-value,
  .webhooks-page .endpoint-url {
    word-break: break-all;
    overflow-wrap: anywhere;
    max-width: 100%;
  }
  /* Ensure these page tables also scroll horizontally */
  .api-keys-page table,
  .webhooks-page table,
  .api-page table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    max-width: 100%;
  }
}

/* =========================================================
   Print: not in scope but harmless
   ========================================================= */
@media print {
  .sidebar, .topbar, .pane-resize-handle, .sidebar-toggle { display: none !important; }
  .app { grid-template-columns: 1fr !important; }
}

