// markflord — ML maker's-mark monogram directions.
// Four constructions of one struck "ML" glyph. Each renders flat (favicon)
// or forged (hero), on light (ink mark) or dark (steel mark). Ember spark constant.
//
// Construction grammar (shared by Spine / Keystone / Anvil):
//   chevron-M drawn as a single mitered polyline; its right leg is the SPINE
//   that also serves as the L stem; an L foot extends right at the baseline.
//   Centerlines: legs x=37 / x=163, apex (100,150), cap top y=47, baseline y=161.
//   Bar weight t=34.  viewBox 0 0 260 212.

const { useId } = React;

const STROKE = 34;

// ---- finish helpers ---------------------------------------------------------
function Defs({ uid, light }) {
  // steel/graphite body gradient + ember spark gradient + emboss filter
  const bodyTop = light ? "#4A4334" : "#5A5142";
  const bodyMid = light ? "#1A1610" : "#2C261C";
  const bodyBot = light ? "#0B0907" : "#16110B";
  return (
    <defs>
      <linearGradient id={`${uid}-body`} x1="0" y1="0" x2="0" y2="1">
        <stop offset="0" stopColor={bodyTop} />
        <stop offset="0.5" stopColor={bodyMid} />
        <stop offset="1" stopColor={bodyBot} />
      </linearGradient>
      <linearGradient id={`${uid}-ember`} x1="0" y1="0" x2="0" y2="1">
        <stop offset="0" stopColor="#FFE6C2" />
        <stop offset="0.4" stopColor="#FF5B1F" />
        <stop offset="1" stopColor="#9A3408" />
      </linearGradient>
      <radialGradient id={`${uid}-glow`} cx="0.5" cy="0.5" r="0.5">
        <stop offset="0" stopColor="#FF7A3D" stopOpacity="0.9" />
        <stop offset="1" stopColor="#FF5B1F" stopOpacity="0" />
      </radialGradient>
      <filter id={`${uid}-emboss`} x="-20%" y="-20%" width="140%" height="150%">
        <feDropShadow dx="0" dy="3" stdDeviation="2.5" floodColor="#000" floodOpacity={light ? "0.28" : "0.55"} />
      </filter>
    </defs>
  );
}

// top bevel highlight: a faint lighter copy offset up
function bevelStroke(light) { return light ? "rgba(255,246,232,0.32)" : "rgba(255,246,232,0.22)"; }

// ---- DIRECTION 1 · SPINE ----------------------------------------------------
function MonoSpine({ size = 120, forged = true, light = false }) {
  const uid = useId().replace(/:/g, "");
  const body = forged ? `url(#${uid}-body)` : (light ? "#0E0C08" : "#F3EEE4");
  const ember = forged ? `url(#${uid}-ember)` : "#FF5B1F";
  const mPath = "M37,161 L37,47 L100,150 L163,47 L163,161";
  return (
    <svg viewBox="0 0 260 212" width={size} height={size * 212 / 260} style={{ display: "block", overflow: "visible" }}>
      {forged && <Defs uid={uid} light={light} />}
      <g filter={forged ? `url(#${uid}-emboss)` : undefined}>
        {/* foot (L) */}
        <rect x="146" y="144" width="92" height="34" fill={body} />
        {/* chevron-M + spine */}
        <path d={mPath} fill="none" stroke={body} strokeWidth={STROKE} strokeLinejoin="miter" strokeLinecap="butt" />
        {forged && (
          <>
            <rect x="146" y="144" width="92" height="3.5" fill={bevelStroke(light)} />
            <path d={mPath} fill="none" stroke={bevelStroke(light)} strokeWidth="2.5" strokeLinejoin="miter"
              strokeLinecap="butt" transform="translate(0,-2)" opacity="0.7" />
          </>
        )}
      </g>
      {/* molten spark at struck apex */}
      {forged && <circle cx="100" cy="150" r="26" fill={`url(#${uid}-glow)`} />}
      <path d="M100,124 L108,150 L100,176 L92,150 Z" fill={ember} />
      <path d="M100,150 m-3,0 a3,3 0 1,0 6,0 a3,3 0 1,0 -6,0" fill="#FFE6C2" opacity={forged ? 0.95 : 0} />
    </svg>
  );
}

// ---- DIRECTION 2 · KEYSTONE -------------------------------------------------
// L nested in M as a structural joint: a keystone wedge locks the V's apex.
function MonoKeystone({ size = 120, forged = true, light = false }) {
  const uid = useId().replace(/:/g, "");
  const body = forged ? `url(#${uid}-body)` : (light ? "#0E0C08" : "#F3EEE4");
  const ember = forged ? `url(#${uid}-ember)` : "#FF5B1F";
  const mPath = "M37,161 L37,47 L100,128 L163,47 L163,161";
  return (
    <svg viewBox="0 0 260 212" width={size} height={size * 212 / 260} style={{ display: "block", overflow: "visible" }}>
      {forged && <Defs uid={uid} light={light} />}
      <g filter={forged ? `url(#${uid}-emboss)` : undefined}>
        <rect x="146" y="144" width="92" height="34" fill={body} />
        <path d={mPath} fill="none" stroke={body} strokeWidth={STROKE} strokeLinejoin="miter" strokeLinecap="butt" />
        {/* keystone — a wide-topped wedge dropped into the V's crown as the locking joint */}
        <path d="M80,96 L120,96 L110,138 L90,138 Z" fill={body} />
        {forged && (
          <>
            <path d={mPath} fill="none" stroke={bevelStroke(light)} strokeWidth="2.5" strokeLinejoin="miter"
              strokeLinecap="butt" transform="translate(0,-2)" opacity="0.7" />
            <path d="M80,96 L120,96 L118.5,104 L81.5,104 Z" fill={bevelStroke(light)} />
          </>
        )}
      </g>
      {/* hot seam where the keystone seats */}
      <rect x="98" y="104" width="4" height="40" fill={ember} />
      {forged && <circle cx="100" cy="138" r="20" fill={`url(#${uid}-glow)`} />}
    </svg>
  );
}

// ---- DIRECTION 3 · ANVIL ----------------------------------------------------
// Baseline reads as an anvil face: foot extends both ways, one end tapers (horn).
function MonoAnvil({ size = 120, forged = true, light = false }) {
  const uid = useId().replace(/:/g, "");
  const body = forged ? `url(#${uid}-body)` : (light ? "#0E0C08" : "#F3EEE4");
  const ember = forged ? `url(#${uid}-ember)` : "#FF5B1F";
  const mPath = "M55,158 L55,47 L100,150 L145,47 L145,158";
  // anvil base: top face wide, waist, foot — sits under the whole mark
  const anvil = "M26,158 L210,158 L210,170 L150,170 L150,184 L196,184 L196,198 L64,198 L64,184 L110,184 L110,170 L26,170 Z";
  return (
    <svg viewBox="0 0 236 214" width={size} height={size * 214 / 236} style={{ display: "block", overflow: "visible" }}>
      {forged && <Defs uid={uid} light={light} />}
      <g filter={forged ? `url(#${uid}-emboss)` : undefined}>
        <path d={mPath} fill="none" strokeWidth="30" stroke={body} strokeLinejoin="miter" strokeLinecap="butt" />
        <path d={anvil} fill={body} />
        {forged && (
          <>
            <path d={mPath} fill="none" stroke={bevelStroke(light)} strokeWidth="2.5" strokeLinejoin="miter"
              strokeLinecap="butt" transform="translate(0,-2)" opacity="0.7" />
            <rect x="26" y="158" width="184" height="3.5" fill={bevelStroke(light)} />
          </>
        )}
      </g>
      {/* spark struck on the anvil face */}
      {forged && <circle cx="118" cy="158" r="22" fill={`url(#${uid}-glow)`} />}
      <path d="M118,140 L124,158 L118,176 L112,158 Z" fill={ember} />
    </svg>
  );
}

// ---- DIRECTION 4 · STRUCK SEAL / INGOT --------------------------------------
function MonoSeal({ size = 120, forged = true, light = false }) {
  const uid = useId().replace(/:/g, "");
  const ringBody = forged ? `url(#${uid}-body)` : (light ? "#0E0C08" : "#F3EEE4");
  const ember = forged ? `url(#${uid}-ember)` : "#FF5B1F";
  const markColor = forged ? "#F3EEE4" : (light ? "#F0E9D7" : "#0E0C08"); // struck mark contrasts the disc
  const fieldFill = forged ? "#0B0907" : (light ? "#1B150D" : "#0E0C08");
  const mPath = "M84,150 L84,82 L116,138 L148,82 L148,150";
  return (
    <svg viewBox="0 0 232 232" width={size} height={size} style={{ display: "block", overflow: "visible" }}>
      {forged && <Defs uid={uid} light={light} />}
      <g filter={forged ? `url(#${uid}-emboss)` : undefined}>
        {/* ingot disc */}
        <circle cx="116" cy="116" r="100" fill={ringBody} />
        {forged && <circle cx="116" cy="116" r="100" fill="none" stroke="rgba(255,246,232,0.16)" strokeWidth="2" transform="translate(0,-2)" />}
        {/* recessed field */}
        <circle cx="116" cy="116" r="80" fill={fieldFill} opacity={forged ? 0.5 : 0.14} />
        <circle cx="116" cy="116" r="80" fill="none" stroke={ember} strokeWidth="1.5" opacity="0.45" />
      </g>
      {/* struck ML inside the field */}
      <g>
        <path d={mPath} fill="none" stroke={markColor} strokeWidth="22" strokeLinejoin="miter" strokeLinecap="butt" opacity={forged ? 0.94 : 1} />
        <rect x="138" y="139" width="42" height="22" fill={markColor} opacity={forged ? 0.94 : 1} />
      </g>
      <path d="M116,116 L121,138 L116,160 L111,138 Z" fill={ember} />
      {forged && <circle cx="116" cy="138" r="16" fill={`url(#${uid}-glow)`} />}
    </svg>
  );
}

// ---- WORDMARK ---------------------------------------------------------------
function MLWordmark({ color = "#0E0C08", scale = 1, oneword = false }) {
  return (
    <span style={{ display: "inline-flex", flexDirection: "column", lineHeight: 1, alignItems: "flex-start" }} aria-label="Mark Lord">
      <span style={{ fontFamily: "'Cinzel', serif", fontWeight: 600, fontSize: 30 * scale, letterSpacing: "0.16em",
        color, textTransform: "uppercase" }}>
        {oneword ? "MARKLORD" : "MARK LORD"}
      </span>
    </span>
  );
}

// ═══════════════════════════════════════════════════════════════════════════
//  BATCH II — STAGGERED & OVERLAID (two distinct letters that layer, not connect)
//  Serif-forward per direction. Each: M is the back plate, L is struck over it,
//  offset down-right, with a ground-matched knockout halo so it reads as a
//  separate layer in front (occlusion + forged shadow, never a fused ligature).
// ═══════════════════════════════════════════════════════════════════════════

// Serif overlay engine — font/weight swappable; `molten` renders the front L hot.
function SerifOverlayMark({ size = 120, forged = true, light = false, font = "'Cinzel', serif", weight = 600, molten = false }) {
  const uid = useId().replace(/:/g, "");
  const body = forged ? `url(#${uid}-body)` : (light ? "#0E0C08" : "#F3EEE4");
  const ember = forged ? `url(#${uid}-ember)` : "#FF5B1F";
  const halo = light ? "#F0E9D7" : "#0E0C08";
  const lFill = molten ? ember : body;
  const bev = bevelStroke(light);
  return (
    <svg viewBox="0 0 284 224" width={size} height={size * 224 / 284} style={{ display: "block", overflow: "visible", fontFamily: font, fontWeight: weight }}>
      {forged && <Defs uid={uid} light={light} />}
      {/* M — back plate */}
      <g filter={forged ? `url(#${uid}-emboss)` : undefined}>
        {forged && <text x="88" y="162" fontSize="174" textAnchor="middle" fill={bev} transform="translate(0,-2.5)" opacity="0.5">M</text>}
        <text x="88" y="162" fontSize="174" textAnchor="middle" fill={body}>M</text>
      </g>
      {/* L — front plate, staggered down-right, knocked out of the M behind it */}
      {!molten && <text x="202" y="200" fontSize="152" textAnchor="middle" fill={halo} stroke={halo} strokeWidth="13" strokeLinejoin="round" style={{ paintOrder: "stroke" }}>L</text>}
      <g filter={forged ? `url(#${uid}-emboss)` : undefined}>
        {forged && !molten && <text x="202" y="200" fontSize="152" textAnchor="middle" fill={bev} transform="translate(0,-2.5)" opacity="0.45">L</text>}
        <text x="202" y="200" fontSize="152" textAnchor="middle" fill={lFill}>L</text>
      </g>
      {/* ember accent — a struck spark at the overlap (or the whole L if molten) */}
      {!molten && <path d="M171,150 L176,167 L171,184 L166,167 Z" fill={ember} />}
      {forged && <circle cx={molten ? 200 : 171} cy={molten ? 172 : 167} r="17" fill={`url(#${uid}-glow)`} />}
    </svg>
  );
}

const MonoEngraved = (p) => <SerifOverlayMark {...p} font="'Cinzel', serif" weight={600} />;
const MonoModernSerif = (p) => <SerifOverlayMark {...p} font="'Fraunces', Georgia, serif" weight={500} />;
const MonoSerifMolten = (p) => <SerifOverlayMark {...p} font="'Cinzel', serif" weight={600} molten />;

// Geometric overlay (modern, non-serif) — two slab letters, L struck over M.
function MonoOverlay({ size = 120, forged = true, light = false }) {
  const uid = useId().replace(/:/g, "");
  const body = forged ? `url(#${uid}-body)` : (light ? "#0E0C08" : "#F3EEE4");
  const ember = forged ? `url(#${uid}-ember)` : "#FF5B1F";
  const halo = light ? "#F0E9D7" : "#0E0C08";
  const mPath = "M40,170 L40,52 L95,150 L150,52 L150,170";
  const lPath = "M151,80 H185 V171 H236 V205 H151 Z";
  return (
    <svg viewBox="0 0 262 234" width={size} height={size * 234 / 262} style={{ display: "block", overflow: "visible" }}>
      {forged && <Defs uid={uid} light={light} />}
      {/* M — back plate */}
      <g filter={forged ? `url(#${uid}-emboss)` : undefined}>
        <path d={mPath} fill="none" stroke={body} strokeWidth="34" strokeLinejoin="miter" strokeLinecap="butt" />
        {forged && <path d={mPath} fill="none" stroke={bevelStroke(light)} strokeWidth="2.5" transform="translate(0,-2)" strokeLinejoin="miter" strokeLinecap="butt" opacity="0.7" />}
      </g>
      {/* knockout halo + L front plate */}
      <path d={lPath} fill={halo} stroke={halo} strokeWidth="13" strokeLinejoin="miter" />
      <g filter={forged ? `url(#${uid}-emboss)` : undefined}>
        <path d={lPath} fill={body} />
        {forged && <path d="M151,80 H161 V205 H151 Z M151,80 H185 V90 H151 Z" fill={bevelStroke(light)} opacity="0.5" />}
      </g>
      {/* ember edge-light on the front plate's leading edges + spark */}
      <rect x="149" y="82" width="3" height="123" fill={ember} />
      <rect x="151" y="169" width="85" height="3" fill={ember} opacity="0.85" />
      <path d="M168,150 L174,168 L168,186 L162,168 Z" fill={ember} />
      {forged && <circle cx="168" cy="168" r="16" fill={`url(#${uid}-glow)`} />}
    </svg>
  );
}

Object.assign(window, { MonoSpine, MonoKeystone, MonoAnvil, MonoSeal, MonoEngraved, MonoModernSerif, MonoSerifMolten, MonoOverlay, MLWordmark });
