// Full-viewport Split-Layout Website — responsive, mit Tweaks

function ServiceRow({ s, i, accent, mobile }) {
  const [open, setOpen] = React.useState(false);
  return (
    <div
      style={{
        borderTop:
          i === 0 ? `1px solid ${KS.charcoal}` : `1px solid ${KS.border}`,
      }}
    >
      {mobile ? (
        <button
          onClick={() => setOpen((o) => !o)}
          aria-expanded={open}
          style={{
            width: "100%",
            padding: "18px 0",
            background: "transparent",
            border: "none",
            cursor: "pointer",
            textAlign: "left",
            color: "inherit",
            fontFamily: "inherit",
            display: "flex",
            flexDirection: "column",
            gap: 6,
          }}
        >
          <div
            style={{
              display: "flex",
              alignItems: "center",
              gap: 12,
              width: "100%",
            }}
          >
            <span
              style={{
                fontFamily: KSFonts.mono,
                fontSize: 11,
                color: accent,
                flexShrink: 0,
                width: 20,
              }}
            >
              0{i + 1}
            </span>
            <span
              style={{
                fontSize: 15,
                fontWeight: 700,
                color: KS.black,
                letterSpacing: -0.3,
                flex: 1,
                minWidth: 0,
              }}
            >
              {s.k}
            </span>
            <span
              style={{
                fontFamily: KSFonts.mono,
                fontSize: 16,
                color: KS.muted,
                transition: "transform 200ms ease",
                transform: open ? "rotate(45deg)" : "rotate(0deg)",
                lineHeight: 1,
                flexShrink: 0,
              }}
            >
              +
            </span>
          </div>
          <div
            style={{
              fontSize: 13,
              color: KS.charcoal,
              lineHeight: 1.5,
              paddingLeft: 32,
              paddingRight: 20,
            }}
          >
            {s.v}
          </div>
        </button>
      ) : (
        <button
          onClick={() => setOpen((o) => !o)}
          aria-expanded={open}
          style={{
            width: "100%",
            padding: "18px 0",
            background: "transparent",
            border: "none",
            cursor: "pointer",
            textAlign: "left",
            color: "inherit",
            fontFamily: "inherit",
            display: "grid",
            gridTemplateColumns: "44px 160px 1fr 24px",
            gap: 16,
            alignItems: "baseline",
          }}
        >
          <span
            style={{ fontFamily: KSFonts.mono, fontSize: 11, color: accent }}
          >
            0{i + 1}
          </span>
          <span
            style={{
              fontSize: 17,
              fontWeight: 700,
              color: KS.black,
              letterSpacing: -0.3,
            }}
          >
            {s.k}
          </span>
          <span style={{ fontSize: 14, color: KS.charcoal, lineHeight: 1.5 }}>
            {s.v}
          </span>
          <span
            style={{
              fontFamily: KSFonts.mono,
              fontSize: 16,
              color: KS.muted,
              transition: "transform 200ms ease",
              transform: open ? "rotate(45deg)" : "rotate(0deg)",
              justifySelf: "end",
              alignSelf: "center",
              lineHeight: 1,
            }}
          >
            +
          </span>
        </button>
      )}
      <div
        style={{
          display: "grid",
          gridTemplateRows: open ? "1fr" : "0fr",
          transition: "grid-template-rows 260ms ease",
        }}
      >
        <div style={{ overflow: "hidden" }}>
          <div
            style={{
              padding: mobile ? "0 0 20px 40px" : "2px 80px 26px 80px",
              fontSize: mobile ? 14 : 15,
              lineHeight: 1.65,
              color: KS.charcoal,
              maxWidth: 640,
              margin: mobile ? 0 : "0 auto",
              textWrap: "pretty",
            }}
          >
            {s.detail}
          </div>
        </div>
      </div>
    </div>
  );
}

function Site({ lang, setLang, tweaks }) {
  const t = COPY[lang];
  const latest = useLatestPost();
  const [mobile, setMobile] = React.useState(() => window.innerWidth < 880);
  React.useEffect(() => {
    const h = () => setMobile(window.innerWidth < 880);
    window.addEventListener("resize", h);
    return () => window.removeEventListener("resize", h);
  }, []);

  const accent = tweaks.accent;
  const leftDark = tweaks.leftDark;
  const showPixel = tweaks.showPixel;
  const density = tweaks.density;

  // derive left-column palette
  const LEFT_BG = leftDark ? KS.charcoal : KS.offwhite;
  const LEFT_FG = leftDark ? KS.offwhite : KS.charcoal;
  const LEFT_MUTED = leftDark ? "rgba(248,247,246,0.6)" : KS.muted;
  const LEFT_BORDER = leftDark ? "rgba(248,247,246,0.14)" : KS.border;

  return (
    <div
      style={{
        minHeight: "100vh",
        background: KS.offwhite,
        color: KS.charcoal,
        fontFamily: KSFonts.body,
        display: "grid",
        gridTemplateColumns: mobile ? "1fr" : "minmax(340px, 42vw) 1fr",
        gridTemplateAreas: mobile ? '"main" "aside"' : '"aside main"',
      }}
    >
      {/* LEFT COLUMN */}
      <aside
        style={{
          gridArea: "aside",
          display: mobile ? "none" : "flex",
          background: LEFT_BG,
          color: LEFT_FG,
          position: mobile ? "relative" : "sticky",
          top: 0,
          height: mobile ? "auto" : "100vh",
          flexDirection: "column",
          overflow: "hidden",
        }}
      >
        <NodeGraph
          color={LEFT_FG}
          opacity={leftDark ? 0.1 : 0.08}
          width={500}
          height={900}
          seed={7}
          density={density}
        />

        {!mobile && (
          <div
            style={{
              position: "relative",
              padding: "24px 32px 0",
              display: "flex",
              gap: 10,
              flexWrap: "wrap",
              justifyContent: "center",
            }}
          >
            <a
              href="events.html"
              style={{
                display: "inline-flex",
                alignItems: "center",
                gap: 10,
                padding: "10px 18px",
                border: `1px solid ${accent}`,
                background: "transparent",
                color: leftDark ? KS.offwhite : KS.charcoal,
                textDecoration: "none",
                fontFamily: KSFonts.mono,
                fontSize: 11,
                letterSpacing: 1.2,
                textTransform: "uppercase",
                transition: "background 150ms ease, color 150ms ease",
              }}
              onMouseEnter={(e) => {
                e.currentTarget.style.background = accent;
                e.currentTarget.style.color = "#fff";
              }}
              onMouseLeave={(e) => {
                e.currentTarget.style.background = "transparent";
                e.currentTarget.style.color = leftDark
                  ? KS.offwhite
                  : KS.charcoal;
              }}
            >
              <span
                style={{
                  display: "inline-block",
                  width: 6,
                  height: 6,
                  background: accent,
                  borderRadius: 3,
                }}
              />
              <span>{t.eventsNav}</span>
              <span style={{ opacity: 0.7 }}>↗</span>
            </a>
            <a
              href="stufen.html"
              style={{
                display: "inline-flex",
                alignItems: "center",
                gap: 10,
                padding: "10px 18px",
                background: accent,
                color: "#fff",
                textDecoration: "none",
                fontFamily: KSFonts.mono,
                fontSize: 11,
                letterSpacing: 1.2,
                textTransform: "uppercase",
                transition: "filter 150ms ease",
              }}
              onMouseEnter={(e) =>
                (e.currentTarget.style.filter = "brightness(0.92)")
              }
              onMouseLeave={(e) =>
                (e.currentTarget.style.filter = "brightness(1)")
              }
            >
              <span
                style={{
                  display: "inline-block",
                  width: 6,
                  height: 6,
                  background: "#fff",
                  borderRadius: 3,
                }}
              />
              <span>5 Stufen der KI-Nutzung</span>
              <span style={{ opacity: 0.85 }}>↗</span>
            </a>
          </div>
        )}

        <div
          style={{
            position: "relative",
            padding: "28px 32px",
            flex: 1,
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
          }}
        >
          <div
            style={{
              width: "100%",
              maxWidth: 420,
              aspectRatio: "3/4",
              background: KS.blush,
              overflow: "hidden",
              position: "relative",
              boxShadow: leftDark
                ? "0 30px 60px -20px rgba(0,0,0,0.5)"
                : "0 20px 50px -20px rgba(0,0,0,0.2)",
            }}
          >
            <img
              src={PHOTO_URL}
              alt="Kilian Springer"
              style={{
                width: "100%",
                height: "100%",
                objectFit: "cover",
                display: "block",
                filter: "saturate(0.95)",
              }}
            />
            <div
              style={{
                position: "absolute",
                left: 0,
                bottom: 20,
                background: accent,
                color: "#fff",
                fontFamily: KSFonts.mono,
                fontSize: 10,
                letterSpacing: 1.5,
                textTransform: "uppercase",
                padding: "6px 14px",
              }}
            >
              {t.availableFor}
            </div>
          </div>
        </div>

        <div
          style={{
            position: "relative",
            padding: "0 32px 24px",
            display: "flex",
            justifyContent: "flex-end",
            alignItems: "flex-end",
          }}
        >
          <div
            style={{
              fontFamily: KSFonts.mono,
              fontSize: 10,
              letterSpacing: 1,
              color: LEFT_MUTED,
            }}
          >
            kilian-springer.com
          </div>
        </div>
      </aside>

      {/* RIGHT COLUMN */}
      <main
        style={{
          gridArea: "main",
          padding: mobile ? "28px 24px 40px" : "48px 64px 56px",
          display: "flex",
          flexDirection: "column",
          minHeight: mobile ? "auto" : "100vh",
        }}
      >
        {/* top bar */}
        <div
          style={{
            display: "flex",
            justifyContent: "space-between",
            alignItems: "center",
            marginBottom: mobile ? 32 : 56,
          }}
        >
          <div
            style={{
              fontFamily: KSFonts.mono,
              fontSize: 11,
              letterSpacing: 1,
              textTransform: "uppercase",
              color: KS.muted,
            }}
          >
            <span
              style={{
                display: "inline-block",
                width: 6,
                height: 6,
                borderRadius: 3,
                background: KS.midGreen,
                marginRight: 8,
                verticalAlign: "middle",
              }}
            />
            {t.nowLabel}
          </div>
          <LangToggle lang={lang} setLang={setLang} />
        </div>

        {/* mobile-only: compact photo + events link */}
        {mobile && (
          <div
            style={{
              display: "flex",
              alignItems: "center",
              gap: 14,
              marginBottom: 28,
            }}
          >
            <div
              style={{
                width: 56,
                height: 56,
                borderRadius: 28,
                overflow: "hidden",
                flexShrink: 0,
                background: KS.blush,
                border: `2px solid ${accent}`,
              }}
            >
              <img
                src={PHOTO_URL}
                alt="Kilian Springer"
                style={{
                  width: "100%",
                  height: "100%",
                  objectFit: "cover",
                  display: "block",
                }}
              />
            </div>
            <a
              href="events.html"
              style={{
                display: "inline-flex",
                alignItems: "center",
                gap: 8,
                padding: "8px 14px",
                border: `1px solid ${accent}`,
                color: KS.charcoal,
                textDecoration: "none",
                fontFamily: KSFonts.mono,
                fontSize: 10.5,
                letterSpacing: 1.2,
                textTransform: "uppercase",
              }}
            >
              <span
                style={{
                  display: "inline-block",
                  width: 5,
                  height: 5,
                  background: accent,
                  borderRadius: 3,
                }}
              />
              {t.eventsNav}
              <span style={{ opacity: 0.6 }}>↗</span>
            </a>
            <a
              href="stufen.html"
              style={{
                display: "inline-flex",
                alignItems: "center",
                gap: 8,
                padding: "8px 14px",
                background: accent,
                color: "#fff",
                textDecoration: "none",
                fontFamily: KSFonts.mono,
                fontSize: 10.5,
                letterSpacing: 1.2,
                textTransform: "uppercase",
                marginLeft: 10,
              }}
            >
              <span
                style={{
                  display: "inline-block",
                  width: 5,
                  height: 5,
                  background: "#fff",
                  borderRadius: 3,
                }}
              />
              5 Stufen
              <span style={{ opacity: 0.85 }}>↗</span>
            </a>
          </div>
        )}

        <h1
          style={{
            fontSize: mobile ? 52 : 96,
            fontWeight: 800,
            letterSpacing: mobile ? -2 : -4,
            lineHeight: 0.92,
            color: KS.black,
            margin: 0,
            marginBottom: 18,
            textWrap: "balance",
          }}
        >
          Kilian
          <br />
          Springer.
        </h1>

        <div
          style={{
            fontSize: mobile ? 15 : 17,
            color: KS.charcoal,
            marginBottom: mobile ? 24 : 36,
            fontWeight: 500,
          }}
        >
          {t.role}
        </div>

        <p
          style={{
            fontSize: mobile ? 14 : 15,
            lineHeight: 1.6,
            color: KS.charcoal,
            margin: 0,
            marginBottom: mobile ? 32 : 44,
            maxWidth: 560,
            textWrap: "pretty",
          }}
        >
          {t.introLong}
        </p>

        {/* Services list */}
        <div style={{ marginBottom: mobile ? 32 : 44 }}>
          <div
            style={{
              fontFamily: KSFonts.mono,
              fontSize: 10,
              letterSpacing: 1.5,
              textTransform: "uppercase",
              color: KS.muted,
              marginBottom: 18,
            }}
          >
            {t.servicesTitle} — 04
          </div>
          <div style={{ display: "grid", gap: 0 }}>
            {t.services.map((s, i) => (
              <ServiceRow
                key={s.k}
                s={s}
                i={i}
                accent={accent}
                mobile={mobile}
              />
            ))}
            <div style={{ borderTop: `1px solid ${KS.border}`, height: 0 }} />
          </div>
        </div>

        {/* Latest post */}
        {(() => {
          const p = latest.post || {};
          const date = p.pubDate ? new Date(p.pubDate) : null;
          const dateStr =
            date && !isNaN(date)
              ? date.toLocaleDateString(lang === "de" ? "de-DE" : "en-US", {
                  day: "2-digit",
                  month: "short",
                  year: "numeric",
                })
              : null;
          return (
            <a
              href={p.link || "https://kiliansblog.substack.com/"}
              target="_blank"
              rel="noreferrer"
              style={{
                display: "grid",
                gridTemplateColumns: p.image
                  ? mobile
                    ? "1fr"
                    : "180px 1fr auto"
                  : mobile
                    ? "1fr"
                    : "1fr auto",
                gap: 0,
                textDecoration: "none",
                background: "#fff",
                border: `1px solid ${KS.border}`,
                marginBottom: mobile ? 28 : 40,
                transition: "transform 150ms ease, box-shadow 150ms ease",
                overflow: "hidden",
              }}
              onMouseEnter={(e) => {
                e.currentTarget.style.transform = "translateY(-1px)";
                e.currentTarget.style.boxShadow = "0 6px 24px rgba(0,0,0,0.06)";
              }}
              onMouseLeave={(e) => {
                e.currentTarget.style.transform = "translateY(0)";
                e.currentTarget.style.boxShadow = "none";
              }}
            >
              {p.image && (
                <div
                  style={{
                    width: mobile ? "100%" : 180,
                    aspectRatio: mobile ? "16 / 9" : "auto",
                    minHeight: mobile ? 0 : 140,
                    background: `url(${p.image}) center/cover no-repeat, ${KS.blush}`,
                    borderRight: mobile ? "none" : `1px solid ${KS.border}`,
                    borderBottom: mobile ? `1px solid ${KS.border}` : "none",
                  }}
                />
              )}
              <div
                style={{
                  padding: mobile
                    ? "18px 20px"
                    : "clamp(16px, 2.2cqw, 26px) clamp(20px, 2.8cqw, 32px)",
                  borderLeft: p.image ? "none" : `4px solid ${accent}`,
                  display: "flex",
                  flexDirection: "column",
                  justifyContent: "center",
                  minWidth: 0,
                  containerType: "inline-size",
                }}
              >
                <div
                  style={{
                    fontFamily: KSFonts.mono,
                    fontSize: mobile ? 10 : "clamp(9.5px, 1.05cqw, 11px)",
                    letterSpacing: 1.2,
                    textTransform: "uppercase",
                    color: KS.muted,
                    marginBottom: 10,
                    display: "flex",
                    gap: 8,
                    alignItems: "center",
                    whiteSpace: "nowrap",
                    overflow: "hidden",
                    textOverflow: "ellipsis",
                  }}
                >
                  <span>{t.latestPost}</span>
                  {dateStr && <span>· {dateStr}</span>}
                  {latest.loading && <span>· lädt…</span>}
                </div>
                <div
                  style={{
                    fontSize: mobile ? 15 : "clamp(14px, 1.8cqw, 17px)",
                    fontWeight: 600,
                    color: KS.black,
                    lineHeight: 1.3,
                    letterSpacing: -0.2,
                    textWrap: "balance",
                    marginBottom: p.summary ? 8 : 0,
                  }}
                >
                  {p.title || "Kilians Blog auf Substack"}
                </div>
                {p.summary && (
                  <div
                    style={{
                      fontSize: mobile ? 13 : "clamp(12px, 1.35cqw, 14px)",
                      color: KS.muted,
                      lineHeight: 1.5,
                      display: "-webkit-box",
                      WebkitLineClamp: 2,
                      WebkitBoxOrient: "vertical",
                      overflow: "hidden",
                    }}
                  >
                    {p.summary}
                  </div>
                )}
              </div>
              {!mobile && (
                <div
                  style={{
                    padding: "0 22px",
                    display: "flex",
                    alignItems: "center",
                    fontSize: 22,
                    color: accent,
                  }}
                >
                  →
                </div>
              )}
            </a>
          );
        })()}

        {/* Elsewhere + pixel signature */}
        <div
          style={{
            display: "grid",
            gridTemplateColumns: mobile ? "1fr" : "1fr auto",
            gap: mobile ? 24 : 44,
            marginBottom: mobile ? 32 : 40,
            alignItems: "end",
          }}
        >
          <div>
            <div
              style={{
                fontFamily: KSFonts.mono,
                fontSize: 10,
                letterSpacing: 1.5,
                textTransform: "uppercase",
                color: KS.muted,
                marginBottom: 14,
              }}
            >
              {t.elsewhere}
            </div>
            <div style={{ fontSize: 14, display: "grid", gap: 6 }}>
              <a
                href="https://www.linkedin.com/in/kilianspringer/"
                target="_blank"
                rel="noreferrer"
                style={{ color: KS.charcoal, textDecoration: "none" }}
              >
                LinkedIn ↗
              </a>
              <a
                href="https://www.instagram.com/kilian.springer/"
                target="_blank"
                rel="noreferrer"
                style={{ color: KS.charcoal, textDecoration: "none" }}
              >
                Instagram ↗
              </a>
              <a
                href="https://kiliansblog.substack.com/"
                target="_blank"
                rel="noreferrer"
                style={{ color: KS.charcoal, textDecoration: "none" }}
              >
                Blog ↗
              </a>
            </div>
          </div>
          {!mobile && (
            <div
              style={{
                display: "flex",
                alignItems: "flex-end",
                justifyContent: "flex-end",
                alignSelf: "stretch",
                position: "relative",
                overflow: "visible",
                marginBottom: -40, // drop him down so his feet land on the divider line below
              }}
            >
              <PixelKilian size={128} />
            </div>
          )}
        </div>

        {/* CTA row */}
        <div
          style={{
            display: "flex",
            gap: 10,
            alignItems: "center",
            flexWrap: "wrap",
            marginTop: "auto",
            paddingTop: 24,
            borderTop: `1px solid ${KS.border}`,
          }}
        >
          <a
            href={`mailto:${t.email}`}
            style={{
              background: accent,
              color: "#fff",
              padding: "14px 22px",
              fontSize: 14,
              fontWeight: 600,
              textDecoration: "none",
              transition: "filter 150ms ease",
            }}
            onMouseEnter={(e) =>
              (e.currentTarget.style.filter = "brightness(0.94)")
            }
            onMouseLeave={(e) =>
              (e.currentTarget.style.filter = "brightness(1)")
            }
          >
            {t.cta} →
          </a>
          <CopyEmailButton
            label={t.copyEmail}
            copiedLabel={t.emailCopied}
            email={t.email}
            style={{ padding: "14px 16px" }}
          />
          <div style={{ flex: 1 }} />
          <div
            style={{
              fontFamily: KSFonts.mono,
              fontSize: 10,
              color: KS.muted,
              letterSpacing: 0.5,
              textTransform: "uppercase",
            }}
          >
            <a
              href="events.html"
              style={{
                color: "inherit",
                textDecoration: "none",
                marginRight: 14,
              }}
            >
              {t.eventsNav}
            </a>
            <a
              href="stufen.html"
              style={{
                color: "inherit",
                textDecoration: "none",
                marginRight: 14,
              }}
            >
              5 Stufen
            </a>
            <a
              href="impressum.html"
              style={{
                color: "inherit",
                textDecoration: "none",
                marginRight: 14,
              }}
            >
              {t.imprint}
            </a>
            <a
              href="datenschutz.html"
              style={{ color: "inherit", textDecoration: "none" }}
            >
              {t.privacy}
            </a>
          </div>
        </div>
      </main>
    </div>
  );
}

Object.assign(window, { Site });
