* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Dynamic Background Gradient System */
body {
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  /* Remove the static background gradient and just set a fallback color in case JS fails */
  background-color: #1e3c72;
  min-height: 100vh;
  color: #333;
  transition: background 0.5s ease;
}

/* Dynamic Background Animation */
@keyframes gradientAnimation {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

/* Animated background for dynamic effect */
body.animated-bg {
  animation: gradientAnimation 10s ease infinite;
}

@keyframes gradientAnimation {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

/* Connection status classes for background states */
body.mqtt-connected {
  /* Blue gradient for connected state */
}

body.mqtt-reconnecting {
  /* Yellow gradient for reconnecting state */
}

body.mqtt-disconnected {
  /* Red gradient for disconnected state */
}

/* Connected state styling - this will be applied by default via JS */
body.connected {
  background: linear-gradient(to bottom, hsla(220, 100%, 40%, 0.8), hsla(240, 100%, 50%, 0.8)),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 100 100'%3E%3Cfilter id='noise' x='0' y='0'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.65' numOctaves='3' stitchTiles='stitch'/%3E%3CfeBlend mode='screen'/%3E%3C/filter%3E%3Crect width='100' height='100' filter='url(%23noise)' opacity='0.1'/%3E%3C/svg%3E");
  background-size: cover;
  background-attachment: fixed;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

/* Header */
.header {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header h1 {
  color: #4a5568;
  font-size: 2rem;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
}

/* Login/Register Forms */
.login-container,
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
}

.login-form,
.register-form {
  background: rgba(255, 255, 255, 0.95);
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-form h2,
.register-form h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #4a5568;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
  font-weight: 600;
  color: #4a5568;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 12px;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 16px;
  transition: border-color 0.3s;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: #667eea;
}

.form-group small {
  display: block;
  margin-top: 5px;
  color: #718096;
  font-size: 12px;
}

/* Current device info in rename modal */
.current-device-info {
  background: #f7fafc;
  padding: 10px;
  border-radius: 6px;
  border-left: 4px solid #667eea;
}

.current-device-info span {
  display: block;
  font-weight: 600;
  color: #4a5568;
}

.current-device-info small {
  color: #718096;
  font-size: 12px;
}

/* User info display */
.user-info {
  background: #f7fafc;
  padding: 10px;
  border-radius: 6px;
  border-left: 4px solid #48bb78;
}

.user-info span {
  display: block;
  font-weight: 600;
  color: #4a5568;
}

.user-info small {
  color: #718096;
  font-size: 12px;
}

/* Permissions section */
.permissions-section {
  margin: 20px 0;
  padding: 15px;
  background: #f7fafc;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
}

.permissions-section h4 {
  margin-bottom: 15px;
  color: #4a5568;
  font-size: 16px;
}

.permissions-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 10px;
}

.permission-item {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: background-color 0.2s;
}

.permission-item:hover {
  background: #edf2f7;
}

.permission-item input[type="checkbox"] {
  width: auto;
  margin: 0;
}

.permission-item span {
  font-size: 14px;
  color: #4a5568;
}

/* Buttons */
.btn {
  padding: 12px 24px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
}

.btn-sm {
  padding: 6px 12px;
  font-size: 14px;
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
  background: #e2e8f0;
  color: #4a5568;
}

.btn-secondary:hover {
  background: #cbd5e0;
}

.btn-success {
  background: #48bb78;
  color: white;
}

.btn-warning {
  background: #ed8936;
  color: white;
}

.btn-danger {
  background: #f56565;
  color: white;
}

.btn-info {
  background: #4299e1;
  color: white;
}

/* Dashboard */
.dashboard {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

/* Navigation Tabs */
.nav-tabs {
  display: flex;
  gap: 10px;
  margin-bottom: 30px;
  border-bottom: 2px solid #e2e8f0;
}

.tab-btn {
  padding: 15px 25px;
  background: none;
  border: none;
  font-size: 16px;
  font-weight: 600;
  color: #718096;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  transition: all 0.3s;
}

.tab-btn.active {
  color: #667eea;
  border-bottom-color: #667eea;
}

.tab-btn:hover {
  color: #667eea;
}

/* Status Bar */
.status-bar {
  display: flex;
  gap: 30px;
  margin-bottom: 30px;
  padding: 15px;
  background: #f7fafc;
  border-radius: 10px;
}

.status-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-item i {
  font-size: 18px;
}

.status-item i.fa-wifi {
  color: #48bb78;
}

.status-item i.fa-wifi.offline {
  color: #f56565;
}

/* Section Headers */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 25px;
}

.section-header h2 {
  color: #4a5568;
  flex: 1;
}

/* Grids */
.coordinators-grid,
.devices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

/* Cards */
.coordinator-card,
.device-card {
  background: #f7fafc;
  border-radius: 12px;
  padding: 20px;
  border-left: 4px solid #667eea;
  transition: transform 0.3s, box-shadow 0.3s;
}

.coordinator-card:hover,
.device-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.card-title {
  font-size: 18px;
  font-weight: 600;
  color: #4a5568;
}

.status-badge {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
}

.status-online {
  background: #c6f6d5;
  color: #22543d;
}

.status-offline {
  background: #fed7d7;
  color: #742a2a;
}

.card-info {
  margin-bottom: 15px;
}

.card-info p {
  margin-bottom: 5px;
  color: #718096;
}

.card-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.card-actions .btn {
  padding: 8px 16px;
  font-size: 14px;
}

/* Device Types */
.device-card.door_sensor {
  border-left-color: #ed8936;
}

.device-card.pir_sensor {
  border-left-color: #48bb78;
}

.device-card.temperature_sensor {
  border-left-color: #4299e1;
}

.device-card.smart_plug {
  border-left-color: #9f7aea;
}

/* Events List */
.events-list {
  max-height: 600px;
  overflow-y: auto;
}

.event-item {
  background: #f7fafc;
  border-radius: 8px;
  padding: 15px;
  margin-bottom: 10px;
  border-left: 4px solid #667eea;
}

.event-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.event-type {
  font-weight: 600;
  color: #4a5568;
}

.event-time {
  font-size: 12px;
  color: #718096;
}

.event-message {
  color: #718096;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
}

.modal-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: white;
  border-radius: 15px;
  padding: 0;
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.large-modal .modal-content {
  max-width: 800px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #e2e8f0;
}

.modal-header h3 {
  color: #4a5568;
}

.close-btn {
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: #718096;
}

.modal form,
.modal-body {
  padding: 20px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

/* User Management Styles */
.device-info-header {
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e2e8f0;
}

.device-info-header h4 {
  color: #4a5568;
  margin-bottom: 5px;
}

.device-info-header small {
  color: #718096;
}

.users-list {
  max-height: 400px;
  overflow-y: auto;
  margin-bottom: 20px;
}

.user-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px;
  margin-bottom: 10px;
  background: #f7fafc;
  border-radius: 8px;
  border: 1px solid #e2e8f0;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 15px;
  flex: 1;
}

.user-details {
  flex: 1;
}

.user-name {
  font-weight: 600;
  color: #4a5568;
  display: block;
}

.user-email {
  color: #718096;
  font-size: 12px;
}

.user-role {
  margin-right: 15px;
}

.role-badge {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
}

.role-owner {
  background: #fed7d7;
  color: #742a2a;
}

.role-admin {
  background: #fbb6ce;
  color: #702459;
}

.role-user {
  background: #bee3f8;
  color: #2a4365;
}

.role-viewer {
  background: #e2e8f0;
  color: #4a5568;
}

.user-permissions {
  flex: 2;
  margin-right: 15px;
}

.permissions-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-bottom: 5px;
}

.permission-tag {
  background: #c6f6d5;
  color: #22543d;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  font-weight: 500;
}

.granted-date {
  color: #718096;
  font-size: 11px;
}

.user-actions {
  display: flex;
  gap: 5px;
}

.add-user-section {
  text-align: center;
  padding: 20px;
  border-top: 1px solid #e2e8f0;
}

/* Notifications */
.notification-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 2000;
}

.notification {
  background: white;
  border-radius: 8px;
  padding: 15px 20px;
  margin-bottom: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border-left: 4px solid #48bb78;
  animation: slideIn 0.3s ease;
}

.notification.error {
  border-left-color: #f56565;
}

.notification.warning {
  border-left-color: #ed8936;
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* Responsive */
@media (max-width: 768px) {
  .container {
    padding: 10px;
  }

  .header-content {
    flex-direction: column;
    gap: 15px;
  }

  .nav-tabs {
    flex-wrap: wrap;
  }

  .status-bar {
    flex-direction: column;
    gap: 10px;
  }

  .section-header {
    flex-direction: column;
    gap: 15px;
    align-items: stretch;
  }

  .coordinators-grid,
  .devices-grid {
    grid-template-columns: 1fr;
  }

  .user-item {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .user-info {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }

  .permissions-grid {
    grid-template-columns: 1fr;
  }

  .card-actions {
    justify-content: center;
  }
}

/* Loading States */
.loading {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  color: #718096;
}

.loading i {
  animation: spin 1s linear infinite;
  margin-right: 10px;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* Empty States */
.empty-state {
  text-align: center;
  padding: 40px;
  color: #718096;
}

.empty-state i {
  font-size: 48px;
  margin-bottom: 15px;
  opacity: 0.5;
}

/* Device Control Styles */
.device-controls {
  padding: 20px;
}

.control-group {
  margin-bottom: 20px;
}

.control-group h4 {
  margin-bottom: 10px;
  color: #4a5568;
}

.control-buttons {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.device-info {
  background: #f7fafc;
  padding: 15px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.device-info h4 {
  margin-bottom: 10px;
  color: #4a5568;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 10px;
}

.info-item {
  display: flex;
  justify-content: space-between;
}

.info-label {
  font-weight: 600;
  color: #718096;
}

.info-value {
  color: #4a5568;
}

/* Error states */
.error {
  color: #f56565;
  text-align: center;
  padding: 20px;
  background: #fed7d7;
  border-radius: 8px;
  margin: 10px 0;
}

/* Device State Styles */
.state-open {
  color: #ed8936;
  font-weight: 600;
}

.state-closed {
  color: #48bb78;
  font-weight: 600;
}

.state-motion {
  color: #f56565;
  font-weight: 600;
  animation: pulse 1.5s infinite;
}

.state-no-motion {
  color: #48bb78;
  font-weight: 600;
}

.state-power-on {
  color: #48bb78;
  font-weight: 600;
}

.state-power-off {
  color: #718096;
  font-weight: 600;
}

.state-sensor {
  color: #4299e1;
  font-weight: 600;
}

.state-water-leak {
  color: #f56565;
  font-weight: 600;
  animation: pulse 1.5s infinite;
}

.state-water-ok {
  color: #48bb78;
  font-weight: 600;
}

.state-tamper {
  color: #f56565;
  font-weight: 600;
  animation: pulse 1.5s infinite;
}

.state-unknown {
  color: #718096;
  font-style: italic;
}

.state-good {
  color: #48bb78;
  font-weight: 600;
}

.state-fair {
  color: #ed8936;
  font-weight: 600;
}

.state-poor {
  color: #f56565;
  font-weight: 600;
}

/* Pulse animation for alerts */
@keyframes pulse {
  0% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
  100% {
    opacity: 1;
  }
}

/* Enhanced device card info section */
.card-info p {
  margin-bottom: 8px;
  color: #4a5568;
  font-size: 14px;
}

.card-info p strong {
  color: #2d3748;
}

.card-info small {
  color: #718096;
  font-size: 12px;
}

/* State-specific card border colors */
.device-card.door_sensor.state-open {
  border-left-color: #ed8936;
}

.device-card.door_sensor.state-closed {
  border-left-color: #48bb78;
}

.device-card.pir_sensor.state-motion {
  border-left-color: #f56565;
}

.device-card.smart_plug.state-power-on {
  border-left-color: #48bb78;
}

.device-card.smart_plug.state-power-off {
  border-left-color: #718096;
}
