/* Ibad ornament border: sizing rules so it can never break.
   Use:  <div class="ibad-border [round] [ink|cream]" style="--o:.3"> ... </div>
   --o = requested scale (1 = scan size). Everything else is derived. */

.ibad-border {
  /* 1. Scale floor: below .2 the 3px ornament lines drop under ~1 device px and alias into dashes. */
  --s: max(var(--o, .3), .2);

  /* 2. Band thickness (also the corner size) and one edge-tile length, all from --s. */
  --bv: calc(184px * var(--s));   /* top/bottom band */
  --bh: calc(181px * var(--s));   /* left/right band */
  --tx: calc(126px * var(--s));   /* horizontal tile length */
  --ty: calc(122px * var(--s));   /* vertical tile length */
  --ic: #2f5d3a;                  /* rule colour; the ornament colour comes from the image */

  box-sizing: border-box;
  position: relative;
  border-style: solid;
  border-width: var(--bv) var(--bh);
  border-image-source: url(ibad-border-green.svg);
  border-image-slice: 184 181;
  border-image-width: var(--bv) var(--bh);
  border-image-repeat: round;

  /* 3. Size floor: always room for the two corners plus 3 whole tiles. Corners never overlap or squash,
        and `round` never has to stretch a tile by more than +-17% (1 tile would allow +-50%). */
  min-width:  calc(2 * var(--bh) + 3 * var(--tx));
  min-height: calc(2 * var(--bv) + 3 * var(--ty));
}

/* 4. Snap the band to whole CSS pixels so the slice lines land on pixel edges (no blurry corner seams). */
@supports (width: round(1.4px, 1px)) {
  .ibad-border {
    --bv: round(calc(184px * var(--s)), 1px);
    --bh: round(calc(181px * var(--s)), 1px);
  }
}

/* 5. The inner rule is CSS, not part of the image: one continuous line, >= 1px, colourable. */
.ibad-border::after {
  content: ""; position: absolute; pointer-events: none; box-sizing: border-box;
  inset: calc(-60px * var(--s)) calc(-57px * var(--s));
  border: max(1px, calc(6px * var(--s))) solid var(--ic);
}

/* Round corners: its own image + 220px square band, tile 126 both ways; rule follows the arc. */
.ibad-border.round {
  --bv: calc(220px * var(--s)); --bh: var(--bv);
  --tx: calc(126px * var(--s)); --ty: var(--tx);
  border-width: var(--bv);
  border-image-source: url(ibad-border-round-green.svg);
  border-image-slice: 220;
  border-image-width: var(--bv);
  border-radius: var(--bv);            /* clips the background to the round outline */
}
@supports (width: round(1.4px, 1px)) {
  .ibad-border.round { --bv: round(calc(220px * var(--s)), 1px); }
}
.ibad-border.round::after { inset: calc(-96px * var(--s)); border-radius: calc(96px * var(--s)); }

/* Colours (one image per colour; border-image cannot use currentColor) */
.ibad-border.ink          { --ic: #1a1a1a; border-image-source: url(ibad-border-ink.svg); }
.ibad-border.cream        { --ic: #f3ead2; border-image-source: url(ibad-border-cream.svg); }
.ibad-border.round.ink    { border-image-source: url(ibad-border-round-ink.svg); }
.ibad-border.round.cream  { border-image-source: url(ibad-border-round-cream.svg); }

/* Optional exact fit: give the tile counts and the box size is computed, so there is zero stretch.
   <div class="ibad-border fit" style="--o:.3; --nx:8; --ny:3"> */
.ibad-border.fit {
  width:  calc(2 * var(--bh) + var(--nx, 8) * var(--tx));
  height: calc(2 * var(--bv) + var(--ny, 3) * var(--ty));
}
