/* ──────────────────────────────────────────────────────────────
   App — sets up state shared between the two phones, lays them
   out side-by-side, and renders the brand-extraction footer
   (palette swatches + font pairings).
   ────────────────────────────────────────────────────────────── */

const { useState, useCallback, useEffect } = React;

const USER = {
  name: 'Marina',
  lastName: 'Etxebarria',
  initial: 'M',
  id: '4087-2231',
  since: '2024',
};
const TOTAL_STAMPS = 10;

const App = () => {
  // single source of truth: stamp count drives BOTH phones
  const [stampCount, setStampCount] = useState(7);
  const [justAdded, setJustAdded] = useState(null);
  const [justScanned, setJustScanned] = useState(false);
  const [showCelebration, setShowCelebration] = useState(false);

  useEffect(() => {
    // demo loop: simulate a fresh scan after a beat
    const t = setTimeout(() => setJustScanned(true), 300);
    return () => clearTimeout(t);
  }, []);

  const handleAddStamp = useCallback(() => {
    setStampCount(c => {
      if (c >= TOTAL_STAMPS) return c;
      const next = c + 1;
      setJustAdded(c);
      setTimeout(() => setJustAdded(null), 700);
      if (next === TOTAL_STAMPS) {
        setShowCelebration(true);
        setTimeout(() => setShowCelebration(false), 4500);
      }
      return next;
    });
  }, []);

  const handleRedeem = useCallback(() => {
    setStampCount(0);
    setShowCelebration(false);
  }, []);

  const handleReset = useCallback(() => {
    setStampCount(0);
    setShowCelebration(false);
  }, []);

  return (
    <div style={{
      minHeight: '100vh', padding: '48px 32px 60px',
      display: 'flex', flexDirection: 'column', alignItems: 'center',
    }}>
      <KeyframesAndStyles />

      {/* page header */}
      <PageHeader />

      {/* two phones side-by-side */}
      <div style={{
        display: 'flex', gap: 60, alignItems: 'flex-start', flexWrap: 'wrap',
        justifyContent: 'center', marginTop: 36,
      }}>
        <CustomerScreen
          stampCount={stampCount}
          total={TOTAL_STAMPS}
          justAdded={justAdded}
          user={USER}
          complete={showCelebration}
        />
        <StaffScreen
          user={USER}
          stampCount={stampCount}
          total={TOTAL_STAMPS}
          onAddStamp={handleAddStamp}
          onRedeem={handleRedeem}
          justScanned={justScanned}
        />
      </div>

      {/* hint — interactive demo */}
      <div style={{
        marginTop: 28, padding: '12px 22px',
        background: 'rgba(255,253,251,0.7)',
        border: '1px solid rgba(184,153,104,0.25)',
        borderRadius: 100,
        fontFamily: 'var(--font-body)', fontSize: 13,
        color: 'var(--sakura-gold-dk)',
        display: 'flex', alignItems: 'center', gap: 14,
        boxShadow: '0 4px 14px rgba(40,20,10,0.06)',
      }}>
        <span>👆 Pulsa <b style={{ color: 'var(--sakura-ink)' }}>Añadir sello</b> en la pantalla del personal — la tarjeta del cliente se actualiza al instante.</span>
        <button
          onClick={handleReset}
          style={{
            border: '1px solid rgba(184,153,104,0.4)',
            background: 'transparent',
            padding: '4px 12px', borderRadius: 100, cursor: 'pointer',
            fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 11,
            color: 'var(--sakura-gold-dk)', letterSpacing: '0.05em',
          }}
        >Reiniciar</button>
      </div>

      {/* brand extraction sheets */}
      <BrandSheet />
    </div>
  );
};

const PageHeader = () => (
  <div style={{ textAlign: 'center', maxWidth: 720 }}>
    <div style={{
      display: 'inline-flex', alignItems: 'center', gap: 8,
      padding: '6px 14px',
      background: 'rgba(184,153,104,0.10)',
      border: '1px solid rgba(184,153,104,0.3)',
      borderRadius: 100,
      fontFamily: 'var(--font-display)', fontWeight: 600,
      fontSize: 11, letterSpacing: '0.2em', textTransform: 'uppercase',
      color: 'var(--sakura-gold-dk)',
    }}>
      <SakuraGlyph size={14} color="var(--sakura-gold)" />
      Sakura · Tarjeta de sellos digital
    </div>
    <h1 style={{
      fontFamily: 'var(--font-display)', fontWeight: 800,
      fontSize: 44, letterSpacing: '-0.025em', lineHeight: 1.05,
      color: 'var(--sakura-char)', margin: '18px 0 10px',
    }}>La tarjeta de papel,<br/>
      <span style={{ fontFamily: 'var(--font-accent)', fontStyle: 'italic', fontWeight: 700, color: 'var(--sakura-rose)' }}>en tu bolsillo.</span>
    </h1>
    <p style={{
      fontFamily: 'var(--font-body)', fontSize: 15, color: 'var(--sakura-gold-dk)',
      maxWidth: 540, margin: '0 auto', lineHeight: 1.55,
    }}>
      Una traducción digital fiel de la tarjeta de fidelidad —en rosa y oro— de Sakura.
      Dos pantallas, cliente y personal, que comparten un mismo recuento de sellos.
    </p>
  </div>
);

/* ── Brand-extraction sheets ─────────────────────────────────────── */
const BrandSheet = () => (
  <div style={{
    width: '100%', maxWidth: 1040, marginTop: 80,
    display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 24,
  }}>
    <PaletteCard />
    <TypeCard />
    <CSSVarsCard />
    <StampStudyCard />
  </div>
);

const PaletteCard = () => {
  const colors = [
    { name: 'Rosa Sakura',      token: '--sakura-blush',   hex: '#F4C5C7', note: 'Degradado del borde · rosa principal' },
    { name: 'Crema Sakura',     token: '--sakura-cream',   hex: '#FBE6E2', note: 'Centro de la tarjeta, rosa crema suave' },
    { name: 'Rosa pétalo',      token: '--sakura-petal',   hex: '#E89AA0', note: 'Rosa cerezo más intenso' },
    { name: 'Rosa acento',      token: '--sakura-rose',    hex: '#C46B73', note: 'Núcleo del pétalo · botones' },
    { name: 'Oro champán',      token: '--sakura-gold',    hex: '#B89968', note: 'Logotipo, filetes, iconos' },
    { name: 'Oro intenso',      token: '--sakura-gold-dk', hex: '#8A7148', note: 'Texto en oro' },
    { name: 'Cobalto sello',    token: '--sakura-ink',     hex: '#2F5BAA', note: 'El azul del sello de tinta' },
    { name: 'Cobalto profundo', token: '--sakura-ink-dk',  hex: '#1E3F7E', note: 'Sombra / profundidad del sello' },
    { name: 'Carbón cálido',    token: '--sakura-char',    hex: '#1A1815', note: 'Texto · casi negro cálido' },
    { name: 'Blanco cálido',    token: '--sakura-paper',   hex: '#FFFDFB', note: 'Blanco papel de la tarjeta' },
  ];

  return (
    <SheetCard title="Colores de marca" subtitle="Extraídos de la tarjeta física">
      <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
        {colors.map(c => (
          <div key={c.token} style={{
            display: 'flex', gap: 12, alignItems: 'center',
            padding: 10, borderRadius: 12,
            background: 'rgba(255,253,251,0.5)',
            border: '1px solid rgba(184,153,104,0.15)',
          }}>
            <div style={{
              width: 44, height: 44, borderRadius: 10,
              background: c.hex,
              boxShadow: 'inset 0 0 0 1px rgba(0,0,0,0.08), 0 2px 6px rgba(40,20,10,0.08)',
              flexShrink: 0,
            }} />
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{
                fontFamily: 'var(--font-display)', fontWeight: 700,
                fontSize: 12, color: 'var(--sakura-char)', lineHeight: 1.2,
              }}>{c.name}</div>
              <div style={{
                fontFamily: 'var(--font-body)', fontSize: 10,
                color: 'var(--sakura-gold-dk)', lineHeight: 1.3,
                whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis',
              }}>{c.note}</div>
              <div style={{
                fontFamily: 'var(--font-display)', fontWeight: 600,
                fontSize: 10, color: 'var(--sakura-rose)', marginTop: 2,
                fontVariantNumeric: 'tabular-nums', letterSpacing: '0.04em',
              }}>{c.hex}</div>
            </div>
          </div>
        ))}
      </div>
    </SheetCard>
  );
};

const TypeCard = () => (
  <SheetCard title="Tipografías" subtitle="A juego con el peso geométrico del logotipo">
    <div style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>
      {/* Display — Outfit */}
      <div style={{
        padding: 16, borderRadius: 14,
        background: 'linear-gradient(180deg, var(--sakura-cream) 0%, var(--sakura-blush) 100%)',
        border: '1px solid rgba(184,153,104,0.2)',
        position: 'relative', overflow: 'hidden',
      }}>
        <div className="hairline-diag" style={{ position: 'absolute', inset: 0, opacity: 0.35 }} />
        <div style={{ position: 'relative' }}>
          <div style={{
            fontFamily: 'var(--font-display)', fontSize: 10, fontWeight: 600,
            letterSpacing: '0.2em', textTransform: 'uppercase',
            color: 'var(--sakura-gold-dk)', marginBottom: 6,
          }}>Titular · Outfit 800</div>
          <div style={{
            fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: 44,
            color: 'var(--sakura-gold)', letterSpacing: '-0.025em', lineHeight: 1,
          }}>sakura</div>
          <div style={{
            fontFamily: 'var(--font-display)', fontWeight: 500, fontSize: 11,
            letterSpacing: '0.18em', textTransform: 'uppercase',
            color: 'var(--sakura-char)', marginTop: 8,
          }}>Sushi Japonés y Cocina Asiática</div>
        </div>
      </div>

      {/* Accent — Shippori Mincho */}
      <div style={{
        padding: 16, borderRadius: 14,
        background: 'var(--sakura-paper)',
        border: '1px solid rgba(184,153,104,0.2)',
      }}>
        <div style={{
          fontFamily: 'var(--font-display)', fontSize: 10, fontWeight: 600,
          letterSpacing: '0.2em', textTransform: 'uppercase',
          color: 'var(--sakura-gold-dk)', marginBottom: 6,
        }}>Acento · Shippori Mincho cursiva</div>
        <div style={{
          fontFamily: 'var(--font-accent)', fontStyle: 'italic', fontWeight: 700,
          fontSize: 30, color: 'var(--sakura-rose)', lineHeight: 1.1,
        }}>llévate 1 gratis</div>
        <div style={{
          fontFamily: 'var(--font-accent)', fontSize: 14, fontWeight: 500,
          color: 'var(--sakura-gold)', marginTop: 4,
        }}>桜 · こんにちは · ありがとう</div>
      </div>

      {/* Body — DM Sans */}
      <div style={{
        padding: 16, borderRadius: 14,
        background: 'var(--sakura-paper)',
        border: '1px solid rgba(184,153,104,0.2)',
      }}>
        <div style={{
          fontFamily: 'var(--font-display)', fontSize: 10, fontWeight: 600,
          letterSpacing: '0.2em', textTransform: 'uppercase',
          color: 'var(--sakura-gold-dk)', marginBottom: 6,
        }}>Cuerpo · DM Sans 400 / 500 / 700</div>
        <div style={{ fontFamily: 'var(--font-body)', fontSize: 14, color: 'var(--sakura-char)', lineHeight: 1.55 }}>
          Por cada 10 menús completos llévate 1 menú gratis. La tarjeta se sellará después de pagar la cuenta.
        </div>
        <div style={{
          marginTop: 8,
          display: 'flex', gap: 10,
          fontFamily: 'var(--font-body)', fontSize: 12, color: 'var(--sakura-gold-dk)',
          fontVariantNumeric: 'tabular-nums',
        }}>
          <span>12:00 – 16:00</span>
          <span>·</span>
          <span>20:00 – 23:30</span>
          <span>·</span>
          <span>948 952 818</span>
        </div>
      </div>

      <div style={{
        fontFamily: 'var(--font-body)', fontSize: 11,
        color: 'var(--sakura-gold-dk)', lineHeight: 1.5,
        padding: '8px 4px 0',
      }}>
        <b style={{ color: 'var(--sakura-char)' }}>Por qué esta combinación:</b>{' '}
        Las formas geométricas y gruesas en minúscula de Outfit reflejan casi al milímetro el logotipo impreso.
        Shippori Mincho aporta una voz serif japonesa tradicional para los acentos emocionales.
        DM Sans se encarga del texto funcional en tamaños pequeños sin competir.
      </div>
    </div>
  </SheetCard>
);

const CSSVarsCard = () => (
  <SheetCard title="Variables CSS" subtitle="Pégalas en :root para cambiar el tema al instante">
    <pre style={{
      margin: 0, padding: 16,
      background: 'var(--staff-bg)', color: '#F4E7DA',
      borderRadius: 12,
      fontFamily: 'ui-monospace, "SF Mono", Menlo, monospace',
      fontSize: 11, lineHeight: 1.65,
      overflowX: 'auto',
      border: '1px solid rgba(184,153,104,0.25)',
    }}>
{`:root {
  /* Extracted from the physical card */
  --sakura-blush:    #F4C5C7;  `}<span style={{ color: '#8FA8C9' }}>{`/* card edges */`}</span>{`
  --sakura-cream:    #FBE6E2;  `}<span style={{ color: '#8FA8C9' }}>{`/* card center */`}</span>{`
  --sakura-petal:    #E89AA0;  `}<span style={{ color: '#8FA8C9' }}>{`/* deep blossom */`}</span>{`
  --sakura-rose:     #C46B73;  `}<span style={{ color: '#8FA8C9' }}>{`/* petal cores */`}</span>{`
  --sakura-gold:     #B89968;  `}<span style={{ color: '#8FA8C9' }}>{`/* wordmark */`}</span>{`
  --sakura-gold-dk:  #8A7148;  `}<span style={{ color: '#8FA8C9' }}>{`/* gold body */`}</span>{`
  --sakura-ink:      #2F5BAA;  `}<span style={{ color: '#8FA8C9' }}>{`/* stamp blue */`}</span>{`
  --sakura-ink-dk:   #1E3F7E;  `}<span style={{ color: '#8FA8C9' }}>{`/* stamp shade */`}</span>{`
  --sakura-char:     #1A1815;  `}<span style={{ color: '#8FA8C9' }}>{`/* warm black */`}</span>{`
  --sakura-paper:    #FFFDFB;  `}<span style={{ color: '#8FA8C9' }}>{`/* paper white */`}</span>{`

  /* Type */
  --font-display: 'Outfit', sans-serif;
  --font-accent:  'Shippori Mincho', serif;
  --font-body:    'DM Sans', sans-serif;
}`}
    </pre>
  </SheetCard>
);

const StampStudyCard = () => (
  <SheetCard title="El sello" subtitle="Del tampón de goma al vector">
    <div style={{
      display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 14,
    }}>
      {[
        { title: 'Vacío',      fill: false, idx: 0, bg: 'linear-gradient(180deg, var(--sakura-cream), var(--sakura-blush))' },
        { title: 'Sellado',    fill: true,  idx: 3, bg: 'linear-gradient(180deg, var(--sakura-cream), var(--sakura-blush))' },
        { title: 'En personal', fill: true, idx: 5, bg: 'linear-gradient(180deg, #221C15, var(--staff-bg))' },
      ].map((s, i) => (
        <div key={i} style={{
          aspectRatio: '1 / 1',
          background: s.bg,
          borderRadius: 14,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          border: '1px solid rgba(184,153,104,0.2)',
          position: 'relative', overflow: 'hidden',
        }}>
          <StampGlyph filled={s.fill} index={s.idx} size={80} />
          <span style={{
            position: 'absolute', bottom: 8, left: 0, right: 0, textAlign: 'center',
            fontFamily: 'var(--font-display)', fontSize: 9, fontWeight: 600,
            letterSpacing: '0.18em', textTransform: 'uppercase',
            color: i === 2 ? 'var(--sakura-gold)' : 'var(--sakura-gold-dk)',
          }}>{s.title}</span>
        </div>
      ))}
    </div>
    <div style={{
      marginTop: 14,
      fontFamily: 'var(--font-body)', fontSize: 12,
      color: 'var(--sakura-gold-dk)', lineHeight: 1.55,
    }}>
      The actual photo of the cobalt rubber stamp, paper-background removed and ink-pixels
      thresholded to alpha. Each cell is rotated a deterministic ±11° to feel hand-pressed,
      sits on a white paper halo, and adds a cobalt drop-shadow when filled.
      Empty cells use the same artwork desaturated to near-white for a subtle silhouette.
    </div>
  </SheetCard>
);

/* shared sheet card */
const SheetCard = ({ title, subtitle, children }) => (
  <div style={{
    background: 'rgba(255,253,251,0.78)',
    border: '1px solid rgba(184,153,104,0.22)',
    borderRadius: 22,
    padding: 22,
    boxShadow: '0 10px 30px -16px rgba(40,20,10,0.18)',
    backdropFilter: 'blur(6px)',
  }}>
    <div style={{ marginBottom: 16 }}>
      <div style={{
        fontFamily: 'var(--font-display)', fontSize: 10, fontWeight: 600,
        letterSpacing: '0.22em', textTransform: 'uppercase',
        color: 'var(--sakura-gold-dk)', marginBottom: 4,
      }}>System</div>
      <div style={{
        fontFamily: 'var(--font-display)', fontWeight: 800, fontSize: 22,
        color: 'var(--sakura-char)', letterSpacing: '-0.015em',
      }}>{title}</div>
      <div style={{
        fontFamily: 'var(--font-body)', fontSize: 12, color: 'var(--sakura-gold-dk)',
        marginTop: 2,
      }}>{subtitle}</div>
    </div>
    {children}
  </div>
);

const KeyframesAndStyles = () => (
  <style>{`
    @keyframes scan-sweep {
      0%   { transform: translateY(0); opacity: 0; }
      10%  { opacity: 1; }
      90%  { opacity: 1; }
      100% { transform: translateY(220px); opacity: 0; }
    }
    @keyframes stamp-press {
      0%   { transform: scale(1.6) rotate(-8deg); opacity: 0; }
      40%  { transform: scale(0.85) rotate(2deg); opacity: 1; }
      70%  { transform: scale(1.08) rotate(-1deg); }
      100% { transform: scale(1) rotate(0); }
    }
    @keyframes slide-up {
      0%   { transform: translateY(40%); opacity: 0; }
      100% { transform: translateY(0); opacity: 1; }
    }
    @keyframes petal-fall {
      0%   { transform: translateY(0) rotate(0); opacity: 0; }
      10%  { opacity: 0.9; }
      100% { transform: translateY(120vh) rotate(720deg); opacity: 0.4; }
    }
    /* responsive: stack phones if viewport narrow */
    @media (max-width: 900px) {
      h1 { font-size: 32px !important; }
    }
  `}</style>
);

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