/* Problem, chaotic stack diorama showing fragmented tooling */

const ProblemMiniChannelChip = ({ label, dotColor }) => (
  <span className="pcm-chip">
    <span className="pcm-chip-dot" style={{ background: dotColor }} />
    {label}
  </span>
);

/* Each card is a tiny interface fragment, distinct content per card */
const PROBLEM_CARDS = [
  {
    id: "wa",
    label: "WhatsApp tool",
    sub: "Templates · broadcasts",
    rotate: -3,
    x: 0, y: 8, w: 280,
    z: 3,
    body: (
      <div className="pcm-thread">
        <div className="pcm-msg pcm-msg-in">Hi, what time is check-in?</div>
        <div className="pcm-msg pcm-msg-out pcm-msg-template">[ Template: check_in_v2 ]</div>
        <div className="pcm-msg pcm-msg-in">…and breakfast hours?</div>
        <div className="pcm-msg-pending">No template found</div>
      </div>
    ),
  },
  {
    id: "bot",
    label: "Chatbot",
    sub: "Decision trees · web only",
    rotate: 2.5,
    x: 22, y: 0, w: 260,
    z: 4,
    body: (
      <div className="pcm-tree">
        <div className="pcm-tree-q">› Are you a guest?</div>
        <div className="pcm-tree-opts">
          <span>Yes</span><span>No</span><span className="pcm-dim">Other</span>
        </div>
        <div className="pcm-tree-q pcm-dim-line">› Booking ref?</div>
        <div className="pcm-tree-leaf">⊗ flow ended</div>
      </div>
    ),
  },
  {
    id: "crm",
    label: "Hotel CRM",
    sub: "Email-only · contacts",
    rotate: -1.5,
    x: 6, y: 14, w: 280,
    z: 2,
    body: (
      <div className="pcm-table">
        <div className="pcm-table-row pcm-table-head">
          <span>Name</span><span>Email</span><span>Stays</span>
        </div>
        <div className="pcm-table-row"><span>M. Aguilar</span><span>m.a@…</span><span>4</span></div>
        <div className="pcm-table-row pcm-table-dup"><span>M. Aguilar</span><span>marta@…</span><span>,</span></div>
        <div className="pcm-table-row pcm-dim"><span>,</span><span>?</span><span>?</span></div>
      </div>
    ),
  },
  {
    id: "call",
    label: "Call center",
    sub: "Manual handoffs",
    rotate: 3.2,
    x: 18, y: 6, w: 270,
    z: 5,
    body: (
      <div className="pcm-call">
        <div className="pcm-call-status">
          <span className="pcm-call-dot" /> 4 in queue · avg 6m 42s
        </div>
        <div className="pcm-wave">
          {Array.from({length: 24}).map((_,i)=>(
            <span key={i} style={{height: `${20 + Math.abs(Math.sin(i*0.7))*60}%`}}/>
          ))}
        </div>
        <div className="pcm-call-note">No CRM context loaded</div>
      </div>
    ),
  },
  {
    id: "email",
    label: "Email platform",
    sub: "Marketing automation",
    rotate: -2.5,
    x: 4, y: 4, w: 280,
    z: 3,
    body: (
      <div className="pcm-email">
        <div className="pcm-email-row"><span className="pcm-email-tag">Campaign</span><span>Spring offer · 12,400</span></div>
        <div className="pcm-email-row"><span className="pcm-email-tag">Open rate</span><span>14.2%</span></div>
        <div className="pcm-email-row pcm-bad"><span className="pcm-email-tag">Bounces</span><span>1,802</span></div>
        <div className="pcm-email-row pcm-dim"><span className="pcm-email-tag">Booked</span><span>?</span></div>
      </div>
    ),
  },
  {
    id: "loy",
    label: "Loyalty system",
    sub: "Standalone tier rules",
    rotate: 1.8,
    x: 14, y: 12, w: 260,
    z: 4,
    body: (
      <div className="pcm-loy">
        <div className="pcm-loy-card">
          <span className="pcm-loy-tier">GOLD</span>
          <span className="pcm-loy-pts">8,420 pts</span>
        </div>
        <div className="pcm-loy-rule">if nights ≥ 6 → +5%</div>
        <div className="pcm-loy-rule pcm-dim">if pms.guest = ?</div>
      </div>
    ),
  },
  {
    id: "survey",
    label: "Survey tool",
    sub: "Post-stay forms",
    rotate: -3.5,
    x: 24, y: 2, w: 260,
    z: 2,
    body: (
      <div className="pcm-survey">
        <div className="pcm-survey-q">Rate your stay</div>
        <div className="pcm-stars">★★★★☆</div>
        <div className="pcm-survey-q pcm-dim">Comments…</div>
        <div className="pcm-survey-meta">Sent 14d after checkout</div>
      </div>
    ),
  },
  {
    id: "an",
    label: "Analytics",
    sub: "Disconnected dashboards",
    rotate: 2,
    x: 8, y: 10, w: 280,
    z: 3,
    body: (
      <div className="pcm-an">
        <div className="pcm-an-grid">
          <div className="pcm-an-tile"><span>Sessions</span><b>12.4k</b></div>
          <div className="pcm-an-tile"><span>Conversion</span><b className="pcm-bad-text">,</b></div>
          <div className="pcm-an-tile pcm-dim"><span>Source: ?</span><b>?</b></div>
          <div className="pcm-an-tile"><span>Revenue</span><b>,</b></div>
        </div>
      </div>
    ),
  },
];

const SectionProblem = () => {
  const wrapRef = React.useRef(null);
  const [drift, setDrift] = React.useState(0);

  React.useEffect(() => {
    // Scroll-linked parallax is the effect people with vestibular
    // disorders report most, and this one moved eight cards at once with
    // no opt-out. Leave drift at 0 and the cards simply sit still; the
    // diorama still says everything it needs to say.
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;

    const onScroll = () => {
      if (!wrapRef.current) return;
      const rect = wrapRef.current.getBoundingClientRect();
      const vh = window.innerHeight;
      const progress = (vh - rect.top) / (vh + rect.height);
      setDrift(Math.max(0, Math.min(1, progress)));
    };
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  return (
    <section className="section-pad problem" id="problem">
      <div className="container-narrow">
        <Reveal><div className="eyebrow" style={{ marginBottom: 28 }}>The state of guest communication</div></Reveal>
        <Reveal delay={120}>
          <h2 className="h-1" style={{ fontSize: "clamp(36px, 4.6vw, 64px)", marginBottom: 40 }}>
            Hotels don't lack tools. They lack a <span className="italic-accent">system.</span>
          </h2>
        </Reveal>
        <Reveal delay={220}>
          <p className="lead" style={{ fontSize: 19, marginBottom: 18 }}>
            Your guests message you everywhere, WhatsApp, Instagram, web chat, email, the phone. Your data lives somewhere else, PMS, booking engine, CRM, spreadsheets, the front desk. Your team sits in the middle, answering the same questions a hundred times a day, missing upsells, and reacting instead of driving revenue.
          </p>
        </Reveal>
        <Reveal delay={300}>
          <p className="lead" style={{ fontSize: 19, marginBottom: 64 }}>
            The result is predictable: slow responses, fragmented guest experience, lost direct bookings, and a CRM that nobody trusts.
          </p>
        </Reveal>
      </div>

      {/* Diorama */}
      <div className="container">
        <Reveal delay={400}>
          <div className="pcm-diorama" ref={wrapRef}>
            {/* Floating warning labels */}
            <div className="pcm-tag pcm-tag-1"><span className="pcm-tag-dot"/>Same guest · 3 systems</div>
            <div className="pcm-tag pcm-tag-2"><span className="pcm-tag-dot"/>No shared context</div>
            <div className="pcm-tag pcm-tag-3"><span className="pcm-tag-dot"/>No revenue attribution</div>

            {/* Severed connection threads between cards */}
            <svg className="pcm-threads" viewBox="0 0 1200 700" preserveAspectRatio="none" aria-hidden="true">
              <defs>
                <pattern id="pcm-dash" patternUnits="userSpaceOnUse" width="6" height="1">
                  <rect width="3" height="1" fill="var(--ink-4)"/>
                </pattern>
              </defs>
              <path d="M180 180 Q 320 100 460 180" stroke="var(--ink-4)" strokeWidth="1" fill="none" strokeDasharray="3 5" opacity=".4"/>
              <path d="M460 180 Q 580 280 720 200" stroke="var(--ink-4)" strokeWidth="1" fill="none" strokeDasharray="3 5" opacity=".3"/>
              <path d="M720 200 Q 880 320 1020 220" stroke="var(--ink-4)" strokeWidth="1" fill="none" strokeDasharray="3 5" opacity=".4"/>
              <path d="M180 460 Q 360 540 540 460" stroke="var(--ink-4)" strokeWidth="1" fill="none" strokeDasharray="3 5" opacity=".3"/>
              <path d="M540 460 Q 760 380 940 480" stroke="var(--ink-4)" strokeWidth="1" fill="none" strokeDasharray="3 5" opacity=".4"/>
              {/* Severed marker */}
              <g transform="translate(600, 360)">
                <circle r="8" fill="var(--bg)" stroke="var(--accent)" strokeWidth="1"/>
                <path d="M-3 -3 L3 3 M-3 3 L3 -3" stroke="var(--accent)" strokeWidth="1.5" strokeLinecap="round"/>
              </g>
            </svg>

            {PROBLEM_CARDS.map((c, i) => {
              const driftY = drift * 30 * (i % 2 === 0 ? 1 : -1);
              const driftX = drift * 16 * (i % 3 === 0 ? 1 : -1);
              return (
                <div
                  key={c.id}
                  className="pcm-card"
                  style={{
                    "--rot": `${c.rotate}deg`,
                    "--w": `${c.w}px`,
                    "--z": c.z,
                    transform: `translate(${c.x + driftX}px, ${c.y + driftY}px) rotate(${c.rotate}deg)`,
                  }}
                >
                  <div className="pcm-card-head">
                    <div>
                      <div className="pcm-card-name">{c.label}</div>
                      <div className="pcm-card-sub">{c.sub}</div>
                    </div>
                    <div className="pcm-card-x" aria-hidden="true">
                      <svg width="12" height="12" viewBox="0 0 12 12" fill="none">
                        <circle cx="6" cy="6" r="5" stroke="currentColor" strokeWidth="1"/>
                        <path d="M3.5 3.5 L8.5 8.5 M8.5 3.5 L3.5 8.5" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round"/>
                      </svg>
                    </div>
                  </div>
                  <div className="pcm-card-body">{c.body}</div>
                </div>
              );
            })}
          </div>
        </Reveal>
      </div>

      <div className="container-narrow">
        <Reveal delay={500}>
          <p className="problem-verdict">
            Modern guest communication isn't a tooling problem.<br />
            It's a <span className="italic-accent">system</span> problem.
          </p>
        </Reveal>
      </div>

      <style>{`
        .problem { background: var(--bg); overflow: hidden; }
        .problem-verdict {
          font-family: var(--f-display);
          font-size: clamp(34px, 4.4vw, 60px);
          line-height: 1.1;
          letter-spacing: -0.02em;
          margin: 80px 0 0;
          text-wrap: balance;
        }

        /* === Diorama === */
        .pcm-diorama {
          position: relative;
          display: grid;
          grid-template-columns: repeat(4, 1fr);
          grid-auto-rows: 1fr;
          align-items: stretch;
          gap: 28px 32px;
          padding: 80px 20px 100px;
          margin: 16px 0 24px;
        }
        .pcm-card { display: flex; flex-direction: column; height: 100%; }
        .pcm-card-body { flex: 1; display: flex; flex-direction: column; min-height: 100px; }
        .pcm-threads {
          position: absolute; inset: 0;
          width: 100%; height: 100%;
          pointer-events: none;
          z-index: 0;
        }

        /* Floating tags */
        .pcm-tag {
          position: absolute;
          display: inline-flex; align-items: center; gap: 7px;
          padding: 6px 12px;
          background: var(--bg-elev-1);
          border: 1px solid color-mix(in srgb, var(--accent) 30%, var(--hairline-strong));
          border-radius: 999px;
          font-family: var(--f-mono);
          font-size: 10.5px;
          letter-spacing: 0.06em;
          text-transform: uppercase;
          color: var(--accent);
          z-index: 6;
          box-shadow: 0 8px 20px -8px rgba(0,0,0,.4);
        }
        /* NOTE: no backticks in this block — the whole stylesheet is a JS
           template literal, so one would end the string and take the
           section down with it.

           These dots animated on a keyframe named "pulse" which THIS FILE
           never defines. They only moved because section-inbox.jsx happens
           to declare @keyframes pulse, and every one of these style blocks
           is global. Rename that one, or drop that section, and these
           silently stop. Own the keyframes here.

           Held still until the diorama is hovered, like every other
           animation on the page. Three dots blinking forever under a
           section nobody is looking at is exactly the restlessness we
           removed from the cards above. */
        @keyframes pcmTagPulse {
          0%, 100% { opacity: 1; }
          50% { opacity: 0.3; }
        }
        .pcm-tag-dot {
          width: 6px; height: 6px; border-radius: 50%;
          background: var(--accent);
          box-shadow: 0 0 6px var(--accent);
        }
        .pcm-diorama:hover .pcm-tag-dot,
        .pcm-diorama:focus-within .pcm-tag-dot {
          animation: pcmTagPulse 1.8s ease-in-out infinite;
        }
        .pcm-tag-1 { top: 4%; left: 22%; transform: rotate(-2deg); }
        .pcm-tag-2 { top: 46%; left: 50%; transform: rotate(1.5deg); }
        .pcm-tag-3 { bottom: 8%; right: 14%; transform: rotate(-1.5deg); }

        /* === Card === */
        .pcm-card {
          position: relative;
          background: var(--bg-elev-1);
          border: 1px solid var(--hairline);
          border-radius: 14px;
          padding: 18px 18px 16px;
          z-index: var(--z, 2);
          box-shadow:
            0 24px 48px -16px rgba(0,0,0,0.55),
            0 1px 0 rgba(245, 241, 234, 0.04) inset;
          transition: transform .6s cubic-bezier(.16,1,.3,1), box-shadow .3s ease, border-color .3s ease;
          will-change: transform;
        }
        .pcm-card:hover {
          z-index: 50;
          box-shadow:
            0 32px 64px -16px rgba(0,0,0,0.7),
            0 0 0 1px var(--hairline-strong);
          transform: translate(0, -4px) rotate(0deg) !important;
          border-color: var(--hairline-strong);
        }
        .pcm-card-head {
          display: flex; justify-content: space-between; align-items: flex-start;
          gap: 10px;
          padding-bottom: 12px;
          border-bottom: 1px dashed var(--hairline);
          margin-bottom: 12px;
        }
        .pcm-card-name {
          font-family: var(--f-display);
          font-size: 17px;
          letter-spacing: -0.005em;
          line-height: 1.1;
          color: var(--ink);
        }
        .pcm-card-sub {
          font-size: 10px;
          color: var(--ink-3);
          font-family: var(--f-mono);
          letter-spacing: 0.06em;
          text-transform: uppercase;
          margin-top: 4px;
        }
        .pcm-card-x {
          color: var(--ink-4);
          flex-shrink: 0;
          margin-top: 2px;
        }
        .pcm-card-body { min-height: 100px; flex: 1; display: flex; flex-direction: column; }

        /* shared bits */
        .pcm-dim { color: var(--ink-4) !important; opacity: 0.55; }
        .pcm-bad-text { color: var(--accent); }

        /* WhatsApp thread */
        .pcm-thread { display: flex; flex-direction: column; gap: 4px; font-size: 10px; }
        .pcm-msg {
          padding: 5px 9px;
          border-radius: 8px;
          max-width: 80%;
          line-height: 1.3;
        }
        .pcm-msg-in { background: var(--bg-elev-2); color: var(--ink-2); align-self: flex-start; border-bottom-left-radius: 2px; }
        .pcm-msg-out { background: color-mix(in srgb, var(--accent) 18%, var(--bg-elev-2)); color: var(--ink); align-self: flex-end; border-bottom-right-radius: 2px; }
        .pcm-msg-template { font-family: var(--f-mono); font-size: 9px; }
        .pcm-msg-pending { font-family: var(--f-mono); font-size: 9px; color: var(--accent); align-self: flex-end; opacity: 0.85; }

        /* Decision tree */
        .pcm-tree { display: flex; flex-direction: column; gap: 6px; font-size: 10.5px; font-family: var(--f-mono); color: var(--ink-2); }
        .pcm-tree-q { letter-spacing: 0.02em; }
        .pcm-tree-opts { display: flex; gap: 4px; padding-left: 12px; }
        .pcm-tree-opts span { padding: 2px 7px; border: 1px solid var(--hairline); border-radius: 4px; font-size: 9px; }
        .pcm-dim-line { opacity: 0.45; }
        .pcm-tree-leaf { color: var(--accent); padding-left: 12px; font-size: 9.5px; }

        /* CRM table */
        .pcm-table { display: flex; flex-direction: column; gap: 0; font-size: 10px; font-family: var(--f-mono); }
        .pcm-table-row {
          display: grid; grid-template-columns: 1fr 1fr 28px;
          gap: 6px; padding: 4px 0;
          border-bottom: 1px dashed var(--hairline);
          color: var(--ink-2);
          align-items: center;
        }
        .pcm-table-head { color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.06em; font-size: 9px; }
        .pcm-table-row span:nth-child(2) { color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
        .pcm-table-row span:last-child { text-align: right; color: var(--ink-3); }
        .pcm-table-dup { background: color-mix(in srgb, var(--accent) 8%, transparent); border-radius: 4px; }

        /* Call */
        .pcm-call { display: flex; flex-direction: column; gap: 8px; }
        .pcm-call-status { display: inline-flex; align-items: center; gap: 6px; font-family: var(--f-mono); font-size: 10px; color: var(--accent); }
        /* Same borrowed-keyframes problem as .pcm-tag-dot above. The live
           call indicator earns its blink, but only while you are looking
           at the card it sits on. */
        .pcm-call-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
        .pcm-card:hover .pcm-call-dot { animation: pcmTagPulse 1.4s ease-in-out infinite; }
        .pcm-wave { display: flex; align-items: flex-end; gap: 2px; height: 36px; }
        .pcm-wave span { flex: 1; background: linear-gradient(180deg, var(--accent), color-mix(in srgb, var(--accent) 40%, transparent)); border-radius: 1px; opacity: 0.7; }
        .pcm-call-note { font-family: var(--f-mono); font-size: 9.5px; color: var(--ink-4); }

        /* Email */
        .pcm-email { display: flex; flex-direction: column; gap: 5px; font-size: 10.5px; }
        .pcm-email-row {
          display: flex; justify-content: space-between; gap: 8px;
          padding: 4px 0;
          border-bottom: 1px dashed var(--hairline);
          color: var(--ink-2);
        }
        .pcm-email-tag {
          font-family: var(--f-mono); font-size: 9px;
          color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.06em;
        }
        .pcm-bad { color: var(--accent); }

        /* Loyalty */
        .pcm-loy { display: flex; flex-direction: column; gap: 8px; }
        .pcm-loy-card {
          display: flex; justify-content: space-between; align-items: center;
          padding: 10px 12px;
          background: linear-gradient(135deg, color-mix(in srgb, var(--accent) 24%, var(--bg-elev-2)), var(--bg-elev-2));
          border: 1px solid color-mix(in srgb, var(--accent) 30%, var(--hairline));
          border-radius: 8px;
        }
        .pcm-loy-tier { font-family: var(--f-mono); font-size: 11px; letter-spacing: 0.12em; color: var(--accent); font-weight: 700; }
        .pcm-loy-pts { font-family: var(--f-display); font-size: 14px; color: var(--ink); }
        .pcm-loy-rule { font-family: var(--f-mono); font-size: 10px; color: var(--ink-2); padding: 2px 0; }

        /* Survey */
        .pcm-survey { display: flex; flex-direction: column; gap: 6px; }
        .pcm-survey-q { font-family: var(--f-mono); font-size: 10px; color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.06em; }
        .pcm-stars { font-size: 18px; color: var(--accent); letter-spacing: 2px; }
        .pcm-survey-meta { font-family: var(--f-mono); font-size: 9.5px; color: var(--ink-4); margin-top: auto; }

        /* Analytics */
        .pcm-an-grid {
          display: grid; grid-template-columns: 1fr 1fr;
          gap: 6px;
        }
        .pcm-an-tile {
          padding: 8px 10px;
          background: var(--bg-elev-2);
          border: 1px solid var(--hairline);
          border-radius: 6px;
          display: flex; flex-direction: column; gap: 2px;
        }
        .pcm-an-tile span { font-family: var(--f-mono); font-size: 9px; color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.06em; }
        /* weight declared, because <b> is bold by default and the display
           face has no bold to give — it would be synthesised. */
        .pcm-an-tile b { font-family: var(--f-display); font-weight: 400; font-size: 16px; color: var(--ink); }

        @media (prefers-reduced-motion: reduce) {
          .pcm-tag-dot, .pcm-call-dot { animation: none !important; }
          .pcm-card { transition: none !important; }
        }

        @media (max-width: 1100px) { .pcm-diorama { grid-template-columns: repeat(2, 1fr); } }
        @media (max-width: 600px)  { .pcm-diorama { grid-template-columns: 1fr; } }
      `}</style>
    </section>
  );
};

window.SectionProblem = SectionProblem;
