/*
 * T177.2 hover-defs styles.
 *
 * Pairs with hover-defs.js to render an accessible glossary popover
 * on <dfn data-term="key">. Desktop hover and focus-visible are pure
 * CSS (no JS required); touch tap-to-reveal is driven by the .is-open
 * class set by the JS runtime.
 *
 * Theme: reuses lesson.css custom properties (--accent, --bg, --ink,
 * --rule, --mono, --serif). No new color tokens.
 *
 * Theme tokens (T208.3): the popover background is themed via
 * --popover-bg so it never renders as dark text on a dark box.
 * Light theme uses a cream surface (--bg-2 #FAF5EB) with the dark
 * --ink (#1A1330) text. Dark theme keeps the original near-black
 * #1a1a1a with light --ink (#e8e6df) text. Both directions clear
 * WCAG AA 4.5:1: dark ink on cream is ~14:1, light ink on near-black
 * is ~13.4:1. Defined here (not lesson.css) to keep the fix self-
 * contained to the hover-defs surface this file owns.
 *
 * Tier-0 deterrent (T157.1): body retains user-select: none. The
 * popover content explicitly opts back into selectable text so
 * screen-reader and read-aloud users can highlight definitions.
 */

:root {
  --popover-bg: #FAF5EB;
}

html[data-theme="dark"] {
  --popover-bg: #1a1a1a;
}

dfn[data-term] {
  position: relative;
  font-style: normal;
  color: inherit;
  border-bottom: 1px dotted var(--accent);
  cursor: help;
  outline: none;
}

dfn[data-term]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hover-def-popover {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 10px);
  transform: translateX(-50%);
  z-index: 50;
  min-width: 200px;
  max-width: 320px;
  padding: 10px 14px;
  background: var(--popover-bg);
  color: var(--ink);
  border: 1px solid var(--rule);
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
  text-align: left;
  font-style: normal;
  user-select: text;
  -webkit-user-select: text;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition:
    opacity 120ms ease-out,
    transform 120ms ease-out;
}

.hover-def-popover::before {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-right: 6px solid transparent;
  border-top: 6px solid var(--popover-bg);
}

/* Desktop hover and focus path: pure CSS, no JS required. */
@media (hover: hover) and (pointer: fine) {
  dfn[data-term]:hover .hover-def-popover,
  dfn[data-term]:focus-visible .hover-def-popover {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translateX(-50%) translateY(-2px);
  }
}

/* Touch / keyboard JS-driven open state. */
dfn[data-term].is-open .hover-def-popover {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateX(-50%) translateY(-2px);
}

/* Reduced-motion respect: instant show / hide. */
@media (prefers-reduced-motion: reduce) {
  .hover-def-popover {
    transition: none;
  }
}

/* T213.1 — viewport clamp. On a phone the centered popover (min-width 200px,
 * max-width 320px) is wider than the gutter beside a dfn near the screen edge
 * and spills off-screen (~22px measured on L01 at 390px). Cap its width to the
 * viewport minus a small gutter so it can never be wider than the screen; the
 * hover-defs.js runtime then nudges its horizontal offset back on-screen for
 * the tap-reveal (touch) path. */
@media (max-width: 640px) {
  .hover-def-popover {
    max-width: min(320px, calc(100vw - 24px));
  }
}
