/* =============================================================
   DIVCAAS — Shared Responsive Layer (responsive.css)
   Loaded LAST on every active page so it can adapt any layout
   to mobile phones without breaking the desktop experience.
   Strategy: desktop styles are untouched; everything here is
   scoped behind max-width media queries (mobile-first overrides).
   ============================================================= */

/* ---------- 0. Global safety: never allow horizontal scroll ---------- */
html, body {
  max-width: 100%;
  overflow-x: hidden;
  -webkit-text-size-adjust: 100%;
}
*, *::before, *::after { box-sizing: border-box; }

/* Long words / URLs / codes shouldn't blow out the viewport */
p, span, td, th, li, h1, h2, h3, h4, h5, h6, a, label, div {
  overflow-wrap: break-word;
  word-wrap: break-word;
}

/* Media should always shrink to fit */
img, video, canvas, svg, iframe, embed, object {
  max-width: 100%;
  height: auto;
}

/* ============================================================
   TABLET & DOWN  (<= 1024px)
   ============================================================ */
@media (max-width: 1024px) {
  /* Allow wide tables to scroll horizontally inside their wrapper
     instead of pushing the whole page wide. */
  .table-wrap, .table-responsive, .data-table-wrap,
  .table-container, .records-table-wrap {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* ============================================================
   PHONES & SMALL TABLETS  (<= 768px)
   Core mobile transformation breakpoint.
   ============================================================ */
@media (max-width: 768px) {

  /* ---------- Base typography & spacing ---------- */
  body { font-size: 15px; line-height: 1.5; }
  h1 { font-size: 1.6rem !important; line-height: 1.25; }
  h2 { font-size: 1.35rem !important; line-height: 1.3; }
  h3 { font-size: 1.15rem !important; }

  /* ---------- Containers: collapse fixed widths & big padding ---------- */
  .container, .page, .main, .content, main, .wrapper, .app-content,
  .dashboard, .dashboard-content, .panel, .card, .section, section {
    width: 100% !important;
    max-width: 100% !important;
  }
  .container, .page, .content, main .wrapper {
    padding-left: 14px !important;
    padding-right: 14px !important;
  }

  /* ---------- Generic grid systems -> single column ---------- */
  .grid, .grid-2, .grid-3, .grid-4,
  .cols-2, .cols-3, .cols-4,
  .stats-grid, .cards-grid, .card-grid, .kpi-grid,
  .two-col, .three-col, .four-col, .row.cards,
  .summary-grid, .metrics-grid, .tiles {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }

  /* Flex rows that were side-by-side -> stack vertically */
  .flex-row, .row-flex, .toolbar, .actions-bar, .filters,
  .header-actions, .page-header, .toolbar-row, .controls-row {
    flex-direction: column !important;
    align-items: stretch !important;
  }
  .toolbar > *, .filters > *, .actions-bar > * {
    width: 100%;
  }

  /* ---------- Sidebars: don't reserve desktop gutter ---------- */
  .content-with-sidebar, .with-sidebar, .layout, .app-layout {
    display: block !important;
  }
  .main-with-sidebar, .content-area {
    margin-left: 0 !important;
    width: 100% !important;
  }

  /* ---------- Touch targets: >= 44px tall/clickable ---------- */
  button, .btn, .button, input[type="button"], input[type="submit"],
  a.btn, .nav-link, .tab, .pill, select, .dropdown-toggle {
    min-height: 44px;
  }
  input, select, textarea, .form-control, .input {
    min-height: 44px;
    font-size: 16px !important;   /* prevents iOS zoom-on-focus */
    width: 100%;
  }
  .btn, .button { padding: 11px 16px; }

  /* Form fields stack full width */
  .form-row, .field-row, .form-group.inline {
    flex-direction: column !important;
  }
  .form-row > *, .field-row > * { width: 100% !important; }

  /* ---------- Buttons in a row -> wrap nicely ---------- */
  .btn-group, .button-group, .actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
  }

  /* ============================================================
     TABLE -> CARD STACKING
     Any table marked .responsive-stack (or inside .stack-on-mobile)
     turns each row into a stacked card with label::before headers.
     Tables NOT marked get horizontal scroll instead (safe default).
     ============================================================ */
  table:not(.responsive-stack) {
    display: block;
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
  }

  table.responsive-stack,
  .stack-on-mobile table {
    display: block;
    width: 100%;
    white-space: normal;
  }
  table.responsive-stack thead,
  .stack-on-mobile table thead {
    position: absolute;
    left: -9999px;
    top: -9999px;
  }
  table.responsive-stack tr,
  .stack-on-mobile table tr {
    display: block;
    margin: 0 0 12px 0;
    border: 1px solid rgba(0,0,0,0.12);
    border-radius: 10px;
    padding: 8px 12px;
    background: #fff;
  }
  table.responsive-stack td,
  .stack-on-mobile table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 8px 0;
    border: none;
    border-bottom: 1px solid rgba(0,0,0,0.06);
    text-align: right;
    white-space: normal;
  }
  table.responsive-stack td:last-child,
  .stack-on-mobile table td:last-child { border-bottom: none; }
  table.responsive-stack td::before,
  .stack-on-mobile table td::before {
    content: attr(data-label);
    font-weight: 600;
    text-align: left;
    color: #475569;
    flex: 0 0 45%;
  }

  /* ---------- Modals / dialogs full-screen-ish on phones ---------- */
  .modal, .modal-content, .dialog, .modal-box, .modal-card,
  .drawer, .panel-modal {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 92vh !important;
    margin: 0 !important;
    border-radius: 14px 14px 0 0 !important;
    overflow-y: auto;
  }
  .modal-overlay, .modal-backdrop { padding: 0 !important; }

  /* ---------- Hide elements explicitly desktop-only ---------- */
  .desktop-only, .hide-mobile { display: none !important; }
  .mobile-only, .show-mobile { display: block !important; }

  /* ---------- Tabs scroll horizontally instead of wrapping awkwardly ---------- */
  .tabs, .tab-bar, .nav-tabs, .segmented {
    display: flex;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    gap: 4px;
  }
  .tabs::-webkit-scrollbar, .tab-bar::-webkit-scrollbar { height: 0; }

  /* ---------- Cards a touch tighter ---------- */
  .card, .panel, .box { padding: 14px !important; }
}

/* ============================================================
   SMALL PHONES  (<= 480px)
   ============================================================ */
@media (max-width: 480px) {
  h1 { font-size: 1.4rem !important; }
  h2 { font-size: 1.2rem !important; }
  .stat-value, .kpi-value, .metric-value { font-size: 1.5rem !important; }

  /* Logo / brand text smaller */
  .logo, .brand, .navbar-brand { font-size: 1.05rem !important; }

  /* Reduce big hero paddings */
  .hero, .login-hero, .splash { padding: 24px 16px !important; }
}

/* ============================================================
   COARSE POINTERS (touch devices regardless of width)
   ============================================================ */
@media (pointer: coarse) {
  a, button, .btn, .nav-link, .tab, .icon-btn { touch-action: manipulation; }
}

/* ============================================================
   SHARED MOBILE NAV (hamburger drawer)
   Pages can opt-in by including:
     <button class="rs-nav-toggle" aria-label="Menu"><i class="fas fa-bars"></i></button>
     <div class="rs-nav-overlay"></div>
   and wrapping their primary nav in .rs-mobile-drawer.
   Hidden on desktop; shown <=768px.
   ============================================================ */
.rs-nav-toggle { display: none; }

@media (max-width: 768px) {
  .rs-nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border: none;
    background: transparent;
    font-size: 1.4rem;
    color: inherit;
    cursor: pointer;
  }
  .rs-nav-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 9998;
    opacity: 0;
    visibility: hidden;
    transition: opacity .2s ease;
  }
  .rs-nav-overlay.open { opacity: 1; visibility: visible; }

  .rs-mobile-drawer {
    position: fixed !important;
    top: 0;
    left: 0;
    bottom: 0;
    width: 82%;
    max-width: 320px;
    background: #fff;
    z-index: 9999;
    transform: translateX(-100%);
    transition: transform .25s ease;
    overflow-y: auto;
    box-shadow: 2px 0 18px rgba(0,0,0,0.18);
  }
  .rs-mobile-drawer.open { transform: translateX(0); }
}
