/* ==========================================================================
   OgbonMath — Math Input Helper
   --------------------------------------------------------------------------
   A small "fx" button that sits next to any math-capable input. Click it
   (or Ctrl+M when focused) to open a template picker. Selected template
   inserts at cursor, wrapping selection in @@...@@ if needed.

   Components:
     .mh-trigger        — the small "fx" button adorning an input
     .mh-popover        — floating panel anchored to the input
     .mh-grid           — 3-column grid of template buttons
     .mh-tpl-btn        — single template tile (glyph + caption)
     .mh-popover-caret  — little tail pointing at the trigger
   ========================================================================== */

/* ==========================================================================
   Trigger button — sits inline next to an input
   ==========================================================================
*/
.mh-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  padding: 0;
  background: var(--bg2);
  border: 1px solid var(--bdr);
  border-radius: var(--radius-md);
  color: var(--tx2);
  font-family: var(--font-serif);
  font-size: 14px;
  font-style: italic;
  font-weight: var(--fw-semibold);
  line-height: 1;
  cursor: pointer;
  transition: color var(--dur-fast) var(--ease-out),
              border-color var(--dur-fast) var(--ease-out),
              background var(--dur-fast) var(--ease-out);
}

.mh-trigger::before {
  content: "ƒx";
  letter-spacing: -0.5px;
}

.mh-trigger:hover {
  color: var(--ac);
  border-color: var(--ac);
  background: var(--ac-soft);
}

.mh-trigger:focus-visible {
  outline: 2px solid var(--ac);
  outline-offset: 2px;
}

.mh-trigger.is-open {
  color: var(--ac);
  border-color: var(--ac);
  background: var(--ac-soft);
}

/* When the trigger is attached inside a flex row, make sure it aligns
   with text inputs of various heights. */
.mh-trigger[data-size="sm"] { width: 28px; height: 28px; font-size: 13px; }
.mh-trigger[data-size="lg"] { width: 36px; height: 36px; font-size: 15px; }

/* ==========================================================================
   Popover — floating picker
   ==========================================================================
*/
.mh-popover {
  position: absolute;
  z-index: var(--z-popover, 1040);
  width: 280px;
  max-width: calc(100vw - var(--sp-4));
  background: var(--bg2);
  border: 1px solid var(--bdr);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  padding: var(--sp-3);
  opacity: 0;
  transform: translateY(-4px);
  animation: mh-pop-in var(--dur-fast) var(--ease-calm) forwards;
  /* Prevent flashing over content: use CSS containment */
  contain: layout;
}

.mh-popover.closing {
  animation: mh-pop-out var(--dur-fast) var(--ease-calm) forwards;
}

@keyframes mh-pop-in {
  from { opacity: 0; transform: translateY(-4px); }
  to   { opacity: 1; transform: translateY(0); }
}

@keyframes mh-pop-out {
  from { opacity: 1; transform: translateY(0); }
  to   { opacity: 0; transform: translateY(-4px); }
}

.mh-popover-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: var(--sp-2);
  padding: 0 2px;
}

.mh-popover-title {
  font-family: var(--font-sans);
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  letter-spacing: var(--ls-widest);
  text-transform: uppercase;
  color: var(--tx3);
}

.mh-popover-hint {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--tx3);
  opacity: 0.7;
}

/* ==========================================================================
   Template grid
   ==========================================================================
*/
.mh-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.mh-tpl-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 10px 4px;
  background: var(--bg);
  border: 1px solid var(--bdr);
  border-radius: var(--radius-md);
  color: var(--tx);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-out),
              background var(--dur-fast) var(--ease-out),
              transform var(--dur-fast) var(--ease-out);
}

.mh-tpl-btn:hover {
  border-color: var(--ac);
  background: var(--ac-soft);
}

.mh-tpl-btn:active {
  transform: scale(var(--active-scale, 0.96));
}

.mh-tpl-btn:focus-visible {
  outline: 2px solid var(--ac);
  outline-offset: -1px;
}

.mh-tpl-glyph {
  font-family: var(--font-serif);
  font-size: 18px;
  font-style: italic;
  line-height: 1;
  color: var(--tx);
}

.mh-tpl-caption {
  font-family: var(--font-sans);
  font-size: 9px;
  letter-spacing: var(--ls-wide);
  text-transform: uppercase;
  color: var(--tx3);
}

/* ==========================================================================
   Footer hint — small "Powered by KaTeX" line
   ==========================================================================
*/
.mh-popover-footer {
  margin-top: var(--sp-2);
  padding-top: var(--sp-2);
  border-top: 1px dashed var(--bdr);
  font-family: var(--font-sans);
  font-size: 10px;
  color: var(--tx3);
  text-align: center;
}

.mh-popover-footer code {
  font-family: var(--font-mono);
  font-size: 10px;
  background: var(--bg);
  padding: 1px 5px;
  border-radius: 3px;
  color: var(--tx2);
}

/* ==========================================================================
   Mobile — full-width bottom sheet style
   ==========================================================================
*/
@media (max-width: 560px) {
  .mh-popover {
    position: fixed;
    left: var(--sp-2);
    right: var(--sp-2);
    top: auto;
    bottom: calc(var(--sp-4) + env(safe-area-inset-bottom, 0px));
    width: auto;
    max-width: none;
  }
  .mh-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

/* ==========================================================================
   Reduced motion
   ==========================================================================
*/
@media (prefers-reduced-motion: reduce) {
  .mh-popover,
  .mh-popover.closing {
    animation-duration: 0.01ms !important;
  }
  .mh-tpl-btn:active { transform: none; }
}
