:root {
  /* --- Layout & Grid --- */
  --container-gutter: var(--space-m);
  --grid-columns: 12;

  /* Asymmetric Layouts (Sidebar/Content) */
  --grid-1-2: minmax(0, 1fr) minmax(0, 2fr);
  --grid-1-3: minmax(0, 1fr) minmax(0, 3fr);
  --grid-2-1: minmax(0, 2fr) minmax(0, 1fr);
  --grid-2-3: minmax(0, 2fr) minmax(0, 3fr);
  --grid-3-1: minmax(0, 3fr) minmax(0, 1fr);
  --grid-3-2: minmax(0, 3fr) minmax(0, 2fr);

  /* Grid Auto-Responsive Math (Mapped to your Framework) */
  /* This uses your --container-width and --container-gutter for the math */
  --grid-auto-2: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((2 - 1) * var(--container-gutter))) / 2) * 0.7),
          (100% - (2 - 1) * var(--container-gutter)) / 2
        )
      ),
      1fr
    )
  );
  --grid-auto-3: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((3 - 1) * var(--container-gutter))) / 3) * 0.7),
          (100% - (3 - 1) * var(--container-gutter)) / 3
        )
      ),
      1fr
    )
  );
  --grid-auto-4: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((4 - 1) * var(--container-gutter))) / 4) * 0.7),
          (100% - (4 - 1) * var(--container-gutter)) / 4
        )
      ),
      1fr
    )
  );
  --grid-auto-5: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((5 - 1) * var(--container-gutter))) / 5) * 0.7),
          (100% - (5 - 1) * var(--container-gutter)) / 5
        )
      ),
      1fr
    )
  );
  --grid-auto-6: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((6 - 1) * var(--container-gutter))) / 6) * 0.7),
          (100% - (6 - 1) * var(--container-gutter)) / 6
        )
      ),
      1fr
    )
  );
  --grid-auto-7: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((7 - 1) * var(--container-gutter))) / 7) * 0.7),
          (100% - (7 - 1) * var(--container-gutter)) / 7
        )
      ),
      1fr
    )
  );
  --grid-auto-8: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((8 - 1) * var(--container-gutter))) / 8) * 0.7),
          (100% - (8 - 1) * var(--container-gutter)) / 8
        )
      ),
      1fr
    )
  );
  --grid-auto-9: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((9 - 1) * var(--container-gutter))) / 9) * 0.7),
          (100% - (9 - 1) * var(--container-gutter)) / 9
        )
      ),
      1fr
    )
  );
  --grid-auto-10: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((10 - 1) * var(--container-gutter))) / 10) * 0.7),
          (100% - (10 - 1) * var(--container-gutter)) / 10
        )
      ),
      1fr
    )
  );
  --grid-auto-11: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((11 - 1) * var(--container-gutter))) / 11) * 0.7),
          (100% - (11 - 1) * var(--container-gutter)) / 11
        )
      ),
      1fr
    )
  );
  --grid-auto-12: repeat(
    auto-fill,
    minmax(
      min(
        100%,
        max(
          calc(((var(--container-width) - ((12 - 1) * var(--container-gutter))) / 12) * 0.7),
          (100% - (12 - 1) * var(--container-gutter)) / 12
        )
      ),
      1fr
    )
  );
}

/* --- Core Elements --- */
h1 {
  font-size: var(--h1);
}

h2 {
  font-size: var(--h2);
}

h3 {
  font-size: var(--h3);
}

h4 {
  font-size: var(--h4);
  line-height: 1.2;
}

h5 {
  font-size: var(--h5);
  line-height: 1.3;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

h6 {
  font-size: var(--h6);
  line-height: 1.3;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

section {
  padding-block: var(--space-xl);
}

.container {
  width: 100%;
  max-width: var(--container-width);
  margin-inline: auto;
  padding-inline: var(--container-gutter);
}

:where(p) {
  margin-bottom: var(--space-m);
}

:where(.row p:last-of-type) {
  margin-bottom: 0;
}

/* --- Row Engines --- */
.row {
  /* Reset both variables on every .row to prevent inheritance from parent rows.
     Without this, --row-layout set on .row-1-2 would inherit into any nested
     .row inside its children, breaking their column layout silently. */
  --row-layout: initial;
  --row-cols: initial;
  display: grid;
  /* If --row-layout exists, use it. Otherwise, use the standard repeat math. */
  grid-template-columns: var(--row-layout, repeat(var(--row-cols, var(--grid-columns)), 1fr));
  gap: var(--container-gutter);
}

/* --- The Flex engine (Auto-Row) --- */
/* Use this when you want items to just figure themselves out */
.row-flex {
  display: flex;
  flex-wrap: wrap;
  gap: var(--container-gutter);
}
.row-flex > * {
  flex: 1 1 0;
  min-width: var(--flex-item-min, 280px); /* Minimum width before stacking */
}

/* --- Column Logic (Desktop First) --- */
/*
  3 breakpoints:
  lg-   Desktop      1024px+      default, no media query
  md-   Tablet       max 1023px   portrait tablets, landscape phones
  sm-   Mobile       max 767px    all phones, everything stacks

  Usage example:
  <div class="col lg-3 md-2">
    3 columns on desktop, 2 on tablet, full width on mobile
  </div>

  .col is intentionally a plain grid child with no flex behaviour by default.
  This means it works cleanly for any content — text, images, plugin output —
  without imposing layout assumptions.

  When you need flex behaviour (equal-height cards, stretched wrappers),
  add the .col-flex modifier:

  PLAIN column — text, images, simple content:
  <div class="row row-auto-3">
    <div class="col">
      <h2>Title</h2>
      <p>Some text. No wrapper needed.</p>
    </div>
  </div>

  FLEX column — cards, wrappers that need to stretch to full height:
  <div class="row row-auto-3">
    <div class="col col-flex">
      <div class="wrapper">
        <h2>Card Title</h2>
        <p>Card text.</p>
        <a href="#" class="button">CTA</a>  ← this will be pushed to the bottom
      </div>
    </div>
  </div>

  MIXED — one col plain, one col flex (e.g. sidebar + card area):
  <div class="row row-1-2">
    <aside class="col">
      <p>Plain sidebar text.</p>
    </aside>
    <div class="col col-flex">
      <div class="wrapper">Card that stretches to match sidebar height.</div>
    </div>
  </div>
*/
.col,
[class*="col-"],
[class*="md-"],
[class*="lg-"],
[class*="sm-"] {
  grid-column: span var(--col-span, 12);
}

/* Desktop — default, no media query needed (1024px+) */
.lg-2 {
  --col-span: 6;
}
.lg-3 {
  --col-span: 4;
}
.lg-4 {
  --col-span: 3;
}
.lg-6 {
  --col-span: 2;
}
.lg-12 {
  --col-span: 1;
}

/* Grid Overrides for 5 and 10 columns */
/* Use like this:
<div class="row row-5">
  <div><div class="wrapper">1/5th width</div></div>
  <div><div class="wrapper">1/5th width</div></div>
  <div><div class="wrapper">1/5th width</div></div>
  <div><div class="wrapper">1/5th width</div></div>
  <div><div class="wrapper">1/5th width</div></div>
</div>*/
.row-5 {
  --row-cols: 5;
}
.row-10 {
  --row-cols: 10;
}

/* Auto-span children to 1 track when in a custom grid */
.row-5 > *,
.row-10 > * {
  --col-span: 1;
}

/* --- Fixed Column Rows for Nested Contexts --- */
/* Use when you need a fixed number of equal columns inside a constrained
   parent (e.g. inside a .col of an asymmetric row).
   Unlike lg- classes, these divide whatever space is available — no
   12-column math, no container-width assumptions.

   Use them like this:
   <div class="row row-cols-3">
     <div class="col stack">Icon 1</div>
     <div class="col stack">Icon 2</div>
     <div class="col stack">Icon 3</div>
   </div>

   WHEN TO USE WHICH:
   row-cols-*   → nested columns, sidebars, any constrained width context
   row-auto-*   → full .container width only (uses container-width math)
   lg- / md-    → full .container width rows only
*/
.row-cols-2 {
  --row-cols: 2;
}
.row-cols-3 {
  --row-cols: 3;
}
.row-cols-4 {
  --row-cols: 4;
}
.row-cols-6 {
  --row-cols: 6;
}

/* Reset col-span so children don't try to span 12 tracks */
.row-cols-2 > *,
.row-cols-3 > *,
.row-cols-4 > *,
.row-cols-6 > * {
  --col-span: 1;
}

/* Tablet (max 1023px) — portrait tablets, landscape phones */
@media (max-width: 1023px) {
  .md-2 {
    --col-span: 6;
  }
  .md-3 {
    --col-span: 4;
  }
  .md-4 {
    --col-span: 3;
  }
  .md-6 {
    --col-span: 2;
  }
  .md-12 {
    --col-span: 1;
  }
}

/* Mobile (max 767px) — all phones */
@media (max-width: 767px) {
  /* Everything stacks full width first */
  .col,
  [class*="col-"],
  [class*="md-"],
  [class*="lg-"] {
    --col-span: 12;
  }

  /* sm- classes override the reset above for intentional mobile spans */
  .sm-2 {
    --col-span: 6;
  }
  .sm-3 {
    --col-span: 4;
  }
}

/* Opt-in flex column — add .col-flex when you need:
   - Equal-height cards across a row
   - A .wrapper inside the col to stretch to full column height
   - A CTA button pinned to the bottom of a card (use margin-top: auto on the button) */
.col-flex {
  display: flex;
  flex-direction: column;
}

/* --- The Stack (Vertical Content Rhythm) --- */
/*
  .stack arranges its children vertically with consistent spacing.
  Use it inside a .col when you need to control the internal layout
  of the column's content — spacing between a heading, text, and a button.

  Two variables you can override contextually:
  --stack-gap    spacing between children  (default: --space-m)
  --stack-align  horizontal alignment      (default: flex-start)

  ALIGN OPTIONS:
  flex-start  → children sit at their natural width, left-aligned  (default)
  stretch     → children fill full width (use for inputs, full-width buttons)
  center      → children centered horizontally
  flex-end    → children right-aligned

  BASIC — default gap, children natural width:
  <div class="col stack">
    <h2>Title</h2>
    <p>Some text.</p>
    <a href="#" class="button">CTA</a>
  </div>

  TIGHTER GAP — override spacing contextually:
  <div class="col stack" style="--stack-gap: var(--space-s);">
    <h3>Compact Title</h3>
    <p>Less space between items.</p>
  </div>

  STRETCH — full-width children (forms, block buttons):
  <div class="col stack" style="--stack-align: stretch;">
    <label>Email</label>
    <input type="email">
    <button type="submit">Subscribe</button>
  </div>

  CENTERED — children centered horizontally (hero text, CTA blocks):
  <div class="col stack" style="--stack-align: center;">
    <h1>Hero Title</h1>
    <p>Subtitle text.</p>
    <a href="#" class="button">Get Started</a>
  </div>

  COMBINED with .col-flex and .wrapper — stack inside a card:
  <div class="col col-flex">
    <div class="wrapper stack">
      <h3>Card Title</h3>
      <p>Card text.</p>
      <a href="#" class="button" style="margin-top: auto;">Read More</a>
    </div>
  </div>
*/
.stack {
  display: flex;
  flex-direction: column;
  width: 100%;
  gap: var(--stack-gap, var(--space-xxs));
  align-items: var(--stack-align, flex-start);
}

/* --- The Wrapper (The Styling Zone) --- */
/*
  .wrapper is the content styling zone inside a .col-flex.
  It stretches to fill the full column height, making it the right
  place to add backgrounds, borders, padding, and border-radius.

  NOTE: .wrapper requires .col-flex on the parent .col to stretch correctly.
  Without .col-flex, flex: 1 has nothing to grow into.

  Example — card with CTA pinned to bottom:
  <div class="col col-flex">
    <div class="wrapper" style="padding: var(--space-m); background: #f5f5f5;">
      <h3>Card Title</h3>
      <p>Card description text.</p>
      <a href="#" class="button" style="margin-top: auto;">Read More</a>
    </div>
  </div>
*/
.wrapper {
  width: 100%;
  flex: 1; /* Fills the full height of the column */
  display: flex;
  flex-direction: column;
  align-items: var(--stack-align, stretch); /* respects --stack-align when set alongside .stack */
}

/* Ensure nested rows take full width inside columns/wrappers */
.col > .row,
.wrapper > .row {
  width: 100%;
}

/* --- Utility Classes --- */
/* Asymmetric Row Utilities */
/* Use them like this:
    <div class="row row-1-2">
      <aside class="col"><div class="wrapper">Sidebar</div></aside>
      <main class="col"><div class="wrapper">Main Content</div></main>
    </div> 
*/
.row-1-2 {
  --row-layout: var(--grid-1-2);
}
.row-1-3 {
  --row-layout: var(--grid-1-3);
}
.row-2-1 {
  --row-layout: var(--grid-2-1);
}
.row-2-3 {
  --row-layout: var(--grid-2-3);
}
.row-3-1 {
  --row-layout: var(--grid-3-1);
}
.row-3-2 {
  --row-layout: var(--grid-3-2);
}

/* Auto-Responsive Row Utilities (No media queries needed!) */
/* Use them like this:
    <div class="row row-auto-3">
      <div class="col"><div class="wrapper">Item</div></div>
      <div class="col"><div class="wrapper">Item</div></div>
      <div class="col"><div class="wrapper">Item</div></div>
      <div class="col"><div class="wrapper">Item</div></div>
    </div>
*/
.row-auto-2 {
  --row-layout: var(--grid-auto-2);
}
.row-auto-3 {
  --row-layout: var(--grid-auto-3);
}
.row-auto-4 {
  --row-layout: var(--grid-auto-4);
}
.row-auto-5 {
  --row-layout: var(--grid-auto-5);
}
.row-auto-6 {
  --row-layout: var(--grid-auto-6);
}
.row-auto-7 {
  --row-layout: var(--grid-auto-7);
}
.row-auto-8 {
  --row-layout: var(--grid-auto-8);
}
.row-auto-9 {
  --row-layout: var(--grid-auto-9);
}
.row-auto-10 {
  --row-layout: var(--grid-auto-10);
}
.row-auto-11 {
  --row-layout: var(--grid-auto-11);
}
.row-auto-12 {
  --row-layout: var(--grid-auto-12);
}

/* Ensure children don't try to span 12 tracks in an asymmetric/auto row */
.row-1-2 > *,
.row-1-3 > *,
.row-2-1 > *,
.row-2-3 > *,
.row-3-1 > *,
.row-3-2 > *,
.row-auto-2 > *,
.row-auto-3 > *,
.row-auto-4 > *,
.row-auto-5 > *,
.row-auto-6 > *,
.row-auto-7 > *,
.row-auto-8 > *,
.row-auto-9 > *,
.row-auto-10 > *,
.row-auto-11 > *,
.row-auto-12 > * {
  --col-span: 1;
}
