/**
 * IOT CORE - Modern Dashboard Stylesheet
 * Thiết kế thuần CSS nội bộ (Zero CDN / Zero Internet dependency)
 * Hỗ trợ Responsive (1440px desktop, 1024px tablet, 390px mobile)
 * Dark mode, Glassmorphism, Accessibility, prefers-reduced-motion
 */

:root, [data-theme="light"] {
  --sidebar: #0E1B2E;
  --sidebar-active: #F97316;
  --page: #F5F7FA;
  --card: #FFFFFF;
  --border: #E6EAF0;
  --text: #0F172A;
  --text-muted: #64748B;
  --accent: #F97316;
  --accent-soft: #FFF1E6;
  --ok: #22C55E;
  --warn: #F59E0B;
  --danger: #EF4444;
  --info: #3B82F6;
  --offline: #94A3B8;
  --unconfigured: #94A3B8;

  --chart-bg: #F8FAFC;
  --surface-sunken: #F1F5F9;
  --gutter-line: #E2E8F0;

  --bg-base: var(--page);
  --bg-surface: var(--card);
  --bg-surface-elevated: var(--card);
  --bg-input: #FFFFFF;
  --bg-hover: rgba(15, 23, 42, 0.04);
  
  --border-subtle: var(--border);
  --border-focus: var(--accent);
  --border-active: rgba(249, 115, 22, 0.4);

  --text-main: var(--text);
  --text-dim: var(--text-muted);

  --primary: var(--accent);
  --primary-hover: #EA580C;
  --primary-glow: rgba(249, 115, 22, 0.25);

  --success: var(--ok);
  --warning: var(--warn);

  --shadow-card: 0 1px 2px rgba(15, 23, 42, 0.04), 0 8px 24px rgba(15, 23, 42, 0.06);
  --shadow-hover: 0 12px 30px rgba(15, 23, 42, 0.12);

  --radius-lg: 16px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --radius-pill: 9999px;

  --sidebar-w: 240px;
  --topbar-h: 70px;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}

[data-theme="dark"] {
  --sidebar: #0A1626;
  --sidebar-active: #F97316;
  --page: #0B1220;
  --card: #111C2E;
  --border: #1E293B;
  --text: #E2E8F0;
  --text-muted: #94A3B8;
  --accent: #F97316;
  --accent-soft: #3A2415;
  --ok: #22C55E;
  --warn: #F59E0B;
  --danger: #EF4444;
  --info: #60A5FA;
  --offline: #64748B;
  --unconfigured: #64748B;

  --chart-bg: rgba(15, 23, 42, 0.4);
  --surface-sunken: #0D172A;
  --gutter-line: #1E293B;

  --bg-base: var(--page);
  --bg-surface: var(--card);
  --bg-surface-elevated: rgba(30, 41, 59, 0.7);
  --bg-input: #0B1220;
  --bg-hover: rgba(255, 255, 255, 0.05);

  --border-subtle: var(--border);
  --border-focus: var(--accent);
  --border-active: rgba(249, 115, 22, 0.4);

  --text-main: var(--text);
  --text-dim: var(--text-muted);

  --primary: var(--accent);
  --primary-hover: #EA580C;
  --primary-glow: rgba(249, 115, 22, 0.35);

  --success: var(--ok);
  --warning: var(--warn);

  --shadow-card: 0 10px 25px -5px rgba(0, 0, 0, 0.5), 0 0 0 1px rgba(255, 255, 255, 0.06);
  --shadow-hover: 0 20px 35px -10px rgba(0, 0, 0, 0.7), 0 0 0 1px rgba(249, 115, 22, 0.3);
}

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

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

html, body {
  min-height: 100vh;
  font-family: var(--font-family);
  background-color: var(--page);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* App Layout */
.app-layout {
  display: flex;
  min-height: 100vh;
  position: relative;
  overflow-x: hidden;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--sidebar, #0E1B2E);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  z-index: 50;
  transition: width 0.25s ease, transform 0.25s ease;
}

.sidebar-header {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.brand-logo {
  width: 36px;
  height: 36px;
  object-fit: contain;
  border-radius: var(--radius-sm);
  filter: drop-shadow(0 0 8px rgba(249, 115, 22, 0.35));
}

.brand-info {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.brand-title {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: #F8FAFC;
  white-space: nowrap;
}

.brand-sub {
  font-size: 0.75rem;
  color: #94A3B8;
  white-space: nowrap;
}

.sidebar-nav {
  padding: 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  color: #94A3B8;
  text-decoration: none;
  font-size: 0.92rem;
  font-weight: 500;
  transition: all 0.15s ease;
  border: 1px solid transparent;
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.06);
  color: #FFFFFF;
}

.nav-link.active {
  background: var(--sidebar-active, #F97316) !important;
  color: #FFFFFF !important;
  border-color: transparent !important;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(249, 115, 22, 0.35);
}

.nav-link.active svg {
  stroke: #FFFFFF !important;
}

.app-icon {
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
}

.nav-icon {
  font-size: 1.15rem;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

/* Main Content Area */
.main-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow-x: hidden;
}

/* Topbar */
.topbar {
  height: var(--topbar-h);
  background: var(--card);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 28px;
  position: sticky;
  top: 0;
  z-index: 40;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 16px;
  flex: 1;
  max-width: 480px;
}

.btn-mobile-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--text-main);
  font-size: 1.4rem;
  cursor: pointer;
  padding: 4px;
}

.search-box {
  position: relative;
  width: 100%;
}

.search-input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill);
  padding: 8px 16px 8px 38px;
  color: var(--text-main);
  font-size: 0.88rem;
  outline: none;
  transition: all 0.2s ease;
}

.search-input:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px var(--primary-glow);
}

.search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-dim);
  pointer-events: none;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: var(--radius-pill);
  background: rgba(16, 185, 129, 0.1);
  border: 1px solid rgba(16, 185, 129, 0.25);
  font-size: 0.8rem;
  color: #34d399;
}

.status-indicator.offline {
  background: rgba(239, 68, 68, 0.1);
  border-color: rgba(239, 68, 68, 0.25);
  color: #f87171;
}

.status-indicator.connecting {
  background: rgba(245, 158, 11, 0.1);
  border-color: rgba(245, 158, 11, 0.25);
  color: #fbbf24;
}

.dot-pulse {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: currentColor;
  box-shadow: 0 0 8px currentColor;
  animation: pulse-ring 2s infinite;
}

@keyframes pulse-ring {
  0% { transform: scale(0.95); opacity: 1; }
  50% { transform: scale(1.3); opacity: 0.6; }
  100% { transform: scale(0.95); opacity: 1; }
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-left: 16px;
  border-left: 1px solid var(--border-subtle);
}

.user-info {
  display: flex;
  flex-direction: column;
  text-align: right;
}

.user-name {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text-main);
}

.user-role-badge {
  font-size: 0.7rem;
  color: #a5b4fc;
  background: rgba(79, 70, 229, 0.25);
  border: 1px solid rgba(99, 102, 241, 0.3);
  padding: 1px 6px;
  border-radius: 4px;
  align-self: flex-end;
  text-transform: uppercase;
}

.btn-topbar-action {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--text-muted);
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.82rem;
  font-weight: 500;
  text-decoration: none;
  cursor: pointer;
  transition: all 0.2s ease;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-topbar-action:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-main);
}

.btn-topbar-logout {
  color: #fca5a5;
  border-color: rgba(239, 68, 68, 0.3);
}

.btn-topbar-logout:hover {
  background: rgba(239, 68, 68, 0.2);
  color: #fff;
}

/* Page Content */
.content-body {
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 28px;
  max-width: 1440px;
  width: 100%;
  margin: 0 auto;
  min-width: 0;
  box-sizing: border-box;
}

/* KPI Cards Grid */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 20px;
}

.kpi-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 22px;
  box-shadow: var(--shadow-card);
  display: flex;
  align-items: center;
  gap: 18px;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  position: relative;
  overflow: hidden;
  min-width: 0;
}

.kpi-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-hover);
  border-color: var(--border-active);
}

.kpi-card::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--card-accent, var(--primary)), transparent);
}

.kpi-icon-box {
  width: 52px;
  height: 52px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  background: var(--card-accent-bg, rgba(79, 70, 229, 0.15));
  border: 1px solid var(--card-accent-border, rgba(99, 102, 241, 0.3));
  color: var(--card-accent, var(--primary));
  flex-shrink: 0;
}

.kpi-content {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.kpi-label {
  font-size: 0.82rem;
  color: var(--text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.kpi-value {
  font-size: 1.9rem;
  font-weight: 700;
  color: var(--text-main);
  line-height: 1.2;
}

.kpi-sub {
  font-size: 0.75rem;
  color: var(--text-dim);
}

/* Card Themes */
.card-total { --card-accent: #6366f1; --card-accent-bg: rgba(99, 102, 241, 0.15); --card-accent-border: rgba(99, 102, 241, 0.3); }
.card-online { --card-accent: #10b981; --card-accent-bg: rgba(16, 185, 129, 0.15); --card-accent-border: rgba(16, 185, 129, 0.3); }
.card-offline { --card-accent: #9ca3af; --card-accent-bg: rgba(156, 163, 175, 0.15); --card-accent-border: rgba(156, 163, 175, 0.3); }
.card-alert { --card-accent: #ef4444; --card-accent-bg: rgba(239, 68, 68, 0.15); --card-accent-border: rgba(239, 68, 68, 0.3); }

/* Chart Section */
.chart-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
}

.chart-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
  min-width: 0;
  max-width: 100%;
}

.panel-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-main);
  display: flex;
  align-items: center;
  gap: 8px;
}

.chart-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.select-custom {
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  color: var(--text-main);
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  outline: none;
  cursor: pointer;
  transition: border-color 0.2s;
}

.select-custom:focus {
  border-color: var(--border-focus);
}

.segmented-group {
  display: flex;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 2px;
}

.btn-segment {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 5px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-segment:hover {
  color: var(--text-main);
}

.btn-segment.active {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 2px 8px var(--primary-glow);
}

.chart-svg-container {
  width: 100%;
  height: 280px;
  position: relative;
  background: var(--chart-bg);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  overflow: hidden;
}

/* Two Column Layout: Devices Table & Alerts Panel */
.dashboard-grid-dual {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 24px;
  min-width: 0;
  max-width: 100%;
}

/* Panel Containers */
.card-panel {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: var(--shadow-card);
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
}

.panel-header-action {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  min-width: 0;
  max-width: 100%;
}

.table-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  min-width: 0;
  max-width: 100%;
}

/* Buttons */
.btn-primary {
  background: var(--primary);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: var(--radius-sm);
  padding: 7px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-primary:hover {
  background: var(--primary-hover);
  box-shadow: 0 0 12px var(--primary-glow);
}

/* Custom Table */
.table-responsive {
  width: 100%;
  max-height: 680px;
  overflow-x: auto;
  overflow-y: auto;
  position: relative;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-subtle);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
  font-size: 0.88rem;
}

.data-table th {
  background: var(--surface-sunken);
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 12px 16px;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border-subtle);
  white-space: nowrap;
}

.table-pagination-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface-sunken);
  border-top: 1px solid var(--border-subtle);
  font-size: 0.85rem;
  color: var(--text-muted);
}

.table-pagination-bar .pagination-info {
  font-weight: 500;
  color: var(--text-main);
}

.table-pagination-bar .btn-pagination {
  padding: 6px 12px;
  font-size: 0.82rem;
}

.data-table td {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-subtle);
  color: var(--text-main);
  vertical-align: middle;
}

.data-table tbody tr {
  transition: background 0.15s;
}

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

.data-table tbody tr:last-child td {
  border-bottom: none;
}

/* Device Item Badge */
.device-name-cell {
  display: flex;
  flex-direction: column;
}

.device-title {
  font-weight: 600;
  color: var(--text-main);
}

.device-id-code {
  font-size: 0.75rem;
  font-family: var(--font-mono);
  color: var(--text-dim);
}

/* Status Dot Badge */
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  border-radius: var(--radius-pill);
  font-size: 0.75rem;
  font-weight: 600;
}

.status-badge.online {
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.3);
}

.status-badge.offline {
  background: rgba(156, 163, 175, 0.15);
  color: #9ca3af;
  border: 1px solid rgba(156, 163, 175, 0.3);
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}

/* Relay Button & States (Optimistic UI) */
.btn-relay {
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  border: 1px solid transparent;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.btn-relay.on {
  background: rgba(16, 185, 129, 0.2);
  color: #34d399;
  border-color: rgba(16, 185, 129, 0.4);
}

.btn-relay.on:hover {
  background: rgba(16, 185, 129, 0.3);
  box-shadow: 0 0 10px var(--success-glow);
}

.btn-relay.off {
  background: rgba(75, 85, 99, 0.3);
  color: #d1d5db;
  border-color: rgba(75, 85, 99, 0.5);
}

.btn-relay.off:hover {
  background: rgba(75, 85, 99, 0.5);
}

.btn-relay.loading {
  background: rgba(245, 158, 11, 0.2) !important;
  color: #fbbf24 !important;
  border-color: rgba(245, 158, 11, 0.5) !important;
  cursor: wait;
}

.btn-relay.ack {
  background: rgba(16, 185, 129, 0.4) !important;
  color: #fff !important;
  border-color: #10b981 !important;
  box-shadow: 0 0 14px var(--success-glow);
}

.btn-relay.fail {
  background: rgba(239, 68, 68, 0.4) !important;
  color: #fff !important;
  border-color: #ef4444 !important;
  box-shadow: 0 0 14px var(--danger-glow);
}

/* Alerts List */
.alerts-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-height: 480px;
  overflow-y: auto;
  padding-right: 4px;
}

.alert-item {
  background: var(--surface-sunken);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: border-color 0.2s;
}

.alert-item.critical {
  border-left: 4px solid var(--danger);
  background: rgba(239, 68, 68, 0.05);
}

.alert-item.warning {
  border-left: 4px solid var(--warning);
  background: rgba(245, 158, 11, 0.05);
}

.alert-item.info {
  border-left: 4px solid var(--info);
  background: rgba(14, 165, 233, 0.05);
}

.alert-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.alert-badge {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  padding: 2px 6px;
  border-radius: 4px;
}

.alert-badge.critical { background: rgba(239, 68, 68, 0.15); color: var(--danger); }
.alert-badge.warning { background: rgba(245, 158, 11, 0.15); color: var(--warning); }
.alert-badge.info { background: rgba(59, 130, 246, 0.15); color: var(--info); }
.alert-badge.device-offline,
.alert-badge-device-offline { background: rgba(100, 116, 139, 0.15); color: #64748b; border: 1px solid rgba(100, 116, 139, 0.3); }

.alert-time {
  font-size: 0.75rem;
  color: var(--text-dim);
}

.alert-rule-text {
  font-size: 0.85rem;
  color: var(--text-main);
  font-weight: 500;
}

.alert-footer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.alert-device-tag {
  font-size: 0.75rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
}

.btn-ack {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--border-subtle);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-ack:hover {
  background: rgba(16, 185, 129, 0.2);
  color: #34d399;
  border-color: rgba(16, 185, 129, 0.4);
}

/* Empty State */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 36px 16px;
  gap: 10px;
  color: var(--text-dim);
}

.empty-icon {
  font-size: 2.5rem;
  opacity: 0.6;
}

.empty-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-muted);
}

.empty-desc {
  font-size: 0.8rem;
  max-width: 320px;
}

/* Skeleton Loading */
.skeleton {
  background: linear-gradient(90deg, rgba(255, 255, 255, 0.04) 25%, rgba(255, 255, 255, 0.08) 50%, rgba(255, 255, 255, 0.04) 75%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.5s infinite;
  border-radius: var(--radius-sm);
}

@keyframes skeleton-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* Modal Dialog (<dialog class="modal">) */
dialog.modal {
  position: fixed;
  inset: 0;
  margin: auto;
  padding: 20px;
  border: none;
  background: transparent;
  color: var(--text);
  max-width: 100vw;
  max-height: 100vh;
  overflow: visible;
  z-index: 100;
  outline: none;
}

dialog.modal::backdrop {
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

dialog.modal[open] {
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-card {
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  width: 100%;
  max-width: 440px;
  max-height: 92vh;
  overflow-y: auto;
  box-sizing: border-box;
  box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.8);
  display: flex;
  flex-direction: column;
  gap: 20px;
  transform: translateY(0);
}

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

.modal-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-main);
}

.btn-close-modal {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 1.5rem;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  transition: color 0.15s, background-color 0.15s;
}

.btn-close-modal:hover {
  color: var(--text-main);
  background: var(--bg-hover, rgba(255, 255, 255, 0.08));
}

.modal-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-label {
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-muted);
}

.form-input {
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  color: var(--text-main);
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.2s;
}

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

.modal-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 8px;
}

.btn-cancel {
  background: transparent;
  border: 1px solid var(--border-subtle);
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  padding: 8px 16px;
  font-size: 0.85rem;
  cursor: pointer;
}

.btn-cancel:hover {
  background: var(--bg-hover);
  color: var(--text-main);
}

/* Toast Notifications */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 110;
  pointer-events: none;
}

.toast-item {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 18px;
  color: var(--text-main);
  font-size: 0.85rem;
  box-shadow: var(--shadow-card);
  pointer-events: auto;
  animation: toast-in 0.3s ease;
  display: flex;
  align-items: center;
  gap: 10px;
}

.toast-item.success { border-left: 4px solid var(--success); }
.toast-item.error { border-left: 4px solid var(--danger); }
.toast-item.info { border-left: 4px solid var(--info); }

@keyframes toast-in {
  from { transform: translateX(50px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Responsive Breakpoints */
@media (max-width: 1024px) {
  :root {
    --sidebar-w: 68px;
  }
  .brand-info, .brand-sub, .nav-text, .sidebar-footer {
    display: none;
  }
  .sidebar-header {
    justify-content: center;
    padding: 0;
  }
  .nav-link {
    justify-content: center;
    padding: 12px 0;
  }
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
  }
  .dashboard-grid-dual {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 640px) {
  .app-layout {
    flex-direction: column;
    overflow-x: hidden;
  }
  .sidebar {
    position: fixed;
    left: -100%;
    top: 0;
    width: 240px;
    height: 100vh;
    box-shadow: 20px 0 50px rgba(0,0,0,0.5);
  }
  .sidebar.mobile-open {
    left: 0;
  }
  .brand-info, .brand-sub, .nav-text, .sidebar-footer {
    display: flex;
  }
  .sidebar-header {
    justify-content: flex-start;
    padding: 0 20px;
  }
  .nav-link {
    justify-content: flex-start;
    padding: 10px 14px;
  }
  .btn-mobile-toggle {
    display: block;
    padding: 6px 8px;
    line-height: 1;
  }
  .topbar {
    padding: 0 12px;
    gap: 8px;
    max-width: 100%;
  }
  .topbar-left {
    min-width: 0;
    flex: 0 0 auto;
    gap: 8px;
  }
  .search-box {
    display: none;
  }
  .topbar-right {
    min-width: 0;
    flex: 1;
    justify-content: flex-end;
    gap: 6px;
  }
  .status-indicator {
    padding: 4px 8px;
    font-size: 0.72rem;
  }
  #sse-indicator-text {
    display: none;
  }
  .user-profile {
    padding-left: 6px;
    gap: 6px;
  }
  .user-info {
    display: none;
  }
  .topbar-btn-text {
    display: none;
  }
  .btn-topbar-action {
    padding: 6px 8px;
    font-size: 0.9rem;
    gap: 0;
  }
  .content-body {
    padding: 16px 12px;
    gap: 18px;
    display: flex;
    flex-direction: column;
  }
  .kpi-grid { order: 1; }
  .widgets-section { order: 2; }
  .dashboard-grid-dual { display: contents; }
  #alerts { order: 3; }
  .chart-panel { order: 4; }
  #devices { order: 5; }
  .kpi-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  .card-panel {
    padding: 16px 12px;
  }
  .table-controls {
    width: 100%;
    justify-content: flex-start;
  }
  .table-controls .select-custom {
    flex: 1 1 calc(50% - 6px);
    min-width: 120px;
  }
  .table-controls .btn-primary {
    width: 100%;
    justify-content: center;
  }
  dialog.modal {
    padding: 12px;
  }
  .modal-card {
    padding: 18px 14px;
    gap: 14px;
    width: 100%;
    max-width: 100%;
  }
}

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

/* ============================================================================
 * CHẶNG 3.2 — Dynamic Sensor Widgets Grid & Enhanced Modals Styles
 * ============================================================================
 */

.widgets-section {
  display: flex;
  flex-direction: column;
  gap: 16px;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 24px;
  box-shadow: var(--shadow-card);
}

.section-header-action {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 16px;
}

.section-title-wrap {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.panel-subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.widgets-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

.widgets-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 16px;
  min-height: 120px;
}

.sensor-widget-card {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 12px;
  position: relative;
  transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.2s ease, border-color 0.2s ease;
  cursor: grab;
  user-select: none;
  touch-action: none;
  min-width: 0;
}

.sensor-widget-card:hover {
  transform: translateY(-2px);
  border-color: rgba(99, 102, 241, 0.35);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.45);
}

.sensor-widget-card:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.sensor-widget-card.normal {
  border-left: 4px solid var(--success);
}
.sensor-widget-card.warning {
  border-left: 4px solid var(--warning);
  background: var(--bg-surface-elevated);
}
.sensor-widget-card.critical {
  border-left: 4px solid var(--danger);
  background: var(--bg-surface-elevated);
}
.sensor-widget-card.offline,
.sensor-widget-card.no-data {
  border-left: 4px solid var(--text-dim);
  opacity: 0.65;
  filter: grayscale(0.5);
}

.sensor-widget-card.is-dragging {
  opacity: 0.45;
  transform: scale(1.04);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.7), 0 0 0 2px var(--primary);
  z-index: 100;
  cursor: grabbing;
}

.sensor-widget-card.drop-target {
  outline: 2px dashed var(--primary);
  outline-offset: -2px;
}

.widget-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.widget-card-info {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow: hidden;
  min-width: 0;
}

.widget-card-icon {
  font-size: 1.4rem;
  line-height: 1;
  flex-shrink: 0;
}

.widget-card-titles {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.widget-card-label {
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.widget-card-device {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.widget-card-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.btn-card-action {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 0.85rem;
  padding: 4px 6px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s ease;
  line-height: 1;
}

.btn-card-action:hover {
  color: var(--text-main);
  background: var(--bg-hover);
}

.btn-card-action:focus-visible {
  outline: 2px solid var(--primary);
}

.widget-card-body {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin: 6px 0;
}

.widget-card-value {
  font-size: 1.85rem;
  font-weight: 700;
  font-family: var(--font-mono);
  color: var(--text-main);
  line-height: 1.1;
  letter-spacing: -0.02em;
}

.widget-card-unit {
  font-size: 1rem;
  font-weight: 500;
  color: var(--text-muted);
}

.widget-card-boolean-badge {
  display: inline-block;
  padding: 4px 12px;
  border-radius: var(--radius-pill);
  font-weight: 700;
  font-size: 0.9rem;
}
.widget-card-boolean-badge.on {
  background: rgba(16, 185, 129, 0.2);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.4);
}
.widget-card-boolean-badge.off {
  background: rgba(107, 114, 128, 0.2);
  color: #9ca3af;
  border: 1px solid rgba(107, 114, 128, 0.3);
}

.widget-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--text-dim);
  padding-top: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.04);
}

.widget-card-status-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  font-weight: 600;
  font-size: 0.7rem;
}
.widget-card-status-pill.normal {
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
}
.widget-card-status-pill.warning {
  background: rgba(245, 158, 11, 0.15);
  color: #fbbf24;
}
.widget-card-status-pill.critical {
  background: rgba(239, 68, 68, 0.15);
  color: #f87171;
}
.widget-card-status-pill.offline,
.widget-card-status-pill.no-data,
.widget-card-status-pill.no-threshold {
  background: rgba(107, 114, 128, 0.15);
  color: #9ca3af;
}

.widget-card-value.widget-card-no-data {
  font-size: 0.95rem;
  color: var(--text-dim);
  font-weight: 500;
  letter-spacing: -0.01em;
}

.modal-card-wide {
  max-width: 680px;
  width: 95%;
}
.modal-card-sm {
  max-width: 440px;
  width: 90%;
}

.presets-box {
  background: var(--surface-sunken);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 14px;
}
.preset-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.preset-chip {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-subtle);
  color: var(--text-main);
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s ease;
}
.preset-chip:hover {
  background: rgba(99, 102, 241, 0.2);
  border-color: var(--primary);
  transform: translateY(-1px);
}

.form-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.form-row-3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 14px;
}
.form-check-group {
  margin: 10px 0;
}
.form-checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
  color: var(--text-main);
  cursor: pointer;
}
.form-checkbox-label input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
  cursor: pointer;
}

.field-error {
  display: block;
  font-size: 0.75rem;
  color: #f87171;
  margin-top: 4px;
  min-height: 16px;
}

.modal-actions-split {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border-subtle);
}
.modal-actions-right {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
}

.btn-danger {
  background: #dc2626;
  color: #ffffff;
  border: none;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.2s ease;
}
.btn-danger:hover {
  background: #b91c1c;
  box-shadow: 0 4px 12px var(--danger-glow);
}

.modal-confirm-body {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  padding: 16px 0 20px;
}
.confirm-icon {
  font-size: 2rem;
  line-height: 1;
}
.confirm-message {
  font-size: 0.95rem;
  color: var(--text-main);
  line-height: 1.5;
}

.alert-ack-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  font-size: 0.75rem;
  font-weight: 600;
  background: rgba(16, 185, 129, 0.15);
  color: #34d399;
  border: 1px solid rgba(16, 185, 129, 0.3);
}
.alert-resolved-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  font-size: 0.75rem;
  font-weight: 600;
  background: rgba(14, 165, 233, 0.15);
  color: #38bdf8;
  border: 1px solid rgba(14, 165, 233, 0.3);
}

@media (max-width: 1024px) {
  .widgets-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .form-row-3 {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 768px) {
  .widgets-grid {
    grid-template-columns: minmax(0, 1fr);
  }
  .form-row-2, .form-row-3 {
    grid-template-columns: 1fr;
  }
  .modal-actions-split {
    flex-direction: column-reverse;
    align-items: stretch;
  }
  .modal-actions-right {
    width: 100%;
    justify-content: flex-end;
  }
  .btn-danger {
    width: 100%;
  }
}

/* 2.4 Device Classes Grid Styling */
.device-classes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 8px;
  max-height: 320px;
  overflow-y: auto;
  padding: 8px 4px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--bg-input);
  margin-top: 8px;
}

.device-class-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 6px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-main);
  cursor: pointer;
  transition: all 0.2s ease;
  user-select: none;
}

.device-class-card:hover {
  background: var(--bg-hover);
  border-color: var(--primary);
  transform: translateY(-2px);
}

.device-class-card.selected {
  background: rgba(79, 70, 229, 0.25);
  border-color: var(--primary);
  box-shadow: 0 0 12px var(--primary-glow);
}

.device-class-label {
  font-size: 0.75rem;
  font-weight: 500;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  width: 100%;
}

/* 2.6 Unconfigured Sensors Box */
.unconfigured-sensors-box {
  background: var(--surface-sunken);
  border: 1px dashed var(--border);
  border-radius: var(--radius-lg);
  padding: 16px 20px;
  margin-top: 20px;
}

.unconfigured-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
}

.unconfigured-badge {
  background: rgba(99, 102, 241, 0.2);
  color: #818cf8;
  font-size: 0.72rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: var(--radius-pill);
}

.unconfigured-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-main);
}

.unconfigured-desc {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 12px;
}

.unconfigured-chips-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.unconfigured-sensor-chip {
  background: var(--surface-sunken);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill);
  color: var(--text-main);
  padding: 6px 14px;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.2s;
  display: inline-flex;
  align-items: center;
}

.unconfigured-sensor-chip:hover {
  background: var(--primary);
  border-color: var(--primary);
  transform: translateY(-1px);
}

/* ══════════════════════════════════════════════════════════════════
   GIAI ĐOẠN 3: DISCOVERY BANNER & ONBOARDING WIZARD
   ══════════════════════════════════════════════════════════════════ */
.discovery-banner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface-sunken);
  border: 1px solid var(--info);
  border-radius: 10px;
  padding: 12px 18px;
  margin-bottom: 20px;
  box-shadow: var(--shadow-card);
  animation: fadeInDown 0.3s ease-out;
}

.discovery-banner-content {
  display: flex;
  align-items: center;
  gap: 12px;
}

.discovery-badge {
  background: #3b82f6;
  color: #ffffff;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.discovery-text {
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--text);
}

.discovery-banner-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Wizard 3 bước (Phase 2 Overhaul) */
.wizard-modal-card {
  max-width: 680px;
  width: 95vw;
}

.wizard-steps-indicator {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border-subtle, rgba(255, 255, 255, 0.1));
}

.wizard-step-node {
  font-size: 0.85rem;
  color: var(--text-muted, #94a3b8);
  font-weight: 500;
  padding: 4px 8px;
  border-radius: 6px;
  transition: all 0.2s;
}

.wizard-step-node.active {
  color: var(--accent, #f97316);
  font-weight: 700;
  background: rgba(249, 115, 22, 0.12);
}

.wizard-device-type-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 6px;
}

@media (max-width: 600px) {
  .wizard-device-type-grid {
    grid-template-columns: 1fr;
  }
}

.wizard-type-card {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px;
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
  border: 2px solid var(--border-subtle);
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s, transform 0.15s;
  user-select: none;
}

.wizard-type-card:hover {
  border-color: var(--accent, #f97316);
  transform: translateY(-1px);
}

.wizard-type-card.active {
  border-color: var(--accent, #f97316);
  background: rgba(249, 115, 22, 0.08);
  box-shadow: 0 0 0 1px var(--accent, #f97316);
}

.wizard-type-icon {
  font-size: 1.8rem;
  line-height: 1;
  flex-shrink: 0;
}

.wizard-type-title {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--text-main);
  margin-bottom: 4px;
}

.wizard-type-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.35;
}

.wizard-topic-preview-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 6px;
  flex-wrap: wrap;
}

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

.wizard-topic-preview {
  background: var(--surface-sunken);
  border: 1px solid var(--border-subtle);
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  color: var(--accent, #f97316);
  font-family: monospace;
  font-size: 0.82rem;
}

.wizard-classes-header {
  margin-bottom: 6px;
}

.wizard-classes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(115px, 1fr));
  gap: 8px;
  max-height: 320px;
  overflow-y: auto;
  padding: 4px;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
}

.wizard-class-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px 6px;
  border-radius: var(--radius-sm);
  background: var(--card);
  border: 1px solid var(--border-subtle);
  cursor: pointer;
  text-align: center;
  transition: all 0.15s ease;
  user-select: none;
}

.wizard-class-item:hover {
  border-color: var(--accent, #f97316);
  transform: translateY(-1px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}

.wizard-class-item:active {
  transform: scale(0.96);
}

.wizard-class-item-icon {
  font-size: 1.35rem;
  line-height: 1;
}

.wizard-class-item-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-main);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.wizard-class-item-unit {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.wizard-sensors-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.wizard-sensors-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 240px;
  overflow-y: auto;
  padding-right: 4px;
}

.wizard-sensor-card {
  background: var(--surface-sunken);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.wizard-sensor-main-row {
  display: grid;
  grid-template-columns: 32px 1.6fr 1fr 32px;
  gap: 8px;
  align-items: center;
}

.wizard-sensor-icon-box {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
}

.wizard-sensor-thresh-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  background: var(--card);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
}

.wizard-sensor-thresh-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.wizard-sensor-thresh-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-muted);
}

.wizard-sensor-details {
  margin-top: 2px;
}

.wizard-sensor-details summary {
  font-size: 0.76rem;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
  font-weight: 500;
}

.wizard-sensor-details summary:hover {
  color: var(--accent, #f97316);
}

.wizard-sensor-advanced-grid {
  display: grid;
  grid-template-columns: 1.2fr 1fr 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
  padding: 8px 10px;
  background: var(--card);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
}

/* Review Step 3 */
.wizard-review-box {
  background: var(--surface-sunken);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 18px;
}

.wizard-review-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 14px;
}

@media (max-width: 600px) {
  .wizard-review-grid {
    grid-template-columns: 1fr;
  }
}

.wizard-review-cell {
  background: var(--card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.wizard-review-cell-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin-bottom: 4px;
}

.wizard-review-cell-val {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-main);
}

.wizard-review-sensors-title {
  font-size: 0.88rem;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 8px;
}

.wizard-review-sensors-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.wizard-review-sensor-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--card);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
}

.wizard-review-sensor-left {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}

.wizard-review-sensor-thresh {
  font-size: 0.78rem;
  color: var(--text-muted);
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}




/* ============================================================================
 * GIAI ĐOẠN 4.1 — STYLES: HASH ROUTING, TOPBAR, SENSORS/AREAS & DONUT
 * ============================================================================
 */

/* Màn hình (Hash screens) */
.app-screen {
  display: none;
}
.app-screen.active {
  display: block;
  animation: fadeIn 0.2s ease-in-out;
}

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

/* Tablet (768px - 1023px): sidebar 72px icon-only */
@media (max-width: 1023px) and (min-width: 768px) {
  .sidebar {
    width: 72px !important;
  }
  .sidebar .brand-info,
  .sidebar .nav-text,
  .sidebar .sidebar-footer {
    display: none !important;
  }
  .sidebar .sidebar-header {
    justify-content: center;
    padding: 16px 0;
  }
  .sidebar .nav-link {
    justify-content: center;
    padding: 14px 0;
  }
}

/* Mobile (< 768px): sidebar thành drawer trượt */
@media (max-width: 767px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 260px !important;
    z-index: 1000;
    transform: translateX(-100%);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
  }
  .sidebar.mobile-open,
  .sidebar.open {
    transform: translateX(0);
  }
  .sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 999;
  }
  .sidebar-backdrop.active {
    display: block;
  }
}

/* Topbar search & Theme button */
.btn-search-mobile {
  display: none;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px;
}

@media (max-width: 767px) {
  .search-box {
    display: none !important;
  }
  .btn-search-mobile {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

.search-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 1050;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}
.search-overlay input {
  flex: 1;
  background: var(--page);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 14px;
  color: var(--text);
  outline: none;
}
.search-overlay button {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
}

.btn-theme-toggle {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 6px 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.15s ease;
}
.btn-theme-toggle:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

/* SSE Indicator 3 trạng thái */
.status-indicator {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--card);
  border: 1px solid var(--border);
  font-size: 12px;
  font-weight: 500;
  color: var(--text);
}
.status-indicator .dot-pulse {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ok, #22C55E);
  box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7);
  animation: ssePulse 2s infinite;
}
.status-indicator.connecting .dot-pulse {
  background: var(--warn, #F59E0B);
  box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.7);
}
.status-indicator.disconnected .dot-pulse {
  background: var(--danger, #EF4444);
  box-shadow: none;
  animation: none;
}

@keyframes ssePulse {
  0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0.7); }
  70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(34, 197, 94, 0); }
  100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(34, 197, 94, 0); }
}

/* Overview Charts Grid (Chart 2/3 + Donut 1/3) */
.overview-charts-grid {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 24px;
  margin-top: 24px;
}
@media (max-width: 1023px) {
  .overview-charts-grid {
    grid-template-columns: 1fr;
  }
}

/* Device Status Donut */
.donut-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-card);
}
.donut-header {
  margin-bottom: 16px;
}
.donut-body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  gap: 16px;
  flex: 1;
}
.donut-svg-wrap {
  position: relative;
  width: 140px;
  height: 140px;
  flex-shrink: 0;
}
.donut-center-text {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.donut-center-num {
  font-size: 26px;
  font-weight: 700;
  color: var(--text);
  line-height: 1;
}
.donut-center-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  margin-top: 4px;
}
.donut-legend {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
}
.donut-legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text);
}
.donut-legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.donut-legend-val {
  margin-left: auto;
  font-weight: 600;
  color: var(--text-muted);
}

/* Khối Theo khu vực (Home Assistant style) */
.areas-section {
  margin-top: 24px;
}
.areas-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.area-block {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  margin-bottom: 24px;
  box-shadow: var(--shadow-card);
}
.area-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
.area-block-title {
  display: flex;
  align-items: center;
  gap: 10px;
}
.area-icon-wrap {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
}
.area-name {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}
.area-device-count {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.15);
  color: var(--text-muted);
}
.area-entities-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

/* Thẻ Entity */
.entity-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: relative;
  transition: transform 0.15s ease, box-shadow 0.15s ease, opacity 0.2s ease;
}
.entity-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.06);
}
.entity-card-offline {
  opacity: 0.6 !important;
}
.entity-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.entity-card-info {
  display: flex;
  align-items: center;
  gap: 10px;
}
.entity-card-icon-box {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(59, 130, 246, 0.1);
  color: var(--info);
  flex-shrink: 0;
}
.entity-card-icon-box.status-online {
  background: rgba(34, 197, 94, 0.1);
  color: var(--ok);
}
.entity-card-icon-box.status-delayed {
  background: rgba(245, 158, 11, 0.1);
  color: var(--warn);
}
.entity-card-icon-box.status-offline {
  background: rgba(239, 68, 68, 0.1);
  color: var(--danger);
}
.entity-card-icon-box.status-unconfigured {
  background: rgba(148, 163, 184, 0.1);
  color: var(--unconfigured);
}
.entity-card-titles {
  display: flex;
  flex-direction: column;
}
.entity-card-label {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.2;
}
.entity-card-device {
  font-size: 11px;
  color: var(--text-muted);
}
.entity-card-value-wrap {
  display: flex;
  align-items: baseline;
  gap: 4px;
  margin-top: 4px;
}
.entity-card-value {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}
.entity-card-unit {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}
.entity-card-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: auto;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  font-size: 11px;
  color: var(--text-muted);
}
.entity-status-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: capitalize;
}
.entity-status-badge.badge-online {
  background: rgba(34, 197, 94, 0.15);
  color: var(--ok);
}
.entity-status-badge.badge-delayed {
  background: rgba(245, 158, 11, 0.15);
  color: var(--warn);
}
.entity-status-badge.badge-offline {
  background: rgba(239, 68, 68, 0.15);
  color: var(--danger);
}
.entity-status-badge.badge-unconfigured {
  background: rgba(148, 163, 184, 0.15);
  color: var(--unconfigured);
}

/* Topbar Page Title */
.topbar-page-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  margin: 0 12px 0 0;
  white-space: nowrap;
}
@media (max-width: 767px) {
  .topbar-page-title {
    font-size: 15px;
  }
}

/* Screen Containers (Hash Routing) */
.screen-container {
  display: none;
}
.screen-container.active {
  display: block;
}

.screen-placeholder {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 48px 24px;
  text-align: center;
  box-shadow: var(--shadow-card);
  margin-top: 20px;
}
.screen-placeholder-icon {
  font-size: 48px;
  margin-bottom: 12px;
}
.screen-placeholder-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 8px;
}
.screen-placeholder-desc {
  font-size: 14px;
  color: var(--text-muted);
  max-width: 520px;
  margin: 0 auto 20px auto;
  line-height: 1.5;
}

/* Skeleton Loading */
.skeleton-box {
  background: linear-gradient(90deg, rgba(148, 163, 184, 0.1) 25%, rgba(148, 163, 184, 0.2) 37%, rgba(148, 163, 184, 0.1) 63%);
  background-size: 400% 100%;
  animation: skeletonShimmer 1.4s ease infinite;
  border-radius: 6px;
}
@keyframes skeletonShimmer {
  0% { background-position: 100% 50%; }
  100% { background-position: 0 50%; }
}

/* ==========================================================================
   GIAI ĐOẠN 4.2: QUICK VIEW DRAWER & 4 MÀN HÌNH CHỨC NĂNG
   ========================================================================== */

/* --- QUICK VIEW DRAWER --- */
.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1040;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}
.drawer-backdrop.active {
  opacity: 1;
  pointer-events: auto;
}

.quick-view-drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 420px;
  max-width: 100%;
  background: var(--card);
  border-left: 1px solid var(--border);
  box-shadow: -8px 0 32px rgba(0, 0, 0, 0.25);
  z-index: 1050;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
.quick-view-drawer.open {
  transform: translateX(0);
}

.drawer-header {
  padding: 20px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}
.drawer-header-left {
  display: flex;
  align-items: center;
  gap: 14px;
}
.drawer-icon {
  font-size: 28px;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
}
.drawer-titles-wrap {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.drawer-title {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
  margin: 0;
}
.drawer-sub {
  font-size: 13px;
  color: var(--text-muted);
}
.btn-close-drawer {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-muted);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  transition: all 0.15s ease;
}
.btn-close-drawer:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.drawer-body {
  padding: 24px;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.drawer-value-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}
.drawer-value-large {
  display: flex;
  align-items: baseline;
  gap: 6px;
}
#drawer-value-display {
  font-size: 32px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: -0.5px;
}
.drawer-unit {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-muted);
}

.drawer-section-title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.drawer-sparkline-box {
  height: 140px;
  background: var(--chart-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px;
}

.drawer-thresholds-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.drawer-thresh-item {
  padding: 12px;
  border-radius: var(--radius-md);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.drawer-thresh-item.thresh-warn {
  border-left: 3px solid var(--warn);
}
.drawer-thresh-item.thresh-crit {
  border-left: 3px solid var(--danger);
}
.thresh-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
}
.thresh-val {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  font-family: ui-monospace, monospace;
}

.drawer-relay-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}
.relay-state-text {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}
.relay-feedback-text {
  font-size: 12px;
  font-weight: 600;
  margin-top: 6px;
  color: var(--accent);
}

.drawer-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border);
  background: var(--card);
}
.drawer-link-detail {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 12px;
  font-size: 14px;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-soft);
  border-radius: var(--radius-md);
  text-decoration: none;
  transition: all 0.2s ease;
}
.drawer-link-detail:hover {
  background: var(--accent);
  color: #ffffff;
}

/* --- SCREEN 2: THIẾT BỊ (#/devices) --- */
.table-controls {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}
.table-loading-skeleton {
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.skeleton-row {
  height: 48px;
  border-radius: var(--radius-md);
  background: linear-gradient(90deg, rgba(148, 163, 184, 0.08) 25%, rgba(148, 163, 184, 0.18) 37%, rgba(148, 163, 184, 0.08) 63%);
  background-size: 400% 100%;
  animation: skeletonShimmer 1.4s ease infinite;
}
.state-card-error {
  padding: 24px;
  background: rgba(239, 68, 68, 0.08);
  border: 1px solid rgba(239, 68, 68, 0.25);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 16px;
}
.state-error-icon {
  font-size: 20px;
}
.state-error-msg {
  font-size: 14px;
  font-weight: 600;
  color: var(--danger);
}
.btn-cta-empty {
  margin-top: 16px;
}

/* --- SCREEN: CHI TIẾT THIẾT BỊ (#/device/<id>) --- */
.detail-top-nav {
  margin-bottom: 16px;
}
.btn-link-back {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
  padding: 6px 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  transition: all 0.15s ease;
}
.btn-link-back:hover {
  background: var(--accent-soft);
}

.device-detail-header-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 20px;
  box-shadow: var(--shadow-card);
}
.detail-header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}
.detail-device-icon {
  font-size: 32px;
  width: 56px;
  height: 56px;
  border-radius: var(--radius-lg);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
}
.detail-title-row {
  display: flex;
  align-items: center;
  gap: 12px;
}
.detail-device-name {
  font-size: 22px;
  font-weight: 800;
  color: var(--text);
  margin: 0;
}
.detail-meta-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 4px;
}
.detail-meta-row code {
  font-family: ui-monospace, monospace;
  background: var(--surface-sunken);
  padding: 2px 6px;
  border-radius: 4px;
}
.detail-meta-sep {
  opacity: 0.5;
}
.detail-header-right {
  display: flex;
  align-items: center;
  gap: 14px;
}
.form-label-inline {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin-right: 6px;
}
.select-sm {
  padding: 6px 10px;
  font-size: 13px;
}
.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
  border-radius: 8px;
}

.detail-sensors-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
  margin-top: 16px;
}

.detail-sensor-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: var(--shadow-card);
  transition: transform 0.15s ease, border-color 0.15s ease;
  cursor: pointer;
}
.detail-sensor-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
}
.detail-sensor-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}

.detail-sensor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.detail-sensor-info {
  display: flex;
  align-items: center;
  gap: 10px;
}
.detail-sensor-icon {
  font-size: 20px;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: var(--surface-sunken);
  display: flex;
  align-items: center;
  justify-content: center;
}
.detail-sensor-label {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}
.detail-sensor-key {
  font-size: 12px;
  color: var(--text-muted);
}
.detail-sensor-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.detail-sensor-body {
  display: flex;
  align-items: baseline;
  gap: 6px;
}
.detail-sensor-val {
  font-size: 24px;
  font-weight: 800;
  color: var(--text);
}
.detail-sensor-unit {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.detail-sensor-sparkline {
  height: 50px;
  background: var(--chart-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px;
}

.detail-relay-control-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}
.relay-state-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
}
.relay-state-sub {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 2px;
}
.relay-feedback-pill {
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  font-size: 12px;
  font-weight: 700;
  background: var(--accent-soft);
  color: var(--accent);
}

/* --- SCREEN 3: LỊCH SỬ (#/history) --- */
.history-controls-card {
  padding: 20px;
  background: var(--surface-sunken);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  margin-bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.history-device-picker {
  display: flex;
  flex-direction: column;
  max-width: 320px;
}
.history-picker-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.history-checkbox-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
}
.history-check-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.15s ease;
}
.history-check-item:hover {
  border-color: var(--accent);
}
.history-check-item.selected {
  border-color: var(--accent);
  background: var(--accent-soft);
}
.history-check-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.history-chart-wrapper {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-card);
}
.history-legend-bar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding-bottom: 16px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}
.history-legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  padding: 4px 10px;
  border-radius: var(--radius-pill);
  background: var(--surface-sunken);
  border: 1px solid var(--border);
}
.history-legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}
.history-legend-val {
  font-family: ui-monospace, monospace;
  color: var(--accent);
}

/* --- RESPONSIVE DƯỚI 768PX --- */
@media (max-width: 767px) {
  .table-controls {
    flex-direction: column;
    align-items: stretch;
  }
  .table-controls select,
  .table-controls .search-input-wrap,
  .table-controls button {
    width: 100% !important;
  }

  .device-detail-header-card {
    flex-direction: column;
    align-items: flex-start;
  }
  .detail-header-right {
    width: 100%;
    justify-content: space-between;
  }

  .detail-relay-control-box {
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
  }
  .detail-relay-control-box button {
    width: 100%;
  }

  /* Table chuyển sang Card List trên Mobile */
  .data-table-responsive thead {
    display: none;
  }
  .data-table-responsive,
  .data-table-responsive tbody,
  .data-table-responsive tr,
  .data-table-responsive td {
    display: block;
    width: 100%;
  }
  .data-table-responsive tr {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: var(--shadow-card);
  }
  .data-table-responsive td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 8px 0;
    border-bottom: 1px solid var(--border);
  }
  .data-table-responsive td:last-child {
    border-bottom: none;
    padding-top: 12px;
  }
  .data-table-responsive td::before {
    content: attr(data-label);
    font-size: 12px;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
  }
}

/* Phase 3.3: Empty State Guide Styling */
.empty-state-guide {
  margin-top: 10px;
  border: 1px dashed var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 8px 10px;
  background: var(--surface-sunken);
  font-size: 12px;
  text-align: left;
}

.empty-state-guide summary {
  cursor: pointer;
  font-weight: 600;
  color: var(--accent);
  outline: none;
  user-select: none;
}

.empty-state-guide-content {
  margin-top: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.empty-state-guide-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.empty-state-guide-label {
  color: var(--text-muted);
  font-size: 11px;
  white-space: nowrap;
  flex-shrink: 0;
}

.empty-state-guide-row code {
  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace);
  font-size: 11px;
  background: rgba(0, 0, 0, 0.2);
  padding: 3px 6px;
  border-radius: 4px;
  word-break: break-all;
  flex: 1 1 auto;
}

.empty-state-no-data-sub {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  margin-top: 2px;
}

