// Shared layout pieces — TopNav, Footer, Ticker, etc.
const PAGES = [
  { id: "home", labelKey: "home", file: "index.html" },
  { id: "platform", labelKey: "platform", file: "platform.html" },
  { id: "capital", labelKey: "capital", file: "capital.html" },
  { id: "cities", labelKey: "cities", file: "cities.html" },
  { id: "manifesto", labelKey: "manifesto", file: "manifesto.html" },
];

function BrandLink() {
  return <a href="index.html" style={{textDecoration:"none"}}><Logo size={48} withWord={true} /></a>;
}

function TopNav({ active }) {
  const { t } = useT();
  return (
    <header className="topnav">
      <div className="topnav__inner">
        <BrandLink />
        <nav className="navlist" aria-label="Primary">
          {PAGES.map(p => (
            <a key={p.id} href={p.file} className={p.id === active ? "active" : ""}>
              {t.nav[p.labelKey]}
            </a>
          ))}
        </nav>
        <div className="navactions">
          <LangSwitcher />
          <a className="btn btn--ink" href="capital.html">{t.nav.access} <span className="arrow">→</span></a>
        </div>
      </div>
    </header>
  );
}

function Ticker({ items }) {
  const doubled = [...items, ...items];
  return (
    <div className="ticker">
      <div className="ticker__track">
        {doubled.map((it, i) => (
          <span className="ticker__item" key={i}>
            <span className="label">{it.label}</span>
            <span className="signal mono-data">{it.value}</span>
            {it.delta && <span style={{color: it.delta.startsWith("-") ? "var(--ink-muted)" : "var(--accent-ink)"}}>{it.delta}</span>}
          </span>
        ))}
      </div>
    </div>
  );
}

function Footer() {
  const { t, lang, tr } = useT();
  const TR = tr;
  return (
    <footer className="foot">
      <div className="shell">
        <div style={{display:"grid", gridTemplateColumns:"2fr 1fr 1fr 1fr", gap: 48, marginBottom: 64}}>
          <div>
            <div style={{display:"flex", alignItems:"center", gap:10, marginBottom: 24, color:"#F4F1EA"}}>
              <Logo size={56} withWord={true} />
            </div>
            <p style={{fontFamily:"var(--font-display)", fontSize: 30, lineHeight: 1.1, margin:"0 0 24px", maxWidth: 460}}>
              {TR(
                "Holding e núcleo de governança do ecossistema BlockUrb.",
                "Holding company and governance core of the BlockUrb ecosystem.",
                "Holding y núcleo de gobernanza del ecosistema BlockUrb."
              )}
            </p>
            <div className="mono" style={{lineHeight: 1.7}}>
              BlockUrb OÜ · Ahtri tn 12 · 15551 Tallinn · {TR("Estônia","Estonia","Estonia")}<br/>
              Reg. 16487991 · VAT EE102543891
            </div>
          </div>
          <FooterCol title={TR("Verticais","Verticals","Verticales")} links={[
            ["BLU Fund", "capital.html"],
            ["Urblock", "platform.html#urblock"],
            ["BlockUrb Smart City", "cities.html"],
            ["BluArq.AI", "platform.html#studios"],
            ["BlockUrb School", "platform.html#school"],
          ]} />
          <FooterCol title={TR("Comunidade","Community","Comunidad")} links={[
            ["Instituto BlockUrb", "platform.html#green"],
            ["BlockUrb Franchise", "platform.html#franchise"],
            ["Magnatas do Lote", "platform.html#magnatas"],
            ["BlockUrb Gamer", "platform.html#gamer"],
          ]} />
          <FooterCol title={TR("Holding","Company","Empresa")} links={[
            ["Manifesto", "manifesto.html"],
            [TR("Os 5 pilares","The 5 pillars","Los 5 pilares"), "index.html#pilares"],
            [TR("Contato","Contact","Contacto"), "contato.html"],
            [TR("Kit de imprensa","Press kit","Kit de prensa"), "contato.html#imprensa"],
          ]} />
        </div>
        <div className="hr hr--dark" style={{background:"#28322C"}}></div>
        <div style={{display:"flex", justifyContent:"space-between", paddingTop: 28, fontFamily:"var(--font-mono)", fontSize: 11, letterSpacing:"0.08em", textTransform:"uppercase"}} className="mono">
          <div>© 2026 BlockUrb OÜ. {TR("Todos os direitos reservados.","All rights reserved.","Todos los derechos reservados.")}</div>
          <div style={{display:"flex", gap: 28}}>
            <span>Status: <span style={{color:"oklch(0.78 0.08 145)"}}>● {TR("Operacional","Operational","Operacional")}</span></span>
            <span>59.4370° N, 24.7536° E</span>
          </div>
        </div>
      </div>
    </footer>
  );
}

function FooterCol({title, links}) {
  return (
    <div>
      <div className="mono" style={{marginBottom: 18}}>{title}</div>
      <ul style={{listStyle:"none", padding:0, margin:0, display:"flex", flexDirection:"column", gap: 10, fontSize: 14}}>
        {links.map(([label, href], i) => <li key={i}><a href={href}>{label}</a></li>)}
      </ul>
    </div>
  );
}

// Iso-block diagram (kept for hero visual)
function CityDiagram({ animated = true }) {
  return (
    <svg viewBox="0 0 600 600" style={{width:"100%", height:"auto"}} aria-hidden="true">
      <defs>
        <pattern id="grid" width="20" height="20" patternUnits="userSpaceOnUse">
          <path d="M 20 0 L 0 0 0 20" fill="none" stroke="currentColor" strokeWidth="0.5" opacity="0.15"/>
        </pattern>
      </defs>
      <rect width="600" height="600" fill="url(#grid)" />
      {[[20,20],[580,20],[20,580],[580,580]].map(([x,y],i)=>(
        <g key={i} stroke="currentColor" strokeWidth="1" opacity="0.5">
          <line x1={x-8} y1={y} x2={x+8} y2={y}/><line x1={x} y1={y-8} x2={x} y2={y+8}/>
        </g>
      ))}
      <line x1="80" y1="430" x2="520" y2="430" stroke="currentColor" strokeWidth="1" opacity="0.4"/>
      <g transform="translate(300 300)">
        <IsoBlock x={-180} y={40} w={90} d={70} h={120} fill="var(--bg-elev)" stroke="currentColor" />
        <IsoBlock x={-100} y={20} w={70} d={70} h={180} fill="var(--bg-elev)" stroke="currentColor" />
        <IsoBlock x={-30} y={-10} w={100} d={80} h={240} fill="var(--accent)" stroke="var(--accent-ink)" highlight />
        <IsoBlock x={70} y={20} w={80} d={70} h={170} fill="var(--bg-elev)" stroke="currentColor" />
        <IsoBlock x={150} y={50} w={70} d={60} h={100} fill="var(--bg-elev)" stroke="currentColor" />
      </g>
      <g fontFamily="JetBrains Mono, monospace" fontSize="10" fill="currentColor" opacity="0.6">
        <text x="40" y="40">SECTOR-A · 04</text>
        <text x="500" y="40" textAnchor="end">{`{ 03/05 ATIVO }`}</text>
        <text x="40" y="575">59.4370°N · 24.7536°E</text>
        <text x="560" y="575" textAnchor="end">SCALE 1:2400</text>
      </g>
      <g stroke="currentColor" strokeWidth="0.75" fill="none" opacity="0.7">
        <path d="M 320 130 L 420 90"/>
        <circle cx="320" cy="130" r="2.5" fill="currentColor"/>
      </g>
      <g fontFamily="JetBrains Mono, monospace" fontSize="9" fill="currentColor" opacity="0.85">
        <text x="425" y="86">PARCEL-273-A</text>
        <text x="425" y="100" opacity="0.6">tokenizado · 4.200 frações</text>
      </g>
      {animated && (
        <circle cx="300" cy="290" r="4" fill="var(--signal)">
          <animate attributeName="r" values="4;14;4" dur="2.4s" repeatCount="indefinite"/>
          <animate attributeName="opacity" values="0.9;0;0.9" dur="2.4s" repeatCount="indefinite"/>
        </circle>
      )}
      <circle cx="300" cy="290" r="3" fill="var(--signal)" />
    </svg>
  );
}

function IsoBlock({x, y, w, d, h, fill, stroke, highlight}) {
  const top = `M ${x} ${y} L ${x+w} ${y-d/2} L ${x+w*2} ${y} L ${x+w} ${y+d/2} Z`;
  const left = `M ${x} ${y} L ${x+w} ${y+d/2} L ${x+w} ${y+d/2+h} L ${x} ${y+h} Z`;
  const right = `M ${x+w} ${y+d/2} L ${x+w*2} ${y} L ${x+w*2} ${y+h} L ${x+w} ${y+d/2+h} Z`;
  return (
    <g>
      <path d={left} fill={fill} stroke={stroke} strokeWidth="1" opacity={highlight ? 1 : 0.85}/>
      <path d={right} fill={fill} stroke={stroke} strokeWidth="1" opacity={highlight ? 0.8 : 0.6}/>
      <path d={top} fill={highlight ? "var(--accent-soft)" : fill} stroke={stroke} strokeWidth="1"/>
    </g>
  );
}

// Vertical glyphs (one per company/product)
function VerticalGlyph({ kind, size = 28 }) {
  const s = { width: size, height: size, color: "var(--ink)" };
  switch (kind) {
    case "fund": return (<svg viewBox="0 0 24 24" style={s} fill="none" stroke="currentColor" strokeWidth="1.25"><circle cx="12" cy="12" r="9"/><path d="M8 12h8 M12 8v8 M9.5 9.5l5 5 M14.5 9.5l-5 5"/></svg>);
    case "urblock": return (<svg viewBox="0 0 24 24" style={s} fill="none" stroke="currentColor" strokeWidth="1.25"><rect x="3" y="3" width="8" height="8"/><rect x="13" y="3" width="8" height="8"/><rect x="3" y="13" width="8" height="8"/><rect x="13" y="13" width="8" height="8" fill="currentColor" opacity="0.2"/></svg>);
    case "smartcity": return (<svg viewBox="0 0 24 24" style={s} fill="none" stroke="currentColor" strokeWidth="1.25"><path d="M3 21V8l9-5 9 5v13"/><path d="M9 21v-7h6v7"/><path d="M3 21h18 M7 11h.01 M17 11h.01"/></svg>);
    case "studios": return (<svg viewBox="0 0 24 24" style={s} fill="none" stroke="currentColor" strokeWidth="1.25"><rect x="3" y="5" width="18" height="14"/><path d="M3 8l9 6 9-6"/><circle cx="12" cy="12" r="2.5"/></svg>);
    case "school": return (<svg viewBox="0 0 24 24" style={s} fill="none" stroke="currentColor" strokeWidth="1.25"><path d="M2 9l10-5 10 5-10 5z"/><path d="M6 11v5c0 1.5 3 3 6 3s6-1.5 6-3v-5"/><path d="M22 9v6"/></svg>);
    case "green": return (<svg viewBox="0 0 24 24" style={s} fill="none" stroke="currentColor" strokeWidth="1.25"><path d="M12 22V12 M12 12c0-5 4-9 9-9 0 5-4 9-9 9z M12 12c0-4-3-7-7-7 0 4 3 7 7 7z"/></svg>);
    case "franchise": return (<svg viewBox="0 0 24 24" style={s} fill="none" stroke="currentColor" strokeWidth="1.25"><path d="M3 21V10l9-7 9 7v11"/><path d="M9 21v-6h6v6"/><path d="M3 21h18"/><circle cx="12" cy="11" r="1.2" fill="currentColor"/><path d="M6 14h2 M16 14h2"/></svg>);
    default: return null;
  }
}

function StatBig({ value, label, sub }) {
  return (
    <div style={{borderTop: "1px solid var(--line)", paddingTop: 24}}>
      <div className="mono" style={{marginBottom: 12}}>{label}</div>
      <div style={{fontFamily:"var(--font-display)", fontSize: 72, lineHeight: 0.95, letterSpacing:"-0.02em"}}>{value}</div>
      {sub && <div className="mono" style={{marginTop: 10, opacity: 0.8}}>{sub}</div>}
    </div>
  );
}

function SectionHead({ index, kicker, title, sub, dark }) {
  return (
    <div style={{display:"grid", gridTemplateColumns:"auto 1fr", gap: 48, alignItems:"start", marginBottom: 64}}>
      <Reveal as="div" className="mono" style={{paddingTop: 6, color: dark ? "color-mix(in srgb, #F4F1EA 60%, transparent)" : undefined}}>
        {index} / {kicker}
      </Reveal>
      <div>
        <h2 className="display" style={{fontSize: 64, margin: 0, color: dark ? "#F4F1EA" : "var(--ink)"}}>
          <SplitReveal text={typeof title === "string" ? [title] : title} />
        </h2>
        {sub && <Reveal delay={150}><p style={{maxWidth: 720, marginTop: 20, color: dark ? "color-mix(in srgb, #F4F1EA 75%, transparent)" : "var(--ink-soft)", fontSize: 17, lineHeight: 1.55}}>{sub}</p></Reveal>}
      </div>
    </div>
  );
}

function CursorFollower() {
  const dotRef = useRef(null);
  const ringRef = useRef(null);
  useEffect(() => {
    let mx = -50, my = -50, rx = -50, ry = -50;
    const onMove = (e) => { mx = e.clientX; my = e.clientY; if (dotRef.current) { dotRef.current.style.left = mx+"px"; dotRef.current.style.top = my+"px"; } };
    const onOver = (e) => {
      if (e.target.closest("a, button, .module-card, .lang-btn")) {
        if (ringRef.current) { ringRef.current.style.width = "56px"; ringRef.current.style.height = "56px"; ringRef.current.style.borderColor = "var(--accent)"; }
      } else {
        if (ringRef.current) { ringRef.current.style.width = "32px"; ringRef.current.style.height = "32px"; ringRef.current.style.borderColor = "var(--ink)"; }
      }
    };
    let raf;
    const loop = () => {
      rx += (mx - rx) * 0.15; ry += (my - ry) * 0.15;
      if (ringRef.current) { ringRef.current.style.left = rx+"px"; ringRef.current.style.top = ry+"px"; }
      raf = requestAnimationFrame(loop);
    };
    loop();
    window.addEventListener("mousemove", onMove);
    window.addEventListener("mouseover", onOver);
    return () => { cancelAnimationFrame(raf); window.removeEventListener("mousemove", onMove); window.removeEventListener("mouseover", onOver); };
  }, []);
  return (<>
    <div ref={dotRef} className="cursor-dot"/>
    <div ref={ringRef} className="cursor-ring"/>
  </>);
}

function CoordPlane() {
  // Static node positions for pulsing dots and scanning beams
  const nodes = [
    [220, 280], [480, 180], [760, 420], [1100, 260], [1380, 540],
    [320, 760], [620, 880], [960, 720], [1280, 940], [180, 980],
  ];
  return (
    <div className="coord-plane" aria-hidden="true">
      <svg viewBox="0 0 1600 1200" preserveAspectRatio="xMidYMid slice">
        <defs>
          <pattern id="coordGrid" width="80" height="80" patternUnits="userSpaceOnUse">
            <path d="M 80 0 L 0 0 0 80" fill="none" className="coord-line"/>
          </pattern>
          <pattern id="coordGridFine" width="20" height="20" patternUnits="userSpaceOnUse">
            <path d="M 20 0 L 0 0 0 20" fill="none" stroke="currentColor" strokeWidth="0.5" opacity="0.04"/>
          </pattern>
          <linearGradient id="scanGradH" x1="0" y1="0" x2="1" y2="0">
            <stop offset="0%" stopColor="var(--accent)" stopOpacity="0"/>
            <stop offset="50%" stopColor="var(--accent)" stopOpacity="0.7"/>
            <stop offset="100%" stopColor="var(--accent)" stopOpacity="0"/>
          </linearGradient>
          <linearGradient id="scanGradV" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="var(--accent)" stopOpacity="0"/>
            <stop offset="50%" stopColor="var(--accent)" stopOpacity="0.55"/>
            <stop offset="100%" stopColor="var(--accent)" stopOpacity="0"/>
          </linearGradient>
          <radialGradient id="radarPulse" cx="50%" cy="50%" r="50%">
            <stop offset="0%" stopColor="var(--accent)" stopOpacity="0"/>
            <stop offset="80%" stopColor="var(--accent)" stopOpacity="0.35"/>
            <stop offset="100%" stopColor="var(--accent)" stopOpacity="0"/>
          </radialGradient>
        </defs>

        <rect width="1600" height="1200" fill="url(#coordGridFine)"/>
        <rect width="1600" height="1200" fill="url(#coordGrid)"/>

        {/* Main axes */}
        <line x1="0" y1="600" x2="1600" y2="600" className="coord-axis"/>
        <line x1="800" y1="0" x2="800" y2="1200" className="coord-axis"/>

        {/* Animated scanning beams */}
        <rect x="0" y="-30" width="1600" height="60" fill="url(#scanGradH)" opacity="0.6">
          <animate attributeName="y" from="-60" to="1200" dur="9s" repeatCount="indefinite"/>
        </rect>
        <rect x="-30" y="0" width="60" height="1200" fill="url(#scanGradV)" opacity="0.45">
          <animate attributeName="x" from="-60" to="1600" dur="14s" repeatCount="indefinite"/>
        </rect>

        {/* Radar pulse from origin */}
        <circle cx="800" cy="600" r="40" fill="url(#radarPulse)">
          <animate attributeName="r" from="40" to="800" dur="6s" repeatCount="indefinite"/>
          <animate attributeName="opacity" from="0.7" to="0" dur="6s" repeatCount="indefinite"/>
        </circle>
        <circle cx="800" cy="600" r="40" fill="url(#radarPulse)">
          <animate attributeName="r" from="40" to="800" dur="6s" begin="3s" repeatCount="indefinite"/>
          <animate attributeName="opacity" from="0.7" to="0" dur="6s" begin="3s" repeatCount="indefinite"/>
        </circle>

        {/* Pulsing nodes */}
        {nodes.map(([x, y], i) => (
          <g key={i}>
            <circle cx={x} cy={y} r="3" fill="var(--accent)" opacity="0.8">
              <animate attributeName="opacity" values="0.2;0.95;0.2" dur={`${2.4 + (i % 3) * 0.7}s`} begin={`${(i * 0.31) % 3}s`} repeatCount="indefinite"/>
            </circle>
            <circle cx={x} cy={y} r="3" fill="none" stroke="var(--accent)" strokeWidth="1" opacity="0.5">
              <animate attributeName="r" values="3;18;3" dur={`${2.4 + (i % 3) * 0.7}s`} begin={`${(i * 0.31) % 3}s`} repeatCount="indefinite"/>
              <animate attributeName="opacity" values="0.6;0;0.6" dur={`${2.4 + (i % 3) * 0.7}s`} begin={`${(i * 0.31) % 3}s`} repeatCount="indefinite"/>
            </circle>
          </g>
        ))}

        {/* Particles traveling along the X axis */}
        {[0, 1, 2].map((i) => (
          <circle key={`px${i}`} cy="600" r="2" fill="var(--accent)" opacity="0.9">
            <animate attributeName="cx" from="0" to="1600" dur={`${10 + i * 3}s`} begin={`${i * 2.4}s`} repeatCount="indefinite"/>
            <animate attributeName="opacity" values="0;1;1;0" dur={`${10 + i * 3}s`} begin={`${i * 2.4}s`} repeatCount="indefinite"/>
          </circle>
        ))}
        {/* Particles traveling along the Y axis */}
        {[0, 1].map((i) => (
          <circle key={`py${i}`} cx="800" r="2" fill="var(--accent)" opacity="0.9">
            <animate attributeName="cy" from="0" to="1200" dur={`${12 + i * 4}s`} begin={`${i * 3 + 1}s`} repeatCount="indefinite"/>
            <animate attributeName="opacity" values="0;1;1;0" dur={`${12 + i * 4}s`} begin={`${i * 3 + 1}s`} repeatCount="indefinite"/>
          </circle>
        ))}

        {/* Connecting trace lines that flicker */}
        <g stroke="var(--accent)" strokeWidth="0.75" fill="none" opacity="0.25">
          <line x1="220" y1="280" x2="480" y2="180" strokeDasharray="3 6">
            <animate attributeName="stroke-dashoffset" from="0" to="-90" dur="4s" repeatCount="indefinite"/>
          </line>
          <line x1="760" y1="420" x2="1100" y2="260" strokeDasharray="3 6">
            <animate attributeName="stroke-dashoffset" from="0" to="-90" dur="5s" repeatCount="indefinite"/>
          </line>
          <line x1="320" y1="760" x2="620" y2="880" strokeDasharray="3 6">
            <animate attributeName="stroke-dashoffset" from="0" to="-90" dur="4.5s" repeatCount="indefinite"/>
          </line>
          <line x1="960" y1="720" x2="1280" y2="940" strokeDasharray="3 6">
            <animate attributeName="stroke-dashoffset" from="0" to="-90" dur="5.5s" repeatCount="indefinite"/>
          </line>
        </g>

        {/* Coord labels */}
        <g fontFamily="JetBrains Mono, monospace" fontSize="9" fill="var(--ink-muted)" opacity="0.5">
          <text x="20" y="595">−X</text>
          <text x="1565" y="595" textAnchor="end">+X</text>
          <text x="805" y="20">+Y</text>
          <text x="805" y="1190">−Y</text>
          <text x="810" y="613" opacity="0.7">{`{ 0,0 } · TLN`}</text>
        </g>

        {/* Crosshair tick marks at intersections */}
        {[400, 800, 1200].flatMap((x) => [200, 600, 1000].map((y) => (
          <g key={`${x}-${y}`} stroke="var(--accent)" strokeWidth="0.75" opacity="0.35">
            <line x1={x-4} y1={y} x2={x+4} y2={y}/>
            <line x1={x} y1={y-4} x2={x} y2={y+4}/>
          </g>
        )))}
      </svg>
    </div>
  );
}

function Page({ children }) {
  return <I18nProvider><CoordPlane/><CursorFollower/>{children}<TranslationOverlay/></I18nProvider>;
}

// Aliases for old code paths
const ModuleGlyph = VerticalGlyph;

Object.assign(window, { TopNav, Footer, Ticker, BrandLink, CityDiagram, IsoBlock, VerticalGlyph, ModuleGlyph, StatBig, SectionHead, PAGES, CursorFollower, CoordPlane, Page });
