/* =============================================================================
   Background grid
   Spec and rationale: 04-layout-and-spacing.md. Decision: DECISIONS.md (4).
   Requires tokens.css (it defines --grid-size, --grid-line, --grid-rail and the
   light and dark themes). Static, neutrals only, CSS only, no image.

   To remove the effect entirely, set --grid-line and --grid-rail to transparent.
   ============================================================================= */
/* The grid. Fixed to the viewport so it never scrolls or moves.

   Horizontal lines are anchored to the bottom of the top bar rather than to the
   top of the viewport, so the rule under the bar IS the first grid line instead
   of sitting a couple of dozen pixels above one. */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background-image:
    linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-line-h, transparent) 1px, transparent 1px);
  background-size: var(--grid-size) var(--grid-size);
  background-position: center var(--nav-h);
}

/* Container rails: two vertical lines at the edges of the content column.
   Add ONE decorative element to the page shell:
     <div class="page-rails" aria-hidden="true"></div>

   Shown only from 1296px (81rem: the container plus two gutters), the width at
   which the container reaches its maximum. Below that the container is a
   percentage of the viewport, so its edges fall between grid lines and a rail
   would draw a second line beside the grid instead of landing on it.

   The rails are positioned hairlines, not borders. A border is painted inside
   the element's box, which puts the right-hand one a pixel to the left of the
   grid line it is supposed to sit on. These are placed on the boundary itself,
   so each rail coincides exactly with a grid line and simply makes that one
   line a little stronger. --container-max is 19 x --grid-size, which is what
   keeps both edges on the grid at every root size. */
.page-rails { display: none; }

@media (min-width: 1296px) {
  .page-rails {
    display: block;
    position: fixed;
    inset-block: 0;
    left: 0;
    right: 0;
    pointer-events: none;
    z-index: -1;
  }

  .page-rails::before,
  .page-rails::after {
    content: "";
    position: absolute;
    inset-block: 0;
    width: 1px;
    background: var(--grid-rail);
  }

  .page-rails::before { left: calc(50% - var(--container-max) / 2); }
  .page-rails::after  { left: calc(50% + var(--container-max) / 2); }
}

/* Never compete with text or system contrast settings. */
@media (forced-colors: active), (prefers-contrast: more) {
  body::before, .page-rails { display: none; }
}
@media print {
  body::before, .page-rails { display: none; }
}

/* Reminder: the page color goes on html or body. Do not give main or ordinary
   sections an opaque background, or the grid will be hidden behind them. Bands
   that use --color-bg-subtle, the dark hero, and gradient panels are meant to
   cover the grid. */
