@keyframes focus-bounce {
  0%,
  75%,
  100% {
    outline-offset: var(--magma-outline-focus-offset, 2px);
  }

  50% {
    outline-offset: var(--magma-outline-blur-offset, 6px);
  }
}

:host(:not(:is([hydrated], .hydrated))) {
  @apply not-hydrated;
}

/* The underline, always visible: what tells the link from the text around it without its color
   (WCAG 1.4.1). Its weight never changes under the pointer: a line that grows, or dots that join
   into a line, read as a jump in the text. Under the pointer every link fills with the same
   faint background from the bottom, whatever its style and tone; a weak line also takes the whole
   color, at once. It follows the text when it wraps. */

/* How far the fill rises: just past the box of the text, which is 1.66 to 1.88 times the cap
   height in every typography and font of Magma (the line height varies too much: 0.73 to 0.99 of
   it). Any further and the fill would spend part of the transition out of sight. */

.link:is(:hover, :active) {
  --private-link-fill: 2cap;
}

/* solid: a thin line in the color of the link */

:host([underline='solid']) .link {
  text-decoration-line: underline;
}

/* dotted: a dotted line, thicker, so that the dots read as dots */

:host([underline='dotted']) {
  --mds-link-underline-thickness: --spacing(50);
}

:host([underline='dotted']) .link {
  text-decoration-line: underline;
  text-decoration-style: dotted;
}

/* Every tint is a share of the color of the link (currentcolor), whatever sets it: the accent,
   the text around it with variant inherit, or --mds-link-color. The text keeps the whole color and
   the tints sit between it and the surface, in light and dark mode alike, so it keeps its
   contrast on every surface; an accent tint under a colored text would muddy both.

   The tone sets how much of the color the line takes (a registered percentage, so that it
   transitions while the color is recomputed): strong the whole color, weak 35%, which rises to
   the whole color under the pointer, with the fill. */

:host([tone='weak']) .link {
  --private-link-ink: 35%;
}

:host([tone='weak']) .link:is(:hover, :active) {
  --private-link-ink: 100%;
}

/* marker: a tinted band under the text, the upper of the two inset shadows of the link. Under
   the pointer it sinks while the fill (above) rises, so the line ends in the lighter tint of the
   background and the text reads on it, in both tones. At rest the band takes the color and the
   thickness of the underline: a tinted surface rather than a line, so after the tone, which here
   picks the lighter tint for weak. */

:host([underline='marker']) {
  --mds-link-underline-color: color-mix(in srgb, currentcolor 30%, transparent);
  --mds-link-underline-thickness: --spacing(100);
}

:host([underline='marker'][tone='weak']) {
  --mds-link-underline-color: color-mix(in srgb, currentcolor 20%, transparent);
}

:host([underline='marker']) .link {
  --private-link-band: var(--mds-link-underline-thickness);

  text-decoration-line: none;
}

:host([underline='marker']) .link:is(:hover, :active) {
  --private-link-band: 0px;
}

/* Motion, held back.
 *
 * Every component zeroes the private durations its own base CSS reads, which composes
 * with the public knobs - but that only reaches the declarations that read them. An audit
 * of the 52 sheets found 21 transitions in 15 components that did not: the host's own
 * transition in mds-button and mds-table, the focus ring of the input family, the tab
 * slider, the close button of mds-note. This reset reaches the whole shadow tree, which is
 * what the preference promises, and keeps reaching it when a declaration is added.
 *
 * Transitions only, on purpose. A keyframe animation is usually a loader, and a loader has
 * to keep saying "working": mds-spinner slows to 8s under reduced motion instead of
 * freezing, and a duration of nearly zero on an infinite animation would spin it out of
 * control instead of stopping it.
 *
 * 0.01ms rather than 0s, so a component that waits for its own transitionend still gets
 * one: mds-modal, mds-push-notification and mds-tree-item all listen for it, and a
 * transition of zero never fires it. */

/* stylelint-disable time-min-milliseconds -- the floor is there to keep a transition
   perceivable; the point of these two is that it is not. */

:host([pref-animation='reduce']),
:host([pref-animation='reduce']) *,
:host([pref-animation='reduce']) *::before,
:host([pref-animation='reduce']) *::after {
  transition-duration: 0.01ms !important;
}

@media (prefers-reduced-motion: reduce) {
  :host([pref-animation='system']),
  :host([pref-animation='system']) *,
  :host([pref-animation='system']) *::before,
  :host([pref-animation='system']) *::after,
  :host(:not([pref-animation])),
  :host(:not([pref-animation])) *,
  :host(:not([pref-animation])) *::before,
  :host(:not([pref-animation])) *::after {
    transition-duration: 0.01ms !important;
  }
}

/* stylelint-enable time-min-milliseconds */


/* Animation preference. The component mirrors the resolved mode from <html>
 * onto its host as pref-animation (see subscribePreference in @common/preference);
 * these plain shadow selectors then zero the private duration the base CSS reads:
 * the underline and the focus ring change at once. `reduce` always stops; `system`
 * and no controller follow the OS via @media; `no-preference` keeps the base value. */

:host([pref-animation='reduce']) {
  --private-link-transition-duration: 0s;
}

@media (prefers-reduced-motion: reduce) {
  :host([pref-animation='system']),
  :host(:not([pref-animation])) {
    --private-link-transition-duration: 0s;
  }
}

/* Contrast preference. The component reflects the resolved mode from <html> onto
 * its host as pref-contrast (see subscribePreference in @common/preference). The
 * color of the link is a role, which already gains contrast; the tint of the weak
 * line does not, so under `more` the line takes the full color. The marker keeps
 * its band, which the text sits on, and gains a thin underline in the color of the
 * link. `system` and no controller follow the OS via @media. Cross-browser, no
 * :host-context. */

:host([pref-contrast='more'][tone='weak']:not([underline='marker'])) .link {
  --private-link-ink: 100%;
}

:host([pref-contrast='more'][underline='marker']) .link {
  text-decoration-color: currentcolor;
  text-decoration-line: underline;
  text-decoration-thickness: --spacing(25);
}

@media (prefers-contrast: more) {
  :host([pref-contrast='system'][tone='weak']:not([underline='marker'])) .link,
  :host(:not([pref-contrast])[tone='weak']:not([underline='marker'])) .link {
    --private-link-ink: 100%;
  }

  :host([pref-contrast='system'][underline='marker']) .link,
  :host(:not([pref-contrast])[underline='marker']) .link {
    text-decoration-color: currentcolor;
    text-decoration-line: underline;
    text-decoration-thickness: --spacing(25);
  }
}


/**
 * @prop --mds-link-background-hover: The background that fills the link from the bottom under the pointer.
 * @prop --mds-link-color: Public. The color of the text, the underline and the icon of the link.
 * @prop --mds-link-icon-gap: The distance between the icon and the text.
 * @prop --mds-link-icon-size: The size of the icon.
 * @prop --mds-link-transition-duration: The duration of the background filling the link under the pointer, and of the focus ring closing in on it.
 * @prop --mds-link-underline-color: The color of the underline, of which tone `weak` takes a tint; the tint of the band with the `marker` style.
 * @prop --mds-link-underline-offset: The distance between the text and the underline.
 * @prop --mds-link-underline-thickness: The thickness of the underline, thicker for the `dotted` style, the height of the band with the `marker` style.
 */

/* The link is a word of the sentence it sits in: inline, and with the font of its parent
   (family, size, weight, line height, letter spacing), which an inline box inherits. */

:host {
  --mds-link-background-hover: color-mix(in srgb, currentcolor 12%, transparent);
  --mds-link-color: rgb(var(--magma-accent-fg, var(--variant-primary-03, 0 73 211)));
  --mds-link-icon-gap: --spacing(100);
  --mds-link-icon-size: --spacing(400);
  --mds-link-transition-duration: var(--duration-500, 500ms);
  --mds-link-underline-color: currentcolor;
  --mds-link-underline-offset: --spacing(100);
  --mds-link-underline-thickness: --spacing(25);
  --private-link-transition-duration: var(--mds-link-transition-duration);

  display: inline;
}

/* on a colored surface the accent would lose contrast: the color of the text */

:host([variant='inherit']) {
  --mds-link-color: currentcolor;
}

/* The background is two inset shadows from the bottom, which an inline box draws under every
   line it wraps onto: the band of the marker on top, the fill under the pointer below. A
   background cloned on every line (box-decoration-break) would turn off the text-wrap: pretty of
   mds-text in Chrome, for the whole paragraph. Their heights are lengths registered for the
   document (the property sheet of this folder), and the heights are what transitions, never the
   box-shadow: its color, a tint of currentcolor, interpolates to transparent in Chrome. At rest
   both are 0. */

.link {
  --private-link-band: 0px;
  --private-link-fill: 0px;
  --private-link-ink: 100%;

  @apply focus-bounce;

  box-shadow:
    inset 0 calc(var(--private-link-band, 0px) * -1) var(--mds-link-underline-color),
    inset 0 calc(var(--private-link-fill, 0px) * -1) var(--mds-link-background-hover);
  color: var(--mds-link-color);
  cursor: pointer;
  text-decoration-color: color-mix(
    in srgb,
    var(--mds-link-underline-color) var(--private-link-ink, 100%),
    transparent
  );
  -webkit-text-decoration-skip: ink;
          text-decoration-skip-ink: auto;
  text-decoration-thickness: var(--mds-link-underline-thickness);
  text-underline-offset: var(--mds-link-underline-offset);
  transition-duration: var(--private-link-transition-duration);
  transition-property:
    --private-link-band, --private-link-fill, --private-link-ink, color, outline, outline-offset;

  /* the band, the fill and the ink of the line ease out: they answer the pointer at once and
     settle slowly, so the rise is quick and the fall lingers */
  transition-timing-function:
    var(--ease-out-quint, cubic-bezier(0.23, 1, 0.32, 1)), var(--ease-out-quint, cubic-bezier(0.23, 1, 0.32, 1)), var(--ease-out-quint, cubic-bezier(0.23, 1, 0.32, 1)), var(--ease-out-sine, cubic-bezier(0.39, 0.575, 0.565, 1)),
    var(--ease-out-sine, cubic-bezier(0.39, 0.575, 0.565, 1)), var(--ease-out-sine, cubic-bezier(0.39, 0.575, 0.565, 1));
}

/* the focus ring bounces, as on mds-button */

.link:focus-visible {
  @apply focus-bounce--focused;
}

/* The icon is drawn in the padding of an inline box that holds a word joiner: a box the line
   breaks with the word next to it, where an inline-block (the icon itself) would be a place to
   break. Centered on the box, which is as tall as the font. */

.glue {
  position: relative;
}

.glue-right {
  padding-inline-start: calc(var(--mds-link-icon-size) + var(--mds-link-icon-gap));
}

.glue-left {
  padding-inline-end: calc(var(--mds-link-icon-size) + var(--mds-link-icon-gap));
}

.icon {
  fill: currentcolor;
  height: var(--mds-link-icon-size);
  position: absolute;
  top: 50%;
  translate: 0 -50%;
  width: var(--mds-link-icon-size);
}

.glue-right .icon {
  inset-inline-end: 0;
}

.glue-left .icon {
  inset-inline-start: 0;
}

/* read by the assistive technology, never painted */

.hint {
  @apply sr-only;
}
