// ============================================================================
//  CLIENT CARD — картка клієнта CRM (Digitalshop)
//  Дизайн із handoff_client_card, підключений до реального /api/client/:phone.
//  Зміни проти оригіналу: реальна мапа статусів Хорошопа (CC_ORDER_STATUS),
//  кольори статусів заявок (CC_REQ_STATUS), збереження мітки/замітки на бекенд
//  (CcNoteBlock → API.setClientNote). Решта — як у дизайні.
//
//  API: ClientCard({ phone, manager, isMobile, callType, onClose, onCreateLead,
//                    onOpenOrder, onOpenRequest, showToast })
// ============================================================================
const { useState: ccUseState, useEffect: ccUseEffect, useRef: ccUseRef } = React;

// ── format helpers ──────────────────────────────────────────────────────────
const ccFmt   = (n) => (Math.round(n || 0)).toLocaleString("uk-UA").replace(/ /g, " ");
const ccPhone = (p) => "+" + (p || "").replace(/\D/g, "").replace(/^(\d{2})(\d{3})(\d{3})(\d{2})(\d{2}).*/, "$1 ($2) $3-$4-$5");
const ccSoft  = (c, pct = 14) => `color-mix(in oklab, ${c} ${pct}%, transparent)`;
const ccDur   = (s) => { s = s || 0; const m = Math.floor(s / 60); return (m < 10 ? "0" : "") + m + ":" + (s % 60 < 10 ? "0" : "") + (s % 60); };
const ccInitials = (name) => (name || "").trim().split(/\s+/).slice(0, 2).map(w => w[0] || "").join("").toUpperCase() || "?";

const CC_GOLD = "#F5C451"; // VIP неон

// ── статуси замовлень (РЕАЛЬНІ id Хорошопа) → підпис + колір з палітри теми ───
const CC_ORDER_STATUS = {
  1:  { label: "Новий",               color: "var(--status-new)" },
  9:  { label: "Очікуємо відповідь",  color: "var(--status-preparing)" },
  2:  { label: "В обробці",           color: "var(--status-confirmed)" },
  16: { label: "Очікуємо Оплату",     color: "var(--status-preparing)" },
  10: { label: "Готуємо до відправки",color: "var(--status-confirmed)" },
  7:  { label: "Очікує поставку",     color: "var(--status-preparing)" },
  13: { label: "Готова ТТН",          color: "var(--status-shipped)" },
  6:  { label: "Надіслане",           color: "var(--status-shipped)" },
  3:  { label: "В відділенні",        color: "var(--status-shipped)" },
  14: { label: "Очікує переміщення",  color: "var(--status-preparing)" },
  15: { label: "Очікує самовивіз",    color: "var(--status-preparing)" },
  11: { label: "Клієнт думає",        color: "var(--status-new)" },
  8:  { label: "Виконано",            color: "var(--status-completed)" },
  4:  { label: "Скасовано",           color: "var(--status-cancelled)" },
};
const ccStatus = (n) => CC_ORDER_STATUS[n] || CC_ORDER_STATUS[1];

// ── статуси заявок (рядкові, з LEAD_STATUSES) → колір дот-індикатора ──────────
const CC_REQ_STATUS = {
  "Нова":         "var(--accent)",
  "В роботі":     "var(--warning)",
  "Конвертовано": "var(--success)",
  "Закрита":      "var(--fg-muted)",
};
const ccReqColor = (s) => CC_REQ_STATUS[s] || "var(--fg-muted)";

// ── теги-мітки клієнта ────────────────────────────────────────────────────────
const CC_TAGS = [
  { key: "norm",      emoji: "🟢", label: "Норм",        color: "var(--success)" },
  { key: "problem",   emoji: "🔴", label: "Проблемний",  color: "var(--danger)" },
  { key: "messenger", emoji: "💬", label: "Месенджер",   color: "var(--info)" },
];

// ── месенджери (бренд-марки) ──────────────────────────────────────────────────
const CC_MSG = {
  viber:    { color: "#7360F2", path: "M11.4 0c-.7 0-2.7.1-4.1.6C5 1.4 4 2.7 3.5 4.4 3 6.1 3 7.8 3 9.6c0 1.7.1 3.5.5 5 .5 1.7 1.4 3 3.7 3.6.4.1.5.2.5.6v1.7c0 .4.3.5.6.3l2-1.6c.2-.2.4-.2.7-.2 1.5.1 3.2 0 4.7-.4 1.7-.5 2.7-1.6 3.2-3.3.4-1.5.5-3.2.5-5 0-1.7 0-3.5-.5-5-.5-1.7-1.6-3-3.4-3.5C13.9 0 12.4 0 11.4 0z" },
  telegram: { color: "#229ED9", path: "M12 0C5.4 0 0 5.4 0 12s5.4 12 12 12 12-5.4 12-12S18.6 0 12 0zm5.6 8.2-1.9 8.9c-.1.6-.5.8-1.1.5l-3-2.2-1.4 1.4c-.2.2-.3.3-.6.3l.2-3 5.6-5c.2-.2 0-.3-.3-.1l-6.9 4.3-3-.9c-.6-.2-.6-.6.1-.9l11.7-4.5c.5-.2 1 .1.6 1.2z" },
};

// ── keyframes (інжектяться один раз) ─────────────────────────────────────────
const CC_KEYFRAMES = `
@keyframes ccRise    { from { transform: translateY(10px) } to { transform: none } }
@keyframes ccRing    { 0% { transform: scale(.7); opacity: .55 } 70% { opacity: 0 } 100% { transform: scale(2.6); opacity: 0 } }
@keyframes ccBlink   { 0%,100% { opacity: 1 } 50% { opacity: .35 } }
@keyframes ccShimmer { 0% { background-position: 200% 0 } 100% { background-position: -200% 0 } }
@keyframes ccSkel    { 0%,100% { opacity: .45 } 50% { opacity: .9 } }
@keyframes ccGlow    { 0%,100% { box-shadow: 0 0 0 1px ${ccSoft(CC_GOLD,45)}, 0 0 22px ${ccSoft(CC_GOLD,22)} } 50% { box-shadow: 0 0 0 1px ${ccSoft(CC_GOLD,60)}, 0 0 34px ${ccSoft(CC_GOLD,34)} } }
`;
// інжектимо ОДРАЗУ при завантаженні скрипта — до першого рендеру, інакше
// анімація въезду стартує без оголошених keyframes і застрягає на opacity:0.
(function ccInjectKeyframes() {
  if (typeof document === "undefined" || document.getElementById("cc-keyframes")) return;
  const el = document.createElement("style");
  el.id = "cc-keyframes";
  el.textContent = CC_KEYFRAMES;
  document.head.appendChild(el);
})();
function CcStyleInjector() { return null; }

// ════════════════════════════════════════════════════════════════════════════
//  PRIMITIVES
// ════════════════════════════════════════════════════════════════════════════

// кнопка-іконка зі скляним фоном + hover-свічення
function CcIconBtn({ icon, title, onClick, size = 36, glow = "var(--accent)", danger }) {
  const [h, setH] = ccUseState(false);
  const tint = danger ? "var(--danger)" : glow;
  return (
    <button title={title} onClick={(e) => { e.stopPropagation(); onClick && onClick(); }}
      onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)} style={{
        width: size, height: size, borderRadius: 10, cursor: "pointer", flexShrink: 0,
        display: "flex", alignItems: "center", justifyContent: "center",
        border: "1px solid " + (h ? ccSoft(tint, 55) : "var(--border-default)"),
        background: h ? ccSoft(tint, 14) : "rgba(255,255,255,0.02)",
        color: h ? (danger ? "var(--danger)" : "var(--fg-primary)") : "var(--fg-secondary)",
        boxShadow: h ? "0 0 18px " + ccSoft(tint, 28) : "none",
        transition: "all 150ms cubic-bezier(.2,0,0,1)", backdropFilter: "blur(6px)",
      }}>
      <Icon name={icon} size={Math.round(size * 0.46)}/>
    </button>
  );
}

// месенджер: сірий, бренд-колір + свічення на hover
function CcMsgBtn({ brand, title, onClick }) {
  const m = CC_MSG[brand]; const [h, setH] = ccUseState(false);
  if (!m) return null;
  return (
    <button title={title} onClick={(e) => { e.stopPropagation(); onClick && onClick(); }}
      onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)} style={{
        width: 36, height: 36, borderRadius: 10, cursor: "pointer", padding: 0,
        display: "flex", alignItems: "center", justifyContent: "center",
        border: "1px solid " + (h ? ccSoft(m.color, 55) : "var(--border-default)"),
        background: h ? ccSoft(m.color, 16) : "rgba(255,255,255,0.02)",
        color: h ? m.color : "var(--fg-muted)",
        boxShadow: h ? "0 0 18px " + ccSoft(m.color, 30) : "none",
        transition: "all 150ms cubic-bezier(.2,0,0,1)", backdropFilter: "blur(6px)",
      }}>
      <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"><path d={m.path}/></svg>
    </button>
  );
}

// секція з заголовком, лічильником і м'яким fade-up
function CcSection({ title, count, action, delay = 0, children }) {
  return (
    <section style={{ animation: `ccRise 420ms cubic-bezier(.2,0,0,1) ${delay}ms both` }}>
      <div style={{ display: "flex", alignItems: "center", gap: 9, marginBottom: 12 }}>
        <span style={{ fontSize: 11, fontWeight: 600, letterSpacing: ".08em", textTransform: "uppercase", color: "var(--fg-muted)" }}>{title}</span>
        {count != null && count > 0 && (
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, fontWeight: 600, color: "var(--fg-secondary)",
            minWidth: 20, height: 18, padding: "0 6px", borderRadius: 999, display: "inline-flex", alignItems: "center", justifyContent: "center",
            background: "rgba(255,255,255,0.05)", border: "1px solid var(--border-subtle)" }}>{count}</span>
        )}
        <div style={{ flex: 1, height: 1, background: "var(--border-subtle)" }}/>
        {action}
      </div>
      {children}
    </section>
  );
}

// рядок-карточка з hover-підйомом
function CcRow({ children, onClick, accent }) {
  const [h, setH] = ccUseState(false);
  return (
    <div onClick={onClick} onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)} style={{
      cursor: onClick ? "pointer" : "default", borderRadius: 12, padding: "11px 13px",
      background: h ? "rgba(255,255,255,0.035)" : "rgba(255,255,255,0.018)",
      border: "1px solid " + (h ? ccSoft(accent || "var(--accent)", 38) : "var(--border-subtle)"),
      boxShadow: h && onClick ? "0 0 22px " + ccSoft(accent || "var(--accent)", 14) : "none",
      transform: h && onClick ? "translateY(-1px)" : "none",
      transition: "all 150ms cubic-bezier(.2,0,0,1)",
    }}>{children}</div>
  );
}

function CcStatusChip({ n, sm }) {
  const s = ccStatus(n);
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 6, height: sm ? 21 : 24, padding: sm ? "0 9px" : "0 11px",
      borderRadius: 999, fontSize: sm ? 11 : 12, fontWeight: 600, color: s.color, background: ccSoft(s.color, 15),
      border: "1px solid " + ccSoft(s.color, 26), whiteSpace: "nowrap" }}>
      <span style={{ width: 6, height: 6, borderRadius: "50%", background: s.color, boxShadow: "0 0 6px " + s.color }}/>{s.label}
    </span>
  );
}

// скелетон-плашка в стилі теми
function CcSkel({ w = "100%", h = 14, r = 7, style }) {
  return <div style={{ width: w, height: h, borderRadius: r, background: "var(--bg-raised)",
    border: "1px solid var(--border-subtle)", animation: "ccSkel 1.3s ease-in-out infinite", ...style }}/>;
}

// ════════════════════════════════════════════════════════════════════════════
//  HEADER
// ════════════════════════════════════════════════════════════════════════════
function CcHeader({ data, manager, callType, onClose, showToast, isVip, isNew, isMobile }) {
  const name = data && data.name;
  const phoneStr = ccPhone(data ? data.phone : "");
  const digits = (data && data.phone || "").replace(/\D/g, "");
  const copy = () => { try { navigator.clipboard && navigator.clipboard.writeText("+" + digits); } catch (e) {} showToast && showToast("Номер скопійовано"); };
  const openMsg = (brand) => {
    const url = brand === "viber" ? `viber://chat?number=${encodeURIComponent("+" + digits)}` : `https://t.me/+${digits}`;
    try { window.open(url, "_blank"); } catch (e) {}
  };

  // аватар: VIP — золоте кільце+свічення; новий — пунктир+іконка; звичайний — акцент
  const avatarRing = isVip
    ? { background: `conic-gradient(from 140deg, ${CC_GOLD}, #FFE7A8, ${CC_GOLD}, #C99A2E, ${CC_GOLD})`, animation: "ccGlow 3.2s ease-in-out infinite" }
    : isNew
      ? { background: "transparent", border: "1.5px dashed var(--border-strong)" }
      : { background: `linear-gradient(135deg, var(--accent), #8B8DF7)`, boxShadow: "0 0 22px " + ccSoft("var(--accent)", 30) };

  return (
    <div style={{ position: "relative", flexShrink: 0,
      padding: isMobile ? "max(52px, calc(env(safe-area-inset-top) + 14px)) 20px 18px" : "16px 20px 18px",
      borderBottom: "1px solid var(--border-subtle)", overflow: "hidden", backdropFilter: "blur(14px)",
      background: "linear-gradient(180deg, rgba(255,255,255,0.035), rgba(255,255,255,0))" }}>
      {/* декоративне свічення згори */}
      <div style={{ position: "absolute", top: -120, right: -40, width: 300, height: 240, pointerEvents: "none",
        background: `radial-gradient(circle, ${ccSoft(isVip ? CC_GOLD : "var(--accent)", 22)}, transparent 70%)`, filter: "blur(8px)" }}/>

      {/* верхній рядок: індикатор дзвінка + закрити */}
      <div style={{ position: "relative", display: "flex", alignItems: "center", marginBottom: 16 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 11, flex: 1, minWidth: 0 }}>
          <div style={{ position: "relative", width: 10, height: 10, flexShrink: 0 }}>
            <span style={{ position: "absolute", inset: 0, borderRadius: "50%", background: "var(--success)", animation: "ccBlink 1.4s ease-in-out infinite" }}/>
            <span style={{ position: "absolute", inset: 0, borderRadius: "50%", border: "2px solid var(--success)", animation: "ccRing 1.8s ease-out infinite" }}/>
          </div>
          <span style={{ fontSize: 12, fontWeight: 600, letterSpacing: ".05em", color: "var(--success)", textTransform: "uppercase" }}>
            {callType === "out" ? "Вихідний дзвінок" : "Вхідний дзвінок"}
          </span>
          <Icon name={callType === "out" ? "phone-outgoing" : "phone-incoming"} size={14} color="var(--success)"/>
        </div>
        <CcIconBtn icon="x" title="Закрити" onClick={onClose} size={34} danger/>
      </div>

      {/* клієнт */}
      <div style={{ position: "relative", display: "flex", alignItems: "center", gap: 14 }}>
        {/* аватар */}
        <div style={{ width: 58, height: 58, borderRadius: "50%", flexShrink: 0, padding: isVip ? 2 : 0, ...avatarRing,
          display: "flex", alignItems: "center", justifyContent: "center" }}>
          <div style={{ width: "100%", height: "100%", borderRadius: "50%", display: "flex", alignItems: "center", justifyContent: "center",
            background: isVip ? "var(--bg-panel)" : "transparent",
            color: isNew ? "var(--fg-muted)" : (isVip ? CC_GOLD : "#fff"), fontWeight: 700, fontSize: 19,
            fontFamily: isNew ? "inherit" : "var(--font-ui)" }}>
            {isNew ? <Icon name="user" size={24}/> : ccInitials(name)}
          </div>
        </div>

        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ display: "flex", alignItems: "center", gap: 9, flexWrap: "wrap" }}>
            <h2 style={{ fontSize: isNew ? 18 : 21, fontWeight: 700, margin: 0, color: "var(--fg-primary)", letterSpacing: "-.01em",
              lineHeight: 1.1, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis", maxWidth: "100%" }}>
              {isNew ? "🆕 Новий клієнт" : name}
            </h2>
            {isVip && <CcVipBadge/>}
          </div>
          <div style={{ display: "flex", alignItems: "center", gap: 10, marginTop: 9, flexWrap: "wrap" }}>
            <span style={{ fontFamily: "var(--font-mono)", fontSize: 15, fontWeight: 500, color: "var(--fg-primary)", letterSpacing: ".02em" }}>{phoneStr}</span>
            <div style={{ display: "flex", gap: 6 }}>
              <CcIconBtn icon="copy" title="Копіювати номер" onClick={copy} size={32}/>
              <CcMsgBtn brand="viber" title="Viber" onClick={() => openMsg("viber")}/>
              <CcMsgBtn brand="telegram" title="Telegram" onClick={() => openMsg("telegram")}/>
            </div>
          </div>
        </div>
      </div>

      {/* з'єднано з */}
      {manager && (
        <div style={{ position: "relative", display: "flex", alignItems: "center", gap: 7, marginTop: 14, fontSize: 12, color: "var(--fg-muted)" }}>
          <Icon name="user" size={13}/>
          <span>З'єднано з:&nbsp;<span style={{ color: "var(--fg-secondary)", fontWeight: 600 }}>{manager}</span></span>
        </div>
      )}
    </div>
  );
}

function CcVipBadge() {
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 5, height: 23, padding: "0 11px", borderRadius: 999,
      fontSize: 11.5, fontWeight: 700, letterSpacing: ".06em", color: "#241a02",
      background: `linear-gradient(110deg, ${CC_GOLD}, #FFE7A8, ${CC_GOLD})`, backgroundSize: "200% 100%",
      animation: "ccShimmer 3.5s linear infinite", boxShadow: "0 0 18px " + ccSoft(CC_GOLD, 38) }}>
      <Icon name="sparkles" size={12} color="#241a02"/>VIP
    </span>
  );
}

// ════════════════════════════════════════════════════════════════════════════
//  BADGES + SUMMARY
// ════════════════════════════════════════════════════════════════════════════
function CcSummary({ data, isVip, delay }) {
  const c = data.counts || {};
  const isRegular = (c.orders || 0) >= 3;
  const others = (data.names || []).filter(n => n && n !== data.name);

  const chips = [];
  if (isRegular && !isVip) chips.push({ icon: "repeat", label: "Постійний", color: "var(--accent)" });
  if (data.lastManager) chips.push({ icon: "user-check", label: "Веде: " + data.lastManager, color: "var(--info)" });

  const metrics = [
    { label: "Замовлень", value: c.orders || 0 },
    { label: "Заявок",    value: c.requests || 0 },
    { label: "LTV",       value: ccFmt(data.ltv) + " ₴", accent: isVip ? CC_GOLD : null, big: true },
    { label: "Дзвінків",  value: c.calls || 0 },
  ];

  return (
    <div style={{ animation: `ccRise 420ms cubic-bezier(.2,0,0,1) ${delay}ms both`, display: "flex", flexDirection: "column", gap: 14 }}>
      {chips.length > 0 && (
        <div style={{ display: "flex", flexWrap: "wrap", gap: 8 }}>
          {chips.map((ch, i) => (
            <span key={i} style={{ display: "inline-flex", alignItems: "center", gap: 6, height: 26, padding: "0 11px", borderRadius: 999,
              fontSize: 12, fontWeight: 600, color: ch.color, background: ccSoft(ch.color, 13), border: "1px solid " + ccSoft(ch.color, 28) }}>
              <Icon name={ch.icon} size={13}/>{ch.label}
            </span>
          ))}
        </div>
      )}

      {/* 4 метрики */}
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 8 }}>
        {metrics.map((m, i) => (
          <div key={i} style={{ padding: "11px 12px", borderRadius: 12, background: "rgba(255,255,255,0.022)",
            border: "1px solid " + (m.accent ? ccSoft(m.accent, 32) : "var(--border-subtle)"),
            boxShadow: m.accent ? "0 0 20px " + ccSoft(m.accent, 14) : "none" }}>
            <div style={{ fontFamily: "var(--font-mono)", fontVariantNumeric: "tabular-nums", fontWeight: 700,
              fontSize: m.big ? 16 : 19, lineHeight: 1.1, color: m.accent || "var(--fg-primary)", letterSpacing: "-.01em",
              whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{m.value}</div>
            <div style={{ fontSize: 10.5, color: "var(--fg-muted)", marginTop: 5, letterSpacing: ".03em", textTransform: "uppercase", fontWeight: 500 }}>{m.label}</div>
          </div>
        ))}
      </div>

      {others.length > 0 && (
        <div style={{ fontSize: 12, color: "var(--fg-muted)", display: "flex", gap: 6, flexWrap: "wrap", alignItems: "center" }}>
          <Icon name="users" size={13}/><span>Також замовляв як:</span>
          {others.map((n, i) => (
            <span key={i} style={{ color: "var(--fg-secondary)", fontWeight: 500 }}>{n}{i < others.length - 1 ? "," : ""}</span>
          ))}
        </div>
      )}
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
//  TAG + NOTE  (з персистом на бекенд)
// ════════════════════════════════════════════════════════════════════════════
function CcNoteBlock({ data, delay, showToast, phone }) {
  const [tag, setTag] = ccUseState((data.note && data.note.tag) || null);
  const [text, setText] = ccUseState((data.note && data.note.note) || "");
  const [saved, setSaved] = ccUseState(false);
  const timer = ccUseRef(null);

  const persist = (noteText, tagVal) => {
    if (window.API && API.setClientNote) API.setClientNote(phone, { note: noteText, tag: tagVal || "" }).catch(() => {});
  };
  const flagSaved = () => { setSaved(true); clearTimeout(timer.current); timer.current = setTimeout(() => setSaved(false), 1600); };
  const onText = (v) => {
    setText(v); clearTimeout(timer.current);
    timer.current = setTimeout(() => { persist(v, tag); setSaved(true); setTimeout(() => setSaved(false), 1600); }, 700);
  };
  const pickTag = (k) => { const next = tag === k ? null : k; setTag(next); persist(text, next); flagSaved(); showToast && showToast("Мітку оновлено"); };

  return (
    <div style={{ animation: `ccRise 420ms cubic-bezier(.2,0,0,1) ${delay}ms both`, display: "flex", flexDirection: "column", gap: 11 }}>
      <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
        <span style={{ fontSize: 11, fontWeight: 600, letterSpacing: ".08em", textTransform: "uppercase", color: "var(--fg-muted)" }}>Мітка та замітка</span>
        <div style={{ flex: 1 }}/>
        <span style={{ display: "inline-flex", alignItems: "center", gap: 5, fontSize: 11, fontWeight: 500,
          color: "var(--success)", opacity: saved ? 1 : 0, transition: "opacity 200ms" }}>
          <Icon name="check" size={12}/>Збережено
        </span>
      </div>

      <div style={{ display: "flex", gap: 7, flexWrap: "wrap" }}>
        {CC_TAGS.map(t => {
          const on = tag === t.key;
          return (
            <button key={t.key} onClick={() => pickTag(t.key)} style={{
              display: "inline-flex", alignItems: "center", gap: 6, height: 32, padding: "0 13px", borderRadius: 999, cursor: "pointer",
              fontFamily: "inherit", fontSize: 12.5, fontWeight: 600,
              color: on ? t.color : "var(--fg-secondary)",
              background: on ? ccSoft(t.color, 15) : "rgba(255,255,255,0.02)",
              border: "1px solid " + (on ? ccSoft(t.color, 50) : "var(--border-default)"),
              boxShadow: on ? "0 0 16px " + ccSoft(t.color, 22) : "none",
              transition: "all 150ms cubic-bezier(.2,0,0,1)" }}>
              <span style={{ fontSize: 13 }}>{t.emoji}</span>{t.label}
            </button>
          );
        })}
      </div>

      <CcNoteField text={text} onText={onText}/>

      {data.note && data.note.updatedBy && (
        <div style={{ fontSize: 11, color: "var(--fg-muted)" }}>
          оновив <span style={{ color: "var(--fg-secondary)" }}>{data.note.updatedBy}</span>{data.note.updatedAt ? " · " + data.note.updatedAt : ""}
        </div>
      )}
    </div>
  );
}
function CcNoteField({ text, onText }) {
  const [foc, setFoc] = ccUseState(false);
  return (
    <textarea value={text} onChange={e => onText(e.target.value)} onFocus={() => setFoc(true)} onBlur={() => setFoc(false)}
      placeholder="Замітка про клієнта… (автозбереження)" rows={2} style={{
        width: "100%", boxSizing: "border-box", resize: "vertical", minHeight: 58, padding: "11px 13px", borderRadius: 12,
        fontFamily: "inherit", fontSize: 13, lineHeight: 1.5, color: "var(--fg-primary)",
        background: foc ? "rgba(255,255,255,0.04)" : "rgba(255,255,255,0.02)",
        border: "1px solid " + (foc ? "var(--border-focus)" : "var(--border-default)"),
        boxShadow: foc ? "0 0 0 3px var(--accent-ring)" : "none",
        outline: "none", transition: "all 150ms cubic-bezier(.2,0,0,1)" }}/>
  );
}

// ════════════════════════════════════════════════════════════════════════════
//  LISTS — orders / requests / calls / activity
// ════════════════════════════════════════════════════════════════════════════
function CcOrderRow({ o, onOpen }) {
  const s = ccStatus(o.status);
  return (
    <CcRow onClick={() => onOpen && onOpen(o)} accent={s.color}>
      <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
        <span style={{ fontFamily: "var(--font-mono)", fontSize: 12.5, fontWeight: 600, color: "var(--fg-muted)", flexShrink: 0 }}>#{o.id}</span>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 13, color: "var(--fg-primary)", fontWeight: 500, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>
            {o.product}{o.productsCount > 1 ? <span style={{ color: "var(--fg-muted)" }}> +{o.productsCount - 1}</span> : ""}
          </div>
          <div style={{ display: "flex", alignItems: "center", gap: 8, marginTop: 5 }}>
            <CcStatusChip n={o.status} sm/>
            {o.payed ? (
              <span style={{ display: "inline-flex", alignItems: "center", gap: 4, fontSize: 11, fontWeight: 600, color: "var(--credit)" }}>
                <Icon name="badge-check" size={12}/>оплачено
              </span>
            ) : null}
          </div>
        </div>
        <div style={{ textAlign: "right", flexShrink: 0 }}>
          <div style={{ fontFamily: "var(--font-mono)", fontVariantNumeric: "tabular-nums", fontSize: 13.5, fontWeight: 700, color: "var(--fg-primary)" }}>{ccFmt(o.total)} ₴</div>
          <div style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--fg-muted)", marginTop: 3 }}>{o.created}</div>
        </div>
        <Icon name="chevron-right" size={16} color="var(--fg-disabled)"/>
      </div>
    </CcRow>
  );
}

function CcRequestRow({ r, onOpen }) {
  const col = ccReqColor(r.status);
  return (
    <CcRow onClick={() => onOpen && onOpen(r)} accent={col}>
      <div style={{ display: "flex", alignItems: "center", gap: 11 }}>
        <span style={{ width: 8, height: 8, borderRadius: "50%", background: col, boxShadow: "0 0 8px " + col, flexShrink: 0 }}/>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 13, color: "var(--fg-primary)", fontWeight: 500, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{r.product || r.type || "Заявка"}</div>
          <div style={{ fontSize: 11.5, color: "var(--fg-muted)", marginTop: 4 }}>
            {r.type}{r.manager ? <span><span style={{ color: "var(--fg-disabled)" }}> · </span>{r.manager}</span> : ""}
          </div>
        </div>
        <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--fg-muted)", flexShrink: 0 }}>{r.date}</span>
        <Icon name="chevron-right" size={16} color="var(--fg-disabled)"/>
      </div>
    </CcRow>
  );
}

function CcCallRow({ c }) {
  const out = c.call_type === "out";
  const missed = !out && (c.billsec || 0) === 0;
  const tone = missed ? "var(--danger)" : "var(--fg-secondary)";
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 11, padding: "9px 4px" }}>
      <div style={{ width: 28, height: 28, borderRadius: 8, flexShrink: 0, display: "flex", alignItems: "center", justifyContent: "center",
        background: ccSoft(missed ? "var(--danger)" : out ? "var(--accent)" : "var(--success)", 12),
        color: missed ? "var(--danger)" : out ? "var(--accent)" : "var(--success)" }}>
        <Icon name={out ? "phone-outgoing" : "phone-incoming"} size={14}/>
      </div>
      <span style={{ flex: 1, minWidth: 0, fontSize: 12.5, color: "var(--fg-primary)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>{c.employee_fio || "—"}</span>
      <span style={{ fontFamily: "var(--font-mono)", fontSize: 11.5, color: tone, flexShrink: 0 }}>{missed ? "пропущений" : ccDur(c.billsec)}</span>
      <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--fg-muted)", flexShrink: 0 }}>{c.calldate}</span>
    </div>
  );
}

// журнал дій — згортуваний (за замовч. згорнутий)
function CcActivityLog({ activity, delay }) {
  const [open, setOpen] = ccUseState(false);
  const items = activity || [];
  return (
    <div style={{ animation: `ccRise 420ms cubic-bezier(.2,0,0,1) ${delay}ms both` }}>
      <button onClick={() => setOpen(o => !o)} style={{ width: "100%", display: "flex", alignItems: "center", gap: 9, padding: "4px 0",
        background: "transparent", border: 0, cursor: "pointer", fontFamily: "inherit" }}>
        <Icon name={open ? "chevron-down" : "chevron-right"} size={15} color="var(--fg-muted)"/>
        <span style={{ fontSize: 11, fontWeight: 600, letterSpacing: ".08em", textTransform: "uppercase", color: "var(--fg-muted)" }}>Журнал дій</span>
        {items.length > 0 && (
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, fontWeight: 600, color: "var(--fg-secondary)",
            minWidth: 20, height: 18, padding: "0 6px", borderRadius: 999, display: "inline-flex", alignItems: "center", justifyContent: "center",
            background: "rgba(255,255,255,0.05)", border: "1px solid var(--border-subtle)" }}>{items.length}</span>
        )}
        <div style={{ flex: 1, height: 1, background: "var(--border-subtle)" }}/>
      </button>

      {open && (
        <div style={{ marginTop: 12, paddingLeft: 7, display: "flex", flexDirection: "column", gap: 0 }}>
          {items.length === 0 && <div style={{ fontSize: 12.5, color: "var(--fg-disabled)", padding: "4px 0 4px 16px" }}>Подій ще не було</div>}
          {items.map((a, i) => (
            <div key={i} style={{ display: "flex", gap: 12, position: "relative", paddingLeft: 18, paddingBottom: i < items.length - 1 ? 14 : 0 }}>
              {/* timeline */}
              <span style={{ position: "absolute", left: 3, top: 5, width: 7, height: 7, borderRadius: "50%", background: "var(--accent)", boxShadow: "0 0 8px " + ccSoft("var(--accent)", 50) }}/>
              {i < items.length - 1 && <span style={{ position: "absolute", left: 6, top: 12, bottom: -2, width: 1, background: "var(--border-subtle)" }}/>}
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontSize: 12.5, color: "var(--fg-primary)", lineHeight: 1.4 }}>
                  <span style={{ fontWeight: 600 }}>{a.action}</span>
                  {a.detail ? <span style={{ color: "var(--fg-secondary)" }}> — {a.detail}</span> : ""}
                </div>
                <div style={{ fontSize: 11, color: "var(--fg-muted)", marginTop: 3, fontFamily: "var(--font-mono)" }}>
                  {a.ts}{a.manager ? " · " + a.manager : ""}{a.order_id ? " · #" + a.order_id : ""}
                </div>
              </div>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}

// пустий стейт для секцій
function CcEmpty({ icon, text }) {
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 9, padding: "12px 13px", borderRadius: 12,
      border: "1px dashed var(--border-default)", color: "var(--fg-disabled)", fontSize: 12.5 }}>
      <Icon name={icon} size={15}/>{text}
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
//  SKELETON STATE
// ════════════════════════════════════════════════════════════════════════════
function CcSkeleton() {
  return (
    <div style={{ padding: "16px 20px", display: "flex", flexDirection: "column", gap: 22 }}>
      <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
        <CcSkel w={58} h={58} r="50%"/>
        <div style={{ flex: 1, display: "flex", flexDirection: "column", gap: 9 }}>
          <CcSkel w="55%" h={18}/>
          <CcSkel w="70%" h={13}/>
        </div>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 8 }}>
        {[0, 1, 2, 3].map(i => <CcSkel key={i} h={56} r={12}/>)}
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
        <CcSkel w={140} h={11}/>
        {[0, 1, 2].map(i => <CcSkel key={i} h={52} r={12}/>)}
      </div>
      <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
        <CcSkel w={120} h={11}/>
        {[0, 1].map(i => <CcSkel key={i} h={46} r={12}/>)}
      </div>
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
//  FOOTER
// ════════════════════════════════════════════════════════════════════════════
function CcFooter({ isMobile, isNew, onCreateLead, onClose }) {
  const [h, setH] = ccUseState(false);
  return (
    <div style={{ flexShrink: 0, padding: "13px 20px", paddingBottom: isMobile ? "calc(13px + env(safe-area-inset-bottom))" : 13,
      borderTop: "1px solid var(--border-subtle)", display: "flex", gap: 10,
      background: "linear-gradient(0deg, var(--bg-panel), rgba(255,255,255,0.012))", backdropFilter: "blur(14px)" }}>
      <button onClick={onCreateLead} onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)} style={{
        flex: 1, height: 48, border: 0, borderRadius: 13, cursor: "pointer", fontFamily: "inherit", fontSize: 15, fontWeight: 600, color: "#fff",
        display: "flex", alignItems: "center", justifyContent: "center", gap: 9,
        background: h ? "linear-gradient(135deg, var(--accent-hover), #9b9dff)" : "linear-gradient(135deg, var(--accent), #7C7FF3)",
        boxShadow: h ? "0 0 26px " + ccSoft("var(--accent)", 50) : "0 0 18px " + ccSoft("var(--accent)", 30),
        transition: "all 160ms cubic-bezier(.2,0,0,1)" }}>
        <Icon name="plus" size={18} color="#fff"/>Зробити заявку
      </button>
      <button onClick={onClose} title="Скинути дзвінок" style={{
        width: 48, height: 48, borderRadius: 13, cursor: "pointer", flexShrink: 0,
        display: "flex", alignItems: "center", justifyContent: "center",
        border: "1px solid " + ccSoft("var(--danger)", 30), background: ccSoft("var(--danger)", 8), color: "var(--danger)",
        transition: "all 150ms cubic-bezier(.2,0,0,1)" }}
        onMouseEnter={e => { e.currentTarget.style.background = ccSoft("var(--danger)", 18); }}
        onMouseLeave={e => { e.currentTarget.style.background = ccSoft("var(--danger)", 8); }}>
        <Icon name="phone-off" size={19}/>
      </button>
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
//  MAIN
// ════════════════════════════════════════════════════════════════════════════
function ClientCard({ phone, manager = "", isMobile = false, callType = "in",
                      onClose = () => {}, onCreateLead = () => {}, onOpenOrder = () => {}, onOpenRequest = () => {}, showToast }) {
  const [data, setData] = ccUseState(null);
  const [loading, setLoading] = ccUseState(true);
  const [shown, setShown] = ccUseState(isMobile); // десктоп — въезд справа; мобайл — одразу

  ccUseEffect(() => { const t = setTimeout(() => setShown(true), 20); return () => clearTimeout(t); }, []);

  ccUseEffect(() => {
    let alive = true;
    setLoading(true);
    const api = (window.API && window.API.getClient) ? window.API.getClient(phone) : Promise.resolve(null);
    Promise.resolve(api).then(d => { if (alive) { setData(d); setLoading(false); } }).catch(() => { if (alive) { setData(null); setLoading(false); } });
    return () => { alive = false; };
  }, [phone]);

  const shell = {
    position: "fixed", zIndex: 160, display: "flex", flexDirection: "column", overflow: "hidden",
    background: "var(--bg-panel)", color: "var(--fg-primary)", fontFamily: "var(--font-ui)",
    ...(isMobile
      ? { inset: 0 }
      : { top: 0, right: 0, bottom: 0, width: 600, maxWidth: "100%", height: "100dvh",
          borderLeft: "1px solid var(--border-default)", boxShadow: "var(--shadow-2)",
          // въезд через transition (а не keyframe-fill): кінцевий стан завжди видимий,
          // навіть якщо середовище паузить анімації (reduced-motion / прихований таб / print)
          transform: shown ? "none" : "translateX(36px)", opacity: shown ? 1 : 0,
          transition: "transform 260ms cubic-bezier(.2,0,0,1), opacity 260ms cubic-bezier(.2,0,0,1)" }),
  };

  const isNew = !loading && data && !data.name;
  const isVip = !loading && data && (data.ltv || 0) >= 100000;

  return (
    <div style={shell}>
      <CcStyleInjector/>

      {loading ? (
        <>
          <CcSkeleton/>
          <div style={{ flex: 1 }}/>
          <CcFooter isMobile={isMobile} isNew={false} onCreateLead={onCreateLead} onClose={onClose}/>
        </>
      ) : !data ? (
        <div style={{ flex: 1, display: "flex", alignItems: "center", justifyContent: "center", color: "var(--fg-muted)" }}>Не вдалося завантажити</div>
      ) : (
        <>
          <CcHeader data={data} manager={manager} callType={callType} onClose={onClose} showToast={showToast} isVip={isVip} isNew={isNew} isMobile={isMobile}/>

          <div style={{ flex: 1, overflowY: "auto", overflowX: "hidden", padding: "18px 20px 22px", display: "flex", flexDirection: "column", gap: 24 }}>
            <CcSummary data={data} isVip={isVip} delay={40}/>
            <CcNoteBlock data={data} delay={90} showToast={showToast} phone={phone}/>

            {/* ЗАМОВЛЕННЯ */}
            <CcSection title="Замовлення" count={(data.orders || []).length} delay={140}>
              {isNew || (data.orders || []).length === 0 ? (
                <CcEmpty icon="package" text="Замовлень ще немає"/>
              ) : (
                <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
                  {data.orders.map(o => <CcOrderRow key={o.id} o={o} onOpen={onOpenOrder}/>)}
                </div>
              )}
            </CcSection>

            {/* ЗАЯВКИ */}
            {!isNew && (data.requests || []).length > 0 && (
              <CcSection title="Заявки" count={data.requests.length} delay={180}>
                <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
                  {data.requests.map(r => <CcRequestRow key={r.id} r={r} onOpen={onOpenRequest}/>)}
                </div>
              </CcSection>
            )}

            {/* ДЗВІНКИ */}
            {!isNew && (data.calls || []).length > 0 && (
              <CcSection title="Дзвінки" count={data.calls.length} delay={220}>
                <div style={{ display: "flex", flexDirection: "column" }}>
                  {data.calls.map((c, i) => <CcCallRow key={c.uniqueid || i} c={c}/>)}
                </div>
              </CcSection>
            )}

            {/* ЖУРНАЛ ДІЙ */}
            {!isNew && <CcActivityLog activity={data.activity} delay={260}/>}

            {/* пустий стейт для нового клієнта */}
            {isNew && (
              <div style={{ animation: "ccRise 420ms cubic-bezier(.2,0,0,1) 200ms both", display: "flex", flexDirection: "column",
                alignItems: "center", textAlign: "center", gap: 12, padding: "26px 20px", borderRadius: 16,
                border: "1px dashed var(--border-default)", background: "rgba(255,255,255,0.015)" }}>
                <div style={{ width: 52, height: 52, borderRadius: 14, display: "flex", alignItems: "center", justifyContent: "center",
                  background: ccSoft("var(--accent)", 14), color: "var(--accent)", boxShadow: "0 0 24px " + ccSoft("var(--accent)", 22) }}>
                  <Icon name="sparkles" size={24}/>
                </div>
                <div>
                  <div style={{ fontSize: 15, fontWeight: 600, color: "var(--fg-primary)" }}>Перший контакт</div>
                  <div style={{ fontSize: 12.5, color: "var(--fg-muted)", marginTop: 5, lineHeight: 1.5, maxWidth: 280 }}>
                    Історії ще немає. Створіть заявку, щоб зафіксувати інтерес клієнта.
                  </div>
                </div>
              </div>
            )}
          </div>

          <CcFooter isMobile={isMobile} isNew={isNew} onCreateLead={onCreateLead} onClose={onClose}/>
        </>
      )}
    </div>
  );
}

window.ClientCard = ClientCard;
