/* eslint-disable */
// ============================================================
// ASTEM — Decision tree (Arbre de décision)
// 8 steps; sections 1,3,4,5,6,7 per-implant; section 2 global.
// ============================================================

const { useState, useMemo, useEffect } = React;

// --- small icons used in choice cards ---
function ChoiceIcon({ kind }) {
  const props = { width: 60, height: 60, viewBox: "0 0 60 60" };
  switch (kind) {
    case "implant":
      // generic implant w/ crown
      return (
        <svg {...props}>
          <path d="M22 14 L38 14 L36 22 L24 22 Z" fill="#E8E1F3" stroke="#3D2EB8" strokeWidth="1.5" />
          <path d="M26 22 L34 22 L33 30 L27 30 Z" fill="#F5F2FC" stroke="#6A5FCC" strokeWidth="1" />
          <path d="M27 30 L33 30 L30 56 Z" fill="#C9C2E3" stroke="#3D2EB8" strokeWidth="1.2" />
          <path d="M27 34 L33 34 M27 38 L33 38 M27 42 L33 42 M27 46 L33 46 M28 50 L32 50" stroke="#7B72A8" strokeWidth="0.8" />
        </svg>);

    case "screwed":
      return (
        <svg {...props}>
          <path d="M22 12 L38 12 L36 24 L24 24 Z" fill="#E8E1F3" stroke="#3D2EB8" strokeWidth="1.5" />
          <circle cx="30" cy="10" r="3" fill="#FFFFFF" stroke="#3D2EB8" strokeWidth="1.5" />
          <path d="M27 24 L33 24 L30 54 Z" fill="#C9C2E3" stroke="#3D2EB8" strokeWidth="1.2" />
          <path d="M27 28 L33 28 M27 32 L33 32 M27 36 L33 36 M28 40 L32 40 M28 44 L32 44 M28 48 L32 48" stroke="#7B72A8" strokeWidth="0.7" />
        </svg>);

    case "sealed":
      return (
        <svg {...props}>
          <path d="M22 16 L38 16 L36 26 L24 26 Z" fill="#E8E1F3" stroke="#3D2EB8" strokeWidth="1.5" />
          <path d="M27 26 L33 26 L30 54 Z" fill="#C9C2E3" stroke="#3D2EB8" strokeWidth="1.2" />
          <path d="M27 30 L33 30 M27 34 L33 34 M28 38 L32 38 M28 42 L32 42 M28 46 L32 46" stroke="#7B72A8" strokeWidth="0.7" />
        </svg>);

    case "papsi":
      // removable prosthesis (gum-colored arch)
      return (
        <svg {...props}>
          <path d="M8 22 Q30 6 52 22 Q52 36 44 38 Q30 36 16 38 Q8 36 8 22 Z" fill="#E89A8C" stroke="#B4604F" strokeWidth="1.5" />
          <path d="M14 22 Q30 14 46 22" fill="none" stroke="#A9533F" strokeWidth="0.8" opacity="0.5" />
          <g fill="#F5F2FC" stroke="#3D2EB8" strokeWidth="0.8">
            <ellipse cx="15" cy="29" rx="3" ry="4" />
            <ellipse cx="22" cy="33" rx="3" ry="4" />
            <ellipse cx="30" cy="34" rx="3" ry="4" />
            <ellipse cx="38" cy="33" rx="3" ry="4" />
            <ellipse cx="45" cy="29" rx="3" ry="4" />
          </g>
        </svg>);

    case "locator":
      return (
        <svg {...props}>
          <rect x="22" y="14" width="16" height="10" rx="3" fill="#E8E1F3" stroke="#3D2EB8" strokeWidth="1.5" />
          <circle cx="30" cy="19" r="2" fill="#3D2EB8" />
          <path d="M27 24 L33 24 L30 54 Z" fill="#C9C2E3" stroke="#3D2EB8" strokeWidth="1.2" />
          <path d="M27 30 L33 30 M28 35 L32 35 M28 42 L32 42 M28 48 L32 48" stroke="#7B72A8" strokeWidth="0.7" />
        </svg>);

    case "telescope":
      return (
        <svg {...props}>
          <path d="M22 12 L38 12 L36 22 L24 22 Z" fill="#E8E1F3" stroke="#3D2EB8" strokeWidth="1.5" />
          <path d="M24 22 L36 22 L34 28 L26 28 Z" fill="#F5F2FC" stroke="#6A5FCC" strokeWidth="1" />
          <path d="M27 28 L33 28 L30 54 Z" fill="#C9C2E3" stroke="#3D2EB8" strokeWidth="1.2" />
        </svg>);

    case "bar":
      return (
        <svg {...props}>
          <rect x="12" y="16" width="36" height="6" rx="3" fill="#E8E1F3" stroke="#3D2EB8" strokeWidth="1.5" />
          <path d="M18 22 L20 30 L18 50 L14 50 Z" fill="#C9C2E3" stroke="#3D2EB8" strokeWidth="1" />
          <path d="M42 22 L40 30 L42 50 L46 50 Z" fill="#C9C2E3" stroke="#3D2EB8" strokeWidth="1" />
        </svg>);

    case "tooth-1":
      return (
        <svg {...props}>
          <path d="M30 8 Q22 8 20 16 Q19 26 22 36 Q24 52 30 52 Q36 52 38 36 Q41 26 40 16 Q38 8 30 8 Z" fill="url(#tg-1)" stroke="#3D2EB8" strokeWidth="1.5" />
          <defs><radialGradient id="tg-1" cx="50%" cy="30%" r="60%"><stop offset="0%" stopColor="#FFFFFF" /><stop offset="100%" stopColor="#D9CFEF" /></radialGradient></defs>
        </svg>);

    case "tooth-3":
      return (
        <svg {...props}>
          {[10, 30, 50].map((x, i) =>
          <path key={i} d={`M${x} 12 Q${x - 6} 12 ${x - 7} 18 Q${x - 7} 28 ${x - 5} 38 Q${x - 3} 52 ${x} 52 Q${x + 3} 52 ${x + 5} 38 Q${x + 7} 28 ${x + 7} 18 Q${x + 6} 12 ${x} 12 Z`} fill="url(#tg-3)" stroke="#3D2EB8" strokeWidth="1.3" />
          )}
          <defs><radialGradient id="tg-3" cx="50%" cy="30%" r="60%"><stop offset="0%" stopColor="#FFFFFF" /><stop offset="100%" stopColor="#D9CFEF" /></radialGradient></defs>
        </svg>);

    case "arch-up":
      return (
        <svg {...props}>
          <path d="M6 38 Q30 8 54 38" fill="none" stroke="#3D2EB8" strokeWidth="2" />
          {Array.from({ length: 7 }).map((_, i) => {
            const t = i / 6;
            const x = 6 + 48 * t;
            const y = 38 - 30 * Math.sin(Math.PI * t);
            return <ellipse key={i} cx={x} cy={y} rx="4" ry="5" fill="#F5F2FC" stroke="#3D2EB8" strokeWidth="1" />;
          })}
        </svg>);

    case "arch-down":
      return (
        <svg {...props}>
          <path d="M6 22 Q30 52 54 22" fill="none" stroke="#3D2EB8" strokeWidth="2" />
          {Array.from({ length: 7 }).map((_, i) => {
            const t = i / 6;
            const x = 6 + 48 * t;
            const y = 22 + 30 * Math.sin(Math.PI * t);
            return <ellipse key={i} cx={x} cy={y} rx="4" ry="5" fill="#F5F2FC" stroke="#3D2EB8" strokeWidth="1" />;
          })}
        </svg>);

    case "arch-both":
      return (
        <svg {...props}>
          <path d="M6 26 Q30 4 54 26" fill="none" stroke="#3D2EB8" strokeWidth="1.6" />
          <path d="M6 34 Q30 56 54 34" fill="none" stroke="#3D2EB8" strokeWidth="1.6" />
          {Array.from({ length: 5 }).map((_, i) => {
            const t = (i + 1) / 6;
            const x = 6 + 48 * t;
            const yt = 26 - 22 * Math.sin(Math.PI * t);
            const yb = 34 + 22 * Math.sin(Math.PI * t);
            return <g key={i}>
              <ellipse cx={x} cy={yt} rx="3" ry="4" fill="#F5F2FC" stroke="#3D2EB8" strokeWidth="0.8" />
              <ellipse cx={x} cy={yb} rx="3" ry="4" fill="#F5F2FC" stroke="#3D2EB8" strokeWidth="0.8" />
            </g>;
          })}
        </svg>);

    default:
      // Generic tooth placeholder
      return (
        <svg {...props}>
          <path d="M30 8 Q22 8 19 14 Q16 24 18 36 Q19 52 30 52 Q41 52 42 36 Q44 24 41 14 Q38 8 30 8 Z"
          fill="url(#tg-1)" stroke="#3D2EB8" strokeWidth="1.5" />
          <defs><radialGradient id="tg-def" cx="50%" cy="30%" r="60%"><stop offset="0%" stopColor="#FFFFFF" /><stop offset="100%" stopColor="#D9CFEF" /></radialGradient></defs>
        </svg>);

  }
}

// --- vertical stepper used in tree (left column) ---
function TreeStepper({ steps, currentIdx, onStep, done, warnings }) {
  return (
    <div className="tree-stepper-v">
      {steps.map((s, i) =>
      <div key={s.id}
      className={`tsv-step ${i === currentIdx ? "active" : ""} ${done[s.id] ? "done" : ""} ${warnings[s.id] ? "warn" : ""}`}
      onClick={() => onStep(i)}>
        
          {i > 0 && <div className={`tsv-connector ${done[steps[i - 1].id] ? "done" : ""}`} />}
          <div className="tsv-row">
            <div className="tsv-dot">
              {done[s.id] && !warnings[s.id] ?
            <svg width="8" height="8" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="4"><path d="M5 12l5 5 9-9" /></svg> :
            <span>{s.num}</span>
            }
            </div>
            <div className="tsv-label">{s.short}</div>
            {warnings[s.id] && <div className="tsv-warn">⚠</div>}
          </div>
        </div>
      )}
    </div>);

}

// --- recap rail (right side, fills as user picks) ---
function RecapRail({ stepNum, stepShort, picks, currentStepLabel, currentStepSubTitle }) {
  // picks: array of {label, value, alert, prev?}
  return (
    <aside className="recap-rail">
      <div className="head">
        <div className="num">{stepNum}</div>
        <div className="ttl">{stepShort}</div>
        <div className="doc-ic">
          <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><rect x="5" y="3" width="14" height="18" rx="2" /><path d="M9 7h6M9 11h6M9 15h4" /></svg>
        </div>
      </div>
      {picks.map((p, i) =>
      <div key={i} className={`recap-section ${i < picks.length - 1 ? "line" : ""}`}>
          <div className={`marker ${p.alert ? "alert" : ""}`}>
            {p.alert ? "!" : "✓"}
          </div>
          <div className="label">{p.label}</div>
          <div className="row">
            {p.prev && <span className="pill-prev">{p.prev}</span>}
            <span className={`pill-curr ${p.alert ? "alert" : ""}`}>{p.value} <span className="check">✓</span></span>
          </div>
        </div>
      )}
    </aside>);

}

// --- The choices view (depth-first walker through a question tree) ---
function QuestionView({ question, path, onChoose, onBack, visibilityCtx }) {
  // Filtrage des options selon les règles de masquage (propriété `visible`)
  const ctx = visibilityCtx || {};
  const visibleOpts = (question.options || []).filter((opt) =>
  !opt.visible || opt.visible(ctx)
  );
  // question may have title/lede and options[]
  return (
    <div className="tree-main">
      <h2>{question.title}</h2>
      {question.lede && <p className="lede">{question.lede}</p>}
      <div className={`choices-grid ${visibleOpts.length === 2 ? "two" : ""}`}>
        {visibleOpts.map((opt) =>
        <div
          key={opt.id}
          className={`choice-card ${opt.alert ? "alert" : ""}`}
          onClick={() => onChoose(opt)}>
          
            <div className="help" title="Aide">?</div>
            {opt.icon && <ChoiceIcon kind={opt.icon} />}
            <div className="title">{opt.label}</div>
            {opt.sub && <div className="sub">{opt.sub}</div>}
            {opt.alert && opt.acts && opt.acts.includes(26) &&
          <div className="alert-pill">Insuffisant <span className="num">26</span></div>
          }
          </div>
        )}
      </div>
      {path.length > 0 &&
      <div className="bottombar">
          <button className="btn btn-ghost" onClick={onBack}>
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5"><path d="M15 18l-6-6 6-6" /></svg>
            Précédent
          </button>
        </div>
      }
    </div>);

}

// --- Step walker: handles drilling into nested questions for one step ---
function StepWalker({ step, value, onUpdate, onComplete, gotoNextStep, gotoPrevStep, nextStepLabel, visibilityCtx }) {
  // value is an array of selections {questionId, optId, opt}
  // we walk forward until we hit a leaf (option with no .next)
  const path = value || [];

  // determine current question by walking the tree
  let q = step;
  for (const sel of path) {
    if (q.options) {
      const opt = q.options.find((o) => o.id === sel.optId);
      if (!opt || !opt.next) {q = null;break;}
      q = opt.next;
    }
  }

  // synthèse step?
  if (step.id === "synthese") {
    return null; // handled elsewhere
  }

  if (!q) {
    // we reached a leaf; show summary mini panel
    const leaf = path[path.length - 1];
    return (
      <div className="tree-main">
        <h2>{step.title}</h2>
        <p className="lede">Choix enregistré.</p>
        <div style={{ background: "white", borderRadius: 16, padding: 24, maxWidth: 560, boxShadow: "var(--shadow-card)" }}>
          <div style={{ fontWeight: 600, marginBottom: 8 }}>Récapitulatif</div>
          {path.map((p, i) =>
          <div key={i} style={{ display: "flex", gap: 10, padding: "8px 0", borderBottom: "1px solid var(--line-2)" }}>
              <span className="muted" style={{ minWidth: 80 }}>{p.questionLabel}</span>
              <span style={{ fontWeight: 600 }}>{p.optLabel}</span>
            </div>
          )}
        </div>
        <div className="bottombar">
          <div className="spacer" />
          <button className="btn btn-ghost" onClick={() => onUpdate(path.slice(0, -1))}>
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5"><path d="M15 18l-6-6 6-6" /></svg>
            Modifier
          </button>
          <button className="btn btn-primary" onClick={gotoNextStep}>
            {nextStepLabel || "Étape suivante"}
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5"><path d="M9 18l6-6-6-6" /></svg>
          </button>
        </div>
      </div>);

  }

  return (
    <QuestionView
      question={q}
      path={path}
      visibilityCtx={visibilityCtx}
      onChoose={(opt) => {
        const newPath = [...path, {
          questionId: q.question || step.id,
          questionLabel: q.title || step.title,
          optId: opt.id,
          optLabel: opt.label,
          alert: !!opt.alert,
          acts: opt.acts || [],
          hasNext: !!opt.next // permet de savoir si l'étape est complète (feuille atteinte)
        }];
        onUpdate(newPath);
      }}
      onBack={() => onUpdate(path.slice(0, -1))} />);


}

window.TreeStepper = TreeStepper;
window.RecapRail = RecapRail;
window.StepWalker = StepWalker;
window.ChoiceIcon = ChoiceIcon;

// ============================================================
// MiniDentalChart — compact 2-row schema used in the decision tree
// Shows planned state per tooth + which implants have their tree
// filled / partial / to-do. Click an implant to navigate to it.
// ============================================================
function MiniDentalChart({ planning, currentImplant, onSelectImplant, treeData, treeStepsCount }) {
  // FDI rows, panoramic convention (viewer left = patient right)
  const upperRow = [18, 17, 16, 15, 14, 13, 12, 11, 21, 22, 23, 24, 25, 26, 27, 28];
  const lowerRow = [48, 47, 46, 45, 44, 43, 42, 41, 31, 32, 33, 34, 35, 36, 37, 38];

  function stateOf(p) {
    if (!p) return "natural";
    const pr = p.prothese || {};
    // Implant prend la priorité visuelle sur absent/extraction
    if (pr.implant) return "implant";
    if (pr.couronne) return "crown";
    if (pr.bridge) return "bridge";
    if (p.programme === "absente") return "absent";
    if (p.programme === "extraction") return "extraction";
    return "natural";
  }

  function treeProgress(num) {
    const tdi = treeData?.perImplant?.[num];
    if (!tdi) return { done: 0, total: treeStepsCount };
    return { done: Object.keys(tdi).length, total: treeStepsCount };
  }

  function glyphFor(state) {
    const stroke = "currentColor";
    switch (state) {
      case "implant":
        return (
          <svg viewBox="0 0 24 24" fill="none" stroke={stroke} strokeWidth="1.6">
            <path d="M9 4h6l-1 3h-4z" />
            <path d="M10 7h4l-2 13z" fill="currentColor" opacity="0.25" />
            <path d="M10.5 11h3M11 14h2M11.5 17h1" />
          </svg>);

      case "crown":
        return (
          <svg viewBox="0 0 24 24" fill="currentColor" fillOpacity="0.25" stroke={stroke} strokeWidth="1.4">
            <path d="M4 11l3-5 3 5 2-6 2 6 3-5 3 5v6H4z" />
          </svg>);

      case "bridge":
        return (
          <svg viewBox="0 0 24 24" fill="none" stroke={stroke} strokeWidth="1.4">
            <path d="M3 9 L21 9 L21 17 L3 17 Z" fill="currentColor" fillOpacity="0.18" />
            <path d="M9 9 V17 M15 9 V17" />
          </svg>);

      case "extraction":
        return (
          <svg viewBox="0 0 24 24" fill="none" stroke={stroke} strokeWidth="1.6" strokeLinecap="round">
            <circle cx="12" cy="13" r="6" fill="currentColor" fillOpacity="0.2" />
            <path d="M12 4v3M9.5 5l2.5 2 2.5-2" />
          </svg>);

      case "absent":
        return (
          <svg viewBox="0 0 24 24" fill="none" stroke={stroke} strokeWidth="1.4" strokeDasharray="3 2">
            <path d="M12 4Q8 4 8 12Q8 19 12 19Q16 19 16 12Q16 4 12 4Z" />
          </svg>);

      default:
        return (
          <svg viewBox="0 0 24 24" fill="currentColor" fillOpacity="0.15" stroke={stroke} strokeWidth="1.2">
            <path d="M12 4Q8 4 8 12Q8 19 12 19Q16 19 16 12Q16 4 12 4Z" />
          </svg>);

    }
  }

  function ToothCell({ num, midlineLeft }) {
    const p = planning[num];
    const state = stateOf(p);
    const isImplant = state === "implant";
    // Membre d'un groupe plural (pontic) : affiché comme implant mais sans arbre
    const isGroupMember = !!p?.implant_group_root;
    const isSelectable = isImplant && !isGroupMember;
    const isCurrent = currentImplant === num;
    const { done, total } = treeProgress(num);

    const hasBlock26 = isImplant && !isGroupMember && (() => {
      const tdi = treeData?.perImplant?.[num];
      if (!tdi) return false;
      return Object.values(tdi).some((path) =>
      (path || []).some((p2) => (p2.acts || []).includes(26))
      );
    })();

    let badge = null;
    if (isImplant && !isGroupMember) {
      if (hasBlock26) {
        badge = <div className="tree-badge blocking" title="Conflit (acte 26)">!</div>;
      } else if (done === total) {
        badge = <div className="tree-badge done" title="Arbre complet">
          <svg viewBox="0 0 24 24" fill="none" stroke="white" strokeWidth="4"><path d="M5 12l5 5 9-9" /></svg>
        </div>;
      } else if (done > 0) {
        badge = <div className="tree-badge partial" title={`${done}/${total} étapes`}>{done}</div>;
      } else {
        badge =
        <div className="tree-badge todo" title="Arbre à remplir">
            <svg viewBox="0 0 24 24" fill="none" stroke="white" strokeWidth="3.5" strokeLinecap="round">
              <path d="M12 7v6M12 16v1.5" />
            </svg>
          </div>;

      }
    }

    const pos = num % 10;
    const ttype = pos <= 2 ? "incisor" : pos === 3 ? "canine" : pos <= 5 ? "premolar" : "molar";
    const pct = isImplant && !isGroupMember ? done / total : 0;
    const barColor = done === total ? "#00C691" : done > 0 ? "#6366F1" : "#DDD6F2";

    return (
      <div
        className={`mini-tooth state-${state} ${isSelectable ? "is-selectable" : ""} ${isCurrent ? "is-selected" : ""} ${midlineLeft ? "midline-left" : ""}`}
        onClick={() => {if (isSelectable && onSelectImplant) onSelectImplant(num);}}
        title={`Dent ${num}${isImplant && !isGroupMember ? ` — ${done}/${total} étapes` : isGroupMember ? " (pontic)" : ""}`}
        style={{ position: "relative", opacity: isImplant ? 1 : 0.38 }}>
        
        <MiniToothSVG ttype={ttype} state={state} />
        <span style={{ fontSize: 7.5, lineHeight: 1, fontWeight: 700, color: "currentColor" }}>{num}</span>
        {badge}
        {isImplant && !isGroupMember &&
        <div style={{
          position: "absolute", bottom: 0, left: 0, right: 0, height: 3,
          background: "#DDD6F2", borderRadius: "0 0 4px 4px", overflow: "hidden"
        }}>
            <div style={{
            width: `${pct * 100}%`, height: "100%",
            background: barColor, borderRadius: "0 0 4px 4px",
            transition: "width 0.4s ease"
          }} />
          </div>
        }
      </div>);

  }

  return (
    <div className="mini-chart">
      <div className="mini-chart-title">Schéma</div>
      <div className="mini-chart-rows">
        <div className="mini-chart-row">
          <div className="row-label">Max.</div>
          {upperRow.map((n, i) => <ToothCell key={n} num={n} midlineLeft={i === 7} />)}
        </div>
        <div className="mini-chart-row">
          <div className="row-label">Mand.</div>
          {lowerRow.map((n, i) => <ToothCell key={n} num={n} midlineLeft={i === 7} />)}
        </div>
      </div>
      <div className="mini-chart-legend">
        <div className="item"><span className="sw implant" /> Implant</div>
        <div className="item"><span className="sw crown" /> Couronne</div>
        <div className="item"><span className="sw bridge" /> Bridge</div>
        <div className="item"><span className="sw extract" /> Extraction</div>
        <div className="item"><span className="badge-demo done" /> Arbre complet</div>
        <div className="item"><span className="badge-demo todo" /> À renseigner</div>
        <div className="item"><span className="badge-demo blocking" style={{ display: "inline-flex", alignItems: "center", justifyContent: "center", color: "white", fontSize: "7px", fontWeight: 900 }}>!</span> Alerte</div>
      </div>
    </div>);

}

window.MiniDentalChart = MiniDentalChart;

// ── Mini tooth SVG — même apparence que le schéma 2D (ToothShape paths) ──
function MiniToothSVG({ ttype, state }) {
  const W = ttype === "molar" ? 30 : ttype === "premolar" ? 24 : ttype === "canine" ? 22 : 20;
  const H = ttype === "molar" ? 32 : ttype === "premolar" ? 30 : ttype === "canine" ? 34 : 28;
  const hw = W / 2,hh = H / 2;
  const CLIP_Y = 4;
  const hw2 = hw - 2;

  // Paths couronne uniquement (comme ToothShape crownOnly=true)
  let pathD;
  if (ttype === "incisor") {
    pathD = `M ${-hw} ${-hh} Q 0 ${-hh - 4} ${hw} ${-hh} L ${hw2} ${CLIP_Y} Q 0 ${CLIP_Y + 4} ${-hw2} ${CLIP_Y} Z`;
  } else if (ttype === "canine") {
    pathD = `M ${-hw} ${-hh + 6} Q 0 ${-hh - 6} ${hw} ${-hh + 6} L ${hw2} ${CLIP_Y} Q 0 ${CLIP_Y + 4} ${-hw2} ${CLIP_Y} Z`;
  } else if (ttype === "premolar") {
    pathD = `M ${-hw} ${-hh + 4} Q ${-hw / 2} ${-hh - 4} 0 ${-hh + 2} Q ${hw / 2} ${-hh - 4} ${hw} ${-hh + 4} L ${hw2} ${CLIP_Y} Q 0 ${CLIP_Y + 4} ${-hw2} ${CLIP_Y} Z`;
  } else {
    pathD = `M ${-hw} ${-hh + 4} Q ${-hw / 2} ${-hh - 4} 0 ${-hh + 2} Q ${hw / 2} ${-hh - 4} ${hw} ${-hh + 4} L ${hw} ${CLIP_Y} Q ${hw / 2} ${CLIP_Y + 4} 0 ${CLIP_Y + 2} Q ${-hw / 2} ${CLIP_Y + 4} ${-hw} ${CLIP_Y} Z`;
  }

  const colors = {
    natural: { fill: "#F5F2FC", stroke: "#A39BC2", sw: "1.3" },
    absent: { fill: "none", stroke: "#8C84B5", sw: "1.4", dash: "3 2" },
    extraction: { fill: "#FFD9CB", stroke: "#E85029", sw: "1.8" },
    implant: { fill: "#C8F5E2", stroke: "#00C691", sw: "1.8" },
    crown: { fill: "#E0D8F6", stroke: "#3D2EB8", sw: "1.5" },
    bridge: { fill: "#E0D8F6", stroke: "#3D2EB8", sw: "1.5", dash: "3 2" }
  };
  const c = colors[state] || colors.natural;

  const pad = 4;
  const crownH = hh + CLIP_Y + pad * 2; // height from -hh-pad to CLIP_Y+pad
  const vbW = W + pad * 2;

  return (
    <svg
      viewBox={`${-hw - pad} ${-hh - pad} ${vbW} ${crownH}`}
      width="100%"
      height="26"
      preserveAspectRatio="xMidYMid meet"
      style={{ display: "block" }}>
      
      <path d={pathD}
      fill={c.fill}
      stroke={c.stroke}
      strokeWidth={c.sw}
      strokeDasharray={c.dash} />
      
    </svg>);

}