/* eslint-disable */
/* ===============================================================
   Shared SVG assets — train silhouette, train ticket, swedish flag,
   metal sigils, watercolor placeholder, Jan portrait placeholder,
   Swish QR placeholder.
   All hand-drawn here are simple/iconographic by design system rules.
   =============================================================== */

const TrainIcon = ({ size = 56, color = "currentColor" }) => (
  <svg width={size} height={size * 0.5} viewBox="0 0 220 110" fill="none" stroke={color} strokeWidth="1.4" strokeLinecap="round">
    {/* engraved steam locomotive — minimal silhouette */}
    {/* main boiler */}
    <rect x="55" y="35" width="100" height="40" fill="none"/>
    {/* cab */}
    <path d="M 155 25 L 195 25 L 195 75 L 155 75" fill="none"/>
    <rect x="162" y="34" width="14" height="14" fill="none"/>
    {/* smoke stack */}
    <path d="M 75 35 L 75 18 L 92 18 L 92 35" fill="none"/>
    {/* dome */}
    <path d="M 110 35 Q 110 24 122 24 Q 134 24 134 35" fill="none"/>
    {/* headlamp */}
    <circle cx="48" cy="55" r="6" fill="none"/>
    {/* cowcatcher */}
    <path d="M 42 75 L 25 88 L 60 88 Z" fill="none"/>
    {/* big driving wheel */}
    <circle cx="95" cy="85" r="14" fill="none"/>
    <circle cx="95" cy="85" r="3" fill={color}/>
    {/* small wheel */}
    <circle cx="135" cy="88" r="10" fill="none"/>
    <circle cx="135" cy="88" r="2" fill={color}/>
    <circle cx="175" cy="88" r="10" fill="none"/>
    <circle cx="175" cy="88" r="2" fill={color}/>
    {/* coupling rod */}
    <line x1="80" y1="85" x2="190" y2="88"/>
    {/* rail */}
    <line x1="0" y1="100" x2="220" y2="100"/>
    <line x1="0" y1="105" x2="220" y2="105" strokeDasharray="2 4"/>
    {/* steam puffs */}
    <path d="M 84 12 Q 80 4 88 2 Q 96 4 92 12" fill="none"/>
    <path d="M 100 8 Q 98 2 105 1 Q 110 4 107 10" fill="none"/>
  </svg>
);

const TrainLong = ({ width = 600, color = "currentColor" }) => (
  <svg width={width} height={width * 0.18} viewBox="0 0 600 110" fill="none" stroke={color} strokeWidth="1.2" strokeLinecap="round">
    {/* loco */}
    <g transform="translate(0, 0)">
      <rect x="55" y="35" width="100" height="40" fill="none"/>
      <path d="M 155 25 L 195 25 L 195 75 L 155 75" fill="none"/>
      <rect x="162" y="34" width="14" height="14" fill="none"/>
      <path d="M 75 35 L 75 18 L 92 18 L 92 35" fill="none"/>
      <path d="M 110 35 Q 110 24 122 24 Q 134 24 134 35" fill="none"/>
      <circle cx="48" cy="55" r="6" fill="none"/>
      <path d="M 42 75 L 25 88 L 60 88 Z" fill="none"/>
      <circle cx="95" cy="85" r="14" fill="none"/>
      <circle cx="95" cy="85" r="3" fill={color}/>
      <circle cx="135" cy="88" r="10" fill="none"/>
      <circle cx="175" cy="88" r="10" fill="none"/>
      <line x1="80" y1="85" x2="190" y2="88"/>
    </g>
    {/* tender */}
    <rect x="200" y="40" width="70" height="35" fill="none"/>
    <circle cx="215" cy="88" r="9" fill="none"/>
    <circle cx="255" cy="88" r="9" fill="none"/>
    {/* car 1 */}
    <rect x="278" y="28" width="135" height="50" fill="none"/>
    <line x1="290" y1="40" x2="408" y2="40"/>
    <rect x="295" y="48" width="14" height="20" fill="none"/>
    <rect x="318" y="48" width="14" height="20" fill="none"/>
    <rect x="341" y="48" width="14" height="20" fill="none"/>
    <rect x="364" y="48" width="14" height="20" fill="none"/>
    <rect x="387" y="48" width="14" height="20" fill="none"/>
    <circle cx="298" cy="88" r="9" fill="none"/>
    <circle cx="395" cy="88" r="9" fill="none"/>
    {/* car 2 */}
    <rect x="420" y="28" width="135" height="50" fill="none"/>
    <line x1="432" y1="40" x2="550" y2="40"/>
    <rect x="437" y="48" width="14" height="20" fill="none"/>
    <rect x="460" y="48" width="14" height="20" fill="none"/>
    <rect x="483" y="48" width="14" height="20" fill="none"/>
    <rect x="506" y="48" width="14" height="20" fill="none"/>
    <rect x="529" y="48" width="14" height="20" fill="none"/>
    <circle cx="440" cy="88" r="9" fill="none"/>
    <circle cx="537" cy="88" r="9" fill="none"/>

    {/* steam */}
    <path d="M 84 12 Q 80 4 88 2 Q 96 4 92 12" fill="none"/>
    <path d="M 100 8 Q 98 2 105 1 Q 110 4 107 10" fill="none"/>
    {/* rail */}
    <line x1="0" y1="100" x2="600" y2="100"/>
    <line x1="0" y1="106" x2="600" y2="106" strokeDasharray="2 4"/>
  </svg>
);

const Ornament = ({ color = "currentColor", size = 18 }) => (
  <svg width={size * 3} height={size} viewBox="0 0 90 30" fill="none" stroke={color} strokeWidth="1">
    <circle cx="45" cy="15" r="3" fill={color}/>
    <path d="M 12 15 Q 28 4 42 15 Q 28 26 12 15 Z" fill="none"/>
    <path d="M 78 15 Q 62 4 48 15 Q 62 26 78 15 Z" fill="none"/>
    <line x1="0" y1="15" x2="10" y2="15"/>
    <line x1="80" y1="15" x2="90" y2="15"/>
  </svg>
);

const SwedishFlag = ({ size = 14 }) => (
  <svg width={size * 1.6} height={size} viewBox="0 0 16 10">
    <rect width="16" height="10" fill="#006aa7"/>
    <rect x="5" width="2" height="10" fill="#fecc00"/>
    <rect y="4" width="16" height="2" fill="#fecc00"/>
  </svg>
);

const CanadaFlag = ({ size = 14 }) => (
  <svg width={size * 1.6} height={size} viewBox="0 0 16 10">
    <rect width="16" height="10" fill="#fff"/>
    <rect width="4" height="10" fill="#d52b1e"/>
    <rect x="12" width="4" height="10" fill="#d52b1e"/>
    <path d="M 8 3 L 8.5 4 L 9.5 3.8 L 9.2 4.7 L 10 5.2 L 9 5.5 L 9.2 6.3 L 8.4 6 L 8 6.8 L 7.6 6 L 6.8 6.3 L 7 5.5 L 6 5.2 L 6.8 4.7 L 6.5 3.8 L 7.5 4 Z" fill="#d52b1e"/>
  </svg>
);

const MexicoFlag = ({ size = 14 }) => (
  <svg width={size * 1.6} height={size} viewBox="0 0 16 10">
    <rect width="16" height="10" fill="#fff"/>
    <rect width="5.33" height="10" fill="#006847"/>
    <rect x="10.67" width="5.33" height="10" fill="#ce1126"/>
  </svg>
);

const EthiopiaFlag = ({ size = 14 }) => (
  <svg width={size * 1.6} height={size} viewBox="0 0 16 10">
    <rect width="16" height="3.33" fill="#078930"/>
    <rect y="3.33" width="16" height="3.33" fill="#fcdd09"/>
    <rect y="6.66" width="16" height="3.33" fill="#da121a"/>
  </svg>
);

const USFlag = ({ size = 14 }) => (
  <svg width={size * 1.6} height={size} viewBox="0 0 16 10">
    <rect width="16" height="10" fill="#fff"/>
    {[0,2,4,6,8].map(y => <rect key={y} y={y} width="16" height="1" fill="#b22234"/>)}
    <rect width="6" height="5" fill="#3c3b6e"/>
  </svg>
);

const FlagFor = ({ country, size = 14 }) => {
  const map = { Sverige: SwedishFlag, Kanada: CanadaFlag, Mexiko: MexicoFlag, Etiopien: EthiopiaFlag, USA: USFlag };
  const C = map[country] || SwedishFlag;
  return <C size={size}/>;
};

/* Metal sigil — Ag / Au — minimal alchemical-style mark */
const MetalSigil = ({ metal, size = 26, color = "currentColor" }) => {
  if (metal === "Au" || metal === "guld") {
    return (
      <svg width={size} height={size} viewBox="0 0 30 30" fill="none" stroke={color} strokeWidth="1.3">
        <circle cx="15" cy="15" r="9"/>
        <circle cx="15" cy="15" r="2" fill={color}/>
      </svg>
    );
  }
  // Silver — crescent
  return (
    <svg width={size} height={size} viewBox="0 0 30 30" fill="none" stroke={color} strokeWidth="1.3">
      <path d="M 22 7 Q 12 9 12 15 Q 12 21 22 23 Q 14 23 10 17 Q 10 11 22 7 Z" fill="none"/>
    </svg>
  );
};

/* Decorative corner flourish — for ticket-stub */
const CornerFlourish = ({ color = "currentColor" }) => (
  <svg width="40" height="40" viewBox="0 0 40 40" fill="none" stroke={color} strokeWidth="0.8">
    <path d="M 0 8 Q 8 8 8 0"/>
    <path d="M 0 14 Q 14 14 14 0"/>
    <circle cx="6" cy="6" r="1" fill={color}/>
  </svg>
);

/* Watercolor placeholder — engraved woodcut of a Falun-red cottage in snow.
   Hand-built SVG with cross-hatching, no emoji. */
const WatercolorPlaceholder = ({ caption = "Akvarell — röd stuga, fotspår i snö", height = 280 }) => (
  <div style={{
    background: "#ede6d6",
    border: "1px solid var(--parchment-3)",
    height,
    position: "relative",
    overflow: "hidden",
  }}>
    <svg viewBox="0 0 400 280" preserveAspectRatio="xMidYMid slice" style={{ width: "100%", height: "100%", display: "block" }}>
      <defs>
        {/* Sky hatching — diagonal lines, sparse */}
        <pattern id="sky-hatch" patternUnits="userSpaceOnUse" width="6" height="6" patternTransform="rotate(35)">
          <line x1="0" y1="0" x2="0" y2="6" stroke="#9a958a" strokeWidth="0.3" opacity="0.5"/>
        </pattern>
        {/* Ground hatching — horizontal lines for snow shadows */}
        <pattern id="snow-hatch" patternUnits="userSpaceOnUse" width="3" height="6">
          <line x1="0" y1="0" x2="3" y2="0" stroke="#bfb39a" strokeWidth="0.3" opacity="0.4"/>
        </pattern>
        {/* Falun-red cross-hatch — for the cottage walls */}
        <pattern id="wall-hatch" patternUnits="userSpaceOnUse" width="4" height="4">
          <path d="M 0 4 L 4 0" stroke="#5a1f18" strokeWidth="0.4"/>
        </pattern>
        {/* Roof darker hatch */}
        <pattern id="roof-hatch" patternUnits="userSpaceOnUse" width="3" height="3">
          <path d="M 0 3 L 3 0" stroke="#1a0f0a" strokeWidth="0.5"/>
        </pattern>
        {/* Tree foliage hatch */}
        <pattern id="tree-hatch" patternUnits="userSpaceOnUse" width="3" height="3" patternTransform="rotate(45)">
          <line x1="0" y1="0" x2="0" y2="3" stroke="#2a3d24" strokeWidth="0.5"/>
        </pattern>
      </defs>

      {/* Sky — warm parchment with sparse hatching */}
      <rect width="400" height="180" fill="#ede6d6"/>
      <rect width="400" height="180" fill="url(#sky-hatch)"/>

      {/* Ground / snow line */}
      <rect y="180" width="400" height="100" fill="#f5f0e6"/>
      <rect y="180" width="400" height="100" fill="url(#snow-hatch)"/>

      {/* Distant tree line — woodcut style fir trees */}
      <g stroke="#3d4d35" strokeWidth="0.8" fill="#3d4d35">
        <polygon points="20,180 12,165 28,165"/>
        <polygon points="38,180 30,160 46,160"/>
        <polygon points="58,180 50,168 66,168"/>
        <polygon points="340,180 332,158 348,158"/>
        <polygon points="360,180 352,165 368,165"/>
        <polygon points="380,180 372,162 388,162"/>
      </g>

      {/* Larger fir tree — left foreground */}
      <g>
        <polygon points="60,200 40,170 80,170" fill="#2a3d24"/>
        <polygon points="60,180 44,150 76,150" fill="#2a3d24"/>
        <polygon points="60,160 48,134 72,134" fill="#2a3d24"/>
        <polygon points="60,200 40,170 80,170" fill="url(#tree-hatch)"/>
        <rect x="57" y="200" width="6" height="10" fill="#2a1f14"/>
      </g>

      {/* The Falun-red stuga — centered */}
      <g>
        {/* Main wall */}
        <rect x="140" y="135" width="120" height="60" fill="#a8443a" stroke="#3a1410" strokeWidth="1"/>
        <rect x="140" y="135" width="120" height="60" fill="url(#wall-hatch)"/>
        {/* Roof — pitched */}
        <polygon points="135,135 200,95 265,135" fill="#4a2a1a" stroke="#1a0f0a" strokeWidth="1"/>
        <polygon points="135,135 200,95 265,135" fill="url(#roof-hatch)"/>
        {/* Roof line */}
        <line x1="135" y1="135" x2="265" y2="135" stroke="#1a0f0a" strokeWidth="0.8"/>
        {/* Chimney */}
        <rect x="220" y="100" width="10" height="18" fill="#3a2a20" stroke="#1a0f0a" strokeWidth="0.6"/>
        {/* Door */}
        <rect x="190" y="160" width="20" height="35" fill="#2a1810" stroke="#1a0f0a" strokeWidth="0.6"/>
        <circle cx="205" cy="178" r="0.8" fill="#c9a04a"/>
        {/* Step */}
        <rect x="186" y="195" width="28" height="3" fill="#d4c8a8" stroke="#9a958a" strokeWidth="0.4"/>
        {/* Windows — six-pane, white frames glowing warm */}
        {[155, 230].map(x => (
          <g key={x}>
            <rect x={x} y="148" width="20" height="18" fill="#f0e6c8" stroke="#1a0f0a" strokeWidth="0.6"/>
            <line x1={x + 10} y1="148" x2={x + 10} y2="166" stroke="#1a0f0a" strokeWidth="0.4"/>
            <line x1={x} y1="157" x2={x + 20} y2="157" stroke="#1a0f0a" strokeWidth="0.4"/>
            {/* Warm glow inside */}
            <rect x={x + 1} y="149" width="8" height="7" fill="#e2c889" opacity="0.7"/>
            <rect x={x + 11} y="149" width="8" height="7" fill="#e2c889" opacity="0.4"/>
          </g>
        ))}
        {/* Snow on roof — soft cap */}
        <path d="M 135 135 L 200 96 L 265 135 L 260 132 L 200 99 L 140 132 Z" fill="#f5f0e6" stroke="#bfb39a" strokeWidth="0.4"/>
        {/* Snow ledges on windows */}
        <rect x="153" y="146" width="24" height="2.5" fill="#f5f0e6" stroke="#bfb39a" strokeWidth="0.3"/>
        <rect x="228" y="146" width="24" height="2.5" fill="#f5f0e6" stroke="#bfb39a" strokeWidth="0.3"/>
      </g>

      {/* Smoke from chimney — woodcut curl */}
      <g stroke="#7a7164" strokeWidth="0.7" fill="none" opacity="0.7">
        <path d="M 225 100 Q 220 90 228 84 Q 236 78 230 70 Q 224 62 232 56"/>
        <path d="M 226 95 Q 232 88 226 80"/>
      </g>

      {/* Footprints in snow — leading toward the door */}
      <g fill="#9a958a" opacity="0.65">
        {[[80, 245],[100, 235],[120, 240],[140, 230],[160, 235],[178, 228]].map(([x, y], i) => (
          <g key={i}>
            <ellipse cx={x} cy={y} rx="3" ry="1.6" transform={`rotate(${i * 4 - 8} ${x} ${y})`}/>
            <ellipse cx={x + 8} cy={y - 3} rx="3" ry="1.6" transform={`rotate(${i * 4 - 8} ${x + 8} ${y - 3})`}/>
          </g>
        ))}
      </g>

      {/* Fence posts — far right */}
      <g stroke="#4a3a2a" strokeWidth="1" fill="#5a4530">
        {[290, 305, 320, 335, 350].map(x => (
          <g key={x}>
            <rect x={x} y="195" width="2" height="22"/>
            <line x1={x - 4} y1="200" x2={x + 6} y2="200"/>
            <line x1={x - 4} y1="210" x2={x + 6} y2="210"/>
          </g>
        ))}
      </g>

      {/* Falling snow — sparse dots */}
      <g fill="#ffffff" opacity="0.9">
        {[[40,40],[80,25],[130,55],[200,30],[280,50],[330,20],[370,60],[60,85],[170,80],[250,95],[310,90],[380,110]].map(([x,y],i)=>(
          <circle key={i} cx={x} cy={y} r="1.2"/>
        ))}
      </g>

      {/* Vignette frame */}
      <rect x="0.5" y="0.5" width="399" height="279" fill="none" stroke="#a8842f" strokeWidth="1"/>
    </svg>

    {/* Caption strip */}
    <div style={{
      position: "absolute",
      bottom: 0, left: 0, right: 0,
      background: "rgba(245,240,230,0.94)",
      borderTop: "1px solid var(--gold-deep)",
      padding: "6px 14px",
      fontFamily: "var(--mono)",
      fontSize: 10,
      letterSpacing: "0.18em",
      textTransform: "uppercase",
      color: "var(--ink-soft)",
      textAlign: "center",
    }}>{caption}</div>
  </div>
);

/* Jan portrait placeholder — close-crop, glasses, on teal */
const JanPortrait = ({ size = 240 }) => (
  <div style={{
    width: size, height: size,
    background: "var(--teal)",
    border: "1px solid var(--gold-deep)",
    position: "relative",
    overflow: "hidden",
    flex: "0 0 auto",
  }}>
    <svg viewBox="0 0 200 200" style={{ width: "100%", height: "100%" }}>
      {/* engraved hatching background */}
      <defs>
        <pattern id="hatch" patternUnits="userSpaceOnUse" width="3" height="3" patternTransform="rotate(45)">
          <line x1="0" y1="0" x2="0" y2="3" stroke="#15292b" strokeWidth="1"/>
        </pattern>
      </defs>
      <rect width="200" height="200" fill="#1d3a3d"/>
      <rect width="200" height="200" fill="url(#hatch)" opacity="0.4"/>
      {/* shoulders */}
      <path d="M 20 200 Q 30 150 70 140 L 130 140 Q 170 150 180 200 Z" fill="#2a4a4d" stroke="#c9a04a" strokeWidth="0.6"/>
      {/* neck */}
      <rect x="85" y="120" width="30" height="25" fill="#d4b896"/>
      {/* head */}
      <ellipse cx="100" cy="90" rx="40" ry="48" fill="#e0c4a3" stroke="#a8842f" strokeWidth="0.5"/>
      {/* hair — silver, side-parted */}
      <path d="M 60 75 Q 60 50 100 45 Q 140 50 142 78 Q 138 60 115 56 L 95 60 Q 75 65 62 80 Z" fill="#d4d0c8" stroke="#9a958a" strokeWidth="0.5"/>
      {/* glasses */}
      <circle cx="84" cy="92" r="11" fill="none" stroke="#1a1714" strokeWidth="1.3"/>
      <circle cx="116" cy="92" r="11" fill="none" stroke="#1a1714" strokeWidth="1.3"/>
      <line x1="95" y1="92" x2="105" y2="92" stroke="#1a1714" strokeWidth="1.3"/>
      {/* eyes behind glasses */}
      <circle cx="84" cy="92" r="2" fill="#1a1714"/>
      <circle cx="116" cy="92" r="2" fill="#1a1714"/>
      {/* nose */}
      <path d="M 100 95 L 96 110 L 104 110 Z" fill="none" stroke="#a8842f" strokeWidth="0.5"/>
      {/* mouth — slight smile */}
      <path d="M 90 120 Q 100 124 110 120" fill="none" stroke="#7a4a3a" strokeWidth="1.3"/>
      {/* engraved cross-hatching for shading */}
      <g opacity="0.18" stroke="#7a5520" strokeWidth="0.4" fill="none">
        <line x1="62" y1="100" x2="72" y2="105"/>
        <line x1="62" y1="105" x2="72" y2="110"/>
        <line x1="62" y1="110" x2="72" y2="115"/>
        <line x1="128" y1="100" x2="138" y2="105"/>
        <line x1="128" y1="105" x2="138" y2="110"/>
        <line x1="128" y1="110" x2="138" y2="115"/>
      </g>
      {/* corners */}
      <path d="M 0 0 L 14 0 M 0 0 L 0 14" stroke="#c9a04a" strokeWidth="1"/>
      <path d="M 200 0 L 186 0 M 200 0 L 200 14" stroke="#c9a04a" strokeWidth="1"/>
      <path d="M 0 200 L 14 200 M 0 200 L 0 186" stroke="#c9a04a" strokeWidth="1"/>
      <path d="M 200 200 L 186 200 M 200 200 L 200 186" stroke="#c9a04a" strokeWidth="1"/>
    </svg>
  </div>
);

/* Swish QR placeholder */
const SwishQR = ({ size = 200 }) => {
  // generate a deterministic-looking QR pattern (decorative only)
  const cells = 21;
  const pattern = [];
  for (let i = 0; i < cells * cells; i++) {
    // pseudo-random based on position
    pattern.push((i * 73 + 17) % 7 < 3 ? 1 : 0);
  }
  // finder patterns
  const finder = (gx, gy) => {
    for (let y = 0; y < 7; y++) for (let x = 0; x < 7; x++) {
      const idx = (gy + y) * cells + (gx + x);
      const onBorder = (x === 0 || x === 6 || y === 0 || y === 6);
      const inner = (x >= 2 && x <= 4 && y >= 2 && y <= 4);
      pattern[idx] = onBorder || inner ? 1 : 0;
    }
  };
  finder(0, 0); finder(14, 0); finder(0, 14);
  // quiet zone in center for swish wordmark — no, leave clean

  return (
    <div style={{
      background: "#fff",
      padding: 14,
      border: "1px solid var(--parchment-3)",
      width: size + 28,
      display: "inline-block",
    }}>
      <svg width={size} height={size} viewBox={`0 0 ${cells} ${cells}`} shapeRendering="crispEdges">
        <rect width={cells} height={cells} fill="#fff"/>
        {pattern.map((c, i) => c ? (
          <rect key={i} x={i % cells} y={Math.floor(i / cells)} width="1" height="1" fill="#1a1714"/>
        ) : null)}
      </svg>
      <div style={{
        textAlign: "center",
        marginTop: 8,
        fontFamily: "var(--mono)",
        fontSize: 10,
        letterSpacing: "0.18em",
        textTransform: "uppercase",
        color: "#7a7164",
      }}>Swish · 0706010671</div>
    </div>
  );
};

/* Ticket-stub border — perforated edge effect */
const TicketStub = ({ children, accent = "var(--gold)" }) => (
  <div style={{
    background: "var(--parchment-2)",
    border: `1px solid ${accent}`,
    padding: "24px 28px",
    position: "relative",
    backgroundImage: `repeating-linear-gradient(0deg, transparent, transparent 6px, rgba(168,132,47,0.04) 6px, rgba(168,132,47,0.04) 7px)`,
  }}>
    {/* perforations on left */}
    <div style={{
      position: "absolute", left: -7, top: 8, bottom: 8, width: 14,
      backgroundImage: `radial-gradient(circle, var(--parchment) 4px, transparent 5px)`,
      backgroundSize: "14px 14px", backgroundRepeat: "repeat-y",
    }}/>
    {children}
  </div>
);

Object.assign(window, {
  TrainIcon, TrainLong, Ornament, SwedishFlag, CanadaFlag, MexicoFlag,
  EthiopiaFlag, USFlag, FlagFor, MetalSigil, CornerFlourish,
  WatercolorPlaceholder, JanPortrait, SwishQR, TicketStub,
});
