/* ═══════════════════════════════════════════════
   31-MOBILE.CSS — App-Like Mobile Experience
   Comprehensive UX enhancements for mobile devices
   ═══════════════════════════════════════════════ */

/* ── BOTTOM NAVIGATION BAR (Mobile Only) ── */
.mob-bnav {
  display: none;
}

@media (max-width: 640px) {

  /* ── Bottom Nav: Container ── */
  .mob-bnav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 56px;
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--s1);
    border-top: 1px solid var(--b2);
    z-index: 195;
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    box-shadow: 0 -1px 0 var(--b1), 0 -6px 20px rgba(0,0,0,.08);
    align-items: stretch;
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  [data-theme=dark] .mob-bnav {
    box-shadow: 0 -1px 0 var(--b1), 0 -6px 24px rgba(0,0,0,.35);
  }

  /* ── Bottom Nav: Individual Items ── */
  .mob-bnav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    cursor: pointer;
    color: var(--t3);
    font-size: 9.5px;
    font-weight: 600;
    letter-spacing: 0.02em;
    transition: color .15s, background .1s;
    padding: 5px 2px 7px;
    position: relative;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    touch-action: manipulation;
    min-height: 56px;
  }

  .mob-bnav-item:active {
    background: rgba(200,149,90,.07);
  }

  .mob-bnav-item.active {
    color: var(--np);
  }

  .mob-bnav-item svg {
    width: 22px;
    height: 22px;
    flex-shrink: 0;
    stroke-width: 1.8;
    transition: transform .22s cubic-bezier(.34,1.56,.64,1), color .15s;
  }

  .mob-bnav-item.active svg {
    transform: translateY(-1px) scale(1.1);
  }

  /* Active indicator bar at top of item */
  .mob-bnav-item::after {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%) scaleX(0);
    width: 28px;
    height: 3px;
    border-radius: 0 0 4px 4px;
    background: var(--np);
    box-shadow: 0 2px 6px rgba(200,149,90,.5);
    transition: transform .22s cubic-bezier(.34,1.56,.64,1);
  }

  .mob-bnav-item.active::after {
    transform: translateX(-50%) scaleX(1);
  }

  /* Badge for unread/pending counts */
  .mob-bnav-badge {
    position: absolute;
    top: 4px;
    right: calc(50% - 20px);
    background: #d94040;
    color: #fff;
    border-radius: 100px;
    font-size: 9px;
    font-weight: 800;
    padding: 1px 5px;
    min-width: 16px;
    text-align: center;
    border: 2px solid var(--s1);
    line-height: 1.3;
    display: none;
  }

  .mob-bnav-badge.show {
    display: block;
  }

  /* ── Content padding to clear bottom nav ── */
  #app.app-visible {
    padding-bottom: calc(56px + env(safe-area-inset-bottom));
  }

  /* ── Hide redundant hamburger (bottom nav "Mehr" handles it) ── */
  .mob-menu-btn {
    display: none !important;
  }

  /* ── TOUCH TARGETS: Minimum 44px for all interactive elements ── */
  .mclose {
    width: 44px !important;
    height: 44px !important;
    border-radius: 12px !important;
  }

  .icon-btn {
    width: 44px !important;
    height: 44px !important;
    border-radius: 12px !important;
  }

  .mob-drawer-close {
    width: 44px !important;
    height: 44px !important;
    border-radius: 12px !important;
  }

  /* ── MODALS: Full bottom sheet on all mobile sizes ── */
  /* Overrides the 10px-padding centered modal from 05-components.css */
  .mbg {
    padding: 0 !important;
    align-items: flex-end !important;
  }

  .modal {
    border-radius: 24px 24px 0 0 !important;
    max-height: 90vh !important;
    max-width: 100% !important;
    margin: 0 !important;
  }

  .modal-sm {
    border-radius: 24px 24px 0 0 !important;
    max-width: 100% !important;
  }

  /* Drag handle indicator at top of bottom sheet */
  .mhd::before {
    content: '';
    display: block;
    position: absolute;
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: var(--b3);
  }

  .mhd {
    position: relative;
    padding-top: 22px !important;
  }

  /* ── SETTINGS SIDEBAR: better touch targets on mobile ── */
  .set-nav-item {
    min-height: 44px !important;
    padding: 10px 14px !important;
    flex-shrink: 0;
    min-width: 108px;
  }

  /* ── SAVE BAR: raise above safe area ── */
  .set-save-bar {
    padding-bottom: max(13px, calc(13px + env(safe-area-inset-bottom))) !important;
  }

  /* ── KPI GRID: always 2 columns on phone ── */
  .kgrid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* ── CARDS: slightly tighter on mobile ── */
  .cb {
    padding: 12px !important;
  }

  .ch {
    padding: 10px 12px !important;
    gap: 6px !important;
  }

  /* ── EMPTY STATES: reduced top padding ── */
  .empty {
    padding: 36px 16px;
  }

  /* ── STICKY: account for top nav height ── */
  .sticky {
    top: 52px !important;
  }

  /* ── PAGE ANIMATION: slightly snappier ── */
  @keyframes pi {
    from { opacity:0; transform:translateY(4px) }
    to   { opacity:1; transform:none }
  }

  /* ── TABLES: ensure horizontal scroll always works ── */
  .tscroll {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }

  /* ── FAB (Floating Action Buttons): safe area aware ── */
  [class*="fab"], .pshop-cart-fab {
    bottom: calc(72px + env(safe-area-inset-bottom)) !important;
  }
}

/* ── TABLET: Moderate improvements (641-960px) ── */
@media (min-width: 641px) and (max-width: 960px) {
  .page {
    padding: 18px 20px 32px;
  }

  .g3 {
    grid-template-columns: 1fr 1fr;
  }

  .modal {
    max-width: 540px;
  }
}

/* ── LANDSCAPE PHONE: adjust bottom nav height ── */
@media (max-width: 640px) and (orientation: landscape) {
  .mob-bnav {
    height: 48px;
  }

  .mob-bnav-item {
    min-height: 48px;
    font-size: 9px;
  }

  .mob-bnav-item svg {
    width: 18px;
    height: 18px;
  }

  #app.app-visible {
    padding-bottom: calc(48px + env(safe-area-inset-bottom));
  }
}

/* ── PWA: Standalone mode adjustments ── */
@media (display-mode: standalone) {
  /* In PWA mode, ensure status bar area is respected */
  .nav {
    padding-top: env(safe-area-inset-top);
  }
}

/* ════════════════════════════════════════════════════════════════
   Safety-net mobile rules — surgical fixes for overflow risks
   in inline-styled JS templates and legacy components.
   Scoped to ≤640px so desktop layout is untouched.
   ════════════════════════════════════════════════════════════════ */

@media (max-width: 640px){

  /* No horizontal scroll at the page level */
  html, body { overflow-x: hidden; max-width: 100vw; }

  /* Prevent iOS form-zoom: inputs must be ≥16px */
  input, select, textarea, button { font-size: 16px; }
  .btn-sm, .ntab, .pill, .tag, .chip { font-size: 13px; }

  /* Tap-target minimum: any small icon-only button should still be ≥38px */
  .btn-ico, .icon-btn, [role="button"].btn-ico { min-width: 38px; min-height: 38px; }

  /* Cookie banner stacks on small phones */
  #cookie-banner > div,
  .cookie-banner-actions { flex-wrap: wrap; gap: 8px; }
  #cookie-banner button { flex: 1 1 100%; }

  /* Certificate / list edit tiny buttons (e.g. profile certs) */
  button[style*="padding:3px 7px"],
  button[style*="padding: 3px 7px"] {
    padding: 8px 12px !important;
    min-height: 36px;
  }

  /* Generic 2-column grid → single column when child elements would crush */
  [style*="grid-template-columns:1fr 1fr"]:not(.no-mb-stack),
  [style*="grid-template-columns: 1fr 1fr"]:not(.no-mb-stack),
  [style*="grid-template-columns:repeat(2,1fr)"]:not(.no-mb-stack),
  [style*="grid-template-columns: repeat(2, 1fr)"]:not(.no-mb-stack) {
    grid-template-columns: 1fr !important;
  }

  /* Onboarding location step: 120px label + 1fr → stack */
  [style*="grid-template-columns:120px 1fr"],
  [style*="grid-template-columns: 120px 1fr"] {
    grid-template-columns: 1fr !important;
  }

  /* Reports rows: 78px ico + 1fr + auto stays in row but allows wrap */
  [style*="grid-template-columns:78px 1fr auto"],
  [style*="grid-template-columns: 78px 1fr auto"] {
    grid-template-columns: 56px 1fr auto !important;
    gap: 6px !important;
  }

  /* Long tables that have not been converted to mb-* cards: enable safe scroll
     by wrapping their parent. We ensure the table itself does not cause page
     overflow even if it scrolls inside its container. */
  table { max-width: 100%; }
  .tscroll, .table-wrap, [data-table-scroll] {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Modals must always fit viewport height with internal scroll */
  .modal, .dlg-box, .sw-dg-modal, .cf-m, .mb-modal {
    max-height: calc(100vh - 32px) !important;
    overflow-y: auto;
  }

  /* Form builder preview modal (inline 860px max) */
  .dlg-box[style*="max-width:860px"],
  .dlg-box[style*="max-width: 860px"] {
    max-width: calc(100vw - 16px) !important;
  }

  /* Marketplace search 3-col grid → stack */
  .mkp-search { grid-template-columns: 1fr !important; }

  /* Action bars wrap cleanly */
  .actions, .btn-row, [data-action-row] { flex-wrap: wrap; gap: 8px; }
}

@media (max-width: 480px){
  /* Tighter spacing on small phones */
  .nav { padding: 0 10px; }
  .page, .mb-page, .content { padding: 10px 12px; }

  /* Calendar legacy week head — already collapses, but ensure no overflow */
  .cal2-week-head, .cal2-week-body { overflow-x: hidden; }

  /* Toasts narrower */
  .toast, .mb-toast { max-width: calc(100vw - 24px); }
}
