html, body, #root {
  min-height: 100%;
}

/* The pipeline keeps its dedicated search control for mobile, where the
   global top search bar is hidden. On desktop, use the global search only. */
@media (min-width: 761px) {
  .crm-pipeline-toolbar > input[aria-label="Search deal pipeline"] {
    display: none !important;
  }
}

@media (max-width: 760px) {
  body {
    overflow: hidden;
  }

  .crm-shell {
    height: 100dvh !important;
    flex-direction: column !important;
  }

  .crm-sidebar {
    width: 100% !important;
    min-height: auto !important;
    flex-direction: row !important;
    position: relative;
    z-index: 30;
  }

  .crm-sidebar > div:first-child,
  .crm-sidebar > div:nth-last-child(-n+2) {
    display: none !important;
  }

  .crm-nav {
    display: flex !important;
    flex: 1 1 auto !important;
    padding: 7px 8px !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    gap: 4px;
    scrollbar-width: none;
  }

  .crm-nav::-webkit-scrollbar {
    display: none;
  }

  .crm-nav > button {
    width: auto !important;
    min-width: max-content;
    margin: 0 !important;
    padding: 8px 10px !important;
  }

  .crm-nav > button > span:nth-child(2) {
    flex: initial !important;
  }

  .crm-main {
    min-width: 0;
    overflow-x: hidden !important;
  }

  .crm-topbar {
    display: none !important;
  }

  .crm-content {
    padding: 16px 12px 28px !important;
  }

  .crm-content h1 {
    font-size: 21px !important;
  }

  .crm-content [style*="grid-template-columns"] {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .crm-content [style*="justify-content: space-between"] {
    flex-wrap: wrap;
    gap: 10px;
  }

  .crm-deal-toolbar,
  .crm-pipeline-toolbar {
    align-items: flex-start !important;
  }

  .crm-deal-toolbar > div:last-child,
  .crm-pipeline-toolbar > div:last-child {
    width: 100% !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  .crm-deal-toolbar select,
  .crm-deal-toolbar input,
  .crm-pipeline-toolbar input {
    min-width: 0 !important;
    flex: 1 1 145px !important;
    width: auto !important;
  }

  .crm-deal-toolbar button,
  .crm-pipeline-toolbar button {
    flex: 0 0 auto !important;
  }

  .crm-content select,
  .crm-content input,
  .crm-content textarea {
    max-width: 100%;
  }

  .crm-content div:has(> table) {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  .crm-content table {
    min-width: 680px;
  }

  .crm-content [style*="min-width: 240px"] {
    min-width: calc(100vw - 40px) !important;
    max-width: calc(100vw - 40px) !important;
  }

  /* Deal Pipeline: stages stack down the page; deals scroll across each stage. */
  .crm-content div:has(> div[style*="min-width: 240px"]) {
    flex-direction: column !important;
    align-items: stretch !important;
    overflow-x: visible !important;
    gap: 18px !important;
    padding-bottom: 8px !important;
  }

  .crm-content [style*="min-width: 240px"] {
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
  }

  .crm-content [style*="min-width: 240px"] > div:nth-child(2) {
    flex-direction: row !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    scroll-snap-type: x mandatory;
    scroll-padding-left: 2px;
    -webkit-overflow-scrolling: touch;
    gap: 10px !important;
    padding: 10px !important;
    min-height: 0 !important;
    scrollbar-width: none;
  }

  .crm-content [style*="min-width: 240px"] > div:nth-child(2)::-webkit-scrollbar {
    display: none;
  }

  .crm-content [style*="min-width: 240px"] > div:nth-child(2) > div {
    flex: 0 0 min(82vw, 320px) !important;
    scroll-snap-align: start;
  }

  .crm-content [style*="min-width: 240px"] > div:nth-child(2) > div > div:first-child {
    aspect-ratio: 4 / 3;
  }

  .crm-modal-backdrop {
    align-items: flex-start !important;
    padding: 8px !important;
    overflow-x: hidden !important;
    overscroll-behavior-x: none;
    touch-action: pan-y;
  }

  body:has(.crm-modal-backdrop) {
    overflow-x: hidden !important;
    overscroll-behavior-x: none;
  }

  body:has(.crm-modal-backdrop) > .crm-sign-out {
    display: none !important;
  }

  .crm-modal {
    width: calc(100vw - 16px) !important;
    max-width: calc(100vw - 16px) !important;
    min-width: 0 !important;
    max-height: calc(100dvh - 16px) !important;
    border-radius: 10px !important;
    overflow-x: hidden !important;
    overscroll-behavior-x: none;
    touch-action: pan-y;
    box-sizing: border-box !important;
  }

  .crm-modal *,
  .crm-modal *::before,
  .crm-modal *::after {
    box-sizing: border-box;
    max-width: 100%;
  }

  .crm-modal > div {
    width: 100% !important;
    min-width: 0 !important;
    padding: 16px !important;
    overflow-x: hidden !important;
  }

  .crm-modal > div:has(> div > img) {
    padding-top: 0 !important;
  }

  .crm-modal img,
  .crm-modal input,
  .crm-modal textarea,
  .crm-modal select,
  .crm-modal a,
  .crm-modal p,
  .crm-modal span,
  .crm-modal div {
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
  }

  /* Full-bleed hero image in the open deal card. */
  .crm-modal > div > div:has(> img:first-child) {
    margin-left: -16px !important;
    margin-right: -16px !important;
    width: calc(100% + 32px) !important;
    max-width: calc(100% + 32px) !important;
  }

  .crm-modal > div > div:has(> img:first-child) > img:first-child {
    display: block !important;
    width: 100% !important;
    max-width: none !important;
    margin: 0 !important;
    border-radius: 0 !important;
    object-fit: cover !important;
  }

  /* iOS Safari can keep a larger intrinsic width for form controls even when
     their parent is narrower. Clamp controls to the visible deal-card width. */
  .crm-modal input,
  .crm-modal textarea,
  .crm-modal select {
    display: block !important;
    width: 100% !important;
    max-width: calc(100vw - 84px) !important;
    min-width: 0 !important;
    flex: 0 1 calc(100vw - 84px) !important;
    box-sizing: border-box !important;
    margin-right: 0 !important;
  }

  .crm-modal input[type="date"],
  .crm-modal input[type="datetime-local"],
  .crm-modal input[type="time"] {
    -webkit-appearance: none;
    appearance: none;
  }

  .crm-modal label,
  .crm-modal form,
  .crm-modal [style*="width"],
  .crm-modal [style*="flex-basis"],
  .crm-modal [style*="min-width"] {
    min-width: 0 !important;
    max-width: 100% !important;
  }

  .crm-modal [style*="grid-template-columns"] {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .crm-modal [style*="justify-content: space-between"] {
    flex-wrap: wrap;
    gap: 10px;
  }

  .crm-modal button,
  .crm-content button {
    min-height: 38px;
  }
}

@media (max-width: 420px) {
  .crm-nav > button {
    font-size: 12px !important;
  }

  .crm-nav > button > span:first-child {
    display: none;
  }

  .crm-content {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }
}

/* Auth screens are form pages, not the scroll-locked CRM shell. */
@media (max-width: 760px) {
  body:has(.auth-shell) {
    overflow: auto;
  }

  .auth-shell {
    min-height: 100dvh;
    display: grid;
    place-items: start center;
    padding: max(24px, env(safe-area-inset-top)) 16px
      calc(32px + env(safe-area-inset-bottom));
  }

  .auth-card {
    width: min(420px, 100%);
    box-sizing: border-box;
    margin: 0 auto;
    padding: 28px 22px 24px;
    border-radius: 18px;
  }

  .auth-kicker {
    margin-bottom: 10px;
  }

  .auth-card h1 {
    font-size: clamp(36px, 10vw, 42px);
  }

  .auth-card p {
    margin-bottom: 20px;
  }

  .auth-form {
    gap: 12px;
  }

  .auth-switches {
    margin-bottom: 16px;
  }
}

@media (max-width: 360px) {
  .auth-shell {
    padding-left: 12px;
    padding-right: 12px;
  }

  .auth-card {
    padding: 24px 18px 20px;
  }
}

/* Keep the boot spinner perfectly circular on mobile Safari. */
#crm-cloud-loading-overlay .nncrm-spinner {
  display: block !important;
  box-sizing: border-box !important;
  width: 38px !important;
  height: 38px !important;
  min-width: 38px !important;
  min-height: 38px !important;
  max-width: 38px !important;
  max-height: 38px !important;
  aspect-ratio: 1 / 1 !important;
  flex: 0 0 38px !important;
  border-radius: 50% !important;
}
