/*
 * Phone-layout guardrails
 * Loaded after the shared visual layer. These rules deliberately change only
 * layout, sizing, wrapping, and overflow for high-density screens.
 */

@media (max-width: 768px) {
  /* A flex/grid child without min-width:0 is the source of most mobile overflow. */
  #screen-warehouse .wh-view-shell,
  #screen-quotations .q-view-shell,
  #screen-profile .profile-container,
  #screen-tools .tools-menu,
  #screen-notifications .pg-tab-panel,
  #screen-notification-settings .form-container,
  #screen-daily-checklist-form .form-container,
  #screen-daily-checklist-run .detail-container,
  #screen-daily-checklist-my .detail-container,
  #screen-daily-checklist-templates .detail-container {
    min-width: 0;
    max-width: 100%;
  }

  #screen-warehouse .wh-view-shell > *,
  #screen-quotations .q-view-shell > *,
  #screen-profile .profile-container > *,
  #screen-tools .tools-menu > *,
  #screen-notifications .pg-tab-panel > *,
  #screen-notification-settings .form-container > *,
  #screen-daily-checklist-form .form-container > *,
  #screen-daily-checklist-run .detail-container > *,
  #screen-daily-checklist-my .detail-container > *,
  #screen-daily-checklist-templates .detail-container > * {
    min-width: 0;
  }

  /* Warehouse and quotations: keep the work area within the phone viewport. */
  #screen-warehouse .wh-filter-bar,
  #screen-quotations .q-filter-bar {
    margin-inline: 12px;
    padding: 10px;
    border-radius: 18px;
  }

  #screen-warehouse .wh-filter-bar .search-box,
  #screen-quotations .q-filter-bar .search-box,
  #screen-notifications .search-box {
    min-width: 0 !important;
    width: 100%;
  }

  #screen-warehouse .wh-search-box input,
  #screen-quotations .q-search-box input,
  #screen-notifications .search-box input {
    min-width: 0;
    width: 100%;
  }

  #screen-warehouse .wh-type-tabs {
    margin-inline: 12px;
    padding: 6px;
    scroll-padding-inline: 6px;
    -webkit-overflow-scrolling: touch;
  }

  #screen-warehouse .wh-type-tabs .pg-tab-btn {
    flex: 0 0 auto;
    min-height: 40px;
  }

  #screen-warehouse .wh-list,
  #screen-quotations .q-list {
    padding-inline: 12px;
    padding-bottom: calc(var(--nav-h) + 92px);
  }

  #screen-warehouse .wh-project-card,
  #screen-quotations .quotation-project-card,
  #screen-quotations .quotation-item,
  #screen-warehouse .wh-item-card {
    min-width: 0;
  }

  #screen-warehouse .wh-project-card__body,
  #screen-quotations .quotation-item__body,
  #screen-warehouse .wh-item-card__header > div,
  #screen-warehouse .wh-item-card__body {
    min-width: 0;
  }

  #screen-warehouse .wh-item-card__header,
  #screen-warehouse .wh-item-card__actions,
  #screen-quotations .quotation-item {
    gap: 10px;
  }

  #screen-warehouse .wh-item-card__actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
  }

  #screen-warehouse .wh-item-card__actions .btn-icon {
    flex: 0 0 40px;
  }

  /* Quotation detail remains readable: use its mobile cards, never a clipped table. */
  #screen-quotations #q-detail-content {
    width: 100%;
    padding-inline: 12px;
    padding-bottom: calc(var(--nav-h) + 104px);
  }

  /* Settings forms with a fixed bottom nav: reserve space so the Save
     button at the end of the form isn't hidden underneath it. */
  #screen-branding-settings .form-container,
  #screen-shifts-settings .form-container,
  #screen-roles-permissions .form-container {
    padding-bottom: calc(var(--nav-h, 64px) + env(safe-area-inset-bottom, 0px) + 32px) !important;
  }

  #screen-quotations .q-detail-content .quotation-paper,
  #screen-quotations #q-detail-content .quotation-paper {
    width: 100%;
    min-width: 0;
    overflow: hidden;
  }

  #screen-quotations .quotation-paper__table-wrap {
    display: none;
  }

  #screen-quotations .quotation-paper__mobile-list {
    display: grid !important;
    gap: 10px;
  }

  #screen-quotations .quotation-paper__hero,
  #screen-quotations .quotation-paper__footer,
  #screen-quotations .quotation-paper__company {
    grid-template-columns: 1fr;
  }

  #screen-quotations .quotation-paper__company,
  #screen-quotations .quotation-paper__item-main,
  #screen-quotations .quotation-paper__total-row {
    min-width: 0;
  }

  #screen-quotations .quotation-paper__company-copy,
  #screen-quotations .quotation-paper__recipient-line span,
  #screen-quotations .quotation-paper__item-copy,
  #screen-quotations .quotation-paper__item-name,
  #screen-quotations .quotation-paper__item-description {
    overflow-wrap: anywhere;
  }

  #screen-quotations #q-detail-actions,
  #screen-quotations .detail-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  }

  #screen-quotations #q-detail-actions .btn,
  #screen-quotations .detail-actions .btn {
    min-width: 0;
    width: 100%;
    padding-inline: 10px;
    font-size: 12px;
  }

  #screen-quotations .q-item-row,
  #screen-quotations .q-item-grid,
  #screen-quotations .q-item-product-picker,
  #screen-quotations .q-item-product-dropdown {
    min-width: 0;
    max-width: 100%;
  }

  #screen-quotations .q-item-editable-fields .form-row-2 {
    grid-template-columns: 1fr;
  }

  #screen-quotations .q-totals-box,
  #screen-quotations .q-total-row {
    min-width: 0;
  }

  #screen-quotations .q-total-row strong,
  #screen-quotations .q-line-total-display {
    overflow-wrap: anywhere;
    text-align: end;
  }

  /* Profile and tools: content can shrink without pushing icons off screen. */
  #screen-profile .profile-container,
  #screen-tools .tools-menu {
    width: 100%;
    margin: 0;
    padding: 12px 12px calc(var(--nav-h) + 28px);
  }

  #screen-profile .profile-hero-main,
  #screen-profile .profile-main-info,
  #screen-profile .profile-meta-item {
    min-width: 0;
  }

  #screen-profile .profile-hero-main {
    align-items: flex-start;
  }

  #screen-profile .profile-main-info,
  #screen-profile .profile-main-email,
  #screen-profile .profile-meta-value {
    overflow-wrap: anywhere;
  }

  #screen-profile .profile-hero-chips {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #screen-profile .profile-chip {
    min-width: 0;
  }

  #screen-profile .form-actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  #screen-profile .form-actions .btn {
    width: 100%;
  }

  #screen-tools .tool-card {
    width: 100%;
    min-width: 0;
    align-items: flex-start;
    gap: 10px;
    padding: 14px;
  }

  #screen-tools .tool-card-copy {
    min-width: 0;
  }

  #screen-tools .tool-card-copy strong,
  #screen-tools .tool-card-copy small {
    overflow-wrap: anywhere;
  }

  #screen-tools .tool-card-arrow {
    flex: 0 0 auto;
    margin-top: 5px;
  }

  /* Notification inbox and settings have dense controls and long strings. */
  #screen-notifications .pg-filter-bar {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
    padding: 10px 12px !important;
  }

  #screen-notifications #notif-unread-filter {
    width: 100%;
    min-width: 0;
  }

  #screen-notifications .pg-tab-panel {
    width: 100%;
    padding: 12px 12px calc(var(--nav-h) + 28px) !important;
  }

  #screen-notifications .notif-item,
  #screen-notifications .notif-item-body,
  #screen-notifications .notif-item-title,
  #screen-notifications .notif-item-body-text,
  #screen-notifications .notif-item-meta {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  #screen-notification-settings #notif-settings-tabs {
    gap: 6px !important;
    padding: 8px 12px !important;
    scrollbar-width: none;
    scroll-padding-inline: 12px;
  }

  #screen-notification-settings #notif-settings-tabs::-webkit-scrollbar {
    display: none;
  }

  #screen-notification-settings #notif-settings-tabs .pg-tab-btn {
    flex: 0 0 auto;
    min-height: 40px;
  }

  #screen-notification-settings .form-container {
    width: 100% !important;
    max-width: none !important;
    margin: 0 !important;
    padding: 12px 12px calc(var(--nav-h) + 28px) !important;
  }

  #screen-notification-settings [style*="grid-template-columns"] {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  #screen-notification-settings label[style*="justify-content:space-between"],
  #screen-notification-settings div[style*="justify-content:space-between"] {
    gap: 12px;
  }

  #screen-notification-settings label[style*="justify-content:space-between"] > :first-child,
  #screen-notification-settings div[style*="justify-content:space-between"] > :first-child {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  #screen-notification-settings [style*="font-family:monospace"] {
    min-width: 0;
    max-width: 100%;
  }

  #screen-notification-settings .form-actions,
  #screen-notification-settings .form-actions > div {
    flex-wrap: wrap;
  }

  #screen-notification-settings .form-actions .btn {
    min-width: 0;
  }

  #screen-notification-settings table {
    min-width: 560px;
  }

  /* Daily checklist creation: collapse paired fields and keep repeaters usable. */
  #screen-daily-checklist-form .form-container,
  #screen-daily-checklist-run .detail-container,
  #screen-daily-checklist-my .detail-container,
  #screen-daily-checklist-templates .detail-container {
    width: 100%;
    margin: 0;
    padding: 12px 12px calc(var(--nav-h) + 28px);
  }

  #screen-daily-checklist-form .form-row-2,
  #screen-daily-checklist-form .dcf-item-row .form-row-2 {
    grid-template-columns: minmax(0, 1fr);
  }

  #screen-daily-checklist-form #dcf-bulk-add-panel > div,
  #screen-daily-checklist-form .form-actions {
    flex-wrap: wrap;
  }

  #screen-daily-checklist-form #dcf-bulk-add-panel .btn,
  #screen-daily-checklist-form .form-actions .btn {
    flex: 1 1 148px;
    min-width: 0;
  }

  #screen-daily-checklist-form .dcf-field-row {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) minmax(88px, .65fr) auto;
    align-items: end !important;
  }

  #screen-daily-checklist-form .dcf-field-row input,
  #screen-daily-checklist-form .dcf-field-row select {
    min-width: 0;
    width: 100%;
  }

  #screen-daily-checklist-run .dc-progress-wrap,
  #screen-daily-checklist-run .pdp-header,
  #screen-daily-checklist-run .dc-item-row,
  #screen-daily-checklist-run .dc-item-main {
    min-width: 0;
  }

  #screen-daily-checklist-run .dc-item-row {
    padding-inline: 12px;
  }

  #screen-daily-checklist-run .dc-item-main strong,
  #screen-daily-checklist-run .dc-item-main small,
  #screen-daily-checklist-run .dc-item-comment {
    overflow-wrap: anywhere;
  }

  /* ── Phone nav: real bottom tab bar, not the desktop rail ──
     style.css's sidebar-shell rules (body.app-shell-sidebar-active)
     apply on every screen size, not just desktop/tablet — below
     767px they just kept shrinking .app-sidebar into a narrower and
     narrower vertical rail instead of ever switching over to the
     dedicated #mobile-bottom-nav bar. That rail was also actively
     force-hiding #mobile-bottom-nav via `display:none !important`,
     so on phones the intended bottom tab bar (and its "More" sheet
     trigger) never rendered at all. This restores the correct
     phone layout: hide the rail entirely, show the real bottom bar,
     and stop reserving rail width in the content padding. */
  body.app-shell-sidebar-active .app-sidebar,
  body.app-shell-sidebar-active .app-sidebar__reopen {
    display: none !important;
  }

  body.app-shell-sidebar-active .bottom-nav:not(.app-sidebar__nav).visible {
    display: flex !important;
  }

  body.app-shell-sidebar-active .screen:not(#screen-login):not(#screen-splash) {
    padding: 0 !important;
    padding-bottom: calc(var(--nav-h, 60px) + env(safe-area-inset-bottom, 0px)) !important;
  }

  body.app-shell-sidebar-active .screen:not(#screen-login):not(#screen-splash) .app-header {
    display: flex !important;
  }

  body.app-shell-sidebar-active .detail-actions {
    left: 12px !important;
    right: 12px !important;
    bottom: calc(var(--nav-h, 60px) + 12px) !important;
  }

  /* Several unscoped `.bottom-nav` rules elsewhere in style.css were
     written only for the sidebar's collapsed rail (.app-sidebar__nav)
     but forgot that scope, so they also reshaped #mobile-bottom-nav
     into the same narrow vertical rail (top/left/width/flex-direction:
     column). #mobile-bottom-nav never has .app-sidebar__nav — pin its
     geometry back to a real horizontal bottom bar with an ID selector,
     which outranks those class-only rules no matter the source order. */
  #mobile-bottom-nav {
    top: auto !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    height: var(--nav-h) !important;
    flex-direction: row !important;
    align-items: stretch !important;
    justify-content: stretch !important;
    border-radius: 0 !important;
    padding: 0 !important;
    padding-bottom: env(safe-area-inset-bottom, 0) !important;
  }

  #mobile-bottom-nav .nav-tab {
    flex-direction: column !important;
  }
}

@media (max-width: 420px) {
  #screen-quotations #q-detail-actions,
  #screen-quotations .detail-actions,
  #screen-profile .profile-hero-chips {
    grid-template-columns: 1fr;
  }

  #screen-quotations .q-screen-header .header-right .btn-icon.secondary {
    display: none !important;
  }

  #screen-tools .tool-card {
    padding: 12px;
  }

  #screen-tools .tool-card-icon {
    width: 40px;
    height: 40px;
    flex-basis: 40px;
  }

  #screen-daily-checklist-form .dcf-field-row {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  #screen-daily-checklist-form .dcf-field-row input {
    grid-column: 1 / -1;
  }

  #screen-notification-settings .form-actions .btn {
    flex: 1 1 100%;
    width: 100%;
  }
}

/* ── Global Phone Guardrails ────────────────────────── */
@media (max-width: 768px) {
  /* Prevent iOS Safari auto-zoom on form controls */
  input:not([type='checkbox']):not([type='radio']):not([type='range']),
  select,
  textarea {
    font-size: 16px !important;
  }

  /* Keep tables inside mobile bounds with smooth touch scrolling */
  table {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Checklist inspection run sheet columns */
  #screen-daily-checklist-run .dc-inspection-sheet__head {
    display: none !important;
  }

  #screen-daily-checklist-run .dc-item-row {
    display: grid !important;
    grid-template-columns: 36px minmax(0, 1fr) 36px !important;
    align-items: start !important;
    gap: 8px !important;
    padding: 12px 10px !important;
  }

  #screen-daily-checklist-run .dc-item-status {
    display: inline-flex;
    margin-top: 6px;
    align-self: start;
  }

  /* Collection workspace card pager and list bottom clearance */
  :is(#screen-main, #screen-projects, #screen-all-tasks, #screen-tasks) .collection-panel,
  :is(#screen-main, #screen-projects, #screen-all-tasks, #screen-tasks) .page-workspace__results,
  :is(#screen-main, #screen-projects, #screen-all-tasks, #screen-tasks) .record-grid {
    padding-bottom: calc(var(--nav-h, 64px) + env(safe-area-inset-bottom, 0px) + 28px) !important;
  }
}

