/* ==========================================================================
   可视化组件：矩阵 / 算式面板 / 步进器 / 图表
   ========================================================================== */

/* ---------- 矩阵 ---------- */
.mx {
  display: inline-flex;
  flex-direction: column;
  gap: 6px;
  vertical-align: top;
  --tone: var(--text-2);
}
.mx.mx-sm {
  --cw: 38px;
  --ch: 27px;
}
.mx.mx-xs {
  --cw: 32px;
  --ch: 24px;
}
.mx.mx-lg {
  --cw: 54px;
  --ch: 34px;
}
.mx.tone-q {
  --tone: var(--q);
}
.mx.tone-k {
  --tone: var(--k);
}
.mx.tone-v {
  --tone: var(--v);
}
.mx.tone-grad {
  --tone: var(--grad);
}
.mx.tone-brand {
  --tone: var(--brand);
}

.mx-head {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-height: 22px;
  padding-left: 2px;
}
.mx-name {
  font-weight: 800;
  font-size: 15px;
  color: var(--tone);
  display: inline-flex;
  align-items: baseline;
  gap: 2px;
}
.mx-name math {
  font-size: 1.05em;
}
.mx-shape {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
}

.mx-layout {
  display: grid;
  grid-template-columns: max-content max-content;
  grid-template-rows: max-content max-content max-content;
  column-gap: 6px;
  row-gap: 3px;
  align-items: start;
}
.mx-groups {
  grid-column: 2;
  display: grid;
  height: 17px;
  column-gap: 2px;
  padding: 0 8px;
}
.mx-group {
  font-size: 11px;
  font-weight: 700;
  text-align: center;
  line-height: 15px;
  border-radius: 5px 5px 0 0;
  border-bottom: 2px solid var(--g, var(--brand));
  color: var(--g, var(--brand));
  background: color-mix(in srgb, var(--g, var(--brand)) 10%, transparent);
  white-space: nowrap;
}
.mx-colhead {
  grid-column: 2;
  display: grid;
  column-gap: 2px;
  padding: 0 8px;
}
.mx-ch {
  min-width: 0;
  text-align: center;
  font-size: 11.5px;
  font-family: var(--font-mono);
  color: var(--muted);
  line-height: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.15s, background 0.15s;
}
.mx-ch.is-hl {
  color: var(--brand-ink);
  font-weight: 800;
}
.mx-rowhead {
  grid-column: 1;
  grid-row: 3;
  display: grid;
  row-gap: 2px;
  justify-items: end;
}
.mx-corner {
  grid-column: 1;
  grid-row: 2;
}
.mx-groups {
  grid-row: 1;
}
.mx-colhead {
  grid-row: 2;
}
.mx-frame {
  grid-column: 2;
  grid-row: 3;
  position: relative;
  padding: 0 8px;
}
.mx-frame::before,
.mx-frame::after {
  content: '';
  position: absolute;
  top: -2px;
  bottom: -2px;
  width: 7px;
  border: 2px solid var(--tone);
  opacity: 0.8;
}
.mx-frame::before {
  left: 0;
  border-right: 0;
  border-radius: 5px 0 0 5px;
}
.mx-frame::after {
  right: 0;
  border-left: 0;
  border-radius: 0 5px 5px 0;
}
.mx-grid {
  display: grid;
  gap: 2px;
}
.mx-rh {
  height: var(--ch);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 2px;
  font-size: 12px;
  font-family: var(--font-mono);
  color: var(--muted);
  white-space: nowrap;
  transition: color 0.15s;
}
.mx-rh.is-hl {
  color: var(--brand-ink);
  font-weight: 800;
}
.mx-rh .tok {
  font-size: 12.5px;
}

.mx-c {
  height: var(--ch);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-mono);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  border-radius: var(--radius-xs);
  background: var(--surface-2);
  color: var(--text);
  cursor: default;
  user-select: none;
  position: relative;
  transition: background-color 0.25s ease, opacity 0.15s ease, box-shadow 0.12s ease, transform 0.12s ease;
}
.mx-c.t-plain {
  background: var(--surface-2);
}
.mx-c.is-zero {
  color: var(--muted);
}
.mx-c.is-masked {
  background: repeating-linear-gradient(135deg, var(--surface-2), var(--surface-2) 4px, var(--surface-3) 4px, var(--surface-3) 8px);
  color: var(--muted);
  font-size: 12px;
}
.mx.interactive .mx-c {
  cursor: pointer;
}
.mx.interactive .mx-c:hover {
  transform: scale(1.06);
  z-index: 3;
}
.mx-c.is-row,
.mx-c.is-col {
  box-shadow: inset 0 0 0 1.5px rgb(84 87 224 / 0.55);
}
.mx-c.is-cell {
  box-shadow: 0 0 0 2px var(--brand), 0 4px 14px rgb(84 87 224 / 0.35);
  z-index: 4;
  font-weight: 800;
}
.mx.grad .mx-c.is-cell {
  box-shadow: 0 0 0 2px var(--grad), 0 4px 14px rgb(225 29 72 / 0.35);
}
.mx-c.is-dim {
  opacity: 0.38;
}
.mx-c.is-pick {
  outline: 2px dashed var(--accent);
  outline-offset: 1px;
}
.mx-c.is-flash {
  animation: cellflash 0.9s ease;
}
@keyframes cellflash {
  0% {
    box-shadow: 0 0 0 0 rgb(84 87 224 / 0.8);
  }
  100% {
    box-shadow: 0 0 0 10px rgb(84 87 224 / 0);
  }
}

/* 运算符号（× = + →） */
.mx-op {
  align-self: center;
  font-size: 26px;
  font-weight: 300;
  color: var(--muted);
  padding: 0 2px;
  margin-top: 26px;
  user-select: none;
  line-height: 1;
}
.mx-op.small {
  font-size: 18px;
}
.mx-op .op-note {
  display: block;
  font-size: 11px;
  font-weight: 600;
  margin-top: 2px;
  text-align: center;
  color: var(--muted);
  font-family: var(--font-mono);
}

/* 一串“矩阵 运算符 矩阵 …”：允许换行，运算符粘在后一个矩阵上，避免孤立在行尾 */
.mx-eq {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 14px 6px;
}
.mx-eq > .piece {
  display: inline-flex;
  align-items: flex-start;
  gap: 6px;
}

/* ---------- 算式面板 ---------- */
.calc {
  margin-top: 14px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 4px solid var(--brand);
  font-family: var(--font-mono);
  font-size: 14px;
  line-height: 1.9;
  min-height: 78px;
  overflow-x: auto;
}
.calc.is-grad {
  border-left-color: var(--grad);
}
.calc .calc-title {
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 700;
  color: var(--text-2);
  margin-bottom: 2px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  align-items: center;
}
.calc .calc-title .hint {
  font-weight: 500;
  color: var(--muted);
}
.calc .calc-line {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 6px;
}
.calc .term {
  display: inline-flex;
  align-items: center;
  padding: 0 7px;
  border-radius: 7px;
  background: var(--surface);
  border: 1px solid var(--border);
  white-space: nowrap;
  animation: termIn 0.35s ease both;
}
.calc .term .times {
  color: var(--muted);
  margin: 0 3px;
}
.calc .op {
  color: var(--muted);
}
.calc .res {
  font-weight: 800;
  color: var(--brand-ink);
  background: var(--brand-soft);
  padding: 0 9px;
  border-radius: 7px;
}
.calc.is-grad .res {
  color: var(--grad);
  background: var(--grad-soft);
}
.calc .sub {
  color: var(--muted);
  font-size: 12.5px;
}
.calc .pos {
  color: var(--c-pos-ink, #2563eb);
}
@keyframes termIn {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
.calc-hint {
  margin-top: 8px;
  font-size: 13px;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}
.calc-hint svg {
  width: 15px;
  height: 15px;
}

/* ---------- 步进器 ---------- */
.stepper {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-1);
  overflow: hidden;
}
.stepper:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
}
.stepmap {
  display: flex;
  align-items: stretch;
  gap: 0;
  padding: 14px 16px 12px;
  overflow-x: auto;
  background: linear-gradient(180deg, var(--surface-2), var(--surface));
  border-bottom: 1px solid var(--border);
  scrollbar-width: thin;
}
.stepmap-node {
  appearance: none;
  border: 0;
  background: transparent;
  cursor: pointer;
  padding: 0 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-width: 62px;
  position: relative;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.stepmap-node::before {
  content: '';
  position: absolute;
  top: 13px;
  right: 50%;
  width: 100%;
  height: 3px;
  background: var(--border-strong);
  z-index: 0;
}
.stepmap-node:first-child::before {
  display: none;
}
.stepmap-node.done::before,
.stepmap-node.active::before {
  background: var(--tone-c, var(--brand));
}
.stepmap-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 12px;
  font-weight: 800;
  background: var(--surface);
  border: 2px solid var(--border-strong);
  z-index: 1;
  color: var(--muted);
  transition: all 0.2s;
}
.stepmap-node.done .stepmap-dot {
  background: var(--tone-c, var(--brand));
  border-color: var(--tone-c, var(--brand));
  color: #fff;
}
.stepmap-node.active .stepmap-dot {
  background: var(--surface);
  border-color: var(--tone-c, var(--brand));
  color: var(--tone-c, var(--brand));
  box-shadow: 0 0 0 5px color-mix(in srgb, var(--tone-c, var(--brand)) 20%, transparent);
  transform: scale(1.12);
}
.stepmap-node.active {
  color: var(--text);
}
.stepmap-node:hover .stepmap-dot {
  border-color: var(--tone-c, var(--brand));
}
.stepmap-label {
  white-space: nowrap;
}

.step-body {
  padding: 22px 26px 8px;
  animation: stepIn 0.35s ease both;
}
@keyframes stepIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
.step-title {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 6px 12px;
  margin-bottom: 14px;
}
.step-title .num {
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 800;
  color: #fff;
  background: var(--tone-c, var(--brand));
  padding: 1px 10px;
  border-radius: 999px;
}
.step-title h3 {
  font-size: 22px;
}
.step-title .out-shape {
  margin-left: auto;
  font-size: 12.5px;
  color: var(--muted);
  font-family: var(--font-mono);
}
.step-cols {
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
  gap: 8px 32px;
  align-items: start;
}
.step-cols p {
  margin-bottom: 0.7em;
}
.step-viz {
  margin: 18px 0 8px;
  padding: 18px 18px 14px;
  background: var(--surface);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius);
  overflow-x: auto;
}
.step-viz-hd {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  margin-bottom: 14px;
}
.step-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
}
.step-nav .progress {
  font-size: 13px;
  color: var(--muted);
  font-family: var(--font-mono);
}
.step-nav .kbd-hint {
  font-size: 12.5px;
  color: var(--muted);
}

@media (max-width: 900px) {
  .step-cols {
    grid-template-columns: minmax(0, 1fr);
  }
  .step-body {
    padding: 18px 14px 6px;
  }
  .step-title h3 {
    font-size: 19px;
  }
  .step-viz {
    padding: 12px 10px;
  }
  .step-nav .kbd-hint {
    display: none;
  }
}

/* ---------- 图表通用 ---------- */
.chart-svg text {
  font-family: var(--font-mono);
  font-size: 11px;
  fill: var(--muted);
}
.chart-svg .axis {
  stroke: var(--border-strong);
  stroke-width: 1;
}
.chart-svg .grid {
  stroke: var(--border);
  stroke-width: 1;
  stroke-dasharray: 3 4;
}

/* ---------- 4 位小数梯度矩阵：格子更宽 ---------- */
.mx.mx-xl {
  --cw: 60px;
  --ch: 30px;
}
.mx.mx-xl .mx-c {
  font-size: 12px;
  letter-spacing: -0.2px;
}
.stepmap {
  position: relative;
}

/* ---------- 算式面板里的小部件 ---------- */
.calc .lbl {
  font-family: var(--font-sans);
  font-weight: 700;
  font-size: 12.5px;
  color: var(--text-2);
  background: var(--surface-3);
  border-radius: 6px;
  padding: 0 8px;
  white-space: nowrap;
}
.calc .res.soft {
  font-weight: 700;
  font-size: 0.96em;
  padding: 0 7px;
}
.calc .calc-line + .calc-line {
  margin-top: 4px;
}
.calc .calc-line .sub {
  font-family: var(--font-sans);
}
.calc .term sup {
  font-size: 0.75em;
}
.calc > .sub {
  font-family: var(--font-sans);
  margin-top: 4px;
}

/* 算式面板 + 旁边的图 */
.split {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(0, 1fr);
  gap: 16px;
  align-items: stretch;
}
.split > .calc {
  min-width: 0;
}
.fan-panel {
  margin-top: 14px;
  padding: 12px 16px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  min-width: 0;
}
.fan-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-2);
  margin-bottom: 4px;
}
@media (max-width: 900px) {
  .split {
    grid-template-columns: minmax(0, 1fr);
    gap: 0;
  }
}

/* ---------- 折叠块 ---------- */
.details {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  margin: 14px 0;
}
.details > summary {
  cursor: pointer;
  padding: 10px 16px;
  font-weight: 700;
  color: var(--text-2);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  user-select: none;
}
.details > summary::-webkit-details-marker {
  display: none;
}
.details > summary::before {
  content: '';
  width: 7px;
  height: 7px;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg);
  transition: transform 0.2s;
  flex: none;
}
.details[open] > summary::before {
  transform: rotate(45deg);
}
.details > summary:hover {
  color: var(--brand-ink);
}
.details-b {
  padding: 4px 18px 14px;
  border-top: 1px dashed var(--border);
}
.details-b > :last-child {
  margin-bottom: 0;
}

/* ---------- 手算挑战 ---------- */
.challenge {
  margin: 16px 0 6px;
  padding: 14px 16px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  border: 1px dashed color-mix(in srgb, var(--accent) 55%, transparent);
}
.challenge.is-ok {
  border-style: solid;
  border-color: var(--good);
  background: var(--good-soft);
}
.challenge-hd {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--accent);
  margin-bottom: 4px;
}
.challenge-hd svg {
  width: 18px;
  height: 18px;
}
.challenge-q {
  margin-bottom: 10px;
  font-size: 15px;
}
.challenge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.challenge-row input {
  width: 190px;
  max-width: 100%;
}
.challenge-fb {
  margin-top: 10px;
  font-size: 14px;
  line-height: 1.6;
}
.challenge-fb svg {
  width: 16px;
  height: 16px;
  vertical-align: -3px;
  margin-right: 4px;
}
.challenge-fb.ok {
  color: var(--good);
  font-weight: 700;
}
.challenge-fb.bad {
  color: var(--bad);
}
.challenge-fb.note {
  color: var(--text-2);
}
.challenge-reveal {
  margin-top: 6px;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-weight: 400;
  font-size: 13.5px;
  font-family: var(--font-mono);
  line-height: 1.8;
  overflow-x: auto;
}

/* ---------- 小测验 ---------- */
.quiz {
  display: grid;
  gap: 18px;
}
.quiz-hd {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.quiz-hd b {
  font-size: 18px;
}
.quiz-q {
  display: flex;
  gap: 10px;
  font-weight: 700;
  margin-bottom: 8px;
}
.quiz-no {
  flex: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 13px;
  margin-top: 2px;
}
.quiz-opts {
  display: grid;
  gap: 8px;
}
.quiz-opt {
  appearance: none;
  text-align: left;
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 9px 14px;
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  background: var(--surface);
  cursor: pointer;
  font-size: 15px;
  line-height: 1.55;
  transition: border-color 0.15s, background 0.15s;
}
.quiz-opt:hover:not(:disabled) {
  border-color: var(--brand);
  background: var(--brand-soft);
}
.quiz-opt:disabled {
  cursor: default;
}
.quiz-key {
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: var(--surface-3);
  display: grid;
  place-items: center;
  font-family: var(--font-mono);
  font-size: 12px;
  font-weight: 800;
}
.quiz-opt.is-right {
  border-color: var(--good);
  background: var(--good-soft);
}
.quiz-opt.is-wrong {
  border-color: var(--bad);
  background: var(--bad-soft);
}
.quiz-explain {
  margin-top: 8px;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 14.5px;
  line-height: 1.65;
}
.quiz-explain.ok {
  background: var(--good-soft);
}
.quiz-explain.bad {
  background: var(--warn-soft);
}

/* 窄列矩阵：列头里的词牌缩小，避免被裁切（8 列的 logits / 概率矩阵） */
.mx-sm .mx-ch .tok,
.mx-xs .mx-ch .tok {
  gap: 2px;
  padding: 0 3px;
  font-size: 9.5px;
}
.mx-sm .mx-ch .tok.special,
.mx-xs .mx-ch .tok.special {
  font-size: 9px;
}
.mx-sm .mx-ch .tok .tok-id,
.mx-xs .mx-ch .tok .tok-id {
  display: none;
}
