@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&family=Roboto+Mono:wght@400;500&display=swap");

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  /* Legacy static brand constants - kept unchanged (not theme-reactive) so
     pages not yet converted to the new token system (second wave) keep
     looking exactly as they do today, regardless of theme toggle state. */
  --navy: #0A192F;
  --slate: #4A5568;
  --white: #FFFFFF;
  --charcoal: #718096;
  --bg: #F7F8FA;
  --navy-light: #112240;

  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px; --space-5: 24px; --space-6: 32px; --space-7: 48px;
  --radius-sm: 6px; --radius-md: 8px; --radius-lg: 12px;
  --shadow-sm: 0 1px 2px rgba(16,24,40,0.06), 0 1px 3px rgba(16,24,40,0.06);
  --shadow-raised: 0 2px 6px rgba(16,24,40,0.10), 0 4px 10px rgba(16,24,40,0.06);
  --shadow-md: 0 4px 12px rgba(16,24,40,0.08);
  --transition: 150ms ease-out;

  --gray-100: #F7F8FA; --gray-200: #E2E8F0; --gray-400: #A0AEC0;
  --gray-500: #718096; --gray-700: #4A5568; --gray-900: #1A202C;

  --text-page-title: 700 20px/1.3 "Inter", sans-serif;
  --text-section-header: 700 13px/1.3 "Inter", sans-serif;
  --text-body: 400 14px/1.5 "Inter", sans-serif;
  --text-label: 600 11px/1.3 "Inter", sans-serif;
  --text-caption: 400 12px/1.4 "Inter", sans-serif;

  /* Theme-aware semantic tokens (Section 9.13) - values copied directly
     from the live ethixlaw.com marketing site (same oklch values) rather
     than approximated, so the app and website read as one product. These
     drive shared classes + the representative pages (Dashboard, Referrals,
     Reports, Firms); html.dark below supplies the dark-mode values. */
  --background: oklch(99% .005 250);
  --foreground: oklch(18% .03 258);
  --primary: oklch(42% .13 258);
  --primary-foreground: oklch(99% .005 250);
  --secondary: oklch(94% .015 250);
  --muted: oklch(95% .01 250);
  --muted-foreground: oklch(45% .02 255);
  --accent: oklch(93% .02 258);
  --card: oklch(100% 0 0);
  --card-foreground: oklch(18% .03 258);
  --input: oklch(90% .01 250);
  --ring: oklch(42% .13 258);
  --destructive: oklch(58% .22 27);
  --border: oklch(90% .01 250);
  --green: #2F855A; --red: #C53030; --amber: #D69E2E;
}

html.dark {
  --background: oklch(16% .02 258);
  --foreground: oklch(96% .01 250);
  --primary: oklch(72% .13 258);
  --primary-foreground: oklch(15% .03 258);
  --secondary: oklch(26% .03 258);
  --muted: oklch(24% .025 258);
  --muted-foreground: oklch(72% .02 255);
  --accent: oklch(30% .05 258);
  --card: oklch(20% .025 258);
  --card-foreground: oklch(96% .01 250);
  --input: oklch(30% .02 258);
  --ring: oklch(72% .13 258);
  --destructive: oklch(62% .22 27);
  --border: oklch(30% .02 258);
  --green: #68D391; --red: #FC8181; --amber: #F6E05E;
}

body { font-family: "Inter", sans-serif; background: var(--background); color: var(--foreground); font-size: 14px; transition: background var(--transition), color var(--transition); }
a { color: var(--primary); text-decoration: none; }
input, select, textarea { font-family: "Inter", sans-serif; font-size: 14px; }

/* Sidebar-flex layout only applies on pages that actually render the
   sidebar (class added by nav() in auth.js) - login/register/setup-wizard/
   guest.html share this stylesheet but never call nav(), so they must keep
   plain document flow, not the sidebar's flex row. */
/* align-items must stretch, not flex-start - flex-start sizes
   #nav-placeholder to exactly the sidebar's own height, leaving the sticky
   sidebar zero room to travel/stick once .page's content is taller than
   one viewport, so it just scrolls away with the page instead of pinning. */
body.app-shell { display: flex; align-items: stretch; min-height: 100vh; }

/* SIDEBAR NAV */
#nav-placeholder { flex-shrink: 0; }
.sidebar {
  width: 240px;
  flex-shrink: 0;
  height: 100vh;
  position: sticky;
  top: 0;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--card);
  color: var(--foreground);
  border-right: 1px solid var(--border);
  transition: width 0.15s, background var(--transition), color var(--transition);
}
.sidebar.nav-collapsed { width: 64px; }
.nav-logo { color: var(--foreground); font-weight: 700; font-size: 16px; letter-spacing: 0.5px; }
.nav-link {
  color: var(--muted-foreground);
  font-size: 13px;
  font-weight: 500;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  transition: all 0.15s;
  display: flex;
  align-items: center;
  gap: 10px;
}
.nav-link:hover, .nav-link.active { color: var(--foreground); background: var(--accent); }

/* TOP BAR (inserted into .page via JS - see nav() in auth.js) */
.app-topbar {
  margin: -24px -24px 20px -24px;
  padding: 0 24px;
  height: 56px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  transition: background var(--transition), border-color var(--transition);
}

/* PAGE */
.page { flex: 1; min-width: 0; padding: 24px; max-width: 1200px; margin: 0 auto; }
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.page-title { font-size: 20px; font-weight: 700; color: var(--foreground); }

/* BUTTONS */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 22px; border-radius: var(--radius-sm); font-size: 14px; min-height: 44px;
  font-weight: 600; cursor: pointer; border: none; transition: var(--transition);
  font-family: "Inter", sans-serif;
}
.btn-primary { background: var(--primary); color: var(--primary-foreground); }
.btn-primary:hover { filter: brightness(0.92); }
.btn-secondary { background: var(--card); color: var(--primary); border: 1px solid var(--primary); }
.btn-secondary:hover { background: var(--accent); }
/* Deliberately short of the 44px baseline: these back dense table-row
   actions (View/Edit/Details) on desktop, where a full 44px button per
   row would wreck table density for no real accessibility gain in a
   mouse-first context. 36px is a reasonable middle ground. */
.btn-sm { padding: 8px 14px; font-size: 12.5px; min-height: 36px; }
.btn-danger { background: var(--destructive); color: #fff; }
.btn-success { background: var(--green); color: #fff; }

/* Tap targets */
.modal-close {
  width: 40px; height: 40px; display: flex; align-items: center;
  justify-content: center; border-radius: 4px; transition: background 0.15s;
}
.modal-close:hover { background: var(--accent); }
.nav-logout {
  color: var(--muted-foreground); font-size: 13px; cursor: pointer;
  background: none; border: none; padding: 10px 12px; min-height: 44px;
  display: inline-flex; align-items: center;
}
.nav-logout:hover { color: var(--destructive); }
input[type="checkbox"] { width: 18px; height: 18px; }

/* CARDS */
.card { background: var(--card); color: var(--card-foreground); border: 1px solid var(--border); border-radius: var(--radius-md); box-shadow: var(--shadow-sm); padding: var(--space-5); transition: background var(--transition), border-color var(--transition); }
.card-title { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted-foreground); margin-bottom: 8px; }
.card-value { font-size: 28px; font-weight: 700; color: var(--foreground); font-family: "Roboto Mono", monospace; }
.card-sub { font-size: 12px; color: var(--muted-foreground); margin-top: 4px; }

/* GRID */
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
/* Grid items default to min-width:auto, which sizes them to their
   content's intrinsic (max-content) width rather than the 1fr track -
   for a wide child like a Chart.js <canvas> (300px intrinsic by default)
   this "grid blowout" silently forces the whole grid, and everything
   around it up to <body>, wider than the viewport. Found live on the
   Reports page at mobile widths (Section 9.16 QA) - charts themselves
   looked fine in the earlier screenshot-based baseline, but the page
   itself was 736px wide inside a 375px viewport, which just isn't
   visible in a screenshot the way an overlapping topbar is. */
.grid-4 > *, .grid-3 > *, .grid-2 > *, .charts-grid > * { min-width: 0; }
canvas { max-width: 100%; }

/* TABLE */
.table-wrap { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-md); box-shadow: var(--shadow-sm); overflow: hidden; transition: background var(--transition), border-color var(--transition); }
table { width: 100%; border-collapse: collapse; }
thead th { background: var(--primary); color: var(--primary-foreground); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.6px; padding: 10px 16px; text-align: left; }
thead th.sortable { cursor: pointer; user-select: none; }
thead th.sortable:hover { background: color-mix(in srgb, var(--primary) 85%, black); }
tbody tr { border-bottom: 1px solid var(--border); }
tbody tr:nth-child(even) { background: var(--muted); }
tbody tr:hover { background: var(--accent); }
tbody td { padding: 10px 16px; font-size: 13px; color: var(--muted-foreground); }
tbody td.bold { font-weight: 600; color: var(--foreground); }

/* FILTERS */
.filter-bar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
.filter-bar input, .filter-bar select {
  padding: 7px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 13px; background: var(--card); color: var(--foreground);
}
.filter-bar input:focus, .filter-bar select:focus { outline: none; border-color: var(--ring); }

/* FORM */
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-group { display: flex; flex-direction: column; gap: 5px; }
.form-group.full { grid-column: 1 / -1; }
.form-label { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted-foreground); }
.required-mark { color: var(--destructive); margin-left: 3px; }
.form-input {
  padding: 8px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 13px; color: var(--foreground); background: var(--card);
}
.form-input:focus { outline: none; border-color: var(--ring); }
textarea.form-input { resize: vertical; min-height: 80px; }

/* BADGES / STATUS - background is a light/dark-adaptive tint of the badge's
   own color blended against the current page background, so one rule
   works in both themes without a duplicate dark-mode value per badge. */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: var(--radius-sm);
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.4px;
}
.badge-new { background: color-mix(in srgb, var(--primary) 15%, var(--background)); color: var(--primary); }
.badge-retained { background: color-mix(in srgb, var(--green) 15%, var(--background)); color: var(--green); }
.badge-declined { background: color-mix(in srgb, var(--red) 15%, var(--background)); color: var(--red); }
.badge-pending { background: color-mix(in srgb, var(--amber) 15%, var(--background)); color: var(--amber); }
.badge-paid { background: color-mix(in srgb, var(--green) 15%, var(--background)); color: var(--green); }
.badge-overdue { background: color-mix(in srgb, var(--red) 15%, var(--background)); color: var(--red); }
.badge-closed { background: var(--muted); color: var(--muted-foreground); }
.badge-inbound { background: color-mix(in srgb, var(--primary) 15%, var(--background)); color: var(--primary); }
.badge-outbound { background: color-mix(in srgb, #6B46C1 15%, var(--background)); color: #6B46C1; }

/* ATTENTION LIST */
.attention-list { display: flex; flex-direction: column; gap: 8px; }
.attention-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; background: color-mix(in srgb, var(--amber) 12%, var(--background)); border: 1px solid color-mix(in srgb, var(--amber) 40%, var(--background));
  border-radius: var(--radius-sm); font-size: 13px;
}
.attention-item .reason { font-size: 11px; color: var(--amber); font-weight: 600; text-transform: uppercase; }

/* MODAL */
.modal-overlay {
  display: flex; position: fixed; inset: 0; background: rgba(10,25,47,0.6);
  z-index: 200; align-items: center; justify-content: center;
  opacity: 0; visibility: hidden;
  transition: opacity var(--transition), visibility 0s linear 150ms;
}
.modal-overlay.open { opacity: 1; visibility: visible; transition: opacity var(--transition); }
.modal-overlay .modal { transform: translateY(8px) scale(0.98); transition: transform var(--transition); }
.modal-overlay.open .modal { transform: translateY(0) scale(1); }
.modal {
  background: var(--card); color: var(--card-foreground); border-radius: var(--radius-lg); box-shadow: var(--shadow-md); padding: var(--space-6); width: 680px;
  max-width: 95vw; max-height: 90vh; overflow-y: auto;
}
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; }
.modal-title { font-size: 16px; font-weight: 700; }
.modal-close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--muted-foreground); }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border); }

/* SECTION */
.section { margin-bottom: 24px; }
.section-title { font-size: 13px; font-weight: 700; color: var(--foreground); margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }

/* STAT ROW */
.stat-row { display: flex; gap: 8px; }
.stat-box { flex: 1; text-align: center; padding: 12px; background: var(--muted); border-radius: var(--radius-sm); }
.stat-box .val { font-size: 22px; font-weight: 700; font-family: "Roboto Mono", monospace; color: var(--foreground); }
.stat-box .lbl { font-size: 10px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted-foreground); margin-top: 2px; }

/* ACTIVITY */
.activity-list { display: flex; flex-direction: column; }
.activity-item { padding: 10px 0; border-bottom: 1px solid var(--border); font-size: 12px; color: var(--muted-foreground); }
.activity-item:last-child { border-bottom: none; }
.activity-item .who { font-weight: 600; color: var(--foreground); }
.activity-item .when { color: var(--muted-foreground); float: right; }

/* LOGIN */
.login-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: var(--primary); }
.login-box { background: var(--card); color: var(--card-foreground); padding: 40px; border-radius: var(--radius-lg); box-shadow: var(--shadow-md); width: 380px; }
.login-logo { font-size: 20px; font-weight: 700; color: var(--card-foreground); margin-bottom: 4px; }
.login-sub { font-size: 13px; color: var(--muted-foreground); margin-bottom: 28px; }

/* EMPTY STATE */
.empty { text-align: center; padding: 48px; color: var(--muted-foreground); }
.empty-title { font-size: 15px; font-weight: 600; margin-bottom: 6px; }
.empty-sub { font-size: 13px; }

/* TOAST */
.toast {
  position: fixed; bottom: 24px; right: 24px; background: var(--primary);
  color: var(--primary-foreground); padding: 12px 20px; border-radius: var(--radius-sm); font-size: 13px;
  z-index: 999; box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  opacity: 0; visibility: hidden; transform: translateY(8px);
  transition: opacity var(--transition), transform var(--transition), visibility 0s linear 150ms;
}
.toast.show { opacity: 1; visibility: visible; transform: translateY(0); transition: opacity var(--transition), transform var(--transition); }
.toast.error { background: var(--destructive); color: #fff; }

/* PAGINATION (was duplicated per-page; consolidated here) */
.pagination { display: flex; gap: 6px; align-items: center; margin-top: 12px; }
.pagination button { padding: 4px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--card); color: var(--foreground); cursor: pointer; font-size: 12px; transition: var(--transition); }
.pagination button.active { background: var(--primary); color: var(--primary-foreground); border-color: var(--primary); }
.pagination button:hover:not(.active) { background: var(--accent); }

/* SKELETON LOADING - one shared shimmer pattern, used instead of bare
   "Loading..." text or spinners wherever a page fetches data before
   rendering. Configurable width/height blocks; see skeletonBlock()/
   skeletonRows() in auth.js for the JS helpers that build these. */
@keyframes skeleton-shimmer {
  0% { background-position: -200px 0; }
  100% { background-position: calc(200px + 100%) 0; }
}
.skeleton {
  display: inline-block; border-radius: var(--radius-sm);
  background: var(--muted);
  background-image: linear-gradient(90deg, var(--muted) 0px, var(--accent) 40px, var(--muted) 80px);
  background-size: 200px 100%;
  background-repeat: no-repeat;
  animation: skeleton-shimmer 1.3s ease-in-out infinite;
}

/* MISC */
.mono { font-family: "Roboto Mono", monospace; }
/* Numeric/tabular columns (fee amounts, dates, counts) - right-aligned
   with tabular-nums so digit columns line up cleanly, per the design-
   language spec's numeric-formatting requirement. */
.num { text-align: right; font-variant-numeric: tabular-nums; font-family: "Roboto Mono", monospace; }
.text-green { color: var(--green); }
.text-red { color: var(--red); }
.text-amber { color: var(--amber); }
.text-charcoal { color: var(--charcoal); }
.mt-16 { margin-top: 16px; }
.mt-24 { margin-top: 24px; }
.flex { display: flex; }
.gap-8 { gap: 8px; }
.gap-12 { gap: 12px; }
.align-center { align-items: center; }
.justify-between { justify-content: space-between; }.btn-share { background: #2B6CB0; color: #fff; }

/* HELP TOOLTIP - one plain-language sentence, click or hover to open */
.help-tip {
  position: relative; display: inline-flex; align-items: center; justify-content: center;
  width: 16px; height: 16px; border-radius: 50%; background: var(--muted); color: var(--muted-foreground);
  font-size: 11px; font-weight: 700; cursor: pointer; margin-left: 4px; vertical-align: middle;
}
.help-tip-text {
  display: none; position: absolute; bottom: 22px; left: 0; width: 220px;
  background: var(--navy); color: #fff; font-size: 12px; font-weight: 400;
  padding: 8px 10px; border-radius: 6px; line-height: 1.4; z-index: 500; cursor: default;
}
.help-tip:hover .help-tip-text, .help-tip.open .help-tip-text { display: block; }

/* ============================================================
   MOBILE OPTIMIZATION (Section 9.16)
   Everything below is additive at narrower widths only - no desktop
   behavior changes. Two breakpoints: 900px collapses the sidebar into an
   off-canvas drawer (matches where it starts feeling cramped, a bit above
   the tablet width called out in the spec), 640px is the small-phone
   tightening pass (topbar controls, table cards, form columns).
   ============================================================ */

.topbar-mobile-search-toggle, .mobile-search-row, .filters-toggle-btn, .topbar-mobile-logo, .topbar-mobile-theme-toggle { display: none; }
.topbar-desktop-logo { display: block; height: 24px; width: auto; flex-shrink: 0; }
/* Bottom nav bar / FAB / splash markup is always rendered into the DOM
   (see nav() and showMobileSplashIfColdStart() in auth.js) but hidden by
   default here - only the max-width:900px block below turns it on, so
   desktop is completely unaffected regardless of what JS injects. */
.bottom-nav-mobile, .mobile-fab, .mobile-splash, .mobile-only-block, .mobile-only-flex, .login-logo-mobile, .settings-mobile-header, .settings-mobile-signout { display: none; }

@media (max-width: 900px) {
  /* SIDEBAR -> replaced by the bottom nav bar + FAB (this supersedes the
     earlier off-canvas hamburger-drawer pattern from the first mobile
     pass; the drawer, hamburger button, and backdrop are gone entirely
     rather than just hidden). */
  body.app-shell { display: block; }
  #nav-placeholder { position: static; }
  .sidebar { display: none; }

  /* PAGE / TOPBAR - padding-bottom is a general rule (not a per-page patch)
     so every scrollable mobile page clears the fixed FAB (bottom:78px,
     ~46px tall) plus the bottom nav bar beneath it, regardless of scroll
     position. See .mobile-fab below for the matching bottom offset. */
  .page { padding: 16px; padding-bottom: 148px; }
  .app-topbar { margin: -16px -16px 16px -16px; padding: 0 10px; gap: 8px; }
  #topbar-firm-name { display: none; } /* removed from the mobile top bar - Settings' shield tab carries the brand mark now */
  .nav-user { display: none; }
  .nav-logout { display: none; } /* mobile top bar is search + bell only; sign-out lives on the mobile Settings page */

  /* Full Ethix logo lockup (shield + wordmark), reinstated on the mobile
     top bar's left edge per live-testing feedback reversing the earlier
     "no logo" decision - the search bar collapses to icon-only until
     tapped, leaving real unused space here. Left of search icon / bell. */
  .topbar-mobile-logo { display: block; height: 24px; width: auto; flex-shrink: 0; margin-right: 2px; }
  .topbar-desktop-logo { display: none; } /* mobile top bar carries its own logo instance above */

  /* Mobile light/dark toggle - sits between the search icon and the
     notification bell, per spec. Extends the existing desktop theme system
     (toggleSidebarTheme/html.dark) rather than a separate mobile-only one. */
  .topbar-mobile-theme-toggle {
    display: flex; align-items: center; justify-content: center;
    width: 44px; height: 44px; background: none; border: none;
    color: var(--muted-foreground); cursor: pointer; border-radius: var(--radius-sm);
    flex-shrink: 0;
  }
  .topbar-mobile-theme-toggle:hover { background: var(--accent); }

  /* BOTTOM NAV BAR (Dashboard | Referrals | Firms/Attorneys | Settings) */
  .bottom-nav-mobile {
    display: flex;
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 400;
    background: var(--card); border-top: 1px solid var(--border);
    box-shadow: 0 -2px 8px rgba(16,24,40,0.08);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  .bottom-nav-item {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px; padding: 8px 4px 10px 4px; min-height: 56px;
    color: var(--muted-foreground); font-size: 10.5px; font-weight: 600;
    text-align: center;
  }
  .bottom-nav-item.active { color: var(--primary); }
  .bottom-nav-item svg { flex-shrink: 0; }
  .bottom-nav-shield { height: 18px; width: 18px; flex-shrink: 0; }
  /* Dark mode: --card is nearly the same near-black as --background, so the
     nav bar visually disappeared (live-screenshot finding). Reuses the
     exact --primary token already driving every button's background
     app-wide, rather than inventing a new color, so it reads as a distinct
     persistent surface. Light mode is untouched. */
  html.dark .bottom-nav-mobile { background: var(--primary); border-top-color: var(--primary); }
  html.dark .bottom-nav-item { color: var(--primary-foreground); opacity: 0.72; }
  html.dark .bottom-nav-item.active { color: var(--primary-foreground); opacity: 1; }

  /* FLOATING ACTION BUTTON - raised clear of the bottom nav bar (previously
     bottom:46px overlapped the nav's ~56-66px height, covering its labels/
     tap targets and underlying page content on every page - live-screenshot
     finding). bottom:78px leaves real visual separation above the nav bar;
     .page's padding-bottom above is sized to match this + the FAB's own
     height so page content never scrolls underneath it either. */
  .mobile-fab {
    display: flex; align-items: center; gap: 6px;
    position: fixed; right: 16px; bottom: 78px; z-index: 410;
    background: var(--primary); color: var(--primary-foreground);
    border: none; border-radius: 999px; padding: 12px 18px 12px 14px;
    font-size: 13px; font-weight: 600; cursor: pointer;
    box-shadow: 0 4px 12px rgba(16,24,40,0.25);
  }
  .mobile-fab-plus { font-size: 17px; line-height: 1; }

  /* The FAB (z-index 410) and bottom nav (z-index 400) both sit above the
     modal overlay (z-index 200), so opening any modal (e.g. Referral
     Detail) on mobile left them floating on top of the modal's own footer
     buttons - the "Archive/Edit/Share + FAB crammed together" overlap
     confirmed via live screenshot. Hide both while a modal is open rather
     than raising the modal's z-index, since the modal is meant to be the
     sole focus - nav underneath it shouldn't be interactable anyway. */
  body:has(.modal-overlay.open) .mobile-fab,
  body:has(.modal-overlay.open) .bottom-nav-mobile { display: none; }

  /* Modal footer action row (Archive / Edit / Share / Close) - side-by-side
     desktop layout doesn't fit a phone screen; stack into two full-width
     rows instead (left group, then right group). */
  .modal-footer[style*="space-between"] { flex-direction: column; align-items: stretch; gap: 8px; }
  .modal-footer[style*="space-between"] > div { display: flex !important; gap: 8px; }
  .modal-footer[style*="space-between"] > div > .btn { flex: 1; }

  /* Label/input+button rows on the Referral Detail page (Request Update,
     Client Testimonial, Documents upload) kept their desktop side-by-side
     flex layout on mobile, pushing the total row width past the viewport
     and forcing horizontal scroll to reach content past the fold - live-
     screenshot confirmed. Stack vertically instead; !important is needed
     since the row's own inline style sets display:flex directly. */
  .mobile-stack-row { flex-direction: column !important; align-items: stretch !important; }
  .mobile-stack-row > * { width: 100% !important; }

  /* COLD-START SPLASH SCREEN */
  .mobile-splash {
    display: flex; align-items: center; justify-content: center;
    position: fixed; inset: 0; z-index: 9999;
    background: var(--background);
    opacity: 1; transition: opacity 0.3s ease-out;
  }
  .mobile-splash.fade-out { opacity: 0; }
  .mobile-splash-logo { width: min(70vw, 280px); height: auto; }

  /* Generic "only show this on mobile" utility - used for the Dashboard's
     View Reports card and the Settings page's mobile header logo. */
  .mobile-only-block { display: block; }
  .mobile-only-flex { display: flex !important; }

  /* Inverse: hide a desktop-only control on mobile - e.g. the Directory
     page's separate "+ Add Attorney" button, replaced on mobile by a
     consolidated "+ Add Contact" (Attorney/General type dropdown). */
  .desktop-only { display: none !important; }

  /* Directory page mobile toggle row: stack the Firms/Attorneys/General
     tab row above the status filter dropdown, rather than the desktop's
     single inline row - consistent with the general "stack, don't cram
     into one row" mobile principle used throughout Section 9.16. */
  .dir-toggle-row { flex-direction: column; align-items: stretch !important; }
  .dir-toggle-row .dir-tabs { display: flex; gap: 8px; }
  .dir-toggle-row select { margin-left: 0 !important; width: 100% !important; }

  /* LOGIN - full logo lockup takes over from the small icon+wordmark pair
     used on desktop, prominent and centered above the form. */
  .login-logo-desktop { display: none !important; }
  .login-logo-mobile { display: block; text-align: center; margin-bottom: 16px; }
  .login-logo-mobile img { width: min(60vw, 220px); height: auto; }

  /* SETTINGS - full logo/shield header, only on mobile (the desktop
     sidebar already carries the brand mark everywhere else). */
  .settings-mobile-header {
    display: flex; align-items: center; justify-content: center;
    flex-direction: column; gap: 10px; padding: 8px 0 20px 0;
  }
  .settings-mobile-header img { height: 40px; width: auto; }
  .settings-mobile-signout { display: block; margin: 0 0 20px 0; }

  /* REPORTS KPI CARDS -> 2-PER-ROW (Section 9.16 QA finding: the 8 KPI
     cards were squeezing 4-across, truncating text like "CONVERSION RATE"
     and "126.4 days"). grid-3 (Dashboard's 3-card row) already reads fine
     at this width given its shorter labels, so it's deliberately left
     alone here. */
  .grid-4 { grid-template-columns: repeat(2, 1fr); }

  /* REPORTS CHARTS -> ONE FULL-WIDTH CARD PER CHART, at the same mobile
     breakpoint as everything else on this page (previously only kicked in
     at 640px, so 768px-wide phones still saw two shrunken charts side by
     side with skipped x-axis labels). Larger tick/legend fonts are applied
     in reports.html's loadCharts() based on window.innerWidth. */
  .charts-grid { grid-template-columns: 1fr; }

  /* FILTER BARS -> COLLAPSIBLE PANEL, toggled via the shared
     .filters-toggle-btn button. Originally Reports-only (#globalFilterBar);
     generalized to a reusable class so Referrals' filter bar (and any
     future page) can opt into the same collapse-behind-a-button pattern
     instead of a second bespoke implementation. */
  .mobile-collapsible-filters { display: none; flex-direction: column; align-items: stretch; gap: 10px; }
  .mobile-collapsible-filters.open { display: flex; }
  .mobile-collapsible-filters select, .mobile-collapsible-filters input { width: 100% !important; }
  .filters-toggle-btn { display: flex; }
}

@media (max-width: 640px) {
  .topbar-search-group > div { display: none; } /* hide the inline search input, replaced by the toggle below */
  .topbar-mobile-search-toggle {
    display: flex; align-items: center; justify-content: center;
    width: 44px; height: 44px; background: none; border: none;
    color: var(--muted-foreground); cursor: pointer; border-radius: var(--radius-sm);
  }
  .topbar-mobile-search-toggle:hover { background: var(--accent); }
  .mobile-search-row {
    display: none;
    padding: 10px 16px; background: var(--card); border-bottom: 1px solid var(--border);
    position: relative;
  }
  .mobile-search-row.open { display: block; }

  .page-header { flex-wrap: wrap; gap: 10px; }
  .page-title { font-size: 17px; }

  /* GRIDS -> SINGLE COLUMN (grid-4 reflows to 2-per-row and charts-grid to
     1-per-row at the 900px breakpoint above; grid-2/form-grid are the ones
     still forcing two columns at this narrower width) */
  .grid-2, .form-grid { grid-template-columns: 1fr; }
  .stat-row { flex-wrap: wrap; }
  .stat-box { min-width: 45%; }

  /* DATA TABLES -> STACKED CARDS. Cells are labeled with data-label via
     labelResponsiveTables() in auth.js so this doesn't need per-page
     markup changes. The row-action column has no header text, so it's
     deliberately left unlabeled (see JS) and just right-aligned here. */
  .table-wrap { overflow-x: visible; border: none; background: transparent; box-shadow: none; }
  .table-wrap table, .table-wrap tbody, .table-wrap tr, .table-wrap td { display: block; width: 100%; }
  .table-wrap thead { display: none; }
  .table-wrap tbody tr {
    margin-bottom: 12px; padding: 12px 14px;
    background: var(--card); border: 1px solid var(--border); border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
  }
  .table-wrap tbody tr:nth-child(even) { background: var(--card); } /* cancel the desktop zebra-stripe on cards */
  .table-wrap tbody tr:hover { background: var(--card); }
  .table-wrap tbody td {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    padding: 6px 0; border: none; text-align: right; font-size: 13px;
  }
  .table-wrap tbody td[data-label]::before {
    content: attr(data-label);
    font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.4px;
    color: var(--muted-foreground); text-align: left; flex-shrink: 0; margin-right: 8px;
  }
  .table-wrap tbody td:not([data-label]) { justify-content: flex-end; flex-wrap: wrap; }
  .table-wrap tbody td.num { text-align: right; }

  /* FORMS - inputs already stack via form-grid above; just widen tap
     targets so labeled selects/inputs meet the ~44px minimum on touch. */
  .form-input, .filter-bar input, .filter-bar select { min-height: 44px; }
  .card-form-row { flex-direction: column !important; gap: 0 !important; }
  #referral-card-layout { grid-template-columns: 1fr !important; }

  /* TOUCH TARGETS - generic minimum across nav items, dropdown links,
     pagination, and badges/buttons wherever they're not already sized. */
  .nav-link, .dropdown-link, a[style*="dropdownLinkStyle"] { min-height: 44px; }
  .pagination button { min-height: 36px; min-width: 36px; }
  input[type="checkbox"] { width: 22px; height: 22px; }
}
