/* ✅ CSS Variables */
:root {
  --primary: #007aff;
  --success: #34c759;
  --danger: #ff3b30;
  --warning: #ff9500;
  --bg: #f5f5f7;
  --bg-secondary: #e8e8ed;
  --text: #1d1d1f;
  --text-secondary: #86868b;
  --border: #d2d2d7;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

/* ============================================ */
/* HEADER & NAVIGATION */
/* ============================================ */

.header {
  background: white;
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header .container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 64px;
}

.logo {
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo:hover {
  color: #0051d5;
}

.nav {
  display: flex;
  gap: 24px;
  align-items: center;
}

.nav a {
  color: var(--text);
  text-decoration: none;
  font-weight: 500;
  transition: color 0.2s;
  padding: 8px 0;
}

.nav a:hover {
  color: var(--primary);
}

/* Mobile Menu */
.mobile-menu {
  background: white;
  border-bottom: 1px solid var(--border);
  display: none; /* За замовчуванням приховано */
}

.mobile-menu.active {
  display: block; /* Показується при додаванні класу active */
}

.mobile-menu .nav {
  flex-direction: column;
  padding: 20px;
  gap: 0;
}

.mobile-menu .nav a {
  padding: 12px 0;
  border-bottom: 1px solid var(--bg);
  width: 100%;
}

.mobile-menu .nav a:last-child {
  border-bottom: none;
}

/* На desktop завжди приховувати мобільне меню */
@media (min-width: 769px) {
  .mobile-menu {
    display: none !important;
  }
}

/* Responsive helpers */
.hide-mobile {
  display: flex !important;
}

.hide-desktop {
  display: none !important;
}

@media (max-width: 768px) {
  .hide-mobile {
    display: none !important;
  }
  
  .hide-desktop {
    display: block !important;
  }
}

/* ============================================ */
/* AUTH PAGES */
/* ============================================ */

.auth-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  background: white;
  padding: 40px;
  border-radius: var(--radius-lg);
  box-shadow: 0 4px 20px rgba(0,0,0,0.08);
  width: 100%;
  max-width: 400px;
}

/* ============================================ */
/* TYPOGRAPHY */
/* ============================================ */

h1 { font-size: 28px; margin-bottom: 20px; font-weight: 600; }
h2 { font-size: 24px; margin-bottom: 16px; font-weight: 600; }
h3 { font-size: 18px; margin-bottom: 12px; font-weight: 600; }

/* ============================================ */
/* FORMS */
/* ============================================ */

input, select, textarea {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
  margin-bottom: 12px;
  font-family: inherit;
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.1);
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: var(--text);
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 16px;
}

.form-input:focus {
  outline: none;
  border-color: var(--primary);
}

/* ============================================ */
/* BUTTONS */
/* ============================================ */

button, .btn {
  background: var(--primary);
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: var(--radius-sm);
  font-size: 16px;
  cursor: pointer;
  text-decoration: none;
  display: inline-block;
  transition: all 0.2s;
  font-weight: 500;
  text-align: center;
}

button:hover, .btn:hover { 
  background: #0051d5;
  transform: translateY(-1px);
}

button:active, .btn:active {
  transform: translateY(0);
}

button:disabled, .btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.btn-secondary {
  background: #fff;
  color: var(--primary);
  border: 1px solid var(--primary);
}

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

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

.btn-success:hover {
  background: #28a745;
}

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

.btn-danger:hover { 
  background: #cc0000; 
}

.btn-warning {
  background: var(--warning);
}

.btn-warning:hover {
  background: #e68a00;
}

.btn-delete { 
  background: var(--danger); 
  padding: 8px 16px; 
}

.btn-small, .btn-sm { 
  padding: 8px 16px; 
  font-size: 14px; 
}

.btn-block { 
  width: 100%; 
  display: block;
}

/* ============================================ */
/* ALERTS */
/* ============================================ */

.error, .success {
  padding: 12px;
  border-radius: var(--radius-sm);
  margin-bottom: 16px;
}

.error { 
  background: #ffebee; 
  color: #c62828; 
}

.success { 
  background: #e8f5e9; 
  color: #2e7d32; 
}

.alert {
  padding: 16px;
  border-radius: var(--radius-md);
  margin-bottom: 16px;
}

.alert-success { 
  background: #d4edda; 
  color: #155724; 
  border: 1px solid #c3e6cb; 
}

.alert-danger { 
  background: #f8d7da; 
  color: #721c24; 
  border: 1px solid #f5c6cb; 
}

.alert-warning { 
  background: #fff3cd; 
  color: #856404; 
  border: 1px solid #ffeaa7; 
}

.alert-info {
  background: #d1ecf1;
  color: #0c5460;
  border: 1px solid #bee5eb;
}

/* ============================================ */
/* BADGES */
/* ============================================ */

.badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
}

.badge.trial, .badge-warning { 
  background: #fff3cd; 
  color: #856404; 
}

.badge.active, .badge-success { 
  background: #d4edda; 
  color: #155724; 
}

.badge-danger { 
  background: #f8d7da; 
  color: #721c24; 
}

.badge-secondary { 
  background: var(--bg-secondary); 
  color: var(--text-secondary); 
}

.badge-primary {
  background: #cfe2ff;
  color: #084298;
}

/* ============================================ */
/* LAYOUT */
/* ============================================ */

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* ============================================ */
/* CARDS */
/* ============================================ */

.card {
  background: white;
  border-radius: var(--radius-lg);
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  margin-bottom: 20px;
  overflow: visible; /* ✅ Щоб badges не обрізались */
}

.card-header {
  padding: 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 18px;
  /* overflow видалено щоб badges були видні */
}

.card-body {
  padding: 20px;
}

.sub-card {
  background: white;
  padding: 24px;
  border-radius: var(--radius-lg);
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.sub-info { 
  margin: 16px 0; 
}

/* ============================================ */
/* DEVICES */
/* ============================================ */

.devices-section {
  background: white;
  padding: 24px;
  border-radius: var(--radius-lg);
  margin-bottom: 20px;
}

.device-form {
  display: flex;
  gap: 12px;
  margin-bottom: 20px;
}

.device-form input { 
  margin-bottom: 0; 
  flex: 1; 
}

.devices-list {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}

.device-card {
  border: 1px solid var(--border);
  padding: 16px;
  border-radius: var(--radius-md);
  transition: box-shadow 0.2s;
}

.device-card:hover {
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.device-ip {
  color: var(--text-secondary);
  font-size: 14px;
  margin: 8px 0;
}

.device-actions {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.device-actions .btn {
  padding: 8px 16px;
  font-size: 14px;
  flex: 1;
  min-width: 100px;
}

/* ============================================ */
/* CONFIG */
/* ============================================ */

.config-section {
  background: white;
  padding: 24px;
  border-radius: var(--radius-lg);
  margin-bottom: 20px;
}

.config-card {
  border: 1px solid var(--border);
  padding: 20px;
  border-radius: var(--radius-md);
}

.speed-info {
  color: var(--text-secondary);
  font-size: 14px;
  margin: 8px 0;
}

.config-actions {
  display: flex;
  gap: 12px;
  margin-top: 16px;
  flex-wrap: wrap;
}

.config-actions .btn {
  flex: 1;
  min-width: 150px;
  text-align: center;
}

/* ============================================ */
/* GRIDS */
/* ============================================ */

.grid {
  display: grid;
  gap: 20px;
}

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

.grid-3 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
  padding-top: 16px; /* ✅ Відступ для badges */
}

@media (min-width: 1024px) {
  .grid-3 {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .grid-3 {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* ============================================ */
/* STATS */
/* ============================================ */

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

.stat-card {
  background: white;
  padding: 24px;
  border-radius: var(--radius-md);
  text-align: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

/* ============================================ */
/* TABLES */
/* ============================================ */

.users-table {
  width: 100%;
  background: white;
  border-radius: var(--radius-md);
  overflow: hidden;
}

.users-table th,
.users-table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #f0f0f0;
}

.users-table th {
  background: var(--bg);
  font-weight: 600;
}

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

/* ============================================ */
/* MODAL */
/* ============================================ */

.modal {
  display: none;
  position: fixed;
  z-index: 9999;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.7);
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
}

.modal.active {
  display: flex !important;
}

.modal-content {
  background: white;
  padding: 0;
  border-radius: var(--radius-lg);
  width: 90%;
  max-width: 500px;
  position: relative;
  animation: modalFadeIn 0.3s ease;
  max-height: 90vh;
  overflow-y: auto;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: scale(0.9) translateY(-20px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

.modal-header {
  padding: 20px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 18px;
}

.modal-body {
  padding: 20px;
}

.modal-footer {
  padding: 20px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 12px;
  justify-content: flex-end;
}

.modal-footer .btn {
  flex: 1;
}

.close {
  position: absolute;
  right: 20px;
  top: 15px;
  font-size: 28px;
  cursor: pointer;
  color: var(--text-secondary);
  transition: color 0.2s;
  line-height: 1;
}

.close:hover {
  color: var(--text);
}

#qr-image {
  width: 100%;
  margin-top: 20px;
  border-radius: var(--radius-md);
}

/* ============================================ */
/* DANGER ZONE */
/* ============================================ */

.danger-zone {
  background: #fff5f5;
  padding: 20px;
  border-radius: var(--radius-md);
  border: 1px solid #ffcdd2;
}

/* ============================================ */
/* UTILITIES */
/* ============================================ */

.loading {
  width: 20px;
  height: 20px;
  border: 3px solid var(--bg-secondary);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 1s linear infinite;
  display: inline-block;
  vertical-align: middle;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.mb-3 { margin-bottom: 24px; }
.mt-3 { margin-top: 24px; }
.mb-2 { margin-bottom: 16px; }
.mt-2 { margin-top: 16px; }
.mb-1 { margin-bottom: 8px; }
.mt-1 { margin-top: 8px; }

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

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

/* ============================================ */
/* RESPONSIVE */
/* ============================================ */

@media (max-width: 768px) {
  .devices-list {
    grid-template-columns: 1fr;
  }
  
  .auth-card {
    padding: 24px;
  }
  
  .config-actions .btn {
    flex: 1 1 100%;
  }
  
  .grid-2,
  .grid-3 {
    grid-template-columns: 1fr;
  }
  
  .modal-content {
    width: 95%;
    max-height: 95vh;
  }

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

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

  h1 { font-size: 24px; }
  h2 { font-size: 20px; }
  h3 { font-size: 16px; }
}

@media (max-width: 480px) {
  .auth-card {
    padding: 20px;
  }

  .card-body,
  .card-header {
    padding: 16px;
  }
}
