// ui.jsx — backdrop, icons and form field primitives for the contest page.

const LAKE_HUE = 212;

function Backdrop() {
  const a = `oklch(0.22 0.07 ${LAKE_HUE})`;
  const b = `oklch(0.08 0.04 ${LAKE_HUE + 10})`;
  const c = `oklch(0.35 0.09 ${LAKE_HUE - 20})`;
  return (
    <div
      aria-hidden="true"
      style={{
        position: "fixed",
        inset: 0,
        zIndex: 0,
        pointerEvents: "none",
        background: `
          radial-gradient(ellipse 80% 50% at 20% 10%, ${c} 0%, transparent 55%),
          radial-gradient(ellipse 60% 60% at 85% 70%, ${a} 0%, transparent 60%),
          linear-gradient(180deg, ${a} 0%, ${b} 100%)
        `,
      }}
    >
      <div style={{
        position: "absolute",
        left: "70%",
        top: "12%",
        width: 220,
        height: 220,
        borderRadius: "50%",
        background: `radial-gradient(circle, ${c} 0%, transparent 70%)`,
        filter: "blur(50px)",
        opacity: 0.85,
      }}/>
    </div>
  );
}

const Icon = {
  Upload: ({ size = 22 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M12 16V4M8 8l4-4 4 4" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M4 15v3a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-3" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
  Check: ({ size = 16 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M5 12.5l4.5 4.5L19 7.5" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  Close: ({ size = 18 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M6 6l12 12M18 6 6 18" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/>
    </svg>
  ),
  Heart: ({ filled, size = 18 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={filled ? "currentColor" : "none"} aria-hidden="true">
      <path d="M12 20s-7-4.35-7-10a4 4 0 0 1 7-2.65A4 4 0 0 1 19 10c0 5.65-7 10-7 10Z" stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round"/>
    </svg>
  ),
  Trophy: ({ size = 20 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M7 4h10v5a5 5 0 0 1-10 0V4Z" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"/>
      <path d="M7 6H4.5A1.5 1.5 0 0 0 3 7.5C3 9.4 4.6 11 6.5 11H7M17 6h2.5A1.5 1.5 0 0 1 21 7.5c0 1.9-1.6 3.5-3.5 3.5H17" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
      <path d="M12 14v3M9 20h6M10 17h4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
    </svg>
  ),
  Camera: ({ size = 20 }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" aria-hidden="true">
      <path d="M3 8.5A1.5 1.5 0 0 1 4.5 7h2.2l1.3-2h7l1.3 2h2.2A1.5 1.5 0 0 1 20 8.5v9A1.5 1.5 0 0 1 18.5 19h-13A1.5 1.5 0 0 1 4 17.5v-9Z" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"/>
      <circle cx="12" cy="13" r="3.4" stroke="currentColor" strokeWidth="1.5"/>
    </svg>
  ),
};

const FIELD_LABEL_STYLE = {
  display: "block",
  fontFamily: "'JetBrains Mono', monospace",
  fontSize: 10,
  letterSpacing: "0.22em",
  textTransform: "uppercase",
  color: "rgba(255,255,255,0.55)",
  marginBottom: 8,
};

function fieldSurface(focused, invalid) {
  return {
    width: "100%",
    borderRadius: 14,
    border: `1px solid ${
      invalid ? "rgba(255,120,120,0.65)" : focused ? "rgba(255,255,255,0.42)" : "rgba(255,255,255,0.16)"
    }`,
    background: focused ? "rgba(255,255,255,0.1)" : "rgba(255,255,255,0.06)",
    color: "#fff",
    padding: "13px 14px",
    fontSize: 16,
    fontFamily: "'Inter', system-ui, sans-serif",
    outline: "none",
    transition: "border-color 180ms ease, background 180ms ease",
  };
}

function FieldError({ children }) {
  if (!children) return null;
  return (
    <div style={{ marginTop: 6, fontSize: 12.5, color: "rgba(255,138,138,0.98)" }}>
      {children}
    </div>
  );
}

function FieldHint({ children }) {
  if (!children) return null;
  return (
    <div style={{ marginTop: 6, fontSize: 12.5, lineHeight: 1.45, color: "rgba(255,255,255,0.48)" }}>
      {children}
    </div>
  );
}

function TextField({ id, label, value, onChange, placeholder, type = "text", error, hint, disabled, required, autoComplete }) {
  const [focused, setFocused] = React.useState(false);
  return (
    <div>
      <label htmlFor={id} style={FIELD_LABEL_STYLE}>
        {label}{required ? " *" : ""}
      </label>
      <input
        id={id}
        type={type}
        value={value}
        placeholder={placeholder}
        disabled={disabled}
        autoComplete={autoComplete}
        aria-invalid={error ? "true" : undefined}
        onFocus={() => setFocused(true)}
        onBlur={() => setFocused(false)}
        onChange={(e) => onChange(e.target.value)}
        style={fieldSurface(focused, Boolean(error))}
      />
      <FieldError>{error}</FieldError>
      <FieldHint>{hint}</FieldHint>
    </div>
  );
}

function SelectField({ id, label, value, onChange, options, placeholder, error, disabled, required }) {
  const [focused, setFocused] = React.useState(false);
  return (
    <div>
      <label htmlFor={id} style={FIELD_LABEL_STYLE}>
        {label}{required ? " *" : ""}
      </label>
      <select
        id={id}
        value={value}
        disabled={disabled}
        aria-invalid={error ? "true" : undefined}
        onFocus={() => setFocused(true)}
        onBlur={() => setFocused(false)}
        onChange={(e) => onChange(e.target.value)}
        style={{
          ...fieldSurface(focused, Boolean(error)),
          appearance: "none",
          cursor: disabled ? "not-allowed" : "pointer",
          backgroundImage:
            "linear-gradient(45deg, transparent 50%, rgba(255,255,255,0.6) 50%), linear-gradient(135deg, rgba(255,255,255,0.6) 50%, transparent 50%)",
          backgroundPosition: "calc(100% - 20px) 22px, calc(100% - 14px) 22px",
          backgroundSize: "6px 6px, 6px 6px",
          backgroundRepeat: "no-repeat",
        }}
      >
        <option value="" disabled style={{ background: "#11151c" }}>{placeholder}</option>
        {options.map((option) => (
          <option key={option.value} value={option.value} style={{ background: "#11151c" }}>
            {option.label}
          </option>
        ))}
      </select>
      <FieldError>{error}</FieldError>
    </div>
  );
}

function CheckboxField({ id, checked, onChange, children, error, disabled }) {
  return (
    <div>
      <label
        htmlFor={id}
        style={{
          display: "flex",
          alignItems: "flex-start",
          gap: 12,
          cursor: disabled ? "not-allowed" : "pointer",
          fontSize: 13.5,
          lineHeight: 1.55,
          color: "rgba(255,255,255,0.82)",
        }}
      >
        <input
          id={id}
          type="checkbox"
          checked={checked}
          disabled={disabled}
          onChange={(e) => onChange(e.target.checked)}
          style={{ position: "absolute", opacity: 0, width: 1, height: 1 }}
        />
        <span
          aria-hidden="true"
          style={{
            flexShrink: 0,
            width: 22,
            height: 22,
            marginTop: 1,
            borderRadius: 7,
            display: "inline-flex",
            alignItems: "center",
            justifyContent: "center",
            border: `1px solid ${
              error ? "rgba(255,120,120,0.7)" : checked ? "rgba(255,255,255,0.85)" : "rgba(255,255,255,0.28)"
            }`,
            background: checked ? "rgba(255,255,255,0.9)" : "rgba(255,255,255,0.05)",
            color: "#0a0a0a",
            transition: "background 180ms ease, border-color 180ms ease",
          }}
        >
          {checked && <Icon.Check size={14} />}
        </span>
        <span>{children}</span>
      </label>
      <FieldError>{error}</FieldError>
    </div>
  );
}

Object.assign(window, {
  Backdrop,
  CheckboxField,
  FIELD_LABEL_STYLE,
  FieldError,
  FieldHint,
  Icon,
  LAKE_HUE,
  SelectField,
  TextField,
  fieldSurface,
});
