/* ===========================================================================
   Lore terms and their tooltips.

   This is the signature element of the site. It is also the thing most likely
   to be annoying if handled carelessly, because a dense paragraph of tagged
   terms turns reading into a minefield of accidental taps.

   Two presentations, one data source:
     pointer: fine    → hover popover
     pointer: coarse  → tap opens a bottom sheet

   A sheet rather than a popover on touch because popovers clip at viewport
   edges, land under the thumb, and are fiddly to dismiss.
   ======================================================================== */

/* --- The term itself ----------------------------------------------------- */

.lore-term {
  color: inherit;
  cursor: help;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-decoration-thickness: 1px;
  text-decoration-color: var(--accent);
  text-underline-offset: 0.2em;

  /* Grow the touch target vertically without disturbing line layout. The
     negative margin cancels the padding so surrounding text does not shift.
     --leading-body is set generously so adjacent lines' targets don't overlap. */
  padding-block: 0.45em;
  margin-block: -0.45em;

  /* Stops a long term breaking across lines mid-word, which would split the
     hit area into two disconnected rectangles. */
  white-space: nowrap;
}

/* A term inside a link is a link first. The annotator should not produce these,
   but if one slips through, make the precedence visible. */
a .lore-term {
  text-decoration-color: currentColor;
  cursor: pointer;
}

.lore-term:hover,
.lore-term[aria-expanded='true'] {
  text-decoration-style: solid;
  background: var(--accent-wash);
  border-radius: var(--radius-sm);
}

.lore-term:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-radius: var(--radius-sm);
}

/* --- Desktop popover ----------------------------------------------------- */

.lore-popover {
  position: absolute;
  z-index: var(--z-tooltip);
  max-width: min(22rem, calc(100vw - 2rem));
  padding: var(--space-xs);
  background: var(--bg-raised);
  border: 1px solid var(--rule-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  font-family: var(--font-ui);
  font-size: var(--text-sm);
  line-height: var(--leading-snug);

  opacity: 0;
  transform: translateY(4px);
  pointer-events: none;
  transition: opacity var(--dur-fast) var(--ease),
              transform var(--dur-fast) var(--ease);
}

.lore-popover[data-open] {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

/* --- Mobile bottom sheet ------------------------------------------------- */

.lore-sheet {
  position: fixed;
  inset-inline: 0;
  inset-block-end: 0;
  z-index: var(--z-sheet);

  padding: var(--space-sm) var(--gutter);
  /* Keep clear of the home indicator on iOS */
  padding-block-end: max(var(--space-sm), env(safe-area-inset-bottom));

  background: var(--bg-raised);
  border-block-start: 1px solid var(--border);
  border-start-start-radius: var(--radius-sheet);
  border-start-end-radius: var(--radius-sheet);
  box-shadow: var(--shadow-lg);

  transform: translateY(100%);
  transition: transform var(--dur) var(--ease);
  touch-action: none;
}

.lore-sheet[data-open] { transform: translateY(0); }

/* Grab handle. Signals swipe-to-dismiss without needing a label. */
.lore-sheet__handle {
  width: 2.5rem;
  height: 4px;
  margin: 0 auto var(--space-xs);
  border-radius: 2px;
  background: var(--rule-strong);
}

.lore-sheet__title {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 600;
  line-height: var(--leading-tight);
  margin-block-end: var(--space-3xs);
}

.lore-sheet__category {
  font-family: var(--font-ui);
  font-size: var(--text-xs);
  font-weight: 600;
  letter-spacing: var(--tracking-wide);
  text-transform: uppercase;
  color: var(--accent);
  margin-block-end: var(--space-2xs);
}

.lore-sheet__body {
  font-family: var(--font-ui);
  font-size: var(--text-base);
  line-height: var(--leading-snug);
  color: var(--fg-muted);
  margin-block-end: var(--space-sm);
}

.lore-sheet__link {
  display: inline-flex;
  align-items: center;
  min-height: var(--tap-min);
  font-family: var(--font-ui);
  font-weight: 600;
}

.lore-sheet-scrim {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-sheet) - 1);
  background: rgb(0 0 0 / 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--dur) var(--ease);
}
.lore-sheet-scrim[data-open] { opacity: 1; pointer-events: auto; }

/* --- Print --------------------------------------------------------------- */

@media print {
  .lore-term { text-decoration: none; padding: 0; margin: 0; }
  .lore-popover, .lore-sheet, .lore-sheet-scrim { display: none; }
}
