// ─────────────────────────────────────────────────────────────
// Shared DM chrome — the header strip that sits inside the phone for
// every Tree variant. Matches the user's original screenshot:
//   ┌─────────────────────────────────────────────────────┐
//   │  [M+]      [ Canvas | Tree | Manage ]               │  ← top row
//   │           ↑ Load older                              │
//   │  [< (M) marina_k    332 nodes · tree         ⊙ ]    │  ← thread card
//   └─────────────────────────────────────────────────────┘
//
// And the bottom-pinned "selected node" preview + Branch CTA that's
// also reused across variants.
// ─────────────────────────────────────────────────────────────


function DMTopBar({ variant }) {
  return (
    <div style={{ padding: '8px 16px 0', flexShrink: 0 }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 8,
                    justifyContent: 'space-between' }}>
        {/* Left: user avatar + add */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
          <div style={{
            width: 32, height: 32, borderRadius: '50%',
            background: '#1F4520', border: '0.5px solid #2C5F2E',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            color: '#F2EDE4', font: '500 13px var(--font-mono)',
            position: 'relative',
          }}>
            M
            <span style={{
              position: 'absolute', bottom: -1, right: -1, width: 9, height: 9,
              borderRadius: '50%', background: '#D4960C',
              boxShadow: '0 0 0 1.5px #07100A',
            }}/>
          </div>
          <div style={{
            width: 32, height: 32, borderRadius: '50%',
            border: '1px dashed #2D3A22',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            color: '#586552', font: '300 16px var(--font-sans)',
          }}>+</div>
        </div>

        {/* Right: view toggle */}
        <ViewToggle variant={variant}/>
      </div>

      {/* Load older */}
      <div style={{ display: 'flex', justifyContent: 'center', marginTop: 8 }}>
        <button style={{
          padding: '5px 14px', cursor: 'pointer',
          background: '#0F1A09', border: '0.5px solid #2D3A22', borderRadius: 999,
          font: '500 11.5px var(--font-sans)', color: '#C9D2BB',
          display: 'inline-flex', alignItems: 'center', gap: 6,
        }}>
          <span style={{ font: '500 11px var(--font-sans)' }}>↑</span> Load older
        </button>
      </div>
    </div>
  );
}

function ViewToggle({ variant = 'tree' }) {
  const pill = (id, label) => {
    const active = id === 'tree';
    return (
      <span key={id} style={{
        padding: '6px 14px', borderRadius: 999,
        font: `${active ? 600 : 500} 12.5px var(--font-sans)`,
        background: active ? '#2C5F2E' : 'transparent',
        color: active ? '#F2EDE4' : '#586552',
        border: active ? '0.5px solid #4F8E51' : '0.5px solid transparent',
        whiteSpace: 'nowrap',
      }}>{label}</span>
    );
  };
  return (
    <div style={{
      display: 'inline-flex', padding: 3, gap: 2,
      background: '#0F1A09', border: '0.5px solid #2D3A22', borderRadius: 999,
    }}>
      {pill('canvas', 'Canvas')}
      {pill('tree',   'Tree')}
      {pill('manage', 'Manage')}
    </div>
  );
}

// The peer header — variant-tagged in italic serif at top-right so it's
// easy for the user to keep track of which artboard is which.
function DMHeaderCard({ subtitle = '332 nodes · tree', variantTag = '' }) {
  return (
    <div style={{
      margin: '10px 12px 8px', padding: '10px 12px',
      background: '#0F1A09', border: '0.5px solid #2D3A22', borderRadius: 14,
      display: 'flex', alignItems: 'center', gap: 10,
      flexShrink: 0, position: 'relative',
    }}>
      <button style={{
        background: 'transparent', border: 'none', cursor: 'pointer',
        color: '#C9D2BB', padding: 4, display: 'flex',
      }} aria-label="Back">
        <FLIcons.ChevronLeft size={18} color="#C9D2BB"/>
      </button>
      <div style={{
        width: 34, height: 34, borderRadius: '50%',
        background: '#1F4520', border: '0.5px solid #2C5F2E',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        color: '#F2EDE4', font: '500 13px var(--font-mono)',
      }}>M</div>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ font: '500 15.5px var(--font-mono)', color: '#F2EDE4',
                      letterSpacing: '-0.01em' }}>
          marina_k
        </div>
        <div style={{ font: 'italic 400 11.5px var(--font-serif)', color: '#8A9678',
                      marginTop: 1 }}>
          {subtitle}
        </div>
      </div>
      {/* target / aim glyph — kept from original; means "scroll Canvas to here" */}
      <div style={{
        width: 28, height: 28, display: 'flex', alignItems: 'center',
        justifyContent: 'center',
      }}>
        <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
          <circle cx="10" cy="10" r="8" stroke="#586552" strokeWidth="1.2"/>
          <circle cx="10" cy="10" r="3.5" stroke="#586552" strokeWidth="1.2"/>
          <circle cx="10" cy="10" r="1.5" fill="#D4960C"/>
        </svg>
      </div>

      {variantTag && (
        <span style={{
          position: 'absolute', top: -8, right: 14,
          padding: '2px 8px', borderRadius: 999,
          background: '#07100A', border: '0.5px solid #2D3A22',
          font: '500 9px var(--font-sans)', color: '#F4DCA0',
          letterSpacing: '0.14em', textTransform: 'uppercase',
        }}>{variantTag}</span>
      )}
    </div>
  );
}

// Bottom-pinned "selected node" preview + Branch CTA.
// Almost every variant pins this to the bottom so the user can read the
// chosen root in place. Tap a root in the tree → this populates.
function SelectedNodePanel({ node }) {
  if (!node) return null;
  return (
    <div style={{
      padding: '12px 14px 16px',
      background: 'linear-gradient(180deg, rgba(7,16,10,0) 0%, #07100A 22%)',
      flexShrink: 0,
    }}>
      <div style={{
        padding: '12px 14px',
        background: '#0F1A09', border: '0.5px solid #2D3A22',
        borderRadius: 14,
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8,
                      marginBottom: 6, flexWrap: 'wrap' }}>
          <span style={{
            width: 8, height: 8, borderRadius: '50%',
            background: FROM_COLORS[node.from] || '#76AB78',
            display: 'inline-block',
          }}/>
          <span style={{ font: '500 11px var(--font-mono)', color: '#F2EDE4',
                         letterSpacing: '0.04em' }}>
            NODE_{node.id.toUpperCase().padStart(6, 'C')}
          </span>
          <span style={{ marginLeft: 'auto',
                         font: '400 10.5px var(--font-mono)', color: '#586552' }}>
            d:{node.depth} · b:{node.branches} · l:{node.leaves}
          </span>
        </div>
        <div style={{ font: 'italic 400 14px/1.45 var(--font-serif)',
                      color: '#F2EDE4', marginBottom: 4 }}>
          {node.body}
        </div>
        <div style={{ font: '400 11px var(--font-sans)', color: '#8A9678' }}>
          {node.from === 'you' ? 'you' : 'marina_k'} · {node.age}
        </div>
      </div>
      <button style={{
        width: '100%', marginTop: 8,
        padding: '12px 14px', cursor: 'pointer',
        background: '#2C5F2E', border: '0.5px solid #4F8E51', borderRadius: 12,
        font: '500 13.5px var(--font-sans)', color: '#F2EDE4',
        display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      }}>
        <FLIcons.Branch size={14} color="#F2EDE4"/> Branch
      </button>
    </div>
  );
}

// Tiny relative-time-to-thickness helper — keeps a consistent feel of
// "older = thinner / dimmer" across variants.
function ageWeight(age) {
  if (age.endsWith('h')) return 1;
  const v = parseInt(age, 10);
  if (age.endsWith('d')) return v <= 2 ? 0.95 : 0.85;
  if (age.endsWith('w')) {
    if (v <= 2) return 0.7;
    if (v <= 6) return 0.5;
    return 0.32;
  }
  return 0.32;
}

// Small "from" dot — green = you, dew = them. Mirrors the bubble bg colors
// in Canvas view so the user can tell at-a-glance who started each thread.
function FromDot({ from, size = 7 }) {
  return (
    <span style={{
      display: 'inline-block', width: size, height: size, borderRadius: '50%',
      background: from === 'you' ? '#4F8E51' : '#A8C5A0',
      flexShrink: 0,
    }}/>
  );
}

Object.assign(window, { DMTopBar, ViewToggle, DMHeaderCard, SelectedNodePanel,
                        ageWeight, FromDot });
