/* hovercraft markers: a visible cue on a tipped tag. This is hovercraft's own layer, on top of the tip engine, and it
   never changes the engine. It reads only the engine's public attributes (the preset and the pin) and the attributes
   R writes (data-hovercraft-marker and its parts). Every marker is drawn in the tag's own text colour, so it keeps the
   text's contrast in light and dark. While a marked tag's tip is open the tag is highlighted, from the engine's open
   attribute (see "While its tip is open" below). Works from Firefox 92, Chrome 93 and Safari 15.4: no :has(), no
   color-mix(). */

/* ---- Which line an underline draws -------------------------------------------------------------------------------
   Three custom properties carry the choice down the page, so the nearest preset wins: a page preset sits on <html>,
   a tag's own preset on the tag. --hc-ml-deco and --hc-ml-style are text-decoration's; --hc-ml-img is the drawn wave
   (none for the other two lines). The wave is two tiles of half a period, a crest and a trough, whose arcs meet where
   the stroke is level, so it is smooth and follows the text's colour (an SVG file cannot, a gradient can). */

:where(:root) {
  --hc-ml-deco: underline;
  --hc-ml-style: dotted;
  --hc-ml-img: none;
  --hc-mk-open: rgba(127, 127, 127, 0.22);
  --hc-mk-glow: rgba(127, 127, 127, 0.4);
}

[data-hovercraft-tip-preset="none"],
[data-hovercraft-tip-preset="sober"],
[data-hovercraft-tip-preset="dense"] {
  --hc-ml-deco: underline;
  --hc-ml-style: dotted;
  --hc-ml-img: none;
}

[data-hovercraft-tip-preset="modern"],
[data-hovercraft-tip-preset="editorial"] {
  --hc-ml-deco: underline;
  --hc-ml-style: dashed;
  --hc-ml-img: none;
}

[data-hovercraft-tip-preset="lively"],
[data-hovercraft-marker-line="wavy"] {
  --hc-ml-deco: none;
  --hc-ml-style: solid;
  --hc-ml-img:
    radial-gradient(
      circle at 50% 0.769em,
      transparent 0.682em,
      currentColor 0.694em,
      currentColor 0.769em,
      transparent 0.781em
    ),
    radial-gradient(
      circle at 50% -0.649em,
      transparent 0.682em,
      currentColor 0.694em,
      currentColor 0.769em,
      transparent 0.781em
    );
}

/* An explicit line on the tag beats the preset, so it comes after the preset rules. */
[data-hovercraft-marker-line="dotted"] {
  --hc-ml-deco: underline;
  --hc-ml-style: dotted;
  --hc-ml-img: none;
}

[data-hovercraft-marker-line="dashed"] {
  --hc-ml-deco: underline;
  --hc-ml-style: dashed;
  --hc-ml-img: none;
}

/* ---- Underline ---------------------------------------------------------------------------------------------------
   Drawn on the tag itself when it sits in a line of text, and on the span R puts round the words of a block tag
   (a cell, a paragraph, a value box's title), so the line goes under the words and not across the whole box. A drawn
   line takes no room: it is a background, so nothing moves. The bottom padding lets the background reach below the
   descenders; vertical padding on an inline box changes no line, so nothing moves. */

[data-hovercraft-marker="underline"]:not([data-hovercraft-marker-text]),
[data-hovercraft-marker="underline"] .hc-marker-text {
  text-decoration-line: var(--hc-ml-deco);
  text-decoration-style: var(--hc-ml-style);
  text-decoration-thickness: max(1.5px, 0.09em);
  text-underline-offset: 0.22em;
  text-decoration-skip-ink: auto;
  background-image: var(--hc-ml-img);
  background-repeat: repeat-x, repeat-x;
  background-size:
    1.35em 0.12em,
    1.35em 0.12em;
  background-position:
    0 calc(100% - 0.12em),
    0.675em 100%;
  padding-bottom: 0.14em;
  -webkit-box-decoration-break: clone;
  box-decoration-break: clone;
}

/* ---- Icon --------------------------------------------------------------------------------------------------------
   The built-in icon is a mask in the text's colour on an inline pseudo-element, after the words (in a value box, after
   its title). A word joiner in its
   content keeps it on the line of the last word. A custom icon is a child R adds (class hc-marker-icon, hidden from a
   screen reader) and marks with data-hovercraft-marker-icon, so the built-in one steps aside without :has(). */

[data-hovercraft-marker="icon"]:not(.bslib-value-box):not([data-hovercraft-marker-icon])::after,
.bslib-value-box[data-hovercraft-marker="icon"]:not([data-hovercraft-marker-icon]) .value-box-title::after {
  content: "\2060";
  padding-inline-start: 1.23em;
  background-color: currentColor;
  -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round'%3E%3Ccircle cx='8' cy='8' r='6.5'/%3E%3Cline x1='8' y1='7.2' x2='8' y2='11.2'/%3E%3Ccircle cx='8' cy='4.8' r='0.75' fill='black' stroke='none'/%3E%3C/svg%3E");
  mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='black' stroke-width='1.5' stroke-linecap='round'%3E%3Ccircle cx='8' cy='8' r='6.5'/%3E%3Cline x1='8' y1='7.2' x2='8' y2='11.2'/%3E%3Ccircle cx='8' cy='4.8' r='0.75' fill='black' stroke='none'/%3E%3C/svg%3E");
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
  -webkit-mask-position: 0.28em 50%;
  mask-position: 0.28em 50%;
  -webkit-mask-size: 0.95em 0.95em;
  mask-size: 0.95em 0.95em;
  pointer-events: none;
}

/* Right to left: the icon's room is on the other side of its box, so the gap goes between it and the words. */
[dir="rtl"] [data-hovercraft-marker="icon"]:not(.bslib-value-box):not([data-hovercraft-marker-icon])::after,
[dir="rtl"] .bslib-value-box[data-hovercraft-marker="icon"]:not([data-hovercraft-marker-icon]) .value-box-title::after {
  -webkit-mask-position: 0 50%;
  mask-position: 0 50%;
}

.hc-marker-icon {
  display: inline-block;
  width: 0.95em;
  height: 0.95em;
  vertical-align: -0.14em;
  line-height: 1;
  pointer-events: none;
}

.hc-marker-icon > svg {
  display: block;
  width: 100%;
  height: 100%;
  fill: currentColor;
}

/* ---- Corner ------------------------------------------------------------------------------------------------------
   A small triangle in the top corner, drawn over the tag, so it takes no room. :where() keeps the specificity at
   nothing: a tag that is already positioned (absolute, fixed, sticky) keeps its position. */

:where([data-hovercraft-marker="corner"]:not([data-hovercraft-marker-in])) {
  position: relative;
}

[data-hovercraft-marker="corner"]:not([data-hovercraft-marker-in])::after {
  content: "";
  position: absolute;
  top: 0;
  inset-inline-end: 0;
  z-index: 2;
  width: 0.5rem;
  height: 0.5rem;
  background-color: currentColor;
  clip-path: polygon(0 0, 100% 0, 100% 100%);
  pointer-events: none;
}

[dir="rtl"] [data-hovercraft-marker="corner"]:not([data-hovercraft-marker-in])::after {
  clip-path: polygon(0 0, 100% 0, 0 100%);
}

span[data-hovercraft-marker="corner"]::after,
label[data-hovercraft-marker="corner"]::after {
  top: -0.1em;
  inset-inline-end: -0.4rem;
  width: 0.4rem;
  height: 0.4rem;
}

.bslib-value-box[data-hovercraft-marker="corner"]::after {
  width: 0.9rem;
  height: 0.9rem;
  border-start-end-radius: var(--bs-card-inner-border-radius, var(--bs-border-radius, 0.375rem));
}

/* A value box that puts its showcase in the top right takes the corner at the top left instead. */
.bslib-value-box.showcase-top-right[data-hovercraft-marker="corner"]::after {
  inset-inline-end: auto;
  inset-inline-start: 0;
  clip-path: polygon(0 0, 100% 0, 0 100%);
  border-start-end-radius: 0;
  border-start-start-radius: var(--bs-card-inner-border-radius, var(--bs-border-radius, 0.375rem));
}

/* A cell or header cell of a table (tip_table() writes data-hovercraft-marker-in="table"). The triangle is a background,
   not a pseudo-element: DT draws a header cell's sort arrows with ::before and ::after, and a widget may restyle its
   cells. It takes no room and follows the cell's text colour. */

[data-hovercraft-marker="corner"][data-hovercraft-marker-in="table"] {
  background-image: linear-gradient(to bottom left, currentColor calc(50% - 0.5px), transparent calc(50% + 0.5px));
  background-repeat: no-repeat;
  background-size: 0.5rem 0.5rem;
  background-position: top right;
}

[dir="rtl"] [data-hovercraft-marker="corner"][data-hovercraft-marker-in="table"] {
  background-image: linear-gradient(to bottom right, currentColor calc(50% - 0.5px), transparent calc(50% + 0.5px));
  background-position: top left;
}

/* ---- While its tip is open ---------------------------------------------------------------------------------------
   The engine writes its open attribute ("hint" or "pin") on the tagged element while the tip shows and takes it off
   when the tip closes. A marked tag is tinted then, so a reader sees which of several markers the tip belongs to: the
   words of an underline (the tag, or the span R puts round the words of a block tag), the tag of an icon or a corner
   (a cell, a header cell, a label). The tint is a grey that is see-through, so it keeps the text's contrast in light
   and dark and lies behind the marker, which is drawn as a background image, a mask or a pseudo-element on top.
   Meaning never rests on it alone: the tip is the content. A value box has a background of its own, so it gets a
   glow round its edge, not a tint. No colour-mix(): the browsers' floor has none. */

[data-hovercraft-marker="underline"][data-hovercraft-tip-open]:not([data-hovercraft-marker-text]),
[data-hovercraft-marker="underline"][data-hovercraft-tip-open] .hc-marker-text,
[data-hovercraft-marker="icon"][data-hovercraft-tip-open]:not(.bslib-value-box),
[data-hovercraft-marker="corner"][data-hovercraft-tip-open]:not(.bslib-value-box) {
  background-color: var(--hc-mk-open);
}

.bslib-value-box[data-hovercraft-marker][data-hovercraft-tip-open]:not([data-hovercraft-marker="none"]) {
  box-shadow: 0 0 0 0.2rem var(--hc-mk-glow);
}

/* ---- The pointer -------------------------------------------------------------------------------------------------
   A tip does nothing on a click, so the pointer says "help" and never "pointer", which promises a click. A tip that
   can be pinned (the engine's pin attribute) does answer a click, and keeps the hand. A button or a link keeps the
   cursor it has. */

:where([data-hovercraft-marker]:not([data-hovercraft-marker="none"]):not(a[href], button, [role="button"], summary)) {
  cursor: help;
}

:where([data-hovercraft-marker][data-hovercraft-tip-pin]) {
  cursor: pointer;
}

/* ---- Forced colours ----------------------------------------------------------------------------------------------
   A shape drawn as a background would vanish; keep its colour as the text's. */

@media (forced-colors: active) {
  [data-hovercraft-marker][data-hovercraft-tip-open] {
    outline: 1px solid CanvasText;
    outline-offset: 1px;
  }

  [data-hovercraft-marker="icon"]:not(.bslib-value-box):not([data-hovercraft-marker-icon])::after,
  .bslib-value-box[data-hovercraft-marker="icon"]:not([data-hovercraft-marker-icon]) .value-box-title::after,
  [data-hovercraft-marker="corner"]:not([data-hovercraft-marker-in])::after {
    forced-color-adjust: none;
    background-color: CanvasText;
  }

  .hc-marker-icon {
    forced-color-adjust: none;
    color: CanvasText;
  }
}
