// Shared atoms, tokens, and small bits that look exactly like Portal.

// Tailwind isn't loaded — use inline width/height (rem-based) instead of utility classes.
const remFromClass = (cls) => {
  const m = /(?:w|h)-(\d+(?:\.\d+)?)/.exec(cls || "");
  return m ? `${parseFloat(m[1]) * 0.25}rem` : "0.75rem";
};
const Check = ({ className = "w-3 h-3", stroke = 3 }) => {
  const s = remFromClass(className);
  return (
    <svg width={s} height={s} fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={stroke} style={{ display: "inline-block", flexShrink: 0 }}>
      <path strokeLinecap="round" strokeLinejoin="round" d="M5 13l4 4L19 7" />
    </svg>
  );
};
const Plus = ({ className = "w-2.5 h-2.5" }) => {
  const s = remFromClass(className);
  return (
    <svg width={s} height={s} fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2.5} style={{ display: "inline-block", flexShrink: 0 }}>
      <path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" />
    </svg>
  );
};
const Minus = ({ className = "w-2.5 h-2.5" }) => {
  const s = remFromClass(className);
  return (
    <svg width={s} height={s} fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2.5} style={{ display: "inline-block", flexShrink: 0 }}>
      <path strokeLinecap="round" strokeLinejoin="round" d="M5 12h14" />
    </svg>
  );
};
const ChevronR = ({ className = "w-3 h-3" }) => {
  const s = remFromClass(className);
  return (
    <svg width={s} height={s} fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} style={{ display: "inline-block", flexShrink: 0 }}>
      <path strokeLinecap="round" strokeLinejoin="round" d="M9 5l7 7-7 7" />
    </svg>
  );
};
const ChevronD = ({ className = "w-3 h-3" }) => {
  const s = remFromClass(className);
  return (
    <svg width={s} height={s} fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} style={{ display: "inline-block", flexShrink: 0 }}>
      <path strokeLinecap="round" strokeLinejoin="round" d="M6 9l6 6 6-6" />
    </svg>
  );
};
const SearchIcon = ({ className = "w-3.5 h-3.5" }) => {
  const s = remFromClass(className);
  return (
    <svg width={s} height={s} fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} style={{ display: "inline-block", flexShrink: 0 }}>
      <circle cx="11" cy="11" r="7" />
      <path strokeLinecap="round" d="M21 21l-4.3-4.3" />
    </svg>
  );
};

const formatPrice = (n) =>
  n == null ? "POA"
  : "$" + n.toLocaleString("en-AU", { maximumFractionDigits: 0 });

const Label = ({ children, dim = false, className = "" }) => (
  <span className={"label " + (dim ? "" : "label-dark ") + className}>{children}</span>
);

// Small "kicker" used at the top of each marketing section.
const Kicker = ({ n, children }) => (
  <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
    <span className="mono" style={{ fontSize: 11, color: "#111", letterSpacing: "0.04em", fontWeight: 600 }}>
      {String(n).padStart(2, "0")}
    </span>
    <span style={{ height: 0.5, width: 32, background: "var(--g300)" }} />
    <span className="label">{children}</span>
  </div>
);

// Pill / tag mirrors Portal's Tag component
const Tag = ({ children, dark = false }) => (
  <span
    style={{
      fontSize: 11,
      padding: "3px 10px",
      borderRadius: 999,
      background: dark ? "#111" : "rgba(0,0,0,0.04)",
      border: dark ? "none" : "0.5px solid rgba(0,0,0,0.08)",
      color: dark ? "#fff" : "var(--g500)",
      whiteSpace: "nowrap",
      fontWeight: dark ? 600 : 500,
      letterSpacing: dark ? "0.04em" : 0,
      textTransform: dark ? "uppercase" : "none",
    }}
  >
    {children}
  </span>
);

// Status pill for bookings
const STATUS_STYLES = {
  Pending:    { bg: "#fefce8", fg: "#854d0e", dot: "#ca8a04" },
  Confirmed:  { bg: "#eff6ff", fg: "#1e40af", dot: "#2563eb" },
  "In Production": { bg: "#f5f3ff", fg: "#5b21b6", dot: "#7c3aed" },
  Delivered:  { bg: "#ecfdf5", fg: "#065f46", dot: "#059669" },
  Cancelled:  { bg: "#f9fafb", fg: "#6b7280", dot: "#9ca3af" },
};
const StatusPill = ({ status }) => {
  const s = STATUS_STYLES[status] || STATUS_STYLES.Pending;
  return (
    <span
      style={{
        display: "inline-flex", alignItems: "center", gap: 6,
        padding: "3px 10px", borderRadius: 999,
        background: s.bg, color: s.fg,
        fontSize: 11, fontWeight: 600, letterSpacing: "0.02em",
      }}
    >
      <span className="pulse-dot" style={{ width: 5, height: 5, borderRadius: "50%", background: s.dot }} />
      {status}
    </span>
  );
};

// "Mac-style" chrome row for showing a portal screen
const BrowserChrome = ({ children, tall = false, noShadow = false }) => (
  <div
    style={{
      background: "#fff",
      border: "0.5px solid rgba(0,0,0,0.08)",
      borderRadius: 6,
      overflow: "hidden",
      boxShadow: noShadow ? "none" : "0 1px 2px rgba(0,0,0,0.03), 0 30px 60px -30px rgba(0,0,0,0.12)",
      display: "flex",
      flexDirection: "column",
    }}
  >
    <div
      style={{
        height: 28, padding: "0 12px",
        borderBottom: "0.5px solid rgba(0,0,0,0.06)",
        display: "flex", alignItems: "center", gap: 10,
        background: "#fbfbfa",
      }}
    >
      <div style={{ display: "flex", gap: 6 }}>
        <span style={{ width: 9, height: 9, borderRadius: "50%", background: "#e5e7eb" }} />
        <span style={{ width: 9, height: 9, borderRadius: "50%", background: "#e5e7eb" }} />
        <span style={{ width: 9, height: 9, borderRadius: "50%", background: "#e5e7eb" }} />
      </div>
    </div>
    <div style={{ minHeight: tall ? 560 : undefined, flex: 1 }}>{children}</div>
  </div>
);

// Portal sidebar (icon-only rail) — matches header/client-layout shape
const PortalSidebar = ({ active = "bookings" }) => {
  const Icon = ({ name, activeThis }) => {
    const paths = {
      book: <><path d="M4 7h16M4 12h16M4 17h10" strokeLinecap="round" /></>,
      list: <><rect x="4" y="4" width="16" height="16" rx="1.5" /><path d="M8 9h8M8 13h8M8 17h5" strokeLinecap="round" /></>,
      chat: <><path d="M20 12a8 8 0 11-3.5-6.6L20 4l-.9 3.5A7.9 7.9 0 0120 12z" strokeLinejoin="round" /></>,
      help: <><circle cx="12" cy="12" r="8" /><path d="M10 10a2 2 0 114 0c0 1.5-2 1.5-2 3" strokeLinecap="round" /><circle cx="12" cy="16" r=".5" fill="currentColor" /></>,
      user: <><circle cx="12" cy="8" r="3.5" /><path d="M5 20c1.2-3.5 4-5 7-5s5.8 1.5 7 5" strokeLinecap="round" /></>,
    };
    return (
      <div
        style={{
          width: 36, height: 36, borderRadius: 4,
          display: "flex", alignItems: "center", justifyContent: "center",
          color: activeThis ? "#111" : "var(--g400)",
          background: activeThis ? "rgba(0,0,0,0.04)" : "transparent",
        }}
      >
        <svg width="16" height="16" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24">
          {paths[name]}
        </svg>
      </div>
    );
  };
  return (
    <div
      style={{
        width: 56, borderRight: "0.5px solid rgba(0,0,0,0.06)",
        display: "flex", flexDirection: "column", alignItems: "center",
        padding: "12px 0", gap: 6, background: "#fff",
      }}
    >
      <div className="mark" style={{ marginBottom: 8 }}>P</div>
      <Icon name="book" activeThis={active === "book"} />
      <Icon name="list" activeThis={active === "bookings"} />
      <Icon name="chat" activeThis={active === "chat"} />
      <Icon name="help" activeThis={active === "help"} />
      <div style={{ flex: 1 }} />
      <Icon name="user" />
    </div>
  );
};

// Underline input (matches editorial input style)
const UnderlineInput = ({ label, value, placeholder, right = null, large = false }) => (
  <div>
    <div className="label" style={{ marginBottom: 8 }}>{label}</div>
    <div
      style={{
        display: "flex", alignItems: "center", gap: 8,
        padding: "6px 0 10px",
        borderBottom: "0.5px solid rgba(0,0,0,0.12)",
        fontSize: large ? 18 : 15,
        color: value ? "var(--ink)" : "var(--g300)",
      }}
    >
      <span style={{ flex: 1 }}>{value || placeholder}</span>
      {right}
    </div>
  </div>
);

// Progress stepper matching Portal's
const BookingProgress = ({ current = 0, steps = ["Service", "Details", "Review"] }) => (
  <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 32, paddingBottom: 20, borderBottom: "0.5px solid var(--hair)" }}>
    {steps.map((s, i) => (
      <div key={s} style={{ display: "flex", alignItems: "center", gap: 8, flex: 1 }}>
        <div style={{ display: "flex", alignItems: "center", gap: 10, flex: 1 }}>
          <div
            style={{
              width: 22, height: 22, borderRadius: "50%",
              display: "flex", alignItems: "center", justifyContent: "center",
              background: i <= current ? "#111" : "var(--g100)",
              color: i <= current ? "#fff" : "var(--g400)",
              fontSize: 10, fontWeight: 600,
              flexShrink: 0,
            }}
          >
            {i < current ? <Check className="w-2.5 h-2.5" /> : i + 1}
          </div>
          <span
            style={{
              fontSize: 11, fontWeight: 600, letterSpacing: "0.06em",
              textTransform: "uppercase",
              color: i <= current ? "#111" : "var(--g300)",
            }}
          >
            {s}
          </span>
        </div>
        {i < steps.length - 1 && (
          <div style={{ height: 1, flex: 1, background: i < current ? "#111" : "var(--g100)" }} />
        )}
      </div>
    ))}
  </div>
);

// Tight row used in tables
const TableRow = ({ name, sub, meta, price, selected = false, dimmed = false }) => (
  <div
    style={{
      display: "grid",
      gridTemplateColumns: "1fr 1fr auto",
      borderBottom: "0.5px solid rgba(0,0,0,0.06)",
      background: selected ? "#f5f5f5" : "transparent",
      borderLeft: selected ? "2px solid #1a1a1a" : "2px solid transparent",
      opacity: dimmed ? 0.25 : 1,
      transition: "opacity .3s, background .15s",
    }}
  >
    <div style={{ padding: "14px 20px", display: "flex", flexDirection: "column", gap: 2 }}>
      <span style={{ fontSize: 14, fontWeight: selected ? 600 : 500 }}>{name}</span>
      <span style={{ fontSize: 12, color: "var(--g400)", lineHeight: 1.35 }}>{sub}</span>
    </div>
    <div style={{ padding: "14px 20px", display: "flex", alignItems: "center", borderLeft: "0.5px solid rgba(0,0,0,0.06)" }}>
      <span style={{ fontSize: 12, color: "var(--g400)" }}>{meta}</span>
    </div>
    <div style={{ padding: "14px 20px", display: "flex", alignItems: "center", justifyContent: "flex-end", borderLeft: "0.5px solid rgba(0,0,0,0.06)", minWidth: 100 }}>
      <span style={{ fontSize: 15, fontWeight: 500 }}>{price}</span>
    </div>
  </div>
);

Object.assign(window, {
  Check, Plus, Minus, ChevronR, ChevronD, SearchIcon,
  formatPrice, Label, Kicker, Tag, StatusPill,
  BrowserChrome, PortalSidebar, UnderlineInput, BookingProgress, TableRow,
});
