/**
 * Phase 50 — Mobile density system (phones ≤768px only).
 * Desktop / tablet (≥769px) are intentionally unaffected.
 *
 * Utilities:
 *   .mobile-dense  — opt-in dense page root
 *   .mobile-grid   — compact multi-column grids
 *   .mobile-card   — compressed cards
 *   .mobile-list   — compact list rows
 *   .mobile-toolbar— compact toolbars
 *   .mobile-tabs   — horizontal scrolling tab strips
 *   .mobile-fab    — floating primary action
 */

@media (max-width: 768px) {
  :root {
    --fo-phone-edge: 6px;
    --fo-phone-gap: 5px;
    --fo-phone-pad: 6px;
    --fo-phone-title: 17px;
    --fo-phone-section: 14px;
    --fo-phone-body: 12.5px;
    --fo-phone-secondary: 11px;
    --fo-phone-caption: 10.5px;
    --fo-phone-btn: 12.5px;
    --fo-phone-input: 13px;
    --fo-phone-table: 12px;
    --fo-phone-btn-h: 38px;
    --fo-phone-row-h: 44px;
    --fo-phone-topbar-h: 44px;
    --fo-phone-nav-h: 56px;
    --fo-phone-fab: 48px;
  }

  /* Align shell tokens with density targets */
  .mobile-mode,
  .fieldops-shell.mobile-mode {
    --fo-m-page-pad: var(--fo-phone-edge);
    --fo-m-card-gap: var(--fo-phone-gap);
    --fo-m-section-gap: 10px;
    --fo-m-radius: 10px;
    --fo-m-radius-sm: 8px;
    --fo-m-tap: 40px;
    --fo-m-btn-h: var(--fo-phone-btn-h);
    --fo-m-input-h: 40px;
    --fo-m-nav-h: var(--fo-phone-nav-h);
    --fo-m-fab-size: var(--fo-phone-fab);
    --fo-m-body: var(--fo-phone-body);
    --fo-m-title: var(--fo-phone-title);
    --fo-topbar-height: var(--fo-phone-topbar-h);
    font-size: var(--fo-phone-body);
  }

  /* ── Top bar ───────────────────────────────────────────────── */
  .mobile-mode .fieldops-topbar {
    height: auto !important;
    min-height: calc(var(--fo-phone-topbar-h) + env(safe-area-inset-top, 0px)) !important;
    max-height: none !important;
    padding: env(safe-area-inset-top, 0px) 6px 0 !important;
    gap: 4px !important;
    padding-left: calc(6px + env(safe-area-inset-left, 0px)) !important;
    padding-right: calc(6px + env(safe-area-inset-right, 0px)) !important;
  }

  .mobile-mode .fieldops-topbar > * {
    min-height: var(--fo-phone-topbar-h);
  }

  .mobile-mode .company-branding-name {
    font-size: 13px !important;
    max-width: 120px;
  }

  .mobile-mode .company-branding-logo,
  .mobile-mode .company-branding-logo img {
    max-height: 28px !important;
    width: auto;
  }

  .mobile-mode .topbar-icon-btn,
  .mobile-mode .topbar-search-mobile-btn {
    width: 36px !important;
    height: 36px !important;
    min-height: 36px !important;
    padding: 0 !important;
  }

  /* ── Page chrome / edge padding ────────────────────────────── */
  .mobile-mode .content-area {
    padding: var(--fo-phone-edge) !important;
    padding-bottom: calc(var(--fo-phone-nav-h) + env(safe-area-inset-bottom, 0px) + 10px) !important;
  }

  .mobile-mode .fo-page,
  .mobile-mode .fo-page-body,
  .mobile-mode .fo-m-page,
  .mobile-dense {
    padding-left: 0 !important;
    padding-right: 0 !important;
    gap: var(--fo-phone-gap) !important;
    max-width: 100% !important;
  }

  /* ── Typography ────────────────────────────────────────────── */
  .mobile-mode h1,
  .mobile-mode .fo-page-title,
  .mobile-mode .fo-m-page-header h1 {
    font-size: var(--fo-phone-title) !important;
    line-height: 1.25 !important;
    margin: 0 0 4px !important;
    font-weight: 700 !important;
  }

  .mobile-mode h2,
  .mobile-mode h3,
  .mobile-mode .fo-section-title,
  .mobile-mode .fo-m-card__title {
    font-size: var(--fo-phone-section) !important;
    line-height: 1.3 !important;
    margin: 0 0 4px !important;
  }

  .mobile-mode p,
  .mobile-mode .fo-body,
  .mobile-mode label,
  .mobile-mode .form-label {
    font-size: var(--fo-phone-body) !important;
    margin-bottom: 2px !important;
  }

  .mobile-mode .text-muted,
  .mobile-mode .fo-secondary,
  .mobile-mode small,
  .mobile-mode .fo-m-page-header__sub,
  .mobile-mode .fo-m-entity-card__meta {
    font-size: var(--fo-phone-secondary) !important;
  }

  .mobile-mode .badge,
  .mobile-mode .caption,
  .mobile-mode .fo-caption {
    font-size: var(--fo-phone-caption) !important;
  }

  /* ── Spacing compression ───────────────────────────────────── */
  .mobile-mode .mb-1,
  .mobile-mode .mb-2,
  .mobile-mode .mb-3,
  .mobile-mode .mb-4 {
    margin-bottom: 6px !important;
  }

  .mobile-mode .mt-1,
  .mobile-mode .mt-2,
  .mobile-mode .mt-3,
  .mobile-mode .mt-4 {
    margin-top: 6px !important;
  }

  .mobile-mode .py-3,
  .mobile-mode .py-4,
  .mobile-mode .p-3,
  .mobile-mode .p-4 {
    padding: var(--fo-phone-pad) !important;
  }

  .mobile-mode .gap-2,
  .mobile-mode .gap-3,
  .mobile-mode .gap-4 {
    gap: var(--fo-phone-gap) !important;
  }

  /* ── Cards ─────────────────────────────────────────────────── */
  .mobile-mode .card,
  .mobile-mode .fo-card,
  .mobile-mode .kpi-card,
  .mobile-mode .fo-m-card,
  .mobile-mode .fo-m-entity-card,
  .mobile-card {
    padding: 8px 10px !important;
    margin-bottom: 6px !important;
    border-radius: 10px !important;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06) !important;
  }

  .mobile-mode .card-body,
  .mobile-mode .fo-card-body {
    padding: 6px 8px !important;
  }

  .mobile-mode .card-header,
  .mobile-mode .fo-card-header {
    padding: 6px 8px !important;
    font-size: var(--fo-phone-section) !important;
  }

  /* ── Lists ─────────────────────────────────────────────────── */
  .mobile-list,
  .mobile-mode .fo-m-list,
  .mobile-mode .list-group {
    display: flex;
    flex-direction: column;
    gap: 4px !important;
  }

  .mobile-list > *,
  .mobile-mode .list-group-item,
  .mobile-mode .fo-m-entity-card {
    min-height: var(--fo-phone-row-h);
    padding: 6px 8px !important;
  }

  .mobile-mode .fo-m-entity-card__title {
    font-size: 13px !important;
    font-weight: 700 !important;
    margin: 0 !important;
  }

  .mobile-mode .fo-m-entity-card__meta {
    margin-top: 1px !important;
    line-height: 1.25 !important;
  }

  /* ── Forms ─────────────────────────────────────────────────── */
  .mobile-mode .form-group,
  .mobile-mode .mb-3.form-group,
  .mobile-mode .fo-field {
    margin-bottom: 6px !important;
  }

  .mobile-mode .form-label,
  .mobile-mode label.form-label {
    margin-bottom: 2px !important;
    font-size: var(--fo-phone-secondary) !important;
  }

  .mobile-mode .form-control,
  .mobile-mode .form-select,
  .mobile-mode input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="submit"]),
  .mobile-mode select,
  .mobile-mode textarea {
    min-height: 40px !important;
    height: auto !important;
    padding: 6px 10px !important;
    font-size: 16px !important; /* keep ≥16px to avoid iOS zoom */
    line-height: 1.3 !important;
  }

  .mobile-mode .row.g-3,
  .mobile-mode .row.g-4,
  .mobile-grid {
    --bs-gutter-x: 8px;
    --bs-gutter-y: 6px;
    gap: var(--fo-phone-gap);
  }

  /* Prefer two-up city/state style rows when using .mobile-grid */
  .mobile-grid {
    display: grid !important;
    grid-template-columns: 1fr 1fr;
  }

  .mobile-grid > .mobile-grid__full {
    grid-column: 1 / -1;
  }

  /* ── Buttons ───────────────────────────────────────────────── */
  .mobile-mode .btn,
  .mobile-mode button.btn,
  .mobile-mode .fo-m-btn {
    min-height: var(--fo-phone-btn-h) !important;
    height: auto !important;
    padding: 6px 12px !important;
    font-size: var(--fo-phone-btn) !important;
    border-radius: 8px !important;
    font-weight: 600 !important;
  }

  .mobile-mode .btn-sm {
    min-height: 34px !important;
    font-size: 11.5px !important;
    padding: 4px 8px !important;
  }

  .mobile-mode .btn-lg {
    min-height: 40px !important;
    font-size: 13px !important;
  }

  /* ── Tables ────────────────────────────────────────────────── */
  .mobile-mode table,
  .mobile-mode .table {
    font-size: var(--fo-phone-table) !important;
  }

  .mobile-mode .table > :not(caption) > * > * {
    padding: 4px 6px !important;
  }

  .mobile-mode .table thead th {
    font-size: 11px !important;
    padding: 5px 6px !important;
    white-space: nowrap;
  }

  .mobile-mode .table-responsive {
    margin-left: calc(-1 * var(--fo-phone-edge));
    margin-right: calc(-1 * var(--fo-phone-edge));
    padding-left: var(--fo-phone-edge);
    padding-right: var(--fo-phone-edge);
  }

  /* ── Horizontal tabs ───────────────────────────────────────── */
  .mobile-tabs,
  .mobile-mode .fo-customer-tabbar,
  .mobile-mode .fo-workspace-tabs,
  .mobile-mode .nav-tabs,
  .mobile-mode .fo-subnav {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch;
    gap: 4px !important;
    padding: 2px 0 6px !important;
    scrollbar-width: none;
  }

  .mobile-tabs::-webkit-scrollbar,
  .mobile-mode .fo-customer-tabbar::-webkit-scrollbar,
  .mobile-mode .nav-tabs::-webkit-scrollbar {
    display: none;
  }

  .mobile-tabs > *,
  .mobile-mode .fo-customer-tab,
  .mobile-mode .nav-tabs .nav-link,
  .mobile-mode .fo-subnav a,
  .mobile-mode .fo-subnav button {
    flex: 0 0 auto !important;
    white-space: nowrap !important;
    min-height: 34px !important;
    padding: 4px 10px !important;
    font-size: 12px !important;
  }

  /* Customer tabs: top strip (not competing with bottom app nav) */
  .mobile-mode .fo-customer-tabbar--mobile {
    position: sticky !important;
    top: 0;
    bottom: auto !important;
    left: auto !important;
    right: auto !important;
    z-index: 20;
    background: inherit;
    border-top: none !important;
    border-bottom: 1px solid rgba(148, 163, 184, 0.25);
    padding: 4px 0 6px !important;
  }

  .mobile-mode .fo-customer-tabbar--mobile .fo-customer-tab {
    flex: 0 0 auto !important;
    flex-direction: row !important;
    min-height: 34px !important;
    font-size: 12px !important;
  }

  .mobile-mode .fo-customer-shell--mobile .fo-customer-main {
    padding-bottom: calc(var(--fo-phone-nav-h) + env(safe-area-inset-bottom, 0px) + 12px) !important;
  }

  .mobile-mode .fo-customer-more-menu {
    bottom: auto !important;
    top: 2.75rem;
    right: 0.5rem;
  }

  /* ── Toolbar ───────────────────────────────────────────────── */
  .mobile-toolbar,
  .mobile-mode .fo-toolbar,
  .mobile-mode .ops-topbar,
  .mobile-mode .page-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px !important;
    margin-bottom: 6px !important;
  }

  /* ── Bottom nav density ────────────────────────────────────── */
  .mobile-mode .fo-m-bottom-nav {
    min-height: calc(var(--fo-phone-nav-h) + env(safe-area-inset-bottom, 0px));
    padding: 4px 2px calc(4px + env(safe-area-inset-bottom, 0px));
    grid-template-columns: repeat(5, 1fr) !important;
  }

  .mobile-mode .fo-m-bottom-nav__item {
    min-height: 44px;
    font-size: 10.5px !important;
    gap: 1px;
    padding: 2px 1px;
  }

  .mobile-mode .fo-m-bottom-nav__item i {
    font-size: 1.15rem !important;
  }

  /* Center FAB slot removed from 5-tab IA — hide leftover wrap if present */
  .mobile-mode .fo-m-bottom-nav__fab-wrap {
    display: none !important;
  }

  /* ── Floating action button ────────────────────────────────── */
  .mobile-fab,
  .mobile-mode .fo-m-page-fab {
    position: fixed;
    right: 12px;
    bottom: calc(var(--fo-phone-nav-h) + env(safe-area-inset-bottom, 0px) + 12px);
    z-index: 1030;
    width: var(--fo-phone-fab);
    height: var(--fo-phone-fab);
    border-radius: 50%;
    border: 0;
    background: var(--bs-primary, #0d6efd);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 6px 16px rgba(13, 110, 253, 0.35);
    font-size: 1.35rem;
    line-height: 1;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-fab span,
  .mobile-mode .fo-m-page-fab span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
  }

  /* ── Photos grid ───────────────────────────────────────────── */
  .mobile-mode .fo-photo-grid,
  .mobile-mode .job-photo-grid,
  .mobile-mode .unified-photo-grid {
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    gap: 4px !important;
  }

  @media (max-width: 380px) {
    .mobile-mode .fo-photo-grid,
    .mobile-mode .job-photo-grid,
    .mobile-mode .unified-photo-grid {
      grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
    }
  }

  @media (min-width: 381px) and (max-width: 768px) {
    .mobile-mode .fo-photo-grid,
    .mobile-mode .job-photo-grid,
    .mobile-mode .unified-photo-grid {
      grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    }
  }

  /* ── Calendar / agenda density ─────────────────────────────── */
  .mobile-mode .fo-m-agenda-list,
  .mobile-mode .fc .fc-timegrid-slot {
    gap: 4px !important;
  }

  .mobile-mode .fo-m-agenda-list {
    gap: 4px !important;
  }

  .mobile-mode .fc .fc-timegrid-slot {
    height: 2rem !important;
  }

  /* Estimate sticky bar chrome lives in estimate-mobile-sticky.css (fixed + actions). */

  /* Agenda / entity card truncation — keep rows compact */
  .mobile-mode .fo-m-agenda-list .fo-m-entity-card__title,
  .mobile-mode .fo-m-agenda-list .fo-m-entity-card__meta {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow-wrap: normal;
    word-break: normal;
  }

  .mobile-mode .fo-m-agenda-list .fo-m-entity-card__title {
    display: block;
    max-width: 100%;
  }

  /* Photo grid — keep controls inside tiles */
  .mobile-mode .unified-photo-card,
  .mobile-mode .unified-photo-card-meta {
    min-width: 0;
  }

  .mobile-mode .unified-photo-card img {
    aspect-ratio: 1 / 1;
    height: auto !important;
    width: 100%;
    object-fit: cover;
  }

  /* ── Work order header density ─────────────────────────────── */
  .mobile-mode .fo-m-wo-header {
    padding: 8px !important;
    margin-bottom: 6px !important;
  }

  .mobile-mode .fo-m-wo-header .fo-m-entity-card__meta,
  .mobile-mode .wo-summary-strip {
    font-size: var(--fo-phone-secondary) !important;
    margin-top: 4px !important;
    margin-bottom: 0 !important;
  }

  /* ── Overflow safety ───────────────────────────────────────── */
  .mobile-mode .main-content,
  .mobile-mode .content-area,
  .mobile-mode .fo-m-page {
    overflow-x: hidden;
    max-width: 100vw;
  }
}

/* Explicit desktop lock — never show phone chrome above tablet */
@media (min-width: 769px) {
  .mobile-fab,
  .fo-m-page-fab {
    display: none !important;
  }
}
