/* ==========================================================================
   ZAdvocates LLP ERP — responsive layer (phone, tablet, laptop, large screens)
   Loaded AFTER theme.css / premium_ui.css so it can refine them.
   Breakpoints:  ≤1024 tablet & small laptop · ≤768 tablet portrait / large phone
                 ≤600 phone · ≤420 small phone · ≥1600 large monitors
   ========================================================================== */

/* ---------- Safe defaults for every screen ---------- */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
*, *::before, *::after { box-sizing: border-box; }
img, video, canvas, svg, iframe { max-width: 100%; }
img, video { height: auto; }
.main-area, .content, .topbar-title { min-width: 0; }
.content { overflow-wrap: anywhere; }
pre, code { max-width: 100%; overflow-x: auto; }
table { max-width: 100%; }

/* Tables are wrapped in this by responsive.js so wide tables scroll sideways
   inside their card instead of stretching the whole page. */
.zad-table-scroll { width: 100%; max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.zad-table-scroll > table { min-width: 640px; }

/* Hamburger button + overlay exist everywhere but only show on small screens */
.zad-menu-btn {
  display: none; flex: 0 0 auto; width: 40px; height: 40px; padding: 0;
  align-items: center; justify-content: center;
  border: 1px solid rgba(16,43,92,.16); border-radius: 10px; background: #fff;
  color: #102b5c; font-size: 20px; line-height: 1; cursor: pointer;
}
.zad-menu-btn:focus-visible { outline: 3px solid rgba(65,107,158,.35); outline-offset: 2px; }
.zad-overlay {
  display: none; position: fixed; inset: 0; z-index: 998;
  background: rgba(10,20,40,.55); -webkit-backdrop-filter: blur(1px); backdrop-filter: blur(1px);
}

/* ---------- Large monitors ---------- */
@media (min-width: 1600px) {
  .content { padding-left: 40px; padding-right: 40px; }
  .content > * { max-width: 1680px; margin-left: auto; margin-right: auto; }
}

/* ---------- Tablet & small laptop: sidebar becomes a slide-in drawer ---------- */
@media (max-width: 1024px) {
  .erp-layout { height: 100vh; height: 100dvh; min-height: 0; overflow: hidden; }
  .erp-layout .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; z-index: 999;
    width: min(300px, 86vw); max-height: none; height: 100%;
    transform: translateX(-102%); transition: transform .25s ease;
    overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  body.zad-nav-open .erp-layout .sidebar { transform: translateX(0); }
  body.zad-nav-open { overflow: hidden; }
  body.zad-nav-open .zad-overlay { display: block; }
  .erp-layout .main-area { width: 100%; overflow: hidden; }
  .erp-layout .content { flex: 1; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; }
  .zad-menu-btn { display: inline-flex; }
  .topbar { padding-left: 16px; padding-right: 16px; gap: 10px; }

  /* Two/three column page grids → fewer columns */
  .grid-3, .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ---------- Tablet portrait / large phone ---------- */
@media (max-width: 768px) {
  .topbar {
    height: auto; min-height: 56px; flex-wrap: wrap; row-gap: 6px;
    padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top, 0));
  }
  .topbar-title { font-size: 16px; flex: 1 1 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .topbar-date { display: none; }
  .content { padding: 16px 12px 28px; padding-bottom: calc(28px + env(safe-area-inset-bottom, 0)); }

  /* Headers, toolbars, filter bars, action rows wrap instead of overflowing */
  .page-header, .module-head, .section-head, .card-header, .toolbar, .filters, .filter-bar,
  .search-bar, .form-actions, .actions, .btn-row, .button-row, .header-actions {
    flex-wrap: wrap; gap: 10px;
  }
  .page-header, .module-head { flex-direction: column; align-items: flex-start; }
  .page-header .btn, .module-head .btn { max-width: 100%; }

  /* Page grids: one column */
  .grid-2, .grid-3, .grid-4, .detail-grid, .form-card, .form-grid, .two-col, .three-col, .split {
    grid-template-columns: minmax(0,1fr) !important;
  }
  .field, .form-group { min-width: 0; }
  .form-card .field:has(textarea), .form-actions { grid-column: auto; }

  /* Inline-styled grids (used throughout templates) collapse on small screens */
  [style*="grid-template-columns"]:not([style*="auto-fit"]):not([style*="auto-fill"]) {
    grid-template-columns: minmax(0,1fr) !important;
  }
  /* …but KPI / stat tiles keep two per row so they stay compact */
  [style*="repeat(3"], [style*="repeat(4"], [style*="repeat(5"], [style*="repeat(6"], [style*="repeat(7"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
  [style*="grid-column"] { grid-column: auto !important; }

  /* Prevent iOS zooming into fields; comfortable touch inputs */
  input:not([type=checkbox]):not([type=radio]):not([type=range]),
  select, textarea { font-size: 16px; min-height: 42px; max-width: 100%; }
  input[type=checkbox], input[type=radio] { width: 20px; height: 20px; }

  /* Cards & tables */
  .table-card, .section-card { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .data-table, .table-card table { min-width: 640px; }

  /* Dropdowns / popovers never run off-screen */
  .notification-dropdown { max-width: calc(100vw - 24px); width: 340px; }
  .language-switcher { margin-left: 0; }

  /* Modals / dialogs */
  .modal, .modal-card, .modal-content, .modal-dialog, dialog {
    width: calc(100vw - 24px) !important; max-width: calc(100vw - 24px) !important;
    max-height: calc(100dvh - 24px); overflow-y: auto;
  }
}

/* ---------- Phone ---------- */
@media (max-width: 600px) {
  .content { padding: 14px 10px 26px; }
  .topbar-badge { width: 36px; height: 36px; }
  .language-switcher select { max-width: 96px; padding-left: 8px; }
  h1 { font-size: 22px; } h2 { font-size: 18px; } h3 { font-size: 16px; }

  /* Fixed-position notification panel spans the screen width */
  .topbar-notification-wrap { position: static; }
  .notification-dropdown { position: fixed; left: 10px; right: 10px; top: 60px; width: auto; max-width: none; }

  /* Buttons: comfortable, full-width inside forms and action rows */
  .btn, button, .btn-primary, .btn-secondary, [class*="-btn"] { min-height: 40px; }
  .form-actions .btn, .form-actions button, .form-actions a,
  .actions .btn, .button-row .btn { flex: 1 1 100%; text-align: center; justify-content: center; }

  .data-table, .table-card table, .zad-table-scroll > table { min-width: 560px; font-size: 12.5px; }
  .data-table th, .data-table td, .table-card th, .table-card td { padding: 9px 10px; }
}

/* ---------- Small phones ---------- */
@media (max-width: 420px) {
  .topbar-title { font-size: 15px; }
  [style*="repeat(3"], [style*="repeat(4"], [style*="repeat(5"], [style*="repeat(6"], [style*="repeat(7"] {
    grid-template-columns: minmax(0,1fr) !important;
  }
  .language-switcher select { max-width: 84px; font-size: 11px; }
  .sidebar-brand .firm-name { font-size: 15px; }
}

/* ---------- Phones in landscape: keep the top bar short ---------- */
@media (max-height: 480px) and (orientation: landscape) {
  .topbar { min-height: 46px; padding-top: 4px; padding-bottom: 4px; }
  .erp-layout .sidebar { width: min(280px, 60vw); }
}

/* ---------- Touch devices: bigger tap targets in the menu ---------- */
@media (pointer: coarse) {
  .nav-item { min-height: 44px; display: flex; align-items: center; }
  .topbar-badge, .zad-menu-btn { min-width: 40px; min-height: 40px; }
}

/* ---------- Accessibility ---------- */
@media (prefers-reduced-motion: reduce) {
  .erp-layout .sidebar { transition: none; }
}

/* ---------- Module-specific touch-ups ---------- */
@media (max-width: 600px) {
  .payment-method-grid, .invoice-preview { grid-template-columns: minmax(0,1fr) !important; }
  .history-head { flex-direction: column; }
}
