/* =============================
   IoT Control Hub — Dark Theme
   ============================= */

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=JetBrains+Mono:wght@400;500&display=swap');

/* ── Tokens ── */
:root {
  --void:        #0A0E1A;
  --surface:     #111827;
  --card:        #1A2235;
  --card-hover:  #1E2A45;
  --border:      rgba(255,255,255,0.07);
  --border-glow: rgba(0, 212, 255, 0.35);

  --accent:      #00D4FF;
  --accent-dim:  rgba(0, 212, 255, 0.12);
  --purple:      #7C3AED;
  --purple-dim:  rgba(124, 58, 237, 0.15);

  --on:          #10B981;
  --on-dim:      rgba(16, 185, 129, 0.15);
  --off:         #EF4444;
  --off-dim:     rgba(239, 68, 68, 0.12);
  --warn:        #F59E0B;

  --text-primary:   #F1F5F9;
  --text-secondary: #94A3B8;
  --text-muted:     #4B5563;

  --font-ui:   'Inter', system-ui, sans-serif;
  --font-data: 'JetBrains Mono', 'Courier New', monospace;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 22px;

  --shadow-card: 0 4px 24px rgba(0,0,0,0.4);
  --shadow-glow: 0 0 20px rgba(0, 212, 255, 0.2);

  --sidebar-w: 240px;
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; scroll-behavior: smooth; }

body {
  font-family: var(--font-ui);
  background: var(--void);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* ── Layout ── */
.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: auto 1fr;
  min-height: 100vh;
}

/* ── Sidebar ── */
.sidebar {
  grid-row: 1 / -1;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 24px 0;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar-logo {
  padding: 0 20px 28px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}

.sidebar-logo .logo-icon {
  width: 34px;
  height: 34px;
  background: linear-gradient(135deg, var(--accent), var(--purple));
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
  box-shadow: 0 0 12px rgba(0,212,255,0.3);
}

.sidebar-logo span { color: var(--accent); }

.nav-section-label {
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 10px 20px 6px;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 500;
  border-left: 3px solid transparent;
  transition: all var(--transition);
  cursor: pointer;
}

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

.nav-link.active {
  color: var(--accent);
  background: var(--accent-dim);
  border-left-color: var(--accent);
}

.nav-link .nav-icon { font-size: 1.05rem; flex-shrink: 0; }

.sidebar-footer {
  margin-top: auto;
  padding: 16px 20px 0;
  border-top: 1px solid var(--border);
}

/* ── Header ── */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 28px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(12px);
}

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

.page-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
}

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

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  background: var(--card);
  padding: 6px 12px;
  border-radius: 100px;
  border: 1px solid var(--border);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--on);
  box-shadow: 0 0 6px var(--on);
  animation: pulse-dot 2s ease-in-out infinite;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--purple), var(--accent));
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: #fff;
  cursor: pointer;
  border: 2px solid var(--border);
  transition: border-color var(--transition);
}

.avatar:hover { border-color: var(--accent); }

/* ── Main Content ── */
.main-content {
  padding: 28px;
  overflow-y: auto;
}

/* ── Stats Bar ── */
.stats-bar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 28px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px 20px;
  display: flex;
  align-items: center;
  gap: 14px;
  transition: border-color var(--transition), transform var(--transition);
}

.stat-card:hover {
  border-color: rgba(0,212,255,0.2);
  transform: translateY(-1px);
}

.stat-icon {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
}

.stat-icon.cyan  { background: var(--accent-dim); }
.stat-icon.green { background: var(--on-dim); }
.stat-icon.red   { background: var(--off-dim); }
.stat-icon.purple{ background: var(--purple-dim); }

.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  font-family: var(--font-data);
  line-height: 1;
  color: var(--text-primary);
}

.stat-label {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 3px;
}

/* ── Section Header ── */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.section-title {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
}

.section-title::before {
  content: '';
  display: block;
  width: 3px;
  height: 16px;
  background: var(--accent);
  border-radius: 2px;
}

/* ── Devices Grid ── */
.devices {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  margin-bottom: 28px;
}

/* ── Device Card ── */
.device-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 22px;
  position: relative;
  overflow: hidden;
  transition: border-color var(--transition), transform var(--transition), box-shadow var(--transition);
  cursor: pointer;
}

.device-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: var(--border);
  transition: background var(--transition), box-shadow var(--transition);
}

.device-card:hover {
  border-color: rgba(0,212,255,0.2);
  transform: translateY(-2px);
  box-shadow: var(--shadow-card);
}

.device-card.is-on::before {
  background: linear-gradient(90deg, var(--on), var(--accent));
  box-shadow: 0 0 16px rgba(16,185,129,0.5);
  animation: glow-bar 3s ease-in-out infinite;
}

@keyframes glow-bar {
  0%, 100% { box-shadow: 0 0 10px rgba(16,185,129,0.4); }
  50%       { box-shadow: 0 0 22px rgba(0,212,255,0.6); }
}

.device-card.is-off::before {
  background: var(--border);
}

.device-card.is-offline {
  opacity: 0.65;
}

.device-card.is-offline::before {
  background: var(--off);
}

/* Card header row */
.device-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 16px;
}

.device-icon-wrap {
  width: 46px;
  height: 46px;
  border-radius: var(--radius-md);
  background: var(--accent-dim);
  border: 1px solid rgba(0,212,255,0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
}

.device-card.is-off .device-icon-wrap  { background: rgba(255,255,255,0.04); border-color: var(--border); }
.device-card.is-offline .device-icon-wrap { background: var(--off-dim); border-color: rgba(239,68,68,0.2); }

/* Toggle switch */
.toggle {
  position: relative;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}

.toggle input { opacity: 0; width: 0; height: 0; }

.toggle-track {
  position: absolute;
  inset: 0;
  border-radius: 100px;
  background: rgba(255,255,255,0.1);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

.toggle input:checked + .toggle-track {
  background: var(--on);
  border-color: var(--on);
  box-shadow: 0 0 12px rgba(16,185,129,0.4);
}

.toggle-track::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  transition: transform var(--transition);
  box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}

.toggle input:checked + .toggle-track::after { transform: translateX(20px); }

.toggle input:disabled + .toggle-track { cursor: not-allowed; opacity: 0.4; }

/* Card body */
.device-name {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 3px;
}

.device-location {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 14px;
}

.device-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

/* Status badge */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 100px;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.04em;
}

.badge::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
}

.badge.on  { background: var(--on-dim); color: var(--on); }
.badge.on::before  { background: var(--on); box-shadow: 0 0 4px var(--on); }

.badge.off { background: var(--off-dim); color: var(--off); }
.badge.off::before { background: var(--off); }

.badge.online  { background: var(--on-dim);  color: var(--on); }
.badge.online::before  { background: var(--on);  box-shadow: 0 0 4px var(--on); }

.badge.offline { background: var(--off-dim); color: var(--off); }
.badge.offline::before { background: var(--off); }

.badge.warn { background: rgba(245,158,11,0.12); color: var(--warn); }
.badge.warn::before { background: var(--warn); }

/* Sensor mini-value */
.device-reading {
  font-family: var(--font-data);
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.device-reading span {
  color: var(--accent);
  font-weight: 500;
}

/* ── Sensor Readings Section ── */
.sensor-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 28px;
}

.sensor-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 20px;
  transition: border-color var(--transition);
}

.sensor-card:hover { border-color: rgba(124,58,237,0.3); }

.sensor-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.sensor-value {
  font-family: var(--font-data);
  font-size: 2rem;
  font-weight: 500;
  color: var(--text-primary);
  line-height: 1;
  margin-bottom: 4px;
}

.sensor-value sup {
  font-size: 0.9rem;
  color: var(--text-secondary);
  vertical-align: super;
}

.sensor-sub {
  font-size: 0.72rem;
  color: var(--text-muted);
}

/* Sparkline bar */
.sparkline {
  margin-top: 12px;
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}

.sparkline-fill {
  height: 100%;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--purple), var(--accent));
}

/* ── Login Card ── */
.login-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
  background: var(--void);
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  padding: 36px 32px;
  box-shadow: 0 24px 64px rgba(0,0,0,0.5);
}

.login-logo {
  text-align: center;
  margin-bottom: 28px;
}

.login-logo .logo-icon {
  width: 52px;
  height: 52px;
  background: linear-gradient(135deg, var(--accent), var(--purple));
  border-radius: var(--radius-md);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  margin-bottom: 12px;
  box-shadow: 0 0 24px rgba(0,212,255,0.35);
}

.login-logo h1 {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.02em;
}

.login-logo p {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* Form elements */
.field {
  margin-bottom: 14px;
}

label {
  display: block;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

input[type="text"],
input[type="email"],
input[type="password"],
input[type="number"],
select,
textarea {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 11px 14px;
  font-family: var(--font-ui);
  font-size: 0.875rem;
  color: var(--text-primary);
  transition: border-color var(--transition), box-shadow var(--transition);
  outline: none;
  appearance: none;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(0,212,255,0.12);
}

input::placeholder { color: var(--text-muted); }

select { cursor: pointer; }

/* ── Buttons ── */
button, .button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 11px 20px;
  border-radius: var(--radius-sm);
  font-family: var(--font-ui);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  border: 1px solid transparent;
  transition: all var(--transition);
  white-space: nowrap;
}

/* Primary */
.btn-primary, button {
  background: var(--accent);
  color: #0A0E1A;
  border-color: var(--accent);
}

.btn-primary:hover, button:hover {
  background: #33DDFF;
  box-shadow: 0 0 20px rgba(0,212,255,0.35);
  transform: translateY(-1px);
}

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

/* Secondary */
.btn-secondary {
  background: transparent;
  color: var(--text-secondary);
  border-color: var(--border);
}

.btn-secondary:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-dim);
}

/* Ghost */
.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border-color: transparent;
  padding: 8px 12px;
}

.btn-ghost:hover {
  color: var(--text-primary);
  background: rgba(255,255,255,0.05);
}

/* Danger */
.btn-danger {
  background: var(--off-dim);
  color: var(--off);
  border-color: rgba(239,68,68,0.25);
}

.btn-danger:hover {
  background: var(--off);
  color: #fff;
  box-shadow: 0 0 16px rgba(239,68,68,0.3);
}

/* Full width */
.btn-full { width: 100%; }

/* ── Alerts / Errors ── */
.alert {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.825rem;
  margin-bottom: 16px;
  border: 1px solid transparent;
}

.alert-error {
  background: var(--off-dim);
  color: #FCA5A5;
  border-color: rgba(239,68,68,0.2);
}

.alert-success {
  background: var(--on-dim);
  color: #6EE7B7;
  border-color: rgba(16,185,129,0.2);
}

.alert-warn {
  background: rgba(245,158,11,0.1);
  color: #FCD34D;
  border-color: rgba(245,158,11,0.2);
}

/* Legacy aliases kept for backward compat */
.error  { background: var(--off-dim); color: #FCA5A5; padding: 12px 16px; border-radius: var(--radius-sm); margin-bottom: 16px; font-size: 0.825rem; border: 1px solid rgba(239,68,68,0.2); }
.on     { color: var(--on); font-weight: 600; }
.off    { color: var(--off); font-weight: 600; }
.online { color: var(--on); font-weight: 600; }
.offline{ color: var(--off); font-weight: 600; }

/* ── Divider ── */
.divider {
  height: 1px;
  background: var(--border);
  margin: 20px 0;
}

/* ── Utility ── */
.text-muted  { color: var(--text-muted); }
.text-accent { color: var(--accent); }
.text-sm     { font-size: 0.8rem; }
.mono        { font-family: var(--font-data); }
.flex        { display: flex; align-items: center; }
.flex-between{ display: flex; align-items: center; justify-content: space-between; }
.gap-8       { gap: 8px; }
.gap-12      { gap: 12px; }
.mb-16       { margin-bottom: 16px; }
.mb-24       { margin-bottom: 24px; }

/* ── Scrollbar ── */
::-webkit-scrollbar       { width: 6px; }
::-webkit-scrollbar-track { background: var(--void); }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.22); }

/* ── Focus ── */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 2px; }

/* ── Reduced motion ── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* ══════════════════════════════
   RESPONSIVE
   ══════════════════════════════ */

/* Tablet */
@media (max-width: 1024px) {
  .stats-bar { grid-template-columns: repeat(2, 1fr); }
  :root { --sidebar-w: 200px; }
}

/* Mobile */
@media (max-width: 768px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: 56px 1fr auto;
  }

  /* Sidebar becomes bottom nav on mobile */
  .sidebar {
    grid-row: 3;
    height: auto;
    flex-direction: row;
    align-items: center;
    padding: 0;
    border-right: none;
    border-top: 1px solid var(--border);
    position: sticky;
    bottom: 0;
    z-index: 100;
    overflow-x: auto;
    overflow-y: visible;
  }

  .sidebar-logo,
  .nav-section-label,
  .sidebar-footer { display: none; }

  .nav-link {
    flex-direction: column;
    gap: 3px;
    padding: 10px 16px;
    font-size: 0.65rem;
    border-left: none;
    border-top: 3px solid transparent;
    white-space: nowrap;
    min-width: 64px;
    text-align: center;
  }

  .nav-link.active { border-top-color: var(--accent); border-left-color: transparent; }
  .nav-link .nav-icon { font-size: 1.3rem; }

  header { grid-row: 1; }

  .main-content { padding: 16px; }

  .stats-bar { grid-template-columns: repeat(2, 1fr); gap: 10px; }

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

  .sensor-grid { grid-template-columns: repeat(2, 1fr); }

  .login-card { padding: 28px 20px; }
}

@media (max-width: 420px) {
  .stats-bar  { grid-template-columns: 1fr 1fr; }
  .sensor-grid{ grid-template-columns: 1fr 1fr; }
  .stat-value { font-size: 1.2rem; }
}
