/* SmartBox Design System — inspired by sb-manager */

:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --accent-light: #eff6ff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --sidebar-width: 260px;
  --sidebar-bg: #1e293b;
  --sidebar-text: #94a3b8;
  --sidebar-hover: rgba(255,255,255,0.07);
  --sidebar-active-bg: rgba(37,99,235,0.18);
  --sidebar-active-text: #60a5fa;
  --danger: #ef4444;
  --danger-light: #fef2f2;
  --warning: #f59e0b;
  --warning-light: #fffbeb;
  --success: #10b981;
  --success-light: #ecfdf5;
  --radius: 10px;
  --shadow: 0 1px 6px rgba(0,0,0,0.07);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.10);
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

/* ── Shell Layout ────────────────────────────────────────── */

.shell {
  display: flex;
  min-height: 100vh;
}

/* ── Sidebar ─────────────────────────────────────────────── */

.sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  overflow-y: auto;
  z-index: 200;
  flex-shrink: 0;
}

.sidebarBrand {
  padding: 22px 20px 18px;
  border-bottom: 1px solid rgba(255,255,255,0.07);
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebarBrand .brandLogo {
  font-size: 24px;
}

.sidebarBrand .brandName {
  font-size: 18px;
  font-weight: 700;
  color: #f1f5f9;
  letter-spacing: -0.3px;
}

.sidebarBrand .brandSub {
  font-size: 11px;
  color: var(--sidebar-text);
  margin-top: 1px;
}

.sidebarNav {
  flex: 1;
  padding: 12px 0;
  list-style: none;
}

.navItem {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 20px;
  color: var(--sidebar-text);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  border-radius: 0;
  font-size: 14px;
  font-weight: 500;
  user-select: none;
  border-left: 3px solid transparent;
}

.navItem .navIcon {
  font-size: 17px;
  min-width: 20px;
  text-align: center;
}

.navItem:hover {
  background: var(--sidebar-hover);
  color: #e2e8f0;
}

.navItem.active {
  background: var(--sidebar-active-bg);
  color: var(--sidebar-active-text);
  border-left-color: var(--accent);
}

.sidebarFooter {
  padding: 16px 20px;
  border-top: 1px solid rgba(255,255,255,0.07);
}

.sidebarUser {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.sidebarAvatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: #fff;
  font-size: 15px;
  flex-shrink: 0;
}

.sidebarUserInfo {
  min-width: 0;
}

.sidebarUserName {
  font-size: 13px;
  font-weight: 600;
  color: #f1f5f9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebarUserRole {
  font-size: 11px;
  color: var(--sidebar-text);
}

/* ── Main Content ─────────────────────────────────────────── */

.main {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 28px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow);
}

.topbarTitle {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
}

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

.content {
  flex: 1;
  padding: 28px;
}

/* ── Page visibility ──────────────────────────────────────── */

.page { display: none; }
.page.active { display: block; }

/* ── Cards ────────────────────────────────────────────────── */

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  overflow: hidden;
}

.cardHeader {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.cardHeader h2,
.cardHeader h3 {
  font-size: 15px;
  font-weight: 700;
  margin: 0;
}

.cardBody {
  padding: 20px;
}

/* ── KPI Grid ─────────────────────────────────────────────── */

.kpiGrid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 18px;
  margin-bottom: 24px;
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px 22px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.kpiLabel {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.kpiValue {
  font-size: 28px;
  font-weight: 800;
  color: var(--text);
  line-height: 1.1;
}

.kpiSub {
  font-size: 12px;
  color: var(--text-muted);
}

/* ── Badges ────────────────────────────────────────────────── */

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.3px;
}

.badge.healthy,
.badge.success,
.badge.active,
.badge.enabled {
  background: var(--success-light);
  color: var(--success);
}

.badge.warning,
.badge.check {
  background: var(--warning-light);
  color: var(--warning);
}

.badge.error,
.badge.danger,
.badge.inactive,
.badge.disabled {
  background: var(--danger-light);
  color: var(--danger);
}

.badge.info,
.badge.neutral {
  background: var(--accent-light);
  color: var(--accent);
}

/* ── Buttons ───────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s, box-shadow 0.15s;
  text-decoration: none;
  white-space: nowrap;
}

.btn:active { transform: scale(0.97); }

.btn.primary {
  background: var(--accent);
  color: #fff;
}
.btn.primary:hover {
  background: var(--accent-dark);
  box-shadow: 0 4px 14px rgba(37,99,235,0.3);
}

.btn.secondary {
  background: #f1f5f9;
  color: var(--text);
  border: 1px solid var(--border);
}
.btn.secondary:hover { background: #e2e8f0; }

.btn.danger {
  background: var(--danger);
  color: #fff;
}
.btn.danger:hover { background: #dc2626; }

.btn.success {
  background: var(--success);
  color: #fff;
}
.btn.success:hover { background: #059669; }

.btn.sm {
  padding: 5px 10px;
  font-size: 12px;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

/* ── Table ─────────────────────────────────────────────────── */

.tableWrap {
  overflow-x: auto;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
}

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

.tableWrap th {
  background: #f8fafc;
  padding: 11px 14px;
  text-align: left;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  border-bottom: 1px solid var(--border);
}

.tableWrap td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  vertical-align: middle;
}

.tableWrap tr:last-child td { border-bottom: none; }
.tableWrap tr:hover td { background: #f8fafc; }

/* ── Forms ─────────────────────────────────────────────────── */

.formGroup {
  margin-bottom: 16px;
}

.formGroup label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.formGroup input,
.formGroup select,
.formGroup textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 7px;
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.formGroup input:focus,
.formGroup select:focus,
.formGroup textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
}

.formGroup input[type="checkbox"] {
  width: auto;
}

.formFieldset {
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 14px 16px;
  margin-bottom: 16px;
}

.formFieldset legend {
  padding: 0 6px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
}

/* ── Grid helpers ──────────────────────────────────────────── */

.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
}

.grid3 {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 18px;
}

/* ── Callout / Alert ───────────────────────────────────────── */

.callout {
  padding: 14px 16px;
  border-radius: 8px;
  border-left: 4px solid transparent;
  font-size: 13px;
  margin-bottom: 16px;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.callout.info {
  background: var(--accent-light);
  border-left-color: var(--accent);
  color: #1d4ed8;
}

.callout.success {
  background: var(--success-light);
  border-left-color: var(--success);
  color: #065f46;
}

.callout.warning {
  background: var(--warning-light);
  border-left-color: var(--warning);
  color: #92400e;
}

.callout.error {
  background: var(--danger-light);
  border-left-color: var(--danger);
  color: #991b1b;
}

/* ── Result / pre output ───────────────────────────────────── */

.result {
  background: #0f172a;
  color: #94a3b8;
  padding: 16px;
  border-radius: 8px;
  font-family: 'Courier New', Courier, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 400px;
  overflow-y: auto;
}

/* ── Toolbar ───────────────────────────────────────────────── */

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}

/* ── Section title ─────────────────────────────────────────── */

.pageTitle {
  font-size: 22px;
  font-weight: 800;
  color: var(--text);
  margin-bottom: 20px;
}

/* ── Loading spinner ───────────────────────────────────────── */

.spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: middle;
}

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

.loadingMsg {
  text-align: center;
  padding: 32px 20px;
  color: var(--text-muted);
  font-size: 14px;
}

/* ── Modal ─────────────────────────────────────────────────── */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 16px;
}

.modalContent {
  background: var(--surface);
  border-radius: var(--radius);
  max-width: 540px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-md);
}

.modalHeader {
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modalHeader h3 {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
}

.modalClose {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
  padding: 2px 6px;
  border-radius: 5px;
}
.modalClose:hover { background: var(--border); }

.modalBody {
  padding: 22px;
}

.modalFooter {
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

/* ── Toast notifications ───────────────────────────────────── */

#toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toastMsg {
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow-md);
  animation: slideIn 0.25s ease;
  pointer-events: all;
  max-width: 320px;
}

.toastMsg.success {
  background: var(--success);
  color: #fff;
}

.toastMsg.error {
  background: var(--danger);
  color: #fff;
}

.toastMsg.info {
  background: var(--accent);
  color: #fff;
}

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to   { transform: translateX(0);   opacity: 1; }
}

/* ── Login page ────────────────────────────────────────────── */

.loginWrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1e293b 0%, #1d4ed8 100%);
  padding: 24px;
}

.loginBox {
  background: var(--surface);
  border-radius: 14px;
  padding: 42px 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 16px 60px rgba(0,0,0,0.25);
}

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

.loginLogo .logoIcon {
  font-size: 44px;
}

.loginLogo h1 {
  font-size: 26px;
  font-weight: 800;
  color: var(--text);
  margin: 8px 0 4px;
}

.loginLogo p {
  font-size: 13px;
  color: var(--text-muted);
  margin: 0;
}

.loginError {
  display: none;
}

.loginError.show {
  display: flex;
}

.loginBtn {
  width: 100%;
  padding: 11px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, box-shadow 0.15s;
  margin-top: 4px;
}

.loginBtn:hover {
  background: var(--accent-dark);
  box-shadow: 0 4px 18px rgba(37,99,235,0.35);
}

/* ── Error pages ───────────────────────────────────────────── */

.errorWrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 24px;
}

.errorBox {
  background: var(--surface);
  border-radius: 14px;
  padding: 48px 40px;
  max-width: 480px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow-md);
  border: 1px solid var(--border);
}

.errorCode {
  font-size: 80px;
  font-weight: 900;
  line-height: 1;
  color: var(--accent);
  margin-bottom: 12px;
}

.errorTitle {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 10px;
}

.errorMsg {
  font-size: 14px;
  color: var(--text-muted);
  margin-bottom: 28px;
}

/* ── Hamburger button (hidden by default) ──────────────────── */

.hamburger {
  display: none;
  background: none;
  border: none;
  font-size: 24px;
  cursor: pointer;
  color: var(--text);
  padding: 4px 8px;
  border-radius: 6px;
  line-height: 1;
}
.hamburger:hover { background: var(--border); }

/* ── Sidebar overlay (hidden by default) ───────────────────── */

.sidebarOverlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 250;
}
.sidebarOverlay.show { display: block; }

/* ── Bottom navigation (hidden by default) ─────────────────── */

.bottomNav {
  display: none;
}

/* ── Responsive: Tablet ────────────────────────────────────── */

@media (max-width: 900px) {
  .sidebar { width: 200px; }
  .main { margin-left: 200px; }
  .kpiGrid { grid-template-columns: repeat(2, 1fr) !important; }
  .kpiGrid6 { grid-template-columns: repeat(3, 1fr) !important; }
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .content { padding: 16px; }
  .donutGrid { grid-template-columns: 1fr 1fr !important; }
}

/* ── Responsive: Mobile ────────────────────────────────────── */

@media (max-width: 640px) {
  /* Sidebar: off-canvas drawer */
  .sidebar {
    position: fixed;
    width: 280px;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    z-index: 300;
    overflow-y: auto;
  }
  .sidebar.open { transform: translateX(0); }

  .main { margin-left: 0; }
  .topbar { padding: 0 16px; height: 52px; }
  .content { padding: 12px; padding-bottom: 80px; }

  /* Hamburger visible */
  .hamburger { display: block; }

  /* KPI grid: 2 columns */
  .kpiGrid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px !important; }
  .kpiGrid6 { grid-template-columns: repeat(2, 1fr) !important; gap: 10px !important; }
  .kpi { padding: 14px 16px; }
  .kpiValue { font-size: 20px; }

  /* Cards compact */
  .card { border-radius: 8px; }
  .cardHeader { padding: 12px 14px; }
  .cardBody { padding: 14px; }

  /* Buttons touch-friendly */
  .btn { min-height: 44px; padding: 10px 16px; }
  .btn.sm { min-height: 36px; padding: 6px 10px; }

  /* Page title smaller */
  .pageTitle { font-size: 18px; margin-bottom: 14px; }

  /* Table scroll */
  .tableWrap { font-size: 12px; }

  /* Grid helpers stack */
  .grid2, .grid3 { grid-template-columns: 1fr; }

  /* Donut charts stack vertically */
  .donutGrid { grid-template-columns: 1fr !important; }

  /* Energy flow SVG responsive */
  .flowSvgWrap { display: flex; justify-content: center; padding: 12px 8px; }
  .flowSvgWrap svg { width: 100%; max-width: 520px; height: auto; }

  /* Chart controls: hide dual slider on mobile */
  .dualSliderWrapMobile { display: none !important; }

  /* Chart toggle buttons: wrap nicely */
  .chartTogglesWrap { flex-wrap: wrap; gap: 4px; }

  /* Unit toggle: hide on mobile (default to W) */
  .unitToggleWrap { display: none !important; }

  /* Tab controls: stack */
  .tabControls { flex-wrap: wrap; gap: 6px; }
  .tabControls label { font-size: 11px; }
  .tabControls input, .tabControls select { font-size: 12px; max-width: 130px; }

  /* Toolbar wrap */
  .toolbar { gap: 8px; }
  .toolbar .btn { font-size: 12px; padding: 8px 12px; }

  /* Hide sidebar nav on mobile (use bottom nav instead) */
  .sidebarNav { display: none; }

  /* Bottom tab bar */
  .bottomNav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    z-index: 200;
    padding: 6px 0 env(safe-area-inset-bottom, 6px);
    box-shadow: 0 -2px 10px rgba(0,0,0,0.05);
  }
  .bottomNav .bottomNavItem {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 8px 4px;
    font-size: 10px;
    font-weight: 600;
    color: var(--text-muted);
    cursor: pointer;
    border: none;
    background: none;
  }
  .bottomNav .bottomNavItem.active {
    color: var(--accent);
  }
  .bottomNav .bottomNavItem .bottomNavIcon {
    font-size: 20px;
  }

  /* Toast repositioned above bottom nav */
  #toast {
    bottom: 72px;
  }

  /* Login page adjustments */
  .loginBox { padding: 28px 24px; }
  .loginLogo h1 { font-size: 22px; }

  /* Modal full-width on mobile */
  .modalContent { max-width: 100%; }
  .modal { padding: 8px; }
}
