<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Professional Currency Converter</title>

<style>
body{
    font-family: 'Segoe UI', sans-serif;
    background: linear-gradient(135deg,#0f2027,#203a43,#2c5364);
    display:flex;
    justify-content:center;
    align-items:center;
    height:100vh;
    margin:0;
}

.converter{
    background:#ffffff;
    padding:30px;
    border-radius:15px;
    width:400px;
    box-shadow:0 10px 25px rgba(0,0,0,0.3);
}

.converter h2{
    text-align:center;
    margin-bottom:20px;
    color:#2c5364;
}

input, select{
    width:100%;
    padding:12px;
    margin:10px 0;
    border-radius:8px;
    border:1px solid #ddd;
    font-size:15px;
}

button{
    width:100%;
    padding:12px;
    border-radius:8px;
    border:none;
    font-size:16px;
    cursor:pointer;
    transition:0.3s;
}

.convert-btn{
    background:#2c5364;
    color:#fff;
}

.convert-btn:hover{
    background:#203a43;
}

.swap-btn{
    background:#f39c12;
    color:#fff;
    margin:5px 0;
}

.swap-btn:hover{
    background:#e67e22;
}

.result{
    margin-top:15px;
    padding:12px;
    background:#f4f4f4;
    border-radius:8px;
    text-align:center;
    font-weight:bold;
    font-size:16px;
}
</style>
</head>

<body>

<div class="converter">
    <h2>Live Currency Converter</h2>

    <input type="number" id="amount" value="100">

    <select id="from"></select>

    <button class="swap-btn" onclick="swapCurrency()">Swap ↑↓</button>

    <select id="to"></select>

    <button class="convert-btn" onclick="convert()">Convert Currency</button>

    <div class="result" id="result">Loading rates...</div>
</div>

<script>
const apiKey = "YOUR_API_KEY"; // Replace this
const fromSelect = document.getElementById("from");
const toSelect = document.getElementById("to");
const amountInput = document.getElementById("amount");
const resultDiv = document.getElementById("result");

async function loadCurrencies(){
    const res = await fetch(`https://v6.exchangerate-api.com/v6/${"46a5ea835da2b0d97933de3d"}/latest/USD`);
    const data = await res.json();

    const currencies = Object.keys(data.conversion_rates);

    currencies.forEach(currency=>{
        fromSelect.add(new Option(currency, currency));
        toSelect.add(new Option(currency, currency));
    });

    fromSelect.value = "USD";
    toSelect.value = "INR";

    convert();
}

async function convert(){
    const amount = amountInput.value;
    const from = fromSelect.value;
    const to = toSelect.value;

    if(!amount || amount <= 0){
        resultDiv.innerHTML = "Please enter valid amount";
        return;
    }

    const res = await fetch(`https://v6.exchangerate-api.com/v6/${"46a5ea835da2b0d97933de3d"}/latest/${from}`);
    const data = await res.json();
    const rate = data.conversion_rates[to];

    const converted = (amount * rate).toFixed(2);

    resultDiv.innerHTML = `
        ${amount} ${from} = ${converted} ${to}
        <br><small>1 ${from} = ${rate} ${to}</small>
    `;
}

function swapCurrency(){
    let temp = fromSelect.value;
    fromSelect.value = toSelect.value;
    toSelect.value = temp;
    convert();
}

amountInput.addEventListener("input", convert);
fromSelect.addEventListener("change", convert);
toSelect.addEventListener("change", convert);

loadCurrencies();
</script>

</body>
</html>