/* ==========================================================================
   CaseStudyChart.jsx  —  @role
   Home-page animated case-study line chart (scroll-triggered stroke-dashoffset draw + endpoint ping + staggered pills). Pure CSS keyframes gated by IntersectionObserver class.
   ========================================================================== */
// ============================================================
//  CaseStudyChart — the "12-month journey" predictability chart
//  (committed vs. delivered) supplied for the Case Study in Focus box.
//
//  The SVG + CSS are lifted from the uploaded standalone file; the
//  timeline is driven by a `play` prop (so the surrounding card can
//  start it — and slide its "What we did" bullets in — off the same
//  in-view signal). A Replay button re-runs the sequence.
//
//  CSS is scoped under .psl-case-chart and keyframes are renamed to
//  avoid clashing with other components on the page.
// ============================================================

const CASE_CHART_CSS = `
  .psl-case-chart { font-family: "Mona Sans", -apple-system, sans-serif; }
  .psl-case-chart .chart-card {
    background: #fff;
    border: 1px solid rgba(13,0,66,0.06);
    border-radius: 12px;
    padding: 18px 16px 8px;
    position: relative;
    overflow: hidden;
    width: 100%;
  }
  .psl-case-chart .chart-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 8px; padding: 0 8px; }
  .psl-case-chart .chart-head .l, .psl-case-chart .chart-head .r { font-size: 11px; font-weight: 700; letter-spacing: 0.08em; color: #4644B9; text-transform: uppercase; }
  .psl-case-chart .chart-head .r { text-align: right; }
  .psl-case-chart .chart-head .r .sub { font-style: italic; font-weight: 500; font-size: 10px; letter-spacing: 0.04em; color: #6B7280; text-transform: none; margin-top: 2px; }
  .psl-case-chart .chart-svg { width: 100%; height: auto; display: block; }
  .psl-case-chart .replay-row { display: flex; justify-content: flex-end; padding: 4px 6px 0; }
  .psl-case-chart .replay-btn { background: transparent; border: 1px solid #E5E7EB; padding: 5px 12px; border-radius: 6px; font-family: inherit; color: #4644B9; cursor: pointer; font-size: 11px; font-weight: 600; transition: all 0.2s; display: inline-flex; align-items: center; gap: 6px; }
  .psl-case-chart .replay-btn:hover { background: #4644B9; color: #fff; border-color: #4644B9; }

  .psl-case-chart .line-before {
    fill: none; stroke: #D4537E; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round;
    stroke-dasharray: 500; stroke-dashoffset: 500;
    transition: stroke-dashoffset 2.2s linear;
  }
  .psl-case-chart .line-before.draw { stroke-dashoffset: 0; }
  .psl-case-chart .line-after {
    fill: none; stroke: url(#grad-after); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round;
    stroke-dasharray: 500; stroke-dashoffset: 500;
    transition: stroke-dashoffset 2.6s linear;
  }
  .psl-case-chart .line-after.draw { stroke-dashoffset: 0; }
  .psl-case-chart .committed-line { stroke: #0D0042; stroke-width: 1; stroke-dasharray: 4 4; opacity: 0; transition: opacity 0.6s; }
  .psl-case-chart .committed-line.show { opacity: 0.4; }
  .psl-case-chart .committed-label { opacity: 0; transition: opacity 0.6s; }
  .psl-case-chart .committed-label.show { opacity: 1; }
  .psl-case-chart .corridor-before, .psl-case-chart .corridor-after { opacity: 0; transition: opacity 0.9s ease-out; }
  .psl-case-chart .corridor-before.show, .psl-case-chart .corridor-after.show { opacity: 1; }
  .psl-case-chart .miss-mark { opacity: 0; transform: scale(0.4); transform-origin: center; transform-box: fill-box; transition: opacity 0.35s, transform 0.35s; }
  .psl-case-chart .miss-mark.show { opacity: 1; transform: scale(1); }

  .psl-case-chart .psl-marker .card-anim { opacity: 0; transform: translateY(-55px); transition: opacity 0.7s ease-out, transform 0.85s cubic-bezier(0.34, 1.3, 0.64, 1); }
  .psl-case-chart .psl-marker.show .card-anim { opacity: 1; transform: translateY(0); }
  .psl-case-chart .psl-marker .dot-wrap { opacity: 0; transition: opacity 0.5s ease-out; }
  .psl-case-chart .psl-marker.show .dot-wrap { opacity: 1; transition-delay: 0.7s; }
  .psl-case-chart .psl-marker .pulse { transform-origin: center; transform-box: fill-box; animation: pslCasePulse 2.4s ease-out infinite; }
  @keyframes pslCasePulse {
    0% { transform: scale(1); opacity: 0.55; }
    100% { transform: scale(4); opacity: 0; }
  }
  .psl-case-chart .end-dot { opacity: 0; transition: opacity 0.5s; }
  .psl-case-chart .end-dot.show { opacity: 1; }
  .psl-case-chart .end-dot .breathe { transform-origin: center; transform-box: fill-box; animation: pslCaseBreathe 2s ease-in-out infinite; }
  @keyframes pslCaseBreathe { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.18); } }

  @media (prefers-reduced-motion: reduce) {
    .psl-case-chart .line-before, .psl-case-chart .line-after { stroke-dashoffset: 0 !important; transition: none !important; }
    .psl-case-chart .committed-line { opacity: 0.4 !important; }
    .psl-case-chart .committed-label, .psl-case-chart .corridor-before, .psl-case-chart .corridor-after,
    .psl-case-chart .miss-mark, .psl-case-chart .end-dot { opacity: 1 !important; transition: none !important; transform: none !important; }
    .psl-case-chart .psl-marker .card-anim { opacity: 1 !important; transform: none !important; }
    .psl-case-chart .psl-marker .dot-wrap { opacity: 1 !important; }
  }
`;

const CASE_CHART_SVG = `
<div class="chart-card">
  <div class="chart-head">
    <div class="l">The 12-month journey</div>
    <div class="r">Predictability<div class="sub">(committed vs. delivered)</div></div>
  </div>

  <svg class="chart-svg" viewBox="0 0 720 380" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid meet">
    <defs>
      <linearGradient id="bg-before" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#FBEAF0" stop-opacity="0.55"/>
        <stop offset="100%" stop-color="#FBEAF0" stop-opacity="0"/>
      </linearGradient>
      <linearGradient id="bg-after" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#E1F5EE" stop-opacity="0"/>
        <stop offset="100%" stop-color="#E1F5EE" stop-opacity="0.7"/>
      </linearGradient>
      <linearGradient id="grad-after" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#D4537E"/>
        <stop offset="22%" stop-color="#EF9F27"/>
        <stop offset="58%" stop-color="#1D9E75"/>
        <stop offset="100%" stop-color="#19E6C4"/>
      </linearGradient>
      <linearGradient id="corridor-before-fill" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#F4C0D1" stop-opacity="0.45"/>
        <stop offset="100%" stop-color="#F4C0D1" stop-opacity="0.35"/>
      </linearGradient>
      <linearGradient id="corridor-after-fill" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#9FE1CB" stop-opacity="0.35"/>
        <stop offset="100%" stop-color="#9FE1CB" stop-opacity="0.6"/>
      </linearGradient>
    </defs>

    <rect x="60" y="40" width="300" height="280" fill="url(#bg-before)" />
    <rect x="360" y="40" width="300" height="280" fill="url(#bg-after)" />

    <line x1="60" y1="320" x2="660" y2="320" stroke="#D1D5DB" stroke-width="1"/>
    <line x1="60" y1="40" x2="60" y2="320" stroke="#D1D5DB" stroke-width="1"/>

    <line class="committed-line" x1="60" y1="170" x2="660" y2="170"/>
    <text class="committed-label" x="65" y="163" font-size="10" font-weight="700" fill="#0D0042" letter-spacing="1.2">COMMITTED · TARGET</text>

    <path class="corridor-before"
      d="M 60,160 Q 90,155 130,170 Q 180,175 220,185 Q 270,200 310,210 L 360,220 L 360,250 Q 310,245 270,242 Q 220,238 180,230 Q 130,225 90,215 L 60,200 Z"
      fill="url(#corridor-before-fill)"/>
    <path class="corridor-after"
      d="M 360,222 Q 400,225 430,215 Q 470,195 510,172 Q 555,155 600,150 Q 630,135 660,122 L 660,142 Q 630,150 600,165 Q 555,170 510,182 Q 470,210 430,225 Q 400,238 360,238 Z"
      fill="url(#corridor-after-fill)"/>

    <path class="line-before"
      d="M 60,178 Q 80,190 110,205 Q 130,218 150,200 Q 175,185 200,200 Q 225,215 250,228 Q 275,218 295,225 Q 320,232 340,228 L 360,232"/>

    <g class="miss-mark" data-delay="900">
      <circle cx="131" cy="218" r="9" fill="none" stroke="#993556" stroke-width="1" opacity="0.4"/>
      <path d="M 127,214 L 135,222 M 135,214 L 127,222" stroke="#993556" stroke-width="2" stroke-linecap="round"/>
    </g>
    <g class="miss-mark" data-delay="1700">
      <circle cx="251" cy="228" r="9" fill="none" stroke="#993556" stroke-width="1" opacity="0.4"/>
      <path d="M 247,224 L 255,232 M 255,224 L 247,232" stroke="#993556" stroke-width="2" stroke-linecap="round"/>
    </g>

    <g class="psl-marker">
      <line class="psl-vline" x1="360" y1="40" x2="360" y2="40" stroke="#4644B9" stroke-width="1" stroke-dasharray="3 3" opacity="0.55"/>
      <g class="dot-wrap">
        <circle class="pulse" cx="360" cy="232" r="8" fill="#4644B9" opacity="0.5"/>
        <circle cx="360" cy="232" r="6" fill="#4644B9"/>
        <circle cx="360" cy="232" r="2.5" fill="white"/>
      </g>
      <g class="card-anim">
        <g transform="translate(360, 80)">
          <text x="0" y="-40" font-size="10" font-weight="700" fill="#6B7280" text-anchor="middle" letter-spacing="2">WHERE WE CAME IN</text>
          <rect x="-95" y="-25" width="190" height="50" rx="6" fill="white" stroke="#4644B9" stroke-width="2.2"/>
          <line x1="-10.5" y1="25" x2="10.5" y2="25" stroke="white" stroke-width="3"/>
          <polygon points="-10,25 10,25 0,39" fill="#4644B9"/>
          <polyline points="-10,25 0,39 10,25" fill="none" stroke="#4644B9" stroke-width="2.2" stroke-linejoin="round" stroke-linecap="round"/>
          <g transform="translate(-95, -25.5) scale(0.19)" fill="#0D0042" stroke="#0D0042" stroke-width="0.6" stroke-linejoin="round">
            <path d="M110.9,65.7c-2.9-1.7-6.4-2.5-10.5-2.5h-38.3v60.4h14.1v-20.6h23.8c4.2,0,7.7-.8,10.7-2.5,3-1.7,5.2-4,6.9-7,1.6-3,2.4-6.5,2.4-10.4s-.8-7.5-2.3-10.4c-1.6-3-3.8-5.3-6.7-6.9ZM103.6,89.4c-1.6,1.5-3.7,2.3-6.5,2.3h-20.8v-17h20.8c1.9,0,3.6.3,4.9,1s2.3,1.6,3,2.9,1,2.8,1,4.6c0,2.7-.8,4.8-2.3,6.3Z"/>
            <polygon points="152.7 98.5 188.8 98.5 188.8 87.2 152.7 87.2 152.7 74.6 193.5 74.6 193.5 63.2 138.5 63.2 138.5 123.6 194.1 123.6 194.1 112.2 152.7 112.2 152.7 98.5"/>
            <path d="M273,92.5c1.8-3.1,2.7-6.5,2.7-10.3s-.7-6.8-2.1-9.7c-1.4-2.9-3.5-5.1-6.4-6.8s-6.5-2.5-10.9-2.5h-41v60.4h14.1v-21.4h21.5l10.7,21.4h15.7l-12.5-23.9c3.6-1.7,6.3-4.1,8-7.2ZM259.1,88.7c-1.4,1.6-3.4,2.3-6,2.3h-23.6v-16.4h23.6c1.8,0,3.2.3,4.4.9,1.2.6,2.1,1.5,2.8,2.7s1,2.6,1,4.3c0,2.6-.7,4.6-2.2,6.2Z"/>
            <polygon points="311 74.6 348.3 74.6 348.3 63.2 296.9 63.2 296.9 123.6 311 123.6 311 100.2 344.4 100.2 344.4 88.9 311 88.9 311 74.6"/>
            <path d="M431,110.6c2.8-4.6,4.2-10.3,4.2-17.1s-1.4-12.6-4.2-17.2c-2.8-4.6-6.8-8.1-12.1-10.5-5.2-2.4-11.5-3.6-18.8-3.6s-13.4,1.2-18.7,3.6c-5.2,2.4-9.3,5.9-12.1,10.5-2.8,4.6-4.2,10.4-4.2,17.2s1.4,12.5,4.2,17.1c2.8,4.6,6.8,8.1,12.1,10.5,5.2,2.4,11.5,3.6,18.7,3.6s13.5-1.2,18.8-3.6c5.2-2.4,9.3-5.9,12.1-10.5ZM408.7,112.1c-2.5.8-5.4,1.2-8.5,1.2s-5.9-.4-8.4-1.2c-2.5-.8-4.7-2-6.5-3.6-1.8-1.6-3.1-3.5-4.1-5.9s-1.4-5-1.4-8v-2.4c0-3,.5-5.7,1.4-8,.9-2.3,2.3-4.3,4.1-5.8s3.9-2.8,6.5-3.6c2.5-.8,5.3-1.2,8.4-1.2s6,.4,8.5,1.2c2.5.8,4.7,2,6.5,3.6s3.2,3.5,4.1,5.8c1,2.3,1.4,5,1.4,8v2.4c0,3-.5,5.7-1.4,8s-2.3,4.3-4.1,5.9c-1.8,1.6-3.9,2.8-6.5,3.6Z"/>
            <path d="M502.4,123.6h15.7l-12.5-23.9c3.6-1.7,6.3-4.1,8-7.2,1.8-3.1,2.7-6.5,2.7-10.3s-.7-6.8-2.1-9.7c-1.4-2.9-3.5-5.1-6.4-6.8s-6.5-2.5-10.9-2.5h-41v60.4h14.2v-21.4h21.5l10.7,21.4ZM470.2,74.6h23.6c1.8,0,3.2.3,4.4.9,1.2.6,2.1,1.5,2.8,2.7.6,1.2,1,2.6,1,4.3,0,2.6-.7,4.6-2.2,6.2-1.4,1.6-3.4,2.3-6,2.3h-23.6v-16.4Z"/>
            <path d="M592.4,89.7c.4-1.2.9-2.7,1.4-4.3.5-1.6,1-3.2,1.4-4.7s.8-2.7,1-3.5h.7c0,.8,0,2,0,3.6,0,1.6-.1,3.5-.2,5.5,0,2,0,3.9,0,5.8v31.6h14.1v-60.4h-21.4l-10.5,30.1c-.5,1.3-1,2.8-1.5,4.4s-1,3.2-1.4,4.8c-.5,1.6-.9,2.9-1.1,4h-.7c-.4-1.3-.8-2.8-1.3-4.4-.5-1.6-1-3.3-1.5-4.9-.5-1.6-.9-2.9-1.3-4l-10.5-30h-21.8v60.4h13.4v-31.6c0-1.6,0-3.4,0-5.2,0-1.8,0-3.6-.1-5.3,0-1.7-.1-3.1-.2-4.3h.7c.4,1.1.7,2.3,1.1,3.8.4,1.5.8,3,1.3,4.5.5,1.5,1,2.9,1.4,4.2l12.3,33.9h12.8l12.2-33.9Z"/>
            <path d="M697.8,123.6l-26.6-60.4h-16.6l-26.6,60.4h14.6l4.8-11.4h30.2l4.8,11.4h15.4ZM651.9,101.1l5.9-14.3c.4-.8.8-1.9,1.3-3.3.5-1.4,1.1-2.8,1.6-4.3.6-1.5,1-2.8,1.4-4h.6c.2.5.5,1.3.8,2.4.4,1.1.8,2.2,1.2,3.3.4,1.2.8,2.3,1.2,3.3.4,1,.7,1.9,1,2.5l6,14.3h-21.1Z"/>
            <path d="M776.3,123.6v-60.4h-13.4v31.2c0,1,0,2.2,0,3.5s0,2.5.1,3.6h-.6c-.5-.6-1.1-1.3-1.8-2.2-.8-.9-1.5-1.8-2.2-2.6-.7-.9-1.4-1.6-1.9-2l-28.3-31.5h-13v60.4h13.4v-31.2c0-1.5,0-2.9,0-4.3,0-1.3,0-2.4,0-3.2h.5c.6.8,1.5,1.8,2.7,3.2,1.1,1.3,2.2,2.6,3.3,3.7l28.5,31.8h12.8Z"/>
            <path d="M845.9,107.5c-1.4,1.9-3.5,3.4-6.1,4.4-2.6,1-5.7,1.4-9.3,1.4s-7.6-.7-10.4-2.2c-2.8-1.5-4.9-3.6-6.4-6.4s-2.2-6.2-2.2-10.2v-2.5c0-3.9.7-7.3,2.2-10.1s3.6-4.9,6.4-6.4c2.8-1.5,6.3-2.2,10.4-2.2s6.4.5,8.9,1.5,4.5,2.5,6,4.4c1.4,1.9,2.2,4.3,2.2,7.1h14c0-5.3-1.3-9.7-4-13.3-2.6-3.6-6.3-6.3-10.9-8.2s-10-2.8-16-2.8c-10.8,0-19.1,2.6-24.8,7.9-5.8,5.3-8.7,13-8.7,23.3s1.3,12.9,3.8,17.5c2.5,4.6,6.3,8.1,11.3,10.4,5,2.3,11.1,3.4,18.5,3.4s11.5-.9,16.2-2.8c4.6-1.9,8.2-4.6,10.9-8.2,2.6-3.6,3.9-8,3.9-13.2h-13.6c0,2.8-.7,5.1-2.2,7Z"/>
            <polygon points="896.5 98.5 932.6 98.5 932.6 87.2 896.5 87.2 896.5 74.6 937.3 74.6 937.3 63.2 882.3 63.2 882.3 123.6 937.9 123.6 937.9 112.2 896.5 112.2 896.5 98.5"/>
            <path d="M178.4,183.1c-1.5-1-3.3-1.7-5.2-2.3-1.9-.6-3.9-1.1-5.9-1.6s-4-.8-5.9-1.2c-1.9-.4-3.6-.8-5.2-1.3-1.5-.5-2.8-1.1-3.7-1.8-.9-.7-1.4-1.7-1.4-2.8s.5-2,1.4-2.8c.9-.8,2.3-1.4,4.1-1.8,1.8-.4,4.1-.6,6.8-.6s4.1.3,5.8.8,3,1.2,3.9,2.1c.9.9,1.4,1.9,1.4,3.1v1h7.5v-1c0-1.9-.4-3.7-1.2-5.2s-2-2.8-3.6-3.9c-1.6-1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-1.1-3.6-1.9-5.9-2.4-2.3-.6-5-.8-8-.8s-6,.3-8.5.8c-2.4.5-4.5,1.3-6.2,2.3-1.7,1-2.9,2.3-3.8,3.8s-1.3,3.2-1.3,5,.4,3.9,1.3,5.3c.9,1.4,2.1,2.6,3.6,3.6,1.5.9,3.3,1.7,5.2,2.3,1.9.6,3.9,1.1,5.9,1.5,2.1.4,4,.8,5.9,1.2,1.9.4,3.6.9,5.2,1.4s2.8,1.2,3.6,2,1.3,1.9,1.3,3.2-.2,1.7-.7,2.3-1.1,1.3-1.9,1.7-1.7.9-2.8,1.2c-1.1.3-2.2.5-3.5.6-1.2.1-2.5.2-3.8.2-2.6,0-4.9-.3-6.9-.8s-3.5-1.3-4.6-2.3c-1.1-1-1.6-2.3-1.6-3.7s0-.6,0-.8c0-.2,0-.4,0-.6h-7.6c0,.1,0,.3,0,.5v.5c0,2.6.5,4.8,1.6,6.5,1.1,1.7,2.6,3.1,4.5,4.2,1.9,1,4.1,1.8,6.6,2.2,2.5.4,5.1.7,7.9.7,4,0,7.6-.4,10.6-1.3,3.1-.9,5.5-2.3,7.2-4.3s2.6-4.6,2.6-7.8-.4-3.8-1.3-5.3c-.9-1.4-2.1-2.6-3.6-3.6Z"/>
            <polygon points="346.2 167.8 363.2 167.8 363.2 204.6 370.8 204.6 370.8 167.8 387.9 167.8 387.9 161.1 346.2 161.1 346.2 167.8"/>
            <polygon points="425.1 185.6 452.8 185.6 452.8 179.1 425.1 179.1 425.1 167.6 456.1 167.6 456.1 161.1 417.5 161.1 417.5 204.6 456.5 204.6 456.5 198.2 425.1 198.2 425.1 185.6"/>
            <path d="M516.8,186.8c-.3.8-.6,1.7-1,2.8-.4,1.1-.7,2.1-1,3.1-.3,1-.6,1.9-.9,2.6h-.4c-.3-.8-.6-1.7-.9-2.8-.4-1.1-.7-2.1-1.1-3.1s-.7-1.9-1-2.6l-10.1-25.6h-11.8v43.5h7.4v-25.6c0-1,0-2.1,0-3.4s0-2.4,0-3.6c0-1.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.5.4,5.1.7,7.9.7,4,0,7.6-.4,10.6-1.3,3.1-.9,5.5-2.3,7.2-4.3s2.6-4.6,2.6-7.8-.4-3.8-1.3-5.3c-.9-1.4-2.1-2.6-3.6-3.6Z"/>
            <polygon points="689.6 161.1 682 161.1 682 204.6 714.2 204.6 714.2 197.9 689.6 197.9 689.6 161.1"/>
            <path d="M758.6,161.1l-19.6,43.5h7.9l4.3-9.7h23.6l4.3,9.7h8.4l-19.6-43.5h-9.2ZM754,188.5l5.3-12.3c.3-.6.6-1.5,1-2.5.4-1,.9-2.1,1.3-3.2.4-1.1.8-2.1,1.2-2.9h.4c.2.4.4,1,.7,1.8.3.8.6,1.6,1,2.5s.7,1.7,1,2.4c.3.8.6,1.4.8,1.8l5.3,12.3h-18Z"/>
            <path d="M853.7,183.9c-1.3-.9-2.7-1.6-4.3-1.9v-.3c1.4-.5,2.6-1.2,3.7-2.1,1.1-.9,1.9-2.1,2.6-3.5s1-2.9,1-4.5-.5-4-1.6-5.6-2.5-2.8-4.3-3.7-3.9-1.3-6.3-1.3h-27.8v43.5h27.8c2.5,0,4.8-.4,6.8-1.2s3.6-2.1,4.8-3.9c1.2-1.8,1.8-4,1.8-6.8s-.4-3.7-1.1-5.2c-.7-1.5-1.7-2.7-3-3.6ZM824.2,167.6h18.5c1.8,0,3.3.5,4.4,1.4,1.1.9,1.7,2.4,1.7,4.3s-.3,2.2-.8,3.1c-.5.9-1.3,1.6-2.3,2.1-1,.5-2.1.7-3.5.7h-18v-11.6ZM848.1,196.7c-1.2,1-2.8,1.5-4.8,1.5h-19.2v-12.6h18.8c1.5,0,2.8.3,3.8.8s1.8,1.2,2.4,2.2c.5,1,.8,2.1.8,3.5,0,2.2-.6,3.7-1.8,4.7Z"/>
          </g>
        </g>
      </g>
    </g>

    <path class="line-after"
      d="M 360,232 Q 370,242 385,238 Q 400,232 420,222 Q 445,210 470,195 Q 495,180 520,168 Q 545,158 575,155 Q 605,152 625,142 L 660,120"/>

    <g class="end-dot">
      <circle class="breathe" cx="660" cy="120" r="10" fill="#19E6C4" opacity="0.35"/>
      <circle cx="660" cy="120" r="5" fill="#19E6C4"/>
    </g>

    <text x="60" y="338" font-size="10" fill="#6B7280" font-weight="500" text-anchor="middle">−6</text>
    <text x="160" y="338" font-size="10" fill="#6B7280" font-weight="500" text-anchor="middle">−4</text>
    <text x="260" y="338" font-size="10" fill="#6B7280" font-weight="500" text-anchor="middle">−2</text>
    <text x="360" y="338" font-size="10" fill="#6B7280" font-weight="500" text-anchor="middle">0</text>
    <text x="460" y="338" font-size="10" fill="#6B7280" font-weight="500" text-anchor="middle">+2</text>
    <text x="560" y="338" font-size="10" fill="#6B7280" font-weight="500" text-anchor="middle">+4</text>
    <text x="660" y="338" font-size="10" fill="#6B7280" font-weight="500" text-anchor="middle">+6</text>

    <text x="210" y="362" font-size="12" font-weight="700" letter-spacing="1.6" fill="#993556" text-anchor="middle">6 MONTHS BEFORE</text>
    <text x="510" y="362" font-size="12" font-weight="700" letter-spacing="1.6" fill="#0F6E56" text-anchor="middle">6 MONTHS AFTER</text>

    <text x="-180" y="22" font-size="11" font-weight="700" letter-spacing="2" fill="#4644B9" transform="rotate(-90)" text-anchor="middle">PREDICTABILITY ↑</text>
  </svg>
</div>
`;

function ensureCaseChartStyles() {
  if (typeof document === 'undefined') return;
  if (document.getElementById('__psl-case-chart-style')) return;
  const s = document.createElement('style');
  s.id = '__psl-case-chart-style';
  s.textContent = CASE_CHART_CSS;
  document.head.appendChild(s);
}

const CaseStudyChart = ({ play = false, onStart }) => {
  const rootRef = React.useRef(null);
  const onStartRef = React.useRef(onStart);
  onStartRef.current = onStart;
  const runRef = React.useRef(null);
  const hasRun = React.useRef(false);

  ensureCaseChartStyles();

  React.useEffect(() => {
    const root = rootRef.current;
    if (!root) return;
    let timers = [];

    function run() {
      timers.forEach(clearTimeout);
      timers = [];
      root.querySelectorAll('.show').forEach((e) => e.classList.remove('show'));
      root.querySelectorAll('.draw').forEach((e) => e.classList.remove('draw'));
      const vl = root.querySelector('.psl-vline');
      if (vl) vl.setAttribute('y2', '40');
      if (onStartRef.current) onStartRef.current();

      const T_BEFORE_START = 700;
      const T_BEFORE_DURATION = 2200;
      const T_HANDOFF = T_BEFORE_START + T_BEFORE_DURATION;
      const T_PSL_START = T_HANDOFF - 1500;

      timers.push(setTimeout(() => { root.querySelector('.committed-line').classList.add('show'); root.querySelector('.committed-label').classList.add('show'); }, 300));
      timers.push(setTimeout(() => root.querySelector('.corridor-before').classList.add('show'), 500));
      timers.push(setTimeout(() => root.querySelector('.line-before').classList.add('draw'), T_BEFORE_START));

      root.querySelectorAll('.miss-mark').forEach((m) => {
        const d = parseInt(m.dataset.delay, 10) || 0;
        timers.push(setTimeout(() => m.classList.add('show'), T_BEFORE_START + d));
      });

      timers.push(setTimeout(() => {
        root.querySelector('.psl-marker').classList.add('show');
        const vline = root.querySelector('.psl-vline');
        if (vline) {
          const start = performance.now();
          const dur = 850;
          const tick = (t) => {
            const p = Math.min((t - start) / dur, 1);
            const eased = 1 - Math.pow(1 - p, 3);
            vline.setAttribute('y2', String(40 + (320 - 40) * eased));
            if (p < 1) requestAnimationFrame(tick);
          };
          requestAnimationFrame(tick);
        }
      }, T_PSL_START));

      timers.push(setTimeout(() => root.querySelector('.corridor-after').classList.add('show'), T_HANDOFF + 100));
      timers.push(setTimeout(() => root.querySelector('.line-after').classList.add('draw'), T_HANDOFF));
      timers.push(setTimeout(() => root.querySelector('.end-dot').classList.add('show'), T_HANDOFF + 2600));
    }
    runRef.current = run;

    const btn = root.querySelector('.replay-btn');
    const onReplay = (ev) => { ev.preventDefault(); ev.stopPropagation(); run(); };
    if (btn) btn.addEventListener('click', onReplay);

    return () => {
      timers.forEach(clearTimeout);
      if (btn) btn.removeEventListener('click', onReplay);
    };
  }, []);

  React.useEffect(() => {
    if (play && !hasRun.current && runRef.current) {
      hasRun.current = true;
      runRef.current();
    }
  }, [play]);

  return (
    <div className="psl-case-chart" ref={rootRef} dangerouslySetInnerHTML={{ __html: CASE_CHART_SVG }} />
  );
};

Object.assign(window, { CaseStudyChart });
