CSS has now come with the "field-sizing" property.
input { field-sizing: content; }
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/field-sizing
Still experimental by november 2025, but it seems to work very well.