
/* =====================================================================
   D. RESPONSIVE
   ===================================================================== */

/* Tablet: collapse the sidebar to compact icon-rail by default */
@media (min-width: 769px) and (max-width: 1100px) {
  .crm-sidebar {
    width: var(--sidebar-w-compact);
    flex-basis: var(--sidebar-w-compact);
    position: sticky;
  }
  .crm-sidebar .brand-text,
  .crm-sidebar .nav-label,
  .crm-sidebar .nav-chevron,
  .crm-sidebar .sidebar-foot-text,
  .crm-sidebar .nav-sub { display: none; }
  .crm-sidebar .sidebar-brand { justify-content: center; padding: 20px 0; }
  .crm-sidebar .nav-item { justify-content: center; padding: 11px 0; }
  .crm-sidebar .nav-item.is-active::before { left: -6px; }

  .crm-sidebar.expanded {
    width: var(--sidebar-w);
    flex-basis: var(--sidebar-w);
  }
  .crm-sidebar.expanded .brand-text,
  .crm-sidebar.expanded .nav-label,
  .crm-sidebar.expanded .nav-chevron,
  .crm-sidebar.expanded .sidebar-foot-text { display: block; }
  .crm-sidebar.expanded .nav-group.open .nav-sub { display: flex; }
  .crm-sidebar.expanded .sidebar-brand {
    justify-content: flex-start;
    padding: 20px 22px;
  }
  .crm-sidebar.expanded .nav-item {
    justify-content: flex-start;
    padding: 10px 12px;
  }
}

/* Landing nav becomes a slide-in drawer; table foot stacks */
@media (max-width: 900px) {
  .landing .menu-toggle { display: flex; }
  .landing .nav {
    position: fixed;
    top: 0;
    right: 0;
    height: 100vh;
    height: 100dvh;
    width: min(320px, 84vw);
    flex-direction: column;
    align-items: stretch;
    justify-content: flex-start;
    gap: 4px;
    padding: 100px 26px 40px;
    background: #000000;
    border-left: 1px solid var(--secondary-12);
    transform: translateX(105%);
    transition: transform .45s var(--ease);
    z-index: 950;
    box-shadow: -20px 0 60px -30px rgba(0, 0, 0, 0.60);
  }
  .landing .nav.is-open { transform: translateX(0); }
  .landing .nav a {
    padding: 16px 18px;
    font-size: 1.02rem;
    border-radius: 10px;
    color: var(--secondary);
    border-bottom: 1px solid var(--secondary-08);
  }
  .landing .nav a::after { display: none; }
  .landing .nav a:hover { background: var(--secondary-08); color: var(--secondary); }

  .table-foot { justify-content: center; }
  .table-foot .foot-left,
  .table-foot .foot-right { flex: 0 0 auto; justify-content: center; }
  .table-foot .foot-center { order: -1; flex: 1 1 100%; }
}

/* Phone: off-canvas sidebar, 2-up stat grid, 3-up quick actions */
@media (max-width: 768px) {
  :root { --navbar-h: 58px; }

  .landing .footer-top { grid-template-columns: 1fr 1fr; gap: 40px; }

  .crm-sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 272px;
    flex-basis: 272px;
    height: 100vh;
    height: 100dvh;
    transform: translateX(-100%);
    transition: transform .28s cubic-bezier(.4,0,.2,1);
    z-index: 200;
    box-shadow: 6px 0 30px rgba(0, 0, 0, .45);
  }
  .crm-sidebar.open { transform: translateX(0); }

  .crm-main { padding: 18px 16px 26px; gap: 16px; }

  .crm-navbar { padding: 0 14px; gap: 10px; }
  .navbar-search { display: none; }
  .navbar-title h1 { font-size: 15px; }
  .navbar-title p { display: none; }
  .user-meta { display: none; }
  .user-chevron { display: none; }

  .crm-footer {
    padding: 14px 16px;
    font-size: 11.5px;
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }

  .quick-actions { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .stat-grid     { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2        { grid-template-columns: 1fr; }
  .alerts-grid   { grid-template-columns: 1fr; }
}

/* Small phone: landing hero & footer stack, toolbar goes full-width */
@media (max-width: 640px) {
  .landing .hero { padding: 40px 0 56px; }
  .landing .hero-copy h1 { font-size: clamp(2.1rem, 9vw, 3rem); }
  .landing .hero-actions .btn { flex: 1 1 auto; justify-content: center; }

  .landing .footer-top {
    grid-template-columns: 1fr;
    gap: 36px;
    padding-bottom: 28px;
  }
  .landing .footer-bottom {
    flex-direction: column;
    align-items: flex-start;
  }
  .landing .brand-logo { height: 38px; }

  .page-toolbar { padding: 14px 16px; }
  .toolbar-search { max-width: none; flex: 1 1 100%; }
  .toolbar-spacer { display: none; }
  .page-toolbar .btn { flex: 1 1 auto; justify-content: center; }
  .table-foot { padding: 14px 16px; gap: 12px; }
  .page-btn { min-width: 32px; height: 32px; }
}

/* Compact forms / modals on narrow screens */
@media (max-width: 560px) {
  .form-grid,
  .view-grid { grid-template-columns: 1fr; }

  .crm-modal { padding: 12px; }
  .crm-modal-dialog { max-height: calc(100vh - 24px); }
  .crm-modal-head,
  .crm-modal-body,
  .crm-modal-foot { padding-left: 16px; padding-right: 16px; }

  .crm-modal-foot { flex-direction: column-reverse; }
  .crm-modal-foot .btn { width: 100%; justify-content: center; }
}

/* Very small phone: 2-up quick actions, single-column stat grid */
@media (max-width: 480px) {
  .quick-actions { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat-grid     { grid-template-columns: 1fr; }
  .stat-value    { font-size: 20px; }
  .donut-wrap    { justify-content: center; }
  .donut-chart   { flex-basis: 170px; max-width: 170px; }
  .card-head     { padding: 16px 16px 10px; }
  .card-body     { padding: 6px 16px 16px; }
  .card-body.no-pad { padding: 0; }
  .data-table thead th,
  .data-table tbody td { padding-left: 16px; padding-right: 16px; }
}


/* =====================================================================
   E. REDUCED MOTION
   ===================================================================== */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
  html { scroll-behavior: auto; }
  .landing .reveal { opacity: 1; transform: none; }
  .login-card { animation: none; }
}


/* =====================================================================
   F. TDS DEDUCTION STATE STYLING
   ---------------------------------------------------------------------
   TDS OFF: grey button and grey disabled amount input.
   TDS ON: white button and white editable amount input.
   ===================================================================== */
#f_tds_enabled.btn-ghost,
#f_tds_enabled.btn-ghost:hover,
#f_tds_enabled.btn-ghost:focus,
#f_tds_enabled.btn-ghost:active {
  background: var(--surface-2) !important;
  color: var(--text-secondary) !important;
  border-color: var(--card-border) !important;
  box-shadow: none !important;
  transform: none !important;
}

#f_tds_enabled.btn-primary,
#f_tds_enabled.btn-primary:hover,
#f_tds_enabled.btn-primary:focus,
#f_tds_enabled.btn-primary:active {
  background: #ffffff !important;
  color: var(--text) !important;
  border-color: var(--card-border) !important;
  box-shadow: none !important;
  transform: none !important;
}

.form-field input#f_tds_amt:disabled,
.form-field input#f_tds_amt.readonly-balance:disabled {
  background: var(--surface-2) !important;
  color: var(--text-secondary) !important;
  -webkit-text-fill-color: var(--text-secondary) !important;
  opacity: 1 !important;
  cursor: not-allowed;
}

.form-field input#f_tds_amt:not(:disabled) {
  background: #ffffff !important;
  color: var(--text) !important;
  -webkit-text-fill-color: var(--text) !important;
  opacity: 1 !important;
}


/* =====================================================================
   G. REPORTS PAGE — compact Profit & Loss statement
   ---------------------------------------------------------------------
   Left untouched on mobile: keeps its fixed-layout table, which is
   already the ideal presentation for a numeric P&L statement.
   ===================================================================== */
.report-pl-card {
  width: 50%;
  max-width: 50%;
  min-width: 0;
  align-self: center;
  margin-inline: auto;
}
.report-pl-card .table-wrap {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}
.report-pl-card .data-table {
  width: 100%;
  table-layout: fixed;
}
.report-pl-card .data-table th,
.report-pl-card .data-table td {
  overflow-wrap: anywhere;
  word-break: normal;
}
.report-pl-card .data-table td.num,
.report-pl-card .data-table th.num {
  white-space: normal;
}
@media (max-width: 900px) {
  .report-pl-card {
    width: 100%;
    max-width: 100%;
  }
}


/* =====================================================================
   H. REPORTS PAGE — five summary cards on a single row
   ===================================================================== */
.stat-grid.report-summary {
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 12px;
}

.stat-grid.report-summary .stat-card {
  padding: 14px 12px;
  gap: 10px;
  align-items: center;
}

.stat-grid.report-summary .stat-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
}

.stat-grid.report-summary .stat-value {
  font-size: clamp(12.5px, 1.15vw, 20px);
  overflow-wrap: anywhere;
  word-break: break-word;
  letter-spacing: -.01em;
}

.stat-grid.report-summary .stat-foot { flex-wrap: wrap; }

.stat-grid.report-summary .stat-foot span {
  white-space: normal;
  overflow: visible;
  text-overflow: clip;
}

@media (max-width: 1100px) {
  .stat-grid.report-summary { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .stat-grid.report-summary .stat-value { font-size: clamp(13px, 1.8vw, 19px); }
}

@media (max-width: 768px) {
  .stat-grid.report-summary { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat-grid.report-summary .stat-value { font-size: 18px; }
}

@media (max-width: 480px) {
  .stat-grid.report-summary { grid-template-columns: 1fr; }
  .stat-grid.report-summary .stat-value { font-size: 20px; }
}


/* =====================================================================
   I. MOBILE-APP FOUNDATION  (screen only, ≤ 768px)
   ---------------------------------------------------------------------
   · Safe-area handling so UI never sits under a notch / home bar.
   · 16px minimum on every input → no iOS auto-zoom.
   · 44px minimum on every tappable icon → comfortable touch.
   · Touch-friendly tap highlight.
   ===================================================================== */
@media screen and (max-width: 768px) {

  html {
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0.06);
    overscroll-behavior-x: none;
  }

  /* --- Prevent iOS auto-zoom on focus ------------------------- */
  input,
  select,
  textarea,
  .form-field input,
  .form-field select,
  .form-field textarea,
  .toolbar-search input,
  .items-table input[type="text"],
  .items-table input[type="number"],
  .items-table input[type="date"] {
    font-size: 16px;
  }

  /* --- Minimum touch targets (WCAG 2.5.5 / iOS HIG) ----------- */
  .act-btn,
  .crm-modal-close,
  .icon-btn,
  .nav-toggle,
  .pw-toggle,
  .page-btn,
  .row-add,
  .row-del {
    min-width: 44px;
    min-height: 44px;
  }

  /* --- Prevent pull-to-refresh inside scrollable overlays ----- */
  .crm-sidebar .sidebar-nav,
  .crm-modal-body { overscroll-behavior: contain; }

  /* --- Body overflow lock while the mobile drawer is open ----- */
  body:has(.crm-sidebar.open) { overflow: hidden; }
  body:has(.crm-modal.open)   { overflow: hidden; }
}


/* =====================================================================
   J. MOBILE NAVBAR & SIDEBAR  (app-like chrome, ≤ 768px)
   ---------------------------------------------------------------------
   · Navbar honours the notch via safe-area-inset-top, stays
     compact so it never eats vertical space.
   · Sidebar becomes a full-height premium drawer with comfortable
     48px touch rows, safe-area padding at top & bottom, and a
     stronger active-state pill.
   ===================================================================== */
@media screen and (max-width: 768px) {

  /* --- Navbar ------------------------------------------------ */
  .crm-navbar {
    height: auto;
    min-height: 56px;
    padding-top: max(0px, env(safe-area-inset-top));
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right));
    padding-bottom: 0;
    gap: 10px;
    box-shadow: 0 1px 0 rgba(255, 255, 255, 0.06);
  }

  .crm-navbar .nav-toggle {
    width: 40px;
    height: 40px;
    border-radius: 10px;
  }
  .crm-navbar .navbar-title h1 {
    font-size: 15px;
    font-weight: 600;
    letter-spacing: -.005em;
  }
  .crm-navbar .navbar-actions { gap: 4px; }

  .crm-navbar .user-btn {
    padding: 3px;
    gap: 0;
    border-radius: 100px;
  }
  .crm-navbar .user-avatar {
    width: 34px;
    height: 34px;
    font-size: 12.5px;
  }

  /* --- Sidebar drawer ---------------------------------------- */
  .crm-sidebar {
    width: min(288px, 84vw);
    flex-basis: min(288px, 84vw);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    border-right: 1px solid rgba(255, 255, 255, .12);
    box-shadow: 10px 0 40px -18px rgba(0, 0, 0, .55);
    transition: transform .3s cubic-bezier(.22,.8,.25,1);
  }

  .crm-sidebar .sidebar-brand {
    padding: 16px 18px;
  }
  .crm-sidebar .sidebar-brand .brand-mark { width: 32px; height: 32px; }
  .crm-sidebar .sidebar-brand .brand-text strong { font-size: 16px; }
  .crm-sidebar .sidebar-brand .brand-text em {
    font-size: 10.5px;
    letter-spacing: .16em;
  }

  .crm-sidebar .sidebar-nav {
    padding: 12px 10px 16px;
    gap: 1px;
  }
  .crm-sidebar .nav-item {
    padding: 12px 12px;
    font-size: 14px;
    min-height: 48px;
    border-radius: 10px;
  }
  .crm-sidebar .nav-item.is-active {
    background: rgba(255, 255, 255, .14);
  }
  .crm-sidebar .nav-item.is-active::before {
    height: 20px;
    border-radius: 0 3px 3px 0;
  }
  .crm-sidebar .nav-icon { width: 22px; height: 22px; }

  .crm-sidebar .nav-sub {
    margin-left: 22px;
    padding: 4px 0 6px;
  }
  .crm-sidebar .nav-sub-item {
    padding: 10px 10px;
    min-height: 40px;
    font-size: 13px;
    border-radius: 8px;
  }

  .crm-sidebar .sidebar-foot {
    padding: 14px 18px max(14px, env(safe-area-inset-bottom));
  }

  /* --- Footer respects the home indicator -------------------- */
  .crm-footer {
    padding-bottom: max(14px, env(safe-area-inset-bottom));
  }
}


/* =====================================================================
   K. MOBILE TABLES → RECORD CARDS  (screen only, ≤ 768px)
   ---------------------------------------------------------------------
   Display tables (.data-table) become stacked record cards. Column
   headings are conveyed via `data-label` when present on <td>; when
   absent, values still read clearly because they carry their own
   semantic styling (ref, pill, num, cust-cell).

   The P&L report (.report-pl-card) is explicitly excluded — it stays
   a table on mobile because that is the correct presentation for a
   numeric statement.

   The invoice line-items editor (.items-table) is transformed into a
   compact two-column form-card per row (see further below), but the
   read-only balance preview (.readonly-balance) is excluded.
   ===================================================================== */
@media screen and (max-width: 768px) {

  /* --- Card transform for display tables --------------------- */
  .data-table:not(.report-pl-card .data-table) {
    display: block;
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 14px;
  }
  .data-table:not(.report-pl-card .data-table) thead { display: none; }
  .data-table:not(.report-pl-card .data-table) tbody {
    display: block;
    width: 100%;
  }

  .data-table:not(.report-pl-card .data-table) tbody tr {
    display: block;
    width: 100%;
    padding: 14px 16px;
    margin-bottom: 10px;
    border: 1px solid var(--card-border);
    border-radius: 14px;
    background: #ffffff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
    transition: transform .15s ease, box-shadow .15s ease;
    overflow: hidden;
  }
  .data-table:not(.report-pl-card .data-table) tbody tr:hover {
    background: #ffffff;
    transform: none;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
  }

  .data-table:not(.report-pl-card .data-table) tbody td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    width: 100%;
    padding: 7px 0;
    border: 0;
    font-size: 13.5px;
    color: var(--text);
    white-space: normal;
    word-break: break-word;
    overflow-wrap: anywhere;
    min-width: 0;
  }
  .data-table:not(.report-pl-card .data-table) tbody td + td {
    border-top: 1px dashed var(--grid-line);
    padding-top: 8px;
    margin-top: 2px;
  }

  /* First cell = card title row */
  .data-table:not(.report-pl-card .data-table) tbody td:first-child {
    display: block;
    font-size: 15px;
    font-weight: 600;
    padding-top: 0;
    padding-bottom: 10px;
    border-top: 0;
    border-bottom: 1px solid var(--grid-line);
    margin-bottom: 6px;
    color: var(--text);
    line-height: 1.35;
  }

  /* Numeric cells align left inside cards */
  .data-table:not(.report-pl-card .data-table) tbody td.num {
    text-align: left;
    font-variant-numeric: tabular-nums;
  }

  /* `data-label` (optional but recommended) restores column
     headings above each cell without breaking layout when absent. */
  .data-table:not(.report-pl-card .data-table) tbody td[data-label]::before {
    content: attr(data-label);
    display: inline-block;
    font-size: 10.5px;
    font-weight: 600;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--accent);
    flex-shrink: 0;
    white-space: nowrap;
  }

  /* Actions cell drops to its own row, right-aligned */
  .data-table:not(.report-pl-card .data-table) tbody td:has(.row-actions) {
    justify-content: flex-end;
    padding-top: 10px;
    padding-bottom: 0;
  }
  .data-table:not(.report-pl-card .data-table) tbody .row-actions {
    justify-content: flex-end;
  }

  /* Legacy empty state still works */
  .data-table:not(.report-pl-card .data-table) .table-empty {
    display: block;
  }

  /* Keep pill / ref crisp inside cards */
  .data-table:not(.report-pl-card .data-table) tbody .pill { font-size: 11.5px; }
  .data-table:not(.report-pl-card .data-table) tbody .ref  { font-size: 11.5px; }

  /* --- Report P&L stays a table (unchanged visuals) ---------- */
  .report-pl-card .data-table { padding-left: 6px; padding-right: 6px; }

  /* --- Invoice line-items editor → compact form cards ------- */
  /* Target only the interactive editor, not the read-only preview */
  .items-wrap:not(.readonly-balance) .items-table {
    display: block;
    width: 100%;
  }
  .items-wrap:not(.readonly-balance) .items-table thead { display: none; }
  .items-wrap:not(.readonly-balance) .items-table tbody {
    display: block;
    width: 100%;
    padding: 8px;
  }
  .items-wrap:not(.readonly-balance) .items-table tbody tr {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: auto;
    gap: 8px 10px;
    padding: 12px 14px;
    padding-right: 52px;
    position: relative;
    margin-bottom: 8px;
    border: 1px solid var(--card-border);
    border-radius: 12px;
    background: var(--surface-2);
  }
  .items-wrap:not(.readonly-balance) .items-table tbody tr:last-child {
    margin-bottom: 0;
  }
  .items-wrap:not(.readonly-balance) .items-table tbody td {
    display: block;
    padding: 0;
    border: 0;
  }
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-sr { display: none; }
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-date,
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-particular {
    grid-column: 1 / -1;
  }
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-qty,
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-rate,
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-amount {
    grid-column: span 1;
  }

  /* Pseudo-labels for the editor's stacked fields */
  .items-wrap:not(.readonly-balance) .items-table tbody td::before {
    display: block;
    font-size: 10px;
    font-weight: 600;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--accent);
    margin-bottom: 4px;
  }
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-date::before        { content: "Date"; }
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-particular::before  { content: "Particular"; }
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-qty::before         { content: "Qty"; }
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-rate::before        { content: "Rate"; }
  .items-wrap:not(.readonly-balance) .items-table tbody td.col-amount::before      { content: "Amount"; }

  /* Row action buttons pinned top-right inside the card */
  .items-wrap:not(.readonly-balance) .items-table tbody td.row-actions-cell {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 0;
    text-align: right;
  }
  .items-wrap:not(.readonly-balance) .items-table .row-add,
  .items-wrap:not(.readonly-balance) .items-table .row-del {
    width: 32px;
    height: 32px;
    border-radius: 8px;
  }

  /* Full-width, comfortable input inside each form card */
  .items-wrap:not(.readonly-balance) .items-table input[type="text"],
  .items-wrap:not(.readonly-balance) .items-table input[type="number"],
  .items-wrap:not(.readonly-balance) .items-table input[type="date"] {
    background: #ffffff;
    border: 1px solid var(--card-border);
    border-radius: 8px;
    padding: 8px 10px;
    min-height: 40px;
  }
  .items-wrap:not(.readonly-balance) .items-table .it-amount {
    text-align: right;
    padding-right: 10px;
    color: var(--text);
    font-weight: 500;
  }
}


/* =====================================================================
   L. MOBILE MODALS → BOTTOM SHEETS  (screen only, ≤ 640px)
   ---------------------------------------------------------------------
   · Modal slides up from the bottom as a native-style sheet
     with a grab handle and rounded top corners.
   · Sheet is capped at 92 dvh with a scrollable body.
   · Footer sits at the bottom, honours the home indicator, and
     stacks its buttons full-width.
   ===================================================================== */
@media screen and (max-width: 640px) {

  .crm-modal {
    align-items: flex-end;
    padding: 0;
  }

  .crm-modal-dialog,
  .crm-modal-dialog.modal-sm,
  .crm-modal-dialog.modal-lg,
  .crm-modal-dialog.modal-invoice {
    width: 100%;
    max-width: 100%;
    max-height: 92vh;
    max-height: 92dvh;
    border-radius: 20px 20px 0 0;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
    transform: translateY(100%);
    transition: transform .32s cubic-bezier(.22,.85,.3,1);
  }
  .crm-modal.open .crm-modal-dialog {
    transform: translateY(0);
  }

  /* Grab handle */
  .crm-modal-dialog::before {
    content: "";
    display: block;
    width: 40px;
    height: 4px;
    border-radius: 2px;
    background: var(--primary-25);
    margin: 10px auto 4px;
    flex-shrink: 0;
  }

  .crm-modal-head {
    padding: 6px 16px 14px;
    border-bottom: 1px solid var(--card-border);
  }
  .crm-modal-title h3 { font-size: 15px; }
  .crm-modal-title p  { font-size: 11.5px; }

  .crm-modal-body {
    padding: 16px;
    padding-bottom: 16px;
  }

  .crm-modal-foot {
    flex-direction: column-reverse;
    gap: 8px;
    padding: 12px 16px max(12px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--card-border);
  }
  .crm-modal-foot .btn {
    width: 100%;
    justify-content: center;
    height: 48px;
    font-size: 14.5px;
  }
}


/* =====================================================================
   M. MOBILE FORMS & INPUTS  (screen only, ≤ 640px)
   ---------------------------------------------------------------------
   · Comfortable 48–52px field height.
   · 12px corner radius, matching premium feel.
   · Full-width layout; labels stay 11–12px, values 16px.
   · Select chevron scaled up for touch.
   ===================================================================== */
@media screen and (max-width: 640px) {

  .form-grid { gap: 12px; }

  .form-field { gap: 6px; }
  .form-field > label {
    font-size: 11px;
    letter-spacing: .05em;
  }

  .form-field input,
  .form-field select,
  .form-field textarea {
    min-height: 48px;
    padding: 12px 14px;
    border-radius: 12px;
    font-size: 16px;
    line-height: 1.35;
  }
  .form-field textarea { min-height: 96px; }

  .form-field .select-wrap,
  .form-field .select-wrap select { width: 100%; }
  .form-field .select-wrap select {
    height: 48px;
    border-radius: 12px;
  }

  .select-wrap select {
    height: 44px;
    border-radius: 10px;
    padding-right: 38px;
  }
  .select-wrap::after { right: 14px; }

  /* Compact invoice totals box */
  .totals-wrap { justify-content: stretch; }
  .totals-box { max-width: 100%; width: 100%; }
  .totals-row { font-size: 13.5px; }
  .totals-row.grand { font-size: 14.5px; }
  .totals-row.grand .val { font-size: 15.5px; }

  /* Add-row split button grows for touch */
  .items-foot { margin-top: 12px; }
  .items-foot .btn-sm { height: 42px; font-size: 13.5px; padding: 0 14px; }

  /* View modal reflows cleanly */
  .view-invoice-head { flex-direction: column; gap: 8px; }
  .view-invoice-head .no { text-align: left; }
  .view-items th,
  .view-items td { padding: 8px 8px; font-size: 12.5px; }
}


/* =====================================================================
   N. MOBILE CARDS, STATS, QUICK ACTIONS  (screen only, ≤ 640px)
   ---------------------------------------------------------------------
   · Slightly larger corner radius on cards → premium feel.
   · Quick actions get bigger icons and better touch rows.
   · Stat cards remain compact; the existing 2-up / 1-up grid is
     preserved and only spacing tightens.
   ===================================================================== */
@media screen and (max-width: 640px) {

  .card {
    border-radius: 16px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .04);
  }
  .card-head { padding: 16px 16px 10px; gap: 10px; }
  .card-body { padding: 8px 16px 16px; }

  .quick-actions { gap: 10px; }
  .qa-tile {
    padding: 12px;
    border-radius: 14px;
    gap: 10px;
  }
  .qa-icon {
    width: 40px;
    height: 40px;
    border-radius: 12px;
  }
  .qa-text strong { font-size: 12.5px; }
  .qa-text em     { font-size: 11px; }

  .stat-card {
    padding: 14px;
    border-radius: 14px;
  }
  .stat-icon { width: 40px; height: 40px; border-radius: 12px; }
  .stat-value { font-size: 20px; }
  .stat-label { font-size: 11.5px; }
  .stat-foot  { font-size: 11px; }

  /* Donut block stacks cleanly */
  .donut-wrap { padding: 4px 16px 16px; gap: 16px; }
  .donut-chart { flex-basis: 170px; max-width: 170px; margin-inline: auto; }
  .donut-legend { flex-basis: 100%; }

  /* Alerts and stock stay comfortable */
  .alerts-card .card-body { padding: 4px 16px 16px; }
  .alert-item { padding: 12px 14px; }
  .stock-wrap { padding: 4px 16px 16px; }
}


/* =====================================================================
   O. MOBILE TOOLBAR, TOAST, PAGINATION  (screen only, ≤ 640px)
   ---------------------------------------------------------------------
   · Toolbar wraps neatly and stays out of the way.
   · Toast is anchored below the notch, full-width, safe-area aware.
   · Pagination buttons and per-page selector are comfortable to hit.
   ===================================================================== */
@media screen and (max-width: 640px) {

  /* --- Page toolbar ------------------------------------------ */
  .page-toolbar {
    flex-wrap: wrap;
    gap: 10px;
    padding: 12px;
    border-radius: 14px;
  }
  .toolbar-search {
    flex: 1 1 100%;
    max-width: none;
    min-width: 0;
  }
  .toolbar-search input {
    height: 44px;
    font-size: 16px;
    border-radius: 12px;
  }
  .page-toolbar .btn,
  .page-toolbar .select-wrap,
  .page-toolbar .select-wrap select {
    height: 44px;
    font-size: 13.5px;
    border-radius: 12px;
  }

  /* --- Toast ------------------------------------------------- */
  #toastHost {
    top: max(10px, env(safe-area-inset-top));
    left: 10px;
    right: 10px;
    gap: 8px;
  }
  .crm-toast {
    max-width: 100%;
    border-radius: 12px;
    padding: 12px 14px;
    font-size: 13px;
  }

  /* --- Pagination -------------------------------------------- */
  .table-foot {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    padding: 14px 16px;
  }
  .table-foot .foot-left,
  .table-foot .foot-right,
  .table-foot .foot-center {
    flex: 1 1 100%;
    justify-content: center;
    text-align: center;
  }
  .pagination { justify-content: center; }
  .page-btn {
    min-width: 36px;
    height: 36px;
    font-size: 13px;
  }
  .per-page-label { text-align: center; }
}


/* =====================================================================
   Extra-narrow phones (≤ 380px) — final polish
   ---------------------------------------------------------------------
   Only refines spacing so no control overflows even on 320px screens.
   ===================================================================== */
@media screen and (max-width: 380px) {

  .crm-main { padding-left: 12px; padding-right: 12px; }
  .crm-navbar { padding-left: 10px; padding-right: 10px; }

  .card-head { padding: 14px 14px 8px; }
  .card-body { padding: 6px 14px 14px; }
  .qa-tile { padding: 10px; }
  .qa-icon { width: 36px; height: 36px; border-radius: 10px; }

  .page-toolbar { padding: 10px; }
  .crm-modal-body { padding: 14px; }
  .crm-modal-head { padding-left: 14px; padding-right: 14px; }
  .crm-modal-foot { padding-left: 14px; padding-right: 14px; }

  .data-table:not(.report-pl-card .data-table) tbody tr {
    padding: 12px 14px;
  }
  .data-table:not(.report-pl-card .data-table) tbody td { font-size: 13px; }

  .items-wrap:not(.readonly-balance) .items-table tbody tr {
    padding: 10px 12px;
    padding-right: 48px;
  }
}

/* =====================================================================
   CUSTOMERS PAGE — MOBILE-ONLY FINISHING OVERRIDES
   Desktop / large-screen customer layout is intentionally unchanged.
   ===================================================================== */
@media screen and (min-width: 769px) {
  #custTable .customer-contact-icon {
    display: none;
  }
}

@media screen and (max-width: 768px) {
  /* Match each customer card's width and side gutters to .toolbar-search.
     At 641–768px the toolbar uses 16px horizontal padding; at 640px and
     below its mobile rule uses 12px horizontal padding. */
  #custTable {
    width: 100%;
    max-width: 100%;
  }

  #custTable > tbody > tr[data-row] {
    box-sizing: border-box;
    width: calc(100% - 32px);
    max-width: calc(100% - 32px);
    margin: 0 16px 12px;
  }

  /* GST remains available in the edit/view data, but is not shown on
     the mobile customer summary card. */
  #custTable > tbody > tr[data-row] > td:nth-child(2) {
    display: none !important;
  }

  /* Contact details each occupy their own clean line with a matching icon. */
  #custTable > tbody > tr[data-row] > td.customer-mobile-cell,
  #custTable > tbody > tr[data-row] > td.customer-email-cell {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 9px;
    min-width: 0;
    width: 100%;
    box-sizing: border-box;
  }

  #custTable > tbody > tr[data-row] > td.customer-mobile-cell::before,
  #custTable > tbody > tr[data-row] > td.customer-email-cell::before {
    content: none !important;
    display: none !important;
  }

  #custTable .customer-contact-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 20px;
    width: 20px;
    height: 20px;
    color: var(--accent);
  }

  #custTable .customer-contact-icon .ico {
    display: block;
    width: 15px;
    height: 15px;
  }

  #custTable .customer-contact-value {
    display: block;
    flex: 1 1 auto;
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  /* Keep balance and customer actions on one row on mobile only. */
  #custTable > tbody > tr[data-row] {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    column-gap: 8px;
  }

  #custTable > tbody > tr[data-row] > td:first-child,
  #custTable > tbody > tr[data-row] > td.customer-mobile-cell,
  #custTable > tbody > tr[data-row] > td.customer-email-cell {
    grid-column: 1 / -1;
    min-width: 0;
  }

  #custTable > tbody > tr[data-row] > td:nth-child(5) {
    grid-column: 1;
    grid-row: 4;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    width: auto;
    min-width: 0;
    padding: 10px 0 0;
    margin: 0;
    border-top: 1px dashed var(--grid-line);
    white-space: nowrap;
  }

  #custTable > tbody > tr[data-row] > td:nth-child(5) > a {
    display: inline-block;
    min-width: 0;
    max-width: 100%;
    white-space: nowrap;
  }

  /* Double the current balance amount font size on mobile only. */
  #custTable > tbody > tr[data-row] > td:nth-child(5) > a > strong {
    font-size: 1.5em;
    line-height: 1.3;
  }

  #custTable > tbody > tr[data-row] > td:nth-child(6) {
    grid-column: 2;
    grid-row: 4;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    width: auto;
    min-width: 0;
    padding: 8px 0 0;
    margin: 0;
    border-top: 1px dashed var(--grid-line);
  }

  #custTable > tbody > tr[data-row] > td:nth-child(6) .row-actions {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    min-width: 0;
    width: auto;
  }

  #custTable > tbody > tr[data-row] > td:nth-child(6) .act-btn {
    flex: 0 0 30px;
    width: 30px;
    min-width: 30px;
    max-width: 30px;
    height: 32px;
    min-height: 32px;
    padding: 0;
  }

  /* Keep modal actions side-by-side for both Add and Edit customer forms. */
  #crmModal .crm-modal-foot {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: stretch;
    gap: 10px;
  }

  #crmModal .crm-modal-foot .btn {
    flex: 1 1 0;
    width: auto;
    min-width: 0;
    max-width: 100%;
    justify-content: center;
    white-space: normal;
    overflow-wrap: anywhere;
    padding-left: 10px;
    padding-right: 10px;
  }
}

@media screen and (max-width: 640px) {
  /* .page-toolbar/.toolbar-search use 12px side padding at this breakpoint. */
  #custTable > tbody > tr[data-row] {
    width: calc(100% - 24px);
    max-width: calc(100% - 24px);
    margin-left: 12px;
    margin-right: 12px;
  }
}

@media screen and (max-width: 360px) {

  #crmModal .crm-modal-foot {
    gap: 8px;
    padding-left: 12px;
    padding-right: 12px;
  }

  #crmModal .crm-modal-foot .btn {
    font-size: 13px;
    padding-left: 7px;
    padding-right: 7px;
  }
}

/* Prevent accidental page-level horizontal scrolling on small screens. */
@media screen and (max-width: 768px) {
  html,
  body,
  .crm-app,
  .crm-main-wrap,
  .crm-main {
    max-width: 100%;
    overflow-x: clip;
  }

  #custTable,
  #custTable > tbody,
  #custTable > tbody > tr[data-row],
  #custTable > tbody > tr[data-row] > td {
    min-width: 0;
  }
}
