const css = `
.m-label{display:flex;flex-direction:column;gap:4px;font-family:var(--mono);font-size:11px;color:var(--muted);letter-spacing:.08em}
.m-input{font-family:var(--mono);font-size:13px;background:var(--bg-deep);color:var(--ink);border:1px solid var(--line);padding:7px 10px;outline:none;transition:border-color .12s;border-radius:var(--radius-sm,4px)}
.m-input:focus{border-color:var(--green-dim);box-shadow:0 0 0 1px var(--green-dim)}
.m-input::placeholder{color:var(--faint)}
.m-req{color:var(--red)}
`;
export function ensureFieldCss() {
  if (typeof document === 'undefined' || document.getElementById('m-field-css')) return;
  const s = document.createElement('style'); s.id = 'm-field-css'; s.textContent = css;
  document.head.appendChild(s);
}

export function Input({ label, required, type = 'text', placeholder, value, defaultValue, onChange, width, style }) {
  ensureFieldCss();
  const ctrl = (
    <input className="m-input" type={type} placeholder={placeholder} value={value}
      defaultValue={defaultValue} style={{ width, ...style }} autoComplete="off"
      onChange={onChange ? (e) => onChange(e.target.value) : undefined} readOnly={value !== undefined && !onChange} />
  );
  if (!label) return ctrl;
  return (
    <label className="m-label">
      <span>{label}{required ? <span className="m-req"> *</span> : null}</span>
      {ctrl}
    </label>
  );
}
