/* global React */
// Bike-Konfigurator — UI-Komponenten

const { useState, useEffect, useMemo, useCallback, useRef } = React;
const { fmtPrice, fmtNum, SLOT_LABELS, SLOT_TO_POOL, PRIMARY_SLOTS, SECONDARY_SLOTS, TERRAIN_OPTIONS, STYLE_OPTIONS, SKILL_OPTIONS } = window.BikeFmt;
const Icon = window.BikeIcon;

// ----- Brand registry -----
const BRAND_MAP = {
  "Shimano":      { url: "https://www.shimano.com",           logo: "https://logo.clearbit.com/shimano.com" },
  "SRAM":         { url: "https://www.sram.com",              logo: "https://logo.clearbit.com/sram.com" },
  "RockShox":     { url: "https://www.sram.com/en/rockshox",  logo: "https://logo.clearbit.com/sram.com" },
  "Fox":          { url: "https://www.ridefox.com",           logo: "https://logo.clearbit.com/ridefox.com" },
  "Maxxis":       { url: "https://www.maxxis.com",            logo: "https://logo.clearbit.com/maxxis.com" },
  "Schwalbe":     { url: "https://www.schwalbe.com",          logo: "https://logo.clearbit.com/schwalbe.com" },
  "Crankbrothers":{ url: "https://www.crankbrothers.com",     logo: "https://logo.clearbit.com/crankbrothers.com" },
  "OneUp":        { url: "https://www.oneupcomponents.com",   logo: "https://logo.clearbit.com/oneupcomponents.com" },
  "Ergon":        { url: "https://www.ergon-bike.com",        logo: "https://logo.clearbit.com/ergon-bike.com" },
  "BikeYoke":     { url: "https://bikeyoke.de",               logo: "https://logo.clearbit.com/bikeyoke.de" },
  "Bosch":        { url: "https://www.bosch-ebike.com",       logo: "https://logo.clearbit.com/bosch-ebike.com" },
  "DT Swiss":     { url: "https://www.dtswiss.com",           logo: "https://logo.clearbit.com/dtswiss.com" },
  "Suntour":      { url: "https://www.srsuntour-cycling.com", logo: "https://logo.clearbit.com/srsuntour-cycling.com" },
  "ODI":          { url: "https://www.odigrips.com",          logo: "https://logo.clearbit.com/odigrips.com" },
  "SQlab":        { url: "https://www.sq-lab.com",            logo: "https://logo.clearbit.com/sq-lab.com" },
  "X-Fusion":     { url: "https://www.xfusionshox.com",       logo: "https://logo.clearbit.com/xfusionshox.com" },
  "KS":           { url: "https://www.kindshock.com",         logo: "https://logo.clearbit.com/kindshock.com" },
  "Magura":       { url: "https://www.magura.com",            logo: "https://logo.clearbit.com/magura.com" },
  "TRP":          { url: "https://www.trpcycling.com",        logo: "https://logo.clearbit.com/trpcycling.com" },
  "Enve":         { url: "https://www.enve.com",              logo: "https://logo.clearbit.com/enve.com" },
  "Brand-X":      { url: "https://www.chainreactioncycles.com", logo: "https://logo.clearbit.com/chainreactioncycles.com" },
};

function detectBrand(name) {
  for (const brand of Object.keys(BRAND_MAP)) {
    if (name && name.toLowerCase().includes(brand.toLowerCase())) return brand;
  }
  return null;
}

function BrandBadge({ name, size = "sm" }) {
  const brand = detectBrand(name);
  if (!brand) return null;
  const { url, logo } = BRAND_MAP[brand];
  const [imgOk, setImgOk] = useState(true);
  const isLarge = size === "lg";
  return (
    <a href={url} target="_blank" rel="noopener noreferrer"
      onClick={e => e.stopPropagation()}
      title={`${brand} Website`}
      style={{
        display: "inline-flex", alignItems: "center", gap: isLarge ? 8 : 5,
        background: "rgba(255,255,255,0.06)", border: "1px solid var(--hairline)",
        padding: isLarge ? "6px 12px" : "3px 8px",
        textDecoration: "none", flexShrink: 0,
      }}>
      {imgOk && (
        <img src={logo} alt={brand}
          onError={() => setImgOk(false)}
          style={{ height: isLarge ? 18 : 12, width: "auto", objectFit: "contain", filter: "brightness(0) invert(1)" }} />
      )}
      <span style={{ font: `600 ${isLarge ? 11 : 9}px/1 var(--font-sans)`, letterSpacing: "0.6px", color: "var(--ink-muted)", textTransform: "uppercase" }}>
        {imgOk ? "" : brand}
      </span>
      <svg width={isLarge ? 10 : 8} height={isLarge ? 10 : 8} viewBox="0 0 10 10" fill="none" style={{ opacity: 0.5 }}>
        <path d="M2 8L8 2M8 2H4M8 2V6" stroke="currentColor" strokeWidth="1.5" strokeLinecap="square"/>
      </svg>
    </a>
  );
}

// ----- Brand Ticker -----
const TICKER_BRANDS = Object.entries(BRAND_MAP).map(([name, { url, logo }]) => ({ name, url, logo }));

function BrandTicker() {
  // Inject keyframes once
  useEffect(() => {
    if (document.getElementById("bf-ticker-style")) return;
    const s = document.createElement("style");
    s.id = "bf-ticker-style";
    s.textContent = `
      @keyframes bf-marquee {
        0%   { transform: translateX(0); }
        100% { transform: translateX(-50%); }
      }
      .bf-ticker-track { animation: bf-marquee 28s linear infinite; }
      .bf-ticker-track:hover { animation-play-state: paused; }
    `;
    document.head.appendChild(s);
  }, []);

  const items = [...TICKER_BRANDS, ...TICKER_BRANDS]; // seamless loop

  return (
    <div style={{
      overflow: "hidden",
      borderTop: "1px solid var(--hairline)",
      borderBottom: "1px solid var(--hairline)",
      padding: "18px 0",
      background: "rgba(0,0,0,0.25)",
      position: "relative",
    }}>
      {/* fade edges */}
      <div style={{ position: "absolute", inset: 0, zIndex: 2, pointerEvents: "none",
        background: "linear-gradient(90deg, var(--canvas) 0%, transparent 8%, transparent 92%, var(--canvas) 100%)" }} />
      <div className="bf-ticker-track" style={{ display: "flex", gap: 0, width: "max-content" }}>
        {items.map(({ name, url, logo }, i) => (
          <a key={i} href={url} target="_blank" rel="noopener noreferrer"
            title={name}
            style={{
              display: "inline-flex", alignItems: "center", gap: 10,
              padding: "0 36px", textDecoration: "none", flexShrink: 0,
              borderRight: "1px solid var(--hairline)",
            }}>
            <img
              src={logo} alt={name}
              style={{ height: 22, width: "auto", maxWidth: 80, objectFit: "contain",
                filter: "brightness(0) invert(1)", opacity: 0.55,
                transition: "opacity 0.2s" }}
              onMouseEnter={e => e.target.style.opacity = "1"}
              onMouseLeave={e => e.target.style.opacity = "0.55"}
              onError={e => {
                e.target.style.display = "none";
                e.target.nextSibling.style.display = "block";
              }}
            />
            <span style={{ display: "none", font: "700 11px/1 var(--font-sans)", letterSpacing: "1.2px",
              textTransform: "uppercase", color: "var(--ink-muted)" }}>{name}</span>
          </a>
        ))}
      </div>
    </div>
  );
}

// ----- Top Nav -----
function TopNav({ onOpenGarage, savedCount, onReset, user, onLogout, plan, onUpgrade }) {
  return (
    <div className="topnav">
      <div className="brand-mark">
        <span className="brand-mark-icon"><Icon name="bolt" size={14} /></span>
        BIKEFORGE
      </div>
      <div style={{ display: "flex", alignItems: "center", gap: 4 }}>
        {user && (
          <span style={{
            font: "600 11px/1 var(--font-sans)",
            letterSpacing: "0.6px",
            color: "var(--ink-soft)",
            marginRight: 8,
            textTransform: "uppercase"
          }}>
            {user.name}
          </span>
        )}
        {plan === "pro" ? (
          <span style={{
            background: "var(--accent)", color: "var(--accent-ink)",
            font: "700 10px/1 var(--font-sans)", letterSpacing: "1.2px",
            textTransform: "uppercase", padding: "5px 10px", marginRight: 4
          }}>PRO</span>
        ) : (
          <button className="btn btn-ghost btn-sm" onClick={onUpgrade} style={{ marginRight: 4, boxShadow: "inset 0 0 0 1px var(--accent)", color: "var(--accent)" }}>
            Upgrade → Pro
          </button>
        )}
        <button className="btn btn-ghost btn-sm" onClick={onReset} title="Profil zurücksetzen">
          <Icon name="refresh" size={14} /> Reset
        </button>
        <button className="btn btn-ghost btn-sm" onClick={onOpenGarage}>
          <Icon name="list" size={14} /> Garage
          {savedCount > 0 && (
            <span style={{
              background: "var(--accent)", color: "var(--accent-ink)",
              padding: "2px 8px", fontSize: 10, fontWeight: 700,
              letterSpacing: "0.6px", marginLeft: 4
            }}>{savedCount}</span>
          )}
        </button>
        {onLogout && (
          <button className="btn btn-ghost btn-sm" onClick={onLogout} title="Abmelden">
            <Icon name="close" size={14} />
          </button>
        )}
      </div>
    </div>
  );
}

// ----- Upgrade Modal -----
function UpgradeModal({ plan, token, onClose }) {
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState(null);

  const handleCheckout = async () => {
    setLoading(true);
    setError(null);
    try {
      const res = await fetch("/api/checkout/session", {
        method: "POST",
        headers: { "Authorization": `Bearer ${token}`, "Content-Type": "application/json" }
      });
      const data = await res.json();
      if (!res.ok) { setError(data.error || "Fehler beim Erstellen der Checkout-Session."); setLoading(false); return; }
      window.location.href = data.url;
    } catch {
      setError("Netzwerkfehler. Bitte versuche es erneut.");
      setLoading(false);
    }
  };

  const features = [
    { label: "Builds speichern",     free: "3 Builds",    pro: "Unbegrenzt" },
    { label: "Komponenten-Tausch",   free: true,          pro: true },
    { label: "Garage & Verlauf",     free: true,          pro: true },
    { label: "Build-Vergleich",      free: false,         pro: true },
    { label: "PDF-Export",           free: false,         pro: true },
    { label: "Prioritäts-Support",   free: false,         pro: true },
  ];

  return (
    <>
      <div className="drawer-backdrop" onClick={onClose} style={{ zIndex: 900 }} />
      <div style={{
        position: "fixed", top: "50%", left: "50%",
        transform: "translate(-50%, -50%)",
        zIndex: 901, background: "var(--canvas-card)",
        border: "1px solid var(--hairline)",
        width: "min(520px, 95vw)", maxHeight: "90vh", overflowY: "auto"
      }}>
        <div style={{ padding: "32px 40px 40px" }}>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start", marginBottom: 32 }}>
            <div>
              <div className="eyebrow eyebrow-accent" style={{ marginBottom: 8 }}>Upgrade</div>
              <h2 style={{ font: "500 28px/1.1 var(--font-sans)", letterSpacing: "-0.56px" }}>BikeForge Pro</h2>
            </div>
            <button className="btn btn-ghost btn-sm" onClick={onClose}><Icon name="close" size={16} /></button>
          </div>

          <div style={{ display: "grid", gridTemplateColumns: "1fr auto auto", gap: "0 24px", marginBottom: 32 }}>
            <div style={{ font: "600 10px/1 var(--font-sans)", letterSpacing: "1px", color: "var(--ink-muted)", textTransform: "uppercase", paddingBottom: 10, borderBottom: "1px solid var(--hairline)" }}>Feature</div>
            <div style={{ font: "600 10px/1 var(--font-sans)", letterSpacing: "1px", color: "var(--ink-muted)", textTransform: "uppercase", paddingBottom: 10, borderBottom: "1px solid var(--hairline)", textAlign: "center" }}>Free</div>
            <div style={{ font: "600 10px/1 var(--font-sans)", letterSpacing: "1px", color: "var(--accent)", textTransform: "uppercase", paddingBottom: 10, borderBottom: "1px solid var(--accent)" }}>Pro</div>
            {features.map((f, i) => (
              <React.Fragment key={i}>
                <div style={{ padding: "14px 0", borderBottom: "1px solid var(--hairline)", font: "400 14px/1 var(--font-sans)", color: "var(--ink)" }}>{f.label}</div>
                <div style={{ padding: "14px 0", borderBottom: "1px solid var(--hairline)", textAlign: "center", color: "var(--ink-muted)", font: "500 13px/1 var(--font-sans)" }}>
                  {f.free === true ? "✓" : f.free === false ? "—" : f.free}
                </div>
                <div style={{ padding: "14px 0", borderBottom: "1px solid var(--hairline)", textAlign: "center", color: "var(--accent)", font: "700 13px/1 var(--font-sans)" }}>
                  {f.pro === true ? "✓" : f.pro === false ? "—" : f.pro}
                </div>
              </React.Fragment>
            ))}
          </div>

          <div style={{ textAlign: "center", marginBottom: 24 }}>
            <div style={{ font: "500 42px/1 var(--font-sans)", letterSpacing: "-0.84px" }}>3,99 €</div>
            <div style={{ font: "400 13px/1 var(--font-sans)", color: "var(--ink-muted)", marginTop: 6 }}>/ Monat · jederzeit kündbar</div>
          </div>

          {error && (
            <div style={{ background: "var(--accent-soft)", border: "1px solid var(--accent)", padding: "10px 16px", marginBottom: 16, font: "500 13px/1.4 var(--font-sans)", color: "var(--ink)" }}>
              {error}
            </div>
          )}

          <button className="btn btn-primary" onClick={handleCheckout} disabled={loading}
            style={{ width: "100%", justifyContent: "center", padding: "18px 0", fontSize: 14 }}>
            {loading ? "Weiterleitung …" : "Jetzt upgraden → Pro"}
          </button>

          <div style={{ textAlign: "center", marginTop: 14, font: "400 12px/1 var(--font-sans)", color: "var(--ink-muted)" }}>
            Sichere Zahlung über Stripe · Keine versteckten Kosten
          </div>
        </div>
      </div>
    </>
  );
}

// ----- Profil-Eingabe -----
function ProfileForm({ profile, onChange }) {
  const set = (k, v) => onChange({ ...profile, [k]: v });
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 28 }}>
      <div>
        <div className="eyebrow" style={{ marginBottom: 18 }}>01 — Fahrer-Profil</div>
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 24 }}>
          <SliderField label="Gewicht" min={50} max={130} step={1} unit="kg"
            value={profile.weight} onChange={(v) => set("weight", v)} />
          <SliderField label="Größe" min={150} max={205} step={1} unit="cm"
            value={profile.height} onChange={(v) => set("height", v)} />
        </div>
      </div>

      <hr className="divider" />

      <div>
        <div className="eyebrow" style={{ marginBottom: 14 }}>02 — Antrieb</div>
        <Segmented options={[
          { value: "analog", label: "Bio" },
          { value: "ebike", label: "E-Bike" }
        ]} value={profile.bike_type || "analog"} onChange={(v) => set("bike_type", v)} />
      </div>

      <div>
        <div className="eyebrow" style={{ marginBottom: 14 }}>03 — Skill</div>
        <Segmented options={SKILL_OPTIONS} value={profile.skill_level} onChange={(v) => set("skill_level", v)} />
      </div>

      <div>
        <div className="eyebrow" style={{ marginBottom: 14 }}>04 — Fahrstil</div>
        <Segmented options={STYLE_OPTIONS} value={profile.riding_style} onChange={(v) => set("riding_style", v)} />
      </div>

      <div>
        <div className="eyebrow" style={{ marginBottom: 14 }}>05 — Einsatzbereich</div>
        <Segmented accent options={TERRAIN_OPTIONS} value={profile.terrain} onChange={(v) => set("terrain", v)} />
      </div>

      <hr className="divider" />

      <div>
        <div className="eyebrow" style={{ marginBottom: 18 }}>06 — Budget</div>
        <SliderField label="Budget" min={1200} max={8000} step={100} unit="€"
          value={profile.budget} onChange={(v) => set("budget", v)} large />
      </div>
    </div>
  );
}

function SliderField({ label, min, max, step, unit, value, onChange, large }) {
  return (
    <div className="field">
      <div className="field-label">{label}</div>
      <div className="slider-row">
        <input type="range" min={min} max={max} step={step} value={value}
          onChange={(e) => onChange(Number(e.target.value))} />
        <div className="slider-readout" style={large ? { fontSize: 32 } : null}>
          {unit === "€" ? fmtNum(value) : value}
          <span className="field-suffix" style={{ marginLeft: 6, fontSize: 11 }}>{unit}</span>
        </div>
      </div>
    </div>
  );
}

function Segmented({ options, value, onChange, accent }) {
  return (
    <div className={"seg" + (accent ? " seg-accent" : "")}>
      {options.map(o => (
        <button key={o.value} className={value === o.value ? "is-on" : ""} onClick={() => onChange(o.value)}>
          {o.label}
        </button>
      ))}
    </div>
  );
}

// ----- Component card -----
function ComponentCard({ slot, item, onClick }) {
  if (!item) return null;
  const high = item.level === "high" || item.level === "mid_high";
  return (
    <div className="comp-card" onClick={onClick}>
      <div className="comp-card-icon"><Icon name={slot} size={28} /></div>
      <div className="comp-card-body">
        <div className="comp-card-cat">{SLOT_LABELS[slot]}</div>
        <div className="comp-card-name">{item.name}</div>
        <div style={{ display: "flex", alignItems: "center", gap: 6, marginTop: 4, flexWrap: "wrap" }}>
          {item.level && <span className={"comp-card-level-pill" + (high ? " is-high" : "")}>{item.level.replace("_", "-")}</span>}
          <BrandBadge name={item.name} size="sm" />
        </div>
      </div>
      <div style={{ display: "flex", flexDirection: "column", alignItems: "flex-end", gap: 4 }}>
        <div className="comp-card-price">{fmtPrice(item.price || 0)}</div>
        <div style={{ color: "var(--ink-muted)" }}><Icon name="chevron" size={14} /></div>
      </div>
    </div>
  );
}

// ----- Spec cell (Ferrari big-number) -----
function SpecCell({ label, value, unit, accent, mega }) {
  return (
    <div className={"spec" + (accent ? " spec-accent" : "") + (mega ? " spec-mega" : "")}>
      <div className="spec-label">{label}</div>
      <div>
        <span className="spec-value">{value}</span>
        {unit && <span className="spec-unit">{unit}</span>}
      </div>
    </div>
  );
}

// ----- Score panel -----
function ScorePanel({ scores }) {
  const items = [
    { key: "fit_score", label: "FIT", v: scores.fit_score, accent: true },
    { key: "performance_score", label: "PERFORMANCE", v: scores.performance_score },
    { key: "value_score", label: "VALUE", v: scores.value_score }
  ];
  return (
    <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 32 }}>
      {items.map(it => (
        <div key={it.key}>
          <div className="spec-label" style={{ marginBottom: 12 }}>{it.label}</div>
          <div style={{ display: "flex", alignItems: "baseline", gap: 4 }}>
            <span style={{ font: "500 56px/1 var(--font-sans)", letterSpacing: "-1.12px", fontVariantNumeric: "tabular-nums", color: it.accent ? "var(--accent)" : "var(--ink)" }}>
              {it.v.toFixed(1)}
            </span>
            <span className="spec-unit" style={{ color: "var(--ink-muted)" }}>/10</span>
          </div>
          <div className="score-bar">
            <div className={"score-bar-fill" + (it.accent ? " is-accent" : "")} style={{ width: `${it.v * 10}%` }} />
          </div>
        </div>
      ))}
    </div>
  );
}

// ----- Warnings -----
function Warnings({ warnings }) {
  if (!warnings || warnings.length === 0) return null;
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
      {warnings.map((w, i) => (
        <div key={i} style={{
          display: "flex", alignItems: "flex-start", gap: 10,
          padding: "12px 14px",
          background: w.kind === "safety" ? "var(--accent-soft)" : "rgba(255,255,255,0.04)",
          borderLeft: `2px solid ${w.kind === "safety" ? "var(--accent)" : "var(--ink-soft)"}`,
          color: "var(--ink)"
        }}>
          <Icon name={w.kind === "safety" ? "warn" : "info"} size={16} />
          <span style={{ font: "500 13px/1.4 var(--font-sans)" }}>{w.msg}</span>
        </div>
      ))}
    </div>
  );
}

// ----- Drawer für Komponenten-Tausch -----
function ComponentDrawer({ slot, currentItem, pool, frame, profile, onPick, onClose }) {
  if (!slot || !pool) return null;
  // Filter passende Komponenten (gleiche Kompat.-Regeln wie engine)
  let candidates = pool.slice();
  if (slot === "wheels") {
    candidates = candidates.filter(w => (!w.wheel_sizes || w.wheel_sizes.includes(frame.wheel_size)) && (!w.axle_front || w.axle_front === frame.axle_front));
  } else if (slot === "fork") {
    candidates = candidates.filter(f => (!f.wheel_sizes || f.wheel_sizes.includes(frame.wheel_size)) && (!f.axle || f.axle === frame.axle_front));
  } else if (slot === "tires") {
    candidates = candidates.filter(t => !t.terrain || t.terrain === profile.terrain);
  } else if (slot === "dropper") {
    candidates = candidates.filter(d => !d.seatpost_diameters || d.seatpost_diameters.includes(frame.seatpost_diameter));
  } else if (slot === "shock" && frame.shock_size_class) {
    candidates = candidates.filter(s => !s.size_class || s.size_class.includes(frame.shock_size_class));
  }
  candidates.sort((a, b) => (a.price || 0) - (b.price || 0));

  return (
    <>
      <div className="drawer-backdrop" onClick={onClose} />
      <div className="drawer scroll-area">
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", padding: "24px 32px", borderBottom: "1px solid var(--hairline)", flex: "0 0 auto" }}>
          <div>
            <div className="eyebrow eyebrow-accent">Komponente wählen</div>
            <h3 style={{ marginTop: 6 }}>{SLOT_LABELS[slot]}</h3>
          </div>
          <button className="btn btn-ghost btn-sm" onClick={onClose}><Icon name="close" size={14} /></button>
        </div>
        <div style={{ padding: 24, display: "flex", flexDirection: "column", gap: 6 }}>
          {candidates.map(c => (
            <div key={c.id}
              className={"comp-card" + (currentItem && currentItem.id === c.id ? " is-active" : "")}
              onClick={() => onPick(c)}>
              <div className="comp-card-icon"><Icon name={slot} size={26} /></div>
              <div className="comp-card-body">
                <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 2 }}>
                  <div className="comp-card-cat">{c.level ? c.level.replace("_", "-") : ""}</div>
                  <BrandBadge name={c.name} size="lg" />
                </div>
                <div className="comp-card-name">{c.name}</div>
                <div style={{ display: "flex", gap: 12, marginTop: 6, flexWrap: "wrap" }}>
                  {c.power != null && <SpecMini label="Power" value={`${c.power}/10`} />}
                  {c.range != null && <SpecMini label="Range" value={`${c.range}/10`} />}
                  {c.stiffness != null && <SpecMini label="Steif." value={`${c.stiffness}/10`} />}
                  {c.performance != null && <SpecMini label="Perf." value={`${c.performance}/10`} />}
                  {c.travel_mm != null && <SpecMini label="Travel" value={`${c.travel_mm}mm`} />}
                  {c.weight_g != null && <SpecMini label="Gewicht" value={`${c.weight_g}g`} />}
                </div>
              </div>
              <div className="comp-card-price">{fmtPrice(c.price || 0)}</div>
            </div>
          ))}
          {candidates.length === 0 && <div style={{ color: "var(--ink-muted)", padding: 24 }}>Keine kompatiblen Komponenten gefunden.</div>}
        </div>
      </div>
    </>
  );
}

function SpecMini({ label, value }) {
  return (
    <span style={{ font: "600 10px/1.2 var(--font-sans)", letterSpacing: "0.6px", color: "var(--ink-muted)", textTransform: "uppercase" }}>
      {label} <span style={{ color: "var(--ink)", fontWeight: 700, fontVariantNumeric: "tabular-nums" }}>{value}</span>
    </span>
  );
}

// ----- Saved Builds Drawer -----
function GarageDrawer({ saved, onClose, onLoad, onDelete, onCompare, user, onLogout, plan, onUpgrade }) {
  return (
    <>
      <div className="drawer-backdrop" onClick={onClose} />
      <div className="drawer scroll-area" style={{ display: "flex", flexDirection: "column" }}>
        {/* Header */}
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", padding: "24px 32px", borderBottom: "1px solid var(--hairline)", flex: "0 0 auto" }}>
          <div>
            <div className="eyebrow eyebrow-accent">Meine Builds</div>
            <h3 style={{ marginTop: 6 }}>Garage</h3>
          </div>
          <div style={{ display: "flex", gap: 8 }}>
            {saved.length >= 2 && onCompare && (
              <button className="btn btn-primary btn-sm" onClick={onCompare}>
                <Icon name="compare" size={14} /> Vergleichen
              </button>
            )}
            <button className="btn btn-ghost btn-sm" onClick={onClose}><Icon name="close" size={14} /></button>
          </div>
        </div>

        {/* Build list */}
        <div style={{ padding: 24, display: "flex", flexDirection: "column", gap: 14, flex: 1 }}>
          {saved.length === 0 && (
            <div style={{ textAlign: "center", color: "var(--ink-muted)", padding: 48 }}>
              <Icon name="frame" size={48} />
              <div className="eyebrow" style={{ marginTop: 14 }}>Noch keine Builds gespeichert</div>
              <p style={{ marginTop: 8, color: "var(--ink-muted)" }}>Konfiguriere ein Bike und speichere deinen Build hier.</p>
            </div>
          )}
          {saved.map(s => (
            <div key={s.id} style={{ background: "var(--canvas-card)", border: "1px solid var(--hairline)", padding: 0 }}>
              {s.thumbnail && <div style={{ height: 140, background: `url(${s.thumbnail}) center/cover`, borderBottom: "1px solid var(--hairline)" }} />}
              <div style={{ padding: 18 }}>
                <div className="eyebrow eyebrow-accent">{s.profile.terrain.toUpperCase()} · {s.profile.riding_style}</div>
                <div style={{ font: "500 18px/1.3 var(--font-sans)", marginTop: 4 }}>{s.title}</div>
                <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: 12, marginTop: 14 }}>
                  <SpecMini label="Preis" value={fmtPrice(s.total)} />
                  <SpecMini label="Fit" value={`${s.scores.fit_score}/10`} />
                  <SpecMini label="Perf" value={`${s.scores.performance_score}/10`} />
                </div>
                <div style={{ display: "flex", gap: 8, marginTop: 16 }}>
                  <button className="btn btn-primary btn-sm" onClick={() => onLoad(s)}>Laden</button>
                  <button className="btn btn-outline btn-sm" onClick={() => onDelete(s.id)}>Löschen</button>
                </div>
              </div>
            </div>
          ))}
        </div>

        {/* User profile footer */}
        {user && (
          <div style={{ flex: "0 0 auto", borderTop: "1px solid var(--hairline)", padding: "20px 32px" }}>
            <div style={{ display: "flex", alignItems: "center", gap: 14 }}>
              <div style={{
                width: 40, height: 40, background: "var(--accent)",
                display: "grid", placeItems: "center",
                font: "700 15px/1 var(--font-sans)", color: "var(--accent-ink)", flexShrink: 0
              }}>
                {user.name.charAt(0).toUpperCase()}
              </div>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ font: "600 14px/1 var(--font-sans)", color: "var(--ink)", overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
                  {user.name}
                </div>
                <div style={{ font: "400 12px/1 var(--font-sans)", color: "var(--ink-muted)", marginTop: 4, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }}>
                  {user.email}
                </div>
                {plan === "pro" ? (
                  <span style={{ display: "inline-block", marginTop: 6, background: "var(--accent)", color: "var(--accent-ink)", font: "700 9px/1 var(--font-sans)", letterSpacing: "1px", textTransform: "uppercase", padding: "3px 8px" }}>PRO</span>
                ) : (
                  <button onClick={onUpgrade} style={{ display: "inline-block", marginTop: 6, background: "none", border: "none", padding: 0, cursor: "pointer", font: "600 11px/1 var(--font-sans)", color: "var(--accent)", letterSpacing: "0.4px" }}>
                    → Upgrade auf Pro
                  </button>
                )}
              </div>
              <button className="btn btn-ghost btn-sm" onClick={onLogout} title="Abmelden" style={{ flexShrink: 0 }}>
                <Icon name="close" size={14} /> Abmelden
              </button>
            </div>
          </div>
        )}
      </div>
    </>
  );
}

window.BikeUI = {
  TopNav, ProfileForm, ComponentCard, SpecCell, ScorePanel, Warnings,
  ComponentDrawer, GarageDrawer, UpgradeModal, BrandTicker,
  SpecMini, Segmented, SliderField
};
