/* =========================================================
   BG MOBILE CARD GRIDS — Phase 2
   Mobile-only adaptations for the major card grids.
   Desktop styles untouched.

   Patterns:
   - h-scroll rail: short/medium sets that read as parallel options
   - cap + load-more: long lists, vertical reading
   - compact card: smaller covers, shorter dek, tighter padding
========================================================= */

/* ============================================================
   Mobile horizontal-overflow safety
   Phase 2.2: prevent any data-bgmc-rail bleed from creating
   page-level horizontal scrolling on pages whose container
   padding does not match --bgmc-edge (default 20px).
   `overflow-x: clip` is preferred (does not block position:sticky/fixed)
   and falls back to `hidden` on older browsers.
   ============================================================ */
@media (max-width: 767px) {
  html, body { overflow-x: hidden; overflow-x: clip; }
}

/* ============================================================
   Shared "Show all (N)" button — appended by mobile-cards.js
   ============================================================ */
.bgmc-more {
  display: none; /* shown only on mobile, only when grid is capped */
}

@media (max-width: 767px) {
  .bgmc-more {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    margin-top: 16px;
    min-height: 48px;
    padding: 0 18px;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(79, 200, 255, 0.22);
    border-radius: 4px;
    color: #ECF0FA;
    font-family: var(--font-mono, "JetBrains Mono", monospace);
    font-size: 11px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    font-weight: 500;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: background 180ms ease, border-color 180ms ease, color 180ms ease;
  }
  .bgmc-more:hover,
  .bgmc-more:focus-visible {
    background: rgba(79, 200, 255, 0.08);
    border-color: rgba(79, 200, 255, 0.45);
    color: #fff;
    outline: none;
  }
  .bgmc-more:focus-visible {
    outline: 2px solid var(--site-cyan, #4FC8FF);
    outline-offset: 2px;
  }
  /* Phase 2.2: sticky-CTA safety — when the bottom CTA bar is mounted,
     give the "Show all" button extra room so it never sits flush against
     the bar even mid-scroll (body already has 76px+safe-area padding-bottom,
     this is belt-and-braces and also ensures the LAST visible card in a
     capped grid is comfortably above the bar after expand/collapse). */
  body.bgsc-has .bgmc-more {
    margin-bottom: calc(env(safe-area-inset-bottom, 0px) + 12px);
  }
  body.bgsc-has [data-bgmc-cap][data-bgmc-capped="expanded"] {
    scroll-margin-bottom: calc(76px + env(safe-area-inset-bottom, 0px));
  }
  .bgmc-more .bgmc-count {
    color: var(--site-cyan, #4FC8FF);
    font-weight: 600;
  }
  .bgmc-more .bgmc-arrow {
    width: 14px; height: 14px;
    transition: transform 220ms ease;
  }
  .bgmc-more[aria-expanded="true"] .bgmc-arrow {
    transform: rotate(180deg);
  }
  .bgmc-more[aria-expanded="true"] .bgmc-count {
    display: none;
  }
}

/* ============================================================
   Shared h-scroll rail behavior
   Applied via class .bgmc-rail (added by JS on mobile only)
   ============================================================ */
@media (max-width: 767px) {
  .bgmc-rail {
    display: flex !important;
    flex-direction: row !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    grid-template-columns: none !important;
    grid-template-rows: none !important;
    gap: 12px !important;
    /* Negative margins + matching padding lets cards bleed to screen edges
       while keeping the section's left padding intact */
    margin-left: calc(-1 * var(--bgmc-edge, 20px)) !important;
    margin-right: calc(-1 * var(--bgmc-edge, 20px)) !important;
    padding: 4px var(--bgmc-edge, 20px) 16px !important;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    /* Subtle edge fade hints at more content */
    mask-image: linear-gradient(90deg,
      transparent 0,
      #000 calc(var(--bgmc-edge, 20px) - 4px),
      #000 calc(100% - 28px),
      transparent 100%);
    -webkit-mask-image: linear-gradient(90deg,
      transparent 0,
      #000 calc(var(--bgmc-edge, 20px) - 4px),
      #000 calc(100% - 28px),
      transparent 100%);
  }
  .bgmc-rail::-webkit-scrollbar { display: none; }
  .bgmc-rail > * {
    flex: 0 0 86% !important;
    width: 86% !important;
    max-width: 86% !important;
    min-width: 0 !important;
    scroll-snap-align: start;
    /* Defeat any grid-span the desktop layout assigned */
    grid-column: auto !important;
    grid-row: auto !important;
  }
}

@media (max-width: 380px) {
  .bgmc-rail > * {
    flex-basis: 88% !important;
    width: 88% !important;
    max-width: 88% !important;
  }
}

/* ============================================================
   1. RESOURCES — .res-grid
   Pattern: cap to 6 + "Show all" (long library, vertical reading)
   ============================================================ */
@media (max-width: 767px) {
  /* Collapse to single column on mobile, defeat any size-modifier spans */
  .res-grid {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }
  .res-grid > .res-card,
  .res-grid > .res-card.large,
  .res-grid > .res-card.tall,
  .res-grid > .res-card.wide,
  .res-grid > .res-card.small {
    grid-column: auto !important;
    grid-row: auto !important;
    min-height: 0 !important;
  }

  /* Compact card: shorter cover, smaller type, hide id chip */
  .res-card-cover {
    min-height: 120px !important;
    aspect-ratio: 16 / 8 !important;
  }
  .res-card-id { display: none; } /* monogram ID is decorative */
  .res-card-ttl {
    font-size: 17px !important;
    line-height: 1.2 !important;
    -webkit-line-clamp: 2;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .res-card-dek {
    font-size: 13px !important;
    line-height: 1.45 !important;
    -webkit-line-clamp: 2; /* compact excerpt */
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .res-card-body { padding: 14px 16px !important; gap: 6px !important; }
  .res-card-foot { font-size: 11px !important; }
  .res-card-meta { font-size: 11px !important; }

  /* Cap + load more — hide everything past 6th card by default */
  .res-grid[data-bgmc-capped] > .res-card:nth-child(n+7) {
    display: none;
  }
  .res-grid[data-bgmc-capped="expanded"] > .res-card:nth-child(n+7) {
    display: flex;
  }

  /* Filter tabs → h-scroll chip row */
  .res-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap !important;
    padding-bottom: 4px;
    mask-image: linear-gradient(90deg, #000 0, #000 calc(100% - 24px), transparent 100%);
    -webkit-mask-image: linear-gradient(90deg, #000 0, #000 calc(100% - 24px), transparent 100%);
  }
  .res-tabs::-webkit-scrollbar { display: none; }
  .res-tab {
    flex: 0 0 auto !important;
    white-space: nowrap;
    min-height: 36px !important;
  }
  .res-lib-bar {
    flex-direction: column !important;
    gap: 12px !important;
    align-items: stretch !important;
  }
  .res-search { width: 100% !important; }

  /* Featured cover: stack vertically with compact cover */
  .res-feat-grid {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }
  .res-feat-cover {
    aspect-ratio: 16 / 9 !important;
    min-height: 0 !important;
  }
}

/* ============================================================
   2. AI SOLUTIONS — h-scroll rails on mobile
   ============================================================ */
@media (max-width: 767px) {
  /* Set --bgmc-edge per page (Resources uses 20px container padding;
     AI Solutions uses ~24px). Tunable per page. */
  .aisol-prod-grid,
  .aisol-time-grid,
  .aisol-ind-grid {
    --bgmc-edge: 20px;
  }

  /* aisol-prod-grid uses css grid with .span modifier (large cards span 2).
     Force flex rail. Compact cards within. */
  .aisol-prod-grid {
    display: flex !important;
    flex-direction: row !important;
    grid-template-columns: none !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    gap: 12px !important;
    padding: 4px 20px 18px !important;
    margin-left: -20px !important;
    margin-right: -20px !important;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    mask-image: linear-gradient(90deg, transparent 0, #000 16px, #000 calc(100% - 28px), transparent 100%);
    -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 16px, #000 calc(100% - 28px), transparent 100%);
  }
  .aisol-prod-grid::-webkit-scrollbar { display: none; }
  .aisol-prod-grid > .aisol-prod-card {
    flex: 0 0 86% !important;
    width: 86% !important;
    max-width: 86% !important;
    min-width: 0 !important;
    scroll-snap-align: start;
    grid-column: auto !important;
    grid-row: auto !important;
  }

  /* Stack cards (3 trust cards) — stack vertical, tighter padding */
  .aisol-stack-card {
    padding: 18px 20px !important;
  }
  .aisol-stack-card .num { font-size: 24px !important; }
  .aisol-stack-card h4 { font-size: 15px !important; }
  .aisol-stack-card p { font-size: 13px !important; line-height: 1.5 !important; }

  /* Timeline grid → h-scroll */
  .aisol-time-grid {
    display: flex !important;
    flex-direction: row !important;
    grid-template-columns: none !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    gap: 12px !important;
    padding: 4px 20px 18px !important;
    margin-left: -20px !important;
    margin-right: -20px !important;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    mask-image: linear-gradient(90deg, transparent 0, #000 16px, #000 calc(100% - 28px), transparent 100%);
    -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 16px, #000 calc(100% - 28px), transparent 100%);
  }
  .aisol-time-grid::-webkit-scrollbar { display: none; }
  .aisol-time-grid > .aisol-time-card {
    flex: 0 0 80% !important;
    width: 80% !important;
    max-width: 80% !important;
    min-width: 0 !important;
    scroll-snap-align: start;
    grid-column: auto !important;
    grid-row: auto !important;
  }

  /* Industries grid → h-scroll, compact tiles */
  .aisol-ind-grid {
    display: flex !important;
    flex-direction: row !important;
    grid-template-columns: none !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    gap: 10px !important;
    padding: 4px 20px 18px !important;
    margin-left: -20px !important;
    margin-right: -20px !important;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    mask-image: linear-gradient(90deg, transparent 0, #000 16px, #000 calc(100% - 28px), transparent 100%);
    -webkit-mask-image: linear-gradient(90deg, transparent 0, #000 16px, #000 calc(100% - 28px), transparent 100%);
  }
  .aisol-ind-grid::-webkit-scrollbar { display: none; }
  .aisol-ind-grid > .aisol-ind {
    flex: 0 0 74% !important;
    width: 74% !important;
    max-width: 74% !important;
    min-width: 0 !important;
    scroll-snap-align: start;
    grid-column: auto !important;
    grid-row: auto !important;
  }
}

/* ============================================================
   3. PRODUCTS — .pp-grid (grouped by category)
   Pattern: stay vertical (categories), cap each group to 4 + Show all
   ============================================================ */
@media (max-width: 767px) {
  .pp-grid {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }
  .pp-card,
  .pp-card.is-featured,
  .pp-card.has-mock {
    grid-column: auto !important;
    grid-row: auto !important;
    min-height: 0 !important;
  }
  /* Compact: hide the visual mock on mobile (saves ~200px per card) */
  .pp-card-mock { display: none !important; }
  .pp-card-top { gap: 8px !important; }
  .pp-card-title { font-size: 17px !important; line-height: 1.2 !important; }
  .pp-card-desc {
    font-size: 13px !important;
    line-height: 1.5 !important;
    -webkit-line-clamp: 3;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  /* Cap to 4 per category */
  .pp-grid[data-bgmc-capped] > .pp-card:nth-child(n+5) {
    display: none;
  }
  .pp-grid[data-bgmc-capped="expanded"] > .pp-card:nth-child(n+5) {
    display: flex;
  }
}

/* ============================================================
   4. INDUSTRY pains grid — cap to 4 + Show all
   ============================================================ */
@media (max-width: 767px) {
  .ind-pain-grid {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }
  .ind-pain {
    padding: 18px 20px !important;
  }
  .ind-pain-grid[data-bgmc-capped] > .ind-pain:nth-child(n+5) {
    display: none;
  }
  .ind-pain-grid[data-bgmc-capped="expanded"] > .ind-pain:nth-child(n+5) {
    display: block;
  }
}

/* ============================================================
   5. Hero stats row — wrap to 2x2 on small screens
   ============================================================ */
@media (max-width: 767px) {
  .ind-hero-stats {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 12px 14px !important;
  }
  .ind-stat-n { font-size: 26px !important; }
  .ind-stat-l { font-size: 11px !important; }
}

/* ============================================================
   Reduce motion — disable scroll-snap snap-on animation
   ============================================================ */
@media (prefers-reduced-motion: reduce) {
  .bgmc-rail,
  .aisol-prod-grid,
  .aisol-time-grid,
  .aisol-ind-grid,
  .res-tabs,
  [data-bgmc-rail] { scroll-behavior: auto; }
  .bgmc-more .bgmc-arrow { transition: none; }
}

/* ============================================================
   GENERIC OPT-IN SYSTEM — data-bgmc-* attributes
   For any grid that wants mobile rail/cap/compact behavior
   without us having to know its class namespace.

   ALL rules here are wrapped in @media (max-width: 767px) so
   desktop is never touched.
   ============================================================ */

/* --- Author-tunable knobs (set inline if a page has unusual padding) ---
   --bgmc-edge       : container side-padding the rail should bleed past
                       (default 20px — matches site .container)
   --bgmc-card-width : card flex-basis as a % (default 86%)
   --bgmc-gap        : gap between rail cards (default 12px)                */

@media (max-width: 767px) {

  /* ---- data-bgmc-rail — horizontal scroll rail ---- */
  [data-bgmc-rail] {
    display: flex !important;
    flex-direction: row !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    /* Defeat any inherited grid layout */
    grid-template-columns: none !important;
    grid-template-rows: none !important;
    grid-auto-flow: row !important;
    /* Spacing */
    gap: var(--bgmc-gap, 12px) !important;
    /* Bleed past container padding so cards visually touch screen edges */
    margin-left: calc(-1 * var(--bgmc-edge, 20px)) !important;
    margin-right: calc(-1 * var(--bgmc-edge, 20px)) !important;
    padding: 4px var(--bgmc-edge, 20px) 16px !important;
    /* Snap behavior */
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
    /* Edge fade — hint that more content lives to the right */
    -webkit-mask-image: linear-gradient(90deg,
      transparent 0,
      #000 calc(var(--bgmc-edge, 20px) - 4px),
      #000 calc(100% - 28px),
      transparent 100%);
            mask-image: linear-gradient(90deg,
      transparent 0,
      #000 calc(var(--bgmc-edge, 20px) - 4px),
      #000 calc(100% - 28px),
      transparent 100%);
  }
  [data-bgmc-rail]::-webkit-scrollbar { display: none; }
  [data-bgmc-rail] > * {
    flex: 0 0 var(--bgmc-card-width, 86%) !important;
    width: var(--bgmc-card-width, 86%) !important;
    max-width: var(--bgmc-card-width, 86%) !important;
    min-width: 0 !important;
    scroll-snap-align: start;
    /* Defeat any per-card grid-span the desktop layout assigned */
    grid-column: auto !important;
    grid-row: auto !important;
  }

  /* Slightly tighter cards on the smallest phones */
  @media (max-width: 380px) {
    [data-bgmc-rail] > * {
      flex-basis: 88% !important;
      width: 88% !important;
      max-width: 88% !important;
    }
  }

  /* ---- data-bgmc-cap — cap visible children, JS adds .bgmc-more ----
     Children hidden via :nth-child once the JS sets data-bgmc-capped=""
     (and shown when data-bgmc-capped="expanded"). The :nth-child threshold
     reads the cap from the attribute — we provide rules for the most
     common cap values (2, 3, 4, 5, 6, 8, 10). Other values fall back to
     showing all (no harm, just no cap). */
  [data-bgmc-cap="2"][data-bgmc-capped=""] > *:nth-child(n+3),
  [data-bgmc-cap="3"][data-bgmc-capped=""] > *:nth-child(n+4),
  [data-bgmc-cap="4"][data-bgmc-capped=""] > *:nth-child(n+5),
  [data-bgmc-cap="5"][data-bgmc-capped=""] > *:nth-child(n+6),
  [data-bgmc-cap="6"][data-bgmc-capped=""] > *:nth-child(n+7),
  [data-bgmc-cap="8"][data-bgmc-capped=""] > *:nth-child(n+9),
  [data-bgmc-cap="10"][data-bgmc-capped=""] > *:nth-child(n+11) {
    display: none !important;
  }
  /* When capped, force single-column layout so cards aren't half-empty */
  [data-bgmc-cap][data-bgmc-capped] {
    grid-template-columns: 1fr !important;
  }

  /* ---- data-bgmc-mobile-card="compact" — compact card treatment ----
     Generic visual diet: smaller padding, hide decorative covers/mocks,
     shrink heading, clamp body to ~3 lines. Authors can refine per
     namespace using their own .x-card classes. */
  [data-bgmc-mobile-card="compact"] > * {
    padding: 16px 18px !important;
  }
  /* Hide common decorative children — only if marked decorative */
  [data-bgmc-mobile-card="compact"] [data-bgmc-decoration],
  [data-bgmc-mobile-card="compact"] .bgmc-decoration {
    display: none !important;
  }
}

/* ============================================================
   Rail can ALSO be combined with cap. When both attrs are set,
   on mobile the rail wins (we want h-scroll visible cards, not
   "Show all" — the user can just keep scrolling). The JS still
   counts capped children but does NOT hide them when rail is on
   (see the @media rule below). */
@media (max-width: 767px) {
  [data-bgmc-rail][data-bgmc-cap][data-bgmc-capped=""] > *:nth-child(n) {
    display: flex !important; /* override cap-hide when in rail mode */
  }
}
