<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>{% block title %}Admin{% endblock %} — Collnetwork WiFi</title>
  <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700;800&family=Space+Mono:wght@700&display=swap" rel="stylesheet">
  <style>
    :root {
      --purple-deep:#3b1f6e; --purple-mid:#6b3fa0; --purple-light:#9b6dce;
      --green-vivid:#22c55e; --green-mid:#16a34a;
      --red:#ef4444; --amber:#f59e0b; --blue:#3b82f6;
      --bg:#0f0a1e; --surface:#1a1030; --card:#231545;
      --border:rgba(155,109,206,0.2); --text:#e2d9f3; --muted:#9b8ab8;
      --sidebar-w:240px;
    }
    *,*::before,*::after{margin:0;padding:0;box-sizing:border-box;}
    body{font-family:'Nunito',sans-serif;background:var(--bg);color:var(--text);display:flex;min-height:100vh;font-size:14px;}
    .sidebar{width:var(--sidebar-w);background:var(--surface);border-right:1px solid var(--border);display:flex;flex-direction:column;position:fixed;top:0;left:0;bottom:0;z-index:100;}
    .sidebar-logo{padding:20px 24px;border-bottom:1px solid var(--border);display:flex;align-items:center;gap:10px;}
    .sidebar-logo .badge{width:36px;height:36px;border-radius:9px;flex-shrink:0;background:linear-gradient(135deg,var(--green-vivid),var(--purple-mid));display:flex;align-items:center;justify-content:center;font-size:9px;color:white;font-weight:700;overflow:hidden;}
    .sidebar-logo .badge img{width:100%;height:100%;object-fit:contain;padding:4px;}
    .sidebar-logo .name{font-size:13px;font-weight:800;color:white;line-height:1.3;}
    .sidebar-logo .sub{font-size:10px;color:var(--green-vivid);font-weight:700;}
    nav{flex:1;padding:16px 0;overflow-y:auto;}
    .nav-section{padding:6px 16px 4px;font-size:10px;font-weight:700;color:var(--muted);text-transform:uppercase;letter-spacing:1px;}
    .nav-item{display:flex;align-items:center;gap:10px;padding:10px 20px;color:var(--muted);text-decoration:none;font-weight:600;font-size:13px;border-left:3px solid transparent;transition:all 0.2s;}
    .nav-item:hover{color:white;background:rgba(155,109,206,0.1);}
    .nav-item.active{color:white;background:rgba(107,63,160,0.2);border-left-color:var(--green-vivid);}
    .nav-item .icon{font-size:16px;width:20px;text-align:center;}
    .sidebar-footer{padding:16px 20px;border-top:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;}
    .admin-badge{font-size:12px;color:var(--muted);}
    .admin-badge strong{color:var(--green-vivid);}
    .logout-btn{padding:6px 12px;border-radius:8px;background:rgba(239,68,68,0.15);border:1px solid rgba(239,68,68,0.3);color:#fca5a5;font-size:12px;font-weight:700;text-decoration:none;transition:all 0.2s;}
    .logout-btn:hover{background:rgba(239,68,68,0.25);color:white;}
    .main{margin-left:var(--sidebar-w);flex:1;display:flex;flex-direction:column;}
    .topbar{padding:16px 28px;border-bottom:1px solid var(--border);display:flex;align-items:center;justify-content:space-between;background:var(--surface);position:sticky;top:0;z-index:50;}
    .topbar h1{font-size:18px;font-weight:800;color:white;}
    .topbar .time{font-size:12px;color:var(--muted);font-family:'Space Mono',monospace;}
    .content{padding:28px;flex:1;}
    .card{background:var(--card);border:1px solid var(--border);border-radius:16px;padding:20px 24px;}
    .card-title{font-size:13px;font-weight:700;color:var(--muted);text-transform:uppercase;letter-spacing:0.8px;margin-bottom:16px;}
    .stats-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:16px;margin-bottom:24px;}
    .stat-card{background:var(--card);border:1px solid var(--border);border-radius:14px;padding:18px 20px;display:flex;align-items:center;gap:14px;}
    .stat-icon{width:44px;height:44px;border-radius:12px;display:flex;align-items:center;justify-content:center;font-size:20px;flex-shrink:0;}
    .stat-icon.purple{background:rgba(107,63,160,0.2);}
    .stat-icon.green{background:rgba(34,197,94,0.15);}
    .stat-icon.red{background:rgba(239,68,68,0.15);}
    .stat-icon.amber{background:rgba(245,158,11,0.15);}
    .stat-icon.blue{background:rgba(59,130,246,0.15);}
    .stat-val{font-size:26px;font-weight:900;color:white;line-height:1;}
    .stat-label{font-size:12px;color:var(--muted);margin-top:2px;}
    .table-wrap{overflow-x:auto;border-radius:14px;border:1px solid var(--border);}
    table{width:100%;border-collapse:collapse;}
    thead th{background:rgba(107,63,160,0.15);padding:12px 16px;font-size:11px;font-weight:700;text-transform:uppercase;letter-spacing:0.8px;color:var(--muted);text-align:left;white-space:nowrap;}
    tbody tr{border-top:1px solid var(--border);transition:background 0.15s;}
    tbody tr:hover{background:rgba(155,109,206,0.06);}
    td{padding:12px 16px;font-size:13px;color:var(--text);}
    td.mono{font-family:'Space Mono',monospace;font-size:11px;}
    .badge{display:inline-flex;align-items:center;gap:5px;padding:3px 10px;border-radius:20px;font-size:11px;font-weight:700;}
    .badge::before{content:'●';font-size:8px;}
    .badge.active{background:rgba(34,197,94,0.15);color:#4ade80;border:1px solid rgba(34,197,94,0.3);}
    .badge.expired{background:rgba(239,68,68,0.12);color:#fca5a5;border:1px solid rgba(239,68,68,0.25);}
    .badge.pending{background:rgba(245,158,11,0.12);color:#fcd34d;border:1px solid rgba(245,158,11,0.25);}
    .badge.failed{background:rgba(107,63,160,0.15);color:#c4b5fd;border:1px solid rgba(107,63,160,0.3);}
    .badge.online{background:rgba(34,197,94,0.15);color:#4ade80;border:1px solid rgba(34,197,94,0.3);}
    .badge.yes{background:rgba(34,197,94,0.15);color:#4ade80;border:1px solid rgba(34,197,94,0.3);}
    .badge.no{background:rgba(107,63,160,0.1);color:var(--muted);border:1px solid var(--border);}
    .btn{display:inline-flex;align-items:center;gap:6px;padding:7px 14px;border-radius:8px;border:none;font-family:'Nunito',sans-serif;font-size:12px;font-weight:700;cursor:pointer;text-decoration:none;transition:all 0.2s;}
    .btn-green{background:rgba(34,197,94,0.15);color:#4ade80;border:1px solid rgba(34,197,94,0.3);}
    .btn-green:hover{background:rgba(34,197,94,0.25);}
    .btn-red{background:rgba(239,68,68,0.12);color:#fca5a5;border:1px solid rgba(239,68,68,0.25);}
    .btn-red:hover{background:rgba(239,68,68,0.22);}
    .btn-purple{background:rgba(107,63,160,0.2);color:#c4b5fd;border:1px solid rgba(107,63,160,0.35);}
    .btn-purple:hover{background:rgba(107,63,160,0.3);}
    .btn-amber{background:rgba(245,158,11,0.12);color:#fcd34d;border:1px solid rgba(245,158,11,0.25);}
    .btn-amber:hover{background:rgba(245,158,11,0.22);}
    .flash{padding:12px 18px;border-radius:10px;margin-bottom:20px;font-size:13px;font-weight:700;}
    .flash.success{background:rgba(34,197,94,0.12);border:1px solid rgba(34,197,94,0.3);color:#4ade80;}
    .flash.error{background:rgba(239,68,68,0.1);border:1px solid rgba(239,68,68,0.3);color:#fca5a5;}
    .search-bar{display:flex;gap:10px;margin-bottom:20px;flex-wrap:wrap;align-items:center;}
    .search-bar input,.search-bar select{background:var(--card);border:1px solid var(--border);color:var(--text);border-radius:9px;padding:9px 14px;font-family:'Nunito',sans-serif;font-size:13px;outline:none;transition:border-color 0.2s;}
    .search-bar input:focus,.search-bar select:focus{border-color:var(--purple-light);}
    .search-bar input{flex:1;min-width:200px;}
    .pagination{display:flex;gap:6px;margin-top:20px;justify-content:center;flex-wrap:wrap;}
    .pagination a,.pagination span{padding:7px 13px;border-radius:8px;font-size:12px;font-weight:700;text-decoration:none;border:1px solid var(--border);}
    .pagination a{color:var(--muted);background:var(--card);}
    .pagination a:hover{color:white;border-color:var(--purple-light);}
    .pagination span{background:rgba(107,63,160,0.25);color:white;border-color:var(--purple-mid);}
    .modal-overlay{display:none;position:fixed;inset:0;z-index:999;background:rgba(0,0,0,0.6);backdrop-filter:blur(4px);align-items:center;justify-content:center;}
    .modal-overlay.open{display:flex;}
    .modal{background:var(--card);border:1px solid var(--border);border-radius:18px;padding:28px 32px;max-width:400px;width:90%;text-align:center;}
    .modal h3{font-size:18px;font-weight:800;color:white;margin-bottom:8px;}
    .modal p{color:var(--muted);font-size:14px;margin-bottom:24px;}
    .modal-btns{display:flex;gap:10px;justify-content:center;}
    .two-col{display:grid;grid-template-columns:1fr 1fr;gap:20px;}
    @media(max-width:900px){.two-col{grid-template-columns:1fr;}}
  </style>
</head>
<body>
  <aside class="sidebar">
    <div class="sidebar-logo">
      <div class="badge">
        <img src="/static/img/collnetwork_logo.png" alt="L" onerror="this.style.display='none'">
      </div>
      <div>
        <div class="name">Collnetwork</div>
        <div class="sub">ADMIN PANEL</div>
      </div>
    </div>
    <nav>
      <div class="nav-section">Overview</div>
      <a href="/admin/dashboard" class="nav-item {% if request.endpoint=='admin_dashboard' %}active{% endif %}">
        <span class="icon">📊</span> Dashboard
      </a>
      <div class="nav-section">Management</div>
      <a href="/admin/subscribers" class="nav-item {% if request.endpoint=='admin_subscribers' %}active{% endif %}">
        <span class="icon">👥</span> Subscribers
      </a>
      <a href="/admin/sessions" class="nav-item {% if request.endpoint=='admin_sessions' %}active{% endif %}">
        <span class="icon">📡</span> Live Sessions
      </a>
      <a href="/admin/payments" class="nav-item {% if request.endpoint=='admin_payments' %}active{% endif %}">
        <span class="icon">💳</span> Payments
      </a>
      <a href="/admin/settlements" class="nav-item {% if request.path == '/admin/settlements' %}active{% endif %}">
      <span class="icon">🏦</span> Settlements
    </a>

    <div class="nav-section">Sites</div>
    <a href="/admin/subscribers?site_id=2" class="nav-item {% if request.path == '/admin/subscribers?site_id=2' %}active{% endif %}">
        <span class="icon">🏥</span>
        <span class="nav-text">NDUTH Okolobiri</span>
    </a>
      <div class="nav-section">Tools</div>
      <a href="/admin/export/csv" class="nav-item">
        <span class="icon">📥</span> Export CSV
      </a>
      <a href="/register" target="_blank" class="nav-item">
        <span class="icon">🌐</span> View Portal
      </a>
    </nav>
    <div class="sidebar-footer">
      <div class="admin-badge">Signed in as<br><strong>{{ session.get('admin_user','admin') }}</strong></div>
      <a href="/admin/logout" class="logout-btn">Logout</a>
    </div>
  </aside>

  <div class="main">
    <div class="topbar">
      <h1>{% block page_title %}Dashboard{% endblock %}</h1>
      <div class="time" id="clock"></div>
    </div>
    <div class="content">
      {% with messages = get_flashed_messages(with_categories=true) %}
        {% for cat, msg in messages %}
          <div class="flash {{ cat }}">{{ msg }}</div>
        {% endfor %}
      {% endwith %}
      {% block content %}{% endblock %}
    </div>
  </div>

  <div class="modal-overlay" id="confirmModal">
    <div class="modal">
      <h3>⚠️ Confirm Action</h3>
      <p id="confirmMsg">Are you sure?</p>
      <input type="hidden" id="confirmFormId">
      <div class="modal-btns">
        <button class="btn btn-red" onclick="executeConfirmed()">Yes, Proceed</button>
        <button class="btn btn-purple" onclick="closeModal()">Cancel</button>
      </div>
    </div>
  </div>

  <script>
    function updateClock(){
      const n=new Date();
      document.getElementById('clock').textContent=
        n.toLocaleDateString('en-NG',{weekday:'short',day:'numeric',month:'short'})+
        ' · '+n.toLocaleTimeString('en-NG',{hour:'2-digit',minute:'2-digit',second:'2-digit'});
    }
    updateClock(); setInterval(updateClock,1000);
    function confirmAction(fid,msg){
      document.getElementById('confirmMsg').textContent=msg;
      document.getElementById('confirmFormId').value=fid;
      document.getElementById('confirmModal').classList.add('open');
    }
    function closeModal(){document.getElementById('confirmModal').classList.remove('open');}
    function executeConfirmed(){document.getElementById(document.getElementById('confirmFormId').value).submit();closeModal();}
  </script>
</body>
</html>
