/* Twoslash — interactive TypeScript type hovers, inline errors, ^? queries and completions
 * on ```ts twoslash code blocks. Rendered at build time by Shiki + @shikijs/twoslash (see
 * aardvark/twoslash.py); this file styles the markup that the @shikijs/twoslash *rich*
 * renderer emits. Loaded only on pages that contain a twoslash block (base.html gates it on
 * page_has_twoslash).
 *
 * The structural rules below are vendored from @shikijs/twoslash's reference style-rich.css;
 * the popover / docs / border / shadow colors are driven entirely by the --twoslash-*
 * variables, which we remap onto aardvark's --aardvark-* tokens — so the popovers track the
 * site theme and flip on the dark scheme automatically, with no separate dark structural
 * block. Token colors come from Shiki's dual themes (see the .shiki rules).
 *
 * UPGRADE NOTE: two emitted class names couple to @shikijs/twoslash's output — re-check both when
 * bumping the shiki / @shikijs/twoslash dep:
 *   - `.twoslash-query-presisted` (used below at the :has() reserve and the query-popup rules)
 *     mirrors an intentional MISSPELLING ("presisted", not "persisted"); if upstream fixes the typo
 *     these rules silently stop matching (no error).
 *   - `.twoslash-hover` backs keyboard access: twoslash_render.mjs stamps tabindex="0" on exactly
 *     `<span class="twoslash-hover"`, and the :focus-within rules below reveal the popup on focus.
 *     If a future Shiki emits an extra class on that span the tabindex no-ops and keyboard focus
 *     silently regresses to hover-only. */

/* ===== Variables: aardvark-themed (replaces the upstream :root block) ===== */
:root {
  --twoslash-border-color: var(--aardvark-border);
  --twoslash-underline-color: var(--aardvark-accent);
  --twoslash-highlighted-border: #c37d0d50;
  --twoslash-highlighted-bg: #c37d0d20;
  --twoslash-popup-bg: var(--aardvark-bg);
  --twoslash-popup-color: var(--aardvark-fg);
  --twoslash-popup-shadow: rgba(0, 0, 0, 0.12) 0px 2px 8px;
  --twoslash-docs-color: var(--aardvark-muted);
  --twoslash-docs-font: sans-serif;
  --twoslash-code-font: inherit;
  --twoslash-code-font-size: 0.9em;
  --twoslash-matched-color: inherit;
  --twoslash-unmatched-color: var(--aardvark-muted);
  --twoslash-cursor-color: var(--aardvark-muted);
  --twoslash-error-color: #d45656;
  --twoslash-error-bg: #d4565620;
  --twoslash-warn-color: #c37d0d;
  --twoslash-warn-bg: #c37d0d20;
  --twoslash-tag-color: var(--aardvark-accent);
  --twoslash-tag-bg: rgba(25, 113, 194, 0.1);
  --twoslash-tag-warn-color: var(--twoslash-warn-color);
  --twoslash-tag-warn-bg: var(--twoslash-warn-bg);
  --twoslash-tag-annotate-color: #1ba673;
  --twoslash-tag-annotate-bg: #1ba67320;
}
:root[data-mantine-color-scheme="dark"] {
  --twoslash-popup-shadow: rgba(0, 0, 0, 0.5) 0px 2px 12px;
  --twoslash-error-color: #f87171;
  --twoslash-error-bg: rgba(248, 113, 113, 0.14);
  --twoslash-warn-color: #e0a52b;
  --twoslash-warn-bg: rgba(224, 165, 43, 0.16);
  --twoslash-tag-bg: rgba(116, 192, 252, 0.16);
  --twoslash-tag-annotate-color: #34d399;
  --twoslash-tag-annotate-bg: rgba(52, 211, 153, 0.16);
}

/* ===== Shiki token colors + container parity ===== */
/* Shiki (defaultColor:false) emits dual --shiki-light / --shiki-dark variables per token and
 * sets NO direct color, so apply them here: light by default, --shiki-dark on the dark scheme.
 * The <pre> background stays the theme code surface (.aardvark-content pre, theme.css), so a
 * twoslash block sits in the page like any other code block — only the token hues are Shiki's. */
.aardvark-content .shiki,
.aardvark-content .shiki span {
  color: var(--shiki-light);
}
:root[data-mantine-color-scheme="dark"] .aardvark-content .shiki,
:root[data-mantine-color-scheme="dark"] .aardvark-content .shiki span {
  color: var(--shiki-dark);
}
/* Hover popovers are absolutely positioned and must escape the block, so a twoslash <pre> is
 * not clipped (theme.css gives plain code blocks overflow-x:auto, which would crop the popups).
 * Keep twoslash demos concise — a very long line extends the block rather than scrolling. */
.aardvark-content pre.twoslash {
  overflow: visible;
}
/* A persisted `// ^?` query popup is absolutely positioned below its token and overflows the
 * block's bottom edge; reserve room beneath such a block so it can't overlap the next section.
 * :has() scopes the reserve to blocks that actually end with a query (a graceful no-op in the
 * rare browser without :has — the popup then overlaps, the upstream default). The reserve fits
 * a typical multi-line type popup; an unusually tall one may still extend past it. */
.aardvark-content pre.twoslash:has(.twoslash-query-presisted) {
  margin-bottom: 9em;
}

/* ===== Structural rules (vendored from @shikijs/twoslash/style-rich.css) ===== */

/* Respect people's wishes to not have animations */
@media (prefers-reduced-motion: reduce) {
  .twoslash * {
    transition: none !important;
  }
}

/* ===== Hover Info ===== */
.twoslash:hover .twoslash-hover {
  border-color: var(--twoslash-underline-color);
}

/* Keyboard parity for the hover affordance: underline the focused trigger like the mouse path,
 * and show a clear focus ring so keyboard users can see which token they're on. */
.twoslash .twoslash-hover:focus-within {
  border-color: var(--twoslash-underline-color);
}
.twoslash .twoslash-hover:focus-visible {
  outline: 2px solid var(--twoslash-underline-color);
  outline-offset: 2px;
  border-radius: 2px;
}

.twoslash .twoslash-hover {
  border-bottom: 1px dotted transparent;
  transition-timing-function: ease;
  transition: border-color 0.3s;
  position: relative;
}

.twoslash .twoslash-popup-container {
  position: absolute;
  opacity: 0;
  display: inline-flex;
  flex-direction: column;
  transform: translateY(1.1em);
  background: var(--twoslash-popup-bg);
  color: var(--twoslash-popup-color);
  border: 1px solid var(--twoslash-border-color);
  transition: opacity 0.3s;
  border-radius: 4px;
  pointer-events: none;
  z-index: 10;
  user-select: none;
  text-align: left;
  box-shadow: var(--twoslash-popup-shadow);
}

.twoslash .twoslash-query-presisted .twoslash-popup-container {
  z-index: 9;
  transform: translateY(1.5em);
}

/* :focus-within companions to every :hover reveal so keyboard users reach the type popups —
 * the renderer stamps tabindex="0" on the hover triggers (twoslash_render.mjs), so tabbing onto
 * one fires :focus-within here. Required for keyboard operability (AGENTS.md / WCAG 2.1 AA). */
.twoslash .twoslash-hover:hover .twoslash-popup-container,
.twoslash .twoslash-hover:focus-within .twoslash-popup-container,
.twoslash .twoslash-error-hover:hover .twoslash-popup-container,
.twoslash .twoslash-error-hover:focus-within .twoslash-popup-container,
.twoslash .twoslash-query-presisted .twoslash-popup-container,
.twoslash .twoslash-query-line .twoslash-popup-container {
  opacity: 1;
  pointer-events: auto;
}

.twoslash .twoslash-popup-container:hover {
  user-select: auto;
}

.twoslash .twoslash-popup-arrow {
  position: absolute;
  top: -4px;
  left: 1em;
  border-top: 1px solid var(--twoslash-border-color);
  border-right: 1px solid var(--twoslash-border-color);
  background: var(--twoslash-popup-bg);
  transform: rotate(-45deg);
  width: 6px;
  height: 6px;
  pointer-events: none;
}

.twoslash .twoslash-popup-code,
.twoslash .twoslash-popup-error,
.twoslash .twoslash-popup-docs {
  padding: 6px 8px !important;
}

.twoslash .twoslash-popup-code {
  font-family: var(--twoslash-code-font);
  font-size: var(--twoslash-code-font-size);
}

.twoslash .twoslash-popup-docs {
  color: var(--twoslash-docs-color);
  font-family: var(--twoslash-docs-font);
  font-size: 0.8em;
  border-top: 1px solid var(--twoslash-border-color);
}

.twoslash .twoslash-popup-error {
  color: var(--twoslash-error-color);
  background-color: var(--twoslash-error-bg);
  font-family: var(--twoslash-docs-font);
  font-size: 0.8em;
}

.twoslash .twoslash-popup-docs-tags {
  display: flex;
  flex-direction: column;
  font-family: var(--twoslash-docs-font);
}

.twoslash .twoslash-popup-docs-tags,
.twoslash .twoslash-popup-docs-tag-name {
  margin-right: 0.5em;
}

.twoslash .twoslash-popup-docs-tag-name {
  font-family: var(--twoslash-code-font);
}

/* ===== Query Line ===== */
.twoslash .twoslash-query-line .twoslash-popup-container {
  position: relative;
  margin-bottom: 1.4em;
  transform: translateY(0.6em);
}

/* ===== Error Line ===== */
.twoslash .twoslash-error-line {
  position: relative;
  background-color: var(--twoslash-error-bg);
  border-left: 3px solid var(--twoslash-error-color);
  color: var(--twoslash-error-color);
  padding: 6px 12px;
  margin: 0.2em 0;
  min-width: 100%;
  width: max-content;
}

.twoslash .twoslash-error-line.twoslash-error-level-warning {
  background-color: var(--twoslash-warn-bg);
  border-left: 3px solid var(--twoslash-warn-color);
  color: var(--twoslash-warn-color);
}

.twoslash .twoslash-error {
  background: url("data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%206%203'%20enable-background%3D'new%200%200%206%203'%20height%3D'3'%20width%3D'6'%3E%3Cg%20fill%3D'%23c94824'%3E%3Cpolygon%20points%3D'5.5%2C0%202.5%2C3%201.1%2C3%204.1%2C0'%2F%3E%3Cpolygon%20points%3D'4%2C0%206%2C2%206%2C0.6%205.4%2C0'%2F%3E%3Cpolygon%20points%3D'0%2C2%201%2C3%202.4%2C3%200%2C0.6'%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E")
    repeat-x bottom left;
  padding-bottom: 2px;
}

.twoslash .twoslash-error.twoslash-error-level-warning {
  background: url("data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%206%203'%20enable-background%3D'new%200%200%206%203'%20height%3D'3'%20width%3D'6'%3E%3Cg%20fill%3D'%23c37d0d'%3E%3Cpolygon%20points%3D'5.5%2C0%202.5%2C3%201.1%2C3%204.1%2C0'%2F%3E%3Cpolygon%20points%3D'4%2C0%206%2C2%206%2C0.6%205.4%2C0'%2F%3E%3Cpolygon%20points%3D'0%2C2%201%2C3%202.4%2C3%200%2C0.6'%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E")
    repeat-x bottom left;
  padding-bottom: 2px;
}

/* ===== Completions ===== */
.twoslash .twoslash-completion-cursor {
  position: relative;
}

.twoslash .twoslash-completion-cursor .twoslash-completion-list {
  user-select: none;
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(0, 1.2em);
  margin: 3px 0 0 -1px;
  display: inline-block;
  z-index: 8;
  box-shadow: var(--twoslash-popup-shadow);
  background: var(--twoslash-popup-bg);
  border: 1px solid var(--twoslash-border-color);
}

.twoslash-completion-list {
  width: 240px;
  font-size: 0.8rem;
  padding: 4px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.twoslash-completion-list:hover {
  user-select: auto;
}

.twoslash-completion-list::before {
  background-color: var(--twoslash-cursor-color);
  width: 2px;
  position: absolute;
  top: -1.6em;
  height: 1.4em;
  left: -1px;
  content: ' ';
}

.twoslash-completion-list li {
  overflow: hidden;
  display: flex;
  align-items: center;
  gap: 0.25em;
  line-height: 1em;
}

.twoslash-completion-list li span.twoslash-completions-unmatched {
  color: var(--twoslash-unmatched-color);
}

.twoslash-completion-list .deprecated {
  text-decoration: line-through;
  opacity: 0.5;
}

.twoslash-completion-list li span.twoslash-completions-matched {
  color: var(--twoslash-matched-color);
}

/* Highlights */
.twoslash-highlighted {
  background-color: var(--twoslash-highlighted-bg);
  border: 1px solid var(--twoslash-highlighted-border);
  padding: 1px 2px;
  margin: -1px -3px;
  border-radius: 4px;
}

/* Icons */
.twoslash-completion-list .twoslash-completions-icon {
  color: var(--twoslash-unmatched-color);
  width: 1em;
  flex: none;
}

/* Custom Tags */
.twoslash .twoslash-tag-line {
  position: relative;
  background-color: var(--twoslash-tag-bg);
  border-left: 3px solid var(--twoslash-tag-color);
  color: var(--twoslash-tag-color);
  padding: 6px 10px;
  margin: 0.2em 0;
  display: flex;
  align-items: center;
  gap: 0.3em;
  min-width: 100%;
  width: max-content;
}

.twoslash .twoslash-tag-line .twoslash-tag-icon {
  width: 1.1em;
  color: inherit;
}

.twoslash .twoslash-tag-line.twoslash-tag-error-line {
  background-color: var(--twoslash-error-bg);
  border-left: 3px solid var(--twoslash-error-color);
  color: var(--twoslash-error-color);
}

.twoslash .twoslash-tag-line.twoslash-tag-warn-line {
  background-color: var(--twoslash-tag-warn-bg);
  border-left: 3px solid var(--twoslash-tag-warn-color);
  color: var(--twoslash-tag-warn-color);
}

.twoslash .twoslash-tag-line.twoslash-tag-annotate-line {
  background-color: var(--twoslash-tag-annotate-bg);
  border-left: 3px solid var(--twoslash-tag-annotate-color);
  color: var(--twoslash-tag-annotate-color);
}
