/* <sg-tooltip>. The panel is always linked to its trigger by
   aria-describedby (in tooltip.js), so a screen reader has its text
   whether or not it is ever shown. Without JavaScript this file alone
   reveals it on :hover and :focus-visible, with opacity, never display
   or visibility, so the accessible description is never at the mercy of
   the visual state. With JavaScript the panel becomes a popover, in the
   top layer, positioned in a fixed left/top the element computes
   (tooltip.core.js's placement()), and sg-tooltip[data-open] carries the
   shown state instead of :hover (touch and keyboard both use it).
   sg-tooltip[data-above] says which side of the trigger it landed on. */

sg-tooltip[hidden] { display: none; } /* our display rule would otherwise beat the browser's [hidden] */

sg-tooltip {
  display: inline-block;
  position: relative;
  --sg-tooltip-gap: 8px; /* trigger to panel; the hover bands below are exactly this tall, and tooltip.js's fallback placement reads it from here */
}

sg-tooltip [role="tooltip"] {
  position: absolute;
  left: 50%;
  bottom: calc(100% + var(--sg-tooltip-gap));
  transform: translateX(-50%);
  z-index: 10;
  box-sizing: border-box;
  border: 0; /* the popover UA sheet draws a 3px border in the text colour, which on this panel is the page colour: it ate 3px of the fill and made the gap read 11px not the token's 8 */
  max-width: min(22em, var(--sg-fit-max, 22em)); /* tooltip.js caps it to the visible box less 16px */
  width: max-content;
  padding: 0.35em 0.7em;
  border-radius: var(--sg-radius-2, 4px);
  background: var(--sg-text, CanvasText);
  color: var(--sg-surface, Canvas);
  font: var(--sg-step--1, 0.85rem) / 1.4 var(--sg-font-body, system-ui, sans-serif);
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--sg-dur-quick, 140ms) var(--sg-ease-out, ease-out);
}
/* no JS: revealed by real hover/focus, never JS state */
sg-tooltip:is(:hover, :focus-within) [role="tooltip"]:not([popover]) { opacity: 1; pointer-events: auto; }

/* 1.4.13 hoverable: shown, the panel takes the pointer. The gap between trigger and panel belongs to the tooltip too:
   while it is open, an invisible bridge on the element itself (not on the panel, whose overflow would clip it) is two
   bands as tall as the gap, one above and one below the trigger and 1.5rem wider than it each side (less where the page edge is nearer: tooltip.js sets --sg-bridge-l and -r, because a band past the edge is sideways scroll on a phone), whichever side the
   panel took, so the pointer can cross onto the text without the panel closing under it. Each band stops where the
   gap stops: it never covers the trigger, and never a neighbour that starts a gap's width away or further, so a
   click on either lands on it. */
sg-tooltip[data-open]::before, sg-tooltip[data-open]::after { content: ""; position: absolute; left: calc(-1 * min(1.5rem, var(--sg-bridge-l, 1.5rem))); right: calc(-1 * min(1.5rem, var(--sg-bridge-r, 1.5rem))); height: var(--sg-tooltip-gap); }
sg-tooltip[data-open]::before { bottom: 100%; }
sg-tooltip[data-open]::after { top: 100%; }

/* ── with JavaScript: the panel is a popover, in the top layer ──────────── */
sg-tooltip [role="tooltip"][popover] {
  position: fixed;
  inset: auto;
  margin: 0;
  /* tooltip.js slides an open panel back inside the visual viewport by setting --sg-fit-x; 0 when it already fits */
  transform: translateX(var(--sg-fit-x, 0px));
  --_dur: 120ms; --_ease: var(--sg-ease-out, ease-out); --_rise: 0px;
  translate: 0 var(--_rise);
  transition: opacity var(--_dur) var(--_ease), translate var(--_dur) var(--_ease), overlay var(--_dur) allow-discrete, display var(--_dur) allow-discrete;
}
/* placement: above the trigger, centred on it, flipped below when it will not fit.
   tooltip.js names the anchor; without anchor positioning it places by hand. */
@supports (anchor-name: --a) {
  sg-tooltip [role="tooltip"][popover] {
    bottom: anchor(top);
    left: 0; right: 0;
    margin-block: var(--sg-tooltip-gap);
    justify-self: anchor-center;
    justify-self: safe anchor-center;
    position-try-fallbacks: flip-block;
  }
}
/* entering and leaving: opacity only in quiet; warm adds a rise, playful the spring */
sg-tooltip [role="tooltip"][popover]:popover-open { opacity: 1; translate: 0 0; pointer-events: auto; }
@starting-style {
  sg-tooltip [role="tooltip"][popover]:popover-open { opacity: 0; translate: 0 var(--_rise); }
}
sg-tooltip[data-skin="warm"] [role="tooltip"][popover] { --_dur: 160ms; --_rise: 4px; }
sg-tooltip[data-skin="playful"] [role="tooltip"][popover] { --_dur: 240ms; --_ease: var(--sg-ease-spring, ease-out); --_rise: 6px; }

/* ── warm: hand-lettered ─────────────────────────────────────────────────── */
sg-tooltip[data-skin="warm"] [role="tooltip"] {
  font-family: var(--sg-font-hand, cursive);
  font-size: var(--sg-step-0, 1rem);
  background: var(--sg-accent-text, var(--sg-text, CanvasText));
}

/* the popover UA sheet gives the panel overflow: auto, which clips the line (it sits below the panel's own box) inside a scroll
   container: clip instead, and let the line show for the margin it needs */
sg-tooltip[data-skin="playful"] [role="tooltip"][popover] { overflow: clip; overflow-clip-margin: 10px; }

/* ── playful: a pencil leader line between the panel and the trigger ────── */
/* by default the panel sits above the trigger, so the leader points down */
sg-tooltip[data-skin="playful"] [role="tooltip"]::after {
  content: "";
  position: absolute;
  /* tooltip.js sets --sg-lead-x, the trigger's middle measured from the panel's left edge: the line stays under the trigger
     however the panel was placed or slid */
  left: var(--sg-lead-x, 50%);
  top: 100%;
  width: 2px;
  height: 9px;
  /* not currentColor: the panel's own text is pale-on-dark, which would
     draw the line the same colour as the page behind it and hide it */
  background: var(--sg-text, CanvasText);
  transform: translateX(-50%);
}
/* flipped below the trigger (JS only, no room above): the leader points up */
sg-tooltip[data-skin="playful"]:not([data-above]) [role="tooltip"][popover]::after {
  top: auto;
  bottom: 100%;
}

@media (prefers-reduced-motion: reduce) {
  sg-tooltip [role="tooltip"], sg-tooltip [role="tooltip"][popover] { transition: none; }
}

/* ── the trigger, pressed (the touch contract): see popover.css for the rule of it ── */
sg-tooltip > :where(button, a[href]) {
  transition: translate 120ms ease-out, scale 120ms var(--sg-ease-out, ease-out), box-shadow 100ms ease-out;
}
sg-tooltip > :where(button, a[href]):active { transition: none; translate: 0 1px; box-shadow: inset 0 0 0 100vmax color-mix(in oklab, var(--sg-text, CanvasText) 8%, transparent); }
sg-tooltip[data-skin="warm"] > :where(button, a[href]):active { translate: none; box-shadow: inset 0 0 0 100vmax color-mix(in oklab, var(--sg-text, CanvasText) 16%, transparent); }
sg-tooltip[data-skin="playful"] > :where(button, a[href]) { transition-timing-function: ease-out, var(--sg-ease-spring, ease-out), ease-out; }
sg-tooltip[data-skin="playful"] > :where(button, a[href]):active { translate: none; scale: 0.97; box-shadow: inset 0 0 0 100vmax color-mix(in oklab, var(--sg-accent, Highlight) 20%, transparent); }
@media (prefers-reduced-motion: reduce) {
  sg-tooltip > :where(button, a[href]):active { translate: none !important; scale: none !important; box-shadow: inset 0 0 0 100vmax color-mix(in oklab, var(--sg-text, CanvasText) 16%, transparent); }
}
:root[data-sg-input="key"] sg-tooltip > :where(button, a[href]):active { translate: none !important; scale: none !important; }
@media (forced-colors: active) {
  sg-tooltip [role="tooltip"] { border: 1px solid CanvasText; background: Canvas; color: CanvasText; }
  sg-tooltip[data-skin="playful"] [role="tooltip"]::after { display: none; }
}

/* touch contract: focus (docs/contributing/touch-contract.md) */
sg-tooltip :is(button, a, [tabindex]):focus-visible { outline: var(--sg-focus-width, 2px) solid var(--sg-focus, Highlight); outline-offset: var(--sg-focus-offset, 2px); }
