/* math-chip: a small chip beside an element that opens "the math behind it".
 * Markup (see math-chip.js):
 *   <button class="math-chip" type="button" data-math="KEY"
 *           aria-expanded="false" aria-haspopup="dialog">∑</button>
 * The glyph names the topic (∑ inside, ∂ the wall, ≤ a bound, i this one…),
 * so two chips side by side never look the same.
 * Place a chip as a sibling beside its control, with a gap, never on top of it.
 * The card is one shared modal <div class="math-pop" role="dialog"> over a light
 * scrim, built by the script: a bar with the close button (and Back, on a
 * page), a title (h2), the text, an optional computed line (.math-live) and a
 * footer line (p.math-src). */
.math-chip {
  position: relative;
  display: inline-grid; place-items: center;
  width: 22px; height: 22px; padding: 0; margin: 0;
  border-radius: 999px;
  border: 1px solid rgba(255, 211, 110, 0.55);
  background: rgba(255, 211, 110, 0.08);
  color: #ffd36e;
  font: 700 13px/1 "Manrope", "Avenir Next", "Segoe UI", sans-serif;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, box-shadow 0.15s ease, transform 0.12s ease;
}
/* A 44 px touch box around the small ring. */
.math-chip::after { content: ""; position: absolute; inset: -11px; }
.math-chip:hover, .math-chip[aria-expanded="true"] { background: rgba(255, 211, 110, 0.2); box-shadow: 0 0 12px rgba(255, 211, 110, 0.4); }
.math-chip:active { transform: scale(0.92); }
.math-chip:focus-visible { outline: 2px solid #ffd36e; outline-offset: 2px; }
.math-chip[hidden] { display: none; }

.math-scrim {
  position: fixed; inset: 0; z-index: 10001;
  background: rgba(5, 8, 12, 0.42);
}
.math-scrim[hidden] { display: none; }
/* The language switch stays usable over the scrim: switching keeps the card
   open. (Every lab's switch is fixed, absolute or a grid item, so z-index applies.) */
body:has(.math-pop:not([hidden])) .langSel { z-index: 10003; }
.math-pop {
  position: fixed; z-index: 10002;
  width: min(360px, calc(100vw - 24px));
  max-height: calc(100vh - 24px); overflow: auto;
  padding: 14px 16px 12px;
  border-radius: 14px;
  background: rgba(13, 17, 23, 0.98);
  border: 1px solid rgba(255, 211, 110, 0.45);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.55), 0 0 22px rgba(255, 211, 110, 0.12);
  color: #d8f6ff;
  font: 400 13.5px/1.5 "Manrope", "Avenir Next", "Segoe UI", sans-serif;
}
.math-pop.sheet {
  left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  width: auto; max-height: 62vh;
}
.math-pop[hidden] { display: none; }
.math-pop:focus { outline: none; }
/* The close button, and on a page Back, sit in a bar pinned to the top of the card: they stay in view while a long card scrolls
   under them. The bar spans the card (the margins reach into its padding; a sticky box sticks at the padding's inner edge, hence
   the offset), so they are placed from its corners. On a card it has no height: the close button floats beside the title. On a
   page it is a row of its own, which hides what scrolls under it. */
.math-top { position: sticky; top: -14px; z-index: 3; height: 0; margin: -14px -16px 14px; }
.math-top.with-back {
  box-sizing: border-box; height: 42px; margin-bottom: 0;
  background: #0d1117; border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}
.math-close {
  position: absolute; top: 6px; right: 6px;
  width: 36px; height: 36px; padding: 0;
  display: grid; place-items: center;
  border: 0; border-radius: 999px; background-color: #0d1117;
  color: rgba(216, 246, 255, 0.7); cursor: pointer;
}
.math-close::after { content: ""; position: absolute; inset: -4px; }
.math-close svg { width: 18px; height: 18px; }
.math-close:hover { color: #fff; background-image: linear-gradient(rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.08)); }
.math-close:focus-visible { outline: 2px solid #ffd36e; outline-offset: 1px; }
.math-back {
  position: absolute; top: 10px; left: 10px;
  display: inline-flex; align-items: center; gap: 2px;
  max-width: calc(100% - 64px); margin: 0; padding: 4px 10px 4px 2px;
  border: 0; border-radius: 999px; background-color: #0d1117;
  font-family: inherit; font-size: 12px; font-weight: 700; line-height: 1.2;
  color: rgba(216, 246, 255, 0.75); cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.math-back::after { content: ""; position: absolute; inset: -8px -4px; }
.math-back svg { width: 18px; height: 18px; flex: none; }
.math-back span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.math-back:hover { color: #fff; background-image: linear-gradient(rgba(255, 255, 255, 0.08), rgba(255, 255, 255, 0.08)); }
.math-back:focus-visible { outline: 2px solid #ffd36e; outline-offset: 1px; }
.math-pop h2 { margin: 0 34px 6px 0; font-size: 14px; font-weight: 800; color: #ffd36e; letter-spacing: 0.01em; }
.math-pop p { margin: 0 0 8px; }
.math-pop .math-lead { font-weight: 700; color: #eefbff; }
.math-pop .math-body { margin: 0 0 8px; }
.math-pop .math-live { margin: 0 0 4px; font: 12.5px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; color: #7fe7b8; }
.math-pop i { font-family: "Times New Roman", serif; font-size: 1.08em; }
.math-pop sub, .math-pop sup { font-size: 0.72em; }
.math-pop .live { display: block; margin-top: 8px; font: 12.5px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; color: #7fe7b8; }
.math-pop .math-src {
  margin: 8px 0 0; padding-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  font: 600 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: rgba(216, 246, 255, 0.55);
}
@media (prefers-reduced-motion: no-preference) {
  .math-pop:not([hidden]) { animation: mathPopIn 0.16s ease-out; }
  @keyframes mathPopIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
}
