/* global React, ReactDOM, ANATOMY */
const { useState, useEffect, useMemo, useRef } = React;
const { PARTS } = ANATOMY;

// ---------- Injected styles --------------------------------------------------

const CSS = `
.app-shell {
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  transition: background-color 240ms ease, color 240ms ease;
}

.nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 40px;
  border-bottom: 1px solid var(--border);
}
.nav__brand {
  display: flex; align-items: center; gap: 12px;
  font-family: var(--font-mono);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--ink-secondary);
}
.nav__brand-mark {
  width: 22px; height: 22px; border-radius: 5px;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 14px; font-weight: 600;
  font-family: var(--font-body);
}
.nav__links { display: flex; gap: 28px; }
.nav__link {
  font-size: 14px; color: var(--ink-secondary);
  text-decoration: none; font-weight: 500;
}
.nav__link.is-active { color: var(--ink); }

.hero {
  max-width: 1240px; margin: 0 auto;
  padding: 56px 40px 24px;
  width: 100%;
}
.hero__kicker {
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--ink-muted);
  margin-bottom: 18px;
  display: flex; align-items: center; gap: 10px;
}
.hero__kicker-dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
}
.hero__title {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(44px, 6vw, 84px);
  line-height: 0.98;
  letter-spacing: -0.02em;
  color: var(--ink);
  margin-bottom: 24px;
}
.hero__title em {
  font-style: italic;
  color: var(--accent);
}
.hero__lede {
  font-size: 18px;
  line-height: 1.55;
  color: var(--ink-secondary);
  max-width: 620px;
  text-wrap: pretty;
}
.hero__lede a {
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-color: var(--border-strong);
}

.stage {
  max-width: 1240px;
  margin: 20px auto 0;
  padding: 0 40px;
  width: 100%;
  position: relative;
}
.stage__frame {
  position: relative;
  height: min(640px, 70vh);
  border-radius: 16px;
  background: var(--bg-canvas);
  overflow: hidden;
  border: 1px solid var(--border);
  transition: background-color 240ms ease;
}
.stage__grid {
  position: absolute; inset: 0;
  background-image:
    linear-gradient(to right, rgba(255,255,255,0.025) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255,255,255,0.025) 1px, transparent 1px);
  background-size: 40px 40px;
  pointer-events: none;
  mask-image: radial-gradient(ellipse at center, black 30%, transparent 80%);
}
.scene-wrap {
  position: absolute; inset: 0;
}
.scene-canvas { width: 100%; height: 100%; }
.scene-labels {
  position: absolute; inset: 0;
  pointer-events: none;
}
.scene-label {
  position: absolute;
  transform: translate(12px, -50%);
  pointer-events: none;
  transition: opacity 120ms ease;
}
.scene-label__chip {
  font-family: var(--font-mono);
  font-size: 11px;
  background: #FFFFFF;
  color: #1A1A18;
  padding: 5px 9px;
  border-radius: 4px;
  border: 1px solid #D8D7D2;
  white-space: nowrap;
  box-shadow: 0 2px 6px rgba(0,0,0,0.18);
}
.scene-label.is-active .scene-label__chip {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.scene-label__leader {
  position: absolute;
  right: 100%; top: 50%;
  width: 14px; height: 1px;
  background: #8A8A82;
  margin-right: 2px;
}

.stage__corner {
  position: absolute;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(255, 248, 240, 0.5);
  pointer-events: none;
}
.stage__corner--tl { top: 16px; left: 20px; }
.stage__corner--tr { top: 16px; right: 20px; }
.stage__corner--br { bottom: 16px; right: 20px; }
.stage__corner--bl { bottom: 16px; left: 20px; }

.empty-hint {
  position: absolute;
  left: 50%; bottom: 28px;
  transform: translateX(-50%);
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: rgba(255, 248, 240, 0.6);
  pointer-events: none;
  display: flex; align-items: center; gap: 10px;
}
.empty-hint__dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 0.3; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.4); }
}

.rail {
  max-width: 1240px;
  margin: 24px auto 0;
  padding: 0 40px;
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.rail__label {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-muted);
  margin-right: 4px;
}
.pill {
  font-size: 13px;
  font-weight: 500;
  padding: 8px 14px;
  border-radius: 100px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--ink-secondary);
  cursor: pointer;
  transition: all 160ms ease;
  font-family: var(--font-body);
  display: inline-flex; align-items: center; gap: 8px;
}
.pill:hover {
  border-color: var(--accent);
  color: var(--accent);
}
.pill.is-active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}
.pill__num {
  font-family: var(--font-mono);
  font-size: 10px;
  opacity: 0.5;
}
.pill.is-active .pill__num { opacity: 0.8; }

.footer-note {
  max-width: 1240px;
  margin: 48px auto 40px;
  padding: 28px 40px 0;
  border-top: 1px solid var(--border);
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 32px;
  flex-wrap: wrap;
}
.footer-note__lede {
  font-family: var(--font-display);
  font-size: 22px;
  line-height: 1.35;
  color: var(--ink);
  max-width: 560px;
  font-style: italic;
  text-wrap: pretty;
}
.footer-note__meta {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-muted);
  display: flex; flex-direction: column; gap: 6px;
  text-align: right;
}
.footer-note__meta a { color: var(--ink); text-decoration: none; border-bottom: 1px solid var(--border-strong); }

/* ---------- Side panel ---------- */
.panel-backdrop {
  position: fixed; inset: 0;
  background: rgba(20, 20, 18, 0);
  pointer-events: none;
  transition: background-color 240ms ease;
  z-index: 40;
}
.panel-backdrop.is-open {
  background: rgba(20, 20, 18, 0.28);
  pointer-events: auto;
}
.panel {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(460px, 92vw);
  background: var(--bg);
  border-left: 1px solid var(--border);
  padding: 28px 32px;
  transform: translateX(100%);
  transition: transform 420ms cubic-bezier(.2,.8,.2,1);
  z-index: 50;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.panel.is-open { transform: translateX(0); }

.panel__close {
  position: absolute;
  top: 18px; right: 18px;
  width: 30px; height: 30px;
  border: none; background: transparent;
  font-size: 22px; cursor: pointer;
  color: var(--ink-muted);
  border-radius: 6px;
  line-height: 1;
}
.panel__close:hover { background: var(--surface-inset); color: var(--ink); }

.panel__category {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-muted);
  display: flex; align-items: center; gap: 10px;
}
.panel__category-dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
}

.panel__title {
  font-family: var(--font-display);
  font-size: 48px;
  line-height: 1;
  letter-spacing: -0.02em;
  color: var(--ink);
  margin-top: 4px;
}

.panel__spec {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--ink-secondary);
  line-height: 1.6;
}
.panel__spec-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
}
.panel__spec-row + .panel__spec-row { border-top: 1px dashed var(--border); }
.panel__spec-key { color: var(--ink-muted); letter-spacing: 0.05em; text-transform: uppercase; }
.panel__spec-val { color: var(--ink); }

.panel__desc {
  font-size: 15px;
  line-height: 1.6;
  color: var(--ink-secondary);
  text-wrap: pretty;
}

.panel__checklist-quote {
  border-left: 2px solid var(--accent);
  padding: 8px 0 8px 14px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--ink);
  line-height: 1.55;
}
.panel__checklist-quote-tag {
  display: block;
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-muted);
  margin-bottom: 6px;
}

.panel__danger {
  background: #FDF3EB;
  border: 1px solid #F1C9A8;
  padding: 12px 14px;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.5;
  color: #7A3A14;
  display: flex;
  gap: 10px;
}
[data-theme="dark"] .panel__danger {
  background: #3A2218;
  border-color: #5A3422;
  color: #F1C9A8;
}
.panel__danger-icon {
  font-family: var(--font-mono);
  font-size: 11px;
  flex-shrink: 0;
}

.panel__cta {
  margin-top: 8px;
  display: flex;
  gap: 10px;
  flex-direction: column;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 14px 18px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: 1px solid transparent;
  font-family: var(--font-body);
  transition: all 160ms ease;
  text-decoration: none;
}
.btn--primary {
  background: var(--accent);
  color: #fff;
}
.btn--primary:hover { background: var(--accent-hover); }
.btn--ghost {
  background: transparent;
  color: var(--ink);
  border-color: var(--border-strong);
}
.btn--ghost:hover { background: var(--surface-inset); }
.btn__arrow { font-size: 14px; transition: transform 160ms ease; }
.btn:hover .btn__arrow { transform: translateX(3px); }

/* ---------- Toast (simulated handoff) ---------- */
.toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translate(-50%, 20px);
  background: #1A1A18;
  color: #EEEDE9;
  padding: 14px 18px 14px 16px;
  border-radius: 12px;
  font-size: 13px;
  display: flex; align-items: center; gap: 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.24);
  opacity: 0;
  pointer-events: none;
  transition: transform 300ms ease, opacity 240ms ease;
  z-index: 100;
  max-width: 92vw;
}
.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
  pointer-events: auto;
}
.toast__icon {
  width: 24px; height: 24px; border-radius: 6px;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-mono);
  font-size: 12px;
  color: #fff;
  flex-shrink: 0;
}
.toast__body { display: flex; flex-direction: column; gap: 2px; }
.toast__title { font-weight: 500; }
.toast__prompt {
  font-family: var(--font-mono);
  font-size: 11px;
  color: rgba(238, 237, 233, 0.6);
  max-width: 480px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- Tweaks panel (bottom-right) ---------- */
.tweaks-panel {
  position: fixed;
  bottom: 24px;
  right: 24px;
  width: 260px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0;
  box-shadow: 0 16px 36px rgba(0,0,0,0.18);
  z-index: 60;
  font-family: var(--font-body);
  overflow: hidden;
}
.tweaks-panel__head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-inset);
}
.tweaks-panel__title {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink);
}
.tweaks-panel__close {
  border: none; background: transparent;
  font-size: 18px; line-height: 1;
  color: var(--ink-muted);
  cursor: pointer;
  width: 22px; height: 22px; border-radius: 5px;
}
.tweaks-panel__close:hover { background: var(--border); color: var(--ink); }
.tweaks-panel__body {
  padding: 14px;
  display: flex; flex-direction: column; gap: 12px;
}
.tweaks-panel__foot {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
}
.tweak-row {
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px;
}
.tweak-row__label {
  font-size: 12px;
  color: var(--ink-secondary);
  font-weight: 500;
}
.mono-meta {
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-muted);
}

.toggle {
  width: 36px; height: 20px;
  border-radius: 20px;
  background: var(--border-strong);
  border: none;
  position: relative;
  cursor: pointer;
  padding: 0;
  transition: background 180ms ease;
}
.toggle.is-on { background: var(--accent); }
.toggle__knob {
  position: absolute;
  top: 2px; left: 2px;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform 180ms cubic-bezier(.2,.8,.2,1);
}
.toggle.is-on .toggle__knob { transform: translateX(16px); }

.segmented {
  display: inline-flex;
  background: var(--surface-inset);
  border-radius: 6px;
  padding: 2px;
}
.segmented__opt {
  border: none;
  background: transparent;
  padding: 4px 9px;
  font-size: 11px;
  font-family: var(--font-body);
  font-weight: 500;
  color: var(--ink-secondary);
  border-radius: 4px;
  cursor: pointer;
  transition: all 160ms ease;
}
.segmented__opt.is-active {
  background: var(--bg);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}

@media (max-width: 768px) {
  .nav { padding: 16px 20px; }
  .hero { padding: 36px 20px 16px; }
  .stage { padding: 0 20px; }
  .rail { padding: 0 20px; }
  .footer-note { padding: 28px 20px 0; margin-top: 36px; }
  .tweaks-panel { right: 12px; bottom: 12px; width: 240px; }
  .panel { padding: 24px 22px; }
  .panel__title { font-size: 38px; }
  .hero__title { font-size: 44px; }
}
`;

// ---------- App -------------------------------------------------------------


// ---- Inject CSS + mount ---------------------------------------------------

const styleEl = document.createElement('style');
styleEl.textContent = CSS;
document.head.appendChild(styleEl);

// Static render config. Original prototype shipped with a dev "tweaks" panel
// for toggling explode/labels/camera; stripped for v1. Hardcoded to the
// 80/20 defaults from docs/superpowers/specs/2026-04-21-3d-anatomy-companion-design.md.
const TWEAKS = Object.freeze({
  exploded: false,
  showLabels: 'hover',
  cameraPreset: 'three-quarter',
  theme: 'light',
});

function AppRoot() {
  const [selectedId, setSelectedId] = useState(null);
  const [hoveredId, setHoveredId] = useState(null);

  const selectedPart = useMemo(() => PARTS.find((p) => p.id === selectedId), [selectedId]);

  useEffect(() => {
    const onKey = (ev) => {
      if (ev.key === 'Escape') setSelectedId(null);
    };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, []);

  // Hand off to the homeowner-facing chat with the part's prompt pre-loaded.
  // We're iframed from /labs/[slug] — navigate the parent window so the user
  // lands on the site's chat UI, not a nested iframe.
  const handleAskAboutPart = (part) => {
    const url = '/?prompt=' + encodeURIComponent(part.chatPrompt);
    try {
      (window.top || window.parent || window).location.href = url;
    } catch {
      window.location.href = url;
    }
  };

  return (
    <div className="app-shell">
      <div className="hero">
        <div className="hero__kicker">
          <span className="hero__kicker-dot" />
          Inspection · 3D Companion · Lab
        </div>
        <h1 className="hero__title">
          Know the parts<br />
          <em>before</em> something breaks.
        </h1>
        <p className="hero__lede">
          Six parts of a residential garage door drive the majority of service calls —
          and a couple of them can injure you. Click any part to ask the virtual
          technician what it does, how it wears, and when to stop and call a pro.
          Pairs with the <a href="/inspection" target="_top">Inspection Tool</a>.
        </p>
      </div>

      <div className="stage">
        <div className="stage__frame">
          <div className="stage__grid" />
          <div className="stage__corner stage__corner--tl">ANATOMY-01 · 3/4 VIEW</div>
          <div className="stage__corner stage__corner--tr">REV. 2026.04</div>
          <div className="stage__corner stage__corner--bl">SECTIONAL · RESIDENTIAL</div>
          <div className="stage__corner stage__corner--br">SCALE: NTS</div>

          <window.DoorScene
            selectedId={selectedId}
            hoveredId={hoveredId}
            onHover={setHoveredId}
            onSelect={setSelectedId}
            exploded={TWEAKS.exploded}
            showLabels={TWEAKS.showLabels}
            cameraPreset={TWEAKS.cameraPreset}
            theme={TWEAKS.theme}
          />

          {!selectedId && !hoveredId && (
            <div className="empty-hint">
              <span className="empty-hint__dot" />
              <span>Hover a part · click to inspect</span>
            </div>
          )}
        </div>
      </div>

      <div className="rail">
        <span className="rail__label">Parts</span>
        {PARTS.map((p, i) => (
          <button
            key={p.id}
            className={`pill${selectedId === p.id ? ' is-active' : ''}`}
            onMouseEnter={() => setHoveredId(p.id)}
            onMouseLeave={() => setHoveredId(null)}
            onClick={() => setSelectedId(p.id)}
          >
            <span className="pill__num">{String(i + 1).padStart(2, '0')}</span>
            {p.shortLabel}
          </button>
        ))}
      </div>

      <div className="footer-note">
        <div className="footer-note__lede">
          “Technical service manual, made three-dimensional. Hover to orient,
          click to understand, close and run your inspection.”
        </div>
        <div className="footer-note__meta">
          <span>Companion to</span>
          <a href="/inspection" target="_top">garagedoorscience.com/inspection</a>
          <span style={{ marginTop: 10 }}>© 2026</span>
        </div>
      </div>

      <div
        className={`panel-backdrop${selectedPart ? ' is-open' : ''}`}
        onClick={() => setSelectedId(null)}
      />
      <aside className={`panel${selectedPart ? ' is-open' : ''}`}>
        {selectedPart && (
          <>
            <button className="panel__close" onClick={() => setSelectedId(null)} aria-label="Close">×</button>
            <div className="panel__category">
              <span className="panel__category-dot" />
              {selectedPart.category}
            </div>
            <h2 className="panel__title">{selectedPart.label}</h2>

            <div className="panel__spec">
              <div className="panel__spec-row">
                <span className="panel__spec-key">Part ID</span>
                <span className="panel__spec-val">{selectedPart.id}</span>
              </div>
              <div className="panel__spec-row">
                <span className="panel__spec-key">Section</span>
                <span className="panel__spec-val">{selectedPart.category}</span>
              </div>
              <div className="panel__spec-row">
                <span className="panel__spec-key">Inspect</span>
                <span className="panel__spec-val">Visual + manual</span>
              </div>
            </div>

            <div className="panel__checklist-quote">
              <span className="panel__checklist-quote-tag">From the inspection checklist</span>
              {selectedPart.checklistCopy}
            </div>

            <p className="panel__desc">{selectedPart.detail}</p>

            {selectedPart.dangerNote && (
              <div className="panel__danger">
                <span className="panel__danger-icon">⚠</span>
                <span>{selectedPart.dangerNote}</span>
              </div>
            )}

            <div className="panel__cta">
              <button className="btn btn--primary" onClick={() => handleAskAboutPart(selectedPart)}>
                Ask the virtual technician
                <span className="btn__arrow">→</span>
              </button>
              <a
                className="btn btn--ghost"
                href={`/inspection#${selectedPart.id}`}
                target="_top"
              >
                Jump to checklist item
              </a>
            </div>
          </>
        )}
      </aside>

    </div>
  );
}

ReactDOM.createRoot(document.getElementById('root')).render(<AppRoot />);
