// ─────────────────────────────────────────────────────────────────
// SIKKA ROYALE — Theme: palette, type, atoms, ornaments
// Warm midnight ink. Royal gold. Pearl text. Oxblood accent.
// Filigree corners, 8-point coin mark, rub-el-hizb dividers.
// ─────────────────────────────────────────────────────────────────

const R = {
  // surfaces (warm dark, never pure black)
  void:   '#05080a',
  ink:    '#0a0e10',    // base background — cool ink with faint mint undertone
  ink2:   '#10161a',    // panel
  ink3:   '#1a2227',    // raised
  ink4:   '#243038',
  velvet: '#0e1c18',    // mint-tinted dark hero panel
  velvetD:'#091410',

  // hairline (mint at low alpha)
  hair:   'rgba(74,155,122,0.12)',
  hair2:  'rgba(74,155,122,0.22)',
  hair3:  'rgba(74,155,122,0.34)',
  hairS:  'rgba(239,231,212,0.06)', // sand hair, even subtler
  hairInset:'rgba(74,155,122,0.20)', // inner mint hairline (inset box-shadow stroke)

  // BRAND MINT — replaces "gold" semantically; key names kept for compat
  gold:   '#4a9b7a',
  goldH:  '#6dc6a0',    // highlight
  goldD:  '#2f6e54',    // shadow
  goldDk: '#1d4a39',
  goldDim:'rgba(74,155,122,0.14)',
  goldDim2:'rgba(74,155,122,0.30)',
  goldInk:'#06120e',    // ink on mint (deep teal-ink)

  // SAND text (replaces "pearl")
  pearl:  '#efe7d4',
  pearl2: 'rgba(239,231,212,0.78)',
  pearl3: 'rgba(239,231,212,0.55)',
  pearl4: 'rgba(239,231,212,0.36)',
  pearl5: 'rgba(239,231,212,0.20)',

  // accents (rare)
  oxblood:'#7a1c2a',     // warning / destructive only — never decorative
  emerald:'#4a9b7a',     // success === brand mint
  emeraldD:'rgba(74,155,122,0.18)',

  // bank chips
  bank: {
    adcb:    { bg: '#a31a24', tx: '#fff' },
    fab:     { bg: '#0c2d60', tx: '#fff' },
    enbd:    { bg: '#a31a24', tx: '#fff' },
    mashreq: { bg: '#c98333', tx: '#1c0c00' },
    wio:     { bg: '#2a5fd7', tx: '#fff' },
    rak:     { bg: '#9a1a24', tx: '#fff' },
    adib:    { bg: '#2da866', tx: '#06170d' },
    cbd:     { bg: '#6b3fb0', tx: '#fff' },
  },
};

const FR = {
  display: '"Cinzel", "Trajan Pro", serif',   // engraved roman, for wordmark + seals
  serif:   '"Cormorant Garamond", "Times New Roman", serif',
  sans:    '"Inter Tight", "Inter", system-ui, sans-serif',
  mono:    '"JetBrains Mono", ui-monospace, monospace',
  arab:    '"Amiri", "Noto Naskh Arabic", serif',
  arabD:   '"Aref Ruqaa", "Amiri", serif',
};

// ── Shiny gold card chip ───────────────────────────────────────
// Reusable EMV-style chip drawn with a metallic gold gradient,
// inner gridlines, and a soft specular highlight. Used on every
// card face across the app.
function GoldChip({ width = 38, height = 28, radius = 5, style }) {
  return (
    <div style={{
      width, height, borderRadius: radius, position: 'relative',
      background: `
        linear-gradient(135deg,
          #fff5c0 0%,
          #f0d27a 22%,
          #d4a23a 48%,
          #8a5d1a 72%,
          #e8c469 100%)
      `,
      boxShadow: `
        inset 0 0 0 0.5px rgba(70,40,5,0.6),
        inset 0 1px 0 rgba(255,245,210,0.7),
        inset 0 -1px 0 rgba(60,35,5,0.4),
        0 1px 2px rgba(0,0,0,0.4)
      `,
      overflow: 'hidden',
      ...style,
    }}>
      {/* Etched grid lines */}
      <svg viewBox="0 0 38 28" preserveAspectRatio="none"
        style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }} fill="none">
        <path d={`M0 ${height * 0.32} H${width} M0 ${height * 0.68} H${width} M${width * 0.34} 0 V${height} M${width * 0.66} 0 V${height}`}
          stroke="rgba(70,40,5,0.55)" strokeWidth="0.5" />
      </svg>
      {/* Specular highlight */}
      <div style={{
        position: 'absolute', inset: 0,
        background: 'linear-gradient(135deg, rgba(255,255,255,0.45) 0%, rgba(255,255,255,0) 35%, rgba(255,255,255,0) 70%, rgba(255,255,255,0.18) 100%)',
        pointerEvents: 'none',
      }} />
    </div>
  );
}

// ── Motion CSS ────────────────────────────────────────────────────
const ROYAL_CSS = `
  @keyframes r-rise      { 0% { transform: translateY(14px); opacity: 0 } 100% { transform: translateY(0); opacity: 1 } }
  @keyframes r-rise-soft { 0% { transform: translateY(8px); opacity: 0 } 100% { transform: translateY(0); opacity: 1 } }
  @keyframes r-fade      { 0% { opacity: 0 } 100% { opacity: 1 } }
  @keyframes r-blink     { 0%,100% { opacity: 1 } 50% { opacity: 0.4 } }
  @keyframes r-halo      { 0%,100% { opacity: 0.55; transform: scale(1) } 50% { opacity: 0.95; transform: scale(1.06) } }
  @keyframes r-ring      { 0% { transform: scale(0.5); opacity: 0.9 } 100% { transform: scale(2.4); opacity: 0 } }
  @keyframes r-sheen     { 0% { left: -40% } 100% { left: 140% } }
  @keyframes r-stamp     { 0% { transform: scale(1.6) rotate(-12deg); opacity: 0 } 60% { transform: scale(0.92) rotate(0deg); opacity: 1 } 80% { transform: scale(1.04) rotate(0deg) } 100% { transform: scale(1) rotate(0deg); opacity: 1 } }
  @keyframes r-spin      { 0% { transform: rotate(0deg) } 100% { transform: rotate(360deg) } }
  @keyframes r-spin-slow { 0% { transform: rotate(0deg) } 100% { transform: rotate(360deg) } }
  @keyframes r-coin-flip { 0% { transform: rotateY(0deg) } 100% { transform: rotateY(360deg) } }
  @keyframes r-caret     { 0%,49% { opacity: 1 } 50%,100% { opacity: 0 } }
  @keyframes r-pop       { 0% { transform: scale(0.7); opacity: 0 } 70% { transform: scale(1.08); opacity: 1 } 100% { transform: scale(1); opacity: 1 } }
  @keyframes r-screen-in   { 0% { opacity: 0; transform: translateX(18px) } 100% { opacity: 1; transform: translateX(0) } }
  @keyframes r-screen-back { 0% { opacity: 0; transform: translateX(-18px) } 100% { opacity: 1; transform: translateX(0) } }
  @keyframes r-modal-in  { 0% { transform: translateY(40px) scale(0.96); opacity: 0 } 100% { transform: translateY(0) scale(1); opacity: 1 } }
  @keyframes r-scrim-in  { 0% { opacity: 0 } 100% { opacity: 1 } }
  @keyframes r-shake     { 0%,100% { transform: translateX(0) } 25% { transform: translateX(-6px) } 50% { transform: translateX(6px) } 75% { transform: translateX(-3px) } }
  @keyframes r-flip-in   { 0% { transform: perspective(900px) rotateY(-90deg); opacity: 0 } 60% { transform: perspective(900px) rotateY(10deg); opacity: 1 } 100% { transform: perspective(900px) rotateY(0deg); opacity: 1 } }
  @keyframes r-dot-bounce{ 0%,80%,100% { transform: scale(0.6); opacity: 0.4 } 40% { transform: scale(1); opacity: 1 } }
  @keyframes r-glow-pulse{ 0%,100% { box-shadow: 0 0 0 0 rgba(74,155,122,0.55), 0 8px 22px rgba(74,155,122,0.15) }
                            50%    { box-shadow: 0 0 0 10px rgba(74,155,122,0),  0 8px 22px rgba(74,155,122,0.25) } }
  @keyframes r-hold-fill { 0% { stroke-dashoffset: 282 } 100% { stroke-dashoffset: 0 } }
  @keyframes r-toast-in  { 0% { transform: translateY(-30px); opacity: 0 } 100% { transform: translateY(0); opacity: 1 } }
  @keyframes r-beam      { 0% { transform: translateX(-110%) } 100% { transform: translateX(110%) } }
  @keyframes r-scanline  { 0% { transform: translateY(-100%); opacity: 0 } 20%,80% { opacity: 0.9 } 100% { transform: translateY(220%); opacity: 0 } }
  @keyframes r-card-in   { 0% { transform: translateX(40px); opacity: 0 } 100% { transform: translateX(0); opacity: 1 } }
  @keyframes r-dim-out   { 0% { opacity: 1; transform: scale(1) } 100% { opacity: 0.32; transform: scale(0.97) } }
  @keyframes r-elevate   { 0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(74,155,122,0) }
                            100% { transform: scale(1.025); box-shadow: 0 0 0 1.5px ${R.gold}, 0 14px 36px rgba(74,155,122,0.30) } }
  @keyframes r-spark     { 0% { transform: scale(0); opacity: 0 } 40% { opacity: 1 } 100% { transform: scale(1.8); opacity: 0 } }
  @keyframes r-laser-in  { 0% { stroke-dasharray: 0 400; opacity: 0 } 100% { stroke-dasharray: 400 0; opacity: 1 } }
  @keyframes r-coin-spin { 0%,100% { transform: rotate(0deg) scale(1) } 50% { transform: rotate(6deg) scale(1.04) } }
  @keyframes r-orbit     { 0% { transform: rotate(0deg) translateX(48px) rotate(0deg) } 100% { transform: rotate(360deg) translateX(48px) rotate(-360deg) } }
  @keyframes r-orbit-rev { 0% { transform: rotate(0deg) translateX(48px) rotate(0deg) } 100% { transform: rotate(-360deg) translateX(48px) rotate(360deg) } }
  @keyframes r-shimmer   { 0% { background-position: -200% 0 } 100% { background-position: 200% 0 } }
  @keyframes r-tick      { 0%,100% { transform: scaleY(0.4); opacity: 0.5 } 50% { transform: scaleY(1); opacity: 1 } }
  @keyframes r-trace     { 0% { stroke-dashoffset: 320; opacity: 0 } 30% { opacity: 1 } 100% { stroke-dashoffset: 0; opacity: 1 } }
  @keyframes r-stamp-mint{ 0% { transform: scale(2.2) rotate(-18deg); opacity: 0 } 60% { transform: scale(0.92) rotate(2deg); opacity: 1 } 100% { transform: scale(1) rotate(0deg); opacity: 1 } }
  @keyframes r-burst     { 0% { transform: scale(0); opacity: 0 } 30% { opacity: 1 } 100% { transform: scale(2.4); opacity: 0 } }
  @media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
      animation-duration: 0.001ms !important;
      animation-iteration-count: 1 !important;
      transition-duration: 0.001ms !important;
      scroll-behavior: auto !important;
    }
  }
`;

// ─── Ornaments ────────────────────────────────────────────────────

// The Sikka mark — the brand logo. A rounded rectangle "wallet" outline
// with a curved route line that kinks down to a terminal dot. Rendered
// either as a coin (with ring + halo) or bare. Mint stroke on ink.
function CoinMark({ size = 72, color = R.gold, accent = R.goldH, bare = false, style }) {
  const s = size;
  return (
    <svg viewBox="0 0 100 100" width={s} height={s} style={{ display: 'block', ...style }}>
      {!bare && (
        <React.Fragment>
          {/* Outer coin ring */}
          <circle cx="50" cy="50" r="48" fill={R.ink2} stroke={color} strokeWidth="0.7" opacity="0.9" />
          {/* Inner ring */}
          <circle cx="50" cy="50" r="44.5" fill="none" stroke={color} strokeWidth="0.35" opacity="0.5" />
          {/* Subtle gloss */}
          <ellipse cx="36" cy="32" rx="22" ry="8" fill={accent} opacity="0.06" />
        </React.Fragment>
      )}

      {/* Wallet outline — rounded rect (centred on the coin) */}
      <rect x="22" y="32" width="56" height="36" rx="9" ry="9"
        fill="none" stroke={color}
        strokeWidth={bare ? 5.4 : 3.6}
        strokeLinejoin="round" />

      {/* Curved route line — straight then angled down */}
      <path d="M 32 48 L 46 48 L 56 56 L 64 56"
        fill="none" stroke={color}
        strokeWidth={bare ? 5.4 : 3.6}
        strokeLinecap="round" strokeLinejoin="round" />

      {/* Terminal dot */}
      <circle cx="64" cy="56" r={bare ? 4 : 2.8} fill={color} />
    </svg>
  );
}

// Filigree corner ornament — places 4 of these around a card
function FiligreeCorners({ color = R.gold, inset = 8, length = 22, opacity = 0.65 }) {
  const arm = (
    <svg width={length} height={length} viewBox="0 0 22 22" fill="none">
      <path d="M22 0 L2 0 Q0 0 0 2 L0 22" stroke={color} strokeWidth="0.7" opacity={opacity} />
      <path d="M6 0 Q9 4 9 9 L9 13 Q9 16 12 18 L18 18"
        stroke={color} strokeWidth="0.5" opacity={opacity * 0.7} />
      <circle cx="9" cy="9" r="1" fill={color} opacity={opacity} />
    </svg>
  );
  return (
    <React.Fragment>
      <div style={{ position: 'absolute', top: inset, left: inset, pointerEvents: 'none' }}>
        {arm}
      </div>
      <div style={{ position: 'absolute', top: inset, right: inset, pointerEvents: 'none',
        transform: 'scaleX(-1)' }}>
        {arm}
      </div>
      <div style={{ position: 'absolute', bottom: inset, left: inset, pointerEvents: 'none',
        transform: 'scaleY(-1)' }}>
        {arm}
      </div>
      <div style={{ position: 'absolute', bottom: inset, right: inset, pointerEvents: 'none',
        transform: 'scale(-1, -1)' }}>
        {arm}
      </div>
    </React.Fragment>
  );
}

// Section divider — fine hairlines flanking a small faceted lozenge
function StarDivider({ color = R.gold, width = '100%', dim = 0.55, style }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'center', gap: 14, width, ...style,
    }}>
      <div style={{ flex: 1, height: 1,
        background: `linear-gradient(90deg, transparent, ${color}66 80%, ${color}aa)`,
        opacity: dim }} />
      <svg width="26" height="14" viewBox="-13 -7 26 14" style={{ opacity: dim + 0.25 }}>
        <g fill="none" stroke={color} strokeWidth="0.7" strokeLinecap="round">
          <line x1="-11" y1="0" x2="-5" y2="0" opacity="0.6" />
          <line x1="11" y1="0" x2="5" y2="0" opacity="0.6" />
        </g>
        <g fill={color} stroke={color} strokeWidth="0.5">
          <rect x="-2.2" y="-2.2" width="4.4" height="4.4" transform="rotate(45)" />
        </g>
      </svg>
      <div style={{ flex: 1, height: 1,
        background: `linear-gradient(270deg, transparent, ${color}66 80%, ${color}aa)`,
        opacity: dim }} />
    </div>
  );
}

// Wordmark — lowercase serif "sikka" with the brand mark beside it.
// Matches the official Sikka wordmark: light serif, lowercase, with the
// wallet logo as a leading mark.
function Wordmark({ size = 28, color = R.pearl, sub = false, subColor = R.gold,
  subText = 'NOIR EDITION', withMark = false, style }) {
  return (
    <div style={{ display: 'inline-flex', flexDirection: 'column',
      alignItems: 'center', lineHeight: 0.9, ...style }}>
      <div style={{ display: 'inline-flex', alignItems: 'center', gap: size * 0.36 }}>
        {withMark && <CoinMark size={size * 1.05} color={color} bare />}
        <span style={{
          fontFamily: FR.serif, fontSize: size * 1.4, color,
          letterSpacing: -size * 0.020, fontWeight: 500,
          textTransform: 'lowercase', lineHeight: 0.9,
        }}>sikka</span>
      </div>
      {sub && (
        <span style={{
          fontFamily: FR.mono, fontSize: Math.max(8, size * 0.30), color: subColor,
          marginTop: size * 0.42, letterSpacing: 3.4, textTransform: 'uppercase',
          fontWeight: 500,
        }}>{subText}</span>
      )}
    </div>
  );
}

// Background pattern — refined engine-turned dot grid with faint diagonal
// pinstripes. Reads as a luxury banknote / private-bank texture, neutral
// for any audience. (Renamed conceptually from "mashrabiya"; export name
// kept for compatibility with screens that import it.)
function MashrabiyaBG({ opacity = 0.05, color = R.gold, style }) {
  const uid = React.useId ? React.useId().replace(/:/g, '') : 'lp' + Math.random().toString(36).slice(2, 8);
  return (
    <svg style={{ position: 'absolute', inset: 0, width: '100%', height: '100%',
      pointerEvents: 'none', ...style }} aria-hidden="true">
      <defs>
        <pattern id={'lux-' + uid} patternUnits="userSpaceOnUse" width="36" height="36">
          {/* faint diagonal pinstripe */}
          <line x1="0" y1="36" x2="36" y2="0"
            stroke={color} strokeWidth="0.4" opacity={opacity * 0.55} />
          {/* fine dot grid */}
          <circle cx="18" cy="18" r="0.55" fill={color} opacity={opacity * 1.6} />
          <circle cx="0" cy="0" r="0.4" fill={color} opacity={opacity} />
          <circle cx="36" cy="0" r="0.4" fill={color} opacity={opacity} />
          <circle cx="0" cy="36" r="0.4" fill={color} opacity={opacity} />
          <circle cx="36" cy="36" r="0.4" fill={color} opacity={opacity} />
        </pattern>
      </defs>
      <rect width="100%" height="100%" fill={`url(#lux-${uid})`} />
    </svg>
  );
}

// Royal seal — concentric medallion with engraved perimeter text.
// Universal Roman engraving; cultural neutrality preserved.
function RoyalSeal({ size = 72, color = R.gold, motion = false, style }) {
  const uid = React.useId ? React.useId().replace(/:/g, '') : 'rs' + Math.random().toString(36).slice(2, 8);
  return (
    <div style={{
      position: 'relative', width: size, height: size, ...style,
    }}>
      {/* rotating outer ring of engraved text */}
      <svg viewBox="-50 -50 100 100" width={size} height={size}
        style={{ position: 'absolute', inset: 0,
          animation: motion ? 'r-spin-slow 38s linear infinite' : 'none' }}>
        <defs>
          <path id={'sealPath-' + uid} d="M 0,-40 a 40,40 0 1,1 -0.01,0" fill="none" />
        </defs>
        <circle r="40" fill="none" stroke={color} strokeWidth="0.6" />
        <circle r="34" fill="none" stroke={color} strokeWidth="0.4" opacity="0.6" />
        <text fontSize="6.2" fill={color} fontFamily={FR.display.replace(/^"|"$/g, '')}
          letterSpacing="3.2" fontWeight="600">
          <textPath href={'#sealPath-' + uid}>
            ◆ SIKKA · NOIR EDITION · ESTD 2026 · PRIVATE LEDGER ◆
          </textPath>
        </text>
      </svg>
      {/* center coin */}
      <div style={{ position: 'absolute', inset: '18%' }}>
        <CoinMark size={size * 0.64} color={color} />
      </div>
    </div>
  );
}

// ─── Type atoms ───────────────────────────────────────────────────

function Eyebrow({ children, color = R.pearl4, size = 10, ls = 2.6, style }) {
  return (
    <div style={{
      fontFamily: FR.mono, fontSize: size, color, fontWeight: 500,
      letterSpacing: ls, textTransform: 'uppercase', ...style,
    }}>{children}</div>
  );
}

// Big serif heading with trailing accent word italic & gold
function Headline({ lead, tail, tailPunct = '', size = 42, color = R.pearl, accent = R.gold, style }) {
  return (
    <h1 style={{
      fontFamily: FR.serif, fontSize: size, color, margin: 0,
      letterSpacing: -size * 0.014, lineHeight: 1.04, fontWeight: 500,
      textWrap: 'pretty', ...style,
    }}>
      {lead}{' '}
      <span style={{ color: accent, fontStyle: 'italic' }}>{tail}</span>{tailPunct}
    </h1>
  );
}

function Sub({ children, color = R.pearl3, size = 16, style }) {
  return (
    <div style={{
      fontFamily: FR.serif, fontSize: size, color, lineHeight: 1.42,
      letterSpacing: 0, fontWeight: 400, ...style,
    }}>{children}</div>
  );
}

// Bilingual line — English + Arabic stacked, Arabic dimmer
function Bilingual({ en, ar, size = 14, gap = 6, color = R.pearl3, arColor = R.gold, style, align = 'left' }) {
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap, alignItems:
      align === 'center' ? 'center' : align === 'right' ? 'flex-end' : 'flex-start', ...style }}>
      <div style={{ fontFamily: FR.serif, fontSize: size, color, letterSpacing: 0, lineHeight: 1.25 }}>
        {en}
      </div>
      <div style={{ fontFamily: FR.arab, fontSize: size * 0.92, color: arColor,
        direction: 'rtl', opacity: 0.75, lineHeight: 1.25 }}>{ar}</div>
    </div>
  );
}

// Top bar — back / center / right
function TopBar({ onBack, center, right, sticky = false }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: '40px 1fr 40px',
      alignItems: 'center', padding: '14px 22px 6px',
      position: sticky ? 'sticky' : 'relative', top: 0, zIndex: 5,
      background: sticky ? R.ink : 'transparent',
    }}>
      <div>{onBack ? <BackButton onClick={onBack} /> : null}</div>
      <div style={{ textAlign: 'center' }}>
        {typeof center === 'string'
          ? <Eyebrow color={R.pearl3} size={10.5} ls={3.2}>{center}</Eyebrow>
          : center}
      </div>
      <div style={{ textAlign: 'right' }}>{right || null}</div>
    </div>
  );
}

function BackButton({ onClick }) {
  return (
    <button onClick={onClick} aria-label="Back" style={{
      width: 36, height: 36, padding: 0, cursor: 'pointer',
      background: R.ink2, border: `0.5px solid ${R.hair}`,
      borderRadius: '50%', color: R.gold,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
    }}>
      <svg width="18" height="18" viewBox="0 0 20 20" fill="none">
        <path d="M13 4L7 10L13 16" stroke="currentColor" strokeWidth="1.6"
          strokeLinecap="round" strokeLinejoin="round" />
      </svg>
    </button>
  );
}

// Primary gold button — gilded with subtle sheen + filigree edges
function GoldButton({ children, onClick, disabled, full = true, style, ...rest }) {
  return (
    <button onClick={onClick} disabled={disabled}
      style={{
        position: 'relative', overflow: 'hidden',
        width: full ? '100%' : 'auto',
        padding: '17px 22px', border: 'none',
        color: R.goldInk,
        background: disabled
          ? 'linear-gradient(180deg, rgba(74,155,122,0.4) 0%, rgba(47,110,84,0.5) 100%)'
          : `linear-gradient(180deg, ${R.goldH} 0%, ${R.gold} 50%, ${R.goldD} 100%)`,
        boxShadow: disabled ? 'none' :
          '0 14px 32px rgba(74,155,122,0.25), inset 0 0 0 0.5px rgba(220,255,235,0.6), inset 0 -2px 0 rgba(8,28,18,0.35)',
        borderRadius: 14,
        fontFamily: FR.display, fontSize: 14, letterSpacing: 3.6,
        textTransform: 'uppercase', fontWeight: 600,
        cursor: disabled ? 'not-allowed' : 'pointer',
        ...style,
      }} {...rest}>
      <span style={{ position: 'relative', zIndex: 1 }}>{children}</span>
      {!disabled && (
        <span style={{
          position: 'absolute', top: 0, bottom: 0, width: '34%',
          left: '-40%',
          background: 'linear-gradient(90deg, transparent, rgba(225,255,240,0.5), transparent)',
          animation: 'r-sheen 4s 1s ease-in-out infinite', pointerEvents: 'none',
        }} />
      )}
    </button>
  );
}

// Ghost button — velvet panel with gold border
function GhostButton({ children, onClick, style, ...rest }) {
  return (
    <button onClick={onClick}
      style={{
        width: '100%', padding: '16px 22px',
        background: R.ink2, color: R.pearl,
        border: `0.5px solid ${R.hair2}`,
        borderRadius: 14, fontFamily: FR.display, fontSize: 13,
        letterSpacing: 3.4, textTransform: 'uppercase', fontWeight: 500,
        cursor: 'pointer', ...style,
      }} {...rest}>
      {children}
    </button>
  );
}

// Bank avatar — colored rounded square with mono initials, gold hairline
function BankAvatar({ id, size = 38, text }) {
  const b = R.bank[id] || { bg: R.ink3, tx: R.pearl };
  return (
    <div style={{
      width: size, height: size, borderRadius: size * 0.30,
      background: b.bg, color: b.tx,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      fontFamily: FR.mono, fontSize: size * 0.30, fontWeight: 700,
      letterSpacing: 0.6, flexShrink: 0,
      boxShadow: `inset 0 0 0 0.5px ${R.hairInset}`,
    }}>{text}</div>
  );
}

// Stepper "I / IV" Roman numerals — feels engraved
function StepCounter({ i, total }) {
  const roman = (n) => ['', 'I','II','III','IV','V','VI','VII','VIII','IX','X'][n] || String(n);
  return (
    <span style={{ fontFamily: FR.mono, fontSize: 11, letterSpacing: 3, color: R.pearl3 }}>
      <span style={{ color: R.gold }}>{roman(i)}</span>
      <span style={{ color: R.pearl5, margin: '0 6px' }}>/</span>
      {roman(total)}
    </span>
  );
}

// Progress bar — gold fill
function ProgressBar({ pct = 0, color = R.gold, height = 4, style }) {
  return (
    <div style={{
      width: '100%', height, background: R.hair, borderRadius: 999,
      overflow: 'hidden', ...style,
    }}>
      <div style={{
        height: '100%', width: pct + '%', borderRadius: 999,
        background: `linear-gradient(90deg, ${R.goldD}, ${R.gold} 50%, ${R.goldH})`,
        boxShadow: `0 0 12px ${R.gold}66`,
        transition: 'width 600ms cubic-bezier(.2,.7,.2,1)',
      }} />
    </div>
  );
}

// Animated count-up
function CountUp({ to, decimals = 0, duration = 700, style, prefix, suffix }) {
  const [val, setVal] = React.useState(0);
  React.useEffect(() => {
    const start = performance.now();
    let raf;
    const tick = (t) => {
      const p = Math.min(1, (t - start) / duration);
      const e = 1 - Math.pow(1 - p, 3);
      setVal(to * e);
      if (p < 1) raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [to, duration]);
  return (
    <span style={style}>
      {prefix}{decimals ? val.toFixed(decimals) : Math.round(val).toLocaleString()}{suffix}
    </span>
  );
}

// Hidden numeric input that drives OTP boxes
function HiddenNumInput({ value, onChange, max = 6, autoFocus }) {
  return (
    <input
      type="tel"
      inputMode="numeric"
      maxLength={max}
      value={value}
      onChange={(e) => onChange(e.target.value.replace(/\D/g, '').slice(0, max))}
      autoFocus={autoFocus}
      style={{
        position: 'absolute', opacity: 0, pointerEvents: 'auto',
        inset: 0, width: '100%', height: '100%', border: 'none', background: 'transparent',
        color: 'transparent', caretColor: 'transparent', fontSize: 16,
      }}
    />
  );
}

// Color shade helper
function shade(hex, amt) {
  const h = hex.replace('#', '');
  const r = parseInt(h.slice(0, 2), 16);
  const g = parseInt(h.slice(2, 4), 16);
  const b = parseInt(h.slice(4, 6), 16);
  const clamp = (n) => Math.max(0, Math.min(255, n));
  const f = (c) => clamp(c + amt).toString(16).padStart(2, '0');
  return '#' + f(r) + f(g) + f(b);
}

// "v1.2" gold version badge
function VBadge({ children = 'v1.2', style }) {
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center',
      padding: '3px 8px', borderRadius: 4,
      background: R.goldDim, color: R.gold,
      fontFamily: FR.mono, fontSize: 9.5, letterSpacing: 1.6,
      textTransform: 'uppercase', fontWeight: 500,
      border: `0.5px solid ${R.gold}55`,
      ...style,
    }}>{children}</span>
  );
}

// Tier chip — used in Wallet + Profile
function TierChip({ tier = 'Noir', dot = R.gold }) {
  return (
    <span style={{
      display: 'inline-flex', alignItems: 'center', gap: 6,
      padding: '4px 10px 4px 8px', borderRadius: 999,
      background: R.goldDim, border: `0.5px solid ${R.gold}66`,
      fontFamily: FR.display, fontSize: 9.5, color: R.gold,
      letterSpacing: 2.4, textTransform: 'uppercase', fontWeight: 600,
    }}>
      <span style={{ width: 5, height: 5, borderRadius: '50%', background: dot,
        boxShadow: `0 0 6px ${dot}` }} />
      {tier} Tier
    </span>
  );
}

// ── Real brand logos (investor demo · nominative use). Renders the brand
// mark on a clean tile; if the logo can't load it falls back to the neutral
// monogram, so this is non-breaking even with no network.
const BRAND_DOMAIN = {
  'Carrefour': 'carrefour.com', 'Talabat': 'talabat.com',
  'Emirates': 'emirates.com', 'ADNOC': 'adnoc.ae',
  'Noon': 'noon.com', 'Spinneys': 'spinneys.com',
  'Marriott': 'marriott.com', 'VOX Cinemas': 'voxcinemas.com',
  'Atlantis': 'atlantis.com', 'WhatsApp': 'whatsapp.com',
};
function brandLogoSrcs(domain) {
  if (!domain) return [];
  // High-res app icons first (brand's own background colour, full-bleed, crisp).
  // DuckDuckGo ip3 serves the apple-touch-icon (~180px); Google at 256px; Clearbit last.
  return [
    `https://icons.duckduckgo.com/ip3/${domain}.ico`,
    `https://www.google.com/s2/favicons?domain=${domain}&sz=256`,
    `https://logo.clearbit.com/${domain}`,
  ];
}
function BrandTile({ size = 52, radius = 12, domain, initial, bg = R.ink2, tx = '#fff' }) {
  // WhatsApp's website favicon is green-on-white; the brand app icon is
  // white-on-green. Render the canonical white mark on WhatsApp green.
  if (domain === 'whatsapp.com') {
    return (
      <div style={{
        width: size, height: size, borderRadius: radius, flexShrink: 0,
        background: '#25D366', display: 'flex', alignItems: 'center', justifyContent: 'center',
        boxShadow: 'inset 0 0 0 0.5px rgba(0,0,0,0.14)',
      }}>
        <svg viewBox="0 0 24 24" width={Math.round(size * 0.62)} height={Math.round(size * 0.62)} fill="#fff">
          <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51l-.57-.01c-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 0 1-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 0 1-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 0 1 2.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0 0 12.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 0 0 5.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 0 0-3.48-8.413Z"/>
        </svg>
      </div>
    );
  }
  const srcs = brandLogoSrcs(domain);
  const [idx, setIdx] = React.useState(0);
  const hasLogo = idx < srcs.length;
  return (
    <div style={{
      width: size, height: size, borderRadius: radius, flexShrink: 0, overflow: 'hidden',
      background: bg, color: tx,
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      fontFamily: FR.mono, fontWeight: 700, fontSize: Math.round(size * 0.3), letterSpacing: 0.5,
      boxShadow: `inset 0 0 0 0.5px ${R.hairInset}`,
    }}>
      {hasLogo
        ? <img src={srcs[idx]} alt="" onError={() => setIdx(idx + 1)}
            style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }} />
        : initial}
    </div>
  );
}

Object.assign(window, {
  R, FR, ROYAL_CSS,
  CoinMark, FiligreeCorners, StarDivider, Wordmark, MashrabiyaBG, RoyalSeal,
  Eyebrow, Headline, Sub, Bilingual, TopBar, BackButton,
  GoldButton, GhostButton, BankAvatar, StepCounter, ProgressBar, CountUp,
  HiddenNumInput, shade, VBadge, TierChip, GoldChip,
  BrandTile, BRAND_DOMAIN, brandLogoSrcs,
});
