:root {
  --bg-dark: #090d16;
  --bg-card: #111827;
  --bg-card-hover: #172033;
  --border-color: #1f293d;
  --border-color-light: #2d3b55;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #10b981;
  --success-hover: #059669;
  --warning: #f59e0b;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

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

/* Global Dark Scrollbars (fixes white scrollbars on Windows/browsers) */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 9999px;
}
::-webkit-scrollbar-thumb:hover {
  background: rgba(59, 130, 246, 0.5);
}
* {
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.18) transparent;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-dark);
  color: var(--text-main);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}

.app-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1.5rem 1rem;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 1.25rem;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 1.25rem;
  gap: 1rem;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo-icon {
  font-size: 2rem;
  line-height: 1;
}

.header h1 {
  font-size: 1.35rem;
  font-weight: 700;
  color: #fff;
  line-height: 1.2;
}

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

.header-status {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  padding: 0.25rem 0.65rem;
  border-radius: 9999px;
  font-size: 0.78rem;
  font-weight: 600;
  white-space: nowrap;
  line-height: 1.2;
}

.badge-success {
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.3);
}

.badge-danger {
  background: rgba(239, 68, 68, 0.15);
  color: #f87171;
  border: 1px solid rgba(239, 68, 68, 0.3);
}

.badge-warning {
  background: rgba(245, 158, 11, 0.15);
  color: #fbbf24;
  border: 1px solid rgba(245, 158, 11, 0.3);
}

.badge-secondary {
  background: rgba(255, 255, 255, 0.07);
  color: #cbd5e1;
  border: 1px solid rgba(255, 255, 255, 0.14);
}

.count-badge {
  background: var(--primary);
  color: white;
  padding: 0.1rem 0.45rem;
  border-radius: 10px;
  font-size: 0.72rem;
  font-weight: bold;
  margin-left: 0.35rem;
}

/* Navigation Tabs */
.nav-tabs {
  display: flex;
  gap: 0.4rem;
  overflow-x: auto;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 1.5rem;
  padding-bottom: 0.35rem;
  -webkit-overflow-scrolling: touch;
}

/* Custom Nav Scrollbar */
.nav-tabs::-webkit-scrollbar {
  height: 4px;
}
.nav-tabs::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}

.nav-btn {
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.55rem 0.9rem;
  cursor: pointer;
  border-radius: 8px;
  transition: all 0.2s ease;
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  user-select: none;
}

.nav-btn:hover {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.04);
}

.nav-btn.active {
  color: #60a5fa;
  background: rgba(59, 130, 246, 0.12);
  border-color: rgba(59, 130, 246, 0.35);
}

/* Tab Panels */
.tab-panel {
  display: none;
}

.tab-panel.active {
  display: block;
}

/* Stats Grid */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  transition: border-color 0.2s ease;
}

.stat-card:hover {
  border-color: var(--border-color-light);
}

.stat-icon {
  font-size: 1.8rem;
  background: rgba(255, 255, 255, 0.04);
  padding: 0.5rem 0.75rem;
  border-radius: 10px;
  line-height: 1;
}

.stat-val {
  font-size: 1.5rem;
  font-weight: 800;
  display: block;
  line-height: 1.1;
}

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

.text-success { color: var(--success); }
.text-warning { color: var(--warning); }
.text-primary { color: var(--primary); }
.text-danger  { color: var(--danger); }

/* Section Cards */
.section-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  padding: 1.25rem;
  margin-bottom: 1.5rem;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.1rem;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.card-header h3 {
  font-size: 1.1rem;
  font-weight: 700;
  color: #fff;
}

.caption {
  font-size: 0.82rem;
  color: var(--text-muted);
  display: block;
  margin-top: 0.15rem;
}

.link-btn {
  color: var(--primary);
  text-decoration: none;
  font-size: 0.85rem;
  font-weight: 600;
}
.link-btn:hover {
  text-decoration: underline;
}

/* Tables */
.table-responsive {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: 8px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 0.88rem;
}

.data-table th, .data-table td {
  padding: 0.75rem 0.85rem;
  border-bottom: 1px solid var(--border-color);
  vertical-align: middle;
}

.data-table th {
  color: var(--text-muted);
  font-weight: 600;
  background: rgba(255, 255, 255, 0.02);
  white-space: nowrap;
}

.data-table tr:hover {
  background: var(--bg-card-hover);
}

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

/* Client Table Specific Styling */
.client-ident {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.client-subid {
  font-size: 0.73rem;
  color: var(--text-muted);
  font-family: monospace;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.client-tg {
  font-size: 0.73rem;
  color: #94a3b8;
}

.server-chips-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  max-width: 250px;
}

.server-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.2rem 0.5rem;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  font-size: 0.75rem;
  font-weight: 600;
  color: #f1f5f9;
  white-space: nowrap;
  transition: all 0.15s ease;
  user-select: none;
}

.server-chip:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: var(--primary);
}

.expiry-cell {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  align-items: flex-start;
  white-space: nowrap;
}

.expiry-date {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-main);
}

.traffic-cell {
  white-space: nowrap;
  font-size: 0.82rem;
}

.traffic-used {
  font-weight: 600;
  color: #fff;
}

.traffic-sep {
  color: var(--text-muted);
  margin: 0 0.15rem;
}

.traffic-total {
  color: var(--text-muted);
}

.client-actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: nowrap;
  white-space: nowrap;
}

/* Buttons */
.btn {
  padding: 0.45rem 0.85rem;
  border-radius: 6px;
  font-weight: 600;
  font-size: 0.82rem;
  cursor: pointer;
  border: none;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  user-select: none;
  white-space: nowrap;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-secondary { background: #232d45; color: white; border: 1px solid rgba(255,255,255,0.06); }
.btn-secondary:hover { background: #323f60; }

.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: var(--success-hover); }

.btn-danger { background: var(--danger); color: white; }
.btn-danger:hover { background: var(--danger-hover); }

.btn-sm {
  padding: 0.28rem 0.55rem;
  font-size: 0.75rem;
}

.btn-icon {
  padding: 0.28rem 0.45rem;
  min-width: 28px;
}

/* Filter Buttons */
.filter-group {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.filter-btn {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-color);
  color: var(--text-muted);
  padding: 0.35rem 0.75rem;
  border-radius: 6px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.filter-btn:hover {
  color: var(--text-main);
  background: rgba(255, 255, 255, 0.08);
}

.filter-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

/* Servers Grid */
.servers-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1rem;
}

.server-card {
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  transition: border-color 0.2s ease;
}

.server-card:hover {
  border-color: var(--border-color-light);
}

.server-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.server-name {
  font-weight: 700;
  font-size: 0.95rem;
}

.server-metrics {
  display: flex;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-muted);
  background: rgba(0, 0, 0, 0.25);
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  border: 1px solid rgba(255,255,255,0.03);
}

/* Forms */
.settings-form {
  max-width: 650px;
  display: flex;
  flex-direction: column;
  gap: 1.15rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.form-group label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-main);
}

.form-input, .form-textarea {
  background: #090e1a;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  padding: 0.55rem 0.75rem;
  color: white;
  font-family: inherit;
  font-size: 0.88rem;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.form-input:focus, .form-textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.2);
}

.form-hint {
  font-size: 0.73rem;
  color: var(--text-muted);
}

/* Toggle */
.form-row {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.toggle-switch {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* Logs */
.logs-container {
  max-height: 400px;
  overflow-y: auto;
  font-family: monospace;
  font-size: 0.82rem;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.log-entry {
  padding: 0.5rem 0.75rem;
  background: rgba(255, 255, 255, 0.02);
  border-left: 3px solid var(--primary);
  border-radius: 4px;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
  align-items: center;
  justify-content: center;
  z-index: 999;
  padding: 1rem;
}

.modal.active {
  display: flex;
}

.modal-content {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  padding: 1.5rem;
  border-radius: 14px;
  width: 90%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.6);
}

.modal-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 0.4rem;
  flex-wrap: wrap;
}

/* Login Screen */
.login-overlay {
  position: fixed;
  top: 0; left: 0; width: 100%; height: 100%;
  background: var(--bg-dark);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}

.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 14px;
  padding: 2.25rem 2rem;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.5);
}

.login-header {
  text-align: center;
  margin-bottom: 1.75rem;
}

.login-logo {
  font-size: 2.5rem;
  display: inline-block;
  margin-bottom: 0.5rem;
}

.login-header h2 {
  font-size: 1.4rem;
  font-weight: 700;
  color: #fff;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 1.15rem;
}

.btn-block {
  width: 100%;
  justify-content: center;
  padding: 0.75rem;
  font-size: 0.95rem;
}

.alert-error {
  background: rgba(239, 68, 68, 0.15);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #f87171;
  padding: 0.6rem 0.8rem;
  border-radius: 6px;
  font-size: 0.85rem;
  text-align: center;
}

/* ==========================================================================
   MOBILE & RESPONSIVE ADAPTATIONS
   ========================================================================== */

@media (min-width: 992px) {
  .nav-tabs {
    flex-wrap: wrap;
    overflow-x: visible;
  }
}

@media (max-width: 768px) {
  .app-container {
    padding: 0.75rem 0.5rem;
  }

  .header {
    flex-direction: column;
    align-items: stretch;
    gap: 0.75rem;
    padding-bottom: 0.85rem;
    margin-bottom: 0.85rem;
  }

  .logo-section {
    justify-content: flex-start;
  }

  .header h1 {
    font-size: 1.2rem;
  }

  .header-status {
    justify-content: space-between;
    width: 100%;
  }

  .header-status .btn {
    padding: 0.4rem 0.65rem;
    font-size: 0.78rem;
  }

  .nav-tabs {
    gap: 0.3rem;
    padding-bottom: 0.4rem;
    margin-bottom: 1rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .nav-btn {
    padding: 0.45rem 0.75rem;
    font-size: 0.82rem;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.03);
    flex-shrink: 0;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.65rem;
    margin-bottom: 1rem;
  }

  .stat-card {
    padding: 0.85rem;
    gap: 0.65rem;
    border-radius: 10px;
  }

  .stat-icon {
    font-size: 1.5rem;
    padding: 0.35rem 0.5rem;
  }

  .stat-val {
    font-size: 1.25rem;
  }

  .stat-lbl {
    font-size: 0.75rem;
  }

  .section-card {
    padding: 1rem 0.75rem;
    border-radius: 10px;
    margin-bottom: 1rem;
  }

  .card-header {
    flex-direction: column;
    align-items: stretch;
    gap: 0.65rem;
  }

  .card-header > div {
    width: 100%;
  }

  #clientSearchInput,
  #clientServerFilter {
    max-width: 100% !important;
    width: 100% !important;
  }

  .servers-grid {
    grid-template-columns: 1fr;
  }

  .modal-content {
    width: 95%;
    padding: 1.25rem 1rem;
    border-radius: 12px;
  }

  .login-card {
    padding: 1.75rem 1.25rem;
  }
}

@media (max-width: 480px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }

  .modal-actions {
    flex-direction: column;
  }

  .modal-actions .btn {
    width: 100%;
  }
}
