/* Progress line: a thin line across the top of the screen that fills as the page is scrolled, drawn as a
   thing the trade works with: a tape measure, a pipe filling, a grout joint, a bead of silicone, a trench, a
   string line. No script: the browser ties it to scrolling. See README.md and example.html.

   How to use it: put one element straight after <body>, with the base class and one material:
       <div class="pl pl-tape" aria-hidden="true"></div>

   Where the browser cannot tie an animation to scrolling, the line is simply not shown.

   Set on the element (or :root) to fit the site:
     --pl-top     how far from the top of the screen it sits (0; set it to the height of a sticky header)
     --pl-h       how tall the line is (each material has its own)
     --pl-accent  the colour of the tape, the string and the tool heads
     --pl-ground  the colour of the ground, for the trench */

@property --pl {                /* how far down the page: 0 at the top, 1 at the bottom */
  syntax: "<number>";
  inherits: true;
  initial-value: 0;
}

.pl {
  --pl-top: 0px;
  --pl-h: 12px;
  --pl-head: 14px;              /* the width of the thing at the leading end */
  --pl-accent: #f2c230;
  --pl-ground: #a8482a;
  display: none;
  position: fixed;
  inset: var(--pl-top) 0 auto 0;
  height: var(--pl-h);
  z-index: 30;
  pointer-events: none;
}
@supports (animation-timeline: scroll()) {
  .pl { display: block; animation: pl-go linear both; animation-timeline: scroll(root block); }
}
@keyframes pl-go { from { --pl: 0; } to { --pl: 1; } }

/* ::before is what has been done so far. It is uncovered from the left, so its pattern never stretches. */
.pl::before {
  content: "";
  position: absolute;
  inset: 0;
  clip-path: inset(0 calc((1 - var(--pl)) * 100%) 0 0);
}
/* ::after is the thing at the leading end: the tape's hook, the nozzle, the bucket. */
.pl::after {
  content: "";
  position: absolute;
  top: 0;
  left: calc(var(--pl) * (100% - var(--pl-head)));
  width: var(--pl-head);
  height: 100%;
}

/* ---- Tape measure: yellow blade, a mark every 10, a taller one every 50, the steel hook at the end ---- */
.pl-tape { --pl-h: 14px; --pl-head: 7px; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, .3)); }
.pl-tape::before {
  background:
    repeating-linear-gradient(90deg, rgba(20, 16, 8, .85) 0 1.5px, transparent 1.5px 50px) 0 0 / 100% 78% no-repeat,
    repeating-linear-gradient(90deg, rgba(20, 16, 8, .7) 0 1px, transparent 1px 10px) 0 0 / 100% 42% no-repeat,
    linear-gradient(to bottom, color-mix(in srgb, var(--pl-accent), #fff 25%), var(--pl-accent) 45%, color-mix(in srgb, var(--pl-accent), #000 14%));
}
.pl-tape::after {
  top: -1px;
  height: calc(100% + 5px);
  border-radius: 0 0 2px 2px;
  background: linear-gradient(90deg, #8d9399, #e3e6e9 45%, #9aa0a6);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .35);
}

/* ---- Pipe: an empty white pipe that fills with water ---- */
.pl-pipe {
  --pl-h: 14px;
  --pl-head: 12px;
  background: linear-gradient(to bottom, #fbfaf6 0 12%, #d8d6cc 22%, #33312e 26% 78%, #b9b7ad 84%, #8f8d84);
  box-shadow: 0 2px 4px rgba(0, 0, 0, .28);
}
.pl-pipe::before { background: linear-gradient(to bottom, transparent 0 26%, #7fc0e6 26%, #2f6f9c 60%, #235676 78%, transparent 78%); }
.pl-pipe::after {              /* the front of the water, rounded */
  top: 26%;
  height: 52%;
  border-radius: 0 99px 99px 0;
  background: linear-gradient(to bottom, #7fc0e6, #2f6f9c 65%, #235676);
}

/* ---- Grout: two rows of tile with an open joint between them, filled as you go ---- */
.pl-grout {
  --pl-h: 16px;
  --pl-head: 16px;
  --pl-tile: #ece7dd;
  --pl-joint: #4a4540;
  --pl-grout: #c9c2b4;
  background:
    repeating-linear-gradient(90deg, var(--pl-joint) 0 3px, transparent 3px 132px),
    linear-gradient(to bottom, var(--pl-tile) 0 38%, var(--pl-joint) 38% 62%, var(--pl-tile) 62%);
  box-shadow: 0 2px 4px rgba(0, 0, 0, .25);
}
.pl-grout::before {
  background:
    repeating-linear-gradient(90deg, var(--pl-grout) 0 3px, transparent 3px 132px),
    linear-gradient(to bottom, transparent 0 38%, var(--pl-grout) 38% 62%, transparent 62%);
}
.pl-grout::after {             /* the rubber float, on its corner */
  top: 1px;
  height: calc(100% - 2px);
  border-radius: 3px;
  background: linear-gradient(135deg, #2f3338, #50565d);
  transform: rotate(8deg);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

/* ---- Bead: a bead of silicone run along a joint, with the nozzle at the end ---- */
.pl-bead { --pl-h: 12px; --pl-head: 22px; --pl-bead: #ffffff; }
.pl-bead::before {
  inset: 22% 0 22% 0;
  border-radius: 0 99px 99px 0;
  clip-path: none;
  width: calc(var(--pl) * (100% - var(--pl-head)) + 4px);
  background: linear-gradient(to bottom, var(--pl-bead), color-mix(in srgb, var(--pl-bead), #000 14%));
  box-shadow: 0 1px 2px rgba(0, 0, 0, .3);
}
.pl-bead::after {              /* the nozzle, cut at an angle */
  clip-path: polygon(0 34%, 100% 0, 100% 100%, 0 66%);
  background: linear-gradient(to bottom, #f1f3f5, #a9b0b7);
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, .35));
}

/* ---- Trench: a strip of ground, dug as you go, with the bucket at the face ---- */
.pl-trench {
  --pl-h: 14px;
  --pl-head: 16px;
  background:
    radial-gradient(circle, rgba(255, 255, 255, .25) 0 1.2px, transparent 1.8px) 3px 2px / 23px 9px,
    radial-gradient(circle, rgba(0, 0, 0, .25) 0 1.2px, transparent 1.8px) 12px 6px / 31px 11px,
    linear-gradient(to bottom, color-mix(in srgb, var(--pl-ground), #fff 10%), var(--pl-ground));
  box-shadow: 0 2px 4px rgba(0, 0, 0, .25);
}
.pl-trench::before {
  background: linear-gradient(to bottom, transparent 0 14%, color-mix(in srgb, var(--pl-ground), #000 62%) 14% 30%, color-mix(in srgb, var(--pl-ground), #000 40%) 30% 86%, transparent 86%);
}
.pl-trench::after {            /* the bucket, teeth forward */
  top: -1px;
  height: calc(100% + 2px);
  clip-path: polygon(0 12%, 62% 12%, 62% 0, 100% 22%, 78% 30%, 100% 50%, 78% 58%, 100% 78%, 62% 100%, 62% 88%, 0 88%);
  background: linear-gradient(to bottom, #3a3d41, #1f2124);
}

/* ---- String line: pulled tight from a peg, with flagging tape on the peg that travels ---- */
.pl-string {
  --pl-h: 16px;
  --pl-head: 9px;
  --pl-accent: #ff5fa2;        /* builder's line is usually fluoro pink or orange */
  background: linear-gradient(90deg, #8a5a2b 0 6px, transparent 6px) 0 0 / 100% 100% no-repeat;   /* the peg at the start */
}
.pl-string::before {
  inset: calc(50% - 1px) 0 auto 0;
  height: 2px;
  background: var(--pl-accent);
  box-shadow: 0 1px 1px rgba(0, 0, 0, .35);
}
.pl-string::after {            /* the travelling peg, with a tail of tape */
  border-radius: 1px 1px 3px 3px;
  background:
    linear-gradient(to bottom, var(--pl-accent) 0 30%, transparent 30%) 0 0 / 100% 100% no-repeat,
    linear-gradient(90deg, #a06a34, #7a4d22);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

@media print { .pl { display: none !important; } }
