:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #4f46e5;
  --primary-hover: #4338ca;
  --accent: #06b6d4;
  --danger: #ef4444;
  --success: #10b981;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 4px 12px rgba(15, 23, 42, 0.04);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-alt: #334155;
    --border: #334155;
    --text: #f1f5f9;
    --muted: #94a3b8;
  }
}

/* Manual theme overrides via Settings — beat the system query above.
   Each block tweaks the surface palette plus optional --primary/--accent so the
   accent colour fits the mood of the theme. */

/* Light family */
:root[data-theme="light"] {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
}
:root[data-theme="paper"] {
  --bg: #ffffff;
  --surface: #ffffff;
  --surface-alt: #f5f5f5;
  --border: #d4d4d4;
  --text: #111111;
  --muted: #555555;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --accent: #0891b2;
}
:root[data-theme="sepia"] {
  --bg: #f4ecd8;
  --surface: #fbf6e6;
  --surface-alt: #ece2c8;
  --border: #d4c5a0;
  --text: #5b4636;
  --muted: #6f5c44; /* darkened from #8b7355 to pass WCAG AA on Sepia surface */
  --primary: #b67c3a;
  --primary-hover: #95622c;
  --accent: #a35a2c;
}
:root[data-theme="solarized-light"] {
  --bg: #fdf6e3;
  --surface: #fffbed;
  --surface-alt: #eee8d5;
  --border: #ddd6c1;
  --text: #586e75;
  --muted: #5d747a; /* darkened from #93a1a1 (which only had 2.58 contrast) */
  --primary: #268bd2;
  --primary-hover: #1e6fa9;
  --accent: #2aa198;
}
:root[data-theme="sky"] {
  --bg: #e0f2fe;
  --surface: #ffffff;
  --surface-alt: #f0f9ff;
  --border: #bae6fd;
  --text: #0c4a6e;
  --muted: #475569;
  --primary: #0284c7;
  --primary-hover: #0369a1;
  --accent: #06b6d4;
}

/* Dark family */
:root[data-theme="dark"] {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-alt: #334155;
  --border: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
}
:root[data-theme="solarized-dark"] {
  --bg: #002b36;
  --surface: #073642;
  --surface-alt: #0c4a52;
  --border: #14545d;
  --text: #eee8d5;
  --muted: #93a1a1;
  --primary: #268bd2;
  --primary-hover: #1e6fa9;
  --accent: #2aa198;
}
:root[data-theme="dracula"] {
  --bg: #282a36;
  --surface: #383a4a;
  --surface-alt: #44475a;
  --border: #565969;
  --text: #f8f8f2;
  --muted: #b8c0e0; /* lightened from #9aa1c4 to pass WCAG AA on Dracula surface */
  --primary: #bd93f9;
  --primary-hover: #a07cf0;
  --accent: #ff79c6;
}
:root[data-theme="nord"] {
  --bg: #2e3440;
  --surface: #3b4252;
  --surface-alt: #434c5e;
  --border: #4c566a;
  --text: #d8dee9;
  --muted: #b8c5d6; /* lightened from #9aaabf to pass WCAG AA on Nord surface */
  --primary: #88c0d0;
  --primary-hover: #76aebd;
  --accent: #81a1c1;
}
:root[data-theme="midnight"] {
  --bg: #000814;
  --surface: #001d3d;
  --surface-alt: #003566;
  --border: #023e8a;
  --text: #d4eaff;
  --muted: #93a8c4;
  --primary: #00b4d8;
  --primary-hover: #0096b9;
  --accent: #48cae4;
}

* { box-sizing: border-box; }

/* ── Accessibility primitives ────────────────────────────────────────────── */

/* Skip-to-content link — invisible until focused */
.skip-link {
  position: absolute;
  left: 0;
  top: -100px;
  background: var(--primary);
  color: white;
  padding: 0.6rem 1rem;
  border-radius: 0 0 8px 0;
  text-decoration: none;
  z-index: 2000;
  font-weight: 600;
  transition: top 0.15s;
}
.skip-link:focus { top: 0; outline: 3px solid var(--accent); outline-offset: 2px; }

/* Crisp focus ring on every interactive element via :focus-visible (mouse
   clicks don't trigger a ring; keyboard navigation always does). */
button:focus-visible,
a:focus-visible,
[role="tab"]:focus-visible,
[role="menuitem"]:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: 4px;
}

/* Visually-hidden helper for screen-reader-only labels */
.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0);
  white-space: nowrap; border: 0;
}

/* Honour the user's reduced-motion preference: kill all transitions,
   animations, and the drawer slide-in. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
  .nav-drawer { transition: none !important; }
}

/* Touch-target floor: 44 × 44 px on coarse pointers (phones, tablets). */
@media (pointer: coarse) {
  button, .btn, .calc-btn, .palette-cat, .palette-sym,
  .formula-cat, .train-cat, .nav-item, .alt-chip, .chem-chip,
  .train-tile, .copy-menu-item, .output-tab {
    min-height: 44px;
  }
  .calc-btn { padding: 0.7rem 0.5rem; }
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 1rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  margin-left: 0.75rem; /* breathing room between the ☰ button and the logo */
}

.btn-menu {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 1.25rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, border-color 0.15s;
}
.btn-menu:hover { background: var(--primary); color: white; border-color: var(--primary); }

.btn-help {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface-alt);
  color: var(--muted);
  font-size: 1rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  flex-shrink: 0;
  transition: all 0.15s;
}
/* The accessibility button sits left of the ? help button; give it a hair
   of right-margin so the two don't touch, and bump its icon a touch so the
   ♿ glyph reads at the same visual weight as the ? next to it. */
.btn-help.btn-a11y {
  margin-right: 6px;
  font-size: 1.05rem;
  font-weight: 400;
}

/* ── Voice-test inline status ─────────────────────────────────────────
   Sits right under the Settings voice dropdown. menu.js writes
   "Testing voice…" / "✓ works here" / "✗ doesn't speak here" into
   #voice-test-status and toggles the .ok / .bad classes for colour. */
.voice-test-status {
  font-size: 0.82rem;
  color: var(--muted);
  margin: -0.25rem 0 0.25rem;
  min-height: 1.2em;
}
.voice-test-status.ok  { color: #16a34a; }   /* green-600 */
.voice-test-status.bad { color: var(--danger); }

/* ── Help dialog ──────────────────────────────────────────────────────
   In-page <dialog> hosting an <iframe> of help.html. The iframe is
   pre-loaded after page-idle so opening the dialog is dlg.showModal()
   and feels instant. Closing is dlg.close() — iframe stays mounted so
   re-opens are also instant. */
.help-dialog {
  width: min(960px, 92vw);
  height: 88vh;
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
  overflow: hidden;
}
.help-dialog::backdrop { background: rgba(0, 0, 0, .45); }
.help-dialog-shell {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.help-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
  flex-shrink: 0;
}
.help-dialog-title { margin: 0; font-size: 1.05rem; }
.help-dialog-tools { display: flex; align-items: center; gap: 8px; }
#help-dialog-close { font-size: 18px; padding: 4px 10px; }
.help-dialog-iframe-wrap {
  flex: 1;
  position: relative;
  background: var(--surface);
}
#help-iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  background: var(--surface);
}
.help-dialog-spinner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.9rem;
  background: var(--surface);
  z-index: 1;
  transition: opacity 0.15s;
}
.help-dialog-spinner.gone {
  opacity: 0;
  pointer-events: none;
}

/* ── Calculator popup ─────────────────────────────────────────────────
   Floating panel toggled by Ctrl+Shift+C. The full Scientific calc-card
   is re-parented into here on open and moved back on close, so the
   popup width is whatever calc-card needs. Anchored top-right by
   default; the JS drag handler swaps to left/top once dragged so the
   panel stays where the user put it across reopens. */
.calc-popup {
  position: fixed;
  top: 90px;
  right: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .18);
  z-index: 1000;
  max-width: 95vw;
  max-height: 95vh;
  overflow: auto;
}
.calc-popup.hidden { display: none; }
.calc-popup-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px 8px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
  border-radius: 12px 12px 0 0;
  cursor: move;
  user-select: none;
  position: sticky;
  top: 0;
  z-index: 1;
}
.calc-popup-title { font-weight: 600; }
.calc-popup-close {
  background: transparent;
  border: 0;
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
  padding: 2px 8px;
  color: var(--muted);
  border-radius: 4px;
}
.calc-popup-close:hover { background: var(--border); color: var(--text); }
.calc-popup-body { padding: 0; }
/* When the .calc-card is re-parented in here, strip its outer card
   chrome so it doesn't look like a card-inside-a-card. The popup
   already provides the outer border, radius, and shadow. */
.calc-popup-body .editor-card,
.calc-popup-body .calc-card {
  border: none;
  border-radius: 0;
  box-shadow: none;
  margin: 0;
  background: transparent;
}

.btn-help:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}
/* Immediate visual feedback the moment the pointer goes down — the new
   tab and help page can take a moment to come up, but at least the user
   sees the button respond instantly. transition:none skips the 0.15s
   ease so the press is genuinely instant, not eased in. */
.btn-help:active,
.btn-help.loading {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
  transition: none;
}
.btn-help:active { transform: scale(0.92); }
/* While the help tab is loading, pulse a ring outwards from the button
   so the click clearly registered for the full wait, not just the
   instant the mouse was down. */
.btn-help.loading {
  animation: btnHelpPulse 0.75s ease-in-out infinite alternate;
}
@keyframes btnHelpPulse {
  from { box-shadow: 0 0 0 0 rgba(79,70,229,.55); }
  to   { box-shadow: 0 0 0 9px rgba(79,70,229,0);   }
}

.logo { width: 36px; height: 36px; }

.brand h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.tagline {
  color: var(--muted);
  font-size: 0.875rem;
}

/* Tabs are kept in DOM for handlers but visually hidden — menu drives them */
.tabs {
  display: none;
}

.tab {
  background: none;
  border: none;
  padding: 0.75rem 1.25rem;
  font-size: 0.95rem;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: all 0.2s;
  font-weight: 500;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
}

.tab-icon { font-size: 0.95em; line-height: 1; }

.tab:hover { color: var(--text); }

.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

main {
  flex: 1;
  padding: 1.5rem;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.panel { display: none; }
.panel.active { display: block; animation: fade 0.2s ease; }

@keyframes fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.editor-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.5rem;
}

.editor-card h2 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
}

.label {
  display: block;
  font-weight: 600;
  margin-bottom: 0.5rem;
  color: var(--muted);
  font-size: 0.875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.label.inline {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  text-transform: none;
  letter-spacing: normal;
  font-weight: 500;
  font-size: 0.875rem;
  margin-bottom: 0;
}

math-field {
  display: block;
  width: 100%;
  min-height: 80px;
  font-size: 1.5rem;
  padding: 1rem;
  border: 2px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  color: var(--text);
}

math-field:focus-within {
  border-color: var(--primary);
  outline: none;
}

.toolbar {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 1rem;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.btn:hover {
  background: var(--surface-alt);
  border-color: var(--muted);
}

.btn.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.btn.primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.btn.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}

.btn.ghost:hover { color: var(--danger); }

.btn.large {
  padding: 1rem 2rem;
  font-size: 1.1rem;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Copy ▾ dropdown ─────────────────────────────────────────────────────── */

.copy-menu-wrap {
  position: relative;
  display: inline-block;
}
.copy-menu {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 0.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.12);
  padding: 0.25rem;
  display: flex;
  flex-direction: column;
  min-width: 160px;
  z-index: 50;
}
.copy-menu-item {
  background: none;
  border: none;
  padding: 0.5rem 0.75rem;
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text);
  border-radius: 6px;
  white-space: nowrap;
}
.copy-menu-item:hover { background: var(--surface-alt); }

.output-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.output-tabs {
  display: flex;
  gap: 0;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
}

.output-tab {
  background: none;
  border: none;
  padding: 0.6rem 1rem;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--muted);
  cursor: pointer;
  font-family: inherit;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.output-tab:hover { color: var(--text); }
.output-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  background: var(--surface);
}

.output-panel {
  padding: 1rem 1.25rem;
}

.rendered {
  min-height: 60px;
  font-size: 1.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
}

.code {
  font-family: "SF Mono", Menlo, Monaco, Consolas, monospace;
  background: var(--surface-alt);
  padding: 0.75rem;
  border-radius: 6px;
  margin: 0;
  font-size: 0.875rem;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 200px;
  overflow-y: auto;
}

.muted { color: var(--muted); font-size: 0.9rem; }

.speak-controls {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  margin: 1rem 0;
}

.mic-icon {
  font-size: 1.3rem;
  display: inline-block;
}

#btn-mic.listening {
  background: var(--danger);
  border-color: var(--danger);
  animation: pulse 1.2s infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.5); }
  50% { box-shadow: 0 0 0 12px rgba(239, 68, 68, 0); }
}

.status {
  color: var(--muted);
  font-size: 0.9rem;
}

.status.error { color: var(--danger); }
.status.success { color: var(--success); }

.transcript-box {
  margin-top: 1.25rem;
  padding: 1rem;
  background: var(--surface-alt);
  border-radius: 8px;
}

.transcript-box h3 {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.transcript {
  font-size: 1.05rem;
  min-height: 1.5em;
  margin: 0;
}

.canvas-wrap {
  border: 2px dashed var(--border);
  border-radius: 8px;
  background: white;
  overflow: hidden;
  margin-bottom: 1rem;
  transition: border-color 0.12s ease, background-color 0.12s ease;
}

/* While the user drags a file over the canvas: highlight the dashed border
   so it's obvious the area accepts drops. */
.canvas-wrap.drop-target {
  border-color: var(--primary);
  background: var(--surface-alt, #eef2f7);
}

#draw-canvas {
  display: block;
  width: 100%;
  max-width: 100%;
  cursor: crosshair;
  touch-action: none;
}

.history-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.history-header h2 {
  margin: 0;
  flex: 1;
}

.history-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem;
  background: var(--surface-alt);
  border-radius: 8px;
  gap: 1rem;
  flex-wrap: wrap;
}

.history-item .preview {
  font-size: 1.3rem;
  flex: 1;
  min-width: 200px;
}

.history-item .meta {
  color: var(--muted);
  font-size: 0.8rem;
}

.history-item .actions {
  display: flex;
  gap: 0.5rem;
}

.empty {
  text-align: center;
  padding: 2rem;
}

.app-footer {
  text-align: center;
  padding: 1rem;
  color: var(--muted);
  font-size: 0.85rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.hidden { display: none !important; }

/* MFR AI model banner + progress */
.mfr-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: #ede9fe;
  border-radius: 8px;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
@media (prefers-color-scheme: dark) {
  .mfr-banner { background: #2e1065; }
}
.mfr-banner-text { font-size: 0.9rem; flex: 1; }

.mfr-progress-wrap {
  margin-bottom: 1rem;
}
.mfr-progress-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}
.mfr-progress-bar {
  height: 6px;
  background: var(--surface-alt);
  border-radius: 99px;
  overflow: hidden;
}
.mfr-progress-bar #mfr-progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 99px;
  transition: width 0.3s ease;
}

.draw-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.draw-header h2 { margin: 0 0 0.25rem; }

.tf-badge {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.3rem 0.7rem;
  border-radius: 99px;
  white-space: nowrap;
}

.tf-badge.loading { background: var(--surface-alt); color: var(--muted); }
.tf-badge.ready { background: #d1fae5; color: #065f46; }
.tf-badge.error { background: #fee2e2; color: #991b1b; }

@media (prefers-color-scheme: dark) {
  .tf-badge.ready { background: #064e3b; color: #6ee7b7; }
  .tf-badge.error { background: #7f1d1d; color: #fca5a5; }
}

/* Recognition card — equation is the primary content; LaTeX is secondary. */
.recog-rendered-large {
  min-height: 80px;
  padding: 1rem 0.5rem;
  font-size: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.recog-actions-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.recog-source {
  margin-top: 0.75rem;
  font-size: 0.85rem;
}
.recog-source summary {
  cursor: pointer;
  color: var(--muted);
  user-select: none;
  padding: 0.3rem 0.5rem;
  border-radius: 4px;
  font-weight: 600;
}
.recog-source summary:hover { background: var(--surface-alt); color: var(--text); }
.recog-source[open] summary { color: var(--text); }
.recog-source pre {
  margin-top: 0.5rem;
  max-width: 100%;
  white-space: pre-wrap;
  word-break: break-all;
}

/* Alternatives sit between the rendered equation and the action buttons so
   they're seen before the user hits "Send to Editor" with a wrong pick. */
.alternatives {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin: 0.6rem 0;
  padding: 0.55rem 0.7rem;
  background: var(--surface-alt);
  border-radius: 8px;
  border: 1px dashed var(--border);
}

.alternatives .muted {
  font-size: 0.85rem !important;
  font-weight: 600;
  color: var(--muted);
}

.alt-chip {
  padding: 0.45rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 99px;
  background: var(--surface);
  font-family: "SF Mono", Menlo, monospace;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.15s;
}

.alt-chip:hover, .alt-chip:focus-visible {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  outline: none;
}

.train-summary {
  font-weight: 600;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0;
}

.train-summary::-webkit-details-marker { display: none; }

.train-summary::before {
  content: "▶";
  font-size: 0.7rem;
  color: var(--muted);
  transition: transform 0.2s;
}

details[open] .train-summary::before { transform: rotate(90deg); }

.badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 0.2rem 0.5rem;
  border-radius: 99px;
  background: #ede9fe;
  color: var(--primary);
}

@media (prefers-color-scheme: dark) {
  .badge { background: #2e1065; color: #c4b5fd; }
}

.train-body {
  padding-top: 1rem;
}

.train-controls {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  align-items: flex-end;
  margin: 0.75rem 0;
}

.text-input {
  display: block;
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  color: var(--text);
  font-size: 0.95rem;
  margin-top: 0.25rem;
}

.text-input.short { width: 70px; }

.symbol-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.sym-tile {
  width: 48px;
  height: 48px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  font-weight: 600;
  cursor: default;
  position: relative;
}

.sym-tile.user { border-color: var(--primary); }

/* ── Train-picker ────────────────────────────────────────────────────────── */

.train-section-title {
  margin: 0.85rem 0 0.4rem;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
}

.train-categories {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-bottom: 0.5rem;
}
.train-cat {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.65rem;
  font-size: 0.82rem;
  cursor: pointer;
  color: var(--muted);
  font-family: inherit;
}
.train-cat:hover, .train-cat.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.train-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.25rem 0;
  min-height: 2.6rem;
}
.train-tile {
  min-width: 2.4rem;
  height: 2.4rem;
  padding: 0 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: inherit;
  transition: background 0.12s, border-color 0.12s;
}
.train-tile:hover {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.train-tile.guess {
  /* AI-suggested tiles get a subtle accent so users notice them first */
  border-color: var(--primary);
  background: var(--surface-alt);
}
.train-tile .katex { font-size: 0.92em; }

.train-advanced {
  margin-top: 0.75rem;
  padding-top: 0.5rem;
  border-top: 1px dashed var(--border);
}
.train-advanced summary {
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
  margin-bottom: 0.5rem;
}

.sym-tile .sym-label {
  position: absolute;
  bottom: 2px;
  right: 3px;
  font-size: 0.5rem;
  color: var(--muted);
}

/* ── Scientific Calculator ─────────────────────────────────────────────────── */

.calc-layout {
  display: flex;
  justify-content: center;
}

.calc-card {
  width: 100%;
  max-width: 480px;
}

.calc-display {
  background: var(--surface-alt);
  border-radius: 8px;
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
  text-align: right;
  min-height: 80px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 0.25rem;
}

.calc-expr {
  font-size: 0.9rem;
  color: var(--muted);
  min-height: 1.2em;
  word-break: break-all;
}

.calc-result {
  font-size: 2rem;
  font-weight: 600;
  word-break: break-all;
}

.calc-angle-row {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 0.75rem;
}

.calc-mode {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.6rem;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--muted);
}

.calc-mode.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.calc-mem-ind {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--primary);
  margin-left: auto;
}

.calc-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.4rem;
}

.calc-btn {
  padding: 0.7rem 0.25rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s;
  text-align: center;
}

.calc-btn:hover { background: var(--surface-alt); }

.calc-btn.fn {
  background: var(--surface-alt);
  font-size: 0.82rem;
}

.calc-btn.num { background: var(--surface); font-size: 1.1rem; font-weight: 600; }

.calc-btn.op {
  background: #ede9fe;
  color: var(--primary);
  border-color: #c4b5fd;
  font-size: 1.1rem;
}

@media (prefers-color-scheme: dark) {
  .calc-btn.op { background: #2e1065; border-color: #4c1d95; }
}

.calc-btn.primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
  font-size: 1.2rem;
}

.calc-btn.primary:hover { background: var(--primary-hover); }

.calc-btn.danger {
  background: #fee2e2;
  color: #b91c1c;
  border-color: #fca5a5;
}

@media (prefers-color-scheme: dark) {
  .calc-btn.danger { background: #7f1d1d; color: #fca5a5; border-color: #991b1b; }
}

.calc-btn.zero { grid-column: span 2; }
.calc-btn.equals { grid-column: span 4; }

.calc-footer-row {
  margin-top: 1rem;
  display: flex;
  justify-content: flex-end;
}

/* ── Graphing Calculator ────────────────────────────────────────────────────── */

.graph-header {
  margin-bottom: 1rem;
}

.graph-header h2 { margin: 0 0 0.75rem; }

.graph-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.graph-range-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.graph-mode-row {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.active-toggle {
  background: var(--primary) !important;
  color: white !important;
  border-color: var(--primary) !important;
}

.graph-add-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.5rem;
}

.graph-coords {
  font-family: "SF Mono", Menlo, Monaco, Consolas, monospace;
  font-size: 0.8rem;
  color: var(--muted);
  text-align: right;
  padding: 0.25rem 0;
}

.graph-exprs {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.graph-expr-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.graph-swatch {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
}

.graph-fn-input { flex: 1; margin-top: 0; }

.graph-del { padding: 0.4rem 0.6rem; }

.graph-plot {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  margin: 1rem 0;
  background: white;
}

.graph-plot svg { display: block; }

/* ── Chemistry panel ──────────────────────────────────────────────────────── */

.chem-preview {
  min-height: 3.5rem;
  padding: 1rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin: 0.75rem 0 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  overflow-x: auto;
}

.chem-chips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
  margin: 0.75rem 0;
}

.chem-group-lbl {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0 0.25rem;
}

.chem-chip {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.6rem;
  font-size: 0.88rem;
  cursor: pointer;
  color: var(--text);
  transition: background 0.15s, border-color 0.15s;
}

.chem-chip:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

/* ── Physics palette ──────────────────────────────────────────────────────── */

.physics-palette {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  margin: 0.5rem 0;
}

.palette-cats {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-bottom: 0.75rem;
}

.palette-cat {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.25rem 0.65rem;
  font-size: 0.8rem;
  cursor: pointer;
  color: var(--muted);
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.palette-cat:hover,
.palette-cat.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.palette-symbols {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.palette-sym {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.5rem;
  min-width: 2.4rem;
  font-size: 0.95rem;
  cursor: pointer;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
  line-height: 1;
}

.palette-sym:hover {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

.palette-sym .katex { font-size: 1em; }

@media (max-width: 640px) {
  .app-header { padding: 0.75rem 1rem; }
  main { padding: 1rem; }
  .editor-card { padding: 1rem; }
  math-field { font-size: 1.25rem; }
  .btn.large { padding: 0.75rem 1.5rem; font-size: 1rem; }
  .calc-btn { padding: 0.6rem 0.2rem; font-size: 0.85rem; }
  .calc-btn.fn { font-size: 0.72rem; }
}

/* ── Formula library modal ────────────────────────────────────────────────── */

.formula-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.formula-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
}

.formula-dialog {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.3);
  width: min(960px, 100%);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.formula-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.formula-header h2 {
  margin: 0;
  font-size: 1.15rem;
  white-space: nowrap;
}

.formula-header .text-input {
  flex: 1;
  margin-top: 0;
}

/* ── Camera modal (Take Photo) ──────────────────────────────────────── */
.camera-modal {
  position: fixed; inset: 0; z-index: 9000;
  display: flex; align-items: center; justify-content: center;
}
.camera-modal.hidden { display: none; }
.camera-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.6);
}
.camera-dialog {
  position: relative;
  width: min(92vw, 720px);
  max-height: 90vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.camera-header {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.camera-header h2 {
  margin: 0; font-size: 1.15rem; flex: 1;
}
.camera-body {
  padding: 1rem;
  display: flex; flex-direction: column; align-items: center; gap: 0.6rem;
  background: var(--surface-alt);
}
.camera-body video,
.camera-body canvas {
  width: 100%;
  max-height: 60vh;
  background: #000;
  border-radius: 8px;
  object-fit: contain;
}
.camera-body .hidden { display: none; }
#camera-status {
  margin: 0; font-size: 0.9rem; text-align: center;
}
.camera-actions {
  display: flex; gap: 0.5rem; justify-content: flex-end;
  padding: 0.85rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.camera-actions .hidden { display: none; }

.formula-options {
  padding: 0.55rem 1.25rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
  font-size: 0.9rem;
}
.formula-options .label.inline {
  margin: 0; cursor: pointer; user-select: none;
}

.formula-body {
  display: flex;
  flex: 1;
  min-height: 0;
}

.formula-cats {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  padding: 1rem 0.75rem;
  border-right: 1px solid var(--border);
  background: var(--surface-alt);
  min-width: 160px;
  overflow-y: auto;
}

.formula-cat {
  text-align: left;
  background: none;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 0.4rem 0.75rem;
  font-size: 0.88rem;
  cursor: pointer;
  color: var(--text);
}

.formula-cat:hover { background: var(--surface); }
.formula-cat.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.formula-list {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem;
}

.formula-cat-heading {
  margin: 0 0 0.5rem;
  padding-top: 0.5rem;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.formula-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 0.6rem;
  margin-bottom: 1rem;
}

.formula-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.85rem;
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-family: inherit;
  color: var(--text);
  transition: border-color 0.12s, box-shadow 0.12s;
  min-height: 90px;
}

.formula-card:hover {
  border-color: var(--primary);
  box-shadow: 0 2px 8px rgba(79, 70, 229, 0.12);
}

.formula-card-name {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
}

.formula-card-preview {
  flex: 1;
  display: flex;
  align-items: center;
  font-size: 0.95rem;
  overflow-x: auto;
}

.formula-card-preview .katex { font-size: 0.92em; }

.formula-card-notes {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.15rem;
}

.formula-empty {
  padding: 1.5rem;
  text-align: center;
}

@media (max-width: 700px) {
  .formula-body { flex-direction: column; }
  .formula-cats {
    flex-direction: row;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    min-width: 0;
  }
  .formula-cat { white-space: nowrap; }
}

/* ── Autocomplete popup ──────────────────────────────────────────────────── */

.autocomplete-popup {
  position: absolute;
  z-index: 200;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.12);
  padding: 0.25rem;
  display: flex;
  flex-direction: column;
  max-height: 320px;
  overflow-y: auto;
  min-width: 240px;
  font-size: 0.92rem;
}

.autocomplete-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.4rem 0.6rem;
  background: none;
  border: none;
  cursor: pointer;
  text-align: left;
  border-radius: 6px;
  font-family: inherit;
  font-size: inherit;
  color: var(--text);
}

.autocomplete-row:hover, .autocomplete-row.active {
  background: var(--surface-alt);
}
.autocomplete-row.active {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

.autocomplete-key {
  font-family: "SF Mono", Menlo, Monaco, Consolas, monospace;
  font-size: 0.82rem;
  color: var(--muted);
  min-width: 70px;
  flex-shrink: 0;
}

.autocomplete-display {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.autocomplete-display .katex {
  font-size: 1em;
}

/* ── Drawing — correction popup ────────────────────────────────────────── */

.correction-popup {
  position: fixed;
  z-index: 300;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.15);
  padding: 0.25rem;
  display: flex;
  flex-direction: column;
  min-width: 160px;
}

.correction-row {
  background: none;
  border: none;
  padding: 0.4rem 0.65rem;
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.88rem;
  color: var(--text);
  border-radius: 4px;
}

.correction-row:hover { background: var(--surface-alt); }

/* ── Hamburger drawer ────────────────────────────────────────────────────── */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
  z-index: 800;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }

.nav-drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 260px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  box-shadow: 4px 0 16px rgba(15, 23, 42, 0.08);
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.22s ease;
  z-index: 850;
}
.nav-drawer.open { transform: translateX(0); }

.nav-drawer-header {
  padding: 1.25rem 1rem 0.75rem;
  border-bottom: 1px solid var(--border);
}
.nav-drawer-title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  font-weight: 600;
}

.nav-list {
  flex: 1;
  padding: 0.5rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.nav-drawer-footer {
  border-top: 1px solid var(--border);
  padding: 0.5rem;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.65rem 0.85rem;
  background: none;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.95rem;
  color: var(--text);
  text-align: left;
  width: 100%;
}
.nav-item:hover { background: var(--surface-alt); }
.nav-item.active {
  background: var(--primary);
  color: white;
}
.nav-item-icon {
  font-size: 1.05rem;
  width: 1.5em;
  text-align: center;
}

/* ── Settings modal ──────────────────────────────────────────────────────── */

.settings-modal {
  position: fixed;
  inset: 0;
  z-index: 1100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.settings-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.5); }
.settings-dialog {
  position: relative;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 20px 60px rgba(15, 23, 42, 0.3);
  width: min(560px, 100%);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}
.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}
.settings-header h2 { margin: 0; font-size: 1.15rem; }
.settings-body {
  padding: 1rem 1.25rem;
  overflow-y: auto;
}
.settings-group { margin-bottom: 1.25rem; }
.settings-group h3 {
  margin: 0 0 0.4rem;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.settings-help {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
}
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--border);
}
.settings-row:last-child { border-bottom: none; }
.settings-row select, .settings-row input[type="number"], .settings-row input[type="range"] {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.35rem 0.5rem;
  color: var(--text);
  font-family: inherit;
}
.settings-footer {
  display: flex;
  justify-content: flex-end;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
}

/* Spoken-view word highlighting. While TTS speaks the equation, each word in
   the prosody-processed Spoken text is wrapped in a <span class="spoken-word">.
   The currently-spoken word gets `.speaking` toggled on it — a red underline
   that follows the audio playhead. The underline is thicker than the default
   so it remains readable through the body-text colour and works for users
   with reduced contrast preferences. */
.spoken-word {
  border-radius: 2px;
}
.spoken-word.speaking {
  text-decoration: underline;
  text-decoration-color: var(--danger);
  text-decoration-thickness: 2px;
  text-underline-offset: 3px;
}

/* Speech-control buttons (Read aloud / Pause / Resume) swap their icon and
   label dynamically. While speech is active a translucent pulse on the
   primary tone signals it's running. */
[data-speak-control].is-speaking {
  background: color-mix(in srgb, var(--primary) 12%, transparent);
}

/* Equation-mirror highlight panel. Only shown while Read aloud is running.
   The KaTeX render here is the same equation as the live mathfield above,
   but emitted with \htmlClass{eq-grp grp-N}{…} around each major group
   (fraction, radical, brackets). The speech controller toggles
   .speaking-group on each wrapper as TTS enters / exits the group. */
.highlight-eq {
  margin: 0.5rem 0 0;
  padding: 0.6rem 1rem;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  text-align: center;
  /* Smooth appearance when toggling .hidden off via JS. */
  transition: opacity 0.15s ease;
}
/* IDLE state — no styling at all. Plain `.eq-grp` / `.eq-opd` / `.eq-atom`
   spans are invisible wrappers; KaTeX renders the math glyphs through
   them as if they weren't there. We intentionally apply NO border, NO
   outline, NO display change — those would either show through as faint
   artefacts or shift layout when the speaking-* state toggled.
   .eq-grp, .eq-opd, .eq-atom { (no rules) } */

/* ACTIVE state. Use OUTLINE (drawn outside the box without displacing
   content) so toggling doesn't shift the math layout, and apply
   text-decoration for the atom underline (also non-layout). The outline
   takes the shape of KaTeX's nested boxes, giving the natural "bumps"
   for super/subscripts. */
.eq-grp.speaking-group,
.eq-opd.speaking-operand {
  /* Outline sits further from the term so the atom underline inside
     doesn't visually merge with the bottom edge of the surround. */
  outline: 2px solid var(--danger);
  outline-offset: 4px;
  border-radius: 6px;
}
.eq-atom.speaking-atom {
  /* Underline below the atom without disturbing layout. We can't use
     text-decoration: underline — KaTeX nests each glyph inside several
     layout spans (.mord, .base, .vlist-r, …) and on some browsers
     (notably WebView2 / Edge) the underline doesn't propagate down
     so nothing was visible. We also can't use box-shadow with
     display:inline-block — that switches the atom to a block-like
     inline layout, which throws off the bounding box of the
     surrounding .speaking-group's outline (term rectangle drifts off
     the actual term). Solution: position:relative + ::after absolute
     underline. position:relative on an inline element doesn't change
     its box display mode, so the group rectangle stays accurate. */
  position: relative;
}
.eq-atom.speaking-atom::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: -1px;
  height: 2px;
  background: var(--danger);
  border-radius: 1px;
  pointer-events: none;
}

/* In-situ Editor swap during Read aloud. The live MathLive editor and the
   read-only KaTeX mirror occupy the same visual slot in the editor card:
   the mathfield disappears while speech is running and the mirror takes
   its place at a similar display size. When speech ends, the mathfield
   comes back. (Click-anywhere-silence handles the user wanting to edit
   mid-speech: the click cancels speech, the mathfield reappears, edit
   continues naturally.) */
.editor-card.speech-active #mathfield {
  display: none;
}
.editor-card.speech-active #highlight-eq {
  /* Match the rough visual presence of the math-field: prominent
     centred render with enough vertical space for the largest formula. */
  margin: 0;
  padding: 1rem 1rem;
  min-height: 80px;
  font-size: 1.25em;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
}
