/* <lang-switch>: a DE/EN switch where the E changes sides.

   Size    font-size. 1em = 16 units; the switch is 62 x 32 units.
   Colour  --ls-ink (default currentColor) and --ls-paper (the surface behind the switch).
   Works without the script: two links, the block sits on the current one. */

lang-switch {
  --ls-ink: currentColor;
  --ls-paper: Canvas;
  --ls-track: color-mix(in srgb, var(--ls-ink) 10%, transparent);
  --ls-idle: 0.5;
  --ls-focus: var(--ls-ink);

  /* Light letters on dark look heavier than dark letters on light at the same weight.
     These add stroke to whichever copy is dark on light. Swap the two on a dark surface. */
  --ls-grade-base: 0.14;
  --ls-grade-block: 0;

  /* State, in units. The script drives these while moving; at rest the rules here hold them. */
  --ls-e: 0;   /* the E: 0 beside D, 1 beside N */
  --ls-l: 0;   /* block inset from the left */
  --ls-r: 22;  /* block inset from the right */
  --ls-y: 0;   /* extra inset top and bottom: a stretched block gets thinner */
  --ls-lean: 0;  /* degrees the E leans while it moves */
  --ls-u: 0.0625em;

  position: relative;
  display: inline-block;
  inline-size: calc(62 * var(--ls-u));
  block-size: calc(32 * var(--ls-u));
  flex: none;
  vertical-align: middle;
  border-radius: calc(11 * var(--ls-u));
  background: var(--ls-track);
  color: inherit;
  cursor: pointer;
  touch-action: pan-y;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

lang-switch:has(> a[hreflang^="en" i][aria-current]) {
  --ls-e: 1;
  --ls-l: 22;
  --ls-r: 0;
}

lang-switch[data-drag] { cursor: grabbing; }

/* The two links: each sits over its own pair of letters. Their names are for screen readers. */
lang-switch > a {
  position: absolute;
  inset-block: 0;
  inline-size: calc(40 * var(--ls-u));
  border-radius: inherit;
  color: inherit;
  text-decoration: none;
  outline-offset: 2px;
  -webkit-user-drag: none;
  -webkit-touch-callout: none;
}
lang-switch > a[hreflang^="de" i] { inset-inline-start: 0; }
lang-switch > a[hreflang^="en" i] { inset-inline-end: 0; }
lang-switch > a:focus-visible { outline: 2px solid var(--ls-focus); }
lang-switch > a > span {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* A click anywhere goes to the other language; the hit area is at least 44px tall. */
lang-switch > a[aria-current] { pointer-events: none; }
lang-switch > a:not([aria-current])::after {
  content: "";
  position: absolute;
  inset-block: min(0px, 1em - 22px);
  inset-inline: calc(-22 * var(--ls-u));
}

lang-switch svg {
  position: absolute;
  inset: 0;
  inline-size: 100%;
  block-size: 100%;
  display: block;
  pointer-events: none;
}

/* Bottom copy: the letter that is not in the block. */
lang-switch .ls-base {
  opacity: var(--ls-idle);
  transition: opacity 160ms ease;
}
lang-switch .ls-base path {
  fill: var(--ls-ink);
  stroke: var(--ls-ink);
  stroke-width: var(--ls-grade-base);
}

/* Top copy: the block with the letters knocked out, clipped to where the block is. */
lang-switch .ls-block {
  clip-path: inset(
    calc((3 + var(--ls-y)) * var(--ls-u))
    calc((3 + var(--ls-r)) * var(--ls-u))
    calc((3 + var(--ls-y)) * var(--ls-u))
    calc((3 + var(--ls-l)) * var(--ls-u))
    round calc(8 * var(--ls-u))
  );
}
lang-switch .ls-block rect { fill: var(--ls-ink); }
lang-switch .ls-block path {
  fill: var(--ls-paper);
  stroke: var(--ls-paper);
  stroke-width: var(--ls-grade-block);
}
lang-switch .ls-e {
  transform-origin: 26px 22px;   /* the foot of the E */
  transform: translateX(calc(var(--ls-e) * 10px)) skewX(calc(var(--ls-lean) * 1deg));
}

@media (hover: hover) and (pointer: fine) {
  lang-switch:hover .ls-base { opacity: 1; }
}

@media (forced-colors: active) {
  lang-switch {
    --ls-ink: CanvasText;
    --ls-paper: Canvas;
    --ls-track: transparent;
    --ls-idle: 1;
    outline: 1px solid CanvasText;
  }
  lang-switch svg { forced-color-adjust: none; }
}
