/* Research — the citation button.
 *
 * One component, used wherever a claim on the site has a source behind it. The button
 * says "Research" because that is what it opens: not a tooltip, not a footnote, the
 * actual paper, with its DOI.
 *
 * Rules it follows (DESIGN-SYSTEM.md):
 *   - colour comes from the tokens, nothing new is introduced
 *   - the only motion is the popover showing a state change, and it is inside
 *     prefers-reduced-motion: no-preference
 *   - the reduced-motion path is the same panel with no transition, not a blank one
 */

.research-wrap {
  position: relative;
  display: inline-block;
  /* The gap between prose and the button lives here, so the panel can align to
     the button's left edge (left: 0) instead of to the text it follows. */
  margin-left: 7px;
}

.research {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 10px 2px 8px;
  font-family: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10px;
  font-weight: 500;
  line-height: 1.6;
  letter-spacing: 0.03em;
  vertical-align: 2px;
  color: var(--ink-muted, #55685F);
  background: transparent;
  border: 1px solid var(--line, #D9E4DD);
  border-radius: 999px;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  transition: color 120ms ease, border-color 120ms ease, background-color 120ms ease;
}

.research:hover,
.research[aria-expanded="true"] {
  color: var(--sage-deep, #2E7D32);
  border-color: var(--sage, #2F7A45);
  background: var(--sage-pale, #DFF0E3);
}

.research:focus-visible {
  outline: 2px solid var(--sage, #2F7A45);
  outline-offset: 2px;
}

/* The book is injected by research.js so the markup stays a plain button. */
.research .research-icon {
  width: 11px;
  height: 11px;
  flex: none;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.15;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.9;
}

/* ---- the panel ---- */
/* Popover mode. The panel is in the top layer, so no card's overflow or stacking
   context can clip it; the UA's default inset/margin have to be undone first. */
.research-pop[popover] {
  position: fixed;
  inset: auto;
  margin: 0;
}

/* Pointing up at the button when the panel is flipped above it. */
.research-wrap[data-side="top"] .research-pop::before {
  top: auto;
  bottom: -6px;
  transform: rotate(225deg);
}


.research-pop {
  position: absolute;
  top: calc(100% + 9px);
  left: 0;
  z-index: 70;
  box-sizing: border-box;
  width: min(370px, calc(100vw - 28px));
  padding: 13px 38px 14px 15px;
  text-align: left;
  background: var(--cream, #FFFCF7);
  border: 1px solid var(--line, #D9E4DD);
  border-left: 3px solid var(--sage, #2F7A45);
  border-radius: 3px 10px 10px 3px;
  box-shadow:
    0 26px 50px -30px rgba(18, 32, 25, 0.5),
    0 2px 6px rgba(18, 32, 25, 0.05);
}

.research-pop[hidden] {
  display: none;
}

/* the little caret, so it is obviously attached to its button */
.research-pop::before {
  content: "";
  position: absolute;
  top: -6px;
  left: var(--caret, 20px);
  width: 10px;
  height: 10px;
  background: var(--cream, #FFFCF7);
  border-left: 1px solid var(--line, #D9E4DD);
  border-top: 1px solid var(--line, #D9E4DD);
  transform: rotate(45deg);
}

.research-wrap[data-flip="1"] .research-pop {
  left: auto;
  right: 0;
}

.research-wrap[data-flip="1"] .research-pop::before {
  left: auto;
  right: 16px;
}

.research-kicker {
  display: block;
  margin-bottom: 5px;
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-size: 8.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-muted, #55685F);
}

.research-cite {
  display: block;
  margin: 0;
  font-family: 'Newsreader', Georgia, 'Times New Roman', serif;
  font-size: 15px;
  line-height: 1.5;
  color: var(--ink, #122019);
}

.research-also {
  display: block;
  margin: 7px 0 0;
  font-size: 12px;
  line-height: 1.45;
  color: var(--ink-muted, #55685F);
}

.research-link {
  display: inline-block;
  margin-top: 9px;
  font-family: 'IBM Plex Mono', ui-monospace, monospace;
  font-size: 10.5px;
  letter-spacing: 0.02em;
  color: var(--sage-deep, #2E7D32);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.research-link:hover {
  color: var(--grass-hover, #256428);
}

.research-close {
  position: absolute;
  top: 7px;
  right: 7px;
  width: 24px;
  height: 24px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 15px;
  line-height: 1;
  color: var(--ink-muted, #55685F);
  background: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}

.research-close:hover {
  color: var(--ink, #122019);
  background: var(--sage-pale, #DFF0E3);
}

.research-close:focus-visible {
  outline: 2px solid var(--sage, #2F7A45);
  outline-offset: 1px;
}

@media (prefers-reduced-motion: no-preference) {
  .research-pop {
    animation: research-in 130ms ease-out;
  }

  /* Only the offset moves. The panel is never transparent: if an animation is
     stalled — a screenshot, a print, a browser with motion frozen mid-frame — an
     opacity-from-zero entrance leaves the citation invisible while the button says
     it is open. Four pixels of travel is enough to show the state change. */
  @keyframes research-in {
    from {
      transform: translateY(-4px);
    }
    to {
      transform: translateY(0);
    }
  }
}

/* On the dark bands the pill has to invert, or it disappears into the background. */
.research--on-dark {
  color: #C3D6CB;
  border-color: rgba(255, 255, 255, 0.32);
}

.research--on-dark:hover,
.research--on-dark[aria-expanded="true"] {
  color: #FFFFFF;
  border-color: rgba(255, 255, 255, 0.6);
  background: rgba(255, 255, 255, 0.09);
}

.research--on-dark:focus-visible {
  outline-color: #FFFFFF;
}

.research--on-dark .research-icon {
  opacity: 1;
}

/* In the app's findings panel the button sits on its own line, under the attribution,
   instead of inline at the end of prose. */
.rv-attr + .research-wrap,
.rv-full + .research-wrap {
  margin-left: 0;
  margin-bottom: 9px;
}

@media (max-width: 420px) {
  .research-wrap {
    margin-left: 5px;
  }
}
