:root {
  --bg: #ffffff;
  --fg: #1a1d21;
  --muted: #6b7280;
  --line: #e2e5e9;
  --accent: #2563eb;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--fg);
  background: var(--bg);
  line-height: 1.5;
}
.mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 40px 24px 56px;
}

h1 {
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  margin: 0 0 20px;
}

#viewer {
  position: relative;
  width: 100%;
  height: 480px;
  background: #ffffff;
  border: 1px solid var(--line);   /* light, subtle frame (no dark box) */
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0,0,0,0.05);
}

.controls {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 18px;
}

#play {
  flex: 0 0 auto;
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--fg);
  font-size: 0.95rem;
  cursor: pointer;
  transition: border-color 0.15s;
}
#play:hover { border-color: var(--accent); }

#slider {
  flex: 1 1 auto;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  border-radius: 2px;
  background: var(--line);
  outline: none;
}
#slider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); cursor: pointer;
}
#slider::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); border: none; cursor: pointer;
}

#stepLabel {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.9rem;
  min-width: 78px;
  text-align: right;
}

.row2 {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 18px;
}

.sel {
  display: flex; align-items: center; gap: 8px;
  color: var(--muted); font-size: 0.9rem;
}
select {
  background: #fff; color: var(--fg);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 5px 9px; font-size: 0.9rem; cursor: pointer;
}

.legend { display: flex; flex-wrap: wrap; gap: 14px; }
.legend-item {
  display: flex; align-items: center; gap: 6px;
  font-size: 0.9rem; color: var(--fg);
}
.swatch {
  width: 13px; height: 13px; border-radius: 3px;
  border: 1px solid rgba(0,0,0,0.15);
  display: inline-block;
}

/* Tc / guide-w / steps sits ABOVE the horizontal rule; the rule introduces the notes */
.meta {
  margin-top: 22px;
  color: var(--muted);
  font-size: 0.85rem;
}

.how {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  font-size: 0.9rem;
}
.how p { color: var(--muted); margin: 0 0 8px; }
.how ul { margin: 0; padding-left: 18px; }
.how li { margin: 5px 0; }
.how b { font-weight: 600; }

.err {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  text-align: center; color: var(--muted); padding: 24px; gap: 8px;
}
.err code { color: var(--accent); }

/* ---- section layout (shared by the DPO section) ---- */
.section-rule {
  border: none;
  border-top: 1px solid var(--line);
  margin: 44px 0 28px;
}
.dpo h2 { font-size: 1.2rem; font-weight: 600; margin: 0 0 10px; }
.dpo h3 { font-size: 0.95rem; font-weight: 600; margin: 26px 0 8px; }
.dpo .lead { color: var(--muted); font-size: 0.92rem; margin: 0 0 22px; }
.dpo .lead b { color: var(--fg); }
.dpo .lead em { font-style: normal; color: #0d7a70; }

.flow-loop {
  display: flex; align-items: stretch; flex-wrap: wrap; gap: 6px;
  margin: 18px 0 24px;
}
.flow-loop .step {
  flex: 1 1 0; min-width: 120px;
  border: 1px solid var(--line); border-radius: 8px;
  padding: 10px 12px; font-size: 0.85rem; font-weight: 600;
  display: flex; flex-direction: column; gap: 3px;
}
.flow-loop .step small { font-weight: 400; color: var(--muted); font-size: 0.78rem; }
.flow-loop .step .n {
  display: inline-flex; align-items: center; justify-content: center;
  width: 18px; height: 18px; border-radius: 50%;
  background: #0d9488; color: #fff; font-size: 0.72rem; margin-bottom: 2px;
}
.flow-loop .arrow { display: flex; align-items: center; color: var(--muted); font-size: 1rem; }

.chart {
  border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 4px; background: #fff; min-height: 120px;
}
.run-status { margin-top: 16px; color: var(--muted); font-size: 0.85rem; }
.chart-cap { margin: 10px 0 0; color: var(--muted); font-size: 0.85rem; line-height: 1.5; }
.chart-cap em { font-style: normal; color: #b91c1c; font-weight: 600; }

/* DPO result cards — single row of four */
.dpo-cards {
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 10px; margin: 4px 0 6px;
}
.dpo-card {
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 12px; background: #fff;
}
.dc-lab { font-size: 0.8rem; color: var(--muted); display: flex; flex-direction: column; gap: 1px; }
.dc-lab small { font-size: 0.68rem; opacity: 0.8; }
.dc-val { font-size: 1.4rem; font-weight: 650; color: #0d9488; margin: 6px 0 2px; line-height: 1.1; }
.dc-unit { font-size: 0.72rem; font-weight: 400; color: var(--muted); margin-left: 3px; }
.dc-from { font-size: 0.74rem; color: var(--muted); }
.dc-from b { color: var(--fg); font-weight: 600; }
.dc-note { font-size: 0.74rem; margin-top: 6px; color: var(--muted); }
.dc-note.good { color: #15803d; }
@media (max-width: 560px) { .dpo-cards { grid-template-columns: repeat(2, 1fr); } }

/* DPO mathematical formulation */
.dpo-math { margin: 4px 0 8px; }
.dpo-math .mh { color: var(--muted); font-size: 0.88rem; margin: 14px 0 8px; }
.eq {
  font-family: "Cambria Math", Georgia, "Times New Roman", serif;
  font-size: 1.06rem; color: var(--fg);
  background: #f7f9fa; border: 1px solid var(--line); border-radius: 8px;
  padding: 12px 16px; margin: 4px 0; text-align: center;
  overflow-x: auto; white-space: nowrap;
}
.eq sub, .eq sup { font-size: 0.7em; }
.eq .frac { display: inline-flex; flex-direction: column; vertical-align: middle;
  text-align: center; margin: 0 2px; }
.eq .frac > span:first-child { border-bottom: 1px solid currentColor; padding: 0 5px; }
.eq .frac > span:last-child { padding: 0 5px; }
.eq .eq-side { color: var(--muted); font-size: 0.8rem; font-family: var(--mono, ui-monospace, monospace); margin-left: 18px; }
.eq .eq-sep { margin: 0 2px; }
.eq-notes { margin: 12px 0 0; padding-left: 20px; color: var(--fg); font-size: 0.86rem; }
.eq-notes li { margin: 5px 0; }
.eq-notes b { font-weight: 600; }

/* DPO mechanism animation */
.anim-lead { font-size: 0.9rem; }
.dpo-anim { border: 1px solid var(--line); border-radius: 10px; padding: 16px 18px; background: #fff; }
.da-caption { color: #0d9488; font-weight: 600; font-size: 0.92rem; min-height: 1.3em; margin-bottom: 14px; }
.da-stage { display: flex; align-items: center; justify-content: space-between; gap: 18px; flex-wrap: wrap; }
.da-pair { display: flex; flex-direction: column; gap: 10px; }
.da-cryst {
  position: relative; display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--line); border-radius: 9px; padding: 6px 10px; background: #fff;
  transition: box-shadow 0.4s ease, opacity 0.4s ease; width: 230px;
}
.da-mv { width: 86px; height: 74px; flex: 0 0 auto; }
.da-info { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.da-meta { display: flex; flex-direction: column; gap: 1px; font-size: 0.86rem; }
.da-e { font-family: ui-monospace, Menlo, monospace; font-size: 0.76rem; color: var(--muted); opacity: 0; transition: opacity 0.5s; }
.da-tag { font-size: 0.74rem; font-weight: 600; opacity: 0; transition: opacity 0.5s; }
.da-tag.up { color: #15803d; }
.da-tag.down { color: #b91c1c; }
.da-push { font-size: 0.78rem; font-weight: 600; white-space: nowrap; opacity: 0; transition: opacity 0.45s; }
.da-push.up { color: #15803d; }
.da-push.down { color: #b91c1c; }
/* model column */
.da-model { display: flex; flex-direction: column; align-items: center; gap: 0; }
.da-pi { border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; text-align: center;
  font-size: 1rem; display: flex; flex-direction: column; line-height: 1.15; background: #fff; }
.da-pi small { font-size: 0.66rem; color: var(--muted); font-weight: 400; }
.da-pi.theta { border-color: #0d9488; color: #0d7a70; }
.da-tether { width: 0; height: 26px; border-left: 2px dashed var(--line); position: relative;
  display: flex; align-items: center; justify-content: center; transition: border-color 0.4s; }
.da-beta { position: absolute; background: #fff; padding: 0 4px; font-family: "Cambria Math", Georgia, serif;
  font-size: 0.92rem; color: var(--muted); transition: color 0.4s; }
/* sigma column */
.da-sigma { display: flex; flex-direction: column; gap: 4px; width: 220px; }
.da-spark { width: 100%; height: 48px; }
.da-spark-lab { font-size: 0.7rem; color: var(--muted); margin-top: -2px; }
.da-gauge { position: relative; height: 14px; background: #eef1f3; border-radius: 7px; overflow: hidden; margin-top: 6px; }
.da-fill { height: 100%; width: 51%; background: #0d9488; border-radius: 7px; transition: width 1.1s ease; }
.da-mid { position: absolute; left: 50%; top: 0; bottom: 0; width: 1px; background: #9aa3ad; }
.da-sigval { font-size: 0.78rem; color: var(--fg); }

/* phase-driven reveals */
.dpo-anim:not([data-phase="0"]) .da-e,
.dpo-anim:not([data-phase="0"]) .da-tag { opacity: 1; }
.dpo-anim[data-phase="2"] .da-push,
.dpo-anim[data-phase="3"] .da-push,
.dpo-anim[data-phase="4"] .da-push { opacity: 1; }
.dpo-anim[data-phase="2"] .da-cryst.win,
.dpo-anim[data-phase="3"] .da-cryst.win,
.dpo-anim[data-phase="4"] .da-cryst.win { box-shadow: 0 0 0 2px #15803d; }
.dpo-anim[data-phase="2"] .da-cryst.lose,
.dpo-anim[data-phase="3"] .da-cryst.lose,
.dpo-anim[data-phase="4"] .da-cryst.lose { opacity: 0.5; }
.dpo-anim[data-phase="3"] .da-tether,
.dpo-anim[data-phase="4"] .da-tether { border-color: #0d9488; }
.dpo-anim[data-phase="3"] .da-beta,
.dpo-anim[data-phase="4"] .da-beta { color: #0d7a70; }
