/* ========================================================
   Case Studies — 3 full-width rows with visuals
   ======================================================== */

/* Custom visual: Voice AI call flow dashboard */
function CaseVisualVoice() {
  return (
    <div className="cv cv-voice">
      <div className="cv-chrome">
        <span className="cv-dot"/><span className="cv-dot"/><span className="cv-dot"/>
        <span className="cv-url mono">app.harbor-hotels.com / agent · live</span>
      </div>
      <div className="cv-body">
        <div className="cv-row">
          <div className="cv-metric">
            <div className="mono cv-metric-label">ACTIVE CALLS</div>
            <div className="cv-metric-v"><span className="cv-ml-num">47</span><span className="cv-spark">▁▂▃▅▇▆▄▅▇</span></div>
          </div>
          <div className="cv-metric">
            <div className="mono cv-metric-label">AHT</div>
            <div className="cv-metric-v"><span className="cv-ml-num">1:42</span><span className="cv-delta cv-delta--good">−61%</span></div>
          </div>
        </div>
        <div className="cv-call">
          <div className="cv-call-head">
            <span className="cv-live"/>
            <span className="mono">#CALL-4471 · RESERVATION · EN-US</span>
            <span className="mono cv-call-dur">00:42</span>
          </div>
          <div className="cv-turn cv-turn--user"><span className="mono">GUEST</span><div>Can I get a room for Friday?</div></div>
          <div className="cv-turn cv-turn--agent"><span className="mono">AGENT</span><div>Two queens or a king for Friday the 17th?</div></div>
          <div className="cv-waveform">
            {Array.from({length: 42}).map((_,i) => {
              const h = 4 + Math.abs(Math.sin(i * 0.6)) * 18;
              return <span key={i} style={{ height: `${h}px` }}/>;
            })}
          </div>
        </div>
      </div>
      <style>{`
        .cv {
          position: relative;
          border: 1px solid var(--border-2);
          border-radius: 14px;
          background: linear-gradient(180deg, var(--surface) 0%, var(--bg-soft) 100%);
          overflow: hidden;
          box-shadow: 0 40px 100px -50px rgba(0,0,0,0.9);
        }
        .cv-chrome { display: flex; align-items: center; gap: 7px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
        .cv-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--border-3); }
        .cv-url { margin-left: 8px; font-size: 11px; color: var(--text-3); }
        .cv-body { padding: 20px; display: flex; flex-direction: column; gap: 16px; }
        .cv-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
        .cv-metric { padding: 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg-soft); }
        .cv-metric-label { font-size: 10px; margin-bottom: 6px; }
        .cv-metric-v { display: flex; align-items: baseline; justify-content: space-between; }
        .cv-ml-num { font-size: 28px; font-weight: 600; letter-spacing: -0.02em; color: var(--text); }
        .cv-spark { font-family: var(--font-mono); color: var(--accent); font-size: 14px; letter-spacing: 1px; }
        .cv-delta { font-family: var(--font-mono); font-size: 12px; }
        .cv-delta--good { color: var(--accent); }
        .cv-call {
          padding: 14px;
          border: 1px solid var(--border);
          border-radius: 10px;
          background: var(--bg-soft);
        }
        .cv-call-head {
          display: flex; align-items: center; gap: 8px;
          font-size: 11px; color: var(--text-3);
          margin-bottom: 12px;
        }
        .cv-live { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 8px var(--accent-glow); animation: cvPulse 1.2s infinite; }
        @keyframes cvPulse { 50% { opacity: 0.4; } }
        .cv-call-dur { margin-left: auto; color: var(--text-2); }
        .cv-turn { display: flex; gap: 10px; font-size: 13px; margin-bottom: 8px; }
        .cv-turn .mono { font-size: 9.5px; letter-spacing: 0.1em; width: 48px; color: var(--text-4); padding-top: 4px; }
        .cv-turn div { color: var(--text-2); }
        .cv-turn--agent .mono { color: var(--accent); }
        .cv-waveform { display: flex; align-items: center; gap: 2px; margin-top: 10px; height: 24px; }
        .cv-waveform span { width: 2px; background: var(--accent); border-radius: 1px; opacity: 0.6; }
      `}</style>
    </div>
  );
}

/* Custom visual: RAG retrieval panel */
function CaseVisualRAG() {
  return (
    <div className="cv cv-rag">
      <div className="cv-chrome">
        <span className="cv-dot"/><span className="cv-dot"/><span className="cv-dot"/>
        <span className="cv-url mono">research.axia-capital.com / ask</span>
      </div>
      <div className="cv-body rag-body">
        <div className="rag-query">
          <span className="mono rag-prompt">→</span>
          <span>Summarize Q3 risk exposure across energy sector holdings.</span>
        </div>
        <div className="rag-sources">
          <div className="mono rag-sources-label">RETRIEVED · 8 CHUNKS · 420ms</div>
          {[
            ["axia-q3-risk.pdf", "p.42"],
            ["energy-memo-2026.md", "§3"],
            ["hedging-strategy.pdf", "p.11"],
            ["exposure-tracker.xlsx", "sheet 2"],
          ].map(([f, p]) => (
            <div key={f} className="rag-src">
              <span className="rag-src-check">✓</span>
              <span className="rag-src-name">{f}</span>
              <span className="mono rag-src-loc">{p}</span>
              <span className="rag-src-bar"><span style={{width: `${50 + Math.random()*50}%`}}/></span>
            </div>
          ))}
        </div>
        <div className="rag-answer">
          <div className="mono rag-answer-label">SYNTHESIZED</div>
          <div>Energy exposure increased <span className="rag-hl">+18.4%</span> QoQ, concentrated in midstream assets. Hedged position covers <span className="rag-hl">72%</span> of downside.</div>
          <div className="rag-cites">
            <span>¹ axia-q3-risk.pdf · p.42</span>
            <span>² hedging-strategy.pdf · p.11</span>
          </div>
        </div>
      </div>
      <style>{`
        .rag-body { padding: 20px; gap: 14px; }
        .rag-query {
          display: flex; gap: 10px;
          padding: 12px 14px;
          border: 1px solid var(--border);
          border-radius: 10px;
          background: var(--bg-soft);
          font-size: 14px;
          color: var(--text);
        }
        .rag-prompt { color: var(--accent); font-size: 14px; }
        .rag-sources { padding: 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg-soft); }
        .rag-sources-label { font-size: 10px; color: var(--text-4); margin-bottom: 10px; }
        .rag-src {
          display: grid; grid-template-columns: auto 1fr auto 80px;
          align-items: center; gap: 10px;
          padding: 6px 0;
          font-size: 12.5px;
          color: var(--text-2);
        }
        .rag-src-check { color: var(--accent); }
        .rag-src-name { font-family: var(--font-mono); font-size: 11.5px; color: var(--text-2); }
        .rag-src-loc { font-size: 10px; color: var(--text-4); }
        .rag-src-bar { height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; }
        .rag-src-bar span { display: block; height: 100%; background: var(--accent); }
        .rag-answer { padding: 14px; border: 1px solid var(--accent-line); border-radius: 10px; background: var(--accent-soft); font-size: 13.5px; color: var(--text); }
        .rag-answer-label { font-size: 10px; color: var(--accent); margin-bottom: 8px; }
        .rag-hl { color: var(--accent); font-weight: 600; }
        .rag-cites { display: flex; gap: 16px; margin-top: 10px; font-family: var(--font-mono); font-size: 10px; color: var(--text-4); }
      `}</style>
    </div>
  );
}

/* Custom visual: Insurance agent orchestration graph */
function CaseVisualAgents() {
  return (
    <div className="cv cv-agents">
      <div className="cv-chrome">
        <span className="cv-dot"/><span className="cv-dot"/><span className="cv-dot"/>
        <span className="cv-url mono">meridian-claims / graph · v1.4</span>
      </div>
      <div className="cv-body" style={{paddingBottom: 8}}>
        <svg viewBox="0 0 400 240" className="ag-graph">
          {/* edges */}
          <g stroke="var(--border-3)" strokeWidth="1" fill="none">
            <path d="M60 120 H 140"/>
            <path d="M200 80 H 260"/>
            <path d="M200 160 H 260"/>
            <path d="M160 100 L 200 80"/>
            <path d="M160 140 L 200 160"/>
            <path d="M320 80 H 360"/>
            <path d="M320 160 H 360"/>
          </g>
          {/* active edge */}
          <path d="M60 120 H 140" stroke="var(--accent)" strokeWidth="1.5" fill="none" strokeDasharray="3 3">
            <animate attributeName="stroke-dashoffset" from="0" to="-12" dur="1.2s" repeatCount="indefinite"/>
          </path>
          {/* nodes */}
          <g>
            <rect x="12" y="100" width="48" height="40" rx="6" fill="var(--bg-soft)" stroke="var(--accent)" strokeWidth="1.3"/>
            <text x="36" y="124" textAnchor="middle" fill="var(--accent)" fontFamily="Geist Mono" fontSize="9">INTAKE</text>
            
            <rect x="140" y="100" width="60" height="40" rx="6" fill="var(--bg-soft)" stroke="var(--text-3)" strokeWidth="1"/>
            <text x="170" y="124" textAnchor="middle" fill="var(--text-2)" fontFamily="Geist Mono" fontSize="9">CLASSIFY</text>
            
            <rect x="200" y="60" width="60" height="40" rx="6" fill="var(--bg-soft)" stroke="var(--text-3)" strokeWidth="1"/>
            <text x="230" y="84" textAnchor="middle" fill="var(--text-2)" fontFamily="Geist Mono" fontSize="9">FRAUD?</text>
            
            <rect x="200" y="140" width="60" height="40" rx="6" fill="var(--bg-soft)" stroke="var(--text-3)" strokeWidth="1"/>
            <text x="230" y="164" textAnchor="middle" fill="var(--text-2)" fontFamily="Geist Mono" fontSize="9">ENRICH</text>
            
            <rect x="260" y="60" width="60" height="40" rx="6" fill="var(--bg-soft)" stroke="var(--text-3)" strokeWidth="1"/>
            <text x="290" y="84" textAnchor="middle" fill="var(--text-2)" fontFamily="Geist Mono" fontSize="9">REVIEW</text>
            
            <rect x="260" y="140" width="60" height="40" rx="6" fill="var(--bg-soft)" stroke="var(--text-3)" strokeWidth="1"/>
            <text x="290" y="164" textAnchor="middle" fill="var(--text-2)" fontFamily="Geist Mono" fontSize="9">APPROVE</text>
            
            <rect x="360" y="60" width="28" height="40" rx="6" fill="var(--bg-soft)" stroke="var(--text-3)" strokeWidth="1"/>
            <text x="374" y="84" textAnchor="middle" fill="var(--text-3)" fontFamily="Geist Mono" fontSize="8">HOLD</text>
            
            <rect x="360" y="140" width="28" height="40" rx="6" fill="var(--bg-soft)" stroke="var(--accent)" strokeWidth="1.3"/>
            <text x="374" y="164" textAnchor="middle" fill="var(--accent)" fontFamily="Geist Mono" fontSize="8">PAY</text>
          </g>
        </svg>
        <div className="ag-log">
          <div className="ag-log-line"><span className="mono ag-t">14:02:18</span> classify → auto_approve · conf 0.94</div>
          <div className="ag-log-line"><span className="mono ag-t">14:02:19</span> enrich → ok</div>
          <div className="ag-log-line ag-log-line--a"><span className="mono ag-t">14:02:19</span> approve → $1,240 payout queued</div>
        </div>
      </div>
      <style>{`
        .ag-graph { width: 100%; height: auto; display: block; }
        .ag-log { padding: 10px 12px; border-top: 1px solid var(--border); font-family: var(--font-mono); font-size: 11px; color: var(--text-3); display: flex; flex-direction: column; gap: 4px; }
        .ag-log-line--a { color: var(--accent); }
        .ag-t { color: var(--text-4); margin-right: 10px; }
      `}</style>
    </div>
  );
}

const CASES = [
  {
    tag: "HOSPITALITY",
    name: "Harbor Hotels",
    href: "case-harbor.html",
    problem: "14-property boutique chain drowning in after-hours reservation calls.",
    metrics: [
      ["61%", "lower handle time"],
      ["24/7", "voice coverage"],
      ["$312k", "saved annually"],
    ],
    stack: ["Vapi", "Deepgram", "ElevenLabs", "Twilio", "FastAPI"],
    Visual: CaseVisualVoice,
  },
  {
    tag: "FINTECH",
    name: "Axia Capital",
    href: "case-axia.html",
    problem: "Analysts losing 6+ hours a week hunting through PDFs, memos, and spreadsheets.",
    metrics: [
      ["8×", "faster research"],
      ["98.2%", "citation accuracy"],
      ["4.3k", "docs indexed"],
    ],
    stack: ["Claude", "Pinecone", "LlamaIndex", "Next.js", "Postgres"],
    Visual: CaseVisualRAG,
  },
  {
    tag: "INSURANCE",
    name: "Meridian Claims",
    href: "case-meridian.html",
    problem: "Manual claim triage bottlenecked at 400/day with inconsistent fraud detection.",
    metrics: [
      ["5", "agents orchestrated"],
      ["92%", "auto-resolution"],
      ["3.1d → 4h", "cycle time"],
    ],
    stack: ["LangGraph", "Claude", "Temporal", "Stripe", "AWS"],
    Visual: CaseVisualAgents,
  },
];

function CaseStudy({ c, i }) {
  const flip = i % 2 === 1;
  const { Visual } = c;
  return (
    <article className={"case reveal " + (flip ? "case--flip" : "")}>
      <div className="case-visual">
        <Visual/>
      </div>
      <div className="case-text">
        <span className="tag">{c.tag}</span>
        <h3 className="case-name">{c.name}</h3>
        <p className="case-problem">{c.problem}</p>
        <div className="case-metrics">
          {c.metrics.map(([v, l]) => (
            <div className="case-metric" key={l}>
              <div className="case-metric-v">{v}</div>
              <div className="case-metric-l mono">{l}</div>
            </div>
          ))}
        </div>
        <div className="case-stack">
          {c.stack.map(s => <span key={s} className="case-stack-pill">{s}</span>)}
        </div>
        <a href={c.href} className="case-link">
          Read case study <I.Arrow size={14}/>
        </a>
      </div>
      <style>{`
        .case {
          display: grid;
          grid-template-columns: 1.05fr 1fr;
          gap: 72px;
          align-items: center;
          padding: 56px 0;
          border-top: 1px solid var(--border);
        }
        .case:first-of-type { border-top: 0; }
        .case--flip .case-visual { order: 2; }
        .case-visual { min-width: 0; }
        .case-name { font-size: clamp(32px, 3.2vw, 44px); font-weight: 600; letter-spacing: -0.028em; margin: 16px 0 20px; line-height: 1; }
        .case-problem { font-size: 17px; line-height: 1.55; color: var(--text-2); max-width: 46ch; }
        .case-metrics {
          display: grid; grid-template-columns: repeat(3, 1fr);
          gap: 24px;
          padding: 28px 0;
          margin: 32px 0 28px;
          border-top: 1px solid var(--border);
          border-bottom: 1px solid var(--border);
        }
        .case-metric-v { font-size: 30px; font-weight: 600; letter-spacing: -0.03em; color: var(--text); line-height: 1; }
        .case-metric-l { font-size: 10.5px; color: var(--text-4); margin-top: 8px; }
        .case-stack { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 28px; }
        .case-stack-pill {
          font-family: var(--font-mono); font-size: 10.5px;
          padding: 4px 9px; border: 1px solid var(--border-2); border-radius: 999px;
          color: var(--text-3);
          letter-spacing: 0.06em;
        }
        .case-link {
          display: inline-flex; align-items: center; gap: 8px;
          color: var(--accent); font-size: 14px; font-weight: 500;
          border-bottom: 1px solid transparent;
          padding-bottom: 2px;
          transition: border-color .2s;
        }
        .case-link:hover { border-color: var(--accent); }
        .case-link svg { transition: transform .2s; }
        .case-link:hover svg { transform: translateX(3px); }
        @media (max-width: 960px) {
          .case { grid-template-columns: 1fr; gap: 36px; }
          .case--flip .case-visual { order: 0; }
        }
        @media (max-width: 480px) {
          .case-metrics { grid-template-columns: 1fr; gap: 20px; padding: 20px 0; }
          .case-metric-v { font-size: 26px; }
        }
      `}</style>
    </article>
  );
}

function CaseStudies() {
  return (
    <section id="work" className="cases container" aria-label="Case studies">
      <SectionHeader num="02" label="Selected work">
        Production systems, <span style={{color:"var(--text-3)"}}>real outcomes.</span>
      </SectionHeader>
      <div>
        {CASES.map((c, i) => <CaseStudy key={c.name} c={c} i={i}/>)}
      </div>
      <style>{`
        .cases { padding: var(--section-y) 0; }
      `}</style>
    </section>
  );
}

Object.assign(window, { CaseStudies });
