:root {
  --bg: #08080e;
  --surface: #111119;
  --surface-hover: #181824;
  --border: #222235;
  --border-light: #2a2a40;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --primary-dim: rgba(59,130,246,0.15);
  --text: #e2e8f0;
  --text-muted: #64748b;
  --text-dim: #475569;
  --success: #22c55e;
  --warning: #eab308;
  --danger: #ef4444;
  --danger-dim: rgba(239,68,68,0.15);
  --radius: 16px;
  --radius-sm: 10px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* --- Login --- */

#login-view {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: radial-gradient(ellipse at 50% 0%, rgba(59,130,246,0.08) 0%, transparent 60%);
}

.login-card {
  width: 100%;
  max-width: 380px;
  text-align: center;
}

.login-logo {
  width: 56px;
  height: 56px;
  color: var(--primary);
  margin-bottom: 20px;
}

.login-card h1 {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 6px;
}

.login-sub {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 32px;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.field {
  position: relative;
}

.field input {
  width: 100%;
  padding: 16px;
  padding-top: 22px;
  padding-bottom: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color 0.2s;
}

.field input:focus {
  border-color: var(--primary);
}

.field input:-webkit-autofill,
.field input:-webkit-autofill:hover,
.field input:-webkit-autofill:focus {
  -webkit-text-fill-color: var(--text);
  -webkit-box-shadow: 0 0 0 1000px var(--surface) inset;
  caret-color: var(--text);
}

.field input:-webkit-autofill + label {
  top: 12px;
  font-size: 11px;
  color: var(--primary);
}

.field label {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 16px;
  pointer-events: none;
  transition: all 0.2s;
}

.field input:focus + label,
.field input:not(:placeholder-shown) + label {
  top: 12px;
  font-size: 11px;
  color: var(--primary);
}

#login-form button[type="submit"] {
  padding: 14px;
  background: var(--primary);
  border: none;
  border-radius: var(--radius-sm);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  margin-top: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

#login-form button[type="submit"]:hover {
  background: var(--primary-hover);
}

#login-form button[type="submit"]:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.error {
  color: var(--danger);
  font-size: 14px;
  margin-top: 16px;
}

/* --- Spinner --- */

.spinner {
  display: inline-block;
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

.spinner.large {
  width: 36px;
  height: 36px;
  border-width: 3px;
  border-color: rgba(59,130,246,0.3);
  border-top-color: var(--primary);
}

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

/* --- Dashboard --- */

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  position: sticky;
  top: 0;
  background: rgba(8,8,14,0.85);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  z-index: 10;
  border-bottom: 1px solid var(--border);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

header h1 {
  font-size: 20px;
  font-weight: 700;
}

.badge {
  background: var(--primary-dim);
  color: var(--primary);
  font-size: 12px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 99px;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}

.icon-btn:hover {
  color: var(--text);
  border-color: var(--border-light);
  background: var(--surface-hover);
}

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

.icon-btn.spinning svg {
  animation: spin 0.8s linear infinite;
}

/* --- All controls --- */

.all-controls {
  display: flex;
  gap: 8px;
  padding: 16px 20px 8px;
}

.all-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
}

.all-btn svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

.all-btn:hover {
  color: var(--text);
  border-color: var(--border-light);
}

.all-btn.all-stop:hover {
  border-color: var(--danger);
  color: var(--danger);
  background: var(--danger-dim);
}

/* --- Device grid --- */

.devices-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 6px;
  padding: 6px 16px 20px;
}

.device-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: border-color 0.2s, opacity 0.2s;
}

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

.device-card.offline {
  opacity: 0.45;
}

.device-name {
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-muted);
  letter-spacing: 0.01em;
}

/* --- Sliders --- */

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
  margin: 0;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  background: var(--primary);
  border-radius: 50%;
  cursor: grab;
  border: 2px solid var(--bg);
}

input[type="range"]::-webkit-slider-thumb:active {
  cursor: grabbing;
}

input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  background: var(--primary);
  border-radius: 50%;
  cursor: grab;
  border: 2px solid var(--bg);
}

.tilt-slider::-webkit-slider-thumb {
  background: var(--text-muted);
}
.tilt-slider::-moz-range-thumb {
  background: var(--text-muted);
}

/* --- Loading overlay --- */

.loading-overlay {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 80px 20px;
  color: var(--text-muted);
}

/* --- Empty state --- */

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 80px 20px;
  color: var(--text-muted);
}

.empty-state svg {
  width: 48px;
  height: 48px;
}

/* --- Toast --- */

#toast-container {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 20px;
  font-size: 14px;
  pointer-events: auto;
  animation: toastIn 0.3s ease;
  white-space: nowrap;
}

.toast.error {
  border-color: var(--danger);
  color: var(--danger);
}

.toast.success {
  border-color: var(--success);
  color: var(--success);
}

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

/* --- Responsive --- */

@media (max-width: 640px) {
  .devices-grid {
    grid-template-columns: 1fr;
    padding: 8px 12px 20px;
  }

  .all-controls {
    padding: 10px 12px 4px;
  }

  header {
    padding: 12px 14px;
  }
}

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