<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>AdGuard Block Tracker</title>
  <link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Rajdhani:wght@400;600;700&display=swap" rel="stylesheet"/>
  <style>
    :root {
      --bg:       #0a0a0f;
      --surface:  #12121a;
      --border:   #1e1e2e;
      --text:     #e2e8f0;
      --muted:    #64748b;
      --t1: #4ade80;
      --t2: #facc15;
      --t3: #fb923c;
      --t4: #f87171;
      --t5: #c084fc;
    }

    * { box-sizing: border-box; margin: 0; padding: 0; }

    body {
      background: var(--bg);
      color: var(--text);
      font-family: 'Rajdhani', sans-serif;
      min-height: 100vh;
      overflow-x: hidden;
    }

    /* Scanline overlay */
    body::before {
      content: '';
      position: fixed;
      inset: 0;
      background: repeating-linear-gradient(
        0deg,
        transparent,
        transparent 2px,
        rgba(0,0,0,0.07) 2px,
        rgba(0,0,0,0.07) 4px
      );
      pointer-events: none;
      z-index: 100;
    }

    header {
      padding: 2.5rem 2rem 1rem;
      text-align: center;
      position: relative;
    }

    header::after {
      content: '';
      display: block;
      height: 2px;
      background: linear-gradient(90deg, transparent, var(--t5), var(--t4), var(--t3), var(--t2), var(--t1), transparent);
      margin-top: 1.5rem;
    }

    h1 {
      font-family: 'Bebas Neue', sans-serif;
      font-size: clamp(2.5rem, 6vw, 4.5rem);
      letter-spacing: 0.08em;
      background: linear-gradient(135deg, #c084fc, #f87171, #fb923c);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      background-clip: text;
      line-height: 1;
    }

    .subtitle {
      font-size: 1rem;
      color: var(--muted);
      letter-spacing: 0.2em;
      text-transform: uppercase;
      margin-top: 0.4rem;
    }

    .controls {
      display: flex;
      justify-content: center;
      align-items: center;
      gap: 1.5rem;
      padding: 1.2rem 2rem;
      font-size: 0.85rem;
      color: var(--muted);
      letter-spacing: 0.1em;
    }

    .status-dot {
      width: 8px; height: 8px;
      border-radius: 50%;
      background: var(--t1);
      box-shadow: 0 0 8px var(--t1);
      display: inline-block;
      animation: pulse 2s infinite;
    }

    .status-dot.error { background: var(--t4); box-shadow: 0 0 8px var(--t4); animation: none; }

    @keyframes pulse {
      0%, 100% { opacity: 1; }
      50% { opacity: 0.4; }
    }

    .refresh-btn {
      background: none;
      border: 1px solid var(--border);
      color: var(--muted);
      font-family: 'Rajdhani', sans-serif;
      font-size: 0.85rem;
      letter-spacing: 0.15em;
      padding: 0.4rem 1rem;
      cursor: pointer;
      transition: all 0.2s;
      text-transform: uppercase;
    }

    .refresh-btn:hover {
      border-color: var(--t5);
      color: var(--t5);
    }

    .tier-legend {
      display: flex;
      justify-content: center;
      flex-wrap: wrap;
      gap: 0.6rem;
      padding: 0 2rem 1.5rem;
    }

    .tier-pill {
      font-size: 0.72rem;
      letter-spacing: 0.12em;
      text-transform: uppercase;
      padding: 0.25rem 0.75rem;
      border-radius: 2px;
      border: 1px solid;
      font-weight: 700;
    }

    .grid {
      display: grid;
      grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
      gap: 1.25rem;
      padding: 0 2rem 3rem;
      max-width: 1400px;
      margin: 0 auto;
    }

    .card {
      background: var(--surface);
      border: 1px solid var(--border);
      position: relative;
      overflow: hidden;
      transition: transform 0.25s, box-shadow 0.25s;
      animation: cardIn 0.4s ease both;
    }

    .card:hover {
      transform: translateY(-4px);
      box-shadow: 0 12px 40px rgba(0,0,0,0.5);
    }

    @keyframes cardIn {
      from { opacity: 0; transform: translateY(16px); }
      to   { opacity: 1; transform: translateY(0); }
    }

    .card-accent {
      height: 3px;
      width: 100%;
    }

    .card-body {
      padding: 1.25rem;
    }

    .card-rank {
      font-family: 'Bebas Neue', sans-serif;
      font-size: 0.9rem;
      letter-spacing: 0.15em;
      color: var(--muted);
      margin-bottom: 0.75rem;
    }

    /* Character placeholder art */
    .char-frame {
      width: 100%;
      aspect-ratio: 1 / 1.1;
      margin-bottom: 1rem;
      display: flex;
      align-items: center;
      justify-content: center;
      background: var(--bg);
      border: 1px solid var(--border);
      position: relative;
      overflow: hidden;
    }

    .char-frame svg {
      width: 75%;
      height: 75%;
    }

    /* Tier glow on frame */
    .char-frame::after {
      content: '';
      position: absolute;
      inset: 0;
      box-shadow: inset 0 0 30px rgba(0,0,0,0.6);
      pointer-events: none;
    }

    .tier-badge {
      position: absolute;
      top: 0.5rem; right: 0.5rem;
      font-family: 'Bebas Neue', sans-serif;
      font-size: 0.75rem;
      letter-spacing: 0.1em;
      padding: 0.15rem 0.5rem;
      border-radius: 2px;
    }

    .client-name {
      font-family: 'Bebas Neue', sans-serif;
      font-size: 1.4rem;
      letter-spacing: 0.05em;
      line-height: 1;
      margin-bottom: 0.2rem;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .tier-label {
      font-size: 0.78rem;
      letter-spacing: 0.18em;
      text-transform: uppercase;
      font-weight: 700;
      margin-bottom: 0.75rem;
    }

    .tier-desc {
      font-size: 0.82rem;
      color: var(--muted);
      margin-bottom: 1rem;
      font-style: italic;
    }

    .block-count {
      display: flex;
      align-items: baseline;
      gap: 0.4rem;
    }

    .block-num {
      font-family: 'Bebas Neue', sans-serif;
      font-size: 2.2rem;
      line-height: 1;
    }

    .block-lbl {
      font-size: 0.78rem;
      letter-spacing: 0.15em;
      color: var(--muted);
      text-transform: uppercase;
    }

    .block-bar {
      height: 3px;
      background: var(--border);
      margin-top: 0.75rem;
      border-radius: 2px;
      overflow: hidden;
    }

    .block-bar-fill {
      height: 100%;
      border-radius: 2px;
      transition: width 0.8s ease;
    }

    .empty-state {
      text-align: center;
      padding: 5rem 2rem;
      color: var(--muted);
      grid-column: 1 / -1;
    }

    .empty-state h2 {
      font-family: 'Bebas Neue', sans-serif;
      font-size: 2rem;
      letter-spacing: 0.1em;
      margin-bottom: 0.5rem;
      color: var(--t4);
    }

    .last-updated {
      font-size: 0.75rem;
      color: var(--muted);
      letter-spacing: 0.1em;
    }
  </style>
</head>
<body>

<header>
  <h1>Block Battle Rankings</h1>
  <div class="subtitle">AdGuard Home — Network Threat Index</div>
</header>

<div class="controls">
  <span><span class="status-dot" id="statusDot"></span>&nbsp; <span id="statusText">Connecting...</span></span>
  <span class="last-updated" id="lastUpdated"></span>
  <button class="refresh-btn" onclick="loadClients()">Refresh</button>
</div>

<div class="tier-legend">
  <span class="tier-pill" style="color:var(--t1);border-color:var(--t1)">T1 Innocent 0-500</span>
  <span class="tier-pill" style="color:var(--t2);border-color:var(--t2)">T2 Rookie 51-2000</span>
  <span class="tier-pill" style="color:var(--t3);border-color:var(--t3)">T3 Protagonist 2001-5000</span>
  <span class="tier-pill" style="color:var(--t4);border-color:var(--t4)">T4 Fighter 5001-9000</span>
  <span class="tier-pill" style="color:var(--t5);border-color:var(--t5)">T5 Final Boss Over 9000!</span>
</div>

<div class="grid" id="clientGrid"></div>

<script>
  const TIER_COLORS = { 1: '#4ade80', 2: '#facc15', 3: '#fb923c', 4: '#f87171', 5: '#c084fc' };

  // Placeholder SVG characters per tier
  function charSVG(tier, color) {
    const svgs = {
      1: `<svg viewBox="0 0 100 120" fill="none" xmlns="http://www.w3.org/2000/svg">
        <circle cx="50" cy="35" r="22" stroke="${color}" stroke-width="2" fill="none"/>
        <circle cx="42" cy="31" r="3" fill="${color}" opacity="0.7"/>
        <circle cx="58" cy="31" r="3" fill="${color}" opacity="0.7"/>
        <path d="M42 44 Q50 50 58 44" stroke="${color}" stroke-width="2" stroke-linecap="round" fill="none"/>
        <line x1="50" y1="57" x2="50" y2="90" stroke="${color}" stroke-width="2"/>
        <line x1="50" y1="70" x2="32" y2="85" stroke="${color}" stroke-width="2"/>
        <line x1="50" y1="70" x2="68" y2="85" stroke="${color}" stroke-width="2"/>
        <line x1="50" y1="90" x2="38" y2="115" stroke="${color}" stroke-width="2"/>
        <line x1="50" y1="90" x2="62" y2="115" stroke="${color}" stroke-width="2"/>
        <text x="50" y="10" text-anchor="middle" fill="${color}" font-size="8" opacity="0.5" font-family="monospace">T1</text>
      </svg>`,

      2: `<svg viewBox="0 0 100 120" fill="none" xmlns="http://www.w3.org/2000/svg">
        <circle cx="50" cy="32" r="20" stroke="${color}" stroke-width="2" fill="none"/>
        <circle cx="43" cy="29" r="3.5" fill="${color}"/>
        <circle cx="57" cy="29" r="3.5" fill="${color}"/>
        <path d="M43 41 Q50 46 57 41" stroke="${color}" stroke-width="2" stroke-linecap="round" fill="none"/>
        <path d="M30 25 L36 22" stroke="${color}" stroke-width="2" stroke-linecap="round"/>
        <path d="M70 25 L64 22" stroke="${color}" stroke-width="2" stroke-linecap="round"/>
        <rect x="38" y="52" width="24" height="30" rx="2" stroke="${color}" stroke-width="2" fill="none"/>
        <line x1="38" y1="67" x2="20" y2="78" stroke="${color}" stroke-width="2"/>
        <line x1="62" y1="67" x2="80" y2="78" stroke="${color}" stroke-width="2"/>
        <line x1="44" y1="82" x2="38" y2="112" stroke="${color}" stroke-width="2"/>
        <line x1="56" y1="82" x2="62" y2="112" stroke="${color}" stroke-width="2"/>
        <text x="50" y="10" text-anchor="middle" fill="${color}" font-size="8" opacity="0.5" font-family="monospace">T2</text>
      </svg>`,

      3: `<svg viewBox="0 0 100 120" fill="none" xmlns="http://www.w3.org/2000/svg">
        <circle cx="50" cy="30" r="20" stroke="${color}" stroke-width="2.5" fill="none"/>
        <path d="M40 27 L47 30" stroke="${color}" stroke-width="2.5" stroke-linecap="round"/>
        <path d="M60 27 L53 30" stroke="${color}" stroke-width="2.5" stroke-linecap="round"/>
        <circle cx="44" cy="30" r="2.5" fill="${color}"/>
        <circle cx="56" cy="30" r="2.5" fill="${color}"/>
        <path d="M44 40 Q50 44 56 40" stroke="${color}" stroke-width="2" stroke-linecap="round" fill="none"/>
        <path d="M28 22 C24 18 22 14 28 12" stroke="${color}" stroke-width="2" fill="none"/>
        <path d="M72 22 C76 18 78 14 72 12" stroke="${color}" stroke-width="2" fill="none"/>
        <rect x="36" y="50" width="28" height="32" rx="2" stroke="${color}" stroke-width="2" fill="none"/>
        <line x1="36" y1="66" x2="16" y2="60" stroke="${color}" stroke-width="2.5"/>
        <line x1="64" y1="66" x2="84" y2="60" stroke="${color}" stroke-width="2.5"/>
        <line x1="43" y1="82" x2="36" y2="112" stroke="${color}" stroke-width="2"/>
        <line x1="57" y1="82" x2="64" y2="112" stroke="${color}" stroke-width="2"/>
        <circle cx="50" cy="30" r="20" stroke="${color}" stroke-width="1" opacity="0.2" fill="none" transform="scale(1.3) translate(-12,-8)"/>
        <text x="50" y="10" text-anchor="middle" fill="${color}" font-size="8" opacity="0.6" font-family="monospace">T3</text>
      </svg>`,

      4: `<svg viewBox="0 0 100 120" fill="none" xmlns="http://www.w3.org/2000/svg">
        <circle cx="50" cy="28" r="20" stroke="${color}" stroke-width="3" fill="rgba(248,113,113,0.05)"/>
        <path d="M38 24 L44 28" stroke="${color}" stroke-width="3" stroke-linecap="round"/>
        <path d="M62 24 L56 28" stroke="${color}" stroke-width="3" stroke-linecap="round"/>
        <circle cx="44" cy="28" r="3" fill="${color}"/>
        <circle cx="56" cy="28" r="3" fill="${color}"/>
        <circle cx="44" cy="28" r="1.5" fill="white" opacity="0.8"/>
        <circle cx="56" cy="28" r="1.5" fill="white" opacity="0.8"/>
        <path d="M44 38 Q50 41 56 38" stroke="${color}" stroke-width="2" stroke-linecap="round" fill="none"/>
        <path d="M26 16 C20 10 18 4 26 2 C30 1 32 6 28 8" stroke="${color}" stroke-width="2" fill="none"/>
        <path d="M74 16 C80 10 82 4 74 2 C70 1 68 6 72 8" stroke="${color}" stroke-width="2" fill="none"/>
        <path d="M36 44 L36 78 L64 78 L64 44 Z" stroke="${color}" stroke-width="2" fill="none"/>
        <path d="M36 56 L20 48" stroke="${color}" stroke-width="3" stroke-linecap="round"/>
        <path d="M64 56 L80 48" stroke="${color}" stroke-width="3" stroke-linecap="round"/>
        <line x1="43" y1="78" x2="36" y2="112" stroke="${color}" stroke-width="2.5"/>
        <line x1="57" y1="78" x2="64" y2="112" stroke="${color}" stroke-width="2.5"/>
        <path d="M36 60 L20 68" stroke="${color}" stroke-width="2" stroke-dasharray="3 2"/>
        <path d="M64 60 L80 68" stroke="${color}" stroke-width="2" stroke-dasharray="3 2"/>
        <text x="50" y="10" text-anchor="middle" fill="${color}" font-size="8" opacity="0.7" font-family="monospace">T4</text>
      </svg>`,

      5: `<svg viewBox="0 0 100 120" fill="none" xmlns="http://www.w3.org/2000/svg">
        <circle cx="50" cy="28" r="20" stroke="${color}" stroke-width="3" fill="rgba(192,132,252,0.08)"/>
        <circle cx="50" cy="28" r="26" stroke="${color}" stroke-width="1" opacity="0.25" fill="none"/>
        <circle cx="50" cy="28" r="32" stroke="${color}" stroke-width="0.5" opacity="0.12" fill="none"/>
        <path d="M38 22 L44 27" stroke="${color}" stroke-width="3.5" stroke-linecap="round"/>
        <path d="M62 22 L56 27" stroke="${color}" stroke-width="3.5" stroke-linecap="round"/>
        <circle cx="44" cy="27" r="4" fill="${color}"/>
        <circle cx="56" cy="27" r="4" fill="${color}"/>
        <circle cx="44" cy="27" r="2" fill="white" opacity="0.9"/>
        <circle cx="56" cy="27" r="2" fill="white" opacity="0.9"/>
        <path d="M44 38 Q50 43 56 38" stroke="${color}" stroke-width="2.5" stroke-linecap="round" fill="none"/>
        <path d="M24 14 C14 6 10 -2 22 0 C18 4 22 10 26 10" stroke="${color}" stroke-width="2.5" fill="none"/>
        <path d="M76 14 C86 6 90 -2 78 0 C82 4 78 10 74 10" stroke="${color}" stroke-width="2.5" fill="none"/>
        <path d="M30 18 C26 12 24 8 28 6" stroke="${color}" stroke-width="1.5" fill="none" opacity="0.6"/>
        <path d="M70 18 C74 12 76 8 72 6" stroke="${color}" stroke-width="1.5" fill="none" opacity="0.6"/>
        <path d="M34 44 L34 80 L66 80 L66 44 Z" stroke="${color}" stroke-width="2.5" fill="rgba(192,132,252,0.06)"/>
        <path d="M34 55 L14 44" stroke="${color}" stroke-width="3.5" stroke-linecap="round"/>
        <path d="M66 55 L86 44" stroke="${color}" stroke-width="3.5" stroke-linecap="round"/>
        <path d="M34 65 L14 56" stroke="${color}" stroke-width="2" opacity="0.5"/>
        <path d="M66 65 L86 56" stroke="${color}" stroke-width="2" opacity="0.5"/>
        <line x1="42" y1="80" x2="34" y2="115" stroke="${color}" stroke-width="3"/>
        <line x1="58" y1="80" x2="66" y2="115" stroke="${color}" stroke-width="3"/>
        <line x1="42" y1="97" x2="30" y2="110" stroke="${color}" stroke-width="2" opacity="0.6"/>
        <line x1="58" y1="97" x2="70" y2="110" stroke="${color}" stroke-width="2" opacity="0.6"/>
        <text x="50" y="10" text-anchor="middle" fill="${color}" font-size="8" opacity="0.9" font-family="monospace" font-weight="bold">T5</text>
      </svg>`
    };
    return svgs[tier] || svgs[1];
  }

  let maxBlocks = 1;

const TIER_CHARS = {
    1: ['tier1_1.png', 'tier1_2.png', 'tier1_3.png', 'tier1_4.png', 'tier1_5.png'],
    2: ['tier2_1.png', 'tier2_2.png', 'tier2_3.png', 'tier2_4.png', 'tier2_5.png'],
    3: ['tier3_1.png', 'tier3_2.png', 'tier3_3.png', 'tier3_4.png', 'tier3_5.png'],
    4: ['tier4_1.png', 'tier4_2.png', 'tier4_3.png', 'tier4_4.png', 'tier4_5.png'],
    5: ['tier5_1.png', 'tier5_2.png', 'tier5_3.png', 'tier5_4.png', 'tier5_5.png']
};

function randomChar(tier) {
    const options = TIER_CHARS[tier] || ['tier1_1.png'];
    const pick = options[Math.floor(Math.random() * options.length)];
    return `/static/characters/${pick}`;
}

  async function loadClients() {
    const dot = document.getElementById('statusDot');
    const statusText = document.getElementById('statusText');
    const grid = document.getElementById('clientGrid');

    statusText.textContent = 'Fetching data...';

    try {
      const resp = await fetch('/api/clients');
      const data = await resp.json();

      if (data.error) {
        dot.className = 'status-dot error';
        statusText.textContent = data.error;
        grid.innerHTML = `<div class="empty-state"><h2>Connection Failed</h2><p>${data.error}</p></div>`;
        return;
      }

      dot.className = 'status-dot';
      statusText.textContent = `${data.clients.length} clients tracked`;
      document.getElementById('lastUpdated').textContent =
        'Updated ' + new Date().toLocaleTimeString();

      if (data.clients.length === 0) {
        grid.innerHTML = `<div class="empty-state"><h2>No Block Data Yet</h2><p>AdGuard hasn't recorded any blocked clients in this period.</p></div>`;
        return;
      }

      maxBlocks = Math.max(...data.clients.map(c => c.blocks), 1);

      grid.innerHTML = data.clients.map((client, i) => {
        const color = TIER_COLORS[client.tier];
        const pct = Math.round((client.blocks / maxBlocks) * 100);
        const delay = i * 0.06;
        return `
          <div class="card" style="animation-delay:${delay}s">
            <div class="card-accent" style="background:${color}"></div>
            <div class="card-body">
              <div class="card-rank"># ${i + 1}</div>
              <div class="char-frame" style="box-shadow: inset 0 0 40px ${color}18">
               <img src="${randomChar(client.tier)}" alt="${client.label}" style="width:75%;height:75%;object-fit:contain"/> 
                <div class="tier-badge" style="background:${color}22;color:${color};border:1px solid ${color}44">
                  TIER ${client.tier}
                </div>
              </div>
              <div class="client-name">${client.name}</div>
              <div class="tier-label" style="color:${color}">${client.label}</div>
              <div class="tier-desc">${client.desc}</div>
              <div class="block-count">
                <span class="block-num" style="color:${color}">${client.blocks.toLocaleString()}</span>
                <span class="block-lbl">blocks</span>
              </div>
              <div class="block-bar">
                <div class="block-bar-fill" style="width:${pct}%;background:${color}"></div>
              </div>
            </div>
          </div>`;
      }).join('');

    } catch (err) {
      dot.className = 'status-dot error';
      statusText.textContent = 'Fetch error';
      grid.innerHTML = `<div class="empty-state"><h2>Error</h2><p>${err.message}</p></div>`;
    }
  }

  loadClients();
  setInterval(loadClients, 60000);
</script>
</body>
</html>
