@media (max-width: 1200px) {
  #topbar-root nav {
    overflow-x: auto;
    white-space: nowrap;
    padding-bottom: 4px;
  }
}

@media (max-width: 980px) {
  #mobileNavToggle {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
  }

  #topNav {
    display: none !important;
  }

  #topNav.is-open {
    display: grid !important;
    grid-template-columns: 1fr;
    gap: 6px !important;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 8px;
    max-height: 48dvh;
    overflow-y: auto;
  }

  #topNav.is-open .top-nav-btn.active {
    background: #1b4b8a;
    color: #fff;
    border-color: #1b4b8a;
  }

  #app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto 1fr;
    min-height: 100dvh;
  }

  #topbar-root {
    padding: 8px 12px !important;
    flex-direction: column;
    align-items: stretch !important;
    gap: 8px;
  }

  #topbar-root nav {
    width: 100%;
  }

  #topbar-root .btn {
    padding: 7px 10px;
    font-size: 12px;
  }

  #sidebar-root {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 10px 12px !important;
  }

  #view-root {
    overflow-x: hidden;
  }

  .bf-page-head,
  .bf-page-body {
    padding-left: 14px !important;
    padding-right: 14px !important;
  }

  .bf-page-title {
    font-size: 22px;
  }

  .bf-page-head-row {
    flex-direction: column;
    align-items: stretch;
  }

  .bf-pill-btn {
    width: 100%;
  }

  .bf-stats-row {
    flex-direction: column;
  }

  .bf-stat-card {
    border-right: 0;
    border-bottom: 1px solid #e5e7eb;
  }

  .bf-stat-card:last-child {
    border-bottom: 0;
  }

  .crm-header {
    padding: 12px 14px;
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .crm-body-layout {
    flex-direction: column;
  }

  .crm-sidenav {
    width: 100%;
    border-right: 0;
    border-bottom: 1px solid #e5e7eb;
    padding: 10px 0;
  }

  .crm-main-content {
    min-height: 0;
  }

  .crm-toolbar {
    padding: 10px 12px;
  }

  .crm-table-wrap {
    overflow-x: auto;
  }
}

@media (max-width: 640px) {
  #topbar-root strong {
    font-size: 15px;
  }

  .btn {
    font-size: 12px;
  }

  .bf-tabs {
    overflow-x: auto;
    white-space: nowrap;
  }

  .bf-tab-btn {
    margin-right: 14px;
    font-size: 13px;
  }

  .bf-table {
    min-width: 680px;
  }

  #jobsView .jobs-cal-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  #jobsView .jobs-cal-title {
    font-size: 18px;
    margin-left: 0;
    margin-top: 4px;
    width: 100%;
  }

  #jobsView .jobs-cal-add {
    width: 100%;
    text-align: center;
  }
}

/* View-specific hardening for Fas 2 */
@media (max-width: 980px) {
  /* Offers/Invoices editor: stack form + preview */
  #offerEditorView > div,
  #invoiceEditorView > div {
    grid-template-columns: 1fr !important;
    padding: 10px !important;
    gap: 10px !important;
  }

  #offerEditorView .btn,
  #invoiceEditorView .btn {
    min-height: 40px;
  }

  #offerRowsContainer input,
  #offerRowsContainer select,
  #newInvRows input,
  #newInvRows select {
    min-height: 38px;
    font-size: 14px;
  }

  #newInvRows > div,
  #offerRowsContainer > div {
    margin-bottom: 10px !important;
  }

  /* Schedule: keep horizontal scroll but clearer affordance */
  #scheduleView {
    min-height: 0;
  }

  #scheduleView > div:nth-child(1) {
    padding: 8px 10px !important;
    gap: 8px !important;
  }

  #scheduleView #weekCostBar {
    font-size: 12px;
    padding: 6px 10px !important;
  }

  #scheduleView #scheduleGrid {
    min-width: 820px;
  }

  /* Chat: sidebar stacks on top, active chat below */
  #chattView .chat-layout {
    flex-direction: column;
    min-height: calc(100dvh - 180px);
  }

  #chattView .chat-sidebar {
    width: 100% !important;
    max-height: 280px;
    overflow-y: auto;
  }

  #chattView .chat-main {
    min-height: 380px;
  }

  #chattView #chatMessageFeed {
    max-height: 50dvh;
  }

  #chattView .chat-input-wrap textarea {
    min-height: 42px;
    font-size: 14px;
  }
}

@media (max-width: 640px) {
  /* Reduce dense headers and statistics cards on very small screens */
  .bf-page-title {
    font-size: 20px;
  }

  .bf-stat-value {
    font-size: 18px;
  }

  #offersView .bf-page-head-row,
  #invoicesView .bf-page-head-row {
    margin-bottom: 12px;
  }

  #offersView .bf-page-body,
  #invoicesView .bf-page-body {
    padding-top: 10px !important;
  }

  #offerEditorView h2,
  #invoiceEditorView h2 {
    font-size: 18px;
  }

  /* chat modal */
  #broadcastModal > div {
    width: 96% !important;
    padding: 12px !important;
  }
}

