// /templates — the discovery page on ustack.ai
// Customer lands here, reads the writeup, scans the 6, clicks one for details.

const TEMPLATES = [
  { id: 'floorplan',   name: 'Floorplan',   tagline: 'The workflow IS the home.',                         bestFor: 'Multi-party handoffs',          demos: 'DrawRequest · InspectKit · AuditField',          accent: 'oklch(0.50 0.18 30)',  bg: '#f6f1e8',  cardKey: 'fp',  fonts: 'IBM Plex Sans + Mono',           tone: 'Diagrammatic · place-like' },
  { id: 'atelier',     name: 'Atelier',     tagline: 'The artifact IS the screen.',                       bestFor: 'Document-centric workflows',     demos: 'VenueFlow · PolicyDesk · GrantCycle',           accent: '#1a3d5c',              bg: '#efe5d2',  cardKey: 'at',  fonts: 'DM Serif Display + Caveat',      tone: 'Craft · physicality' },
  { id: 'controlroom', name: 'Control Room',tagline: 'Operator console for businesses that ship things.',  bestFor: 'Real-time ops · 24/7',           demos: 'DispatchHQ',                                    accent: 'oklch(0.82 0.20 145)', bg: '#0a0c0d',  cardKey: 'cr',  fonts: 'IBM Plex Mono · 48px wall',       tone: 'Bloomberg · NASA · serious', dark: true },
  { id: 'technical',   name: 'Technical',   tagline: 'Engineering drawing · dimensioned · annotated.',     bestFor: 'Engineering · manufacturing',     demos: 'JobShop OS · PunchOps',                         accent: '#0a0a0a',              bg: '#fafaf6',  cardKey: 'tech',fonts: 'Helvetica Neue + Plex Mono',     tone: 'Drafted · precise' },
  { id: 'editorial',   name: 'Editorial',   tagline: 'The dashboard reads like a quarterly.',              bestFor: 'Advisory · professional services',demos: 'ClientHub · RFPRoom · CampaignGate',            accent: '#9a2828',              bg: '#fbf8f1',  cardKey: 'ed',  fonts: 'DM Serif Display + Plex Sans',   tone: 'Prestige · slow' },
  { id: 'pastoral',    name: 'Pastoral',    tagline: '"Good afternoon. Five things are waiting for you."',  bestFor: 'Soft SMBs · coordinators',       demos: 'KitchenOps',                                    accent: '#5a7a4a',              bg: '#f6f1e6',  cardKey: 'pas', fonts: 'Lora + Plex Sans',               tone: 'Calm · approachable' },
];

// Mini visual signature per template — abstract, 100% recognizable.
function TemplateThumb({ id, h = 180 }) {
  if (id === 'floorplan') {
    return (
      <div style={{ height: h, background: '#f6f1e8', display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', position: 'relative' }}>
        <div style={{ background: 'oklch(0.92 0.045 50)' }} />
        <div style={{ background: 'oklch(0.90 0.06 145)' }} />
        <div style={{ background: 'oklch(0.88 0.065 240)' }} />
        <div style={{ background: 'oklch(0.93 0.035 320)' }} />
        <svg viewBox="0 0 400 200" preserveAspectRatio="none" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
          <path d="M 50 100 C 130 90, 150 130, 200 110 S 320 130, 350 100" fill="none" stroke="#1a1d20" strokeOpacity="0.45" strokeWidth="1.2" strokeDasharray="4 4" />
        </svg>
        {[[60, 90, '#7a4ac4'], [180, 110, '#a04030'], [300, 90, '#7a4ac4'], [340, 110, '#c05a40']].map((s, i) => (
          <div key={i} style={{ position: 'absolute', left: s[0], top: s[1], width: 24, height: 24, borderRadius: '50%', background: s[2], boxShadow: '0 4px 12px rgba(0,0,0,0.18)', border: '2px solid white', color: 'white', font: '700 9px IBM Plex Mono', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>{['DO','HM','AP','PA'][i]}</div>
        ))}
        <div style={{ position: 'absolute', left: 200, top: 100, transform: 'translate(-50%,-50%)', padding: '2px 6px', borderRadius: 8, background: 'oklch(0.50 0.18 30)', color: 'white', font: '600 9px IBM Plex Mono' }}>DRAW-2031</div>
      </div>
    );
  }
  if (id === 'atelier') {
    return (
      <div style={{ height: h, background: '#1a3d5c', backgroundImage: 'linear-gradient(rgba(255,255,255,0.08) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,0.08) 1px, transparent 1px)', backgroundSize: '14px 14px', padding: 14, position: 'relative', color: '#e6efe6' }}>
        <div style={{ fontFamily: 'Special Elite, IBM Plex Mono, monospace', fontSize: 9, letterSpacing: '0.1em', opacity: 0.7 }}>DOC G702 · APP 07</div>
        <div style={{ fontFamily: 'DM Serif Display, IBM Plex Serif, serif', fontSize: 26, color: '#f5e6c8', marginTop: 4, lineHeight: 1 }}>Riverline Plaza</div>
        <div style={{ fontFamily: 'Special Elite, monospace', fontSize: 22, color: '#f5e6c8', marginTop: 6 }}>$142,000</div>
        <div style={{ position: 'absolute', bottom: 10, left: 14, fontFamily: 'Caveat, cursive', fontSize: 18, color: '#f5e6c8' }}>Diego Ortega</div>
        <div style={{ position: 'absolute', top: 12, right: 12, fontFamily: 'Special Elite, monospace', fontSize: 9, fontWeight: 700, border: '2.5px solid #ffb0a8', color: '#ffb0a8', padding: '3px 7px', transform: 'rotate(-4deg)', letterSpacing: '0.1em' }}>UNDER REVIEW</div>
      </div>
    );
  }
  if (id === 'controlroom') {
    return (
      <div style={{ height: h, background: '#0a0c0d', backgroundImage: 'linear-gradient(rgba(255,255,255,0.025) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,0.025) 1px, transparent 1px)', backgroundSize: '14px 14px', padding: 14, position: 'relative', color: '#d7dde6', fontFamily: 'IBM Plex Mono, monospace' }}>
        <div style={{ fontSize: 9, color: 'oklch(0.82 0.20 145)', letterSpacing: '0.12em', textTransform: 'uppercase' }}>// PORTFOLIO · EXPOSURE</div>
        <div style={{ fontSize: 38, color: 'oklch(0.82 0.20 145)', textShadow: '0 0 14px oklch(0.82 0.20 145)', letterSpacing: '-0.04em', marginTop: 4, fontWeight: 600 }}>$62.6M</div>
        <div style={{ fontSize: 9, color: '#8b95a3', letterSpacing: '0.12em', textTransform: 'uppercase', marginTop: 10 }}>12 ACTIVE · ▲ MTD</div>
        <div style={{ position: 'absolute', bottom: 10, left: 14, right: 14, display: 'flex', gap: 8 }}>
          {[true, true, false, true, false, true, true, false].map((on, i) => (
            <div key={i} style={{ flex: 1 }}>
              <span style={{ width: 7, height: 7, borderRadius: '50%', background: on ? 'oklch(0.82 0.20 145)' : '#3a424e', boxShadow: on ? '0 0 6px oklch(0.82 0.20 145)' : 'none', display: 'block' }} />
            </div>
          ))}
        </div>
      </div>
    );
  }
  if (id === 'technical') {
    return (
      <div style={{ height: h, background: '#fafaf6', backgroundImage: 'linear-gradient(rgba(0,0,0,0.05) 1px, transparent 1px), linear-gradient(90deg, rgba(0,0,0,0.05) 1px, transparent 1px)', backgroundSize: '14px 14px', position: 'relative', overflow: 'hidden' }}>
        <svg viewBox="0 0 400 200" style={{ width: '100%', height: '100%' }}>
          <line x1="40" y1="40" x2="360" y2="40" stroke="#0a0a0a" strokeWidth="0.6" />
          <line x1="40" y1="36" x2="40" y2="44" stroke="#0a0a0a" strokeWidth="0.6" />
          <line x1="360" y1="36" x2="360" y2="44" stroke="#0a0a0a" strokeWidth="0.6" />
          <text x="200" y="34" textAnchor="middle" fontFamily="IBM Plex Mono" fontSize="8" fill="#0a0a0a">2,377,000 · CONTRACT</text>
          <rect x="40" y="90" width="80" height="36" fill="rgba(10,10,10,0.85)" stroke="#0a0a0a" strokeWidth="0.6" />
          <rect x="120" y="90" width="100" height="36" fill="rgba(10,10,10,0.85)" stroke="#0a0a0a" strokeWidth="0.6" />
          <rect x="220" y="90" width="80" height="36" fill="rgba(10,10,10,0.85)" stroke="#0a0a0a" strokeWidth="0.6" />
          <rect x="300" y="90" width="60" height="36" fill="white" stroke="#0a0a0a" strokeWidth="0.6" />
          <text x="80" y="142" textAnchor="middle" fontFamily="IBM Plex Mono" fontSize="7" fill="#0a0a0a">100%</text>
          <text x="170" y="142" textAnchor="middle" fontFamily="IBM Plex Mono" fontSize="7" fill="#0a0a0a">92%</text>
          <text x="260" y="142" textAnchor="middle" fontFamily="IBM Plex Mono" fontSize="7" fill="#0a0a0a">85%</text>
          <line x1="220" y1="90" x2="220" y2="60" stroke="#0a0a0a" strokeWidth="0.4" />
          <circle cx="220" cy="60" r="8" fill="white" stroke="#0a0a0a" strokeWidth="0.6" />
          <text x="220" y="63" textAnchor="middle" fontFamily="IBM Plex Mono" fontSize="8" fontWeight="700" fill="#0a0a0a">1</text>
        </svg>
        <div style={{ position: 'absolute', bottom: 6, right: 8, fontFamily: 'IBM Plex Mono', fontSize: 8, letterSpacing: '0.1em', color: '#0a0a0a', textTransform: 'uppercase' }}>SHEET A-07 · 1/4</div>
      </div>
    );
  }
  if (id === 'editorial') {
    return (
      <div style={{ height: h, background: '#fbf8f1', padding: '14px 18px', position: 'relative', color: '#1a1814' }}>
        <div style={{ fontFamily: 'IBM Plex Mono', fontSize: 9, letterSpacing: '0.16em', textTransform: 'uppercase', color: '#9a8a72' }}>VOL. IV · ISSUE 07</div>
        <div style={{ fontFamily: 'DM Serif Display, IBM Plex Serif, serif', fontSize: 22, letterSpacing: '-0.005em', lineHeight: 1.05, marginTop: 4 }}>Eight applications.<br/><span style={{ color: '#9a8a72' }}>One needs your eyes.</span></div>
        <div style={{ marginTop: 8, fontFamily: 'IBM Plex Sans', fontSize: 11, color: '#3a362d', lineHeight: 1.45 }}>
          <span style={{ float: 'left', fontFamily: 'DM Serif Display, serif', fontSize: 38, lineHeight: 0.85, marginRight: 6, marginTop: 4, color: '#9a2828' }}>P</span>
          ortfolio exposure stands at $62.6M across twelve active loans; this period released $127,800.
        </div>
      </div>
    );
  }
  if (id === 'pastoral') {
    return (
      <div style={{ height: h, background: '#f6f1e6', padding: 16, color: '#2a2620', display: 'flex', flexDirection: 'column', justifyContent: 'space-between', position: 'relative' }}>
        <div>
          <div style={{ fontFamily: 'IBM Plex Sans', fontSize: 10, letterSpacing: '0.06em', textTransform: 'uppercase', color: '#7a7064' }}>Friday afternoon</div>
          <div style={{ fontFamily: 'Lora, "IBM Plex Serif", serif', fontSize: 22, letterSpacing: '-0.01em', lineHeight: 1.1, marginTop: 4 }}>Good afternoon, Marcus.<br/><span style={{ color: '#5a7a4a', fontStyle: 'italic' }}>Five things are waiting for you.</span></div>
        </div>
        <div style={{ padding: 10, background: '#fbf7eb', borderRadius: 12, border: '1px solid #e6dfcd' }}>
          <div style={{ fontFamily: 'IBM Plex Sans', fontSize: 9, color: '#7a7064', textTransform: 'uppercase', letterSpacing: '0.06em' }}>A small approval</div>
          <div style={{ fontFamily: 'Lora, serif', fontSize: 13, marginTop: 2 }}>DRAW-2055 · Northgate Medical</div>
        </div>
      </div>
    );
  }
  return null;
}

// Tiny dotted preview for the bottom of cards — shows the 12 components abstractly
function ComponentsRow({ accent }) {
  return (
    <div style={{ display: 'flex', gap: 3, marginTop: 8 }}>
      {Array.from({ length: 12 }).map((_, i) => (
        <div key={i} style={{ flex: 1, height: 3, background: i < 7 ? accent : 'rgba(0,0,0,0.12)', borderRadius: 2 }} />
      ))}
    </div>
  );
}

// === THE /templates DISCOVERY PAGE ===
function TemplatesDiscovery() {
  return (
    <div style={{ height: '100%', display: 'flex', flexDirection: 'column', background: '#f6f4ef', overflow: 'auto', fontFamily: 'IBM Plex Sans, system-ui', color: '#1a1814' }}>
      {/* Nav — ustack.ai style */}
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '14px 36px', borderBottom: '1px solid #d8d4c8', flex: '0 0 auto' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 26 }}>
          <div style={{ font: '600 16px IBM Plex Mono', letterSpacing: '-0.01em' }}>·stack.ai</div>
          <div style={{ display: 'flex', gap: 18, fontFamily: 'IBM Plex Mono', fontSize: 12, color: '#3a362d' }}>
            {['Examples','How it works','Architecture','Templates','Pricing','Trust'].map((n, i) => (
              <span key={n} style={{ color: n === 'Templates' ? '#1a1814' : '#6b6557', fontWeight: n === 'Templates' ? 600 : 400, borderBottom: n === 'Templates' ? '1.5px solid #1a1814' : 'none', paddingBottom: 4 }}>{n}</span>
            ))}
          </div>
        </div>
        <div style={{ display: 'flex', gap: 10, alignItems: 'center', fontFamily: 'IBM Plex Mono', fontSize: 12 }}>
          <span style={{ color: '#6b6557' }}>Sign in</span>
          <span style={{ padding: '6px 14px', background: '#1a1814', color: '#f6f4ef' }}>Upload brief →</span>
        </div>
      </div>

      {/* Breadcrumb */}
      <div style={{ padding: '14px 36px', fontFamily: 'IBM Plex Mono', fontSize: 10.5, color: '#6b6557', letterSpacing: '0.04em', flex: '0 0 auto' }}>
        ustack / <span style={{ color: '#1a1814' }}>templates</span>
      </div>

      {/* Hero */}
      <div style={{ padding: '20px 36px 40px', borderBottom: '1px solid #d8d4c8', flex: '0 0 auto' }}>
        <div style={{ fontFamily: 'IBM Plex Mono', fontSize: 11, letterSpacing: '0.16em', textTransform: 'uppercase', color: '#6b6557' }}>// app templates · v1 · six to choose from</div>
        <h1 style={{ font: '500 56px "IBM Plex Serif", "DM Serif Display", serif', letterSpacing: '-0.02em', lineHeight: 1, marginTop: 12, maxWidth: 880, textWrap: 'balance' }}>
          Pick how your application <span style={{ fontStyle: 'italic', color: '#6b6557' }}>looks and feels</span> — and we build every screen in it.
        </h1>
        <div style={{ marginTop: 18, display: 'grid', gridTemplateColumns: '1.4fr 1fr', gap: 36, alignItems: 'start' }}>
          <p style={{ fontSize: 15, lineHeight: 1.55, color: '#3a362d', maxWidth: 640 }}>
            A template is the visual operating system your application wears.
            One template paints all <b>12 canonical components</b> every uStack app ships with — the shell, the actor switcher, the home,
            the lists and details, forms, workflows, the audit feed, the generated documents, the settings, the empty states.
            <br/><br/>
            Pick one in the questionnaire; the entire build inherits it. Switch later in two clicks — no rebuild. Your app stays yours; the look is a setting.
          </p>
          <div style={{ borderLeft: '1px solid #d8d4c8', paddingLeft: 24, fontFamily: 'IBM Plex Mono', fontSize: 12, color: '#6b6557', display: 'flex', flexDirection: 'column', gap: 10 }}>
            {[
              ['06', 'complete design systems'],
              ['12', 'components painted per template'],
              ['72', 'production surfaces · all live'],
              ['2', 'clicks to switch · no rebuild'],
            ].map(([n, l]) => (
              <div key={l} style={{ display: 'flex', alignItems: 'baseline', gap: 14, padding: '8px 0', borderBottom: '1px solid #ecebe5' }}>
                <span style={{ font: '500 28px IBM Plex Mono', color: '#1a1814', letterSpacing: '-0.02em', width: 60 }}>{n}</span>
                <span style={{ color: '#3a362d' }}>{l}</span>
              </div>
            ))}
          </div>
        </div>
      </div>

      {/* How they work · 3 columns */}
      <div style={{ padding: '32px 36px 24px', borderBottom: '1px solid #d8d4c8', flex: '0 0 auto' }}>
        <div style={{ fontFamily: 'IBM Plex Mono', fontSize: 11, letterSpacing: '0.16em', textTransform: 'uppercase', color: '#6b6557', marginBottom: 16 }}>// how templates work</div>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 36 }}>
          {[
            ['01', 'You pick once.',         'In the 7-stage intake (UI stage), you select one template. We render every screen in your build through it.'],
            ['02', '12 components painted.', 'The shell, sign-in, actor switcher, home, lists, details, forms, workflows, audit, outputs, settings, empty states — all generated in the template.'],
            ['03', 'Switch anytime.',        'Templates are a setting, not a fork. Try Floorplan for a week, swap to Editorial — your code, schema, and data are untouched.'],
          ].map(([n, h, b], i) => (
            <div key={n}>
              <div style={{ fontFamily: 'IBM Plex Mono', fontSize: 11, letterSpacing: '0.08em', color: '#6b6557' }}>// {n}</div>
              <div style={{ font: '600 18px IBM Plex Sans', letterSpacing: '-0.01em', marginTop: 4 }}>{h}</div>
              <div style={{ fontSize: 13.5, lineHeight: 1.55, color: '#3a362d', marginTop: 6 }}>{b}</div>
            </div>
          ))}
        </div>
      </div>

      {/* THE 6 — grid */}
      <div style={{ padding: '32px 36px 28px' }}>
        <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 18 }}>
          <div>
            <div style={{ fontFamily: 'IBM Plex Mono', fontSize: 11, letterSpacing: '0.16em', textTransform: 'uppercase', color: '#6b6557' }}>// the six</div>
            <h2 style={{ font: '500 28px "IBM Plex Serif", serif', letterSpacing: '-0.015em', marginTop: 2 }}>Each one is a complete design system.</h2>
          </div>
          <div style={{ fontFamily: 'IBM Plex Mono', fontSize: 11, color: '#6b6557', display: 'flex', gap: 10 }}>
            <span style={{ padding: '3px 8px', border: '1px solid #d8d4c8', borderRadius: 999 }}>sorted · default</span>
            <span style={{ padding: '3px 8px', border: '1px solid #d8d4c8', borderRadius: 999, color: '#1a1814', fontWeight: 600 }}>all 6</span>
          </div>
        </div>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 18 }}>
          {TEMPLATES.map((t, i) => (
            <a key={t.id} style={{
              display: 'flex', flexDirection: 'column',
              background: '#fbfaf7',
              border: '1px solid #d8d4c8',
              borderRadius: 6,
              overflow: 'hidden', textDecoration: 'none', color: 'inherit', cursor: 'pointer',
              transition: 'transform .15s, box-shadow .15s',
            }}>
              <TemplateThumb id={t.id} h={180} />
              <div style={{ padding: '14px 16px 16px', display: 'flex', flexDirection: 'column', gap: 4 }}>
                <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between' }}>
                  <h3 style={{ font: '600 17px "IBM Plex Sans"', letterSpacing: '-0.01em' }}>{i + 1 < 10 ? `0${i+1}` : i+1} · {t.name}</h3>
                  <span style={{ fontFamily: 'IBM Plex Mono', fontSize: 10, color: '#9a9382' }}>{t.id}</span>
                </div>
                <div style={{ fontFamily: '"IBM Plex Serif", serif', fontSize: 14.5, fontStyle: 'italic', color: '#3a362d', lineHeight: 1.3, marginTop: 2 }}>{t.tagline}</div>
                <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, marginTop: 8 }}>
                  <span style={{ padding: '2px 8px', borderRadius: 999, border: '1px solid #d8d4c8', fontFamily: 'IBM Plex Mono', fontSize: 10, color: '#3a362d' }}>{t.bestFor}</span>
                </div>
                <div style={{ fontFamily: 'IBM Plex Mono', fontSize: 10.5, color: '#6b6557', marginTop: 8, letterSpacing: '0.02em' }}>
                  default for: <span style={{ color: '#1a1814' }}>{t.demos}</span>
                </div>
                <ComponentsRow accent={t.accent} />
                <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginTop: 12, paddingTop: 10, borderTop: '1px solid #ecebe5' }}>
                  <span style={{ fontFamily: 'IBM Plex Mono', fontSize: 10, color: '#6b6557' }}>12 components painted</span>
                  <span style={{ fontFamily: 'IBM Plex Mono', fontSize: 11, color: t.dark ? '#1a1814' : t.accent, fontWeight: 600 }}>see details →</span>
                </div>
              </div>
            </a>
          ))}
        </div>
      </div>

      {/* Can't decide */}
      <div style={{ padding: '20px 36px 32px', borderTop: '1px solid #d8d4c8' }}>
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 280px', gap: 24, alignItems: 'center' }}>
          <div>
            <div style={{ fontFamily: 'IBM Plex Mono', fontSize: 11, letterSpacing: '0.16em', textTransform: 'uppercase', color: '#6b6557' }}>// can't decide?</div>
            <div style={{ font: '500 22px "IBM Plex Serif", serif', letterSpacing: '-0.01em', marginTop: 4 }}>
              Skip the choice — we'll suggest one based on your spec.
            </div>
            <div style={{ fontSize: 13, color: '#3a362d', marginTop: 6, maxWidth: 560 }}>
              In the 7-stage intake, leave UI on auto. Our build agent reads your problem statement and picks a default template — typically the one already powering the most-similar example app. You can still switch later.
            </div>
          </div>
          <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
            <span style={{ padding: '8px 14px', background: '#1a1814', color: '#f6f4ef', fontFamily: 'IBM Plex Mono', fontSize: 12, textAlign: 'center' }}>Upload your brief →</span>
            <span style={{ padding: '8px 14px', border: '1px solid #d8d4c8', fontFamily: 'IBM Plex Mono', fontSize: 12, textAlign: 'center' }}>See template × demo map ↗</span>
          </div>
        </div>
      </div>

      {/* Footer */}
      <div style={{ padding: '14px 36px', borderTop: '1px solid #d8d4c8', display: 'flex', justifyContent: 'space-between', fontFamily: 'IBM Plex Mono', fontSize: 10.5, color: '#6b6557' }}>
        <span>© 2026 ustack · app templates · six complete design systems</span>
        <span>terms · privacy · trust · hello@ustack.ai</span>
      </div>
    </div>
  );
}

window.TemplatesDiscovery = TemplatesDiscovery;
window.TEMPLATES_LIST = TEMPLATES;
window.TemplateThumb = TemplateThumb;
