/* แผนภาพบทเรียน (content/diagrams.js) — flat editorial ตามพาเลตใน DESIGN.md */
.dg {
  margin: 22px 0;
}
.dg-badge {
  flex: none;
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--soft);
  color: var(--blue);
}
.dg-arrow {
  display: inline-grid;
  place-items: center;
  color: var(--muted);
}

/* ---------- ห่วงโซ่เหตุการณ์ ---------- */
.dg-chain {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  align-items: stretch;
  gap: 6px;
}
.dg-sep {
  display: grid;
  place-items: center;
  flex: none;
}
.dg-chain {
  flex-wrap: wrap;
  row-gap: 10px;
}
.dg-node {
  flex: 1 1 110px;
  display: grid;
  align-content: center;
  gap: 2px;
  text-align: center;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 10px;
  font-size: var(--text-small);
  font-weight: 600;
  box-shadow: var(--shadow-1);
}
.dg-node small {
  font-size: var(--text-meta);
  font-weight: 600;
  color: var(--muted);
}
.dg-node.focus {
  background: var(--navy);
  border-color: var(--navy);
  color: #fff;
  box-shadow: var(--shadow-2);
}
.dg-node.focus small {
  color: var(--gold);
}
.dg-node.end {
  background: var(--danger-soft);
  border-color: var(--danger);
  color: var(--danger);
}

/* ---------- why-why tree ---------- */
.dg-tree {
  display: grid;
  justify-items: center;
}
.dg-root {
  background: var(--navy);
  color: #fff;
  font-weight: 700;
  border-radius: 10px;
  padding: 10px 18px;
  text-align: center;
  box-shadow: var(--shadow-2);
  position: relative;
}
.dg-root::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -18px;
  height: 18px;
  border-left: 2px solid var(--line);
}
.dg-branches {
  list-style: none;
  margin: 18px 0 0;
  padding: 18px 0 0;
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 14px;
  width: 100%;
  position: relative;
}
/* เส้นแนวนอนเชื่อมกิ่ง และเส้นตั้งลงแต่ละกิ่ง */
.dg-branches::before {
  content: "";
  position: absolute;
  top: 0;
  left: calc(100% / var(--n, 3) / 2);
  right: calc(100% / var(--n, 3) / 2);
  border-top: 2px solid var(--line);
}
.dg-branches > li {
  position: relative;
  display: grid;
  align-content: start;
  gap: 8px;
}
.dg-branches > li::before {
  content: "";
  position: absolute;
  top: -18px;
  left: 50%;
  height: 18px;
  border-left: 2px solid var(--line);
}
.dg-branch {
  background: var(--blue-solid);
  color: #fff;
  font-weight: 600;
  border-radius: 10px;
  padding: 9px 12px;
  text-align: center;
  font-size: var(--text-small);
  box-shadow: var(--shadow-1);
}
.dg-leaves {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}
.dg-leaves li {
  background: var(--soft);
  border: 1px solid color-mix(in srgb, var(--blue) 25%, transparent);
  border-radius: 8px;
  padding: 7px 10px;
  font-size: var(--text-small);
  text-align: center;
}

/* ---------- ระบบใหญ่และระบบย่อย ---------- */
.dg-system {
  border: 2px dashed var(--line);
  border-radius: 16px;
  padding: 16px;
  background: var(--paper);
  display: grid;
  gap: 14px;
}
.dg-sys-title {
  margin: 0;
  font-size: var(--text-meta);
  font-weight: 700;
  color: var(--muted);
}
.dg-sys-center {
  display: flex;
  justify-content: center;
}
.dg-sys-center .dg-sys-node {
  max-width: 420px;
}
.dg-sys-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
}
.dg-sys-node {
  display: flex;
  gap: 10px;
  align-items: center;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: var(--text-small);
}
.dg-sys-node small {
  display: block;
  color: var(--muted);
  font-size: var(--text-meta);
}
.dg-sys-node.focus {
  background: var(--navy);
  border-color: var(--navy);
  color: #fff;
  box-shadow: var(--shadow-2);
}
.dg-sys-node.focus small {
  color: var(--gold);
}
.dg-sys-node.focus .dg-badge {
  background: rgb(255 255 255 / 14%);
  color: #fff;
}
.dg-sys-sub {
  border: 2px dashed color-mix(in srgb, var(--blue) 45%, transparent);
  border-radius: 12px;
  padding: 12px;
  background: color-mix(in srgb, var(--soft) 60%, transparent);
  display: grid;
  gap: 10px;
}
.dg-sys-sub .dg-sys-title {
  color: var(--blue);
}
.dg-sys-sub .dg-sys-node {
  background: var(--paper);
}

/* ---------- SVG: run chart และ Swiss cheese ---------- */
.dg-svg {
  display: block;
  width: 100%;
  height: auto;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  font-family: var(--font-sans);
}
.dg-axis {
  stroke: var(--line);
  stroke-width: 2;
}
.dg-band {
  fill: var(--soft);
}
.dg-median {
  stroke: var(--muted);
  stroke-width: 1.5;
  stroke-dasharray: 6 5;
}
.dg-change {
  stroke: var(--gold-ink);
  stroke-width: 2;
  stroke-dasharray: 4 5;
}
.dg-line {
  fill: none;
  stroke: var(--blue);
  stroke-width: 3;
  stroke-linejoin: round;
}
.dg-dot {
  fill: var(--blue);
  stroke: var(--paper);
  stroke-width: 2;
}
.dg-dot-before {
  fill: var(--muted);
  stroke: var(--paper);
  stroke-width: 2;
}
.dg-mark circle {
  fill: var(--blue-solid);
}
.dg-mark text {
  fill: #fff;
  font-size: var(--text-small);
  font-weight: 700;
  text-anchor: middle;
}
.dg-cheese {
  fill: var(--gold);
}
.dg-cheese-edge {
  fill: var(--navy);
  opacity: 0.12;
}
.dg-hole {
  fill: var(--paper);
}
.dg-hazard {
  fill: none;
  stroke: var(--danger);
  stroke-width: 4;
  stroke-linecap: round;
}
.dg-hazard-head {
  fill: none;
  stroke: var(--danger);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.dg-blocked {
  stroke: var(--muted);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-dasharray: 2 7;
}
.dg-patient rect,
.dg-patient circle {
  fill: var(--ink);
}
/* ---------- Impact-Effort matrix ---------- */
.dg-mx {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: 1fr auto;
  gap: 8px 10px;
}
.dg-mx-y {
  grid-row: 1;
  display: flex;
  gap: 4px;
  font-size: var(--text-meta);
  font-weight: 700;
  color: var(--muted);
}
.dg-mx-name {
  font-weight: 700;
  color: var(--ink);
}
.dg-mx-y .dg-mx-name {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  align-self: center;
}
.dg-mx-scale {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.dg-mx-x {
  grid-column: 2;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--text-meta);
  font-weight: 700;
  color: var(--muted);
}
/* เส้นแกนยืดเต็มความยาว หัวลูกศรชี้ไปทางค่ามาก */
.dg-mx-line {
  flex: 1;
  position: relative;
  background: currentColor;
}
.dg-mx-y .dg-mx-line {
  width: 2px;
  margin-top: 8px;
}
.dg-mx-x .dg-mx-line {
  height: 2px;
  margin-right: 8px;
}
.dg-mx-line::after {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  background: currentColor;
}
.dg-mx-y .dg-mx-line::after {
  top: -10px;
  left: -5px;
  clip-path: polygon(50% 0, 100% 100%, 0 100%);
}
.dg-mx-x .dg-mx-line::after {
  right: -10px;
  top: -5px;
  clip-path: polygon(0 0, 100% 50%, 0 100%);
}
.dg-mx-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.dg-mx-q {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 16px;
  display: grid;
  align-content: start;
  gap: 6px;
  min-height: 150px;
}
.dg-mx-q p {
  margin: 0;
  font-size: var(--text-small);
  color: var(--muted);
}
.dg-mx-head {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--text-lead);
}
.dg-mx-items {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 4px;
}
.dg-mx-items span {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: var(--text-meta);
  font-weight: 600;
  box-shadow: var(--shadow-1);
}
.q-win {
  background: var(--ok-soft);
  border-color: color-mix(in srgb, var(--ok) 35%, transparent);
}
.q-win .dg-mx-head {
  color: var(--ok);
}
.q-major {
  background: var(--soft);
}
.q-major .dg-mx-head {
  color: var(--blue);
}
.q-fill {
  background: var(--paper);
}
.q-waste {
  background: var(--danger-soft);
  border-color: color-mix(in srgb, var(--danger) 30%, transparent);
}
.q-waste .dg-mx-head {
  color: var(--danger);
}
@media (max-width: 560px) {
  .dg-mx-q {
    padding: 10px 12px;
    min-height: 0;
  }
  .dg-mx-head {
    font-size: var(--text-body);
  }
}

/* ---------- Causal loop diagram ---------- */
.dg-cld text {
  font-family: var(--font-sans);
  text-anchor: middle;
}
.dg-cld-node rect {
  fill: var(--paper);
  stroke: var(--line);
  stroke-width: 2;
}
.dg-cld-node text {
  fill: var(--ink);
  font-size: 18px;
  font-weight: 700;
}
.dg-cld-node.key rect {
  fill: var(--navy);
  stroke: var(--navy);
}
.dg-cld-node.key text {
  fill: #fff;
}
.dg-cld-link {
  fill: none;
  stroke-width: 3;
  stroke-linecap: round;
}
.dg-cld-link.r {
  stroke: var(--danger);
}
.dg-cld-link.b {
  stroke: var(--ok);
}
.dg-cld-headfill {
  fill: var(--muted);
}
.dg-cld-pol circle {
  fill: var(--paper);
  stroke-width: 2;
}
.dg-cld-pol text {
  font-size: 18px;
  font-weight: 700;
}
.dg-cld-pol.r circle {
  stroke: var(--danger);
}
.dg-cld-pol.r text {
  fill: var(--danger);
}
.dg-cld-pol.b circle {
  stroke: var(--ok);
}
.dg-cld-pol.b text {
  fill: var(--ok);
}
.dg-cld-loop path {
  fill: none;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.dg-cld-loop text {
  font-size: 18px;
  font-weight: 700;
}
.dg-cld-loop.r path {
  stroke: var(--danger);
}
.dg-cld-loop.r text {
  fill: var(--danger);
}
.dg-cld-loop.b path {
  stroke: var(--ok);
}
.dg-cld-loop.b text {
  fill: var(--ok);
}
.dg-cld-delay line {
  stroke: var(--ink);
  stroke-width: 3;
  stroke-linecap: round;
}

/* ---------- วงจรขั้นตอน (cycle) ---------- */
.dg-cy-node rect {
  fill: var(--navy);
  filter: drop-shadow(0 4px 8px rgb(0 0 0 / 16%));
}
.dg-cy-node.tone-1 rect {
  fill: var(--blue-solid);
}
.dg-cy-node.tone-2 rect {
  fill: var(--gold);
}
.dg-cy-node .ico {
  color: #fff;
}
.dg-cy-node text {
  fill: #fff;
  font-family: var(--font-sans);
  font-size: 20px;
  font-weight: 700;
  text-anchor: middle;
}
.dg-cy-node.tone-2 .ico,
.dg-cy-node.tone-2 text {
  color: var(--navy);
  fill: var(--navy);
}
.dg-cy-link {
  fill: none;
  stroke: var(--blue);
  stroke-width: 3;
  stroke-linecap: round;
}
.dg-cy-link.back {
  stroke-dasharray: 8 7;
}
.dg-cy-headfill {
  fill: var(--blue);
}
.dg-cy-loopfill {
  fill: var(--gold-ink);
}
.dg-cy-loop path {
  fill: none;
  stroke: var(--gold-ink);
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.dg-cy-loop text {
  fill: var(--ink);
  font-family: var(--font-sans);
  font-size: 18px;
  font-weight: 700;
  text-anchor: middle;
}
.dg-cy-cards {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}
.dg-cy-cards > li {
  background: var(--paper);
  border: 1px solid var(--line);
  border-top: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: var(--shadow-1);
  background-image: linear-gradient(var(--navy), var(--navy));
  background-repeat: no-repeat;
  background-size: 100% 5px;
}
.dg-cy-cards > li.tone-1 {
  background-image: linear-gradient(var(--blue-solid), var(--blue-solid));
}
.dg-cy-cards > li.tone-2 {
  background-image: linear-gradient(var(--gold), var(--gold));
}
.dg-cy-cards b {
  display: block;
  margin-top: 4px;
}
.dg-cy-cards small {
  color: var(--muted);
  font-size: var(--text-meta);
}
.dg-cy-cards ul {
  margin: 6px 0 0;
  padding-left: 18px;
  font-size: var(--text-small);
}
@media (max-width: 760px) {
  .dg-cy-cards {
    grid-template-columns: 1fr;
  }
}

/* ทีมรอบโต๊ะ (stakeholders) */
.dg-sh-ring {
  stroke: var(--paper);
  stroke-width: 3;
}
.dg-sh-table {
  fill: var(--paper);
  stroke: var(--line);
  stroke-width: 2;
  filter: drop-shadow(0 4px 8px rgb(0 0 0 / 14%));
}
.dg-sh-body {
  stroke: var(--navy);
  stroke-width: 2;
}
.dg-sh-head {
  fill: var(--navy);
}
.dg-sh-notes rect {
  stroke: var(--line);
  stroke-width: 1;
}
.dg-legend {
  margin: 12px 0 0;
  padding: 0;
  list-style: none;
  counter-reset: dg;
  display: grid;
  gap: 6px;
  font-size: var(--text-small);
}
/* ข้อความในรายการไหลเป็นย่อหน้าเดียว เลขกำกับอยู่ด้านซ้าย */
.dg-legend li {
  counter-increment: dg;
  position: relative;
  padding-left: 32px;
}
.dg-legend li::before {
  content: counter(dg);
  position: absolute;
  left: 0;
  top: 2px;
  display: inline-grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--blue-solid);
  color: #fff;
  font-size: var(--text-meta);
  font-weight: 700;
}

/* ---------- จอเล็ก: เรียงลงแนวตั้ง ลูกศรชี้ลง ---------- */
@media (max-width: 760px) {
  .dg-chain {
    flex-direction: column;
  }
  .dg-node {
    flex: none;
  }
  .dg-sep .dg-arrow {
    transform: rotate(90deg);
  }
  .dg-branches {
    grid-auto-flow: row;
    padding: 0;
    margin-top: 10px;
  }
  .dg-branches::before,
  .dg-branches > li::before,
  .dg-root::after {
    display: none;
  }
  .dg-branches > li {
    border-left: 2px solid var(--line);
    padding-left: 12px;
  }
  .dg-leaves li {
    text-align: left;
  }
}

/* ---------- แผนภาพปัจจัยที่ส่งผล (content/contrib.js) ---------- */
.cf {
  margin: 24px 0;
}
.cf-scroll {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  box-shadow: var(--shadow-1);
  overscroll-behavior-x: contain;
}
.cf-board {
  --cf-label: 156px;
  position: relative;
  min-width: calc(var(--cf-label) + var(--cf-cols) * 164px);
}
.cf-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 6px 14px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--gold) 16%, var(--paper));
}
.cf-head span {
  color: var(--muted);
  font-size: var(--text-small);
}
.cf-head b {
  font-size: var(--text-lead);
  color: var(--ink);
}
.cf-sev {
  font-style: normal;
  font-size: var(--text-meta);
  font-weight: 600;
  padding: 4px 12px;
  border-radius: 99px;
  border: 1px solid var(--line);
  background: var(--paper);
}
.cf-sev.mid {
  color: var(--warn);
  border-color: var(--warn-line);
  background: var(--warn-soft);
}
.cf-sev.high {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 35%, transparent);
  background: var(--danger-soft);
}
.cf-grid {
  display: grid;
  grid-template-columns: var(--cf-label) repeat(var(--cf-cols), minmax(0, 1fr));
}
.cf-lane,
.cf-cell,
.cf-timeline {
  border-bottom: 1px solid var(--line);
}
.cf-lane {
  grid-column: 1;
  position: sticky;
  left: 0;
  z-index: 3;
  display: grid;
  align-content: center;
  gap: 2px;
  padding: 12px 14px;
  background: color-mix(in srgb, var(--soft) 55%, var(--paper));
  border-right: 1px solid var(--line);
  line-height: 1.4;
}
.cf-lane b {
  font-size: var(--text-small);
  color: var(--ink);
}
.cf-lane small {
  font-size: var(--text-meta);
  color: var(--muted);
}
.cf-lane.key {
  background: color-mix(in srgb, var(--gold) 14%, var(--paper));
}
.cf-lane.band {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px 10px;
  padding: 8px 14px;
  border-right: 0;
}
.cf-lane.band small {
  display: inline;
}
.cf-cell {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 12px;
  min-height: 52px;
  padding: 18px 14px;
}
.cf-box {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 240px;
  display: grid;
  gap: 2px;
  padding: 8px 12px 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--paper);
  box-shadow: var(--shadow-1);
  font-size: var(--text-small);
  line-height: 1.45;
  color: var(--ink);
}
.cf-box small {
  font-size: var(--text-meta);
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--muted);
}
.cf-box.practice {
  background: var(--ok-soft);
  border-color: color-mix(in srgb, var(--ok) 35%, var(--line));
}
.cf-box.practice small {
  color: var(--ok);
}
.cf-box.cmp {
  background: var(--warn-soft);
  border-color: var(--warn-line);
}
.cf-box.cmp small {
  color: var(--warn);
}
.cf-timeline {
  grid-column: 1 / -1;
  list-style: none;
  margin: 0;
  padding: 14px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(104px, 1fr));
  gap: 12px 18px;
  border-bottom: 0;
}
.cf-box.time {
  max-width: none;
  background: var(--paper);
  border-color: var(--line);
}
.cf-box.time small {
  color: var(--muted);
}
.cf-box.time + .cf-box.time::before {
  content: "";
  position: absolute;
  left: -14px;
  top: 50%;
  width: 8px;
  height: 8px;
  border-top: 2px solid var(--muted);
  border-right: 2px solid var(--muted);
  transform: translateY(-50%) rotate(45deg);
}
.cf-box.event {
  background: var(--danger-soft);
  border-color: color-mix(in srgb, var(--danger) 40%, var(--line));
}
.cf-box.event small {
  color: var(--danger);
}
.cf-wires {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  pointer-events: none;
  overflow: visible;
}
.cf-wires path {
  fill: none;
  stroke: var(--muted);
  stroke-width: 1.6;
  opacity: 0.85;
}
.cf-wires path.dash {
  stroke-dasharray: 4 5;
}
.cf-wires path.up {
  stroke: var(--danger);
  stroke-dasharray: 6 4;
}
.cf-wires marker path {
  fill: var(--muted);
  stroke: none;
}
.cf-wires marker.up path {
  fill: var(--danger);
}
.cf-box.factor.loose {
  border-style: dashed;
  background: var(--paper);
}
.cf-box.factor.loose small {
  color: var(--muted);
}
.cf-scroll:focus-visible {
  outline-offset: 3px;
}
/* คำอธิบายเส้นและข้อความแทนแผนภาพ */
.cf-legend {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 18px;
  margin: 10px 2px 0;
  padding: 0;
  color: var(--muted);
  font-size: var(--text-meta);
}
.cf-legend li {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}
.cf-legend li.warn {
  color: var(--danger);
}
.k-line {
  width: 26px;
  border-top: 2px solid var(--muted);
}
.k-line.dash {
  border-top-style: dashed;
}
.k-line.up {
  border-top: 2px dashed var(--danger);
}
.k-box {
  width: 18px;
  height: 12px;
  border: 1px dashed var(--muted);
  border-radius: 8px;
}
.cf-text {
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  padding: 4px 14px;
  font-size: var(--text-small);
}
.cf-text summary {
  cursor: pointer;
  min-height: 44px;
  display: flex;
  align-items: center;
  color: var(--blue);
  font-weight: 600;
}
.cf-text ol {
  margin: 4px 0 12px;
  padding-left: 22px;
}
.cf-text small {
  font-size: var(--text-meta);
}
.cf-text ol > li {
  margin: 0 0 10px;
}
.cf-text ul {
  margin: 2px 0;
  padding-left: 18px;
}
@media (max-width: 760px) {
  .cf-board {
    --cf-label: 124px;
  }
  .cf-lane {
    padding: 10px;
  }
  .cf-lane small {
    display: none;
  }
  .cf-head {
    justify-content: flex-start;
  }
}

/* กล่องที่ถ้อยคำฟังเหมือนโทษคน หรือมีลิงก์ชี้ขึ้น: เส้นประสีเตือน เห็นได้บนแผนภาพที่นำไปคุยกับทีม */
.cf-box.flagged {
  border: 2px dashed var(--warn);
}
.k-box.flagged {
  border: 2px dashed var(--warn);
}
