/* ─────────────────────────────────────────────────────────────
   Babykare ERP — Mobile patches
   Additive breakpoints for <sm (< 640px) and <md (< 768px).
   Never overrides desktop behaviour.

   Sections:
     1. Layout — main padding for fixed bottom nav + safe areas
     2. Topbar — denser filter header on mobile
     3. Combobox triggers — 40px min-height for touch
     4. Tables — scrollable with min-width inside narrow cards
     5. Sidebar drawer — off-canvas overlay (desktop unaffected)
     6. PWA install button — bottom-fixed, mobile only
     7. Notifications drawer — slide-in panel from right
     8. Generic touch-target helpers
   ───────────────────────────────────────────────────────────── */

/* ── 1. Layout safe-area + bottom nav clearance ───────────── */
@media (max-width: 639px) {
  /* Reserve space for the fixed mobile nav (58px) and the home indicator. */
  main#main-content {
    padding-left: var(--space-3) !important;
    padding-right: var(--space-3) !important;
    padding-bottom: calc(80px + env(safe-area-inset-bottom, 0px)) !important;
  }

  /* Mobile nav respects the iPhone home indicator. */
  nav[aria-label="Navigation mobile"] {
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
}

/* ── 2. Topbar — denser filter header on mobile ──────────── */
@media (max-width: 767px) {
  /* The dashboard v2 filter header (header[role="banner"] inside
     content_for :custom_topbar). Mobile layout:
       Row 1 — hamburger | logo | title (mr-auto) | bell  → action row
       Row 2 — three comboboxes (horizontally scrollable) | CTA
     We keep the title visible and skip the period subline so the topbar
     stays around 110-120px instead of 200+. */
  .filters-topbar {
    padding: var(--space-2) var(--space-3) !important;
  }

  /* Hide the verbose period subline + freshness indicator on mobile —
     room is the bottleneck. The title still says what's selected via
     its •Analytics suffix and the user can re-open the period combobox
     to see the dates. The freshness <p> has an inline `display: flex`
     so we have to !important the override. */
  .filters-topbar__title p { display: none !important; }
  .filters-topbar__title h1 {
    font-size: 13px;
    line-height: 1.2;
  }

  /* The form + CTA wrap together onto a second row that scrolls
     horizontally as one unit. */
  .filters-topbar > form {
    flex-basis: 100%;
    flex-wrap: nowrap !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 4px;
    min-width: 0;
  }
  .filters-topbar > form::-webkit-scrollbar { display: none; }
  .filters-topbar > form > * { flex-shrink: 0; }

  /* CTA sits on the same horizontal-scroll row visually by wrapping
     to be the last item of row 2 when there's no space. Easier: keep
     it on its own row but compact. */
  .filters-topbar > a.btn-primary {
    margin-left: auto;
  }
}

/* ── 3. Combobox triggers — 40px min-height for touch ─────── */
@media (max-width: 767px) {
  .combobox-trigger,
  .btn-primary,
  .btn-secondary,
  .btn-danger {
    min-height: 40px;
  }

  /* Combobox menus get a wider min-width and stay anchored under their
     trigger when the topbar row scrolls horizontally. */
  .combobox-menu {
    min-width: 240px;
    max-width: calc(100vw - 32px);
    max-height: 60vh;
  }
}

/* ── 4. Tables — scrollable with min-width inside narrow cards ─ */
@media (max-width: 767px) {
  .card__body table,
  .card__body .erp-table,
  .overflow-x-auto > table {
    min-width: 480px;
  }

  /* Wrap tall geo cards so they don't stretch the layout. */
  .card__body { padding: var(--space-3) !important; }
  .card__header,
  .card-header { padding: var(--space-2) var(--space-3) !important; }
}

/* ── 5. Sidebar drawer — off-canvas overlay for <sm ─────── */
/* When the body has data-mobile-drawer-open="true", the sidebar slides in
   from the left and a backdrop dims the rest. Stimulus controller
   `mobile_drawer` toggles the body attribute. */
@media (max-width: 639px) {
  /* Force-show the desktop sidebar as an off-canvas drawer on mobile.
     The desktop selectors use `hidden sm:flex` so we override with
     position+transform — no JS class additions to the aside itself. */
  aside[aria-label="Navigation principale"] {
    /* Desktop sets `hidden sm:flex sm:flex-col` — at <sm none of the
       sm:* utilities apply, so we have to force display + direction
       ourselves. */
    display: flex !important;
    flex-direction: column !important;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    z-index: 60;
    transform: translateX(-100%);
    transition: transform var(--duration-normal) var(--ease-out-expo);
    box-shadow: var(--shadow-lg);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    /* Hide internal flex children that overflow horizontally while the
       drawer is closed (the user profile <p> uses `min-width: 0` +
       `truncate` but its bounding box still extends past the aside's
       right edge by ~60px, which would peek through at x=0 because the
       aside has its right edge ON-SCREEN at x=0 when translateX(-100%)). */
    overflow: hidden;
  }

  body[data-mobile-drawer-open="true"] aside[aria-label="Navigation principale"] {
    transform: translateX(0);
  }

  /* Backdrop — appended by the Stimulus controller, faded in via attribute. */
  .mobile-drawer-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(26, 18, 8, 0.45);
    z-index: 55;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--duration-normal) var(--ease-default);
  }
  body[data-mobile-drawer-open="true"] .mobile-drawer-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  /* Lock body scroll while the drawer is open. */
  body[data-mobile-drawer-open="true"] {
    overflow: hidden;
  }
}

/* Hamburger button — visible only on mobile. */
.mobile-drawer-trigger {
  display: none;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-primary);
  cursor: pointer;
  flex-shrink: 0;
  transition: background var(--duration-fast) var(--ease-default);
}
.mobile-drawer-trigger:hover { background: var(--color-bg); }
.mobile-drawer-trigger:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}
@media (max-width: 639px) {
  .mobile-drawer-trigger { display: inline-flex; }
}

/* ── 6. PWA install button — bottom-fixed, mobile only ────── */
.pwa-install-btn {
  display: none;
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  /* Sits ABOVE the bottom mobile nav (58px) + safe area. */
  bottom: calc(72px + env(safe-area-inset-bottom, 0px));
  z-index: 45;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-4);
  background: var(--color-accent);
  color: white;
  font-size: 13px;
  font-weight: 700;
  border: 0;
  border-radius: 999px;
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-default),
              transform var(--duration-fast) var(--ease-default);
  animation: pwa-install-rise 0.3s var(--ease-out-expo);
}
.pwa-install-btn:hover { background: var(--color-accent-hover); }
.pwa-install-btn:active { transform: translateX(-50%) translateY(1px); }
.pwa-install-btn[hidden] { display: none !important; }
.pwa-install-btn[data-visible="true"] { display: inline-flex; }

/* Desktop never shows it. */
@media (min-width: 1024px) {
  .pwa-install-btn[data-visible="true"] { display: none; }
}

/* iOS hint tooltip — replaces the install button on iOS where the
   beforeinstallprompt event never fires. */
.pwa-ios-hint {
  display: none;
  position: fixed;
  left: var(--space-3);
  right: var(--space-3);
  bottom: calc(72px + env(safe-area-inset-bottom, 0px));
  z-index: 45;
  padding: var(--space-3);
  background: var(--color-text-primary);
  color: white;
  font-size: 13px;
  line-height: 1.4;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  animation: pwa-install-rise 0.3s var(--ease-out-expo);
}
.pwa-ios-hint[data-visible="true"] { display: block; }
.pwa-ios-hint__close {
  background: transparent;
  border: 0;
  color: rgba(255,255,255,0.75);
  font-weight: 700;
  cursor: pointer;
  padding: 0 4px;
  margin-left: 8px;
  font-size: 16px;
  line-height: 1;
}

@keyframes pwa-install-rise {
  from { opacity: 0; transform: translateX(-50%) translateY(12px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .pwa-install-btn,
  .pwa-ios-hint { animation: none; }
}

/* ── 7. Notifications drawer — slide-in panel from right ─── */
.notifications-drawer-trigger {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: var(--radius-md);
  background: transparent;
  border: 1px solid transparent;
  color: var(--color-text-secondary);
  cursor: pointer;
  transition: background var(--duration-fast) var(--ease-default);
}
.notifications-drawer-trigger:hover {
  background: var(--color-accent-soft);
  color: var(--color-accent);
}
.notifications-drawer-trigger:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}
.notifications-drawer-trigger__dot {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--color-accent);
  border: 2px solid var(--color-surface);
}

.notifications-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(380px, 90vw);
  z-index: 70;
  background: var(--color-surface);
  border-left: 1px solid var(--color-border);
  box-shadow: var(--shadow-lg);
  transform: translateX(100%);
  transition: transform var(--duration-normal) var(--ease-out-expo);
  display: flex;
  flex-direction: column;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
body[data-notifications-drawer-open="true"] .notifications-drawer {
  transform: translateX(0);
}
.notifications-drawer__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}
.notifications-drawer__title {
  font-size: 15px;
  font-weight: 700;
  color: var(--color-text-primary);
  margin: 0;
}
.notifications-drawer__close {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 0;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--color-text-muted);
}
.notifications-drawer__close:hover {
  background: var(--color-bg);
  color: var(--color-text-primary);
}
.notifications-drawer__body {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-4);
}
.notifications-drawer__empty {
  text-align: center;
  padding: var(--space-7) var(--space-4);
  color: var(--color-text-muted);
  font-size: 13px;
}
.notifications-drawer__empty-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--color-bg);
  margin: 0 auto var(--space-3);
}

.notifications-drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(26, 18, 8, 0.45);
  z-index: 65;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-normal) var(--ease-default);
}
body[data-notifications-drawer-open="true"] .notifications-drawer-backdrop {
  opacity: 1;
  pointer-events: auto;
}

@media (max-width: 639px) {
  body[data-notifications-drawer-open="true"] {
    overflow: hidden;
  }
}

/* ── 8. Touch-target helper ──────────────────────────────── */
.touch-target {
  min-width: 44px;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
