/* ============================================================
   RCO ERP — Mobile responsive CSS v4
   
   Design:
   - Sidebar slides in from left as drawer (controlled by data-open="1")
   - Hamburger button in topbar opens it
   - Backdrop overlay (controlled by display style from React)
   - Tables convert to card-stacked rows on mobile (no horizontal scroll)
   - Modals near-fullscreen
   - Inputs use 16px to prevent iOS zoom
   ============================================================ */

html { -webkit-text-size-adjust: 100%; }
body { -webkit-tap-highlight-color: rgba(76,175,80,.15); }

@media (max-width: 768px) {
  ::-webkit-scrollbar { width: 0; height: 0; }
}

/* ===========================
   PHONES & SMALL TABLETS (≤768px)
   =========================== */
@media (max-width: 768px) {

  /* ---- MAIN APP CONTAINER stays as flex row, sidebar becomes overlay ---- */
  /* (No need to flip to column — sidebar floats above content) */

  /* ---- SIDEBAR DRAWER ---- */
  div[data-mobile="sidebar"] {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    width: 260px !important;
    max-width: 80vw !important;
    height: 100vh !important;
    z-index: 999 !important;
    transform: translateX(-100%) !important;
    transition: transform 0.25s ease-out !important;
    box-shadow: 2px 0 16px rgba(0,0,0,.3) !important;
  }
  div[data-mobile="sidebar"][data-open="1"] {
    transform: translateX(0) !important;
  }

  /* Sidebar logo header */
  div[data-mobile="sidebar"] > div:first-child {
    padding: 16px 18px !important;
  }

  /* Nav scrolls vertically inside drawer */
  div[data-mobile="sidebar"] nav {
    padding: 6px 0 !important;
  }

  /* Section title headers (MASTERS, SALES, etc. inside nav) */
  div[data-mobile="sidebar"] nav > div > div:first-child {
    padding: 12px 18px 4px !important;
  }

  /* Nav items — bigger tap targets */
  div[data-mobile="sidebar"] nav > div > div {
    padding: 12px 18px !important;
    font-size: 14px !important;
    margin: 2px 8px !important;
  }
  /* Section title — keep small */
  div[data-mobile="sidebar"] nav > div > div:first-child:not([role]) {
    /* This selector intentionally narrow — kept compact already */
  }

  /* Backdrop */
  div[data-mobile="backdrop"] {
    display: none !important;
  }
  /* When menu is open, React sets display:block inline → ensure it covers properly */
  div[data-mobile="sidebar"][data-open="1"] ~ div[data-mobile="backdrop"],
  div[data-mobile="backdrop"][style*="display: block"],
  div[data-mobile="backdrop"][style*="display:block"] {
    display: block !important;
  }

  /* ---- MAIN CONTENT AREA ---- */
  div[data-mobile="main"] {
    width: 100% !important;
    flex: 1 1 auto !important;
  }

  /* ---- TOPBAR ---- */
  div[data-mobile="topbar"] {
    padding: 0 12px !important;
    height: 48px !important;
  }
  /* Show hamburger on mobile */
  button[data-mobile="hamburger"] {
    display: inline-flex !important;
  }
  /* Hide brand text on small phones — saves room for page title */
  span[data-mobile="topbar-brand"] {
    display: none !important;
  }
  /* Quote Builder satellite page: hide its own sidebar entirely on mobile,
     show small ← ERP back button in topbar instead */
  div[data-mobile="qb-sidebar"] {
    display: none !important;
  }
  button[data-mobile="qb-back"] {
    display: inline-flex !important;
    align-items: center !important;
  }

  /* Content padding tighter on mobile */
  div[data-mobile="content"] {
    padding: 12px !important;
  }
  /* Satellite PHP pages don't have the data-mobile wrapper — pad the body itself, but only when there's no main app */
  body:not(:has(div[data-mobile="app"])) {
    padding: 8px !important;
  }

  /* ===========================================
     [TABLES] — convert to stacked cards
     =========================================== */
  
  /* Generic transformation — every table inside main content becomes card-stack */
  body table {
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    box-shadow: none !important;
    background: transparent !important;
    border-collapse: collapse !important;
  }
  /* Override scroll-wrappers that satellite pages put around tables —
     they had overflow:auto + minWidth on the table to allow horizontal scroll on desktop.
     On mobile we want full-width cards instead.
     Match BOTH spaced (rendered) and unspaced forms. */
  div[style*="overflow: auto"]:has(> table),
  div[style*="overflow:auto"]:has(> table),
  div[style*="overflow: scroll"]:has(> table),
  div[style*="overflow:scroll"]:has(> table) {
    overflow: visible !important;
    overflow-x: visible !important;
    padding: 0 !important;
  }
  body table thead {
    display: none !important;  /* Hide the header row */
  }
  body table tbody {
    display: block !important;
  }
  /* Each row is a 2-column grid card — labels above values, 2 per row */
  body table tbody tr {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 8px 12px !important;
    background: #fff !important;
    border: 1px solid #E8EAED !important;
    border-radius: 8px !important;
    margin-bottom: 8px !important;
    padding: 10px 12px !important;
    box-shadow: 0 1px 2px rgba(0,0,0,.04) !important;
    align-items: start !important;
  }
  /* Each TD is one grid cell */
  body table tbody td {
    display: block !important;
    width: auto !important;
    border: none !important;
    padding: 0 !important;
    margin: 0 !important;
    text-align: left !important;
    white-space: normal !important;
    font-size: 12px !important;
    line-height: 1.4 !important;
    min-width: 0 !important;
    word-break: break-word !important;
    overflow: hidden !important;
  }
  /* Label appears above value as a small uppercase line */
  body table tbody td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-weight: 600;
    color: #9CA3AF;
    font-size: 9px;
    text-transform: uppercase;
    letter-spacing: 0.4px;
    margin-bottom: 1px;
    line-height: 1.1;
  }
  /* Empty cells — collapse them so they don't take space */
  body table tbody td:empty {
    display: none !important;
  }
  /* Hide checkbox-only cells on mobile (bulk select isn't useful on phone) */
  body table tbody td:has(> input[type="checkbox"]:only-child) {
    display: none !important;
  }
  /* Hide tiny color-bar cells (priority indicators in Leads, etc.) — they have width:4 */
  body table tbody td[style*="width:4"],
  body table tbody td[style*="width: 4px"] {
    display: none !important;
  }
  /* Cells without a data-label that need full row width (Customer name, Notes) — span 2 cols */
  body table tbody td:not([data-label]) {
    grid-column: 1 / -1 !important;
  }
  /* Last cell (action buttons) — full width, top divider, CENTER-aligned buttons */
  body table tbody tr td:last-child {
    grid-column: 1 / -1 !important;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 5px !important;
    margin-top: 4px !important;
    padding-top: 8px !important;
    border-top: 1px solid #F3F4F6 !important;
    justify-content: center !important;
  }
  /* Reset ::before on action button cell (which doesn't have data-label) */
  body table tbody tr td:last-child::before {
    display: none !important;
  }
  /* Action buttons inside last cell — slim */
  body table tbody tr td:last-child button {
    min-height: 28px !important;
    padding: 5px 12px !important;
    font-size: 11px !important;
  }
  /* Inner divs within td values shouldn't claim block width awkwardly */
  body table tbody td > div {
    font-size: inherit !important;
    line-height: 1.35 !important;
  }
  /* Make the primary value (first text node) bolder AND centered */
  body table tbody td[data-label="Name"],
  body table tbody td[data-label="Customer"],
  body table tbody td[data-label="Item"],
  body table tbody td[data-label="Quote #"],
  body table tbody td[data-label="Survey #"],
  body table tbody td[data-label="Ticket"],
  body table tbody td[data-label="Employee"] {
    grid-column: 1 / -1 !important;
    font-size: 14px !important;
    font-weight: 700 !important;
    margin-bottom: 2px !important;
    text-align: center !important;
    padding-bottom: 6px !important;
    border-bottom: 1px solid #F3F4F6 !important;
  }
  body table tbody td[data-label="Name"]::before,
  body table tbody td[data-label="Customer"]::before,
  body table tbody td[data-label="Item"]::before,
  body table tbody td[data-label="Quote #"]::before,
  body table tbody td[data-label="Survey #"]::before,
  body table tbody td[data-label="Ticket"]::before,
  body table tbody td[data-label="Employee"]::before {
    display: none !important;  /* Don't show "NAME:" label for the heading */
  }

  /* ---- MODALS ---- */
  /* On mobile, pin modal to TOP (not vertically centered) so X button is reachable
     and the user can scroll the modal content from top to bottom of form. */
  div[style*="position: fixed"][style*="background: rgba(0, 0, 0, 0.4)"],
  div[style*="position:fixed"][style*="background:rgba(0,0,0,.4)"],
  div[style*="position: fixed"][style*="background: rgba(0,0,0,.4)"] {
    align-items: flex-start !important;
    padding: 8px 0 !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
  }
  div[style*="position: fixed"][style*="background: rgba(0, 0, 0, 0.4)"] > div,
  div[style*="position:fixed"][style*="background:rgba(0,0,0,.4)"] > div,
  div[style*="position: fixed"][style*="background: rgba(0,0,0,.4)"] > div {
    width: 95vw !important;
    max-width: 95vw !important;
    max-height: none !important;
    margin: 0 auto !important;
    padding: 14px !important;
    border-radius: 10px !important;
    overflow: visible !important;
  }

  /* ---- FORMS / GRIDS — collapse multi-col to single col ---- */
  div[style*="grid-template-columns: 1fr 1fr 1fr 1fr"],
  div[style*="grid-template-columns: 1fr 1fr 1fr"],
  div[style*="grid-template-columns: 1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }

  /* ---- INPUTS ---- */
  input, select, textarea {
    font-size: 16px !important;  /* prevents iOS zoom */
    padding: 10px 12px !important;
    min-height: 42px !important;
    box-sizing: border-box !important;
    max-width: 100% !important;
  }
  textarea {
    min-height: 80px !important;
  }

  /* ---- BUTTONS ---- */
  button {
    min-height: 36px !important;
    padding: 8px 14px !important;
    font-size: 13px !important;
  }
  button[style*="padding: 5px 10px"],
  button[style*="padding:5px 10px"] {
    min-height: 28px !important;
    padding: 5px 10px !important;
    font-size: 11px !important;
  }
  /* Sidebar logout / hamburger keep compact */
  div[data-mobile="sidebar"] button {
    min-height: 30px !important;
  }
  button[data-mobile="hamburger"] {
    min-height: 36px !important;
    width: 36px !important;
    padding: 0 !important;
  }

  /* ---- HEADERS ---- */
  h1 { font-size: 20px !important; }
  h2 { font-size: 17px !important; }
  h3 { font-size: 15px !important; }

  /* ---- DASHBOARD STAT TILES — 2 per row on mobile ---- */
  div[style*="display: grid"][style*="grid-template-columns: repeat"],
  div[style*="display:grid"][style*="grid-template-columns:repeat"] {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }

  /* ---- WHITE CARDS (background:#fff with padding) ---- */
  div[style*="background: rgb(255, 255, 255)"][style*="border-radius: 10px"][style*="padding: 18px"],
  div[style*="background:#fff"][style*="border-radius:10px"][style*="padding:18px"] {
    padding: 12px !important;
  }

  /* ---- FILTERS / SEARCH BARS row — stack vertically ---- */
  /* Targets: Customer page filter row, Lead filter, etc. */
  div[style*="display: flex"][style*="gap: 10"],
  div[style*="display:flex"][style*="gap:10"] {
    flex-wrap: wrap !important;
  }
}

/* ===========================
   SMALL PHONES (≤480px) - tighter
   =========================== */
@media (max-width: 480px) {
  body { font-size: 13px !important; }
  h1 { font-size: 18px !important; }
  h2 { font-size: 16px !important; }
  h3 { font-size: 14px !important; }

  /* Modals fully fullscreen */
  div[style*="position: fixed"][style*="background: rgba(0, 0, 0, 0.4)"] > div,
  div[style*="position:fixed"][style*="background:rgba(0,0,0,.4)"] > div {
    width: 100vw !important;
    max-width: 100vw !important;
    max-height: none !important;
    margin: 0 !important;
    padding: 12px !important;
    border-radius: 0 !important;
    overflow: visible !important;
  }

  /* Stat tiles padding */
  div[style*="display: grid"][style*="grid-template-columns: repeat"] > div {
    padding: 10px !important;
  }
}

/* ===========================
   DESKTOP (>768px) — keep as-is
   =========================== */
@media (min-width: 769px) {
  /* Hide hamburger on desktop */
  button[data-mobile="hamburger"] {
    display: none !important;
  }
  /* Hide backdrop on desktop */
  div[data-mobile="backdrop"] {
    display: none !important;
  }
}
