/*
 * pwa_base.css
 * Mobile + PWA compatibility layer for Stepwize
 * Applied on ALL pages via application.html.erb and home_landing.html.erb
 * Desktop layouts are UNCHANGED — these rules only fix iOS/Android/iPad behaviour.
 */

/* ─── 0. Mobile CSS baseline reset (PWA mobile only) ───────────────────────── */
/*                                                                               */
/* Tailwind CDN is only loaded in development (application.html.erb line 51).   */
/* Its Preflight reset was silently normalising box-sizing and the browser's     */
/* default 8px body margin in local dev, but NOT in staging/production.          */
/* These two rules replicate only that subset, scoped strictly to mobile so      */
/* desktop layout is completely unaffected.                                      */
.pwa-root *,
.pwa-root *::before,
.pwa-root *::after {
  box-sizing: border-box; /* prevents width overflow across all screen sizes */
}

@media (max-width: 768px) {
  .pwa-root {
    margin: 0; /* removes browser default 8px body margin — makes header
                  getBoundingClientRect() return the same value in staging
                  as in local dev, so --mobile-header-offset matches */
  }
}

/* ─── 1. Prevent iOS font inflation ────────────────────────────────────────── */
.pwa-root {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* ─── 2. Prevent horizontal scroll on all devices ──────────────────────────── */
.pwa-root {
  overflow-x: hidden;
  max-width: 100%;
}

/* ─── 3. Prevent iOS auto-zoom on input focus (requires font-size >= 16px) ─── */
.pwa-root input:not(.search-input),
.pwa-root select:not(.search-input),
.pwa-root textarea:not(.search-input) {
  font-size: max(16px, 1em);
}

.pwa-root .search-input {
  font-size: max(16px, 0.95rem);
  box-sizing: border-box;
}

@media (max-width: 768px) {
  .pwa-root .search-input {
    width: 100%;
  }
}

/* ─── 4. Remove 300ms tap delay on interactive elements ────────────────────── */
.pwa-root a,
.pwa-root button,
.pwa-root [role="button"],
.pwa-root label,
.pwa-root input[type="submit"],
.pwa-root input[type="button"],
.pwa-root input[type="reset"],
.pwa-root select {
  touch-action: manipulation;
}

/* ─── 5. Smooth momentum scrolling on iOS for overflow containers ───────────── */
.pwa-root .dashboard-sidebar,
.pwa-root .employee-sidebar,
.pwa-root .operator-sidebar,
.pwa-root .modal-container,
.pwa-root .table-container,
.pwa-root [class*="scrollable"],
.pwa-root [class*="-scroll"] {
  -webkit-overflow-scrolling: touch;
}

/* ─── 6. Prevent overscroll rubber-band from breaking sticky headers ─────────── */
.pwa-root .dashboard-sidebar,
.pwa-root .employee-dashboard-container,
.pwa-root .operator-dashboard-container,
.pwa-root .forge-container {
  overscroll-behavior-y: contain;
}

/* ─── 7. Safe area insets (notch / home indicator on iPhone/iPad) ────────────── */
.pwa-root .dashboard-header,
.pwa-root .employee-header,
.pwa-root .operator-header,
.pwa-root .forge-header,
.pwa-root .main-header {
  padding-top: max(0px, env(safe-area-inset-top));
  padding-left: max(0px, env(safe-area-inset-left));
  padding-right: max(0px, env(safe-area-inset-right));
}

/* Ensure footer / bottom areas respect home-indicator on iPhone */
.pwa-root {
  padding-bottom: env(safe-area-inset-bottom);
}

/* ─── 7b. Bottom-nav clearance (phones only) ─────────────────────────────────── */
/* At ≤768px both dashboard chromes turn their sidebar into a fixed bottom nav,  */
/* 64px tall plus the home-indicator inset (§11). Floating widgets and page      */
/* content clear it with literal values:                                         */
/*   calc(64px + env(safe-area-inset-bottom, 0px) + <gap>)                       */
/* Not a custom property: declaring one inside a media query (even a            */
/* non-matching one) measurably shifted native <select> text on desktop Chrome. */

/* ─── 8. Minimum tap target size on mobile (WCAG 2.5.5) ─────────────────────── */
@media (max-width: 1024px) {
  .pwa-root button,
  .pwa-root [role="button"],
  .pwa-root .nav-cta,
  .pwa-root [class*="-btn"]:not([class*="fm-mobile"]),
  .pwa-root [class*="btn-"],
  .pwa-root input[type="submit"],
  .pwa-root input[type="button"],
  .pwa-root input[type="reset"] {
    min-height: 44px;
    min-width: 44px;
  }
}

/* ─── 9. Flash messages: stay within viewport on small screens ───────────────── */
@media (max-width: 640px) {
  .pwa-root .flash {
    top: calc(12px + env(safe-area-inset-top, 0px)) !important;
    max-width: calc(100vw - 24px) !important;
    width: calc(100vw - 24px) !important;
    font-size: 0.875rem;
  }
}

/* ─── 10. Full-width page layout guard (prevents cards/forms from overflowing) ── */
@media (max-width: 768px) {
  .pwa-root .container,
  .pwa-root .card,
  .pwa-root [class*="-container"]:not([class*="search-container"]):not([class*="filter-container"]):not([class*="search-bar"]),
  .pwa-root [class*="-form"],
  .pwa-root [class*="-card"] {
    max-width: 100% !important;
    box-sizing: border-box;
  }

  /* Tables: always scrollable horizontally on mobile */
  .pwa-root table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    width: 100%;
  }

  /* Images: always responsive */
  .pwa-root img {
    max-width: 100%;
    height: auto;
  }
}

@media (max-width: 768px) {
  #globalConfirmModalCancel {
    width: 100%;
  }
}

@media (max-width: 768px) {
  #guidesPageHeader {
    grid-template-columns: 1fr !important;
  }

  #guidesPageHeader .header-right {
    grid-column: 1 / -1 !important;
    grid-row: 3 !important;
    flex-direction: column-reverse !important;
    align-items: stretch !important;
    width: 100% !important;
  }

  #guidesPageHeader .header-right button {
    width: 100% !important;
    box-sizing: border-box !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    padding: 0.75rem 1.5rem !important;
    font-size: 0.875rem !important;
    margin: 0 !important;
  }
}

/* ─── 11. Phone bottom nav: primary items + "More" sheet ────────────────────── */
/* Opt-in via <aside data-mobile-nav> (shared/_capability_sidebar,                */
/* employee_dashboard/_sidebar, expert_dashboard/_sidebar). Sales, operator and   */
/* demo sidebars are not opted in and keep their current bottom nav. Above 768px  */
/* the only effect is hiding the More item, so tablet and desktop are unchanged.  */
/* The sheet reuses the sidebar's own <li> nodes; mobile_nav_controller.js only   */
/* toggles [data-mobile-nav-open].                                                */
.pwa-root [data-mobile-nav] [data-mobile-nav-more] {
  display: none;
}

@media (max-width: 768px) {
  /* Home-indicator inset lives on the <nav>, not the <aside>, so the open-sheet */
  /* backdrop (painted under the <nav>) never dims the bar itself.               */
  .pwa-root [data-mobile-nav] > nav {
    position: relative;
    background: #ffffff;
    padding-bottom: env(safe-area-inset-bottom, 0px) !important;
  }

  .pwa-root [data-mobile-nav] .nav-list {
    flex: 1 1 auto !important; /* .sidebar-nav is itself a flex container */
    width: 100% !important;
    justify-content: space-around !important;
    gap: 0 !important;
    padding: 4px !important;
    overflow-x: hidden !important;
  }

  /* Non-zero basis: a zero-basis item would fit, at zero width, on the same */
  /* line as the open sheet's full-width separator below.                    */
  .pwa-root [data-mobile-nav] .nav-item {
    flex: 1 1 18% !important;
    margin: 0 !important;
    min-width: 0 !important;
    max-width: none !important;
  }

  .pwa-root [data-mobile-nav]:not([data-mobile-nav-open]) .nav-item:not([data-mobile-primary]):not([data-mobile-nav-more]) {
    display: none !important;
  }

  .pwa-root [data-mobile-nav] [data-mobile-nav-more] {
    display: block !important;
  }

  .pwa-root [data-mobile-nav] .nav-link,
  .pwa-root [data-mobile-nav] .mobile-nav-more-btn {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 2px !important;
    width: 100% !important;
    min-width: 0 !important;
    min-height: 56px !important;
    padding: 6px 2px !important;
    border-radius: 8px !important;
    white-space: normal !important;
  }

  /* The Validation count sits on the icon's corner, as the bell's does, so it */
  /* never adds a third row to the item.                                       */
  .pwa-root [data-mobile-nav] .nav-link {
    position: relative;
  }

  .pwa-root [data-mobile-nav] .nav-count-badge {
    position: absolute;
    top: 2px;
    left: calc(50% + 4px);
    margin-left: 0;
  }

  .pwa-root [data-mobile-nav] .mobile-nav-more-btn {
    margin: 0;
    border: 0;
    background: none;
    color: #6b7280;
    font: inherit;
    cursor: pointer;
  }

  .pwa-root [data-mobile-nav] .mobile-nav-more-btn:focus-visible {
    outline: 3px solid rgba(0, 0, 139, 0.3);
    outline-offset: -3px;
  }

  /* Labels stay visible at every phone width (the chromes hide them at ≤480px, */
  /* which left icon-only links with no accessible name). Two lines, no         */
  /* ellipsis, so longer French labels still read in full.                       */
  .pwa-root [data-mobile-nav] .nav-text,
  .pwa-root [data-mobile-nav] .mobile-nav-more-text {
    display: -webkit-box !important;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    overflow: hidden !important;
    max-width: 100% !important;
    white-space: normal !important;
    text-overflow: clip !important;
    overflow-wrap: break-word;
    font-size: 0.6875rem !important;
    line-height: 1.2 !important;
    text-align: center !important;
  }

  /* The More button reads as active when the current page lives in the sheet. */
  .pwa-root [data-mobile-nav]:not([data-mobile-nav-open]):has(.nav-item:not([data-mobile-primary]) .nav-link.active) .mobile-nav-more-btn {
    color: #00008b;
    background: #eef2ff;
  }

  /* Open sheet: secondary items move above the primary row, three per row.     */
  /* The ::after is a full-width flex item that forces the primary row onto its */
  /* own line whatever the number of secondary items.                            */
  .pwa-root [data-mobile-nav][data-mobile-nav-open] .nav-list {
    flex-wrap: wrap !important;
    max-height: 70vh;
    max-height: 70dvh;
    overflow-y: auto !important;
  }

  .pwa-root [data-mobile-nav][data-mobile-nav-open] .nav-item:not([data-mobile-primary]):not([data-mobile-nav-more]) {
    order: -1;
    flex: 0 0 33.3333% !important;
  }

  .pwa-root [data-mobile-nav][data-mobile-nav-open] .nav-list::after {
    content: "";
    order: -1;
    flex: 0 0 100%;
    height: 1px;
    margin: 4px 0;
    background: #e5e7eb;
  }

  .pwa-root [data-mobile-nav][data-mobile-nav-open]::before {
    content: "";
    position: fixed;
    inset: 0;
    z-index: -1;
    background: rgba(15, 23, 42, 0.45);
  }

  .pwa-root:has([data-mobile-nav-open]) .need-help-button,
  .pwa-root:has([data-mobile-nav-open]) .guidance-floating-pill,
  .pwa-root:has([data-mobile-nav-open]) .translation-helper-wrapper {
    visibility: hidden;
  }

  /* Content clears the bar (64px, §7b). */
  .pwa-root:has([data-mobile-nav]) .dashboard-main,
  .pwa-root:has([data-mobile-nav]) .employee-dashboard-main {
    padding-bottom: 80px !important;
  }
}

/* ─── 12. "Ask AI" link in guide viewers (shared/_guide_ask_ai_link) ──────── */
/* Phones only, so desktop viewers render exactly as before.                   */
.pwa-root .guide-ask-ai-link {
  display: none !important;
}

@media (max-width: 768px) {
  .pwa-root .guide-ask-ai-link {
    display: inline-flex !important;
    align-items: center;
    gap: 0.5rem;
    min-height: 44px;
    margin: 0;
    padding: 0.5rem 1rem;
    background: #ffffff;
    border: 2px solid #e5e7eb;
    border-radius: 10px;
    color: #00008b;
    font-size: 0.875rem;
    font-weight: 600;
    text-decoration: none;
  }
}
