/* ==========================================================================
   🎨 PREMIUM GLASSMORPHISM DESIGN SYSTEM
   ========================================================================== */

:root {
  --font-family: 'Outfit', 'Plus Jakarta Sans', system-ui, -apple-system, sans-serif;
  
  /* Harmonious Color Palette */
  --bg-app: #060913;
  --panel-bg: rgba(15, 23, 42, 0.45);
  --panel-border: rgba(255, 255, 255, 0.06);
  --panel-glow: rgba(99, 102, 241, 0.03);
  
  --color-primary: #6366f1;         /* Sleek Indigo */
  --color-primary-glow: rgba(99, 102, 241, 0.35);
  --color-primary-hover: #4f46e5;
  
  --color-secondary: #334155;       /* Slate Grey */
  --color-secondary-hover: #475569;
  
  --color-success: #10b981;         /* Emerald Green */
  --color-success-bg: rgba(16, 185, 129, 0.1);
  --color-warning: #f59e0b;         /* Amber Yellow */
  --color-warning-bg: rgba(245, 158, 11, 0.1);
  --color-danger: #ef4444;          /* Crimson Red */
  --color-danger-bg: rgba(239, 68, 68, 0.1);
  
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-inverse: #ffffff;
  
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  
  --transition-smooth: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ==========================================================================
   🪐 GLOBAL RESET & BASE STYLES
   ========================================================================== */

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

body {
  background-color: var(--bg-app);
  color: var(--text-main);
  font-family: var(--font-family);
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
  line-height: 1.5;
}

/* 🌌 Glowing Ambient Spheres */
.glow-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  pointer-events: none;
  overflow: hidden;
}

.glow-sphere {
  position: absolute;
  border-radius: 50%;
  filter: blur(140px);
  opacity: 0.18;
  transition: var(--transition-smooth);
}

.glow-purple {
  width: 50vw;
  height: 50vw;
  top: -10vw;
  right: -5vw;
  background: radial-gradient(circle, #a855f7 0%, transparent 70%);
}

.glow-blue {
  width: 60vw;
  height: 60vw;
  bottom: -15vw;
  left: -10vw;
  background: radial-gradient(circle, #3b82f6 0%, transparent 70%);
}

/* ==========================================================================
   🧩 REUSABLE COMPONENTS
   ========================================================================== */

/* Glass Panel Effect */
.glass-panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.37);
  border-radius: var(--radius-md);
  padding: 24px;
  position: relative;
  overflow: hidden;
  transition: var(--transition-smooth);
}

.glass-panel::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-radius: inherit;
  background: radial-gradient(800px circle at var(--x, 0) var(--y, 0), rgba(255,255,255,0.03), transparent 40%);
  pointer-events: none;
}

.glass-panel:hover {
  border-color: rgba(255, 255, 255, 0.12);
  box-shadow: 0 12px 40px 0 rgba(0, 0, 0, 0.45);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  font-family: var(--font-family);
  font-size: 0.95rem;
  font-weight: 600;
  padding: 11px 20px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: var(--transition-smooth);
  position: relative;
  overflow: hidden;
}

.btn-primary {
  background: linear-gradient(135deg, var(--color-primary), #818cf8);
  color: var(--text-inverse);
  box-shadow: 0 4px 14px 0 var(--color-primary-glow);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px 0 rgba(99, 102, 241, 0.5);
}

.btn-primary:active {
  transform: translateY(0);
}

.btn-secondary {
  background: var(--color-secondary);
  color: var(--text-main);
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.btn-secondary:hover {
  background: var(--color-secondary-hover);
  color: #fff;
  border-color: rgba(255, 255, 255, 0.1);
}

.btn-block {
  width: 100%;
}

.btn-icon-only {
  padding: 10px;
  border-radius: var(--radius-sm);
}

.btn-icon-only svg {
  width: 18px;
  height: 18px;
}

.btn-icon {
  width: 16px;
  height: 16px;
}

/* Inputs & Form Groups */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.input-wrapper {
  position: relative;
  width: 100%;
}

input[type="text"],
input[type="password"],
input[type="number"] {
  width: 100%;
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  color: var(--text-main);
  font-family: var(--font-family);
  font-size: 0.95rem;
  outline: none;
  transition: var(--transition-smooth);
}

input:focus {
  border-color: var(--color-primary);
  background: rgba(15, 23, 42, 0.8);
  box-shadow: 0 0 12px 0 rgba(99, 102, 241, 0.2);
}

/* Custom Styled Select */
.custom-select-wrapper {
  position: relative;
  width: 100%;
}

.custom-select {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-sm);
  padding: 12px 36px 12px 16px;
  color: var(--text-main);
  font-family: var(--font-family);
  font-size: 0.95rem;
  outline: none;
  cursor: pointer;
  transition: var(--transition-smooth);
}

.custom-select:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 12px 0 rgba(99, 102, 241, 0.2);
}

.custom-select-wrapper::after {
  content: '';
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  width: 8px;
  height: 8px;
  border-right: 2px solid var(--text-muted);
  border-bottom: 2px solid var(--text-muted);
  transform: translateY(-70%) rotate(45deg);
  pointer-events: none;
  transition: var(--transition-smooth);
}

/* ==========================================================================
   🔒 AUTHORIZATION OVERLAY
   ========================================================================== */

.auth-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(3, 7, 18, 0.8);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition-smooth);
}

.auth-card {
  width: 100%;
  max-width: 440px;
  text-align: center;
  padding: 40px;
  border-radius: var(--radius-lg);
  border-color: rgba(99, 102, 241, 0.15);
  box-shadow: 0 20px 50px 0 rgba(0, 0, 0, 0.6);
}

.auth-icon-wrapper {
  width: 64px;
  height: 64px;
  background: rgba(99, 102, 241, 0.1);
  border: 1px solid rgba(99, 102, 241, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 24px;
  color: var(--color-primary);
  box-shadow: 0 0 20px 0 rgba(99, 102, 241, 0.15);
}

.auth-icon {
  width: 28px;
  height: 28px;
}

.auth-card h2 {
  font-size: 1.6rem;
  font-weight: 700;
  margin-bottom: 12px;
  background: linear-gradient(135deg, #fff 0%, #cbd5e1 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.auth-card p {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 28px;
  line-height: 1.6;
}

.auth-card code {
  background: rgba(255, 255, 255, 0.05);
  padding: 2px 6px;
  border-radius: 4px;
  color: #fff;
  font-family: monospace;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.auth-error-msg {
  color: var(--color-danger);
  font-size: 0.85rem;
  font-weight: 500;
  margin-top: 14px;
  min-height: 20px;
}

/* ==========================================================================
   🖥️ LAYOUT & HEADER
   ========================================================================== */

.app-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 40px 24px 80px;
  display: flex;
  flex-direction: column;
  gap: 32px;
  animation: fadeIn 0.6s ease-out;
}

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

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  padding-bottom: 24px;
}

.logo-wrapper {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo-icon {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, var(--color-primary), #818cf8);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.4rem;
  color: #fff;
  box-shadow: 0 4px 20px 0 var(--color-primary-glow);
}

.logo-text h1 {
  font-size: 1.4rem;
  font-weight: 700;
  line-height: 1.2;
}

.logo-text .subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 500;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.server-status-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: rgba(16, 185, 129, 0.08);
  border: 1px solid rgba(16, 185, 129, 0.2);
  padding: 6px 14px;
  border-radius: 30px;
  font-size: 0.8rem;
  font-weight: 600;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  box-shadow: 0 0 10px 0 currentColor;
}

.status-dot.green {
  background-color: var(--color-success);
  color: var(--color-success);
}

.status-dot.red {
  background-color: var(--color-danger);
  color: var(--color-danger);
}

/* ==========================================================================
   📊 STATISTICS SECTION
   ========================================================================== */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 28px;
}

.stat-icon-wrapper {
  width: 56px;
  height: 56px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.stat-icon {
  width: 24px;
  height: 24px;
}

.bg-purple {
  background: rgba(168, 85, 247, 0.1);
  color: #c084fc;
  border: 1px solid rgba(168, 85, 247, 0.2);
}

.bg-blue {
  background: rgba(59, 130, 246, 0.1);
  color: #60a5fa;
  border: 1px solid rgba(59, 130, 246, 0.2);
}

.bg-green {
  background: rgba(16, 185, 129, 0.1);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.2);
}

.stat-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}

.stat-value {
  font-size: 1.8rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.02em;
}

.stat-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* ==========================================================================
   ⚡ WORKSPACE
   ========================================================================== */

.main-workspace {
  display: flex;
  flex-direction: column;
  gap: 32px;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

.sec-icon {
  width: 20px;
  height: 20px;
  color: var(--color-primary);
}

.section-header h2 {
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

/* 🆕 CREATE KEY SECTION */
.form-row {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  align-items: flex-end;
}

.flex-2 { flex: 2 1 280px; }
.flex-1 { flex: 1 1 180px; }
.align-end { align-self: flex-end; }

/* 📋 KEYS TABLE */
.section-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.section-header-row .section-header {
  margin-bottom: 0;
}

.table-container {
  overflow-x: auto;
  border-radius: var(--radius-sm);
}

.keys-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

.keys-table th {
  padding: 14px 18px;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.05em;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.keys-table td {
  padding: 18px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
  vertical-align: middle;
}

.keys-table tr:last-child td {
  border-bottom: none;
}

.keys-table tbody tr {
  transition: var(--transition-smooth);
}

.keys-table tbody tr:hover {
  background: rgba(255, 255, 255, 0.02);
}

.table-loading {
  text-align: center;
  color: var(--text-muted);
  padding: 40px !important;
  font-size: 0.95rem;
}

/* Key label and creation date */
.key-owner {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.owner-name {
  font-weight: 600;
  color: #fff;
}

.owner-date {
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* Key element with copy button */
.key-api-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(15, 23, 42, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.05);
  padding: 8px 12px;
  border-radius: 6px;
  max-width: 250px;
}

.key-code {
  font-family: monospace;
  font-size: 0.85rem;
  color: #3b82f6;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn-copy {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px;
  border-radius: 4px;
  transition: var(--transition-smooth);
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-copy:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.08);
}

.btn-copy svg {
  width: 14px;
  height: 14px;
}

/* Token Progress Container */
.usage-progress-container {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 180px;
}

.usage-stats {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  font-weight: 500;
}

.usage-text {
  color: #fff;
  font-weight: 600;
}

.usage-limit {
  color: var(--text-muted);
}

.progress-bar-bg {
  width: 100%;
  height: 6px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  border-radius: 10px;
  transition: width 0.5s ease-out;
}

.progress-green {
  background: linear-gradient(90deg, #10b981, #34d399);
}

.progress-amber {
  background: linear-gradient(90deg, #f59e0b, #fbbf24);
}

.progress-red {
  background: linear-gradient(90deg, #ef4444, #f87171);
}

/* Status toggler switch */
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
}

.status-badge.active {
  background: var(--color-success-bg);
  color: #34d399;
}

.status-badge.blocked {
  background: var(--color-danger-bg);
  color: #f87171;
}

.status-badge.exhausted {
  background: var(--color-warning-bg);
  color: #fbbf24;
}

/* Action button icons in table row */
.action-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.btn-action {
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.05);
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-sm);
  transition: var(--transition-smooth);
}

.btn-action:hover {
  color: #fff;
  border-color: rgba(255, 255, 255, 0.12);
}

.btn-action.refill:hover {
  background: var(--color-success-bg);
  color: var(--color-success);
  border-color: rgba(16, 185, 129, 0.2);
}

.btn-action.toggle:hover {
  background: var(--color-warning-bg);
  color: var(--color-warning);
  border-color: rgba(245, 158, 11, 0.2);
}

.btn-action.delete:hover {
  background: var(--color-danger-bg);
  color: var(--color-danger);
  border-color: rgba(239, 68, 68, 0.2);
}

.btn-action svg {
  width: 14px;
  height: 14px;
  display: block;
}

.text-right {
  text-align: right;
}

/* ==========================================================================
   📝 LOGS SECTION
   ========================================================================== */

.logs-container {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 320px;
  overflow-y: auto;
  padding-right: 6px;
}

.log-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid rgba(255, 255, 255, 0.04);
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  transition: var(--transition-smooth);
}

.log-item:hover {
  background: rgba(255, 255, 255, 0.03);
  border-color: rgba(255, 255, 255, 0.06);
}

.log-meta {
  display: flex;
  align-items: center;
  gap: 12px;
}

.log-time {
  color: var(--text-muted);
  font-size: 0.75rem;
  width: 60px;
}

.log-name {
  font-weight: 600;
  color: #fff;
}

.log-model {
  background: rgba(99, 102, 241, 0.1);
  color: #818cf8;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 500;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.log-tokens {
  font-weight: 700;
  color: var(--color-success);
}

.log-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 24px;
  font-size: 0.9rem;
}

/* ==========================================================================
   ➕ REFILL BALANCES MODAL
   ========================================================================== */

.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(3, 7, 18, 0.75);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 1100;
  align-items: center;
  justify-content: center;
}

.modal.active {
  display: flex;
  animation: modalFadeIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalFadeIn {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

.modal-content {
  width: 100%;
  max-width: 480px;
  border-color: rgba(255, 255, 255, 0.08);
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
  padding: 32px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-header h2 {
  font-size: 1.3rem;
  font-weight: 700;
}

.modal-close-btn {
  background: transparent;
  border: none;
  font-size: 2rem;
  color: var(--text-muted);
  cursor: pointer;
  transition: var(--transition-smooth);
}

.modal-close-btn:hover {
  color: #fff;
}

.modal-desc {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin-bottom: 20px;
}

.modal-desc strong {
  color: #fff;
}

.modal-preset-buttons {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin: 14px 0 24px;
}

.preset-btn {
  padding: 8px 0;
  font-size: 0.8rem;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* ==========================================================================
   🍞 TOAST NOTIFICATIONS
   ========================================================================== */

.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toast {
  background: rgba(15, 23, 42, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #fff;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.5);
  font-size: 0.85rem;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 10px;
  backdrop-filter: blur(10px);
  animation: toastIn 0.3s cubic-bezier(0.16, 1, 0.3, 1), toastOut 0.3s cubic-bezier(0.16, 1, 0.3, 1) 3.7s forwards;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateY(12px) scale(0.95); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes toastOut {
  from { opacity: 1; transform: translateY(0) scale(1); }
  to { opacity: 0; transform: translateY(12px) scale(0.95); }
}

.toast.success { border-color: rgba(16, 185, 129, 0.3); }
.toast.success svg { color: var(--color-success); }
.toast.error { border-color: rgba(239, 68, 68, 0.3); }
.toast.error svg { color: var(--color-danger); }

/* ==========================================================================
   📱 RESPONSIVE ADJUSTMENTS
   ========================================================================== */

@media (max-width: 768px) {
  .app-container {
    padding: 24px 16px 40px;
  }
  
  .app-header {
    flex-direction: column;
    gap: 16px;
    align-items: flex-start;
  }
  
  .header-actions {
    width: 100%;
    justify-content: space-between;
  }

  .form-row {
    flex-direction: column;
    align-items: stretch;
  }
  
  .align-end {
    align-self: auto;
    margin-top: 10px;
  }
}
