/* Complete product rows. The layout helper counts visible direct children;
   static :has() fallbacks apply only before that data is available. */
.product-grid,
.variant-grid,
.category-grid{
 --catalog-card-span:1;
 --catalog-card-aspect:1;
 grid-template-columns:repeat(2,minmax(0,1fr));
 grid-auto-rows:auto;
 grid-auto-flow:row;
 align-items:stretch;
 justify-items:stretch;
}
.product-grid>.product-card,
.variant-grid>.product-card.variant-card,
.category-grid>.category-card,
.category-grid>.category-card:last-child{
 grid-column:span var(--catalog-card-span);
 grid-row:auto;
 /* An inline-size query container cannot derive its width from its children.
    Size it from the grid area explicitly, including in WebKit's aspect-ratio
    layout, rather than relying on normal self-alignment to stretch it. */
 box-sizing:border-box;
 width:100%;
 max-width:100%;
 min-width:0;
 justify-self:stretch;
 aspect-ratio:var(--catalog-card-aspect);
}
:is(.product-grid,.variant-grid,.category-grid)>[hidden]{
 display:none!important;
}

/* The old last-card feature had two inner columns. All catalogue directions
   now have the same one-column top / title / description layout. */
.category-grid>.category-card,
.category-grid>.category-card:last-child{
 display:grid;
 grid-template-columns:minmax(0,1fr);
 grid-template-rows:auto minmax(min-content,1fr) auto;
 grid-auto-flow:row;
 align-items:stretch;
 justify-items:stretch;
 text-align:left;
}
.category-grid>.category-card>.category-top,
.category-grid>.category-card:last-child>.category-top{
 grid-area:1 / 1;
 align-self:start;
 min-width:0;
 width:100%;
 max-width:100%;
}
.category-grid>.category-card>h2,
.category-grid>.category-card:last-child>h2{
 grid-area:2 / 1;
 align-self:center;
 justify-self:stretch;
 min-width:0;
 width:100%;
 max-width:100%;
 margin:0;
}
.category-grid>.category-card>p,
.category-grid>.category-card:last-child>p{
 grid-area:3 / 1;
 align-self:end;
 justify-self:stretch;
 min-width:0;
 width:100%;
 max-width:100%;
 margin:0;
}
.variant-grid>.product-card.variant-card{
 grid-template-columns:minmax(0,1fr);
 grid-template-rows:auto minmax(min-content,1fr) auto;
}
.variant-grid>.variant-card>.variant-top{grid-area:1 / 1}
.variant-grid>.variant-card>.variant-display{grid-area:2 / 1}
.variant-grid>.variant-card>:is(.variant-bottom,.equipment-option-copy){grid-area:3 / 1}

/* Option cards have a content-driven minimum height. An aspect ratio must not
   transfer that minimum back into their inline size and widen the grid. */
.variant-grid.equipment-options>.product-card.variant-card.equipment-option{
 aspect-ratio:auto;
 min-width:0;
 max-width:100%;
 width:100%;
 height:auto;
}

@media(min-width:601px) and (max-width:1279px){
 /* The final card in an odd tablet row closes the whole container. */
 :is(.product-grid,.variant-grid,.category-grid):is(
  [data-grid-count$="1"],[data-grid-count$="3"],[data-grid-count$="5"],
  [data-grid-count$="7"],[data-grid-count$="9"]
 )>[data-grid-tail="1"],
 :is(.product-grid,.variant-grid,.category-grid):not([data-grid-count]):has(
  >:nth-child(odd):last-child
 )>:last-child{
  --catalog-card-span:2;
  --catalog-card-aspect:2;
 }
 /* The five-item machine filter ends with a full-width PP.5 card. Its larger
    code and specifications need their intrinsic height at this breakpoint. */
 :is(.product-grid,.variant-grid)[data-grid-count="5"]>
  .product-card.variant-card.grade-card[data-grid-tail="1"]{
  aspect-ratio:auto;
  min-height:400px;
  height:auto;
 }
}

@media(min-width:1280px){
 .product-grid,
 .variant-grid,
 .category-grid{
  --catalog-card-span:3;
  --catalog-card-aspect:1;
  grid-template-columns:repeat(12,minmax(0,1fr));
 }
 :is(.product-grid,.variant-grid,.category-grid)[data-grid-count="1"],
 :is(.product-grid,.variant-grid,.category-grid):not([data-grid-count]):has(>:only-child){
  --catalog-card-span:12;
  --catalog-card-aspect:4;
 }
 :is(.product-grid,.variant-grid,.category-grid)[data-grid-count="2"],
 :is(.product-grid,.variant-grid,.category-grid):not([data-grid-count]):has(>:nth-child(2):last-child){
  --catalog-card-span:6;
 }
 :is(.product-grid,.variant-grid,.category-grid):is(
  [data-grid-count="3"],[data-grid-count="5"],[data-grid-count="6"],[data-grid-count="9"]
 ),
 :is(.product-grid,.variant-grid,.category-grid):not([data-grid-count]):has(
  >:is(:nth-child(3),:nth-child(5),:nth-child(6),:nth-child(9)):last-child
 ){
  --catalog-card-span:4;
 }

 /* Five items form three squares followed by two wider closing cards. */
 :is(.product-grid,.variant-grid,.category-grid)[data-grid-count="5"]>
  :is([data-grid-tail="1"],[data-grid-tail="2"]),
 :is(.product-grid,.variant-grid,.category-grid):not([data-grid-count]):has(
  >:nth-child(5):last-child
 )>:nth-last-child(-n+2){
  --catalog-card-span:6;
  --catalog-card-aspect:1.5;
 }

 /* A four-column remainder of two closes with two half-width cards. The
    explicit ten-card branch also works if only count/tail data is present. */
 :is(.product-grid,.variant-grid,.category-grid):is(
  [data-grid-count="10"],
  [data-grid-remainder="2"]:not([data-grid-count="2"]):not([data-grid-count="6"])
 )>:is([data-grid-tail="1"],[data-grid-tail="2"]),
 :is(.product-grid,.variant-grid,.category-grid):not([data-grid-count]):has(
  >:nth-child(10):last-child
 )>:nth-last-child(-n+2){
  --catalog-card-span:6;
  --catalog-card-aspect:2;
 }

 /* General larger catalogues retain complete rows too: ...4+3 or ...3+2. */
 :is(.product-grid,.variant-grid,.category-grid)[data-grid-remainder="3"]:not(
  [data-grid-count="3"]
 )>:is([data-grid-tail="1"],[data-grid-tail="2"],[data-grid-tail="3"]),
 :is(.product-grid,.variant-grid,.category-grid)[data-grid-remainder="1"]:not(
  [data-grid-count="1"]
 ):not([data-grid-count="5"]):not([data-grid-count="9"])>
  :is([data-grid-tail="3"],[data-grid-tail="4"],[data-grid-tail="5"]){
  --catalog-card-span:4;
  --catalog-card-aspect:4 / 3;
 }
 :is(.product-grid,.variant-grid,.category-grid)[data-grid-remainder="1"]:not(
  [data-grid-count="1"]
 ):not([data-grid-count="5"]):not([data-grid-count="9"])>
  :is([data-grid-tail="1"],[data-grid-tail="2"]){
  --catalog-card-span:6;
  --catalog-card-aspect:2;
 }
}

@media(max-width:600px){
 .product-grid,
 .variant-grid,
 .category-grid{
  --catalog-card-span:1;
  --catalog-card-aspect:1;
  grid-template-columns:minmax(0,1fr);
 }
 /* Film specifications and the Super subgrade contain more text than the
    other single-word cards. Let their content grow instead of clipping the
    final row to the square's preferred 288px height on a 320px phone. */
 .variant-grid>.product-card.variant-card.grade-card,
 .variant-grid>.product-card.variant-card:has(.variant-mix){
  aspect-ratio:auto;
  min-height:340px;
  height:auto;
  grid-template-rows:auto minmax(min-content,1fr) auto;
 }
}
