/* ============================================
   EnergySense — Modern Dark Mode Dashboard
   Color palette from project_setup.json
   ============================================ */

:root {
  --primary-dark: #131C3D;
  --card-background: #1A254D;
  --card-background-hover: #1f2d5c;
  --accent: #00CCFF;
  --accent-dim: rgba(0, 204, 255, 0.15);
  --secondary-accent: #FFC107;
  --text-primary: #FFFFFF;
  --text-secondary: #B0C4DE;
  --status-alert: #FF4D4D;
  --status-capped: #FFC107;
  --status-green: #00E676;
  --border-color: rgba(176, 196, 222, 0.12);
  --border-accent: rgba(0, 204, 255, 0.25);
  --sidebar-width: 240px;
  --header-height: 64px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-card: 0 4px 24px rgba(0, 0, 0, 0.25);
  --shadow-glow: 0 0 20px rgba(0, 204, 255, 0.08);
  --transition: 0.2s ease;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
}

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: 16px;
  scroll-behavior: smooth;
}

body {
  font-family: var(--font-sans);
  background: var(--primary-dark);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 50% at 20% -10%, rgba(0, 204, 255, 0.06) 0%, transparent 50%),
    radial-gradient(ellipse 60% 40% at 90% 100%, rgba(255, 193, 7, 0.04) 0%, transparent 50%);
  pointer-events: none;
  z-index: 0;
}

/* ---- App Shell ---- */
.app-shell {
  display: flex;
  min-height: 100vh;
  position: relative;
  z-index: 1;
}

/* ---- Sidebar ---- */
.sidebar {
  width: var(--sidebar-width);
  background: linear-gradient(180deg, #151f42 0%, var(--primary-dark) 100%);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  z-index: 200;
  transition: transform var(--transition);
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 20px 24px;
  border-bottom: 1px solid var(--border-color);
}

.brand-icon {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, var(--accent), #0099cc);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 16px rgba(0, 204, 255, 0.3);
}

.brand-icon svg {
  width: 20px;
  height: 20px;
  color: var(--primary-dark);
}

.brand-text {
  font-weight: 700;
  font-size: 1.125rem;
  letter-spacing: -0.02em;
}

.sidebar-nav {
  flex: 1;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all var(--transition);
  position: relative;
}

.nav-item:hover {
  background: rgba(0, 204, 255, 0.08);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--accent-dim);
  color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}

.nav-icon svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.sidebar-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border-color);
}

.apl-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--status-capped);
  background: rgba(255, 193, 7, 0.1);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255, 193, 7, 0.2);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-dot.capped {
  background: var(--status-capped);
  box-shadow: 0 0 8px var(--status-capped);
  animation: pulse-dot 2s infinite;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.6; transform: scale(0.85); }
}

/* ---- Main Wrapper ---- */
.main-wrapper {
  flex: 1;
  margin-left: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ---- Header ---- */
.top-header {
  height: var(--header-height);
  background: rgba(26, 37, 77, 0.6);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.menu-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-sm);
}

.menu-toggle:hover {
  background: var(--accent-dim);
  color: var(--accent);
}

.menu-toggle svg {
  width: 22px;
  height: 22px;
}

.header-logo {
  display: none;
  align-items: center;
  gap: 8px;
}

.header-logo-icon svg {
  width: 20px;
  height: 20px;
  color: var(--accent);
}

.header-logo-text {
  font-weight: 700;
  font-size: 1rem;
}

.search-bar {
  flex: 1;
  max-width: 420px;
  position: relative;
}

.search-bar input {
  width: 100%;
  background: rgba(19, 28, 61, 0.8);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 10px 14px 10px 40px;
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 0.875rem;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.search-bar input::placeholder {
  color: rgba(176, 196, 222, 0.5);
}

.search-bar input:focus {
  outline: none;
  border-color: var(--border-accent);
  box-shadow: 0 0 0 3px rgba(0, 204, 255, 0.1);
}

.search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  width: 18px;
  height: 18px;
  color: var(--text-secondary);
  pointer-events: none;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-left: auto;
}

.icon-btn {
  background: rgba(19, 28, 61, 0.8);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  cursor: pointer;
  padding: 8px;
  position: relative;
  transition: all var(--transition);
}

.icon-btn:hover {
  border-color: var(--border-accent);
  color: var(--accent);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
  display: block;
}

.notification-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  background: var(--status-alert);
  color: white;
  font-size: 0.625rem;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.notification-badge[hidden] {
  display: none;
}

.notification-list {
  max-height: 320px;
  overflow-y: auto;
}

.notification-empty {
  padding: 18px 16px;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  text-align: center;
}

.notification-item .notif-time {
  display: block;
  margin-top: 4px;
  font-size: 0.6875rem;
  color: var(--text-secondary);
  opacity: 0.85;
}

.notification-wrapper {
  position: relative;
}

.notification-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 320px;
  background: var(--card-background);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-card);
  opacity: 0;
  visibility: hidden;
  transform: translateY(-8px);
  transition: all var(--transition);
  z-index: 300;
}

.notification-dropdown.open {
  opacity: 1;
  visibility: visible;
  transform: translateY(0);
}

.notification-header {
  padding: 14px 16px;
  font-weight: 600;
  font-size: 0.875rem;
  border-bottom: 1px solid var(--border-color);
}

.notification-item {
  display: flex;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-color);
  font-size: 0.8125rem;
}

.notification-item:last-child {
  border-bottom: none;
}

.notification-item strong {
  display: block;
  margin-bottom: 2px;
}

.notification-item p {
  color: var(--text-secondary);
  font-size: 0.75rem;
}

.notif-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-top: 5px;
  flex-shrink: 0;
}

.notif-dot.alert { background: var(--status-alert); }
.notif-dot.info { background: var(--accent); }

.user-profile {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-left: 16px;
  border-left: 1px solid var(--border-color);
}

.user-info {
  text-align: right;
}

.user-name {
  display: block;
  font-size: 0.875rem;
  font-weight: 600;
}

.user-role {
  display: block;
  font-size: 0.6875rem;
  color: var(--text-secondary);
}

.avatar {
  width: 38px;
  height: 38px;
  background: linear-gradient(135deg, var(--accent), #0066aa);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.875rem;
  box-shadow: 0 0 12px rgba(0, 204, 255, 0.25);
}

/* ---- Dashboard ---- */
.dashboard {
  padding: 24px;
  flex: 1;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24px;
}

.page-header h1 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.page-subtitle {
  color: var(--text-secondary);
  font-size: 0.875rem;
  margin-top: 4px;
}

.live-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--status-green);
  background: rgba(0, 230, 118, 0.1);
  padding: 6px 14px;
  border-radius: 20px;
  border: 1px solid rgba(0, 230, 118, 0.2);
}

.live-dot {
  width: 8px;
  height: 8px;
  background: var(--status-green);
  border-radius: 50%;
  animation: pulse-dot 1.5s infinite;
}

/* ---- Metrics Row ---- */
.metrics-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 20px;
}

.metric-card {
  background: var(--card-background);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 20px;
  transition: all var(--transition);
  position: relative;
  overflow: hidden;
}

.metric-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  opacity: 0;
  transition: opacity var(--transition);
}

.metric-card:hover {
  border-color: var(--border-accent);
  box-shadow: var(--shadow-glow);
  transform: translateY(-2px);
}

.metric-card:hover::before {
  opacity: 1;
}

.metric-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.metric-label {
  font-size: 0.8125rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.metric-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.6875rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 4px;
}

.metric-indicator svg {
  width: 12px;
  height: 12px;
}

.metric-indicator.blue {
  color: var(--accent);
  background: var(--accent-dim);
}

.metric-indicator.green {
  color: var(--status-green);
  background: rgba(0, 230, 118, 0.12);
}

.metric-indicator.teal {
  color: var(--accent);
  background: var(--accent-dim);
}

.metric-value {
  font-size: 1.75rem;
  font-weight: 700;
  font-family: var(--font-mono);
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.metric-value .unit {
  font-size: 1rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.metric-value.capped-text {
  font-size: 1.25rem;
  color: var(--status-capped);
}

#gridStatusCard.status-normal .metric-value {
  color: var(--status-green);
}

#gridStatusCard.status-capped .metric-value {
  color: var(--status-capped);
}

#gridStatusCard.status-alarm .metric-value {
  color: var(--status-alert);
}

.metric-value.savings {
  color: var(--accent);
}

.metric-target {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

.metric-desc {
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 6px;
}

.sparkline {
  width: 100%;
  height: 40px;
  margin-top: 12px;
}

.status-light {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(255, 193, 7, 0.15);
  color: var(--status-capped);
}

.status-light.pulse {
  animation: status-pulse 2s infinite;
}

.status-light svg {
  width: 14px;
  height: 14px;
}

@keyframes status-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 193, 7, 0.4); }
  50% { box-shadow: 0 0 0 8px rgba(255, 193, 7, 0); }
}

.comparison-bar {
  height: 4px;
  background: rgba(176, 196, 222, 0.15);
  border-radius: 2px;
  margin-top: 12px;
  overflow: hidden;
}

.comparison-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #00E676);
  border-radius: 2px;
  transition: width 0.6s ease;
}

/* ---- Overview Row ---- */
.overview-row {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 20px;
  margin-bottom: 20px;
}

.chart-panel,
.device-panel,
.bar-chart-section {
  background: var(--card-background);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 20px;
  box-shadow: var(--shadow-card);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 12px;
}

.panel-header h2 {
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
}

.panel-subtitle {
  display: block;
  font-size: 0.75rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

.time-filters {
  display: flex;
  gap: 4px;
  background: rgba(19, 28, 61, 0.6);
  padding: 3px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-color);
}

.filter-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 500;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  transition: all var(--transition);
}

.filter-btn:hover {
  color: var(--text-primary);
}

.filter-btn.active {
  background: var(--accent);
  color: var(--primary-dark);
  font-weight: 600;
}

.chart-container {
  position: relative;
  height: 320px;
  min-height: 320px;
  max-height: 320px;
  overflow: hidden;
}

.bar-chart-container {
  position: relative;
  height: 280px;
  min-height: 280px;
  max-height: 280px;
  overflow: hidden;
}

/* ---- Device Panel ---- */
.global-controls {
  background: rgba(19, 28, 61, 0.5);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 16px;
  margin-bottom: 16px;
}

.control-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.control-row + .control-row {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border-color);
}

.slider-row {
  flex-direction: column;
  align-items: stretch;
}

.control-label span {
  display: block;
  font-size: 0.8125rem;
  font-weight: 600;
}

.control-label small {
  display: block;
  font-size: 0.6875rem;
  color: var(--accent);
  font-family: var(--font-mono);
  margin-top: 2px;
}

.slider-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.625rem;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  margin-top: 4px;
}

/* ---- Toggle Switch ---- */
.toggle-switch {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.toggle-slider {
  position: relative;
  width: 44px;
  height: 24px;
  background: rgba(176, 196, 222, 0.2);
  border-radius: 12px;
  transition: background var(--transition);
  flex-shrink: 0;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  top: 3px;
  background: var(--text-secondary);
  border-radius: 50%;
  transition: transform var(--transition), background var(--transition), box-shadow var(--transition);
}

.toggle-switch input:checked + .toggle-slider {
  background: rgba(0, 204, 255, 0.3);
  box-shadow: inset 0 0 8px rgba(0, 204, 255, 0.2);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(20px);
  background: var(--accent);
  box-shadow: 0 0 10px rgba(0, 204, 255, 0.5);
}

.toggle-switch input:focus-visible + .toggle-slider {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.toggle-state {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent);
  font-family: var(--font-mono);
}

.toggle-switch.compact {
  gap: 8px;
}

.toggle-switch.compact .toggle-slider {
  width: 36px;
  height: 20px;
}

.toggle-switch.compact .toggle-slider::before {
  width: 14px;
  height: 14px;
}

.toggle-switch.compact input:checked + .toggle-slider::before {
  transform: translateX(16px);
}

.toggle-label {
  font-size: 0.75rem;
  font-weight: 600;
  font-family: var(--font-mono);
  min-width: 28px;
}

.toggle-label.on { color: var(--accent); }
.toggle-label.off { color: var(--text-secondary); }

/* ---- Cap Slider ---- */
.cap-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  background: rgba(176, 196, 222, 0.15);
  border-radius: 3px;
  outline: none;
  margin-top: 8px;
}

.cap-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 0 12px rgba(0, 204, 255, 0.4);
  transition: transform var(--transition);
}

.cap-slider::-webkit-slider-thumb:hover {
  transform: scale(1.15);
}

.cap-slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  background: var(--accent);
  border: none;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 0 12px rgba(0, 204, 255, 0.4);
}

/* ---- Device Table ---- */
.device-table-wrapper {
  overflow-x: auto;
}

.device-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.8125rem;
}

.device-table thead th {
  text-align: left;
  padding: 10px 8px;
  color: var(--text-secondary);
  font-weight: 500;
  font-size: 0.6875rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border-color);
}

.device-table tbody tr {
  transition: background var(--transition);
}

.device-table tbody tr:hover {
  background: rgba(0, 204, 255, 0.04);
}

.device-table tbody td {
  padding: 14px 8px;
  border-bottom: 1px solid var(--border-color);
  vertical-align: middle;
}

.device-table tbody tr:last-child td {
  border-bottom: none;
}

.device-icon svg {
  width: 22px;
  height: 22px;
  color: var(--accent);
  opacity: 0.8;
}

.device-name {
  font-weight: 600;
}

.load-value {
  font-family: var(--font-mono);
  font-weight: 500;
  color: var(--accent);
}

.load-value.muted {
  color: var(--text-secondary);
  opacity: 0.6;
}

.status-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 4px;
  font-size: 0.6875rem;
  font-weight: 600;
  font-family: var(--font-mono);
}

.status-badge.essential {
  background: rgba(0, 230, 118, 0.12);
  color: var(--status-green);
  border: 1px solid rgba(0, 230, 118, 0.2);
}

.priority-badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 0.625rem;
  font-weight: 600;
}

.priority-badge.p1 {
  background: rgba(0, 230, 118, 0.1);
  color: var(--status-green);
}

.priority-badge.p3 {
  background: rgba(255, 193, 7, 0.1);
  color: var(--status-capped);
}

/* ---- Bar Chart Section ---- */
.bar-chart-section {
  margin-bottom: 24px;
}

.breach-alert {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--status-alert);
  background: rgba(255, 77, 77, 0.1);
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(255, 77, 77, 0.25);
}

.breach-alert svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* ---- Dashboard layout stability (CLS prevention) ---- */
.metrics-row-stable {
  align-items: stretch;
}

.metric-card-stable {
  display: flex;
  flex-direction: column;
  min-height: 148px;
}

.metric-card-stable .metric-top {
  min-height: 28px;
  flex-shrink: 0;
}

.metric-card-stable .metric-value-stable {
  flex-shrink: 0;
}

.metric-card-stable.metric-card-compact {
  min-height: 124px;
}

.metric-value-stable {
  display: block;
  min-height: 2.125rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.metric-indicator-stable {
  flex-shrink: 0;
  min-width: 5.5rem;
  justify-content: flex-end;
}

#powerTrendValue {
  display: inline-block;
  min-width: 4.25rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.metric-target-stable {
  min-height: 1.125rem;
  white-space: nowrap;
}

.metric-desc-stable {
  min-height: 1.125rem;
  margin-top: auto;
}

.sparkline-stable {
  display: block;
  width: 100%;
  height: 40px;
  min-height: 40px;
  max-height: 40px;
  margin-top: 12px;
  flex-shrink: 0;
}

.layout-stable {
  align-items: stretch;
  contain: layout style;
}

#row_overview_and_controls .chart-panel-stable,
#row_overview_and_controls .device-panel-stable {
  display: flex;
  flex-direction: column;
  min-height: 480px;
}

#row_overview_and_controls .chart-panel-stable .panel-header-stable {
  flex: 0 0 auto;
  min-height: 72px;
  margin-bottom: 16px;
  flex-wrap: nowrap;
  align-items: center;
}

#row_overview_and_controls .device-panel-stable .panel-header-compact {
  flex: 0 0 auto;
  min-height: 36px;
  margin-bottom: 16px;
}

.panel-header-copy {
  flex: 1 1 auto;
  min-width: 0;
}

.chart-container-fixed {
  position: relative;
  flex: 0 0 320px;
  height: 320px;
  min-height: 320px;
  max-height: 320px;
  width: 100%;
  overflow: hidden;
  contain: strict;
}

.bar-chart-container-fixed {
  flex: 0 0 280px;
  height: 280px;
  min-height: 280px;
  max-height: 280px;
  position: relative;
  overflow: hidden;
  contain: layout style;
}

.chart-container-fixed canvas,
.bar-chart-container-fixed canvas {
  display: block !important;
  width: 100% !important;
  height: 100% !important;
  max-width: 100%;
}

.global-controls-stable {
  flex: 0 0 auto;
  min-height: 148px;
}

.control-row-stable {
  min-height: 44px;
}

.toggle-state-stable {
  min-width: 7rem;
  white-space: nowrap;
}

.cap-value-stable {
  display: block;
  min-height: 1rem;
  min-width: 14rem;
  white-space: nowrap;
}

.device-table-wrapper-stable {
  flex: 1 1 auto;
  min-height: 200px;
  max-height: 320px;
  overflow-y: auto;
  overflow-x: auto;
}

#row_overview_and_controls .device-table tbody tr {
  height: 52px;
}

#row_overview_and_controls .device-table td:nth-child(3) {
  min-width: 7.5rem;
}

.load-value-stable {
  display: inline-block;
  min-width: 4.75rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.bar-chart-section-stable .bar-chart-header-stable {
  min-height: 52px;
  flex-wrap: nowrap;
  align-items: center;
}

.breach-alert-stable {
  flex-shrink: 0;
  min-height: 32px;
  max-width: min(320px, 42vw);
}

.breach-alert-text-stable {
  display: inline-block;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  vertical-align: middle;
}

/* ---- SPA Page Sections ---- */
.page-content {
  flex: 1;
}

.page-section {
  display: none;
  animation: fadeIn 0.3s ease;
}

.page-section.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---- Placeholder Pages ---- */
.placeholder-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  min-height: 400px;
  background: var(--card-background);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 48px 24px;
}

.placeholder-icon {
  width: 64px;
  height: 64px;
  background: var(--accent-dim);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 20px;
}

.placeholder-icon svg {
  width: 28px;
  height: 28px;
  color: var(--accent);
}

.placeholder-page h2 {
  font-size: 1.25rem;
  margin-bottom: 8px;
}

.placeholder-page p {
  color: var(--text-secondary);
  font-size: 0.875rem;
  max-width: 360px;
}

/* ---- Device Controls Page ---- */
.device-stats-bar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.stat-pill {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--card-background);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 10px 20px;
  min-width: 80px;
}

.stat-pill.accent {
  border-color: var(--border-accent);
  background: var(--accent-dim);
}

.stat-pill .stat-value {
  font-family: var(--font-mono);
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--accent);
}

.stat-pill .stat-label {
  font-size: 0.6875rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 2px;
}

.appliance-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.appliance-card {
  background: var(--card-background);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 20px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}

.appliance-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent), transparent);
  opacity: 0;
  transition: opacity 0.3s ease;
}

.appliance-card.is-on::before {
  opacity: 1;
}

.appliance-card.is-off {
  opacity: 0.85;
}

.appliance-card.is-off .appliance-power {
  color: var(--text-secondary);
  opacity: 0.5;
}

.appliance-card.is-locked {
  border-color: rgba(0, 230, 118, 0.2);
}

.appliance-card:hover {
  border-color: var(--border-accent);
  box-shadow: var(--shadow-glow);
  transform: translateY(-2px);
}

.appliance-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
}

.appliance-icon-wrap {
  width: 48px;
  height: 48px;
  background: var(--accent-dim);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.3s ease;
}

.appliance-card.is-on .appliance-icon-wrap {
  background: rgba(0, 204, 255, 0.2);
  box-shadow: 0 0 16px rgba(0, 204, 255, 0.15);
}

.appliance-card.is-off .appliance-icon-wrap {
  background: rgba(176, 196, 222, 0.08);
}

.appliance-icon-wrap svg {
  width: 24px;
  height: 24px;
  color: var(--accent);
  transition: color 0.3s ease;
}

.appliance-card.is-off .appliance-icon-wrap svg {
  color: var(--text-secondary);
}

.appliance-card.is-locked .appliance-icon-wrap {
  background: rgba(0, 230, 118, 0.12);
}

.appliance-card.is-locked .appliance-icon-wrap svg {
  color: var(--status-green);
}

.appliance-status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.appliance-status-dot.on {
  background: var(--status-green);
  box-shadow: 0 0 8px var(--status-green);
  animation: pulse-dot 2s infinite;
}

.appliance-status-dot.off {
  background: var(--text-secondary);
  opacity: 0.4;
}

.appliance-name {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 4px;
  letter-spacing: -0.01em;
}

.appliance-power {
  font-family: var(--font-mono);
  font-size: 1.375rem;
  font-weight: 700;
  color: var(--accent);
  margin-bottom: 12px;
  transition: color 0.3s ease, opacity 0.3s ease;
}

.appliance-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 14px;
  border-top: 1px solid var(--border-color);
}

.priority-badge.p2 {
  background: rgba(77, 166, 255, 0.12);
  color: #4DA6FF;
}

.appliance-toggle-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.appliance-toggle-row .toggle-label {
  font-size: 0.8125rem;
  min-width: 32px;
}

.locked-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.6875rem;
  font-weight: 600;
  font-family: var(--font-mono);
  color: var(--status-green);
  background: rgba(0, 230, 118, 0.1);
  padding: 6px 12px;
  border-radius: 20px;
  border: 1px solid rgba(0, 230, 118, 0.2);
}

.locked-badge svg {
  width: 12px;
  height: 12px;
}

/* Toggle loading state */
.toggle-switch.is-loading .toggle-slider {
  opacity: 0.6;
  pointer-events: none;
}

.toggle-switch.is-loading .toggle-slider::before {
  animation: toggle-pulse 0.8s ease infinite;
}

@keyframes toggle-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(0, 204, 255, 0.4); }
  50% { box-shadow: 0 0 0 6px rgba(0, 204, 255, 0); }
}

.appliance-card.is-pending {
  pointer-events: none;
}

.appliance-card.is-pending .toggle-slider::before {
  animation: toggle-pulse 0.8s ease infinite;
}

/* ---- Toast Notifications ---- */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 10px;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card-background);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 14px 18px;
  min-width: 320px;
  max-width: 420px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4), var(--shadow-glow);
  pointer-events: auto;
  transform: translateX(120%);
  opacity: 0;
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.3s ease;
}

.toast.show {
  transform: translateX(0);
  opacity: 1;
}

.toast.hide {
  transform: translateX(120%);
  opacity: 0;
}

.toast-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.toast-icon.loading {
  background: var(--accent-dim);
}

.toast-icon.success {
  background: rgba(0, 230, 118, 0.15);
}

.toast-icon svg {
  width: 18px;
  height: 18px;
}

.toast-icon.loading svg {
  color: var(--accent);
  animation: spin 1s linear infinite;
}

.toast-icon.success svg {
  color: var(--status-green);
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.toast-body {
  flex: 1;
}

.toast-title {
  font-size: 0.8125rem;
  font-weight: 600;
  margin-bottom: 2px;
}

.toast-message {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.toast.toast-success {
  border-color: rgba(0, 230, 118, 0.3);
}

.toast.toast-loading {
  border-color: var(--border-accent);
}

.toast.toast-warning {
  border-color: rgba(255, 193, 7, 0.35);
}

.toast-icon.warning {
  background: rgba(255, 193, 7, 0.15);
}

.toast-icon.warning svg {
  color: var(--status-capped);
}

.toast.toast-danger {
  border-color: rgba(255, 77, 77, 0.45);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4), 0 0 20px rgba(255, 77, 77, 0.18);
}

.toast-icon.danger {
  background: rgba(255, 77, 77, 0.15);
}

.toast-icon.danger svg {
  color: var(--status-alert);
}

.toast-container.toast-container-alert {
  top: 24px;
  bottom: auto;
  right: 24px;
}

/* ---- Analytics Page ---- */
.analytics-metrics-row {
  grid-template-columns: repeat(3, 1fr);
  margin-bottom: 20px;
}

.analytics-charts-row {
  display: grid;
  grid-template-columns: 3fr 2fr;
  gap: 20px;
}

.analytics-bar-container {
  position: relative;
  height: 320px;
  min-height: 320px;
  max-height: 320px;
  overflow: hidden;
  contain: layout style;
}

.analytics-doughnut-container {
  position: relative;
  height: 320px;
  min-height: 320px;
  max-height: 320px;
  overflow: hidden;
  contain: layout style;
}

.analytics-bar-container canvas,
.analytics-doughnut-container canvas {
  display: block !important;
  width: 100% !important;
  height: 100% !important;
  max-width: 100%;
}

/* ---- Cost Reports Page ---- */
.reports-metrics-row {
  grid-template-columns: repeat(3, 1fr);
  margin-bottom: 20px;
}

.reports-chart-panel {
  margin-bottom: 20px;
}

.reports-line-container {
  position: relative;
  height: 300px;
  min-height: 300px;
  max-height: 300px;
  overflow: hidden;
  contain: layout style;
}

.reports-line-container canvas {
  display: block !important;
  width: 100% !important;
  height: 100% !important;
  max-width: 100%;
}

.reports-table-section {
  background: var(--card-background);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 20px;
  box-shadow: var(--shadow-card);
}

.reports-table .txn-time {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-secondary);
  white-space: nowrap;
}

.txn-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 4px;
  font-size: 0.6875rem;
  font-weight: 600;
  font-family: var(--font-mono);
  white-space: nowrap;
}

.txn-badge.apl {
  background: rgba(255, 193, 7, 0.12);
  color: var(--status-capped);
  border: 1px solid rgba(255, 193, 7, 0.2);
}

.txn-badge.alert {
  background: rgba(255, 77, 77, 0.12);
  color: var(--status-alert);
  border: 1px solid rgba(255, 77, 77, 0.2);
}

.txn-badge.cost {
  background: rgba(0, 204, 255, 0.12);
  color: var(--accent);
  border: 1px solid rgba(0, 204, 255, 0.2);
}

.txn-badge.save {
  background: rgba(0, 230, 118, 0.12);
  color: var(--status-green);
  border: 1px solid rgba(0, 230, 118, 0.2);
}

.txn-badge.device {
  background: rgba(77, 166, 255, 0.12);
  color: #4DA6FF;
  border: 1px solid rgba(77, 166, 255, 0.2);
}

.savings-text {
  color: var(--status-green) !important;
}

/* ---- System Settings Page ---- */
.settings-layout {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.settings-card {
  background: var(--card-background);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 24px;
  box-shadow: var(--shadow-card);
  transition: border-color var(--transition);
}

.settings-card:hover {
  border-color: var(--border-accent);
}

.settings-card.hardware-card {
  grid-column: 1 / -1;
}

.settings-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.settings-card-header h2 {
  font-size: 1rem;
  font-weight: 600;
}

.settings-card-icon {
  width: 36px;
  height: 36px;
  background: var(--accent-dim);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
}

.settings-card-icon svg {
  width: 18px;
  height: 18px;
  color: var(--accent);
}

.settings-desc {
  font-size: 0.8125rem;
  color: var(--text-secondary);
  margin-bottom: 16px;
  line-height: 1.5;
}

.profile-block {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
}

.profile-avatar-lg {
  width: 56px;
  height: 56px;
  background: linear-gradient(135deg, var(--accent), #0066aa);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.25rem;
  box-shadow: 0 0 16px rgba(0, 204, 255, 0.25);
}

.profile-name {
  display: block;
  font-size: 1.125rem;
  font-weight: 600;
}

.profile-role {
  display: block;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

.btn-secondary {
  background: var(--accent-dim);
  border: 1px solid var(--border-accent);
  color: var(--accent);
  font-family: var(--font-sans);
  font-size: 0.8125rem;
  font-weight: 600;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
  width: 100%;
}

.btn-secondary:hover {
  background: rgba(0, 204, 255, 0.25);
  box-shadow: var(--shadow-glow);
}

.radio-group {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.radio-option {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 16px;
  background: rgba(19, 28, 61, 0.5);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
}

.radio-option:hover {
  border-color: var(--border-accent);
  background: rgba(0, 204, 255, 0.04);
}

.radio-option input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.radio-custom {
  width: 18px;
  height: 18px;
  border: 2px solid var(--text-secondary);
  border-radius: 50%;
  flex-shrink: 0;
  margin-top: 2px;
  position: relative;
  transition: all var(--transition);
}

.radio-option input:checked ~ .radio-custom {
  border-color: var(--accent);
  background: var(--accent);
  box-shadow: 0 0 10px rgba(0, 204, 255, 0.4);
}

.radio-option input:checked ~ .radio-custom::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 6px;
  height: 6px;
  background: var(--primary-dark);
  border-radius: 50%;
}

.radio-option input:checked ~ .radio-text strong {
  color: var(--accent);
}

.radio-text strong {
  display: block;
  font-size: 0.875rem;
  margin-bottom: 2px;
}

.radio-text small {
  display: block;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.settings-toggles {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.settings-toggle-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 0;
  border-bottom: 1px solid var(--border-color);
}

.settings-toggle-row:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.settings-toggle-label strong {
  display: block;
  font-size: 0.875rem;
  margin-bottom: 2px;
}

.settings-toggle-label small {
  display: block;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.hardware-status-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(19, 28, 61, 0.5);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 20px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 16px;
}

.hardware-status-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.hardware-icon-wrap {
  width: 48px;
  height: 48px;
  background: rgba(0, 230, 118, 0.12);
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
}

.hardware-icon-wrap svg {
  width: 24px;
  height: 24px;
  color: var(--status-green);
}

.hardware-name {
  display: block;
  font-size: 1rem;
  font-weight: 600;
}

.hardware-meta {
  display: block;
  font-size: 0.75rem;
  color: var(--text-secondary);
  font-family: var(--font-mono);
  margin-top: 2px;
}

.online-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.75rem;
  font-weight: 700;
  font-family: var(--font-mono);
  color: var(--status-green);
  background: rgba(0, 230, 118, 0.1);
  padding: 8px 16px;
  border-radius: 20px;
  border: 1px solid rgba(0, 230, 118, 0.25);
}

.status-dot.online {
  background: var(--status-green);
  box-shadow: 0 0 8px var(--status-green);
  animation: pulse-dot 2s infinite;
}

.hardware-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.hardware-stat {
  text-align: center;
  background: rgba(19, 28, 61, 0.4);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 14px;
}

.hardware-stat-value {
  display: block;
  font-family: var(--font-mono);
  font-size: 1.125rem;
  font-weight: 700;
  color: var(--accent);
}

.hardware-stat-label {
  display: block;
  font-size: 0.6875rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 4px;
}

/* ---- Sidebar Overlay ---- */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 150;
}

.sidebar-overlay.active {
  display: block;
}

/* ---- Responsive ---- */
@media (max-width: 1200px) {
  .metrics-row {
    grid-template-columns: repeat(2, 1fr);
  }

  .analytics-metrics-row {
    grid-template-columns: repeat(2, 1fr);
  }

  .overview-row {
    grid-template-columns: 1fr;
  }

  .analytics-charts-row {
    grid-template-columns: 1fr;
  }

  .reports-metrics-row {
    grid-template-columns: repeat(2, 1fr);
  }

  .settings-layout {
    grid-template-columns: 1fr;
  }

  .settings-card.hardware-card {
    grid-column: auto;
  }
}

@media (max-width: 768px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .main-wrapper {
    margin-left: 0;
  }

  .menu-toggle {
    display: flex;
  }

  .header-logo {
    display: flex;
  }

  .search-bar {
    display: none;
  }

  .user-info {
    display: none;
  }

  .metrics-row {
    grid-template-columns: 1fr;
  }

  .analytics-metrics-row {
    grid-template-columns: 1fr;
  }

  .reports-metrics-row {
    grid-template-columns: 1fr;
  }

  .hardware-stats {
    grid-template-columns: 1fr;
  }

  .dashboard {
    padding: 16px;
  }

  .chart-container {
    height: 260px;
    min-height: 260px;
    max-height: 260px;
  }

  .chart-container-fixed {
    flex-basis: 260px;
    height: 260px;
    min-height: 260px;
    max-height: 260px;
  }

  .bar-chart-container,
  .bar-chart-container-fixed {
    height: 240px;
    min-height: 240px;
    max-height: 240px;
  }

  .bar-chart-container-fixed {
    flex-basis: 240px;
  }

  #row_overview_and_controls .chart-panel-stable,
  #row_overview_and_controls .device-panel-stable {
    min-height: 420px;
  }

  #row_overview_and_controls .chart-panel-stable .panel-header-stable {
    flex-wrap: wrap;
  }

  .breach-alert-stable {
    max-width: 100%;
  }

  .breach-alert-text-stable {
    max-width: 180px;
  }

  .page-header {
    flex-direction: column;
    gap: 12px;
  }

  .appliance-grid {
    grid-template-columns: 1fr;
  }

  .toast-container {
    left: 16px;
    right: 16px;
    bottom: 16px;
  }

  .toast {
    min-width: unset;
    max-width: unset;
  }
}

@media (max-width: 480px) {
  .time-filters {
    width: 100%;
    justify-content: space-between;
  }

  .filter-btn {
    padding: 6px 10px;
    flex: 1;
    text-align: center;
  }
}
