* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: #0a0a12;
  color: #e0e0e0;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 24px;
  overflow: hidden;
}

.app {
  width: 100%;
  max-width: 1200px;           /* a tablet's whole width; huge screens stay comfortable */
  height: 100%;               /* no height cap: the canvas takes what the screen has */
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ── Header ─────────────────────────────────────────────────── */

header { text-align: center; flex-shrink: 0; }
.logo { display: flex; align-items: center; justify-content: center; gap: 10px; }
.title-github { color: #555; display: inline-flex; padding: 2px; }
.title-github:hover { color: #bbb; }
h1 {
  font-size: 32px; font-weight: 800; color: #fff;
  letter-spacing: -1px;
  background: linear-gradient(135deg, #8b5cf6, #6366f1, #3b82f6);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
}
.subtitle { font-size: 15px; color: #666; margin-top: 6px; font-weight: 400; }

/* ── Canvas ─────────────────────────────────────────────────── */

.canvas-area { position: relative; flex: 1; min-height: 0; touch-action: none; }
.canvas-scroll {
  background: #ffffff;
  border-radius: 16px;
  overflow: hidden;
  height: 100%;
  touch-action: none;
  box-shadow:
    0 0 0 1px rgba(255,255,255,0.06),
    0 8px 40px rgba(0,0,0,0.4),
    0 0 80px rgba(139, 92, 246, 0.08);
}
.canvas-wrap { position: relative; width: 100%; height: 100%; touch-action: none; }
.canvas-wrap canvas { position: absolute; top: 0; left: 0; }
#drawCanvas { background: transparent; cursor: crosshair; touch-action: none; }
#overlayCanvas { pointer-events: none; touch-action: none; }

.tools {
  position: absolute;
  top: 12px; right: 12px;
  display: flex; gap: 4px;
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(8px);
  border-radius: 10px;
  padding: 4px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  z-index: 10;
}
.tool-btn {
  width: 34px; height: 34px;
  border-radius: 8px; border: none;
  background: transparent; color: #888;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
}
.tool-btn:hover { background: rgba(0,0,0,0.06); color: #333; }
.tool-btn.active { background: #3b82f6; color: #fff; }
#eraseModeBtn.active { background: #ef4444; color: #fff; }
.tool-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.tool-btn.zoom-btn { width: auto; min-width: 44px; padding: 0 6px; font-size: 11px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tool-sep { width: 1px; background: #e0e0e0; margin: 4px 2px; }

/* ── Result card ────────────────────────────────────────────── */

.result-card {
  background: #12121e;
  border: 1px solid #1e1e30;
  border-radius: 16px;
  padding: 20px 24px;
  display: flex;
  flex-direction: row;          /* the expression, its buttons beside it */
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  overflow: hidden;
  transition: border-color 0.3s;
}
.result-card:has(.katex) { border-color: rgba(139, 92, 246, 0.3); }

.result-rendered {
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  text-align: center;
  min-height: 70px;               /* a simple fraction's height: the first reading doesn't make it grow */
  display: flex;
  align-items: center;
  justify-content: center;
}
.result-rendered .katex { font-size: 26px; color: #fff; }
.result-rendered .katex-display { margin: 0; }   /* no display-mode margins: the panel's height is the maths' */
/* centred by auto margins, not by the flex box: an expression wider than the panel starts at its
   left edge and scrolls all the way (centred by flex, its left part overflowed out of reach) */
.result-rendered { justify-content: flex-start; }
.result-rendered > * { margin-left: auto !important; margin-right: auto !important; flex-shrink: 0; }
.result-rendered .placeholder { color: #444; font-size: 15px; font-style: italic; }

.result-latex-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 28px;
}
.result-latex {
  display: none;                /* the source stays in the page for Copy, not shown */
  flex: 1;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
  color: #8b5cf6;
  word-break: break-all;
  background: rgba(139, 92, 246, 0.06);
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid rgba(139, 92, 246, 0.1);
  min-height: 28px;
}

.copy-btn {
  flex-shrink: 0;
  display: flex; align-items: center; gap: 5px;
  background: rgba(139, 92, 246, 0.12);
  border: 1px solid rgba(139, 92, 246, 0.25);
  color: #8b5cf6;
  border-radius: 8px;
  padding: 6px 14px;
  font-size: 12px; font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}
.copy-btn:hover { background: rgba(139, 92, 246, 0.22); border-color: #8b5cf6; }
.copy-btn.copied { background: rgba(16, 185, 129, 0.15); border-color: #10b981; color: #10b981; }

.confirm-btn {
  flex-shrink: 0;
  display: flex; align-items: center; gap: 6px;
  background: rgba(16, 185, 129, 0.08);
  border: 1px solid rgba(16, 185, 129, 0.2);
  color: #10b981;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.15s;
}
.confirm-btn:hover { background: rgba(16, 185, 129, 0.2); border-color: #10b981; }
.confirm-btn:disabled { opacity: 0.35; cursor: not-allowed; pointer-events: none; }
.confirm-label { font-weight: 500; color: #888; }
.confirm-check { font-weight: 700; font-size: 14px; }
.confirm-btn.confirmed { background: #10b981; border-color: #10b981; pointer-events: none; }
.confirm-btn.confirmed .confirm-label { color: #fff; }
.confirm-btn.confirmed .confirm-check { color: #fff; }
.confirm-btn.saving { opacity: 0.7; pointer-events: none; }
.confirm-btn.failed { background: #ef4444; border-color: #ef4444; pointer-events: none; }
.confirm-btn.failed .confirm-label, .confirm-btn.failed .confirm-check { color: #fff; }

/* ── Footer ─────────────────────────────────────────────────── */

footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  padding: 0 4px;
  flex-shrink: 0;
}
.status { color: #444; }
.status.connected { color: #10b981; }
.status.disconnected { color: #ef4444; }
.confidence { color: #555; font-variant-numeric: tabular-nums; }
.footer-credit { color: #444; font-size: 11px; }
.footer-credit a { color: #8b5cf6; text-decoration: none; }
.footer-credit a:hover { text-decoration: underline; }

/* Header links (collected data, about) + modal */
.header-links {
  position: absolute;
  top: 24px;
  right: 24px;
  display: flex;
  gap: 8px;
}
.about-btn {
  text-decoration: none;
  background: rgba(139, 92, 246, 0.1);
  border: 1px solid rgba(139, 92, 246, 0.25);
  color: #8b5cf6;
  font-size: 12px; font-weight: 600;
  padding: 6px 14px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
}
.about-btn:hover { background: rgba(139, 92, 246, 0.2); border-color: #8b5cf6; }

.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  padding: 24px;
}
.modal {
  position: relative;
  background: #12121e;
  border: 1px solid rgba(139, 92, 246, 0.3);
  border-radius: 16px;
  padding: 32px 36px;
  max-width: 520px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  color: #e0e0e0;
}
.modal h2 {
  font-size: 22px; font-weight: 700; color: #fff;
  margin-bottom: 16px;
  background: linear-gradient(135deg, #8b5cf6, #6366f1, #3b82f6);
  -webkit-background-clip: text; -webkit-text-fill-color: transparent;
}
.modal h3 { font-size: 14px; font-weight: 600; color: #a78bfa; margin-top: 20px; margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.5px; }
.modal p { font-size: 14px; line-height: 1.6; color: #ccc; }
.modal a { color: #8b5cf6; text-decoration: none; }
.modal a:hover { text-decoration: underline; }
.modal .examples { list-style: none; padding: 0; margin: 8px 0; display: flex; flex-direction: column; gap: 4px; }
.modal .examples .katex { font-size: 16px; color: #fff; }
.modal-close {
  position: absolute;
  top: 12px; right: 12px;
  width: 32px; height: 32px;
  border: none; background: transparent;
  color: #666; font-size: 24px; line-height: 1;
  cursor: pointer; border-radius: 6px;
}
.modal-close:hover { color: #fff; background: rgba(255,255,255,0.06); }

.logo { position: relative; }
.footer-note { color: #333; font-size: 10px; }

/* ── Expression cards (floating on canvas) ──────────────────── */

.expr-card {
  position: absolute;
  pointer-events: auto;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 6px;
  padding: 4px 8px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.15);
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 0;
  max-width: 400px;
  transition: opacity 0.15s;
}
.expr-card .katex { font-size: 16px; color: #000; }
.expr-card.pending { border: 1px dashed #94a3b8; }
.expr-card.saved { border: 1px solid #8b5cf6; }
.expr-card.selected { border-color: #3b82f6; box-shadow: 0 0 0 2px rgba(59,130,246,0.3); }
.expr-card .card-actions { display: flex; gap: 3px; }
.expr-card .card-btn {
  background: none; border: none; cursor: pointer;
  font-size: 14px; padding: 2px 4px; border-radius: 3px;
  color: #666; line-height: 1;
}
.expr-card .card-btn:hover { background: #e5e7eb; color: #333; }
.expr-card .card-btn.save-btn:hover { color: #8b5cf6; }
.expr-card .card-btn.edit-btn:hover { color: #f59e0b; }
.expr-card .card-btn.redetect-btn:hover { color: #3b82f6; }

/* ── Symbol correction ──────────────────────────────────────── */

.card-main { display: flex; align-items: center; gap: 6px; overflow: hidden; }
.card-main .katex { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.symbol-bar { display: flex; flex-wrap: wrap; gap: 3px; padding-top: 4px; border-top: 1px solid #e5e7eb; margin-top: 4px; }
.sym-chip {
  font-size: 12px; padding: 1px 5px; border-radius: 3px;
  background: #f1f5f9; color: #334155; border: 1px solid #e2e8f0;
  cursor: default; user-select: none; line-height: 1.4;
}
.sym-chip.has-alts { cursor: pointer; border-color: #c7d2fe; background: #eef2ff; }
.sym-chip.has-alts:hover { background: #c7d2fe; border-color: #818cf8; }
.sym-chip.low-conf { background: #fef3c7; border-color: #fcd34d; color: #92400e; }
.sym-chip.sym-more { cursor: pointer; background: #e2e8f0; color: #64748b; font-size: 11px; border-color: #cbd5e1; }
.sym-chip.sym-more:hover { background: #cbd5e1; color: #334155; }

/* the selection card: what it is, its readings, a symbol's other labels */
.expr-card.selected { min-width: 220px; max-width: min(560px, calc(100% - 20px)); }   /* five options + buttons on a line */
.expr-card.selected .card-main { flex-wrap: nowrap; }     /* the reading and its buttons on one line */
.sel-now { flex: 1 1 auto; min-width: 0; display: flex; align-items: center; gap: 8px; min-height: 28px; color: #64748b;
  font-size: 12px; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }   /* long: scrolls, the buttons stay */
.sel-now::-webkit-scrollbar { display: none; }
.expr-card.selected .card-actions { flex-shrink: 0; }
.sel-now .katex { font-size: 1.3em; color: #000; }
.sel-label { white-space: nowrap; }
.expr-card .pct { font-size: 11px; color: #64748b; font-variant-numeric: tabular-nums; }
.expr-card .readings { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; padding-top: 6px; border-top: 1px solid #e5e7eb; }
.expr-card .readings button { display: flex; align-items: center; gap: 6px; padding: 4px 8px; border-radius: 8px;
  border: 1px solid #e5e7eb; background: #fff; cursor: pointer; }
.expr-card .readings button:hover { border-color: #a78bfa; }
.expr-card .readings button.on { border-color: #8b5cf6; background: #f5f3ff; }
/* the card's one row of options: the reading now (highlighted) and the alternatives; scrolls
   sideways when long, the buttons beside it stay */
.expr-card .opts { flex: 1 1 auto; min-width: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.expr-card .opts.expanded { flex-wrap: wrap; }                   /* after "other": the rest may wrap */
.expr-card .opts.syms { margin-top: 6px; padding-top: 6px; border-top: 1px solid #e5e7eb; }   /* line 2: the symbols */
.expr-card .opts.syms:empty { display: none; }
.expr-card .opts::-webkit-scrollbar { display: none; }
.expr-card .opts button { flex-shrink: 0; display: flex; align-items: center; gap: 4px; min-width: 36px; padding: 4px 8px;
  border-radius: 8px; border: 1px solid #c7d2fe; background: #eef2ff; cursor: pointer; }
.expr-card .opts button:hover { border-color: #818cf8; background: #e0e7ff; }
.expr-card .opts button[hidden] { display: none; }        /* behind "other" (display:flex above would show it) */
.expr-card .opts button.on { border-color: #7c3aed; background: #ede9fe; box-shadow: inset 0 0 0 1px #7c3aed; }
.expr-card .opts button.more { background: #f1f5f9; border-color: #cbd5e1; color: #64748b; font-size: 11px; }
.expr-card .opts button:disabled { cursor: default; color: #64748b; background: #f8fafc; border-color: #e5e7eb; }
.expr-card .opts .katex { font-size: 1.15em; color: #000; }
.expr-card .chips { display: flex; flex-wrap: nowrap; gap: 6px; margin-top: 8px; overflow-x: auto; scrollbar-width: none; }
.expr-card .chips::-webkit-scrollbar { display: none; }
.expr-card .chips button { flex-shrink: 0; }
.expr-card .chips:empty { display: none; }
.expr-card .chips button { display: flex; align-items: center; gap: 4px; min-width: 38px; padding: 4px 8px; border-radius: 8px;
  border: 1px solid #c7d2fe; background: #eef2ff; cursor: pointer; }
.expr-card .chips button:hover { border-color: #818cf8; background: #e0e7ff; }
.expr-card .chips button.on { border-color: #7c3aed; background: #ede9fe; box-shadow: inset 0 0 0 1px #7c3aed; }
.expr-card .chips button.more { background: #f1f5f9; border-color: #cbd5e1; color: #64748b; font-size: 11px; }

.sym-dropdown {
  background: #fff; border: 1px solid #d1d5db; border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15); overflow: hidden;
  min-width: 80px; max-width: 180px;
}
.sym-option {
  padding: 6px 10px; cursor: pointer; font-size: 13px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.sym-option:hover { background: #eef2ff; }
.sym-option.current { background: #f0fdf4; font-weight: 600; }
.sym-conf { font-size: 10px; color: #9ca3af; font-weight: 400; }

.symbol-select { font-size: 13px; padding: 2px 6px; border: 1px solid #d1d5db; border-radius: 4px; background: #fff; color: #333; cursor: pointer; max-width: 120px; }

/* ── Suggestion button ──────────────────────────────────────── */

.suggestion-btn {
  position: absolute; z-index: 6; pointer-events: auto;
  width: 22px; height: 22px; border-radius: 50%;
  background: #8b5cf6; color: #fff; border: none;
  font-size: 13px; font-weight: 700; line-height: 1;
  cursor: pointer; box-shadow: 0 1px 4px rgba(139, 92, 246, 0.4);
  animation: suggestion-pop 0.2s ease-out;
}
.suggestion-btn:hover { background: #7c3aed; transform: scale(1.15); }
@keyframes suggestion-pop { from { opacity: 0; transform: scale(0.5); } to { opacity: 1; transform: scale(1); } }

/* ── Command cards ──────────────────────────────────────────── */

.expr-card.command { border: 1.5px solid #d97706; background: rgba(255, 247, 237, 0.95); }
.expr-card.command.active { border-color: #f59e0b; box-shadow: 0 0 0 2px rgba(245, 158, 11, 0.3); }
.expr-card.command.executed { border-color: #9ca3af; opacity: 0.7; }
.expr-card.command .cmd-prefix { color: #d97706; font-weight: 700; font-size: 14px; font-family: monospace; }
.expr-card.command .cmd-latex { color: #92400e; }
.expr-card.command .cmd-latex .katex { font-size: 16px; color: #92400e; }
.expr-card.command .cmd-error { color: #ef4444; font-size: 11px; display: block; margin-top: 2px; }
.expr-card.command .execute-btn:hover { color: #16a34a; }
.expr-card.command .cancel-btn:hover { color: #ef4444; }

/* ── Toast ──────────────────────────────────────────────────── */

#toast-container {
  position: fixed; top: 16px; right: 16px; z-index: 1000;
  display: flex; flex-direction: column; gap: 8px;
  pointer-events: none; max-width: 400px;
}
.toast {
  pointer-events: auto;
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; border-radius: 8px;
  font-size: 13px; font-weight: 500;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  opacity: 0; transform: translateX(40px);
  transition: opacity 0.25s, transform 0.25s;
}
.toast-visible { opacity: 1; transform: translateX(0); }
.toast-exit { opacity: 0; transform: translateX(40px); }
.toast-icon { font-size: 16px; flex-shrink: 0; }
.toast-msg { flex: 1; line-height: 1.4; }
.toast-close {
  background: none; border: none; cursor: pointer;
  font-size: 18px; line-height: 1; padding: 0 2px;
  opacity: 0.6; color: inherit;
}
.toast-close:hover { opacity: 1; }
.toast-error { background: #991b1b; color: #fecaca; border: 1px solid #ef4444; }
.toast-warning { background: #78350f; color: #fef3c7; border: 1px solid #f59e0b; }
.toast-success { background: #064e3b; color: #a7f3d0; border: 1px solid #10b981; }
.toast-info { background: #1e3a5f; color: #bfdbfe; border: 1px solid #3b82f6; }

/* ── Derived expression nodes ───────────────────────────────── */

.derived-node { position: absolute; background: rgba(255,255,255,0.95); border: 1.5px solid #c4b5fd; border-radius: 8px; padding: 8px 14px; cursor: default; pointer-events: auto; z-index: 5; box-shadow: 0 1px 4px rgba(0,0,0,0.08); color: #1a1a2e; }
.derived-node.selected { border-color: #3b82f6; box-shadow: 0 0 0 2px rgba(59,130,246,0.3); }

/* ── Mobile ─────────────────────────────────────────────────── */

@media (max-width: 700px) {
  body { padding: 8px; height: 100dvh; }
  .app { gap: 8px; max-height: none; }
  header { flex-shrink: 0; }
  h1 { font-size: 22px; }
  .subtitle { font-size: 12px; margin-top: 2px; }
  .header-links { position: static; justify-content: center; }
  .about-btn {
    display: inline-block;
    margin-top: 6px;
    padding: 3px 10px;
    font-size: 10px;
  }

  .canvas-area { flex: 1 1 auto; min-height: 0; }
  .canvas-scroll { box-shadow: 0 0 0 1px rgba(255,255,255,0.06), 0 4px 20px rgba(0,0,0,0.3); border-radius: 12px; }
  .tools { top: 8px; right: 8px; gap: 2px; padding: 3px; }
  .tool-btn { width: 32px; height: 32px; }

  .result-card { padding: 10px 14px; gap: 6px; flex-shrink: 0; border-radius: 12px; position: relative; }
  .result-rendered { min-height: 48px; padding-right: 100px; }
  .result-rendered .katex { font-size: 18px; }
  .result-rendered .placeholder { font-size: 12px; }
  .result-latex { display: none; }
  .result-latex-row {
    position: absolute;
    top: 50%; right: 12px;
    transform: translateY(-50%);
    margin: 0;
    min-height: 0;
    gap: 4px;
  }
  .copy-btn, .confirm-btn { padding: 5px 9px; font-size: 11px; }
  .copy-btn span { display: none; }

  .modal { padding: 20px 18px; max-height: 85vh; }
  .modal h2 { font-size: 18px; }
  .modal p { font-size: 13px; }
}

/* Landscape phone — squeeze header further */
@media (max-width: 900px) and (max-height: 500px) {
  body { padding: 4px 8px; }
  .app { gap: 4px; }
  header { display: flex; align-items: center; gap: 8px; justify-content: center; }
  .logo { gap: 6px; }
  .title-github svg { width: 16px; height: 16px; }
  h1 { font-size: 16px; }
  .subtitle, .about-btn { display: none; }
  .result-card { padding: 6px 12px; gap: 4px; }
  .result-rendered { min-height: 38px; padding-right: 90px; }
  .result-rendered .katex { font-size: 14px; }
}

@media (max-width: 480px) {
  h1 { font-size: 18px; }
}

@media (max-width: 700px) {
  #toast-container {
    top: 8px; right: 8px;
    max-width: calc(100vw - 16px);
  }
  .toast {
    padding: 6px 10px;
    font-size: 11px;
    gap: 6px;
  }
  .toast-icon { font-size: 13px; }
  .toast-close { font-size: 14px; }
}

/* ── Partition navigation (top-k grouping candidates) ── */
.part-nav {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}
.part-nav[hidden] { display: none; }
.part-nav button {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 10px;
  color: #64748b;
  padding: 2px 4px;
  border-radius: 4px;
}
.part-nav button:hover { background: #e2e8f0; color: #0f172a; }
.part-nav span {
  font-size: 11px;
  color: #64748b;
  min-width: 24px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* a cluster of red strokes (read as no symbol): its best guess, to confirm or change */
.sym-chip.unexplained { border: 1px dashed #dc2626; color: #dc2626; cursor: pointer; }
.sym-option-head { padding: 4px 10px; font-size: 11px; opacity: 0.7; }

/* the guess next to red strokes: click to confirm it */
/* the reader's questions (ui_red_buttons.js): one-click choices for unread strokes and close calls */
.reader-question { position: absolute; transform: translateY(-100%); z-index: 20; display: flex; gap: 2px; padding: 1px;
  background: var(--surface, #fff); border: 1px solid #fcd34d; border-radius: 9px; }
.reader-choice { font: 13px system-ui, sans-serif; line-height: 16px; padding: 0 6px; border: 0; border-radius: 7px;
  color: #92400e; background: transparent; cursor: pointer; }
.reader-choice.current { background: #fef3c7; }
@media (hover: hover) and (pointer: fine) { .reader-choice:hover { background: #fde68a; } }  /* touch: no sticky highlight */
.reader-choice:disabled { opacity: 0.5; cursor: default; }
.reader-q { font: bold 13px system-ui, sans-serif; line-height: 16px; padding: 0 5px; border: 0; border-radius: 7px;
  color: #92400e; background: transparent; cursor: pointer; }
/* closed: only the "?"; hovered (or tapped open): the choices instead */
.reader-question .reader-choice { display: none; }
/* open on a tap (.open, ui_red_buttons.js); on hover only where there really is hover (a mouse):
   a touch fakes hover the moment it lands, the choices took the "?"'s place under the finger,
   and lifting it picked one */
.reader-picked { position: absolute; transform: translateY(-100%); z-index: 21; pointer-events: none;
  font: 600 13px system-ui, sans-serif; line-height: 16px; padding: 1px 7px; border-radius: 7px;
  background: #dcfce7; color: #166534; box-shadow: 0 1px 3px rgba(0,0,0,0.15); animation: reader-picked 1.3s forwards; }
@keyframes reader-picked { 0%, 65% { opacity: 1; } 100% { opacity: 0; } }
.reader-question.open .reader-choice { display: inline-block; }
.reader-question.open .reader-q { display: none; }
@media (hover: hover) and (pointer: fine) {
  .reader-question:hover .reader-choice { display: inline-block; }
  .reader-question:hover .reader-q { display: none; }
}

/* phones: the selection card as a bottom panel — full width, above everything, bigger targets
   (floating next to the selection it ran off the screen and covered the ink) */
@media (max-width: 700px) {
  .expr-card.selected { position: fixed; left: 8px !important; right: 8px; top: auto !important;
    bottom: calc(8px + env(safe-area-inset-bottom)); max-width: none; min-width: 0; z-index: 50;
    padding: 12px 14px 14px; border-radius: 14px; box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.35); }
  .expr-card .chips button, .expr-card .readings button { min-height: 38px; padding: 6px 10px; }
  .expr-card .card-btn { min-width: 38px; min-height: 38px; }
  .sel-now .katex { font-size: 1.5em; }
}
@media (max-width: 700px) { .expr-card .opts button { min-height: 38px; padding: 6px 10px; } }
.expr-card.selected.busy { opacity: 0.6; pointer-events: none; transition: opacity 0.15s; }   /* read again: dimmed, not replaced */
.result-rendered.stale { opacity: 0.55; transition: opacity 0.15s; }   /* read again: the last reading, dimmed */
@media (max-width: 700px) {
  .copy-btn.copied span { display: inline; }        /* "Copied!" shows on phones too (the label is hidden otherwise) */
}
.copy-btn.copy-failed { background: rgba(239, 68, 68, 0.12); border-color: #ef4444; color: #ef4444; }
@media (max-width: 700px) { .copy-btn.copy-failed span { display: inline; } }
/* the hand (bottom-right of the canvas): touch screens only — a mouse or trackpad pans already */
.pan-btn { position: absolute; right: 12px; bottom: 12px; z-index: 6; display: none;
  background: rgba(255,255,255,0.92); backdrop-filter: blur(8px); border-radius: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.15); width: 40px; height: 40px; }
@media (pointer: coarse) { .pan-btn { display: flex; align-items: center; justify-content: center; } }

/* phones: the header one slim line — the name, "About" at the right */
@media (max-width: 700px) {
  header { display: flex; align-items: center; justify-content: space-between; padding: 0 2px; }
  h1 { font-size: 18px; }
  .subtitle { display: none; }
  .header-links { position: static; }
  .about-btn { margin-top: 0; padding: 3px 9px; font-size: 11px; }
}

/* phones in landscape (a short screen): two columns — the header, the result and the selection
   card on the left, the canvas on the right at full height */
@media (max-height: 500px) and (orientation: landscape) {
  body { padding: 6px; height: 100dvh; }
  .app { display: grid; max-width: none; height: 100%; gap: 6px 8px;
    grid-template-columns: minmax(220px, 34%) 1fr; grid-template-rows: auto auto 1fr auto;
    grid-template-areas: "header canvas" "result canvas" "space canvas" "footer canvas"; }
  header { grid-area: header; display: flex; align-items: center; justify-content: space-between; gap: 6px; }
  h1 { font-size: 16px; }
  .subtitle { display: none; }
  .header-links { position: static; }
  .about-btn { display: inline-block; margin-top: 0; padding: 2px 8px; font-size: 10px; }
  .canvas-area { grid-area: canvas; min-height: 0; }
  .result-card { grid-area: result; flex-direction: column; align-items: stretch; padding: 8px 10px; gap: 6px; }
  .result-rendered { min-height: 44px; padding-right: 0; }
  .result-rendered .katex { font-size: 16px; }
  .result-latex-row { position: static; transform: none; justify-content: flex-end; }
  footer { grid-area: footer; }
  .expr-card.selected { position: fixed; left: 6px !important; top: auto !important; bottom: 6px;
    width: calc(34% - 10px); min-width: 210px; max-width: none; z-index: 50; }
}

/* more room for the canvas: tighter margins — landscape phones (a narrower left column), and a
   light trim in portrait */
@media (max-height: 500px) and (orientation: landscape) {
  body { padding: 2px; }
  .app { gap: 4px; grid-template-columns: minmax(190px, 28%) 1fr; }
  .result-card { padding: 6px 8px; }
  .result-rendered .katex { font-size: 15px; }
  .expr-card.selected { left: 2px !important; bottom: 2px; width: calc(28% - 6px); min-width: 186px; }
}
@media (max-width: 700px) and (orientation: portrait) {
  body { padding: 5px; }
  .app { gap: 5px; }
}

/* landscape phones: the result in the middle of the left column, the expression larger */
@media (max-height: 500px) and (orientation: landscape) {
  .app { grid-template-rows: auto 1fr auto 1fr auto;
    grid-template-areas: "header canvas" "above canvas" "result canvas" "below canvas" "footer canvas"; }
  .result-card { align-self: center; }
  .result-rendered { min-height: 56px; }
  .result-rendered .katex { font-size: 20px; }
}

/* the card: line 1 the groupings at full width; line 2 the symbol's choices and the buttons */
.expr-card .card-foot { display: flex; align-items: center; gap: 6px; margin-top: 6px; padding-top: 6px; border-top: 1px solid #e5e7eb; }
.expr-card .card-foot .opts.syms { margin-top: 0; padding-top: 0; border-top: 0; }
.expr-card .card-foot .opts.syms:empty { display: flex; }          /* keeps the buttons at the right */
.expr-card .card-foot .card-actions { margin-left: auto; flex-shrink: 0; }
@media (max-width: 700px), (max-height: 500px) and (orientation: landscape) {
  .expr-card .opts .katex { font-size: 1em; }
  .expr-card .opts button { padding: 4px 7px; gap: 3px; }
  .expr-card .pct { font-size: 10px; }
}

/* line 2 never breaks up: the symbol's choices on one line; the buttons beside them, or on a
   short line of their own under them when the card is narrow */
.expr-card .card-foot { flex-wrap: wrap; }
.expr-card .card-foot .opts.syms { flex: 0 1 auto; flex-wrap: nowrap; }
.expr-card .card-foot .card-actions { margin-left: auto; }
@media (max-height: 500px) and (orientation: landscape) {
  /* the card stays within the left column: compact chips so a symbol's choices fit its width */
  .expr-card.selected { padding: 8px; }
  .expr-card .opts button { padding: 3px 5px; min-width: 30px; }
  .expr-card .opts { gap: 4px; }
  .expr-card .card-btn { min-width: 30px; min-height: 30px; padding: 2px 4px; }
}
.expr-card .card-foot .opts.syms.expanded { flex-wrap: wrap; flex: 1 1 100%; }   /* after "other": the rest may wrap */
.status.connecting { color: #d97706; }
.toast-action { background: none; border: 1px solid currentColor; color: inherit; border-radius: 4px; padding: 1px 8px;
  margin-left: 8px; cursor: pointer; font: inherit; }
/* line 1 (the groupings): wraps as usual; when the card would take over half the canvas (a big
   expression's readings at full size), one line scrolled sideways instead (ui_cards_view: compact) */
.expr-card.compact #stroke-sel-latex, .expr-card.compact #stroke-sel-latex.expanded {
  flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain; padding-bottom: 2px;
}
/* the grid button: a small matrix and cases as its face; purple when the selection is marked */
.expr-card .grid-btn { padding: 2px 6px; min-width: 0; }
.expr-card .grid-btn .katex { font-size: 11px; color: #475569; }
.expr-card .grid-btn.active { border-color: #8b5cf6; background: #ede9fe; }
.expr-card .grid-btn.active .katex { color: #7c3aed; }
