// components.jsx — Navbar, Footer, ProductCard, WhatsAppBtn (LIGHT THEME)

const { useState, useEffect, useRef } = React;
const { Link, NavLink, useLocation } = window.ReactRouterDOM;
const G = '#264A31',GOLD = '#F27D16',TEXTD = '#1A1A1A',TEXTM = '#595959';

function ShieldLogo({ size = 36, className = '' }) {
  const url = window.useRemoveBg(window.__resources ? window.__resources.logoPng : 'logo.png');
  return (
    <img src={url} alt="Crovista Shield" width={size} height={size}
    style={{ objectFit: 'contain' }} className={`cv-logo-main ${className}`} />);

}

/* ── NAVBAR ── */
function Navbar() {
  const [scrolled, setScrolled] = useState(false);
  const [mobileOpen, setMobileOpen] = useState(false);
  const location = useLocation();

  useEffect(() => {
    const h = () => setScrolled(window.scrollY > 60);
    window.addEventListener('scroll', h, { passive: true });
    return () => window.removeEventListener('scroll', h);
  }, []);
  useEffect(() => setMobileOpen(false), [location]);

  const links = [['/', 'Home'], ['/products', 'Products'], ['/about', 'About'], ['/contact', 'Contact']];

  return (
    <>
      <nav style={{
        position: 'fixed', top: 0, left: 0, right: 0, zIndex: 1000,
        height: 68, display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        padding: '0 2rem',
        background: scrolled ? 'rgba(255,255,255,0.97)' : 'rgba(255,255,255,0.95)',
        backdropFilter: 'blur(12px)',
        borderBottom: scrolled ? '1px solid rgba(38,74,49,0.12)' : '1px solid rgba(38,74,49,0.06)',
        boxShadow: scrolled ? '0 2px 20px rgba(38,74,49,0.07)' : 'none',
        transition: 'all 0.3s ease'
      }}>
        <Link to="/" style={{ display: 'flex', alignItems: 'center', gap: 10, textDecoration: 'none' }}>
          <ShieldLogo size={36} />
          <div>
            <div style={{ fontFamily: 'Montserrat, sans-serif', fontWeight: 800, fontSize: 17, color: TEXTD, letterSpacing: '0.04em', lineHeight: 1 }}>CROVISTA</div>
            <div style={{ fontFamily: 'Montserrat, sans-serif', fontWeight: 600, fontSize: 8.5, color: GOLD, letterSpacing: '0.18em', lineHeight: 1, marginTop: 2 }}>AGROCHEMICALS INDIA</div>
          </div>
        </Link>

        {/* Desktop links */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 32 }} className="cv-desktop-nav">
          {links.map(([to, label]) =>
          <NavLink key={to} to={to} end={to === '/'} className="cv-navlink"
          style={({ isActive }) => ({ color: isActive ? G : TEXTD, fontFamily: 'Inter, sans-serif', fontWeight: 500, fontSize: 14, textDecoration: 'none', position: 'relative', paddingBottom: 3, transition: 'color 0.2s' })}>
              {label}
            </NavLink>
          )}
          <Link to="/contact" style={{ background: G, color: '#fff', padding: '9px 22px', borderRadius: 999, fontFamily: 'Inter, sans-serif', fontWeight: 600, fontSize: 14, textDecoration: 'none', transition: 'all 0.2s ease', boxShadow: '0 2px 10px rgba(38,74,49,0.2)' }} className="cv-cta-btn">
            Get in Touch
          </Link>
        </div>

        {/* Hamburger */}
        <button onClick={() => setMobileOpen(!mobileOpen)} className="cv-hamburger"
        style={{ background: 'none', border: 'none', cursor: 'pointer', padding: 8, display: 'none', flexDirection: 'column', gap: 5, minWidth: 44, minHeight: 44, alignItems: 'center', justifyContent: 'center' }}>
          {[0, 1, 2].map((i) =>
          <span key={i} style={{ display: 'block', width: 22, height: 2, background: TEXTD, borderRadius: 2, transition: 'all 0.3s', transform: mobileOpen ? i === 0 ? 'rotate(45deg) translate(5px,5px)' : i === 2 ? 'rotate(-45deg) translate(5px,-5px)' : 'none' : 'none', opacity: mobileOpen && i === 1 ? 0 : 1 }} />
          )}
        </button>
      </nav>

      {/* Mobile overlay */}
      <div style={{ position: 'fixed', inset: 0, zIndex: 999, background: 'rgba(255,255,255,0.98)', backdropFilter: 'blur(16px)', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 32, transform: mobileOpen ? 'translateX(0)' : 'translateX(100%)', transition: 'transform 0.35s cubic-bezier(0.77,0,0.175,1)' }}>
        {links.map(([to, label], i) =>
        <NavLink key={to} to={to} end={to === '/'} onClick={() => setMobileOpen(false)}
        style={({ isActive }) => ({ fontFamily: 'Montserrat, sans-serif', fontWeight: 800, fontSize: 30, color: isActive ? G : TEXTD, textDecoration: 'none', opacity: mobileOpen ? 1 : 0, transform: mobileOpen ? 'none' : 'translateY(16px)', transition: `all 0.4s ease ${i * 0.06}s` })}>
            {label}
          </NavLink>
        )}
        <Link to="/contact" onClick={() => setMobileOpen(false)} style={{ background: G, color: '#fff', padding: '13px 36px', borderRadius: 999, fontFamily: 'Montserrat, sans-serif', fontWeight: 700, fontSize: 15, textDecoration: 'none', marginTop: 8 }}>Get in Touch</Link>
      </div>
    </>);

}

/* ── FOOTER ── */
function Footer() {
  return (
    <footer style={{ background: G, paddingTop: 56 }}>
      <div style={{ maxWidth: 1200, margin: '0 auto', padding: '0 2rem' }}>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit,minmax(190px,1fr))', gap: 44, paddingBottom: 44 }}>
          {/* Brand */}
          <div>
            <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', gap: 6, marginBottom: 16 }}>
              <div>
                <div style={{ fontFamily: 'Montserrat, sans-serif', fontWeight: 800, fontSize: 16, color: '#fff', letterSpacing: '0.05em' }}>CROVISTA</div>
                <div style={{ fontFamily: 'Montserrat, sans-serif', fontWeight: 500, fontSize: 8, color: 'rgba(255,255,255,0.65)', letterSpacing: '0.15em' }}>AGROCHEMICALS INDIA</div>
              </div>
            </div>
            <p style={{ color: 'rgba(255,255,255,0.68)', fontFamily: 'Inter, sans-serif', fontSize: 13, lineHeight: 1.7, marginBottom: 14 }}>Advanced agrochemical solutions trusted by farmers across India.</p>
            <p style={{ color: GOLD, fontFamily: 'Montserrat, sans-serif', fontWeight: 700, fontSize: 11, letterSpacing: '0.12em' }}>FROM THE LAB. FOR THE LAND.</p>
          </div>

          {/* Quick Links */}
          <div>
            <h4 style={{ color: 'rgba(255,255,255,0.5)', fontFamily: 'Montserrat, sans-serif', fontWeight: 700, fontSize: 11, letterSpacing: '0.2em', marginBottom: 18 }}>QUICK LINKS</h4>
            {[['Home', '/'], ['Products', '/products'], ['About Us', '/about'], ['Contact', '/contact']].map(([l, t]) =>
            <div key={t} style={{ marginBottom: 10 }}>
                <Link to={t} style={{ color: 'rgba(255,255,255,0.75)', fontFamily: 'Inter, sans-serif', fontSize: 14, textDecoration: 'none' }} className="cv-footer-link">{l}</Link>
              </div>
            )}
          </div>

          {/* Products */}
          <div>
            <h4 style={{ color: 'rgba(255,255,255,0.5)', fontFamily: 'Montserrat, sans-serif', fontWeight: 700, fontSize: 11, letterSpacing: '0.2em', marginBottom: 18 }}>PRODUCTS</h4>
            {window.PRODUCTS.map((p) =>
            <div key={p.id} style={{ marginBottom: 10 }}>
                <Link to={`/products/${p.slug}`} style={{ color: 'rgba(255,255,255,0.75)', fontFamily: 'Inter, sans-serif', fontSize: 14, textDecoration: 'none' }} className="cv-footer-link">{p.name}</Link>
              </div>
            )}
          </div>

          {/* Contact */}
          <div>
            <h4 style={{ color: 'rgba(255,255,255,0.5)', fontFamily: 'Montserrat, sans-serif', fontWeight: 700, fontSize: 11, letterSpacing: '0.2em', marginBottom: 18 }}>CONTACT</h4>
            <a href="mailto:info@crovista.in" style={{ color: 'rgba(255,255,255,0.75)', fontFamily: 'Inter, sans-serif', fontSize: 14, textDecoration: 'none', display: 'block', marginBottom: 10 }}>info@crovista.in</a>
            <a href="tel:+918699860146" style={{ color: 'rgba(255,255,255,0.75)', fontFamily: 'Inter, sans-serif', fontSize: 14, textDecoration: 'none', display: 'block', marginBottom: 20 }}>+91 86998 60146</a>
            <a href="https://wa.me/918699860146" target="_blank" rel="noopener noreferrer" style={{ display: 'inline-flex', alignItems: 'center', gap: 8, background: 'rgba(255,255,255,0.15)', color: '#fff', padding: '9px 18px', borderRadius: 999, fontFamily: 'Inter, sans-serif', fontWeight: 600, fontSize: 13, textDecoration: 'none', border: '1px solid rgba(255,255,255,0.25)', transition: 'all 0.2s ease' }}>
              <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor"><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-.51-.173-.008-.371-.01-.57-.01-.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 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-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 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.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 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z" /></svg>
              WhatsApp Us
            </a>
          </div>
        </div>

        {/* Bottom bar */}
        <div style={{ borderTop: '1px solid rgba(255,255,255,0.15)', padding: '18px 0', display: 'flex', alignItems: 'center', justifyContent: 'space-between', flexWrap: 'wrap', gap: 10 }}>
          <p style={{ color: 'rgba(255,255,255,0.45)', fontFamily: 'Inter, sans-serif', fontSize: 12 }}>© 2024 CROVISTA Agrochemicals India Pvt. Ltd. All rights reserved.</p>
          <div style={{ display: 'flex', gap: 14, alignItems: 'center' }}>
            <span style={{ color: GOLD, fontFamily: 'Montserrat, sans-serif', fontWeight: 700, fontSize: 11, letterSpacing: '0.14em', border: `1px solid rgba(242,125,22,0.45)`, padding: '3px 10px', borderRadius: 4 }}>ISO 9001:2015</span>
            <span style={{ color: 'rgba(255,255,255,0.35)', fontSize: 12, fontFamily: 'Inter, sans-serif' }}>Made in India 🇮🇳</span>
          </div>
        </div>
      </div>
    </footer>);

}

/* ── PRODUCT CARD — equal height via flex column ── */
function ProductCard({ product }) {
  const [hovered, setHovered] = useState(false);
  const catColor = { Insecticide: G, Fungicide: '#6B854A', Fertilizer: GOLD }[product.category] || G;

  return (
    <Link to={`/products/${product.slug}`} style={{ textDecoration: 'none', display: 'flex', width: '100%', height: '100%' }}>
      <div onMouseEnter={() => setHovered(true)} onMouseLeave={() => setHovered(false)}
      style={{ background: '#fff', borderRadius: 16, border: `1.5px solid ${hovered ? catColor : 'rgba(38,74,49,0.1)'}`, boxShadow: hovered ? '0 16px 40px rgba(38,74,49,0.12)' : '0 2px 12px rgba(38,74,49,0.06)', transform: hovered ? 'translateY(-4px)' : 'none', transition: 'all 0.28s cubic-bezier(0.34,1.56,0.64,1)', overflow: 'hidden', cursor: 'pointer', display: 'flex', flexDirection: 'column', width: '100%', height: '100%' }}>
        {/* Header */}
        <div style={{ background: hovered ? '#1E3D2F' : G, padding: '16px 20px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 12, transition: 'background 0.25s', flexShrink: 0, height: 104 }}>
          <div style={{ minWidth: 0, flex: '1 1 auto' }}>
            <div style={{ color: '#fff', fontFamily: 'Montserrat, sans-serif', fontWeight: 700, fontSize: 17, letterSpacing: '0.02em', display: '-webkit-box', WebkitLineClamp: 2, WebkitBoxOrient: 'vertical', overflow: 'hidden', lineHeight: 1.25 }}>{product.name}</div>
            <div style={{ color: '#F0E6D2', fontFamily: 'Inter, sans-serif', fontSize: 12, marginTop: 3, lineHeight: 1.4 }}>{product.tagline}</div>
          </div>
        </div>

        {/* Body — flex:1 so all cards stretch equally */}
        <div style={{ padding: '18px 20px', display: 'flex', flexDirection: 'column', flex: 1 }}>
          <p style={{ color: TEXTM, fontFamily: 'Inter, sans-serif', fontSize: 13, lineHeight: 1.6, marginBottom: 14 }}>
            {product.description.slice(0, 95)}…
          </p>

          {/* Benefits */}
          <div style={{ marginBottom: 14 }}>
            {product.benefits.slice(0, 2).map((b, i) =>
            <div key={i} style={{ display: 'flex', alignItems: 'flex-start', gap: 8, marginBottom: 6 }}>
                <svg width="14" height="14" viewBox="0 0 14 14" style={{ flexShrink: 0, marginTop: 2 }}>
                  <circle cx="7" cy="7" r="7" fill={G} />
                  <path d="M4 7l2 2 4-4" stroke="#fff" strokeWidth="1.5" fill="none" strokeLinecap="round" strokeLinejoin="round" />
                </svg>
                <span style={{ color: TEXTD, fontFamily: 'Inter, sans-serif', fontSize: 12, lineHeight: 1.5 }}>{b.title}</span>
              </div>
            )}
          </div>

          {/* Crop tags */}
          <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, marginBottom: 18 }}>
            {product.crops.slice(0, 4).map((crop) =>
            <span key={crop} style={{ background: '#EEF5F1', color: G, padding: '3px 10px', borderRadius: 999, fontFamily: 'Inter, sans-serif', fontSize: 11, fontWeight: 500 }}>{crop}</span>
            )}
          </div>

          {/* CTA pinned to bottom */}
          <div style={{ marginTop: 'auto', display: 'flex', alignItems: 'center', justifyContent: 'center', padding: '10px', borderRadius: 8, border: `1.5px solid ${catColor}`, color: hovered ? '#fff' : catColor, background: hovered ? catColor : 'transparent', fontFamily: 'Inter, sans-serif', fontWeight: 600, fontSize: 13, transition: 'all 0.22s ease', gap: 6 }}>
            View Details
            <svg width="14" height="14" viewBox="0 0 14 14" fill="none"><path d="M3 7h8M7 3l4 4-4 4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" /></svg>
          </div>
        </div>
      </div>
    </Link>);

}

/* ── WHATSAPP BUTTON ── */
function WhatsAppBtn() {
  const [hovered, setHovered] = useState(false);
  return (
    <div style={{ position: 'fixed', bottom: 28, right: 28, zIndex: 888 }}>
      <div style={{ position: 'absolute', inset: 0, borderRadius: '50%', border: '2px solid rgba(38,74,49,0.5)', animation: 'waPulse 2.5s ease-out infinite', pointerEvents: 'none' }} />
      <div style={{ position: 'absolute', inset: 0, borderRadius: '50%', border: '2px solid rgba(38,74,49,0.3)', animation: 'waPulse 2.5s ease-out infinite 0.8s', pointerEvents: 'none' }} />
      {hovered && <div style={{ position: 'absolute', right: 64, top: '50%', transform: 'translateY(-50%)', background: TEXTD, color: '#fff', padding: '6px 12px', borderRadius: 8, fontFamily: 'Inter, sans-serif', fontSize: 13, fontWeight: 500, whiteSpace: 'nowrap', boxShadow: '0 4px 12px rgba(0,0,0,0.15)' }}>Chat with us</div>}
      <a href="https://wa.me/918699860146" target="_blank" rel="noopener noreferrer"
      onMouseEnter={() => setHovered(true)} onMouseLeave={() => setHovered(false)}
      style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', width: 56, height: 56, borderRadius: '50%', background: hovered ? '#1C3A26' : G, boxShadow: hovered ? '0 6px 24px rgba(38,74,49,0.45)' : '0 4px 16px rgba(38,74,49,0.3)', transition: 'all 0.22s ease', color: '#fff', textDecoration: 'none', transform: hovered ? 'scale(1.07)' : 'scale(1)' }}>
        <svg width="26" height="26" viewBox="0 0 24 24" fill="white"><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-.51-.173-.008-.371-.01-.57-.01-.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 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-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 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.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 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z" /></svg>
      </a>
    </div>);

}

Object.assign(window, { ShieldLogo, Navbar, Footer, ProductCard, WhatsAppBtn });