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

:root {
  --bg-primary: rgba(255, 255, 255, 0.85);
  --bg-secondary: rgba(255, 255, 255, 0.9);
  --bg-card: rgba(255, 255, 255, 0.95);
  --text-primary: #1a1a2e;
  --text-secondary: #4a4a6a;
  --text-muted: #6b7280;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --border: rgba(0, 0, 0, 0.1);
  --shadow: rgba(0, 0, 0, 0.1);
  --header-bg: rgba(255, 255, 255, 0.75);
  --card-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

[data-theme="dark"] {
  --bg-primary: rgba(15, 15, 35, 0.85);
  --bg-secondary: rgba(25, 25, 55, 0.9);
  --bg-card: rgba(30, 30, 60, 0.95);
  --text-primary: #e5e7eb;
  --text-secondary: #9ca3af;
  --text-muted: #6b7280;
  --accent: #60a5fa;
  --accent-hover: #3b82f6;
  --border: rgba(255, 255, 255, 0.1);
  --shadow: rgba(0, 0, 0, 0.3);
  --header-bg: rgba(15, 15, 35, 0.8);
  --card-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  color: var(--text-primary);
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
}

.animated-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  overflow: hidden;
  background: linear-gradient(135deg, #0a1628 0%, #1e3a5f 50%, #0d2137 100%);
}

[data-theme="light"] .animated-background {
  background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 50%, #93c5fd 100%);
}

.animated-background::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    radial-gradient(circle at 15% 85%, rgba(59, 130, 246, 0.25) 0%, transparent 45%),
    radial-gradient(circle at 85% 15%, rgba(16, 185, 129, 0.2) 0%, transparent 45%),
    radial-gradient(circle at 50% 50%, rgba(139, 92, 246, 0.15) 0%, transparent 50%);
  animation: pulse-bg 6s ease-in-out infinite;
}

@keyframes pulse-bg {
  0%, 100% { opacity: 0.6; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.02); }
}

.grid-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-image: 
    linear-gradient(rgba(59, 130, 246, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(59, 130, 246, 0.03) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: grid-move 20s linear infinite;
}

[data-theme="light"] .grid-overlay {
  background-image: 
    linear-gradient(rgba(30, 58, 138, 0.05) 1px, transparent 1px),
    linear-gradient(90deg, rgba(30, 58, 138, 0.05) 1px, transparent 1px);
}

@keyframes grid-move {
  0% { transform: translate(0, 0); }
  100% { transform: translate(50px, 50px); }
}

.header {
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 0.75rem 1.5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
  border-bottom: 1px solid var(--border);
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.25), 0 4px 12px rgba(0, 0, 0, 0.15);
}

[data-theme="dark"] .header {
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5), 0 4px 12px rgba(0, 0, 0, 0.3);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.logo {
  font-size: 1.8rem;
  font-weight: 700;
  color: var(--text-primary);
  text-decoration: none;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  font-family: 'Barlow Condensed', sans-serif;
}

.mqtt-indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
  box-shadow: 0 0 8px currentColor;
  transition: all 0.3s ease;
}

.mqtt-indicator.online {
  background: #22c55e;
  box-shadow: 0 0 12px rgba(34, 197, 94, 0.6);
}

.mqtt-indicator.offline {
  background: #ef4444;
  box-shadow: 0 0 12px rgba(239, 68, 68, 0.6);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  position: relative;
}

.theme-toggle {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
  color: var(--text-primary);
}

.theme-toggle:hover {
  background: var(--accent);
  color: white;
  transform: rotate(15deg);
}

.theme-toggle .icon-sun {
  display: block;
}

.theme-toggle .icon-moon {
  display: none;
}

[data-theme="dark"] .theme-toggle .icon-sun {
  display: none;
}

[data-theme="dark"] .theme-toggle .icon-moon {
  display: block;
}

.hamburger {
  background: var(--bg-card);
  border: 1px solid var(--border);
  cursor: pointer;
  padding: 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 5px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.hamburger:hover {
  background: var(--accent);
}

.hamburger:hover span {
  background: white;
}

.hamburger span {
  display: block;
  width: 20px;
  height: 2px;
  background: var(--text-primary);
  border-radius: 2px;
  transition: all 0.3s ease;
}

.hamburger.active span:nth-child(1) {
  transform: rotate(45deg) translate(4px, 5px);
}

.hamburger.active span:nth-child(2) {
  opacity: 0;
}

.hamburger.active span:nth-child(3) {
  transform: rotate(-45deg) translate(4px, -5px);
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: var(--bg-card);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-radius: 12px;
  box-shadow: var(--card-shadow);
  border: 1px solid var(--border);
  min-width: 180px;
  display: none;
  overflow: hidden;
  margin-top: 0.5rem;
}

.dropdown-menu.active {
  display: block;
}

.menu-item {
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
  color: var(--text-primary);
  transition: background 0.2s;
}

.menu-item:hover {
  background: var(--accent);
  color: white;
}

.main-content {
  padding: 1.5rem;
  max-width: 1400px;
  margin: 0 auto;
}

.devices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

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

.device-card {
  background: var(--bg-card);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 1.25rem;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2), 0 4px 12px rgba(0, 0, 0, 0.1);
  border: 1px solid var(--border);
  cursor: grab;
  transition: all 0.3s ease;
}

[data-theme="dark"] .device-card {
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4), 0 4px 12px rgba(0, 0, 0, 0.25);
}

.device-card:hover {
  transform: translateY(-4px) scale(1.02);
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.3), 0 8px 20px rgba(0, 0, 0, 0.15);
}

[data-theme="dark"] .device-card:hover {
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.5), 0 8px 20px rgba(0, 0, 0, 0.3);
}

.device-card.sortable-ghost {
  opacity: 0.5;
}

.device-card.sortable-chosen {
  cursor: grabbing;
}

.device-name {
  font-weight: 600;
  font-size: 1rem;
  margin-bottom: 0.75rem;
  word-break: break-word;
  color: var(--text-primary);
}

.device-status {
  display: flex;
  justify-content: center;
  margin-bottom: 0.75rem;
}

.status-indicator {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  transition: all 0.3s ease;
}

.status-indicator.online {
  background: #22c55e;
  box-shadow: 0 0 20px rgba(34, 197, 94, 0.5);
  animation: pulse-green 2s infinite;
}

.status-indicator.offline {
  background: #ef4444;
  box-shadow: 0 0 20px rgba(239, 68, 68, 0.5);
}

@keyframes pulse-green {
  0%, 100% {
    box-shadow: 0 0 20px rgba(34, 197, 94, 0.5);
  }
  50% {
    box-shadow: 0 0 30px rgba(34, 197, 94, 0.8);
  }
}

.device-last-seen {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

.no-devices {
  grid-column: 1 / -1;
  text-align: center;
  padding: 3rem;
  color: var(--text-secondary);
  background: var(--bg-card);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  border: 1px solid var(--border);
}

.login-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: calc(100vh - 60px);
}

.login-container {
  background: var(--bg-card);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 2rem;
  border-radius: 16px;
  box-shadow: var(--card-shadow);
  border: 1px solid var(--border);
  width: 100%;
  max-width: 400px;
}

.login-container h2 {
  margin-bottom: 1.5rem;
  text-align: center;
  color: var(--text-primary);
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: var(--text-primary);
}

.form-group input {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.2s;
  background: var(--bg-secondary);
  color: var(--text-primary);
}

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

.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.2s;
  font-weight: 500;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.btn-secondary {
  background: #6b7280;
  color: #fff;
}

.btn-secondary:hover {
  background: #4b5563;
}

.btn-small {
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
}

.btn-logout {
  background: transparent;
  color: #ef4444;
  text-decoration: none;
  padding: 0.5rem 1rem;
  border: 1px solid #ef4444;
  border-radius: 8px;
  transition: all 0.2s;
}

.btn-logout:hover {
  background: #ef4444;
  color: #fff;
}

.login-form .btn {
  width: 100%;
  margin-top: 0.5rem;
}

.error-message {
  background: rgba(239, 68, 68, 0.1);
  color: #ef4444;
  padding: 0.75rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  text-align: center;
  border: 1px solid rgba(239, 68, 68, 0.3);
}

.success-message {
  background: rgba(34, 197, 94, 0.1);
  color: #22c55e;
  padding: 0.75rem;
  border-radius: 8px;
  margin-top: 1rem;
  text-align: center;
  border: 1px solid rgba(34, 197, 94, 0.3);
}

.password-form .form-row {
  margin-bottom: 1rem;
}

.back-link {
  text-align: center;
  margin-top: 1rem;
}

.back-link a {
  color: var(--accent);
}

.admin-page {
  padding-bottom: 2rem;
  background: var(--bg-primary);
  min-height: 100vh;
}

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

.admin-section {
  background: var(--bg-card);
  border-radius: 16px;
  padding: 1.5rem;
  margin-bottom: 1.5rem;
  box-shadow: var(--card-shadow);
  border: 1px solid var(--border);
}

.admin-section h2 {
  margin-bottom: 1rem;
  padding-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
}

.mqtt-status-display {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 1rem;
  padding: 0.75rem;
  background: var(--bg-secondary);
  border-radius: 8px;
}

.status-text.connected {
  color: #22c55e;
  font-weight: 600;
}

.status-text.disconnected {
  color: #ef4444;
  font-weight: 600;
}

.mqtt-form .form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}

@media (max-width: 600px) {
  .mqtt-form .form-row {
    grid-template-columns: 1fr;
  }
}

.form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
}

.devices-table-container {
  overflow-x: auto;
}

.devices-table {
  width: 100%;
  border-collapse: collapse;
}

.devices-table th,
.devices-table td {
  padding: 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  color: var(--text-primary);
}

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

.input-small {
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.875rem;
  background: var(--bg-secondary);
  color: var(--text-primary);
}

.input-number {
  width: 80px;
}

.topic-cell {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-badge {
  display: inline-block;
  padding: 0.25rem 0.75rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
}

.status-badge.online {
  background: rgba(34, 197, 94, 0.2);
  color: #16a34a;
}

.status-badge.offline {
  background: rgba(239, 68, 68, 0.2);
  color: #dc2626;
}

.no-devices-text {
  color: var(--text-muted);
  text-align: center;
  padding: 2rem;
}

.device-form {
  display: contents;
}

.logs-container {
  background: #0d1117;
  border-radius: 8px;
  padding: 1rem;
  max-height: 250px;
  overflow-y: auto;
  font-family: 'Monaco', 'Consolas', monospace;
  font-size: 0.8rem;
}

.log-entry {
  padding: 0.35rem 0;
  border-bottom: 1px solid #21262d;
  display: flex;
  gap: 0.75rem;
  color: #8b949e;
}

.log-entry:last-child {
  border-bottom: none;
}

.log-time {
  color: #484f58;
  flex-shrink: 0;
}

.log-message {
  word-break: break-word;
}

.log-entry.success .log-message {
  color: #3fb950;
}

.log-entry.error .log-message {
  color: #f85149;
}

.log-entry.warning .log-message {
  color: #d29922;
}

.log-entry.info .log-message {
  color: #58a6ff;
}

.status-dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
}

.status-dot.online {
  background: #22c55e;
}

.status-dot.offline {
  background: #ef4444;
}

.block-checkbox {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: #ef4444;
}

.block-cell {
  text-align: center;
  width: 60px;
}

.blocked-row {
  background: rgba(239, 68, 68, 0.1);
  opacity: 0.7;
}

.blocked-row td {
  text-decoration: line-through;
  color: var(--text-muted);
}
