/* Trình nhập công thức toán — file riêng để không đụng vào host/style.css. */

/* Ô nhập câu hỏi: textarea nhiều dòng (đề toán thường dài hơn một dòng). */
.field textarea.question-text {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--card-border);
  background: var(--card);
  color: var(--text);
  font-size: 15px;
  font-family: inherit;
  line-height: 1.5;
  resize: vertical;
  min-height: 62px;
}

/* ---- Thanh công cụ công thức ---- */

/* Chỉ hiện thanh công cụ của thẻ câu hỏi đang gõ — danh sách 20 câu không bị
   che kín bởi 20 bảng nút. Nút chèn dùng mousedown + preventDefault nên con trỏ
   vẫn nằm trong ô nhập, :focus-within không bị mất khi bấm nút. */
.formula-toolbar {
  display: none;
  flex-wrap: wrap;
  gap: 10px;
  margin: 10px 0 6px;
}
.question-card:focus-within .formula-toolbar { display: flex; }
.question-card:not(:focus-within) .formula-hint { display: none; }
/* Thẻ đã có ô soạn liền mạch (công thức, bảng, đồ thị ngay trong ô): thanh công thức cũ và khung xem trước của từng
   phương án / lời giải không cần nữa, vì ô soạn đã hiện đúng thứ học sinh thấy. */
.question-card.has-rich-fields .formula-toolbar,
.question-card.has-rich-fields .choice-preview,
.question-card.has-rich-fields .statement-preview,
.question-card.has-rich-fields .solution-preview { display: none !important; }
.question-card .choice-body > .fe-rich, .question-card .statement-body > .fe-rich { flex: 1 1 auto; min-width: 0; }
.formula-group {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--card-border);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.03);
}
/* U22 — chạm tối thiểu 40px (khớp .btn-sm ở host/style.css), trước là 34px. */
.formula-btn {
  min-width: 40px;
  min-height: 40px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.formula-btn:hover {
  background: rgba(37, 99, 235, 0.1);
  border-color: var(--accent);
}
.formula-btn:active { transform: scale(0.95); }
.formula-btn .katex { font-size: 1em; }

.formula-hint code {
  background: rgba(0, 0, 0, 0.08);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 11px;
}

/* ---- Khung xem trước (đúng thứ học sinh sẽ thấy trên điện thoại) ---- */

.math-preview {
  margin: -8px 0 14px;
  padding: 10px 12px;
  border-left: 3px solid var(--accent);
  border-radius: 0 8px 8px 0;
  background: rgba(37, 99, 235, 0.05);
  font-size: 15px;
  line-height: 1.6;
  overflow-x: auto; /* công thức dài thì cuộn ngang, không phá vỡ bố cục thẻ */
}
.math-preview:empty { display: none; }

.choice-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
  min-width: 0;
}
.choice-preview {
  margin: 0;
  padding: 6px 8px;
  font-size: 14px;
}

.paste-note {
  margin: 6px 0 0;
  padding: 8px 10px;
  border-left: 3px solid var(--accent);
  border-radius: 0 6px 6px 0;
  background: rgba(37, 99, 235, 0.08);
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--text);
}

.math-error-msg {
  margin: 8px 0 0;
  font-size: 13px;
  color: var(--danger);
}

/* YC-33 — Hộp "Có N lỗi cần sửa trước khi lưu" (host/views/editor.js > showSaveIssues). */
#saveIssuesPanel {
  margin: 0 0 16px;
  padding: 12px 14px;
  border: 1px solid #f3b7b0;
  border-left: 4px solid #d92d20;
  border-radius: 10px;
  background: #fef3f2;
  color: #7a271a;
  font-size: 14px;
}
#saveIssuesPanel > strong { display: block; margin: 0 0 6px; color: #b42318; font-size: 15px; }
#saveIssuesPanel ul { margin: 0; padding: 0; list-style: none; display: grid; gap: 2px; }
#saveIssuesPanel .link-button {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  width: 100%;
  min-height: 36px;
  padding: 6px 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: #7a271a;
  font: inherit;
  line-height: 1.4;
  text-align: left;
  cursor: pointer;
}
#saveIssuesPanel .link-button::before { content: "⚠"; flex: none; color: #d92d20; }
#saveIssuesPanel .link-button:hover { background: rgba(217, 45, 32, 0.08); text-decoration: underline; }
#saveIssuesPanel .link-button:focus-visible { outline: 2px solid #b42318; outline-offset: 1px; background: rgba(217, 45, 32, 0.08); }
@media (max-width: 720px) {
  #saveIssuesPanel .link-button { min-height: 44px; align-items: center; }
}
input[aria-invalid="true"], textarea[aria-invalid="true"] {
  border-color: #d92d20 !important;
  box-shadow: 0 0 0 3px rgba(217, 45, 32, 0.15);
}

/* YC-33 — xem trước dưới ô đáp án + ghi chú quy tắc 0,5/0.5: gọn, chữ phụ. */
.answer-preview { margin: 4px 0 0; padding: 4px 8px; font-size: 13px; line-height: 1.4; }
.answer-preview::before { content: "Xem trước: "; color: var(--muted); font-size: 11.5px; }
.type-short-answer .hint, .type-short-answer-free .hint { font-size: 12px; line-height: 1.45; margin: 2px 0 10px; }
.answer-rule { font-weight: 400; color: var(--text); }

/* KaTeX kế thừa màu chữ (currentColor) nên tự hợp theme hiện tại; chỉ cần chặn tràn ngang. */
.mathtext { overflow-wrap: break-word; }
.mathtext .katex-display { margin: 0.4em 0; }
.math-raw { font-family: ui-monospace, monospace; color: var(--danger); }

/* ---- Tab nhóm ký hiệu (Cơ bản | Lượng giác & hàm | ...) ---- */
.formula-toolbar { flex-direction: column; align-items: stretch; gap: 6px; }
.question-card:focus-within .formula-toolbar { display: flex; }
.formula-tabs { display: flex; flex-wrap: wrap; gap: 4px; }
.formula-tab {
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--card-border);
  border-radius: 999px;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
}
.formula-tab:hover { border-color: var(--accent); }
.formula-tab.is-active {
  background: rgba(37, 99, 235, 0.12);
  border-color: var(--accent);
  font-weight: 600;
}
.formula-panels .formula-group { max-height: 220px; overflow-y: auto; }
.formula-panels .formula-group.hidden { display: none; }
.formula-btn .katex { font-size: 0.95em; }

/* PB-G02 — "Học sinh sẽ thấy" dưới đề bài (host/views/editor.js) và gợi ý "Biến thành công thức" (host/views/editor-bare-math.js). */
.math-preview[data-label]::before {
  content: attr(data-label);
  display: block;
  margin-bottom: 2px;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
}
.bare-math-hint {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 10px;
  margin: -6px 0 12px;
  padding: 8px 10px;
  border-left: 3px solid #d97706;
  border-radius: 0 8px 8px 0;
  background: #fffbeb;
  color: #78350f;
  font-size: 13px;
  line-height: 1.45;
}
.bare-math-hint.hidden { display: none; }
.choice-body .bare-math-hint, .statement-body .bare-math-hint { margin: 0; }
.bare-math-hint__btn { min-height: 32px; }

/* Nút mở bộ soạn công thức trực quan dùng chung (shared/formula-editor) */
.formula-visual-btn { font-weight: 700; border-color: var(--accent); }
