:root {
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #eef2ff;
  --bg: #f6f7fb;
  --surface: #ffffff;
  --border: #e6e8ef;
  --text: #1a1d29;
  --text-muted: #6b7088;
  --text-soft: #9499ad;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --success: #16a34a;
  --success-soft: #f0fdf4;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.06), 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-lg: 0 10px 30px rgba(16, 24, 40, 0.12);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 60px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
  font-weight: 600;
}
.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.5px;
}
.brand-name { font-size: 16px; }

.nav { display: flex; align-items: center; gap: 6px; }
.nav a, .nav button {
  font: inherit;
  font-size: 14px;
  color: var(--text-muted);
  text-decoration: none;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  border: none;
  background: none;
  cursor: pointer;
}
.nav a:hover, .nav button:hover { background: var(--bg); color: var(--text); }
.nav a.active { color: var(--accent); background: var(--accent-soft); }
.nav .nav-email { color: var(--text-soft); font-size: 13px; padding: 7px 4px; }

/* ---------- Layout ---------- */
.view {
  flex: 1;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 32px 24px 48px;
}
.view.narrow { max-width: 440px; }

.loading {
  text-align: center;
  color: var(--text-muted);
  padding: 80px 0;
}

.page-title { font-size: 24px; font-weight: 700; margin: 0 0 4px; }
.page-sub { color: var(--text-muted); margin: 0 0 24px; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
  margin-bottom: 20px;
}
.card h2 { font-size: 17px; margin: 0 0 4px; }
.card h2 + .card-sub { margin-top: -2px; }
.card-sub { color: var(--text-muted); font-size: 14px; margin: 0 0 16px; }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}
.card-head h2 { margin: 0; }

/* ---------- Auth pages ---------- */
.auth-card { padding: 32px; }
.auth-card h1 { font-size: 22px; margin: 0 0 6px; }
.auth-card .page-sub { margin-bottom: 24px; }
.auth-switch { text-align: center; margin-top: 18px; color: var(--text-muted); font-size: 14px; }
.auth-switch a { color: var(--accent); text-decoration: none; font-weight: 500; }

.divider {
  display: flex;
  align-items: center;
  gap: 12px;
  color: var(--text-soft);
  font-size: 13px;
  margin: 20px 0;
}
.divider::before, .divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ---------- Forms ---------- */
.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  margin-bottom: 6px;
  color: var(--text);
}
.field input {
  width: 100%;
  font: inherit;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ---------- Buttons ---------- */
.btn {
  font: inherit;
  font-weight: 500;
  font-size: 14px;
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-secondary { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--bg); }
.btn-danger { background: var(--surface); color: var(--danger); border-color: var(--border); }
.btn-danger:hover:not(:disabled) { background: var(--danger-soft); border-color: #fecaca; }
.btn-ghost { background: none; color: var(--accent); border: none; padding: 6px 8px; }
.btn-ghost:hover:not(:disabled) { background: var(--accent-soft); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 10px; font-size: 13px; }

/* ---------- Balance / referral ---------- */
.balance-grid {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 20px;
}
@media (max-width: 720px) { .balance-grid { grid-template-columns: 1fr; } }

.balance-card {
  background: linear-gradient(135deg, var(--accent) 0%, #6d28d9 100%);
  color: #fff;
  border: none;
}
.balance-card .balance-label { font-size: 13px; opacity: 0.85; margin: 0; }
.balance-num { font-size: 44px; font-weight: 700; line-height: 1.1; margin: 4px 0 2px; }
.balance-card .balance-unit { font-size: 14px; opacity: 0.85; }
.balance-card .btn { margin-top: 18px; background: rgba(255,255,255,0.18); color:#fff; border-color: rgba(255,255,255,0.25); }
.balance-card .btn:hover { background: rgba(255,255,255,0.28); }

.referral-blurb { color: var(--text-muted); font-size: 14px; margin: 0 0 12px; }
.copy-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}
.copy-row input {
  flex: 1;
  font-family: var(--mono);
  font-size: 13px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
}

/* ---------- API keys table ---------- */
.key-list { display: flex; flex-direction: column; gap: 10px; }
.key-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
}
.key-meta { flex: 1; min-width: 0; }
.key-name { font-weight: 600; font-size: 14px; }
.key-prefix { font-family: var(--mono); font-size: 13px; color: var(--text-muted); }
.key-dates { font-size: 12px; color: var(--text-soft); margin-top: 2px; }
.empty-state { color: var(--text-muted); font-size: 14px; padding: 8px 0; }

.howto {
  background: var(--accent-soft);
  border: 1px solid #e0e7ff;
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  font-size: 13px;
  color: #3730a3;
  margin-top: 16px;
}
.howto strong { color: #312e81; }

/* ---------- Billing packs ---------- */
.pack-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 8px;
}
.pack {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  text-align: center;
}
.pack .pack-credits { font-size: 28px; font-weight: 700; }
.pack .pack-credits-label { color: var(--text-muted); font-size: 13px; margin-bottom: 14px; }
.pack .pack-price { font-size: 20px; font-weight: 600; }
.pack .pack-unit { color: var(--text-soft); font-size: 12px; margin: 4px 0 16px; }
.pack .btn { margin-top: auto; }

/* ---------- Tables (history) ---------- */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
  text-align: left;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-soft);
  font-weight: 600;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
}
.table td { padding: 11px 12px; border-bottom: 1px solid var(--border); }
.table tr:last-child td { border-bottom: none; }

.badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 2px 9px;
  border-radius: 999px;
}
.badge-paid { background: var(--success-soft); color: var(--success); }
.badge-pending { background: var(--warn-soft); color: var(--warn); }
.badge-failed { background: var(--danger-soft); color: var(--danger); }

/* ---------- Alerts ---------- */
.alert {
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-size: 14px;
  margin-bottom: 16px;
}
.alert-error { background: var(--danger-soft); color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: var(--success-soft); color: #166534; border: 1px solid #bbf7d0; }
.alert-info { background: var(--accent-soft); color: #3730a3; border: 1px solid #e0e7ff; }
.alert a { color: inherit; font-weight: 600; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(16, 24, 40, 0.5);
  display: grid;
  place-items: center;
  z-index: 100;
  padding: 20px;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 460px;
  padding: 24px;
}
.modal h3 { margin: 0 0 8px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.key-reveal {
  font-family: var(--mono);
  font-size: 13px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  word-break: break-all;
  margin: 12px 0;
}

/* ---------- Toasts ---------- */
.toasts {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 200;
}
.toast {
  background: var(--text);
  color: #fff;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.2s ease;
  max-width: 320px;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--success); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Footer ---------- */
.footer {
  text-align: center;
  color: var(--text-soft);
  font-size: 13px;
  padding: 24px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.muted { color: var(--text-muted); }
.spinner {
  width: 15px;
  height: 15px;
  border: 2px solid rgba(255,255,255,0.4);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}
.btn-secondary .spinner, .btn-danger .spinner { border-color: rgba(0,0,0,0.2); border-top-color: var(--text); }
@keyframes spin { to { transform: rotate(360deg); } }
