using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using Prospect.Server.Api.Services.Database;
using Prospect.Server.Api.Services.Database.Models;
using Prospect.Server.Api.Services.GameRef;
using Prospect.Server.Api.Services.Steam;
using Prospect.Server.Api.Services.UserData;

namespace Prospect.Server.Api.Controllers;

// Read-only back-office to visualise everything relevant in the database.
// Served at /admin, restricted to LAN / loopback callers (an admin panel must not be
// reachable from the public internet even though :8443 is port-forwarded).
// List pages (players, catalog) are driven by a small self-contained client-side table
// engine (sort / filter / search / paging) fed by the /admin/api/* JSON endpoints.
[Route("admin")]
public class AdminController : ControllerBase
{
    private readonly DbUserService _userService;
    private readonly DbEntityService _entityService;
    private readonly DbUserDataService _userDataService;
    private readonly TitleDataService _titleDataService;
    private readonly SteamWebApiService _steamWebApi;
    private readonly GameRefService _gameRef;

    public AdminController(DbUserService userService, DbEntityService entityService,
        DbUserDataService userDataService, TitleDataService titleDataService, SteamWebApiService steamWebApi,
        GameRefService gameRef)
    {
        _userService = userService;
        _entityService = entityService;
        _userDataService = userDataService;
        _titleDataService = titleDataService;
        _steamWebApi = steamWebApi;
        _gameRef = gameRef;
    }

    // ---------- pages ----------

    [HttpGet("")]
    public async Task<IActionResult> Index()
    {
        if (!IsLan()) return Denied();

        var users = await _userService.GetAllAsync();
        var entities = await _entityService.GetAllAsync();
        var dataCount = await _userDataService.CountAsync();
        var titleKeys = _titleDataService.Find(new List<string>());

        var sb = new StringBuilder();
        sb.Append("<div class='cards'>");
        sb.Append(Card("Joueurs", users.Count.ToString()));
        sb.Append(Card("Entités", entities.Count.ToString()));
        sb.Append(Card("Docs UserData", dataCount.ToString()));
        sb.Append(Card("Clés TitleData", titleKeys.Count.ToString()));
        sb.Append(Card("Catalogue", _gameRef.All.Count.ToString()));
        sb.Append("</div>");
        sb.Append("<p><a class='btn' href='/admin/players'>Joueurs →</a> &nbsp; <a class='btn' href='/admin/catalog'>Catalogue →</a> &nbsp; <a class='btn' href='/admin/titledata'>TitleData →</a></p>");
        return Html(Layout("Dashboard", sb.ToString()));
    }

    [HttpGet("players")]
    public IActionResult Players()
    {
        if (!IsLan()) return Denied();
        var body =
            "<h2>Joueurs</h2>" +
            "<p><a class='btn' href='/admin/backfill-names' title='Récupère les pseudos Steam des comptes sans nom'>⟳ Backfill pseudos Steam</a></p>" +
            "<div id='tbl'></div>" +
            "<script>DT.init({id:'tbl',url:'/admin/api/players',size:25,filters:['auth'],columns:[" +
            "{key:'name',label:'Nom',kind:'link',base:'/admin/players/',lkey:'id'}," +
            "{key:'id',label:'PlayFab Id',kind:'mono'}," +
            "{key:'entity',label:'Entity Id',kind:'mono'}," +
            "{key:'auth',label:'Auth',kind:'mono'}]});</script>";
        return Html(Layout("Joueurs", body));
    }

    [HttpGet("catalog")]
    public IActionResult Catalog()
    {
        if (!IsLan()) return Denied();
        var body =
            "<h2>Catalogue</h2>" +
            "<p class='muted'>Référence dataminée (TCF-Wiki) — " + _gameRef.All.Count + " entrées.</p>" +
            "<div id='tbl'></div>" +
            "<script>DT.init({id:'tbl',url:'/admin/api/catalog',size:50,filters:['category','rarity'],columns:[" +
            "{key:'name',label:'Nom'}," +
            "{key:'rarity',label:'Rareté',kind:'rarity'}," +
            "{key:'category',label:'Catégorie'}," +
            "{key:'id',label:'id',kind:'mono'}]});</script>";
        return Html(Layout("Catalogue", body));
    }

    [HttpGet("players/{id}")]
    public async Task<IActionResult> Player(string id)
    {
        if (!IsLan()) return Denied();

        var user = await _userService.FindByIdAsync(id);
        if (user == null) return Html(Layout("Joueur introuvable", "<p>Aucun joueur avec cet id.</p>"));

        var entity = await _entityService.FindAsync(id);
        var data = await _userDataService.GetAllForUserAsync(id);
        var map = data.ToDictionary(d => d.Key, d => d);

        var sb = new StringBuilder();
        var auth = user.Auth != null ? string.Join(", ", user.Auth.Select(a => $"{a.Type}:{a.Key}")) : "";
        sb.Append($"<p><a class='lnk' href='/admin/players'>← Joueurs</a></p>");
        sb.Append($"<h2>{Esc(user.DisplayName)}</h2>");
        sb.Append($"<p class='muted mono'>PlayFabId {Esc(user.Id)} · Entity {Esc(entity?.Id ?? "-")} · {Esc(auth)}</p>");

        sb.Append("<div class='cards'>");
        if (map.TryGetValue("Balance", out var bal))
        {
            sb.Append(Card("K-Marks", GetNum(bal.Value, "SoftCurrency").ToString()));
            sb.Append(Card("Aurum", GetNum(bal.Value, "HardCurrency").ToString()));
            sb.Append(Card("Insurance", GetNum(bal.Value, "InsuranceCurrency").ToString()));
        }
        foreach (var f in new[] { "Korolev", "Osiris", "ICA" })
            if (map.TryGetValue("FactionProgression" + f, out var fp))
                sb.Append(Card("Rép. " + f, (fp.Value ?? "0").Trim()));
        foreach (var s in new[] { "2", "3" })
            if (map.TryGetValue($"FortunaPass{s}_SeasonXp", out var xp) && (xp.Value ?? "0").Trim() != "0")
                sb.Append(Card($"Fortuna S{s} XP", (xp.Value ?? "0").Trim()));
        sb.Append("</div>");

        if (map.TryGetValue("Inventory", out var inv))
        {
            sb.Append("<h3>Inventaire</h3>");
            sb.Append(InventoryTable(inv.Value));
        }

        sb.Append("<h3>Toutes les données</h3>");
        foreach (var d in data)
        {
            sb.Append("<details><summary>" + Esc(d.Key) +
                      $" <span class='muted small'>· {d.Value?.Length ?? 0} car · maj {d.LastUpdated:yyyy-MM-dd HH:mm}{(d.Public ? " · public" : "")}</span></summary>");
            sb.Append($"<pre>{Esc(Pretty(d.Value))}</pre></details>");
        }
        return Html(Layout("Joueur · " + user.DisplayName, sb.ToString()));
    }

    [HttpGet("titledata")]
    public IActionResult TitleData()
    {
        if (!IsLan()) return Denied();

        var all = _titleDataService.Find(new List<string>());
        var sb = new StringBuilder();
        sb.Append("<h2>TitleData</h2>");
        sb.Append("<table><thead><tr><th>Clé</th><th>Taille</th><th></th></tr></thead><tbody>");
        foreach (var kv in all.OrderBy(k => k.Key))
            sb.Append($"<tr><td class='mono'>{Esc(kv.Key)}</td><td>{kv.Value?.Length ?? 0} car</td>" +
                      $"<td><a class='btn' href='/admin/titledata/{Esc(kv.Key)}'>Voir</a></td></tr>");
        sb.Append("</tbody></table>");
        return Html(Layout("TitleData", sb.ToString()));
    }

    [HttpGet("titledata/{key}")]
    public IActionResult TitleDataKey(string key)
    {
        if (!IsLan()) return Denied();

        var all = _titleDataService.Find(new List<string> { key });
        if (!all.TryGetValue(key, out var value))
            return Html(Layout("Clé introuvable", "<p>Aucune clé TitleData avec ce nom.</p>"));

        var body = value != null && value.Length > 300_000
            ? $"<p class='muted'>Valeur volumineuse ({value.Length} car) — affichage brut.</p><pre>{Esc(value)}</pre>"
            : $"<pre>{Esc(Pretty(value))}</pre>";
        return Html(Layout("TitleData · " + key, $"<p><a class='lnk' href='/admin/titledata'>← TitleData</a></p><h2 class='mono'>{Esc(key)}</h2>" + body));
    }

    [HttpGet("backfill-names")]
    public async Task<IActionResult> BackfillNames()
    {
        if (!IsLan()) return Denied();
        if (!_steamWebApi.Enabled)
            return Html(Layout("Backfill", "<p class='muted'>Steam Web API non configurée (clé absente).</p><p><a class='btn' href='/admin/players'>← Joueurs</a></p>"));

        var users = await _userService.GetAllAsync();
        int updated = 0, skipped = 0, failed = 0;
        var rows = new StringBuilder();
        foreach (var u in users)
        {
            if (!string.IsNullOrEmpty(u.DisplayName) && u.DisplayName != "Unknown") { skipped++; continue; }
            var steam = u.Auth?.FirstOrDefault(a => a.Type == PlayFabUserAuthType.Steam)?.Key;
            if (string.IsNullOrEmpty(steam)) { skipped++; continue; }

            var persona = await _steamWebApi.GetPersonaNameAsync(steam);
            if (!string.IsNullOrWhiteSpace(persona))
            {
                await _userService.UpdateDisplayNameAsync(u.Id, persona);
                updated++;
                rows.Append($"<tr><td class='mono small'>{Esc(u.Id)}</td><td>{Esc(persona)}</td></tr>");
            }
            else { failed++; }
        }

        var body = $"<h2>Backfill des pseudos Steam</h2><p class='muted'>{updated} mis à jour · {skipped} ignorés · {failed} échec(s)</p>" +
                   (updated > 0 ? $"<table><thead><tr><th>PlayFabId</th><th>Nouveau nom</th></tr></thead><tbody>{rows}</tbody></table>" : "") +
                   "<p><a class='btn' href='/admin/players'>← Joueurs</a></p>";
        return Html(Layout("Backfill", body));
    }

    // ---------- JSON API (consumed by the client-side table engine) ----------

    [HttpGet("api/players")]
    public async Task<IActionResult> ApiPlayers()
    {
        if (!IsLan()) return Denied();
        var users = await _userService.GetAllAsync();
        var entities = (await _entityService.GetAllAsync())
            .GroupBy(e => e.UserId).ToDictionary(g => g.Key, g => g.First().Id);
        var list = users.Select(u => new
        {
            name = string.IsNullOrEmpty(u.DisplayName) ? u.Id : u.DisplayName,
            id = u.Id,
            entity = entities.TryGetValue(u.Id, out var e) ? e : "",
            auth = u.Auth != null ? string.Join(", ", u.Auth.Select(a => $"{a.Type}:{a.Key}")) : ""
        });
        return Json(list);
    }

    [HttpGet("api/catalog")]
    public IActionResult ApiCatalog()
    {
        if (!IsLan()) return Denied();
        var list = _gameRef.All.Select(kv => new
        {
            id = kv.Key,
            name = kv.Value.Name,
            rarity = kv.Value.Rarity,
            category = kv.Value.Category
        });
        return Json(list);
    }

    // ---------- helpers ----------

    private bool IsLan()
    {
        var ip = HttpContext.Connection.RemoteIpAddress;
        if (ip == null) return false;
        if (IPAddress.IsLoopback(ip)) return true;
        if (ip.IsIPv4MappedToIPv6) ip = ip.MapToIPv4();
        if (ip.AddressFamily == AddressFamily.InterNetwork)
        {
            var b = ip.GetAddressBytes();
            if (b[0] == 10) return true;
            if (b[0] == 192 && b[1] == 168) return true;
            if (b[0] == 172 && b[1] >= 16 && b[1] <= 31) return true;
            if (b[0] == 127) return true;
            return false;
        }
        var s = ip.GetAddressBytes();
        if ((s[0] & 0xFE) == 0xFC) return true;
        if (s[0] == 0xFE && (s[1] & 0xC0) == 0x80) return true;
        return false;
    }

    private IActionResult Denied() =>
        StatusCode(403, "Admin back-office is LAN-only. Access it from the local network (e.g. https://192.168.1.136:8443/admin).");

    private IActionResult Html(string html) => Content(html, "text/html; charset=utf-8");

    private IActionResult Json(object data) =>
        Content(JsonSerializer.Serialize(data), "application/json; charset=utf-8");

    private static string Esc(string? s) => WebUtility.HtmlEncode(s ?? "");

    private static string Pretty(string? json)
    {
        if (string.IsNullOrWhiteSpace(json)) return json ?? "";
        try
        {
            using var doc = JsonDocument.Parse(json);
            return JsonSerializer.Serialize(doc.RootElement, new JsonSerializerOptions { WriteIndented = true });
        }
        catch { return json; }
    }

    private static long GetNum(string? json, string prop)
    {
        if (string.IsNullOrWhiteSpace(json)) return 0;
        try
        {
            using var doc = JsonDocument.Parse(json);
            foreach (var p in doc.RootElement.EnumerateObject())
                if (string.Equals(p.Name, prop, StringComparison.OrdinalIgnoreCase) && p.Value.ValueKind == JsonValueKind.Number)
                    return p.Value.GetInt64();
        }
        catch { }
        return 0;
    }

    private string InventoryTable(string? json)
    {
        if (string.IsNullOrWhiteSpace(json)) return "<p class='muted'>vide</p>";
        try
        {
            using var doc = JsonDocument.Parse(json);
            if (doc.RootElement.ValueKind != JsonValueKind.Array) return $"<pre>{Esc(Pretty(json))}</pre>";
            var sb = new StringBuilder();
            var count = doc.RootElement.GetArrayLength();
            sb.Append($"<p class='muted'>{count} item(s)</p>");
            sb.Append("<table><thead><tr><th>Nom</th><th>Rareté</th><th>baseItemId</th><th>qté</th><th>durab.</th><th>assurance</th></tr></thead><tbody>");
            foreach (var it in doc.RootElement.EnumerateArray())
            {
                string S(string k) => it.TryGetProperty(k, out var v) ? (v.ValueKind == JsonValueKind.Number ? v.GetRawText() : v.ToString()) : "";
                var baseId = S("baseItemId");
                var entry = _gameRef.Get(baseId);
                var name = entry?.Name ?? baseId;
                sb.Append($"<tr><td>{Esc(name)}</td><td>{Rarity(entry?.Rarity)}</td><td class='mono small'>{Esc(baseId)}</td>" +
                          $"<td>{Esc(S("amount"))}</td><td>{Esc(S("durability"))}</td><td>{Esc(S("insurance"))}</td></tr>");
            }
            sb.Append("</tbody></table>");
            return sb.ToString();
        }
        catch { return $"<pre>{Esc(Pretty(json))}</pre>"; }
    }

    private static string Rarity(string? r)
    {
        if (string.IsNullOrEmpty(r)) return "";
        var color = r.ToLowerInvariant() switch
        {
            "common" => "#9aa4b2",
            "uncommon" => "#4caf50",
            "rare" => "#2f81f7",
            "epic" => "#a371f7",
            "legendary" or "exotic" => "#e8a838",
            _ => "#9aa4b2",
        };
        return $"<span style='color:{color};font-weight:600'>{Esc(r)}</span>";
    }

    private static string Card(string label, string value) =>
        $"<div class='card'><div class='v'>{Esc(value)}</div><div class='l'>{Esc(label)}</div></div>";

    private static string Layout(string title, string body) =>
        Head.Replace("__TITLE__", Esc(title)) + DtScript + body + Foot;

    // Static shell (no C# interpolation → JS braces/quotes stay literal). Single quotes only.
    private const string Head = @"<!doctype html><html lang='fr'><head>
<meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'>
<title>__TITLE__ · The Cycle Admin</title>
<style>
:root{--bg:#0f1115;--panel:#171a21;--line:#262b36;--txt:#e6e8ec;--muted:#8b93a1;--acc:#e8a838;}
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--txt);font:14px/1.5 system-ui,Segoe UI,Roboto,sans-serif}
header{background:var(--panel);border-bottom:1px solid var(--line);padding:12px 20px;display:flex;gap:18px;align-items:center;position:sticky;top:0;z-index:10}
header b{color:var(--acc)}header a{color:var(--muted);text-decoration:none}header a:hover{color:var(--txt)}
main{padding:20px;max-width:1200px;margin:0 auto}
h2{margin:18px 0 10px}h3{margin:22px 0 8px;border-bottom:1px solid var(--line);padding-bottom:4px}
.cards{display:flex;flex-wrap:wrap;gap:12px;margin:12px 0}
.card{background:var(--panel);border:1px solid var(--line);border-radius:10px;padding:14px 18px;min-width:130px}
.card .v{font-size:22px;font-weight:700;color:var(--acc)}.card .l{color:var(--muted);font-size:12px}
table{width:100%;border-collapse:collapse;background:var(--panel);border:1px solid var(--line);border-radius:10px;overflow:hidden}
th,td{text-align:left;padding:8px 12px;border-bottom:1px solid var(--line);vertical-align:top}
th{background:#1c2029;color:var(--muted);font-weight:600}tr:last-child td{border-bottom:none}
tbody tr:hover{background:#1b2028}
.mono{font-family:ui-monospace,Consolas,monospace}.small{font-size:12px}.muted{color:var(--muted)}
.btn{display:inline-block;background:#222834;color:var(--txt);border:1px solid var(--line);border-radius:8px;padding:5px 12px;text-decoration:none;font-size:13px;cursor:pointer}
.btn:hover{border-color:var(--acc);color:var(--acc)}
a.lnk{color:var(--acc);text-decoration:none}a.lnk:hover{text-decoration:underline}
details{background:var(--panel);border:1px solid var(--line);border-radius:8px;margin:6px 0}summary{cursor:pointer;padding:8px 12px}
pre{margin:0;padding:12px;background:#0b0d11;border-top:1px solid var(--line);overflow:auto;max-height:480px;white-space:pre-wrap;word-break:break-word}
.dtbar{display:flex;gap:8px;flex-wrap:wrap;align-items:center;margin:12px 0}
.dtbar input,.dtbar select{background:var(--panel);border:1px solid var(--line);border-radius:8px;color:var(--txt);padding:7px 10px}
.dtsearch{flex:1;min-width:220px}
.dtcount{margin-left:auto}
th.dth{cursor:pointer;user-select:none;white-space:nowrap}th.dth:hover{color:var(--txt)}
.dtfoot{display:flex;gap:12px;align-items:center;margin:10px 0}
</style></head><body>
<header><b>The Cycle · Admin</b>
<a href='/admin'>Dashboard</a><a href='/admin/players'>Joueurs</a><a href='/admin/catalog'>Catalogue</a><a href='/admin/titledata'>TitleData</a>
<span class='muted' style='margin-left:auto'>read-only · LAN</span></header>
<main>";

    private const string Foot = @"</main></body></html>";

    // Client-side table engine, injected right after <main> so DT is defined before any
    // page-level DT.init() runs. Single quotes only (verbatim string → JS braces stay literal).
    private const string DtScript = @"<script>
const DT={
 init(cfg){const el=document.getElementById(cfg.id);el.innerHTML='<p class=muted>Chargement…</p>';
   fetch(cfg.url).then(r=>r.json()).then(rows=>DT.build(el,cfg,rows)).catch(e=>{el.innerHTML='<p class=muted>Erreur: '+e+'</p>';});},
 build(el,cfg,rows){el.innerHTML='';el._rows=rows;el._cfg=cfg;el._sort={k:null,d:1};el._page=1;el._size=cfg.size||25;el._filters={};el._q='';
   const bar=document.createElement('div');bar.className='dtbar';
   const s=document.createElement('input');s.className='dtsearch';s.placeholder='Rechercher…';s.oninput=()=>{el._q=s.value.toLowerCase();el._page=1;DT.draw(el);};bar.appendChild(s);
   (cfg.filters||[]).forEach(fk=>{const vals=[...new Set(rows.map(r=>r[fk]).filter(v=>v!=null&&v!==''))].sort();
     const sel=document.createElement('select');const o0=document.createElement('option');o0.value='';o0.textContent=fk;sel.appendChild(o0);
     vals.forEach(v=>{const o=document.createElement('option');o.value=v;o.textContent=v;sel.appendChild(o);});
     sel.onchange=()=>{el._filters[fk]=sel.value;el._page=1;DT.draw(el);};bar.appendChild(sel);});
   const sz=document.createElement('select');[25,50,100,500].forEach(n=>{const o=document.createElement('option');o.value=n;o.textContent=n+'/page';sz.appendChild(o);});
   const oa=document.createElement('option');oa.value='0';oa.textContent='Tout';sz.appendChild(oa);sz.value=el._size;
   sz.onchange=()=>{el._size=+sz.value;el._page=1;DT.draw(el);};bar.appendChild(sz);
   const cnt=document.createElement('span');cnt.className='dtcount muted';bar.appendChild(cnt);el._cnt=cnt;el.appendChild(bar);
   const tbl=document.createElement('table');const th=document.createElement('thead');const tr=document.createElement('tr');
   cfg.columns.forEach(c=>{const h=document.createElement('th');h.className='dth';h.dataset.k=c.key;h.textContent=c.label;h.onclick=()=>DT.sort(el,c.key);tr.appendChild(h);});
   th.appendChild(tr);tbl.appendChild(th);el._head=tr;const tb=document.createElement('tbody');tbl.appendChild(tb);el._tb=tb;el.appendChild(tbl);
   const ft=document.createElement('div');ft.className='dtfoot';
   const pv=document.createElement('button');pv.className='btn';pv.textContent='‹ Préc.';pv.onclick=()=>{if(el._page>1){el._page--;DT.draw(el);}};
   const nf=document.createElement('span');nf.className='muted';el._nfo=nf;
   const nx=document.createElement('button');nx.className='btn';nx.textContent='Suiv. ›';nx.onclick=()=>{el._page++;DT.draw(el);};
   ft.appendChild(pv);ft.appendChild(nf);ft.appendChild(nx);el.appendChild(ft);DT.draw(el);},
 rows(el){const cfg=el._cfg;let rows=el._rows;
   Object.keys(el._filters).forEach(k=>{const fv=el._filters[k];if(fv)rows=rows.filter(r=>(''+(r[k]??''))===fv);});
   if(el._q)rows=rows.filter(r=>cfg.columns.some(c=>(''+(r[c.key]??'')).toLowerCase().includes(el._q)));
   if(el._sort.k){const k=el._sort.k,d=el._sort.d;rows=[...rows].sort((a,b)=>{let x=a[k],y=b[k];const nx=parseFloat(x),ny=parseFloat(y);
     if(!isNaN(nx)&&!isNaN(ny)&&(''+x).trim()!==''&&(''+y).trim()!==''){x=nx;y=ny;}else{x=(''+(x??'')).toLowerCase();y=(''+(y??'')).toLowerCase();}return x<y?-d:x>y?d:0;});}
   return rows;},
 sort(el,k){if(el._sort.k===k)el._sort.d*=-1;else{el._sort.k=k;el._sort.d=1;}el._page=1;DT.draw(el);},
 draw(el){const cfg=el._cfg,rows=DT.rows(el),total=rows.length,size=el._size||total||1,pages=Math.max(1,Math.ceil(total/size));
   if(el._page>pages)el._page=pages;const start=el._size?(el._page-1)*size:0;const slice=el._size?rows.slice(start,start+size):rows;
   el._tb.innerHTML=slice.map(r=>'<tr>'+cfg.columns.map(c=>DT.cell(c,r)).join('')+'</tr>').join('')||'<tr><td colspan='+cfg.columns.length+' class=muted>Aucun résultat</td></tr>';
   el._cnt.textContent=total+' résultat(s)';el._nfo.textContent='page '+el._page+'/'+pages;
   [...el._head.children].forEach(h=>{const c=cfg.columns.find(x=>x.key===h.dataset.k);h.textContent=c.label+(el._sort.k===h.dataset.k?(el._sort.d>0?' ▲':' ▼'):'');});},
 cell(c,r){let v=r[c.key];v=v==null?'':(''+v);
   if(c.kind==='rarity')return '<td>'+DT.rarity(v)+'</td>';
   if(c.kind==='link')return '<td><a class=lnk href='+c.base+encodeURIComponent(r[c.lkey||'id'])+'>'+DT.esc(v)+'</a></td>';
   const cls=c.kind==='mono'?'mono small':'';return '<td class='+cls+'>'+DT.esc(v)+'</td>';},
 rarity(r){if(!r)return '';const c={common:'#9aa4b2',uncommon:'#4caf50',rare:'#2f81f7',epic:'#a371f7',legendary:'#e8a838',exotic:'#e8a838'}[r.toLowerCase()]||'#9aa4b2';
   return '<span style=color:'+c+';font-weight:600>'+DT.esc(r)+'</span>';},
 esc(s){const d=document.createElement('div');d.textContent=(s==null?'':''+s);return d.innerHTML;}
};
</script>";
}
