/* global React, STOCKS, Sparkline */
const { useState: useStateCP, useMemo: useMemoCP } = React;

const EUR = (lang, n, opts) => n.toLocaleString(lang === "de" ? "de-DE" : "en-GB", { style: "currency", currency: "EUR", ...opts });

/* =========================================================
   Portal shell with sidebar
   ========================================================= */
function PortalShell({ lang, role, page, setPage, onLogout, children }) {
  const t = window.useT(lang);
  const isAdmin = role === "admin";
  const navItems = isAdmin
    ? [
      ["dashboard",  t("adm_dashboard")],
      ["clients",    t("adm_clients")],
      ["approvals",  t("adm_approvals")],
      ["trades",     t("adm_trades")],
      ["compliance", t("adm_compliance")],
      ["announce",   t("adm_announce")],
      ["health",     t("adm_health")],
      ["users",      t("adm_users")],
    ]
    : [
      ["overview",  t("cp_overview")],
      ["portfolio", t("cp_portfolio")],
      ["watchlist", t("cp_watchlist")],
      ["orders",    t("cp_orders")],
      ["history",   t("cp_history")],
      ["funding",   t("cp_funding")],
      ["documents", t("cp_documents")],
      ["settings",  t("cp_settings")],
    ];

  return (
    <div style={{ minHeight: "100vh", display: "grid", gridTemplateColumns: "256px 1fr", background: "var(--paper-100)" }}>
      <aside style={{ background: "var(--ink-950)", color: "var(--paper-100)", display: "flex", flexDirection: "column", position: "sticky", top: 0, height: "100vh" }}>
        <div style={{ padding: "24px 24px 30px", borderBottom: "1px solid rgba(255,255,255,0.08)" }}>
          <window.Logo dark />
          {isAdmin && (
            <div style={{ marginTop: 16, display: "inline-flex", padding: "4px 10px", border: "1px solid var(--gold-deep)", color: "var(--gold)", fontSize: 9.5, letterSpacing: "0.18em", textTransform: "uppercase", fontFamily: "var(--mono)" }}>
              DESK · STUFE 3
            </div>
          )}
        </div>
        <nav style={{ padding: 16, flex: 1, display: "flex", flexDirection: "column", gap: 2 }}>
          {navItems.map(([k, label]) => (
            <button key={k} onClick={() => setPage(k)}
              style={{
                textAlign: "left", padding: "11px 14px", fontSize: 12.5,
                background: page === k ? "rgba(255,255,255,0.06)" : "transparent",
                color: page === k ? "var(--paper-50)" : "var(--ink-300)",
                borderLeft: page === k ? "2px solid var(--gold)" : "2px solid transparent",
                fontWeight: page === k ? 500 : 400,
              }}
            >{label}</button>
          ))}
        </nav>
        <div style={{ padding: 20, borderTop: "1px solid rgba(255,255,255,0.08)", display: "flex", flexDirection: "column", gap: 12 }}>
          <div style={{ display: "flex", gap: 10, alignItems: "center", fontSize: 12.5 }}>
            <div style={{ width: 32, height: 32, borderRadius: "50%", background: "var(--gold)", display: "grid", placeItems: "center", color: "var(--ink-950)", fontFamily: "var(--serif)", fontSize: 14 }}>
              {isAdmin ? "D" : "JD"}
            </div>
            <div>
              <div style={{ color: "var(--paper-50)" }}>{isAdmin ? "Desk-Konsole" : "Jonas Diehl"}</div>
              <div style={{ fontSize: 10.5, color: "var(--ink-400)" }}>{isAdmin ? "ops@aurevon.de" : "Depot · 4827-913-A"}</div>
            </div>
          </div>
          <button onClick={onLogout} style={{ fontSize: 11.5, color: "var(--ink-300)", textAlign: "left", padding: "8px 0", borderTop: "1px solid rgba(255,255,255,0.06)" }}>← {t("cp_logout")}</button>
        </div>
      </aside>
      <main style={{ display: "flex", flexDirection: "column" }}>
        <PortalTopBar lang={lang} role={role} />
        <div style={{ padding: "32px 40px 60px", flex: 1 }}>{children}</div>
      </main>
    </div>
  );
}

function PortalTopBar({ lang, role }) {
  return (
    <div style={{ background: "var(--paper-50)", borderBottom: "1px solid var(--paper-200)", padding: "0 40px", display: "flex", justifyContent: "space-between", alignItems: "center", height: 64, position: "sticky", top: 0, zIndex: 10 }}>
      <div style={{ position: "relative", flex: 1, maxWidth: 460 }}>
        <input placeholder={lang === "de" ? "Symbol, ISIN, Mandat suchen…" : "Search symbol, ISIN, mandate…"}
          style={{ width: "100%", padding: "10px 14px 10px 36px", border: "1px solid var(--paper-200)", background: "var(--paper-100)", fontSize: 12.5 }} />
        <span style={{ position: "absolute", left: 13, top: "50%", transform: "translateY(-50%)", color: "var(--slate-500)", fontSize: 13 }}>⌕</span>
        <span style={{ position: "absolute", right: 14, top: "50%", transform: "translateY(-50%)", color: "var(--slate-500)", fontSize: 10.5, fontFamily: "var(--mono)", border: "1px solid var(--paper-300)", padding: "2px 6px" }}>⌘K</span>
      </div>
      <div style={{ display: "flex", gap: 18, alignItems: "center", fontSize: 11.5, color: "var(--slate-500)" }}>
        <span style={{ display: "flex", alignItems: "center", gap: 6 }}>
          <span style={{ width: 6, height: 6, borderRadius: 999, background: "var(--gain)" }}></span>
          {lang === "de" ? "XETRA offen" : "XETRA open"}
        </span>
        <span style={{ fontFamily: "var(--mono)" }}>STR · {new Date().toLocaleTimeString("de-DE", { hour: "2-digit", minute: "2-digit" })}</span>
        <div className="lang-toggle" style={{ borderColor: "var(--paper-300)" }}>
          <button onClick={() => window.__setLang("de")} className={lang === "de" ? "on" : ""} style={{ color: lang === "de" ? "var(--ink-950)" : "var(--slate-500)", background: lang === "de" ? "var(--paper-100)" : "transparent" }}>DE</button>
          <button onClick={() => window.__setLang("en")} className={lang === "en" ? "on" : ""} style={{ color: lang === "en" ? "var(--ink-950)" : "var(--slate-500)", background: lang === "en" ? "var(--paper-100)" : "transparent" }}>EN</button>
        </div>
        <button style={{ position: "relative", color: "var(--slate-700)", padding: 8 }}>
          🔔 <span style={{ position: "absolute", top: 4, right: 4, width: 7, height: 7, borderRadius: 999, background: "var(--loss)" }}></span>
        </button>
      </div>
    </div>
  );
}

/* =========================================================
   CLIENT — Overview
   ========================================================= */
function ClientOverview({ lang }) {
  const t = window.useT(lang);
  const fmt = (n) => EUR(lang, n, { maximumFractionDigits: 2 });
  return (
    <>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "end", marginBottom: 30 }}>
        <div>
          <div className="kicker">{t("cp_overview")} · {new Date().toLocaleDateString("de-DE", { weekday: "long", day: "numeric", month: "long", year: "numeric" })}</div>
          <h1 style={{ fontFamily: "var(--serif)", fontSize: 40, fontWeight: 400, letterSpacing: "-0.015em", marginTop: 8 }}>{t("cp_welcome")}, Jonas.</h1>
        </div>
        <div style={{ display: "flex", gap: 10 }}>
          <button className="btn btn-ghost btn-sm">{t("cp_statement")}</button>
          <button className="btn btn-primary btn-sm">{t("cp_place_order")}</button>
        </div>
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: 16, marginBottom: 24 }}>
        <StatCard label={t("cp_total_value")} value={fmt(812_480.40)} delta="+1,42 %" up={true} />
        <StatCard label={t("cp_today_pl")} value={fmt(11_394.05)} delta="+1,42 %" up={true} />
        <StatCard label={t("cp_cash")} value={fmt(38_204.66)} delta="EUR" />
        <StatCard label={t("cp_margin")} value={fmt(214_500.00)} delta={t("cp_leverage")} />
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "2fr 1fr", gap: 16, marginBottom: 24 }}>
        <PortfolioChart lang={lang} />
        <AllocationCard lang={lang} />
      </div>
      <div style={{ display: "grid", gridTemplateColumns: "2fr 1fr", gap: 16 }}>
        <PositionsTable lang={lang} />
        <SideFeed lang={lang} />
      </div>
    </>
  );
}

function StatCard({ label, value, delta, up }) {
  return (
    <div style={{ background: "var(--paper-50)", border: "1px solid var(--paper-200)", padding: 22 }}>
      <div className="kicker">{label}</div>
      <div style={{ fontFamily: "var(--serif)", fontSize: 30, fontWeight: 400, letterSpacing: "-0.01em", marginTop: 12 }} className="tabnum">{value}</div>
      <div style={{ marginTop: 8, fontSize: 11.5, color: up === true ? "var(--gain)" : up === false ? "var(--loss)" : "var(--slate-500)" }} className="tabnum">{delta}</div>
    </div>
  );
}

function PortfolioChart({ lang }) {
  const N = 90;
  const pts = useMemoCP(() => {
    const rng = (() => { let s = 7; return () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 0xFFFFFFFF; }; })();
    let v = 690_000;
    const arr = [];
    for (let i = 0; i < N; i++) { v += (rng() - 0.45) * 11000; arr.push(v); }
    return arr;
  }, []);
  const w = 760, h = 280, pad = 16;
  const min = Math.min(...pts), max = Math.max(...pts);
  const path = pts.map((p, i) => {
    const x = pad + (i / (pts.length - 1)) * (w - pad * 2);
    const y = pad + (1 - (p - min) / (max - min)) * (h - pad * 2);
    return `${i === 0 ? "M" : "L"} ${x.toFixed(1)} ${y.toFixed(1)}`;
  }).join(" ");
  const area = path + ` L ${w - pad} ${h - pad} L ${pad} ${h - pad} Z`;
  return (
    <div style={{ background: "var(--paper-50)", border: "1px solid var(--paper-200)", padding: 22 }}>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "start", marginBottom: 20 }}>
        <div>
          <div className="kicker">{t("cp_performance")}</div>
          <div style={{ fontFamily: "var(--serif)", fontSize: 25, fontWeight: 400, marginTop: 6 }}>+{((pts[pts.length-1] - pts[0]) / pts[0] * 100).toFixed(2).replace(".", ",")}% <span style={{ fontSize: 12.5, color: "var(--slate-500)", fontFamily: "var(--sans)" }}>· {t("cp_last_days")}</span></div>
        </div>
        <div style={{ display: "flex", gap: 4 }}>
          {["1T","1W","1M","3M","YTD","1J","All"].map((l, i) => (
            <button key={l} style={{ padding: "6px 10px", fontSize: 10.5, fontFamily: "var(--mono)", background: i === 3 ? "var(--ink-950)" : "transparent", color: i === 3 ? "var(--paper-50)" : "var(--slate-500)", border: "1px solid " + (i === 3 ? "var(--ink-950)" : "var(--paper-200)") }}>{l}</button>
          ))}
        </div>
      </div>
      <svg width="100%" viewBox={`0 0 ${w} ${h}`} preserveAspectRatio="none" style={{ display: "block" }}>
        <defs>
          <linearGradient id="pg" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="var(--ink-700)" stopOpacity="0.28" />
            <stop offset="100%" stopColor="var(--ink-700)" stopOpacity="0" />
          </linearGradient>
        </defs>
        {[0,1,2,3].map(i => (
          <line key={i} x1={pad} y1={pad + i * (h - 2*pad)/3} x2={w-pad} y2={pad + i * (h - 2*pad)/3} stroke="var(--paper-200)" />
        ))}
        <path d={area} fill="url(#pg)" />
        <path d={path} fill="none" stroke="var(--ink-950)" strokeWidth="1.6" />
      </svg>
    </div>
  );
}

function AllocationCard({ lang }) {
  const t = window.useT(lang);
  const data = [
    { k: lang === "de" ? "Aktien Europa" : "European equities", v: 42, c: "var(--ink-950)" },
    { k: lang === "de" ? "Aktien Global" : "Global equities",    v: 22, c: "var(--ink-700)" },
    { k: lang === "de" ? "Anleihen" : "Fixed income",            v: 24, c: "var(--gold-deep)" },
    { k: "ETFs",                                                  v: 8,  c: "var(--gold)" },
    { k: lang === "de" ? "Liquidität" : "Cash",                   v: 4,  c: "var(--slate-300)" },
  ];
  return (
    <div style={{ background: "var(--paper-50)", border: "1px solid var(--paper-200)", padding: 22 }}>
      <div className="kicker">{t("cp_allocation")}</div>
      <div style={{ display: "flex", height: 12, marginTop: 16, marginBottom: 20 }}>
        {data.map(d => <div key={d.k} style={{ width: d.v + "%", background: d.c }} title={d.k}></div>)}
      </div>
      <ul style={{ listStyle: "none", display: "flex", flexDirection: "column", gap: 10 }}>
        {data.map(d => (
          <li key={d.k} style={{ display: "grid", gridTemplateColumns: "11px 1fr auto", gap: 10, fontSize: 12.5, alignItems: "center" }}>
            <span style={{ width: 9, height: 9, background: d.c }}></span>
            <span style={{ color: "var(--slate-700)" }}>{d.k}</span>
            <span className="tabnum" style={{ fontWeight: 500 }}>{d.v},0 %</span>
          </li>
        ))}
      </ul>
    </div>
  );
}

function PositionsTable({ lang }) {
  const t = window.useT(lang);
  return (
    <div style={{ background: "var(--paper-50)", border: "1px solid var(--paper-200)" }}>
      <div style={{ padding: "18px 22px", display: "flex", justifyContent: "space-between", alignItems: "center", borderBottom: "1px solid var(--paper-200)" }}>
        <div className="kicker">{t("cp_open_positions")}</div>
        <a href="#" style={{ fontSize: 11.5, color: "var(--slate-700)" }}>{t("cp_view_all")} →</a>
      </div>
      <table style={{ width: "100%", borderCollapse: "collapse", fontSize: 12.5 }}>
        <thead>
          <tr style={{ background: "var(--paper-100)" }}>
            {[t("cp_symbol"), t("cp_qty"), t("cp_avg_cost"), t("cp_last"), t("cp_value"), "%", "5T"].map(h => (
              <th key={h} style={{ padding: "10px 14px", textAlign: h === t("cp_symbol") ? "left" : "right", fontWeight: 500, fontSize: 10, letterSpacing: "0.09em", textTransform: "uppercase", color: "var(--slate-500)" }}>{h}</th>
            ))}
          </tr>
        </thead>
        <tbody>
          {STOCKS.slice(0, 7).map((s, i) => {
            const qty = [120, 25, 40, 200, 60, 350, 80][i];
            const cost = s.px * (1 - (s.chg/100) - (Math.sin(i)*0.05));
            const val = s.px * qty;
            return (
              <tr key={s.sym} style={{ borderTop: "1px solid var(--paper-200)" }}>
                <td style={{ padding: "12px 14px" }}>
                  <div style={{ fontWeight: 600 }}>{s.sym}</div>
                  <div style={{ fontSize: 10.5, color: "var(--slate-500)" }}>{s.name}</div>
                </td>
                <td className="tabnum" style={{ padding: "12px 14px", textAlign: "right" }}>{qty}</td>
                <td className="tabnum" style={{ padding: "12px 14px", textAlign: "right", color: "var(--slate-500)" }}>{cost.toFixed(2).replace(".", ",")}</td>
                <td className="tabnum" style={{ padding: "12px 14px", textAlign: "right" }}>{s.px.toFixed(2).replace(".", ",")}</td>
                <td className="tabnum" style={{ padding: "12px 14px", textAlign: "right", fontWeight: 500 }}>{val.toLocaleString("de-DE", { maximumFractionDigits: 0 })}</td>
                <td className="tabnum" style={{ padding: "12px 14px", textAlign: "right", color: s.chg >= 0 ? "var(--gain)" : "var(--loss)" }}>{s.chg >= 0 ? "+" : ""}{s.chg.toFixed(2).replace(".", ",")} %</td>
                <td style={{ padding: "8px 14px", textAlign: "right" }}><Sparkline seed={s.sym} up={s.chg >= 0} w={60} h={18} /></td>
              </tr>
            );
          })}
        </tbody>
      </table>
    </div>
  );
}

function SideFeed({ lang }) {
  const items = lang === "de"
    ? [["09:42", "Order ausgeführt", "KAUF 25 SAP @ 214,60"],
       ["09:18", "Dividende gutgeschrieben", "+ 840,00 EUR · ALV"],
       ["08:55", "Morgennotiz", "EZB-Sitzung im Fokus"],
       ["vor 1 T", "Auszahlung freigegeben", "− 5.000,00 EUR"]]
    : [["09:42", "Order filled", "BUY 25 SAP @ 214.60"],
       ["09:18", "Dividend credited", "+ EUR 840.00 · ALV"],
       ["08:55", "Morning note", "ECB meeting in focus"],
       ["1d ago", "Withdrawal cleared", "− EUR 5,000.00"]];
  return (
    <div style={{ background: "var(--paper-50)", border: "1px solid var(--paper-200)" }}>
      <div style={{ padding: "18px 22px", borderBottom: "1px solid var(--paper-200)" }}>
        <div className="kicker">{t_(lang, "cp_activity")}</div>
      </div>
      <ul style={{ listStyle: "none" }}>
        {items.map(([when, head, sub], i) => (
          <li key={i} style={{ padding: "15px 22px", borderTop: i ? "1px solid var(--paper-200)" : "none" }}>
            <div style={{ fontFamily: "var(--mono)", fontSize: 9.5, color: "var(--slate-500)", letterSpacing: "0.07em", marginBottom: 6 }}>{when}</div>
            <div style={{ fontSize: 12.5, fontWeight: 500 }}>{head}</div>
            <div style={{ fontSize: 11.5, color: "var(--slate-500)", marginTop: 2 }} className="tabnum">{sub}</div>
          </li>
        ))}
      </ul>
    </div>
  );
}
function t_(lang, key) { return window.I18N[lang][key] || key; }

/* =========================================================
   Watchlist + order ticket
   ========================================================= */
function ClientWatchlist({ lang }) {
  const t = window.useT(lang);
  const [picked, setPicked] = useStateCP(STOCKS[0].sym);
  const sel = STOCKS.find(s => s.sym === picked);
  return (
    <div>
      <h1 style={{ fontFamily: "var(--serif)", fontSize: 36, fontWeight: 400, letterSpacing: "-0.015em", marginBottom: 22 }}>{t("cp_watchlist")}</h1>
      <div style={{ display: "grid", gridTemplateColumns: "1.6fr 1fr", gap: 16 }}>
        <div style={{ background: "var(--paper-50)", border: "1px solid var(--paper-200)" }}>
          <table style={{ width: "100%", borderCollapse: "collapse", fontSize: 12.5 }}>
            <thead>
              <tr style={{ background: "var(--paper-100)" }}>
                {[t("cp_symbol"), t("cp_venue"), t("cp_sector"), t("cp_last"), "%", t("cp_mktcap")].map(h => (
                  <th key={h} style={{ padding: "12px 14px", textAlign: "left", fontWeight: 500, fontSize: 10, letterSpacing: "0.09em", textTransform: "uppercase", color: "var(--slate-500)" }}>{h}</th>
                ))}
              </tr>
            </thead>
            <tbody>
              {STOCKS.map(s => (
                <tr key={s.sym} onClick={() => setPicked(s.sym)} style={{ borderTop: "1px solid var(--paper-200)", cursor: "pointer", background: picked === s.sym ? "var(--paper-100)" : "transparent" }}>
                  <td style={{ padding: "13px 14px" }}>
                    <div style={{ fontWeight: 600 }}>{s.sym}</div>
                    <div style={{ fontSize: 10.5, color: "var(--slate-500)" }}>{s.name}</div>
                  </td>
                  <td style={{ padding: "13px 14px", color: "var(--slate-700)", fontFamily: "var(--mono)", fontSize: 10.5 }}>{s.venue}</td>
                  <td style={{ padding: "13px 14px", color: "var(--slate-700)" }}>{s.sector}</td>
                  <td className="tabnum" style={{ padding: "13px 14px", fontWeight: 500 }}>{s.px.toFixed(2).replace(".", ",")}</td>
                  <td className="tabnum" style={{ padding: "13px 14px", color: s.chg >= 0 ? "var(--gain)" : "var(--loss)" }}>{s.chg >= 0 ? "+" : ""}{s.chg.toFixed(2).replace(".", ",")} %</td>
                  <td style={{ padding: "13px 14px", color: "var(--slate-500)", fontSize: 11.5 }}>{s.mcap}</td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
        <OrderTicket lang={lang} stock={sel} />
      </div>
    </div>
  );
}

function OrderTicket({ lang, stock }) {
  const t = window.useT(lang);
  const [side, setSide] = useStateCP("buy");
  const [type, setType] = useStateCP("limit");
  const [qty, setQty] = useStateCP(50);
  const [px, setPx] = useStateCP(stock.px.toFixed(2));
  React.useEffect(() => setPx(stock.px.toFixed(2)), [stock.sym]);
  const total = (qty * parseFloat(px || 0));
  return (
    <div style={{ background: "var(--paper-50)", border: "1px solid var(--paper-200)" }}>
      <div style={{ padding: "18px 22px", borderBottom: "1px solid var(--paper-200)" }}>
        <div className="kicker">{t("cp_place_order")}</div>
        <div style={{ fontFamily: "var(--serif)", fontSize: 22, marginTop: 8, fontWeight: 400 }}>{stock.sym} <span style={{ color: "var(--slate-500)", fontSize: 12.5, fontFamily: "var(--sans)" }}>· {stock.name}</span></div>
        <div style={{ fontSize: 11.5, color: "var(--slate-500)", marginTop: 4 }} className="tabnum">{t("cp_last")} {stock.px.toFixed(2).replace(".", ",")} <span style={{ color: stock.chg >= 0 ? "var(--gain)" : "var(--loss)" }}>{stock.chg >= 0 ? "+" : ""}{stock.chg.toFixed(2).replace(".", ",")} %</span></div>
      </div>
      <div style={{ padding: 22, display: "flex", flexDirection: "column", gap: 15 }}>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", border: "1px solid var(--paper-200)" }}>
          <button onClick={() => setSide("buy")} style={{ padding: 12, fontSize: 12.5, fontWeight: 600, background: side === "buy" ? "var(--gain)" : "transparent", color: side === "buy" ? "white" : "var(--slate-700)" }}>{t("cp_buy")}</button>
          <button onClick={() => setSide("sell")} style={{ padding: 12, fontSize: 12.5, fontWeight: 600, background: side === "sell" ? "var(--loss)" : "transparent", color: side === "sell" ? "white" : "var(--slate-700)" }}>{t("cp_sell")}</button>
        </div>
        <div>
          <label style={{ fontSize: 10, letterSpacing: "0.14em", textTransform: "uppercase", color: "var(--slate-500)", display: "block", marginBottom: 6, fontFamily: "var(--mono)" }}>{t("cp_order_type")}</label>
          <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr" }}>
            {[["market", t("cp_market")], ["limit", t("cp_limit")], ["stop", t("cp_stop")]].map(([k, l]) => (
              <button key={k} onClick={() => setType(k)} style={{ padding: 10, fontSize: 11.5, border: "1px solid var(--paper-200)", background: type === k ? "var(--ink-950)" : "transparent", color: type === k ? "var(--paper-50)" : "var(--slate-700)" }}>{l}</button>
            ))}
          </div>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 12 }}>
          <div>
            <label style={{ fontSize: 10, letterSpacing: "0.14em", textTransform: "uppercase", color: "var(--slate-500)", display: "block", marginBottom: 6, fontFamily: "var(--mono)" }}>{t("cp_qty")}</label>
            <input type="number" value={qty} onChange={(e) => setQty(parseInt(e.target.value || 0))} style={{ width: "100%", padding: "10px 12px", border: "1px solid var(--paper-300)", background: "var(--paper-50)", fontSize: 13.5 }} />
          </div>
          <div>
            <label style={{ fontSize: 10, letterSpacing: "0.14em", textTransform: "uppercase", color: "var(--slate-500)", display: "block", marginBottom: 6, fontFamily: "var(--mono)" }}>{t("cp_price")}</label>
            <input type="text" value={type === "market" ? "MKT" : px} disabled={type === "market"} onChange={(e) => setPx(e.target.value)} style={{ width: "100%", padding: "10px 12px", border: "1px solid var(--paper-300)", background: "var(--paper-50)", fontSize: 13.5 }} />
          </div>
        </div>
        <div style={{ background: "var(--paper-100)", padding: "13px 15px", display: "flex", justifyContent: "space-between", fontSize: 12.5 }}>
          <span style={{ color: "var(--slate-500)" }}>{t("cp_order_total")}</span>
          <span className="tabnum" style={{ fontWeight: 600 }}>{EUR(lang, total, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}</span>
        </div>
        <button className="btn btn-primary" style={{ justifyContent: "center", padding: 13, fontSize: 13, fontWeight: 600 }}>
          {side === "buy" ? t("cp_buy") : t("cp_sell")} {stock.sym} →
        </button>
        <div style={{ fontSize: 10.5, color: "var(--slate-500)", lineHeight: 1.5 }}>
          {t("cp_order_commission")}
        </div>
      </div>
    </div>
  );
}

/* =========================================================
   Generic placeholder for portal sub-pages
   ========================================================= */
function GenericPortalPage({ title, lang }) {
  return (
    <div>
      <h1 style={{ fontFamily: "var(--serif)", fontSize: 36, fontWeight: 400, letterSpacing: "-0.015em", marginBottom: 8 }}>{title}</h1>
      <p style={{ color: "var(--slate-500)", marginBottom: 30 }}>{window.useT(lang)("page_placeholder")}</p>
      <div className="placeholder-img" style={{ minHeight: 340 }}>
        <span>BEREICH · {title.toUpperCase()}</span>
      </div>
    </div>
  );
}

window.PortalShell = PortalShell;
window.StatCard = StatCard;
window.ClientOverview = ClientOverview;
window.ClientWatchlist = ClientWatchlist;
window.GenericPortalPage = GenericPortalPage;
