<!doctype html>
<html lang="<%= lang === 'zh' ? 'zh-CN' : lang %>">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
    <title><%= t('adminTitle') %></title>
    <link rel="stylesheet" href="/css/admin.css?v=evm-operator-wallet-20260718-i18n1">
    <style>
      .operator-shell { max-width: 980px; }
      .operator-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
      .operator-grid > div { border: 1px solid var(--wallet-line, #334155); padding: 14px; min-width: 0; }
      .operator-grid span { display: block; color: #9fb0c8; font-size: 13px; }
      .operator-grid strong { display: block; margin-top: 8px; color: #f4f7fb !important; overflow-wrap: anywhere; }
      .operator-address { display: block; margin: 14px 0; padding: 14px; color: #f4f7fb; overflow-wrap: anywhere; }
      .operator-warning { border-color: #b7791f; color: #f8d477; }
      .wallet-coin-row { display: grid; grid-template-columns: 58px minmax(0, 1fr) minmax(140px, auto); gap: 14px; align-items: center; padding: 4px 0; }
      .wallet-coin-symbol { width: 58px; height: 58px; object-fit: contain; }
      .wallet-coin-name strong, .wallet-coin-balance strong { display: block; color: #f4f7fb; font-size: 19px; }
      .wallet-coin-name span, .wallet-coin-balance span { display: block; margin-top: 5px; color: #9fb0c8; font-size: 13px; }
      .wallet-coin-balance { text-align: right; overflow-wrap: anywhere; }
      .wallet-coin-row + .wallet-coin-row { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--wallet-line, #334155); }
      .evm-language { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
      .evm-language .nav-button { min-width: 42px; padding: 8px 10px; text-align: center; }
      .evm-language .active { background: #2563eb; border-color: #3b82f6; color: #fff; }
      .wallet-flow-grid { display: grid; grid-template-columns: minmax(240px, .8fr) minmax(0, 1.2fr); gap: 24px; }
      .wallet-flow-grid > section + section { border-left: 1px solid var(--wallet-line, #334155); padding-left: 24px; }
      .receive-qr { display: block; width: 190px; max-width: 100%; margin: 16px auto; background: #fff; padding: 10px; }
      .wallet-check { display: flex !important; align-items: flex-start; gap: 9px; margin: 12px 0; }
      .wallet-check input { flex: 0 0 auto; width: 18px; height: 18px; margin-top: 2px; }
      .tx-row { display: grid; grid-template-columns: 90px minmax(0, 1fr) 120px; gap: 14px; padding: 14px 0; border-top: 1px solid var(--wallet-line, #334155); }
      .tx-row[hidden] { display: none; }
      .tx-row:first-child { border-top: 0; }
      .tx-row code { overflow-wrap: anywhere; white-space: normal; }
      .tx-row > strong, .tx-amount strong { color: #f4f7fb !important; }
      .tx-meta { color: #9fb0c8; font-size: 13px; line-height: 1.6; }
      .tx-amount { text-align: right; }
      .tx-asset { display: inline-block; color: #38f28b; font-size: 12px; letter-spacing: 0; }
      .wallet-history-links { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 18px; }
      .wallet-history-links a { color: #66e8b5; font-weight: 800; }
      .wallet-history-filter { display: flex; gap: 6px; margin: 0 0 14px; padding-bottom: 4px; overflow-x: auto; scrollbar-width: thin; }
      .wallet-history-filter button { flex: 0 0 auto; min-height: 38px; padding: 8px 14px; border: 1px solid #46536a; border-radius: 6px; background: #171c25; color: #cbd5e1; font-weight: 800; cursor: pointer; }
      .wallet-history-filter button[aria-pressed="true"] { border-color: #38f28b; background: #164936; color: #fff; }
      .wallet-transfer-overlay { position: fixed; inset: 0; z-index: 1000; display: grid; place-items: center; padding: 20px; background: rgba(3, 8, 18, .82); }
      .wallet-transfer-overlay[hidden] { display: none; }
      .wallet-transfer-progress { width: min(420px, 100%); padding: 26px; border: 1px solid #3b82f6; border-radius: 8px; background: #101827; box-shadow: 0 20px 60px rgba(0, 0, 0, .45); text-align: center; }
      .wallet-transfer-progress h2 { margin: 14px 0 8px; color: #f8fafc; }
      .wallet-transfer-spinner { width: 42px; height: 42px; margin: 0 auto; border: 4px solid #334155; border-top-color: #38bdf8; border-radius: 50%; animation: wallet-spin .8s linear infinite; }
      .wallet-toast { position: fixed; top: 20px; right: 20px; z-index: 1100; width: min(440px, calc(100vw - 40px)); padding: 18px 48px 18px 18px; border: 1px solid #22c55e; border-radius: 8px; background: #10251c; color: #dcfce7; box-shadow: 0 16px 42px rgba(0, 0, 0, .42); overflow-wrap: anywhere; }
      .wallet-toast.error { border-color: #ef4444; background: #2b1418; color: #fee2e2; }
      .wallet-toast strong { display: block; margin-bottom: 6px; font-size: 18px; }
      .wallet-toast p { margin: 0; line-height: 1.5; }
      .wallet-toast-close { position: absolute; top: 8px; right: 8px; width: 34px; height: 34px; padding: 0; border: 0; background: transparent; color: inherit; font-size: 24px; line-height: 1; cursor: pointer; }
      .js-transfer-form[aria-busy="true"] { opacity: .72; }
      @keyframes wallet-spin { to { transform: rotate(360deg); } }
      @media (max-width: 720px) {
        .operator-grid { grid-template-columns: 1fr; }
        .evm-language { justify-content: flex-start; }
        .wallet-flow-grid { grid-template-columns: 1fr; }
        .wallet-flow-grid > section + section { border-left: 0; border-top: 1px solid var(--wallet-line, #334155); padding: 20px 0 0; }
        .wallet-coin-row { grid-template-columns: 52px minmax(0, 1fr); }
        .wallet-coin-symbol { width: 52px; height: 52px; }
        .wallet-coin-balance { grid-column: 2; text-align: left; }
        .tx-row { grid-template-columns: 72px minmax(0, 1fr); }
        .tx-amount { grid-column: 2; text-align: left; }
      }
    </style>
  </head>
  <body class="wallet-screen">
    <main class="shell explorer-shell wallet-app wallet-native operator-shell">
      <header class="topbar wallet-topbar">
        <div>
          <p class="eyebrow">SUPERADMIN ONLY</p>
          <h1><%= t('adminTitle') %></h1>
          <p class="muted"><%= walletUser.loginId || walletUser.userid %> <%= t('accountOnly') %></p>
        </div>
        <div class="top-actions">
          <div class="evm-language" aria-label="Language">
            <% languages.forEach(function(code) { %>
              <a class="nav-button subtle <%= code === lang ? 'active' : '' %>" href="<%= langUrl(code) %>"><%= code.toUpperCase() %></a>
            <% }) %>
          </div>
          <a class="nav-button subtle" href="/wallet/evm?personal=1&amp;lang=<%= lang %>"><%= t('personalWallet') %></a>
          <a class="nav-button subtle" href="/explorer?lang=<%= lang %>">Explorer</a>
        </div>
      </header>

      <% if (notice && transferResult) { %><aside class="wallet-toast" id="walletResultToast" role="status" aria-live="polite"><button class="wallet-toast-close" type="button" aria-label="<%= t('close') %>" onclick="this.parentElement.remove()">&times;</button><strong><%= t('transferCompleteTitle') %></strong><p><%= notice %></p></aside><% } else if (notice) { %><section class="panel wallet-success"><%= notice %></section><% } %>
      <% if (error && transferResult) { %><aside class="wallet-toast error" id="walletResultToast" role="alert"><button class="wallet-toast-close" type="button" aria-label="<%= t('close') %>" onclick="this.parentElement.remove()">&times;</button><strong><%= t('transferFailedTitle') %></strong><p><%= error %></p></aside><% } else if (error) { %><section class="panel wallet-alert wallet-alert-error"><%= error %></section><% } %>

      <section class="panel">
        <div class="panel-title">
          <h2><%= t('operatorStatus') %></h2>
          <span><%= operatorWallet.online ? 'ONLINE' : 'OFFLINE' %></span>
        </div>
        <% if (!operatorWallet.configured) { %>
          <p class="wallet-alert-error"><%= t('operatorNotConfigured') %> <%= t('errorLabel') %>: <%= operatorWallet.error %></p>
        <% } else { %>
          <code class="address-code operator-address" id="operatorAddress"><%= operatorWallet.address %></code>
          <button class="nav-button" type="button" onclick="copyOperatorAddress(this)"><%= t('copyAddress') %></button>
          <div class="operator-grid">
            <div><span><%= t('ownerAccount') %></span><strong><%= operatorWallet.ownerLoginId %></strong></div>
            <div><span><%= t('candidateBalance') %></span><strong id="operatorLiveBalance"><%= operatorWallet.balance || '-' %> PLAIS</strong></div>
            <div><span><%= t('storageMethod') %></span><strong>OFFLINE DPAPI / SIGNER LOCKED</strong></div>
            <div><span><%= t('network') %></span><strong><%= network.networkName %></strong></div>
            <div><span>Chain ID</span><strong><%= operatorWallet.chainId %></strong></div>
            <div><span><%= t('latestBlock') %></span><strong><%= operatorWallet.blockNumber === null ? '-' : operatorWallet.blockNumber %></strong></div>
          </div>
        <% } %>
      </section>

      <% if (operatorWallet.configured) { %>
        <section class="panel">
          <div class="panel-title"><h2><%= ({ ko: '코인 목록', en: 'Coins', zh: '代币列表', ja: 'コイン一覧' })[lang] || 'Coins' %></h2><span>4</span></div>
          <div class="wallet-coin-row">
            <img class="wallet-coin-symbol" src="/images/plais-symbol.png" alt="PLAIS symbol">
            <div class="wallet-coin-name"><strong>PLAIS</strong><span><%= network.networkName %></span></div>
            <div class="wallet-coin-balance"><strong id="operatorCoinListBalance"><%= operatorWallet.balance || '-' %> PLAIS</strong><span><%= t('candidateBalance') %></span></div>
          </div>
          <div class="wallet-coin-row"><img class="wallet-coin-symbol" src="/images/bnb-symbol.svg" alt="BNB symbol"><div class="wallet-coin-name"><strong>BNB</strong><span>BNB Smart Chain</span></div><div class="wallet-coin-balance"><strong id="operatorBnbBalance"><%= multiChain.bsc.bnbBalance %> BNB</strong><span>Native coin · network fee</span></div></div>
          <div class="wallet-coin-row"><img class="wallet-coin-symbol" src="/images/babydoge-symbol.png" alt="Baby Doge Coin symbol"><div class="wallet-coin-name"><strong>BABYDOGE</strong><span>BNB Smart Chain</span></div><div class="wallet-coin-balance"><strong id="operatorBabyDogeBalance"><%= multiChain.bsc.babyDogeBalance %> BABYDOGE</strong><span>Network fee: BNB</span></div></div>
          <div class="wallet-coin-row"><img class="wallet-coin-symbol" src="/images/trx-symbol.png" alt="TRX symbol"><div class="wallet-coin-name"><strong>TRX</strong><span>TRON Mainnet</span></div><div class="wallet-coin-balance"><strong id="operatorTrxBalance"><%= multiChain.tron.balance %> TRX</strong><span><%= multiChain.tron.activated ? 'Active' : 'Not activated' %></span></div></div>
        </section>
      <% } %>

      <% if (operatorWallet.configured) { %>
        <section class="panel">
          <div class="wallet-flow-grid">
            <section>
              <div class="panel-title"><h2><%= t('receive') %></h2><span>PLAIS</span></div>
              <p class="muted"><%= t('receiveDescription') %></p>
              <% if (receiveQr) { %><img class="receive-qr" src="<%= receiveQr %>" alt="<%= t('receiveQrAlt') %>"><% } %>
              <code class="address-code operator-address" id="operatorReceiveAddress"><%= operatorWallet.address %></code>
              <button class="nav-button" type="button" onclick="copyText('operatorReceiveAddress', this)"><%= t('copyAddress') %></button>
            </section>
            <section>
              <div class="panel-title"><h2><%= t('operatorSend') %></h2><span><%= config.externalTransfersEnabled ? 'OPEN' : 'LOCKED' %></span></div>
              <p class="muted"><%= t('sendDescription') %></p>
              <form class="wallet-form js-transfer-form" method="post" action="/wallet/evm/admin/send?lang=<%= lang %>">
                <input type="hidden" name="csrfToken" value="<%= csrfToken('operator-send') %>">
                <label><%= t('recipientAddress') %><input name="to" autocomplete="off" placeholder="0x..." required></label>
                <label><%= t('amount') %><input name="amount" type="number" min="<%= config.minimumTransferAmount %>" step="<%= config.minimumTransferAmount %>" inputmode="decimal" placeholder="1.000000" required></label>
                <p class="muted js-amount-preview" aria-live="polite"><%= lang === 'ko' ? '최소 전송 단위' : 'Minimum transfer' %>: <strong><%= config.minimumTransferAmount %> PLAIS</strong></p>
                <label><%= t('memo') %> (<%= t('optional') %>)<input name="memo" maxlength="200"></label>
                <label class="wallet-check"><input name="confirmed" type="checkbox" value="1" required><span><%= t('confirmRecipient') %> <%= t('sendWarning') %></span></label>
                <button type="submit" <%= config.externalTransfersEnabled && operatorWallet.signingEnabled ? '' : 'disabled' %>><%= t('sendFunds') %></button>
              </form>
            </section>
          </div>
        </section>

        <section class="panel">
          <div class="panel-title"><h2>BNB</h2><span>BNB Smart Chain</span></div>
          <div class="wallet-flow-grid"><section><h2><%= t('receive') %></h2><p class="muted">Native BNB only</p><% if (receiveQr) { %><img class="receive-qr" src="<%= receiveQr %>" alt="BNB receive QR"><% } %><code class="address-code operator-address" id="operatorBnbAddress"><%= operatorWallet.address %></code><button class="nav-button" type="button" onclick="copyText('operatorBnbAddress', this)"><%= t('copyAddress') %></button></section><section><h2><%= t('send') %></h2><p class="muted">The network fee is paid separately from the BNB amount.</p><form class="wallet-form js-transfer-form" data-asset="BNB" data-minimum="<%= multiChain.config.bsc.minimumTransferAmount %>" method="post" action="/wallet/evm/admin/send/bnb?lang=<%= lang %>"><input type="hidden" name="csrfToken" value="<%= csrfToken('operator-send-bnb') %>"><label><%= t('recipientAddress') %><input name="to" placeholder="0x..." required></label><label><%= t('amount') %><input name="amount" type="number" min="<%= multiChain.config.bsc.minimumTransferAmount %>" step="0.000001" required></label><label class="wallet-check"><input name="confirmed" type="checkbox" value="1" required><span><%= t('confirmRecipient') %> <%= t('sendWarning') %></span></label><button type="submit" disabled><%= t('sendFunds') %></button></form></section></div>
        </section>

        <section class="panel">
          <div class="panel-title"><h2>BABYDOGE</h2><span>BNB Smart Chain</span></div>
          <div class="wallet-flow-grid"><section><h2><%= t('receive') %></h2><p class="muted">BEP-20 BABYDOGE only</p><% if (receiveQr) { %><img class="receive-qr" src="<%= receiveQr %>" alt="BABYDOGE receive QR"><% } %><code class="address-code operator-address" id="operatorBabyDogeAddress"><%= operatorWallet.address %></code><button class="nav-button" type="button" onclick="copyText('operatorBabyDogeAddress', this)"><%= t('copyAddress') %></button></section><section><h2><%= t('send') %></h2><p class="muted">BNB network fee balance: <%= multiChain.bsc.bnbBalance %> BNB</p><form class="wallet-form js-transfer-form" data-asset="BABYDOGE" data-minimum="<%= multiChain.config.babyDoge.minimumTransferAmount %>" method="post" action="/wallet/evm/admin/send/babydoge?lang=<%= lang %>"><input type="hidden" name="csrfToken" value="<%= csrfToken('operator-send-babydoge') %>"><label><%= t('recipientAddress') %><input name="to" placeholder="0x..." required></label><label><%= t('amount') %><input name="amount" type="number" min="<%= multiChain.config.babyDoge.minimumTransferAmount %>" step="0.000000001" required></label><label class="wallet-check"><input name="confirmed" type="checkbox" value="1" required><span><%= t('confirmRecipient') %> <%= t('sendWarning') %></span></label><button type="submit" disabled><%= t('sendFunds') %></button></form></section></div>
        </section>

        <section class="panel">
          <div class="panel-title"><h2>TRX</h2><span>TRON Mainnet</span></div>
          <div class="wallet-flow-grid"><section><h2><%= t('receive') %></h2><p class="muted">Native TRX only</p><% if (tronReceiveQr) { %><img class="receive-qr" src="<%= tronReceiveQr %>" alt="TRX receive QR"><% } %><code class="address-code operator-address" id="operatorTrxAddress"><%= multiChain.tron.address %></code><button class="nav-button" type="button" onclick="copyText('operatorTrxAddress', this)"><%= t('copyAddress') %></button></section><section><h2><%= t('send') %></h2><form class="wallet-form js-transfer-form" data-asset="TRX" data-minimum="<%= multiChain.config.tron.minimumTransferAmount %>" method="post" action="/wallet/evm/admin/send/trx?lang=<%= lang %>"><input type="hidden" name="csrfToken" value="<%= csrfToken('operator-send-trx') %>"><label><%= t('recipientAddress') %><input name="to" placeholder="T..." required></label><label><%= t('amount') %><input name="amount" type="number" min="<%= multiChain.config.tron.minimumTransferAmount %>" step="0.000001" required></label><label class="wallet-check"><input name="confirmed" type="checkbox" value="1" required><span><%= t('confirmRecipient') %> <%= t('sendWarning') %></span></label><button type="submit" disabled><%= t('sendFunds') %></button></form></section></div>
        </section>

        <section class="panel">
          <div class="panel-title"><h2><%= t('transactionHistory') %></h2><a class="nav-button subtle" href="/wallet/evm/admin?lang=<%= lang %>"><%= t('refresh') %></a></div>
          <div class="wallet-history-filter" data-history-target="operatorLiveTransactions" data-empty-target="operatorHistoryEmpty" data-active-asset="ALL" aria-label="Coin history filter">
            <button type="button" data-asset-filter="ALL" aria-pressed="true"><%= ({ ko: '전체', en: 'All', zh: '全部', ja: 'すべて' })[lang] || 'All' %></button>
            <button type="button" data-asset-filter="PLAIS" aria-pressed="false">PLAIS</button>
            <button type="button" data-asset-filter="BNB" aria-pressed="false">BNB</button>
            <button type="button" data-asset-filter="BABYDOGE" aria-pressed="false">BABYDOGE</button>
            <button type="button" data-asset-filter="TRX" aria-pressed="false">TRX</button>
          </div>
          <p class="muted" id="operatorHistoryEmpty" <%= walletTransactions.length ? 'hidden' : '' %>><%= t('noTransactions') %></p>
          <div id="operatorLiveTransactions"><% walletTransactions.forEach(function(tx) { const outgoing = tx.direction === 'out'; %>
            <div class="tx-row" data-wallet-asset="<%= tx.asset %>">
              <strong><span class="tx-asset"><%= tx.asset %></span><br><%= outgoing ? t('sent') : t('received') %></strong>
              <div><code><%= tx.hash %></code><div class="tx-meta"><%= outgoing ? tx.to : tx.from %><br><%= new Date(tx.createdAt).toLocaleString(localeTag) %> · <%= t(tx.status === 'confirmed' ? 'confirmed' : (tx.status === 'failed' ? 'failedStatus' : 'pending')) %><% if (tx.blockNumber) { %> · <%= t('block') %> <%= tx.blockNumber %><% } %></div><a href="<%= tx.explorerUrl %><%= tx.asset === 'PLAIS' ? '?lang=' + lang : '' %>" <%- tx.asset === 'PLAIS' ? '' : 'target="_blank" rel="noopener noreferrer"' %>><%= t('openExplorer') %></a></div>
              <div class="tx-amount"><strong><%= outgoing ? '-' : '+' %><%= tx.amount %> <%= tx.asset %></strong><% if (tx.fee) { %><div class="tx-meta"><%= t('fee') %> <%= tx.fee %> <%= tx.asset === 'TRX' ? 'TRX' : (tx.asset === 'PLAIS' ? 'PLAIS' : 'BNB') %></div><% } %></div>
            </div>
          <% }) %></div>
          <% if (multiChain && multiChain.transactions) { %><div class="wallet-history-links"><a href="<%= multiChain.transactions.bscAddressUrl %>" target="_blank" rel="noopener noreferrer">BSC <%= t('openExplorer') %></a><a href="<%= multiChain.transactions.tronAddressUrl %>" target="_blank" rel="noopener noreferrer">TRON <%= t('openExplorer') %></a></div><% } %>
        </section>
      <% } %>

      <section class="panel operator-warning">
        <strong><%= t('operatorKeyHidden') %></strong>
        <p><%= t('operatorDescription', { state: config.externalTransfersEnabled ? 'OPEN' : 'LOCKED' }) %></p>
      </section>
    </main>
    <div class="wallet-transfer-overlay" id="walletTransferOverlay" role="status" aria-live="assertive" aria-busy="true" hidden>
      <div class="wallet-transfer-progress">
        <div class="wallet-transfer-spinner" aria-hidden="true"></div>
        <h2><%= t('sending') %></h2>
        <p class="muted"><%= t('sendingDescription') %></p>
      </div>
    </div>
    <script>
      const copiedText = <%- JSON.stringify(t('copied')) %>;
      function copyText(id, button) {
        const target = document.getElementById(id);
        if (!target) return;
        navigator.clipboard.writeText(target.textContent.trim()).then(function() {
          const previous = button.textContent;
          button.textContent = copiedText;
          setTimeout(function() { button.textContent = previous; }, 1200);
        });
      }
      function copyOperatorAddress(button) { copyText('operatorAddress', button); }
      function applyWalletHistoryFilter(group, asset) {
        if (!group) return;
        const target = document.getElementById(group.dataset.historyTarget);
        if (!target) return;
        let visible = 0;
        target.querySelectorAll('[data-wallet-asset]').forEach(function(row) {
          const show = asset === 'ALL' || row.dataset.walletAsset === asset;
          row.hidden = !show;
          if (show) visible += 1;
        });
        group.dataset.activeAsset = asset;
        group.querySelectorAll('[data-asset-filter]').forEach(function(button) {
          button.setAttribute('aria-pressed', button.dataset.assetFilter === asset ? 'true' : 'false');
        });
        const empty = document.getElementById(group.dataset.emptyTarget);
        if (empty) empty.hidden = visible !== 0;
      }
      document.querySelectorAll('.wallet-history-filter').forEach(function(group) {
        group.addEventListener('click', function(event) {
          const button = event.target.closest('[data-asset-filter]');
          if (button) applyWalletHistoryFilter(group, button.dataset.assetFilter);
        });
        applyWalletHistoryFilter(group, group.dataset.activeAsset || 'ALL');
      });
      document.querySelectorAll('.js-transfer-form').forEach(function(form) {
        const transferAsset = form.dataset.asset || 'PLAIS';
        const minimumAmount = form.dataset.minimum || <%- JSON.stringify(config.minimumTransferAmount) %>;
        const amountInput = form.querySelector('input[name="amount"]');
        const amountPreview = form.querySelector('.js-amount-preview strong');
        if (amountInput && amountPreview) {
          amountInput.addEventListener('input', function() {
            amountPreview.textContent = (amountInput.value || minimumAmount) + ' ' + transferAsset;
          });
        }
        form.addEventListener('submit', function(event) {
          if (form.dataset.submitting === '1') {
            event.preventDefault();
            return;
          }
          const amount = amountInput ? amountInput.value : '';
          const confirmText = <%- JSON.stringify(lang === 'ko' ? '전송 수량을 다시 확인하세요.\n\n' : 'Confirm the transfer amount.\n\n') %> + amount + ' ' + transferAsset;
          if (!window.confirm(confirmText)) {
            event.preventDefault();
            return;
          }
          form.dataset.submitting = '1';
          form.setAttribute('aria-busy', 'true');
          const submitButton = form.querySelector('button[type="submit"]');
          if (submitButton) {
            submitButton.disabled = true;
            submitButton.textContent = <%- JSON.stringify(t('sending')) %>;
          }
          document.getElementById('walletTransferOverlay').hidden = false;
        });
      });
      const resultToast = document.getElementById('walletResultToast');
      if (resultToast && !resultToast.classList.contains('error')) {
        setTimeout(function() { resultToast.remove(); }, 10000);
      }
      const resultUrl = new URL(window.location.href);
      if (resultUrl.searchParams.has('sent') || resultUrl.searchParams.has('errorCode')) {
        resultUrl.searchParams.delete('sent');
        resultUrl.searchParams.delete('errorCode');
        window.history.replaceState({}, '', resultUrl.pathname + (resultUrl.search ? resultUrl.search : ''));
      }
      if (document.getElementById('operatorLiveBalance')) {
        setInterval(async function() {
          if (document.hidden || document.querySelector('.js-transfer-form[aria-busy="true"]')) return;
          try {
            const response = await fetch(window.location.href, { credentials: 'same-origin', cache: 'no-store' });
            if (!response.ok) return;
            const nextDocument = new DOMParser().parseFromString(await response.text(), 'text/html');
            const nextBalance = nextDocument.getElementById('operatorLiveBalance');
            const nextCoinBalance = nextDocument.getElementById('operatorCoinListBalance');
            const nextBabyDogeBalance = nextDocument.getElementById('operatorBabyDogeBalance');
            const nextBnbBalance = nextDocument.getElementById('operatorBnbBalance');
            const nextTrxBalance = nextDocument.getElementById('operatorTrxBalance');
            const nextTransactions = nextDocument.getElementById('operatorLiveTransactions');
            if (nextBalance) document.getElementById('operatorLiveBalance').textContent = nextBalance.textContent;
            if (nextCoinBalance) document.getElementById('operatorCoinListBalance').textContent = nextCoinBalance.textContent;
            if (nextBabyDogeBalance) document.getElementById('operatorBabyDogeBalance').textContent = nextBabyDogeBalance.textContent;
            if (nextBnbBalance) document.getElementById('operatorBnbBalance').textContent = nextBnbBalance.textContent;
            if (nextTrxBalance) document.getElementById('operatorTrxBalance').textContent = nextTrxBalance.textContent;
            if (nextTransactions) {
              document.getElementById('operatorLiveTransactions').innerHTML = nextTransactions.innerHTML;
              const historyFilter = document.querySelector('[data-history-target="operatorLiveTransactions"]');
              applyWalletHistoryFilter(historyFilter, historyFilter ? historyFilter.dataset.activeAsset : 'ALL');
            }
          } catch (error) {
            // A temporary refresh failure must not interrupt wallet use.
          }
        }, 10000);
      }
    </script>
  </body>
</html>
