<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{AGENT_NAME}} — Login</title>
<script>
var t = localStorage.getItem('cliver-theme') || 'light';
document.documentElement.setAttribute('data-theme', t);
</script>
<style>
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:      #0d0d1a;
  --card:    #1a1a2e;
  --border:  #2a2a4a;
  --text:    #e0e0e0;
  --text-dim:#888;
  --accent:  #00d4aa;
  --red:     #ff4757;
  --font:    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
             Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans",
             "Helvetica Neue", Arial, sans-serif;
}

[data-theme="light"] {
  --bg:      #f4f5f7;
  --card:    #ffffff;
  --border:  #d8dce3;
  --text:    #1a1a2e;
  --text-dim:#6b7280;
  --accent:  #059669;
  --red:     #dc2626;
}

html, body {
  height: 100%;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px 36px 32px;
  width: 360px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.12);
  text-align: center;
}

.login-logo { margin-bottom: 8px; }

.login-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 2px;
}

.login-subtitle {
  font-size: 11px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 28px;
}

.login-field {
  text-align: left;
  margin-bottom: 16px;
}

.login-field label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 6px;
}

.login-field input {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  transition: border-color 0.15s;
}

.login-field input:focus {
  outline: none;
  border-color: var(--accent);
}

.login-btn {
  width: 100%;
  padding: 11px;
  font-size: 14px;
  font-weight: 600;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: opacity 0.15s;
  margin-top: 4px;
}

.login-btn:hover { opacity: 0.9; }
.login-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.login-error {
  background: rgba(255,71,87,0.1);
  border: 1px solid rgba(255,71,87,0.3);
  border-radius: 8px;
  color: var(--red);
  font-size: 13px;
  padding: 10px 12px;
  margin-bottom: 16px;
  display: {{ERROR_DISPLAY}};
}

.theme-toggle {
  position: fixed;
  top: 16px;
  right: 20px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 18px;
  transition: border-color 0.2s, transform 0.3s;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.theme-toggle:hover {
  border-color: var(--accent);
  transform: scale(1.1);
}
</style>
</head>
<body>

<button class="theme-toggle" id="themeToggle"></button>

<div class="login-card">
  <div class="login-logo">
    <svg width="48" height="48" viewBox="0 0 100 100" fill="none" xmlns="http://www.w3.org/2000/svg">
      <line x1="18" y1="22" x2="48" y2="36" stroke="var(--accent)" stroke-width="0.8" opacity="0.12"/>
      <line x1="18" y1="30" x2="60" y2="50" stroke="var(--accent)" stroke-width="0.8" opacity="0.1"/>
      <line x1="18" y1="70" x2="60" y2="50" stroke="var(--accent)" stroke-width="0.8" opacity="0.1"/>
      <line x1="18" y1="78" x2="48" y2="64" stroke="var(--accent)" stroke-width="0.8" opacity="0.12"/>
      <line x1="18" y1="18" x2="34" y2="28" stroke="var(--accent)" stroke-width="1.8" opacity="0.35"/>
      <line x1="34" y1="28" x2="48" y2="38" stroke="var(--accent)" stroke-width="2" opacity="0.5"/>
      <line x1="48" y1="38" x2="60" y2="50" stroke="var(--accent)" stroke-width="2.2" opacity="0.7"/>
      <line x1="60" y1="50" x2="48" y2="62" stroke="var(--accent)" stroke-width="2.2" opacity="0.7"/>
      <line x1="48" y1="62" x2="34" y2="72" stroke="var(--accent)" stroke-width="2" opacity="0.5"/>
      <line x1="34" y1="72" x2="18" y2="82" stroke="var(--accent)" stroke-width="1.8" opacity="0.35"/>
      <line x1="34" y1="28" x2="34" y2="72" stroke="var(--accent)" stroke-width="0.8" opacity="0.15"/>
      <line x1="48" y1="38" x2="48" y2="62" stroke="var(--accent)" stroke-width="1" opacity="0.2"/>
      <circle cx="18" cy="30" r="2" fill="var(--accent)" opacity="0.25"/>
      <circle cx="18" cy="70" r="2" fill="var(--accent)" opacity="0.25"/>
      <circle cx="18" cy="18" r="3.5" fill="var(--accent)" opacity="0.5"/>
      <circle cx="34" cy="28" r="4" fill="var(--accent)" opacity="0.65"/>
      <circle cx="48" cy="38" r="4.5" fill="var(--accent)" opacity="0.8"/>
      <circle cx="48" cy="62" r="4.5" fill="var(--accent)" opacity="0.8"/>
      <circle cx="34" cy="72" r="4" fill="var(--accent)" opacity="0.65"/>
      <circle cx="18" cy="82" r="3.5" fill="var(--accent)" opacity="0.5"/>
      <circle cx="60" cy="50" r="7" fill="var(--accent)"/>
      <circle cx="60" cy="50" r="3.5" fill="var(--card)"/>
      <circle cx="60" cy="50" r="1.5" fill="var(--accent)" opacity="0.5"/>
      <rect x="72" y="47.5" width="12" height="3.5" rx="1.5" fill="var(--accent)" opacity="0.8">
        <animate attributeName="opacity" values="0.8;0.15;0.8" dur="1.2s" repeatCount="indefinite"/>
      </rect>
    </svg>
  </div>
  <div class="login-title">{{AGENT_NAME}}</div>
  <div class="login-subtitle">Admin Portal</div>

  <div class="login-error" id="loginError">{{ERROR}}</div>

  <form id="loginForm" autocomplete="on">
    <div class="login-field">
      <label for="username">Username</label>
      <input type="text" id="username" name="username" autocomplete="username" autofocus required>
    </div>
    <div class="login-field">
      <label for="password">Password</label>
      <input type="password" id="password" name="password" autocomplete="current-password" required>
    </div>
    <button type="submit" class="login-btn" id="loginBtn">Sign In</button>
  </form>
</div>

<script>
(function() {
  var btn = document.getElementById('themeToggle');
  var stored = localStorage.getItem('cliver-theme');
  var theme = stored || 'light';
  function apply(t) {
    theme = t;
    document.documentElement.setAttribute('data-theme', t);
    btn.textContent = t === 'dark' ? '🌙' : '☀️';
    localStorage.setItem('cliver-theme', t);
  }
  apply(theme);
  btn.addEventListener('click', function(e) { e.stopPropagation(); apply(theme === 'dark' ? 'light' : 'dark'); });
})();

document.getElementById('loginForm').addEventListener('submit', function(e) {
  e.preventDefault();
  var btn = document.getElementById('loginBtn');
  var errEl = document.getElementById('loginError');
  var u = document.getElementById('username').value;
  var p = document.getElementById('password').value;

  btn.disabled = true;
  btn.textContent = 'Signing in...';
  errEl.style.display = 'none';

  fetch('/admin/api/login', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ username: u, password: p })
  }).then(function(resp) {
    return resp.json().then(function(data) { return { ok: resp.ok, data: data }; });
  }).then(function(res) {
    if (res.ok) {
      window.location.href = '/admin/gateway';
    } else {
      errEl.textContent = res.data.error || 'Invalid credentials';
      errEl.style.display = 'block';
      btn.disabled = false;
      btn.textContent = 'Sign In';
    }
  }).catch(function() {
    errEl.textContent = 'Connection error';
    errEl.style.display = 'block';
    btn.disabled = false;
    btn.textContent = 'Sign In';
  });
});
</script>

</body>
</html>
