/*
 * Stage-only UX-M1. This file is loaded after style.css and is inert until
 * html/body receive the mobile-shell-v2 class. Remove this link to rollback.
 */
:root.mobile-shell-v2 {
  --app-vh: 100vh;
  --app-vv-top: 0px;
  --mobile-safe-top: env(safe-area-inset-top, 0px);
  --mobile-safe-right: env(safe-area-inset-right, 0px);
  --mobile-safe-bottom: env(safe-area-inset-bottom, 0px);
  --mobile-safe-left: env(safe-area-inset-left, 0px);
}
@supports (height: 100dvh) {
  :root.mobile-shell-v2 { --app-vh: 100dvh; }
}

@media (max-width: 768px) {
  html.mobile-shell-v2,
  html.mobile-shell-v2 body {
    width: 100%;
    min-height: 100%;
    height: 100%;
  }

  body.mobile-shell-v2 {
    position: fixed;
    top: var(--app-vv-top);
    right: 0;
    bottom: auto;
    left: 0;
    min-height: var(--app-vh);
    height: var(--app-vh);
    overflow: hidden;
    overscroll-behavior: none;
    padding-left: var(--mobile-safe-left);
    padding-right: var(--mobile-safe-right);
  }

  /* Before authentication, the login may be taller than the visual viewport/keyboard. */
  body.mobile-shell-v2.mobile-login-mode {
    position: static;
    min-height: var(--app-vh);
    height: auto;
    overflow-x: hidden;
    overflow-y: auto;
  }
  body.mobile-shell-v2.mobile-login-mode .login-wrap {
    min-height: var(--app-vh);
    height: auto;
    padding-top: calc(12px + var(--mobile-safe-top));
    padding-bottom: calc(12px + var(--mobile-safe-bottom));
  }

  body.mobile-shell-v2 #app.app.active {
    display: flex;
    flex-direction: column;
    width: 100%;
    min-width: 0;
    height: var(--app-vh);
    min-height: 0;
    overflow: hidden;
  }

  /* Header and tab strip are outside the sole scrolling .content. */
  body.mobile-shell-v2 #app > .topbar {
    position: relative;
    top: auto;
    flex: 0 0 auto;
    min-width: 0;
    padding-top: calc(6px + var(--mobile-safe-top));
  }

  body.mobile-shell-v2 #app > .tabs {
    position: relative;
    top: auto !important;
    flex: 0 0 auto;
    min-width: 0;
    max-height: 42px;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
  }

  body.mobile-shell-v2 #app > .tabs.mobile-tabs-open {
    max-height: min(45vh, 260px);
    overflow-y: auto;
    flex-wrap: wrap;
  }

  body.mobile-shell-v2 #app > .content {
    flex: 1 1 auto;
    width: 100%;
    min-width: 0;
    min-height: 0;
    height: auto;
    max-width: none;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(12px + var(--mobile-safe-bottom));
  }

  /* Do not let desktop fullwidth rules create a second mobile scroller. */
  body.mobile-shell-v2.cp-fullwidth,
  body.mobile-shell-v2.cr-fullwidth { overflow: hidden; }
  body.mobile-shell-v2 .cp-fullwidth,
  body.mobile-shell-v2 .cr-fullwidth,
  body.mobile-shell-v2 #panelCp.cp-layout-active.active,
  body.mobile-shell-v2 #panelCr.cr-layout-active.active,
  body.mobile-shell-v2 #panelCp.cp-layout-active.active .cp-content,
  body.mobile-shell-v2 #panelCr.cr-layout-active.active .cr-content {
    height: auto;
    min-height: 0;
    overflow: visible;
  }

  /* Safe-area-aware fixed feedback. */
  body.mobile-shell-v2 .notify { top: var(--mobile-safe-top); }
  body.mobile-shell-v2 .notify.show { top: calc(8px + var(--mobile-safe-top)); }
  body.mobile-shell-v2 .toast-container {
    right: 10px;
    bottom: calc(10px + var(--mobile-safe-bottom));
    left: 10px;
    max-height: calc(var(--app-vh) - 20px - var(--mobile-safe-top) - var(--mobile-safe-bottom));
  }

  /* Keep every editable control at the iOS no-zoom threshold. */
  body.mobile-shell-v2 input:not([type="hidden"]),
  body.mobile-shell-v2 select,
  body.mobile-shell-v2 textarea {
    font-size: 16px !important;
    line-height: 1.35;
  }

  /* CP/CR: chips may wrap; the search/actions get a real second row. */
  body.mobile-shell-v2 #cpEmpresaFilter,
  body.mobile-shell-v2 #crEmpresaFilter {
    flex-wrap: wrap !important;
    overflow-x: visible;
    min-width: 0;
  }
  body.mobile-shell-v2 #cpEmpresaFilter > div:last-child,
  body.mobile-shell-v2 #crEmpresaFilter > div:last-child {
    flex: 1 0 100%;
    width: 100% !important;
    min-width: 0;
    margin-left: 0 !important;
  }

  /* Long tables scroll locally; card renderers still provide labels. */
  body.mobile-shell-v2 .table-wrap,
  body.mobile-shell-v2 .table-container {
    min-width: 0;
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-x: contain;
  }
  body.mobile-shell-v2 .table-wrap table,
  body.mobile-shell-v2 .table-container table { min-width: 0; }

  /* The bottom nav is in the flex shell, never overlaying .content. */
  body.mobile-shell-v2 .mobile-bottom-nav {
    display: flex;
    flex: 0 0 auto;
    align-items: stretch;
    justify-content: space-around;
    min-height: 56px;
    padding: 5px 4px var(--mobile-safe-bottom);
    background: var(--surface);
    border-top: 1px solid var(--border);
    z-index: 120;
  }
  body.mobile-shell-v2 .mobile-bottom-nav button {
    border: 0;
    border-radius: 8px;
    flex: 1 1 0;
    min-width: 0;
    min-height: 46px;
    padding: 5px 3px;
    background: transparent;
    color: var(--text-secondary);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
  }
  body.mobile-shell-v2 .mobile-bottom-nav button[aria-current="page"] {
    color: var(--primary);
    background: var(--primary-glow);
  }
  body.mobile-shell-v2 .mobile-bottom-nav button span { font-size: 18px; line-height: 1; }
  body.mobile-shell-v2 .mobile-bottom-nav button small { font-size: 11px; line-height: 1.1; }

  /* One modal scroll owner: overlay is clipped, modal body can scroll. */
  body.mobile-shell-v2 .modal-overlay,
  body.mobile-shell-v2 .crm-modal,
  body.mobile-shell-v2 .ctx-modal-overlay,
  body.mobile-shell-v2 .pm-ov {
    overflow: hidden;
    padding: 0;
    padding-bottom: var(--mobile-safe-bottom);
    align-items: flex-end;
  }
  body.mobile-shell-v2 .modal-overlay .modal,
  body.mobile-shell-v2 .crm-modal-body,
  body.mobile-shell-v2 .pm-box {
    max-height: calc(var(--app-vh) - var(--mobile-safe-top));
    padding-bottom: calc(18px + var(--mobile-safe-bottom));
  }
}

@media (min-width: 769px) {
  /* The flag may be left on during desktop QA without changing legacy layout. */
  .mobile-bottom-nav { display: none !important; }
}
