/* ──────────────────────────────────────────────────────────────
   Customer screen — /card
   Translates the physical card's front+back into one tall phone view.
   ────────────────────────────────────────────────────────────── */

const PhoneFrame = ({ children, label }) => (
  <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 12 }}>
    <div style={{
      width: 390,
      height: 844,
      borderRadius: 48,
      background: '#0E0B07',
      padding: 11,
      boxShadow:
        '0 50px 80px -30px rgba(40,20,10,0.45),' +
        '0 20px 40px -20px rgba(40,20,10,0.35),' +
        'inset 0 0 0 1px rgba(255,255,255,0.05)',
      position: 'relative',
    }}>
      {/* notch */}
      <div style={{
        position: 'absolute', top: 18, left: '50%', transform: 'translateX(-50%)',
        width: 110, height: 30, background: '#0E0B07', borderRadius: 20, zIndex: 50,
      }} />
      <div style={{
        position: 'relative',
        width: '100%', height: '100%',
        borderRadius: 38,
        overflow: 'hidden',
        background: '#fff',
      }}>
        {children}
      </div>
    </div>
    <div style={{
      fontFamily: 'var(--font-display)', fontWeight: 600,
      fontSize: 12, letterSpacing: '0.18em', textTransform: 'uppercase',
      color: 'var(--sakura-gold-dk)',
    }}>{label}</div>
  </div>
);

const StatusBar = ({ tone = 'dark' }) => (
  <div style={{
    height: 50, display: 'flex', alignItems: 'flex-end', justifyContent: 'space-between',
    padding: '0 28px 8px', color: tone === 'dark' ? '#1a1815' : '#fff',
    fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 15,
    position: 'relative', zIndex: 5,
  }}>
    <span>9:41</span>
    <div style={{ display: 'flex', gap: 5, alignItems: 'center' }}>
      <svg width="17" height="11" viewBox="0 0 17 11" fill="currentColor"><rect x="0" y="6" width="3" height="5" rx="0.5"/><rect x="4.5" y="4" width="3" height="7" rx="0.5"/><rect x="9" y="2" width="3" height="9" rx="0.5"/><rect x="13.5" y="0" width="3" height="11" rx="0.5"/></svg>
      <svg width="16" height="11" viewBox="0 0 16 11" fill="none" stroke="currentColor" strokeWidth="1.2"><path d="M1 4a10 10 0 0 1 14 0M3.5 6.5a6.5 6.5 0 0 1 9 0M6 9a3 3 0 0 1 4 0" /></svg>
      <svg width="26" height="12" viewBox="0 0 26 12"><rect x="0.5" y="0.5" width="22" height="11" rx="3" fill="none" stroke="currentColor" strokeWidth="1" opacity="0.5"/><rect x="2" y="2" width="18" height="8" rx="1.5" fill="currentColor"/><rect x="23.5" y="4" width="1.5" height="4" rx="0.5" fill="currentColor" opacity="0.5"/></svg>
    </div>
  </div>
);

/* ── Header: pink gradient panel echoing the card front ───────────── */
const CardHeader = ({ user }) => (
  <div style={{
    position: 'relative',
    background:
      'radial-gradient(120% 80% at 50% 40%, var(--sakura-cream) 0%, var(--sakura-blush) 60%, #E8A6AC 100%)',
    paddingTop: 0, paddingBottom: 28,
    overflow: 'hidden',
  }}>
    <div className="hairline-diag" style={{
      position: 'absolute', inset: 0, opacity: 0.5, pointerEvents: 'none',
    }} />

    {/* corner blossoms — like the printed card */}
    <div style={{ position: 'absolute', top: -10, right: -30, pointerEvents: 'none' }}>
      <CornerBlossoms size={200} flip={true} opacity={0.7} />
    </div>
    <div style={{ position: 'absolute', bottom: -60, left: -40, pointerEvents: 'none' }}>
      <CornerBlossoms size={160} flip={false} opacity={0.55} />
    </div>

    <StatusBar tone="dark" />

    {/* row 1: avatar + greeting + bell */}
    <div style={{
      display: 'flex', alignItems: 'center', justifyContent: 'space-between',
      padding: '4px 22px 18px', position: 'relative', zIndex: 2,
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
        <div style={{
          width: 44, height: 44, borderRadius: 22,
          background: 'linear-gradient(135deg, #F8D7D9 0%, #E89AA0 100%)',
          border: '2px solid rgba(255,253,251,0.9)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 17,
          color: 'var(--sakura-rose)',
          boxShadow: '0 4px 14px rgba(196,107,115,0.3)',
        }}>{user.initial}</div>
        <div>
          <div style={{
            fontFamily: 'var(--font-accent)', fontWeight: 500, fontStyle: 'italic',
            fontSize: 12, color: 'var(--sakura-gold-dk)', lineHeight: 1,
          }}>こんにちは</div>
          <div style={{
            fontFamily: 'var(--font-display)', fontWeight: 700,
            fontSize: 17, color: 'var(--sakura-char)', marginTop: 3,
          }}>Hola, {user.name}</div>
        </div>
      </div>
      <button style={{
        width: 38, height: 38, borderRadius: 19,
        background: 'rgba(255,253,251,0.55)', border: '1px solid rgba(255,255,255,0.7)',
        backdropFilter: 'blur(6px)',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        color: 'var(--sakura-char)', cursor: 'pointer',
      }}>
        <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M6 8a6 6 0 0 1 12 0c0 7 3 7 3 9H3c0-2 3-2 3-9z"/><path d="M10 21a2 2 0 0 0 4 0"/></svg>
      </button>
    </div>

    {/* wordmark */}
    <div style={{
      display: 'flex', justifyContent: 'center', paddingTop: 4,
      position: 'relative', zIndex: 2,
    }}>
      <SakuraWordmark variant="lg" />
    </div>
  </div>
);

/* ── Stamp grid — the back of the card, live ──────────────────────── */
const StampCard = ({ count, total, justAdded }) => {
  const cells = Array.from({ length: total }, (_, i) => i < count);
  const complete = count >= total;

  return (
    <div style={{
      margin: '-20px 18px 0',
      borderRadius: 26,
      background:
        'radial-gradient(120% 100% at 50% 50%, var(--sakura-cream) 0%, var(--sakura-blush) 65%, #E29CA2 100%)',
      padding: '22px 18px 24px',
      position: 'relative',
      overflow: 'hidden',
      boxShadow: '0 18px 40px -18px rgba(196,107,115,0.45), 0 1px 0 rgba(255,255,255,0.6) inset',
      border: '1px solid rgba(255,255,255,0.55)',
    }}>
      <div className="hairline-diag" style={{
        position: 'absolute', inset: 0, opacity: 0.45, pointerEvents: 'none',
      }} />
      <div style={{ position: 'absolute', top: -20, left: -30, pointerEvents: 'none' }}>
        <CornerBlossoms size={130} flip={true} opacity={0.55} />
      </div>
      <div style={{ position: 'absolute', bottom: -25, right: -30, pointerEvents: 'none' }}>
        <CornerBlossoms size={130} opacity={0.55} />
      </div>

      {/* header row: title + progress chip */}
      <div style={{
        position: 'relative', zIndex: 2,
        display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between',
        marginBottom: 14,
      }}>
        <div>
          <div style={{
            fontFamily: 'var(--font-display)', fontSize: 10, fontWeight: 600,
            letterSpacing: '0.22em', textTransform: 'uppercase',
            color: 'var(--sakura-gold-dk)', marginBottom: 4,
          }}>Tu tarjeta</div>
          <div style={{
            fontFamily: 'var(--font-display)', fontWeight: 700,
            fontSize: 19, lineHeight: 1.15, color: 'var(--sakura-char)',
            maxWidth: 200,
          }}>Por cada 10 menús,<br/>llévate 1 <em style={{ fontFamily: 'var(--font-accent)', fontStyle: 'italic', fontWeight: 700, color: 'var(--sakura-rose)' }}>gratis</em></div>
        </div>
        <div style={{
          display: 'flex', flexDirection: 'column', alignItems: 'flex-end',
        }}>
          <div style={{
            fontFamily: 'var(--font-display)', fontWeight: 800,
            fontSize: 28, lineHeight: 1, color: 'var(--sakura-ink-dk)',
            letterSpacing: '-0.02em',
          }}>
            {count}<span style={{ color: 'var(--sakura-gold)', fontWeight: 600 }}>/{total}</span>
          </div>
          <div style={{
            fontFamily: 'var(--font-display)', fontSize: 10, fontWeight: 500,
            letterSpacing: '0.15em', textTransform: 'uppercase',
            color: 'var(--sakura-gold-dk)', marginTop: 2,
          }}>sellos</div>
        </div>
      </div>

      {/* 5×2 grid */}
      <div style={{
        position: 'relative', zIndex: 2,
        display: 'grid',
        gridTemplateColumns: 'repeat(5, minmax(0, 1fr))',
        gap: 8,
        padding: '6px 0 4px',
      }}>
        {cells.map((on, i) => (
          <div key={i} style={{
            aspectRatio: '1 / 1',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            position: 'relative',
            animation: justAdded === i ? 'stamp-press 0.55s cubic-bezier(.2,1.4,.4,1)' : 'none',
          }}>
            <StampGlyph filled={on} index={i} size={58} />
          </div>
        ))}
      </div>

      {/* footer — progress sentence */}
      <div style={{
        position: 'relative', zIndex: 2,
        marginTop: 14,
        display: 'flex', alignItems: 'center', gap: 10,
        padding: '10px 14px',
        background: 'rgba(255,253,251,0.55)',
        borderRadius: 14,
        border: '1px solid rgba(255,255,255,0.7)',
        backdropFilter: 'blur(4px)',
      }}>
        {complete ? (
          <>
            <div style={{
              width: 28, height: 28, borderRadius: 14,
              background: 'var(--sakura-rose)',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              color: '#fff',
            }}>
              <Icon name="sparkle" size={16} color="#fff" />
            </div>
            <div style={{ flex: 1 }}>
              <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 13, color: 'var(--sakura-char)' }}>¡Tu menú gratis te espera!</div>
              <div style={{ fontFamily: 'var(--font-body)', fontSize: 11, color: 'var(--sakura-gold-dk)' }}>Canjéalo en tu próxima visita</div>
            </div>
          </>
        ) : (
          <>
            <div style={{
              width: 28, height: 28, borderRadius: 14,
              background: 'var(--sakura-ink)',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              boxShadow: '0 0 0 4px rgba(47,91,170,0.15)',
            }}>
              <span style={{ fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: 13, color: '#fff' }}>{total - count}</span>
            </div>
            <div style={{ flex: 1 }}>
              <div style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 13, color: 'var(--sakura-char)' }}>
                {total - count} {total - count === 1 ? 'sello' : 'sellos'} para tu menú gratis
              </div>
              <div style={{ fontFamily: 'var(--font-body)', fontSize: 11, color: 'var(--sakura-gold-dk)' }}>Sigue así · sushi de regalo cerca</div>
            </div>
          </>
        )}
      </div>
    </div>
  );
};

/* ── QR section — always white, high contrast ─────────────────────── */
const QRSection = ({ user }) => (
  <div style={{ margin: '18px 18px 0' }}>
    <div style={{
      borderRadius: 24,
      background: 'var(--sakura-paper)',
      padding: '20px 18px 22px',
      boxShadow: '0 10px 28px -16px rgba(40,20,10,0.25)',
      border: '1px solid rgba(184,153,104,0.18)',
      display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 10,
    }}>
      <div style={{
        fontFamily: 'var(--font-display)', fontSize: 10, fontWeight: 600,
        letterSpacing: '0.22em', textTransform: 'uppercase',
        color: 'var(--sakura-gold-dk)',
      }}>Muéstralo al camarero</div>

      <div style={{
        position: 'relative',
        padding: 10,
        background: '#fff',
        borderRadius: 14,
        boxShadow: '0 0 0 1px rgba(184,153,104,0.25), 0 4px 14px rgba(40,20,10,0.08)',
      }}>
        <FakeQR size={160} fg="#0E0B07" bg="#FFFFFF" />
        {/* centre logo on QR */}
        <div style={{
          position: 'absolute', inset: 0,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          pointerEvents: 'none',
        }}>
          <div style={{
            width: 36, height: 36, borderRadius: 10,
            background: 'var(--sakura-paper)',
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            boxShadow: '0 0 0 3px #fff',
          }}>
            <SakuraGlyph size={20} color="var(--sakura-gold)" />
          </div>
        </div>
      </div>

      <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginTop: 2 }}>
        <div style={{
          fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 12,
          color: 'var(--sakura-char)',
          fontVariantNumeric: 'tabular-nums', letterSpacing: '0.1em',
        }}>ID · {user.id}</div>
        <div style={{ width: 4, height: 4, borderRadius: 2, background: 'var(--sakura-gold)' }} />
        <div style={{ fontFamily: 'var(--font-body)', fontSize: 11, color: 'var(--sakura-gold-dk)' }}>
          Caduca en 30 s
        </div>
      </div>
    </div>
  </div>
);

/* ── Wallet buttons ─────────────────────────────────────────────── */
const WalletButtons = () => (
  <div style={{
    margin: '14px 18px 0', display: 'flex', flexDirection: 'column', gap: 8,
  }}>
    {/* Apple Wallet — black, tinted with sakura-rose accent */}
    <button style={{
      display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      background: '#0E0B07', color: '#fff',
      borderRadius: 14, border: 'none',
      height: 48, cursor: 'pointer',
      fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 14,
      boxShadow: 'inset 0 1px 0 rgba(255,255,255,0.07), 0 4px 12px rgba(0,0,0,0.25)',
    }}>
      <svg width="20" height="20" viewBox="0 0 24 24" fill="#fff"><path d="M17.05 12.04c-.03-2.93 2.4-4.33 2.51-4.4-1.37-2.01-3.5-2.28-4.26-2.31-1.81-.18-3.54 1.07-4.46 1.07-.93 0-2.34-1.05-3.85-1.02-1.98.03-3.81 1.15-4.83 2.92-2.06 3.57-.53 8.85 1.48 11.74.98 1.41 2.16 3 3.69 2.94 1.48-.06 2.04-.96 3.83-.96s2.29.96 3.85.93c1.59-.03 2.6-1.44 3.57-2.86 1.13-1.64 1.59-3.23 1.62-3.31-.04-.02-3.11-1.19-3.15-4.74zM14.12 4.31c.81-.99 1.36-2.36 1.21-3.73-1.17.05-2.59.78-3.43 1.76-.75.87-1.41 2.27-1.23 3.61 1.31.1 2.64-.66 3.45-1.64z"/></svg>
      <span style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', lineHeight: 1.1 }}>
        <span style={{ fontSize: 9, opacity: 0.7, letterSpacing: '0.05em' }}>Añadir a</span>
        <span style={{ fontSize: 14, fontWeight: 700 }}>Apple Wallet</span>
      </span>
    </button>
    {/* Google Wallet — cream/paper with gold border, tinted */}
    <button style={{
      display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
      background: 'var(--sakura-paper)', color: 'var(--sakura-char)',
      borderRadius: 14,
      border: '1px solid rgba(184,153,104,0.4)',
      height: 48, cursor: 'pointer',
      fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 14,
      boxShadow: '0 2px 6px rgba(40,20,10,0.06)',
    }}>
      <svg width="20" height="20" viewBox="0 0 24 24"><path d="M3 8a3 3 0 0 1 3-3h12a3 3 0 0 1 3 3v8a3 3 0 0 1-3 3H6a3 3 0 0 1-3-3z" fill="none" stroke="var(--sakura-char)" strokeWidth="1.6"/><path d="M3 11h18M15 15h3" stroke="var(--sakura-char)" strokeWidth="1.6"/><circle cx="9" cy="15" r="1.2" fill="var(--sakura-ink)"/><circle cx="12" cy="15" r="1.2" fill="var(--sakura-rose)"/></svg>
      <span style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', lineHeight: 1.1 }}>
        <span style={{ fontSize: 9, opacity: 0.6, letterSpacing: '0.05em' }}>Añadir a</span>
        <span style={{ fontSize: 14, fontWeight: 700 }}>Google Wallet</span>
      </span>
    </button>
  </div>
);

/* ── Bottom nav ───────────────────────────────────────────────────── */
const BottomNav = ({ active = 'card' }) => {
  const items = [
    { id: 'card',    icon: 'home',    label: 'Tarjeta' },
    { id: 'rewards', icon: 'gift',    label: 'Premios' },
    { id: 'menu',    icon: 'menu',    label: 'Carta' },
    { id: 'me',      icon: 'user',    label: 'Cuenta' },
  ];
  return (
    <div style={{
      position: 'absolute', bottom: 0, left: 0, right: 0,
      paddingTop: 8, paddingBottom: 26,
      background: 'rgba(255,253,251,0.92)',
      backdropFilter: 'blur(12px)',
      borderTop: '1px solid rgba(184,153,104,0.18)',
      display: 'flex', justifyContent: 'space-around',
    }}>
      {items.map(it => {
        const on = active === it.id;
        return (
          <div key={it.id} style={{
            display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 3,
            color: on ? 'var(--sakura-rose)' : 'var(--sakura-gold-dk)',
            opacity: on ? 1 : 0.55,
            position: 'relative',
          }}>
            {on && (
              <span style={{
                position: 'absolute', top: -8, width: 24, height: 3, borderRadius: 2,
                background: 'var(--sakura-rose)',
              }} />
            )}
            <Icon name={it.icon} size={22} />
            <span style={{
              fontFamily: 'var(--font-display)', fontSize: 10, fontWeight: 600,
              letterSpacing: '0.05em',
            }}>{it.label}</span>
          </div>
        );
      })}
    </div>
  );
};

/* ── Compose the customer screen ──────────────────────────────────── */
const CustomerScreen = ({ stampCount, total, justAdded, user, complete }) => (
  <PhoneFrame label="Cliente · /card">
    <div style={{
      width: '100%', height: '100%', overflow: 'auto', position: 'relative',
      paddingBottom: 100,
      background:
        'linear-gradient(180deg, transparent 0%, transparent 320px, #FAF1E8 320px, #F4E7DA 100%)',
    }}>
      <CardHeader user={user} />
      <StampCard count={stampCount} total={total} justAdded={justAdded} />
      <QRSection user={user} />
      <WalletButtons />
      <div style={{ height: 30 }} />
    </div>

    {/* celebration confetti overlay when complete */}
    {complete && (
      <div style={{
        position: 'absolute', inset: 0, pointerEvents: 'none', zIndex: 30,
        overflow: 'hidden',
      }}>
        {Array.from({ length: 36 }).map((_, i) => {
          const left = (i * 37) % 100;
          const delay = (i * 0.07) % 2;
          const dur = 3 + ((i * 11) % 20) / 10;
          const color = ['#E89AA0', '#F4C5C7', '#B89968', '#2F5BAA'][i % 4];
          return (
            <span key={i} style={{
              position: 'absolute', top: -20, left: left + '%',
              width: 10, height: 10, borderRadius: 5,
              background: color,
              animation: `petal-fall ${dur}s linear ${delay}s infinite`,
              opacity: 0.85,
            }} />
          );
        })}
        <div style={{
          position: 'absolute', top: '38%', left: 0, right: 0,
          textAlign: 'center',
          fontFamily: 'var(--font-accent)', fontStyle: 'italic', fontWeight: 700,
          fontSize: 28, color: 'var(--sakura-rose)',
          textShadow: '0 4px 24px rgba(255,255,255,0.8)',
        }}>¡Felicidades!</div>
      </div>
    )}

    <BottomNav active="card" />
  </PhoneFrame>
);

window.CustomerScreen = CustomerScreen;
