/*
 * Final storefront safety guard.
 * Loaded after built-in or uploaded theme CSS. It intentionally contains
 * only invariants that prevent viewport overflow and unusable phone layouts.
 */

body[data-theme-layout]:not(.admin-root),
body[data-theme-layout]:not(.admin-root) *,
body[data-theme-layout]:not(.admin-root) *::before,
body[data-theme-layout]:not(.admin-root) *::after {
  box-sizing: border-box;
}

body[data-theme-layout]:not(.admin-root) :where(
  .theme-page-frame,
  .theme-main-region,
  .theme-sidebar-region,
  .storefront-stage,
  .storefront-shell,
  .store-sections,
  .store-section,
  .product-catalog-main,
  .theme-product-grid,
  .theme-product-card,
  .theme-product-detail__layout,
  .theme-product-detail__gallery,
  .theme-product-detail__summary
) {
  min-inline-size: 0;
  max-inline-size: 100%;
}

body[data-theme-layout]:not(.admin-root) :where(img, video, canvas, svg, iframe) {
  max-inline-size: 100%;
}

body[data-theme-layout]:not(.admin-root) :where(input, select, textarea, button) {
  max-inline-size: 100%;
}

body[data-theme-layout]:not(.admin-root) .theme-main-region {
  overflow-x: clip;
}

body[data-theme-layout]:not(.admin-root) .theme-product-card__media > img {
  inline-size: 100% !important;
  block-size: 100% !important;
}

/* A theme package may choose one or two phone columns, never three. */
@media (max-width: 600px) {
  body.mobile-products-3:not(.admin-root) {
    --store-mobile-product-columns: 2 !important;
  }

  body[data-theme-layout]:not(.admin-root) .theme-product-grid,
  body[data-theme-layout]:not(.admin-root) .theme-feature-product-grid__track {
    grid-template-columns: repeat(var(--store-mobile-product-columns, 2), minmax(0, 1fr)) !important;
  }
}

@media (max-width: 350px) {
  body[data-theme-layout]:not(.admin-root) .theme-product-grid,
  body[data-theme-layout]:not(.admin-root) .theme-product-grid.is-list-view,
  body[data-theme-layout]:not(.admin-root) .theme-feature-product-grid__track {
    grid-template-columns: minmax(0, 1fr) !important;
  }
}

/* ========================================================================
   Universal Theme Engine v7 structural contract
   The theme may style all regions, while layout.json owns their composition.
   ======================================================================== */

body[data-theme-engine="universal"] .theme-universal-header,
body[data-theme-engine="universal"] .theme-u-head,
body[data-theme-engine="universal"] .theme-u-search,
body[data-theme-engine="universal"] .theme-u-actions,
body[data-theme-engine="universal"] .theme-universal-workspace,
body[data-theme-engine="universal"] .theme-universal-sidebar,
body[data-theme-engine="universal"] .theme-universal-main,
body[data-theme-engine="universal"] .theme-universal-footer {
  max-inline-size: 100% !important;
}

body[data-theme-engine="universal"] .theme-u-head {
  position: relative !important;
  inset: auto !important;
  transform: none !important;
  overflow: visible !important;
}

body[data-theme-engine="universal"] .theme-u-search,
body[data-theme-engine="universal"] .theme-u-search .header-search-field,
body[data-theme-engine="universal"] .theme-u-search input {
  min-inline-size: 0 !important;
}

body[data-theme-engine="universal"] .theme-u-search .header-search-submit {
  writing-mode: horizontal-tb !important;
  text-orientation: mixed !important;
  overflow-wrap: normal !important;
  word-break: normal !important;
}

body[data-theme-engine="universal"] .theme-universal-sidebar {
  inset: auto !important;
  transform: none !important;
  float: none !important;
  block-size: auto !important;
  min-block-size: 0 !important;
}

/* Desktop shell composition */
@media (min-width: 1101px) {
  body[data-theme-engine="universal"][data-theme-shell="top"] .theme-universal-workspace {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-areas: "main" !important;
  }

  body[data-theme-engine="universal"][data-theme-shell="top"] .theme-universal-sidebar {
    display: none !important;
  }

  body[data-theme-engine="universal"][data-theme-shell="sidebar-left"] .theme-universal-workspace {
    display: grid !important;
    grid-template-columns: minmax(210px, var(--theme-engine-sidebar-width)) minmax(0, 1fr) !important;
    grid-template-areas: "sidebar main" !important;
  }

  body[data-theme-engine="universal"][data-theme-shell="sidebar-right"] .theme-universal-workspace {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) minmax(210px, var(--theme-engine-sidebar-width)) !important;
    grid-template-areas: "main sidebar" !important;
  }

  body[data-theme-engine="universal"]:is([data-theme-shell="sidebar-left"],[data-theme-shell="sidebar-right"]) .theme-universal-sidebar {
    display: block !important;
    position: relative !important;
    inline-size: auto !important;
    min-inline-size: 0 !important;
    max-inline-size: none !important;
    overflow: hidden !important;
  }

  body[data-theme-engine="universal"]:is([data-theme-shell="sidebar-left"],[data-theme-shell="sidebar-right"]) .theme-universal-main {
    grid-area: main !important;
    inline-size: 100% !important;
  }
}

/* Tablet and phone never inherit a desktop rail that steals the content area. */
@media (max-width: 1100px) {
  body[data-theme-engine="universal"] .theme-universal-workspace {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-areas: "main" !important;
  }

  body[data-theme-engine="universal"] .theme-universal-sidebar {
    display: none !important;
  }
}

/* Catalog composition from layout.json */
@media (min-width: 981px) {
  body[data-theme-engine="universal"].store-page-shop-products[data-theme-catalog-sidebar="left"] .theme-catalog-shell.has-catalog-sidebar {
    display: grid !important;
    grid-template-columns: minmax(220px, var(--theme-engine-catalog-width)) minmax(0, 1fr) !important;
    grid-template-areas: "catalog-sidebar catalog-main" !important;
    gap: var(--theme-engine-gap) !important;
  }

  body[data-theme-engine="universal"].store-page-shop-products[data-theme-catalog-sidebar="right"] .theme-catalog-shell.has-catalog-sidebar {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) minmax(220px, var(--theme-engine-catalog-width)) !important;
    grid-template-areas: "catalog-main catalog-sidebar" !important;
    gap: var(--theme-engine-gap) !important;
  }

  body[data-theme-engine="universal"].store-page-shop-products .theme-catalog-sidebar {
    grid-area: catalog-sidebar !important;
    grid-column: auto !important;
    inline-size: 100% !important;
  }

  body[data-theme-engine="universal"].store-page-shop-products .theme-catalog-main {
    grid-area: catalog-main !important;
    grid-column: auto !important;
    inline-size: 100% !important;
  }

  body[data-theme-engine="universal"].store-page-shop-products[data-theme-catalog-sidebar="hidden"] .theme-catalog-shell,
  body[data-theme-engine="universal"].store-page-shop-products .theme-catalog-shell.catalog-main-only {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-areas: "catalog-main" !important;
  }

  body[data-theme-engine="universal"].store-page-shop-products[data-theme-catalog-sidebar="hidden"] .theme-catalog-sidebar {
    display: none !important;
  }
}

body[data-theme-engine="universal"][data-theme-catalog-content="filters"] .theme-catalog-sidebar .store-block-category-menu,
body[data-theme-engine="universal"][data-theme-catalog-content="filters"] .theme-catalog-sidebar .side-category-pages {
  display: none !important;
}

body[data-theme-engine="universal"][data-theme-catalog-content="categories"] .theme-catalog-sidebar .catalog-left-filters {
  display: none !important;
}

body[data-theme-engine="universal"][data-theme-catalog-sidebar="hidden"] .catalog-filter-trigger,
body[data-theme-engine="universal"][data-theme-mobile-catalog="hidden"] .catalog-filter-trigger,
body[data-theme-engine="universal"][data-theme-mobile-catalog="hidden"] .catalog-drawer-backdrop {
  display: none !important;
}

@media (max-width: 980px) {
  body[data-theme-engine="universal"].store-page-shop-products .theme-catalog-shell {
    display: grid !important;
    grid-template-columns: minmax(0, 1fr) !important;
    grid-template-areas: "catalog-main" !important;
  }

  body[data-theme-engine="universal"].store-page-shop-products .theme-catalog-main {
    grid-area: catalog-main !important;
    inline-size: 100% !important;
  }

  body[data-theme-engine="universal"][data-theme-mobile-catalog="inline"] .theme-catalog-shell.has-catalog-sidebar {
    grid-template-areas: "catalog-sidebar" "catalog-main" !important;
  }

  body[data-theme-engine="universal"][data-theme-mobile-catalog="inline"] .theme-catalog-sidebar {
    position: static !important;
    inset: auto !important;
    z-index: auto !important;
    display: grid !important;
    grid-area: catalog-sidebar !important;
    inline-size: 100% !important;
    max-inline-size: 100% !important;
    max-block-size: none !important;
    padding: 0 !important;
    overflow: visible !important;
    transform: none !important;
    box-shadow: none !important;
  }

  body[data-theme-engine="universal"][data-theme-mobile-catalog="inline"] .catalog-sidebar-head,
  body[data-theme-engine="universal"][data-theme-mobile-catalog="inline"] .catalog-filter-trigger,
  body[data-theme-engine="universal"][data-theme-mobile-catalog="inline"] .catalog-drawer-backdrop {
    display: none !important;
  }

  body[data-theme-engine="universal"][data-theme-mobile-catalog="hidden"] .theme-catalog-sidebar {
    display: none !important;
  }
}

/* Product geometry remains component driven regardless of shell composition. */
body[data-theme-engine="universal"] .theme-product-grid:not(.is-list-view) {
  display: grid !important;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--theme-product-min, 220px)), 1fr)) !important;
  align-items: stretch !important;
  gap: var(--theme-product-gap, 16px) !important;
}

body[data-theme-engine="universal"] .theme-product-card {
  min-inline-size: 0 !important;
  block-size: 100% !important;
  display: flex !important;
  flex-direction: column !important;
  overflow: hidden !important;
}

body[data-theme-engine="universal"] .theme-product-card__body {
  min-inline-size: 0 !important;
  flex: 1 1 auto !important;
  display: flex !important;
  flex-direction: column !important;
}

body[data-theme-engine="universal"] .theme-product-card__action {
  margin-block-start: auto !important;
}

body[data-theme-engine="universal"] .theme-product-card__title,
body[data-theme-engine="universal"] .theme-product-card__description,
body[data-theme-engine="universal"] .theme-product-card__category {
  max-inline-size: 100% !important;
  overflow-wrap: anywhere !important;
}

@media (max-width: 600px) {
  body[data-theme-engine="universal"] .theme-product-grid:not(.is-list-view) {
    grid-template-columns: repeat(var(--store-mobile-product-columns, 2), minmax(0, 1fr)) !important;
  }
}

@media (max-width: 350px) {
  body[data-theme-engine="universal"] .theme-product-grid:not(.is-list-view) {
    grid-template-columns: minmax(0, 1fr) !important;
  }
}

/* ========================================================================
   Universal Theme Engine 7.0 — hard geometry invariants
   Loaded last. Themes own appearance; layout.json owns composition.
   ======================================================================== */
body[data-theme-engine="universal"]:not(.admin-root) {
  display: block !important;
  grid-template: none !important;
  position: relative !important;
  inline-size: 100% !important;
  max-inline-size: 100% !important;
  min-inline-size: 0 !important;
  overflow-x: clip !important;
}

body[data-theme-engine="universal"]:not(.admin-root) :where(*, *::before, *::after) {
  box-sizing: border-box;
}

body[data-theme-engine="universal"]:not(.admin-root) :where(
  .theme-universal-header,
  .storefront-announcement,
  .theme-universal-workspace,
  .theme-universal-main,
  .theme-universal-footer
) {
  float: none !important;
  transform: none !important;
  margin-inline: 0 !important;
  inline-size: 100% !important;
  max-inline-size: 100% !important;
}

body[data-theme-engine="universal"]:not(.admin-root) .theme-u-container {
  inline-size: min(calc(100% - (2 * var(--theme-engine-gutter, 20px))), var(--theme-engine-content-max, 1480px)) !important;
  max-inline-size: calc(100% - (2 * var(--theme-engine-gutter, 20px))) !important;
  margin-inline: auto !important;
}

body[data-theme-engine="universal"]:not(.admin-root) .theme-universal-header {
  position: relative !important;
  inset: auto !important;
  z-index: 120 !important;
  overflow: visible !important;
}

body[data-theme-engine="universal"]:not(.admin-root) .theme-u-head {
  display: grid !important;
  grid-template-areas: "brand search actions" !important;
  grid-template-columns: minmax(150px, 220px) minmax(240px, 1fr) max-content !important;
  align-items: center !important;
  gap: clamp(12px, 2vw, 28px) !important;
}
body[data-theme-engine="universal"]:not(.admin-root)[data-theme-header="compact"] .theme-u-head {
  grid-template-columns: minmax(130px, 185px) minmax(220px, 1fr) max-content !important;
  gap: 12px !important;
}
body[data-theme-engine="universal"]:not(.admin-root)[data-theme-header="minimal"] .theme-u-head {
  grid-template-columns: minmax(116px, 165px) minmax(210px, 1fr) max-content !important;
  gap: 10px !important;
}
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-brand { grid-area: brand !important; }
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-search { grid-area: search !important; }
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-actions { grid-area: actions !important; }

body[data-theme-engine="universal"]:not(.admin-root) .theme-u-brand,
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-search,
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-actions {
  position: relative !important;
  inset: auto !important;
  transform: none !important;
  min-inline-size: 0 !important;
  max-inline-size: 100% !important;
}

body[data-theme-engine="universal"]:not(.admin-root) .theme-u-search {
  display: grid !important;
  grid-template-columns: minmax(0, 1fr) 46px !important;
  align-items: center !important;
  overflow: visible !important;
}
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-search .header-search-field {
  min-inline-size: 0 !important;
  inline-size: 100% !important;
}
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-search .header-search-submit {
  position: static !important;
  inset: auto !important;
  display: inline-grid !important;
  place-items: center !important;
  inline-size: 46px !important;
  min-inline-size: 46px !important;
  max-inline-size: 46px !important;
  block-size: 42px !important;
  min-block-size: 42px !important;
  padding: 0 !important;
  margin: 0 !important;
  writing-mode: horizontal-tb !important;
  word-break: normal !important;
  overflow-wrap: normal !important;
  overflow: hidden !important;
}
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-search .header-search-submit > span {
  display: none !important;
}

body[data-theme-engine="universal"]:not(.admin-root) .theme-u-actions {
  display: flex !important;
  align-items: center !important;
  justify-content: flex-end !important;
  flex-wrap: nowrap !important;
  white-space: nowrap !important;
}
body[data-theme-engine="universal"]:not(.admin-root) :where(.theme-u-language,.theme-u-currency,.theme-u-account,.theme-u-cart) {
  position: relative !important;
  inset: auto !important;
  transform: none !important;
  flex: 0 0 auto !important;
  min-inline-size: 0 !important;
}

body[data-theme-engine="universal"]:not(.admin-root) .theme-u-sidebar-brand {
  grid-template-columns: minmax(0, 1fr) !important;
  inline-size: 100% !important;
  max-inline-size: 100% !important;
}
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-sidebar-brand img {
  inline-size: min(155px, 100%) !important;
  max-inline-size: 100% !important;
  block-size: auto !important;
}
body[data-theme-engine="universal"]:not(.admin-root) .theme-u-sidebar-brand span,
body[data-theme-engine="universal"]:not(.admin-root) :where(.theme-u-sidebar-primary,.theme-u-sidebar-categories,.theme-u-sidebar-pages) :where(a,strong,span) {
  writing-mode: horizontal-tb !important;
  text-orientation: mixed !important;
  word-break: normal !important;
}

body[data-theme-engine="universal"]:not(.admin-root) .storefront-announcement > div {
  inline-size: min(calc(100% - (2 * var(--theme-engine-gutter, 20px))), var(--theme-engine-content-max, 1480px)) !important;
  max-inline-size: calc(100% - (2 * var(--theme-engine-gutter, 20px))) !important;
  margin-inline: auto !important;
}

@media (min-width: 1101px) {
  body[data-theme-engine="universal"]:not(.admin-root)[data-theme-shell="top"] .theme-universal-workspace {
    display: grid !important;
    grid-template: "main" auto / minmax(0, 1fr) !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root)[data-theme-shell="top"] .theme-universal-sidebar {
    display: none !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root)[data-theme-shell="sidebar-left"] .theme-universal-workspace {
    display: grid !important;
    grid-template: "sidebar main" auto / minmax(210px, var(--theme-engine-sidebar-width, 248px)) minmax(0, 1fr) !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root)[data-theme-shell="sidebar-right"] .theme-universal-workspace {
    display: grid !important;
    grid-template: "main sidebar" auto / minmax(0, 1fr) minmax(210px, var(--theme-engine-sidebar-width, 248px)) !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root):is([data-theme-shell="sidebar-left"],[data-theme-shell="sidebar-right"]) .theme-universal-sidebar {
    display: block !important;
    grid-area: sidebar !important;
    position: relative !important;
    inset: auto !important;
    inline-size: auto !important;
    min-inline-size: 0 !important;
    max-inline-size: none !important;
    overflow: hidden !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-universal-main {
    grid-area: main !important;
    min-inline-size: 0 !important;
    inline-size: 100% !important;
  }
}

@media (max-width: 1220px) {
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-head,
  body[data-theme-engine="universal"]:not(.admin-root)[data-theme-header] .theme-u-head {
    grid-template-columns: minmax(125px, 175px) minmax(180px, 1fr) max-content !important;
    gap: 10px !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) :where(.theme-u-account,.theme-u-cart) > span {
    display: none !important;
  }
}

@media (max-width: 900px) {
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-head,
  body[data-theme-engine="universal"]:not(.admin-root)[data-theme-header] .theme-u-head {
    grid-template-areas: "brand actions" "search search" !important;
    grid-template-columns: minmax(105px, 160px) minmax(0, 1fr) !important;
    gap: 9px !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-actions {
    justify-self: end !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-universal-workspace {
    display: grid !important;
    grid-template: "main" auto / minmax(0, 1fr) !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-universal-sidebar {
    display: none !important;
  }
}

@media (max-width: 560px) {
  body[data-theme-engine="universal"]:not(.admin-root) {
    --theme-engine-gutter: 10px !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-utility__inner {
    display: flex !important;
    min-inline-size: 0 !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-contact {
    min-inline-size: 0 !important;
    overflow-x: auto !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-contact a {
    flex: 0 0 auto !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-language,
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-currency,
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-account > span,
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-cart > span {
    display: none !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-u-brand img {
    inline-size: min(132px, 100%) !important;
  }
  body[data-theme-engine="universal"]:not(.admin-root) .theme-product-grid:not(.is-list-view) {
    grid-template-columns: repeat(var(--store-mobile-product-columns, 2), minmax(0, 1fr)) !important;
  }
}

@media (max-width: 350px) {
  body[data-theme-engine="universal"]:not(.admin-root) .theme-product-grid:not(.is-list-view) {
    grid-template-columns: minmax(0, 1fr) !important;
  }
}
