/* AI 小助教：浮在 BBS 畫面上方的終端機風格視窗。
   線條：奶茶色 #A07D5E；按鈕框：BBS 亮黃 #FFFF00；寶寶外圍一律有白色貼紙底，在全黑畫面上也看得清楚。 */
.rhai {
  --rh-line: #A07D5E;
  --rh-line-soft: #D9B99B;
  --rh-milk: #E8D3BC;
  --rh-yellow: #FFFF00;
  --rh-ink: #6B4F3A;
  --rh-gray: #808080;
  position: fixed; z-index: 50;
  display: flex; flex-direction: column;
  min-width: 300px; min-height: 260px;
  background: #000; color: #fff;
  border: 2px solid var(--rh-line); border-radius: 12px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, .45);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", sans-serif;
  font-size: 14px; line-height: 1.5;
}
.rhai.rhai-mini { min-height: 0; height: auto !important; width: auto !important; min-width: 0; }
.rhai.rhai-mini .rhai-body, .rhai.rhai-mini .rhai-rz { display: none; }
.rhai button { font: inherit; }
.rhai :focus-visible { outline: 3px solid var(--rh-yellow); outline-offset: 2px; }

/* ---------- 標題列 ---------- */
.rhai-head {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; border-bottom: 2px solid var(--rh-line);
  cursor: grab; user-select: none; -webkit-user-select: none; touch-action: none;
}
.rhai.rhai-mini .rhai-head { border-bottom: 0; }
.rhai-head.rhai-dragging { cursor: grabbing; }
.rhai-avatar {
  flex: none; width: 40px; height: 40px; padding: 0;
  border: 2px solid var(--rh-line); border-radius: 50%;
  background: #fff; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.rhai-avatar svg { width: 34px; height: 34px; }
.rhai-title { font-weight: 600; color: #fff; white-space: nowrap; }
.rhai-star {
  flex: none; width: 32px; height: 32px; padding: 0; margin-left: auto;
  border: 0; border-radius: 50%; background: transparent; cursor: pointer; position: relative;
}
.rhai-star svg { width: 22px; height: 22px; }
.rhai-tip {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 3;
  width: max-content; max-width: 260px; padding: 6px 10px;
  background: #1a1612; border: 2px solid var(--rh-line); border-radius: 8px;
  color: var(--rh-milk); font-size: 13px; text-align: left; white-space: normal;
}

/* 星星：一閃一閃＝就緒；亮著不動＝模型休息中；灰色＝連不上 */
.rhai-spark { stroke: var(--rh-line); transform-box: fill-box; transform-origin: center; }
.rhai-st-ready .rhai-spark { animation: rhai-twinkle 2.6s ease-in-out infinite; }
.rhai-st-down .rhai-spark, .rhai-st-missing .rhai-spark { stroke: var(--rh-gray); }
.rhai-spark-fill { fill: var(--rh-line-soft); stroke: none; }
.rhai-st-down .rhai-spark-fill, .rhai-st-missing .rhai-spark-fill { fill: var(--rh-gray); }
@keyframes rhai-twinkle {
  0%, 62%, 100% { transform: scale(1) rotate(0deg); opacity: 1; }
  72% { transform: scale(1.6) rotate(45deg); opacity: 1; }
  82% { transform: scale(.8) rotate(90deg); opacity: .6; }
  90% { transform: scale(1.15) rotate(90deg); opacity: 1; }
}

/* ---------- 頭像選單 ---------- */
.rhai-menu {
  position: absolute; left: 8px; top: 54px; z-index: 4; width: 210px;
  display: flex; flex-direction: column; gap: 4px; padding: 6px;
  background: #000; border: 2px solid var(--rh-line); border-radius: 12px;
}
.rhai-menu button {
  min-height: 44px; padding: 0 12px; text-align: left; cursor: pointer;
  border: 2px solid var(--rh-yellow); border-radius: 8px; background: #000; color: #fff;
}
.rhai-menu button[aria-current="page"] { background: #3a3020; }

/* ---------- 內容 ---------- */
.rhai-body { flex: 1; min-height: 0; overflow: auto; position: relative; }
.rhai-tab { padding: 12px; }
.rhai-stage { position: relative; display: flex; align-items: center; gap: 12px; min-height: 170px; }
.rhai-stage canvas { position: absolute; inset: 0; z-index: 0; width: 100%; height: 100%; pointer-events: none; }
.rhai-robot { flex: none; width: 128px; height: 134px; position: relative; z-index: 1; }
.rhai-robot-run { transform: scaleX(-1); }
.rhai-say { position: relative; z-index: 1; flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 10px; }
.rhai-bubble {
  position: relative; padding: 10px 16px;
  background: #fff; color: var(--rh-ink);
  border: 3px solid var(--rh-line); border-radius: 18px;
  font-size: 18px; font-weight: 600;
}
.rhai-bubble::before {
  content: ""; position: absolute; left: -10px; top: 50%; width: 14px; height: 14px; margin-top: -7px;
  background: #fff; border-left: 3px solid var(--rh-line); border-bottom: 3px solid var(--rh-line);
  transform: rotate(45deg);
}
.rhai-bubble.rhai-beat { animation: rhai-heartbeat 1.4s ease-in-out infinite; }
.rhai-bubble.rhai-pop { animation: rhai-pop .5s ease-out 1; }
.rhai-bubble.rhai-plain { font-size: 15px; }
.rhai-dots span { animation: rhai-dot 1.2s ease-in-out infinite; }
.rhai-dots span:nth-child(2) { animation-delay: .2s; }
.rhai-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes rhai-heartbeat {
  0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(160, 125, 94, .55); }
  14% { transform: scale(1.05); box-shadow: 0 0 0 6px rgba(160, 125, 94, .35); }
  28% { transform: scale(1); box-shadow: 0 0 0 3px rgba(160, 125, 94, .3); }
  42% { transform: scale(1.07); box-shadow: 0 0 0 10px rgba(160, 125, 94, .22); }
  70% { transform: scale(1); box-shadow: 0 0 0 18px rgba(160, 125, 94, 0); }
  100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(160, 125, 94, 0); }
}
@keyframes rhai-pop { 0% { transform: scale(.6); opacity: 0; } 60% { transform: scale(1.08); opacity: 1; } 100% { transform: scale(1); } }
@keyframes rhai-dot { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

/* 寶寶的動作 */
.rhai-bob { animation: rhai-bob .5s ease-in-out infinite; }
.rhai-swing { transform-box: view-box; animation: rhai-swing .5s ease-in-out infinite; }
.rhai-swing.rhai-late { animation-delay: -.25s; }
.rhai-dash { animation: rhai-dash .6s linear infinite; }
.rhai-tongue { transform-box: fill-box; transform-origin: top center; animation: rhai-wag 1.2s ease-in-out infinite; }
@keyframes rhai-bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }
@keyframes rhai-swing { 0%, 100% { transform: rotate(32deg); } 50% { transform: rotate(-32deg); } }
@keyframes rhai-dash { 0% { transform: translateX(0); opacity: 0; } 30% { opacity: 1; } 100% { transform: translateX(-40px); opacity: 0; } }
@keyframes rhai-wag { 0%, 100% { transform: rotate(-6deg); } 50% { transform: rotate(6deg); } }

/* ---------- 按鈕 ---------- */
.rhai-btns { display: flex; flex-wrap: wrap; gap: 8px; }
.rhai-btn {
  min-height: 44px; padding: 0 16px; cursor: pointer;
  border: 2px solid var(--rh-yellow); border-radius: 22px;
  background: #fff; color: var(--rh-ink); font-weight: 600;
}
.rhai-btn.rhai-dark { background: #000; color: #fff; }
.rhai-btn:disabled { cursor: not-allowed; opacity: .45; }
.rhai-icon {
  width: 48px; height: 48px; padding: 0; cursor: pointer;
  border: 2px solid var(--rh-yellow); border-radius: 50%; background: #000;
  display: inline-flex; align-items: center; justify-content: center;
}
.rhai-icon svg { width: 26px; height: 26px; }
.rhai-icon-wrap { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 12px; color: var(--rh-milk); }
.rhai-icons { display: flex; gap: 14px; align-items: flex-start; }

/* ---------- 解題中 ---------- */
.rhai-formula {
  margin: 10px 0; padding: 8px 10px; border: 2px dashed var(--rh-line); border-radius: 8px;
  font-family: Menlo, Consolas, "Courier New", monospace; font-size: 15px; color: #fff;
  overflow-x: auto; white-space: pre;
}
.rhai-note { margin: 6px 0; color: var(--rh-milk); font-size: 13px; }
.rhai-note.rhai-warn { color: var(--rh-yellow); }
.rhai-sent {
  margin-top: 10px; padding: 6px 8px; border-top: 1px solid #3a3020;
  font-family: Menlo, Consolas, monospace; font-size: 12px; color: #c0c0c0; white-space: pre; overflow-x: auto;
}

/* ---------- 競速 ---------- */
.rhai-timers {
  position: absolute; left: 0; top: 0; z-index: 2;
  font-family: Menlo, Consolas, monospace; font-size: 18px; color: #fff; line-height: 1.4;
}
.rhai-timers b { color: var(--rh-yellow); font-weight: 600; margin-right: 8px; }
.rhai-stage.rhai-race { padding-top: 56px; }
.rhai-answers { font-family: Menlo, Consolas, monospace; font-size: 13px; color: #c0c0c0; }
.rhai-answers b { color: var(--rh-yellow); margin-right: 6px; }

/* ---------- 監控分頁 ---------- */
.rhai-h { margin: 0 0 8px; font-size: 15px; color: #fff; }
.rhai-field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.rhai-field label { color: var(--rh-milk); font-size: 13px; }
.rhai-field textarea, .rhai-field input, .rhai-field select {
  width: 100%; padding: 6px 8px; border: 1px solid var(--rh-line); border-radius: 6px;
  background: #111; color: #fff; font-family: Menlo, Consolas, monospace; font-size: 12px;
}
.rhai-field textarea { min-height: 90px; resize: vertical; }
.rhai-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 10px; }
.rhai-check { display: flex; gap: 6px; align-items: flex-start; margin-bottom: 12px; color: var(--rh-milk); }
.rhai-check input { margin-top: 4px; }
.rhai-table-wrap { overflow-x: auto; margin-bottom: 12px; }
.rhai-table { border-collapse: collapse; font-size: 12px; white-space: nowrap; }
.rhai-table th, .rhai-table td { border: 1px solid #3a3020; padding: 3px 6px; text-align: right; }
.rhai-table th { color: var(--rh-milk); font-weight: 600; text-align: center; }
.rhai-table td.rhai-l { text-align: left; }
.rhai-slow { color: var(--rh-yellow); }
.rhai-recent details { border: 1px solid #3a3020; border-radius: 6px; margin-bottom: 6px; }
.rhai-recent summary { cursor: pointer; padding: 6px 8px; color: #fff; }
.rhai-recent pre {
  margin: 0 8px 8px; padding: 6px; background: #111; border-radius: 4px;
  font-size: 11px; color: #c0c0c0; white-space: pre-wrap; word-break: break-all;
}
.rhai-recent h4 { margin: 6px 8px 2px; font-size: 12px; color: var(--rh-milk); }
.rhai-bad { color: #ff5555; }

/* ---------- 縮放把手 ---------- */
.rhai-rz { position: absolute; z-index: 5; touch-action: none; }
.rhai-rz[data-dir="n"] { top: -5px; left: 10px; right: 10px; height: 10px; cursor: ns-resize; }
.rhai-rz[data-dir="s"] { bottom: -5px; left: 10px; right: 10px; height: 10px; cursor: ns-resize; }
.rhai-rz[data-dir="e"] { right: -5px; top: 10px; bottom: 10px; width: 10px; cursor: ew-resize; }
.rhai-rz[data-dir="w"] { left: -5px; top: 10px; bottom: 10px; width: 10px; cursor: ew-resize; }
.rhai-rz[data-dir="ne"] { right: -6px; top: -6px; width: 16px; height: 16px; cursor: nesw-resize; }
.rhai-rz[data-dir="nw"] { left: -6px; top: -6px; width: 16px; height: 16px; cursor: nwse-resize; }
.rhai-rz[data-dir="se"] { right: -6px; bottom: -6px; width: 16px; height: 16px; cursor: nwse-resize; }
.rhai-rz[data-dir="sw"] { left: -6px; bottom: -6px; width: 16px; height: 16px; cursor: nesw-resize; }

@media (prefers-reduced-motion: reduce) {
  .rhai *, .rhai *::before { animation: none !important; }
}

/* ---------- 3 次都沒通過：三個版本 ---------- */
.rhai-attempts { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.rhai-attempt { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px dashed var(--rh-line); border-radius: 8px; }
.rhai-attempt-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.rhai-attempt-line { font-family: Menlo, Consolas, monospace; font-size: 14px; color: #fff; overflow-wrap: anywhere; }
.rhai-attempt-why { font-size: 12px; color: var(--rh-yellow); }
.rhai-struck .rhai-attempt-line { text-decoration: line-through; color: var(--rh-gray); }
.rhai-icon.rhai-small { width: 40px; height: 40px; flex: none; }
.rhai-icon.rhai-small svg { width: 22px; height: 22px; }
.rhai-icon:disabled { opacity: .4; cursor: not-allowed; }

/* ---------- Pseudo ○：三欄對照表 ---------- */
.rhai-table.rhai-sop { width: 100%; white-space: normal; font-size: 13px; }
.rhai-sop th, .rhai-sop td { text-align: left; vertical-align: middle; padding: 6px 8px; }
.rhai-sop thead th { text-align: left; }
.rhai-sop tbody th { color: var(--rh-milk); font-weight: 600; white-space: nowrap; }
.rhai-sop td { font-family: Menlo, Consolas, monospace; color: #fff; overflow-wrap: anywhere; }
.rhai-sop td:nth-child(2) { color: #a8a8a8; }
.rhai-sop .rhai-why { display: block; font-family: -apple-system, "PingFang TC", "Noto Sans TC", sans-serif; color: var(--rh-milk); font-size: 12px; }
.rhai-sop .rhai-mark { white-space: nowrap; text-align: center; }
.rhai-sop .rhai-mark .rhai-icon + .rhai-icon { margin-left: 6px; }
.rhai-sop tr.rhai-ok td.rhai-mark { color: #55ff55; font-size: 18px; }
.rhai-sop tr.rhai-cur { background: #1f1a12; }
.rhai-sop tr.rhai-cur th { color: var(--rh-yellow); }

.rhai-icon.rhai-force { border-color: #55ff55; }
.rhai-sop thead th { white-space: nowrap; }

/* ---------- 標題自動產生器 ---------- */
.rhai.rhai-tw { width: 320px; min-width: 260px; min-height: 0; z-index: 51; }
.rhai-tw-close { margin-left: auto; width: 32px; height: 32px; border: 0; background: transparent; color: var(--rh-milk); font-size: 18px; cursor: pointer; }
.rhai-tw .rhai-field input { font-family: -apple-system, "PingFang TC", "Noto Sans TC", sans-serif; font-size: 14px; }
.rhai-problem { white-space: pre-wrap; max-height: 8.5em; overflow: auto; display: inline-block; vertical-align: top; }
.rhai-btns.rhai-col { flex-direction: column; align-items: stretch; }

/* ---------- 直式檢查表（多項式除法） ---------- */
.rhai.rhai-full { left: 12px !important; top: 12px !important; width: calc(100vw - 24px) !important; height: calc(100vh - 24px) !important; z-index: 60; }
.ld-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ld-wrap { display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.ld-left { flex: 1 1 260px; min-width: 0; overflow-x: auto; }
.ld-right { flex: 1 1 260px; min-width: 0; }
.rhai-full .ld-left, .rhai-full .ld-right { flex: 1 1 0; }
.ld-grid { border-collapse: collapse; font-family: "Times New Roman", "Noto Serif TC", serif; font-size: 20px; color: #fff; margin: 8px 0; }
.rhai-full .ld-grid { font-size: 28px; }
.ld-grid td { min-width: 3.2em; height: 1.7em; padding: 0 6px; text-align: right; white-space: nowrap; }
.ld-grid td.ld-dv { min-width: 0; padding-right: 10px; text-align: right; color: var(--rh-milk); }
.ld-dividend td:not(.ld-dv) { border-top: 2px solid #fff; }
.ld-prod td:not(.ld-dv) { border-bottom: 1px solid #aaa; }
.ld-grid td.ld-pad { color: #ffd84d; font-weight: 700; }
.ld-grid td.ld-blink { outline: 2px solid var(--rh-yellow); outline-offset: -2px; animation: ld-blink 1s steps(2, start) infinite; }
@keyframes ld-blink { to { outline-color: transparent; } }
.ld-concl-h { color: var(--rh-milk); font-size: 13px; margin-top: 10px; }
.ld-concl { font-family: Menlo, Consolas, monospace; color: #fff; font-size: 16px; }
.ld-list { list-style: none; margin: 0; padding: 0; }
.ld-group { color: var(--rh-milk); font-weight: 700; margin: 10px 0 4px; font-size: 13px; }
.ld-item { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; padding: 6px 8px; border-radius: 6px; color: #ddd; font-size: 14px; }
.ld-item.ld-cur { background: #1f1a12; outline: 1px solid var(--rh-yellow); }
.ld-item.ld-todo { opacity: .55; }
.ld-label { font-weight: 700; color: var(--rh-yellow); min-width: 2.6em; }
.ld-name { color: var(--rh-milk); }
.ld-plain { font-family: Menlo, Consolas, monospace; color: #fff; flex: 1 1 auto; }
.ld-mark { color: #55ff55; margin-left: auto; }
.ld-btns { margin-left: auto; display: flex; gap: 6px; }
.ld-read { flex-basis: 100%; font-size: 12px; color: #9fd49f; padding-left: 3.2em; }
.ld-read.ld-warn { color: #ffb347; }
.ld-detail { flex-basis: 100%; background: #10202a; border-left: 3px solid #6cf; padding: 6px 10px; color: #e8f4ff; font-size: 13px; line-height: 1.6; }
@media (prefers-reduced-motion: reduce) { .ld-grid td.ld-blink { animation: none; } }
/* 除式分格；乘積做完時，第一項左邊出現圈起來的減號，發出呼吸光 */
.ld-grid td.ld-dv { min-width: 2.6em; }
.ld-dividend td.ld-dv + td:not(.ld-dv) { border-left: 2px solid #fff; }
.ld-grid td { position: relative; }
.ld-minus {
  position: absolute; left: -0.9em; top: 50%; transform: translateY(-50%);
  width: 1.3em; height: 1.3em; border-radius: 50%; border: 3px solid #ffd84d;
  display: grid; place-items: center; color: #ffd84d; font-weight: 900; font-size: 1em; line-height: 1;
  background: #000; animation: ld-breathe 2.4s ease-in-out infinite; z-index: 1;
}
@keyframes ld-breathe {
  0%, 100% { box-shadow: 0 0 2px 1px rgba(255, 216, 77, .35); }
  50% { box-shadow: 0 0 14px 6px rgba(255, 216, 77, .85); }
}
@media (prefers-reduced-motion: reduce) { .ld-minus { animation: none; box-shadow: 0 0 8px 3px rgba(255, 216, 77, .7); } }

/* ---------- 十字交乘法檢查表 ---------- */
.cx-table { border-collapse: collapse; font-family: "Times New Roman", "Noto Serif TC", serif; font-size: 20px; color: #fff; margin: 6px 0 4px; }
.rhai-full .cx-table { font-size: 26px; }
.cx-table th { color: var(--rh-milk); font-weight: 400; padding: 0 10px; text-align: right; }
.cx-table td { min-width: 2.6em; height: 1.4em; padding: 0 8px; text-align: center; }
.cx-table td.cx-ans { border-left: 3px solid var(--rh-yellow); border-right: 3px solid var(--rh-yellow); background: #1f1a12; }
.cx-table td.cx-ans-top { border-top: 3px solid var(--rh-yellow); }
.cx-table td.cx-ans-bot { border-bottom: 3px solid var(--rh-yellow); }
.ld-item.cx-row-ans { outline: 3px solid var(--rh-yellow); background: #1f1a12; }
.cx-why { flex-basis: 100%; color: var(--rh-yellow); font-size: 13px; }
/* 十字交乘法：滑鼠停在還沒檢查的那一組上方時反白；不合的反饋 */
.cx-table td.cx-hot, .ld-item.cx-hot { background: rgba(160, 125, 94, .45); }
.ld-item.cx-live { cursor: default; }
.cx-bad { color: #ffb347; margin-left: auto; font-weight: 700; }
.ld-item.cx-row-bad { opacity: .8; }
.cx-note { flex-basis: 100%; font-size: 13px; color: #9fd49f; }
.cx-note.cx-note-bad { color: #ffb347; }

/* ---------- 分組法檢查表 ---------- */
.gp-block { border: 1px solid #333; border-radius: 6px; padding: 8px 10px; margin: 8px 0; }
.gp-block.gp-ok { outline: 3px solid var(--rh-yellow); background: #1f1a12; }
.gp-head { display: flex; align-items: center; gap: 8px; }
.gp-block .ld-plain { display: block; margin: 4px 0; }
.gp-ai { display: block; font-size: 12px; color: #9fd49f; }

/* ---------- 英文:分析 ---------- */
.en-prev { border-collapse: collapse; font-family: Menlo, Consolas, monospace; font-size: 18px; margin: 6px 0; }
.rhai-full .en-prev { font-size: 24px; }
.en-prev td { padding: 2px 4px; white-space: nowrap; color: #fff; }
.en-prev td.en-bar { color: #888; }
.en-label { min-width: 7em; }

/* 等 AI 的時候：跑步的小助教面向右邊 */
.rhai-robot-run.rhai-run-right { transform: none; }
.rhai-wait { display: flex; align-items: center; gap: 10px; color: var(--rh-milk); font-size: 14px; }
.rhai-wait .rhai-robot { width: 64px; height: 67px; }
.rhai-btn.rhai-soon, .rhai-btn.rhai-soon:hover { opacity: .45; cursor: not-allowed; }
