function formatVal(val,p){if(val===undefined||val===null)return '';if(!isFinite(val))return val>0?'∞':'-∞';if(p===0)return Math.round(val).toString();const factor=Math.pow(10,p);const rounded=Math.round(val*factor)/factor;return rounded.toFixed(p)}
function renderTemperature(container){const p=state.precision;const vals=state.values;const cVal=vals.Celsius!==undefined?vals.Celsius:'';const fVal=vals.Fahrenheit!==undefined?vals.Fahrenheit:'';const kVal=vals.Kelvin!==undefined?vals.Kelvin:'';container.innerHTML='';const cInput=$('tempC');const fInput=$('tempF');const kInput=$('tempK');cInput.oninput=()=>fromC(parseFloat(cInput.value));fInput.oninput=()=>fromF(parseFloat(fInput.value));kInput.oninput=()=>fromK(parseFloat(kInput.value));function fromC(c){if(isNaN(c))return;state.values={};fInput.value=formatVal(c*9/5+32,p);kInput.value=formatVal(c+273.15,p);state.values.Celsius=c}function fromF(f){if(isNaN(f))return;state.values={};const c=(f-32)*5/9;cInput.value=formatVal(c,p);kInput.value=formatVal(c+273.15,p);state.values.Fahrenheit=f}function fromK(k){if(isNaN(k))return;state.values={};const c=k-273.15;cInput.value=formatVal(c,p);fInput.value=formatVal(c*9/5+32,p);state.values.Kelvin=k}container.querySelectorAll('.copy-btn[data-temp-unit]').forEach(btn=>{btn.addEventListener('click',()=>{const unit=btn.dataset.tempUnit;const val=state.values[unit];if(val===undefined)return;const text=formatVal(val,p);if(navigator.clipboard){navigator.clipboard.writeText(text).catch(()=>{const ta=document.createElement('textarea');ta.value=text;document.body.appendChild(ta);ta.select();document.execCommand('copy');ta.remove()})}else{const ta=document.createElement('textarea');ta.value=text;document.body.appendChild(ta);ta.select();document.execCommand('copy');ta.remove()}showToast('已复制: '+text+' '+unit)})})}