:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface2: #f9fafb;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent2: #1d4ed8;
  --good: #16a34a;
  --bad: #dc2626;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 12px 28px rgba(15, 23, 42, 0.08);
  --radius: 14px;
  --radius2: 12px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, Arial;
  background: var(--bg);
  color: var(--text);
}

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

.sidebar {
  width: 280px;
  padding: 18px 14px;
  background: var(--surface);
  border-right: 1px solid var(--line);
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: auto;
  transition: width 0.2s ease, padding 0.2s ease;
  flex-shrink: 0;
}

/* Collapsed sidebar */
.sidebar.collapsed {
  width: 62px;
  padding: 18px 6px;
}
.sidebar.collapsed .brandText,
.sidebar.collapsed .navItem span:not(.navIcon),
.sidebar.collapsed .sidebarFooter {
  display: none;
}
.sidebar.collapsed .brand {
  justify-content: center;
  padding: 8px 0 16px 0;
}
.sidebar.collapsed .logo {
  width: 36px;
  height: 36px;
  font-size: 12px;
}
.sidebar.collapsed .navItem {
  justify-content: center;
  padding: 10px 0;
}
.sidebar.collapsed .navIcon svg {
  width: 22px;
  height: 22px;
}

/* Toggle button */
.sidebarToggle {
  position: absolute;
  top: 12px;
  right: -14px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface);
  cursor: pointer;
  display: grid;
  place-items: center;
  z-index: 10;
  box-shadow: 0 2px 6px rgba(0,0,0,0.08);
  transition: transform 0.2s;
}
.sidebarToggle:hover {
  background: var(--surface2);
}
.sidebarToggle svg {
  width: 14px;
  height: 14px;
  fill: var(--muted);
  transition: transform 0.2s;
}
.sidebar.collapsed .sidebarToggle svg {
  transform: rotate(180deg);
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px 10px 16px 10px;
}

.logo {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-weight: 900;
  letter-spacing: 0.5px;
  color: white;
  background: linear-gradient(135deg, var(--accent), #60a5fa);
  box-shadow: 0 6px 14px rgba(37, 99, 235, 0.25);
}

.brandTitle {
  font-weight: 800;
  font-size: 16px;
  line-height: 1.2;
}

.brandSub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.nav {
  display: grid;
  gap: 6px;
  padding: 6px 6px 12px 6px;
}

.navItem {
  display: flex;
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 10px 10px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font-weight: 600;
}

.navItem:hover {
  background: rgba(37, 99, 235, 0.06);
  border-color: rgba(37, 99, 235, 0.10);
}

.navItem.active {
  background: rgba(37, 99, 235, 0.10);
  border-color: rgba(37, 99, 235, 0.20);
  color: var(--accent2);
}

.navIcon svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
  opacity: 0.9;
}

.sidebarFooter {
  padding: 12px 10px;
  border-top: 1px solid var(--line);
  margin-top: 12px;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(246, 247, 251, 0.92);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 5;
}

.pageTitle {
  font-weight: 900;
  font-size: 18px;
}

.pageSub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.badge {
  padding: 7px 10px;
  border-radius: 999px;
  border: 1px solid rgba(22, 163, 74, 0.25);
  font-size: 12px;
  font-weight: 700;
  color: var(--good);
  background: rgba(22, 163, 74, 0.08);
}

.badge.bad {
  border-color: rgba(220, 38, 38, 0.25);
  color: var(--bad);
  background: rgba(220, 38, 38, 0.07);
}

.content {
  padding: 18px 22px 32px 22px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  min-width: 0;
}

.page {
  display: none;
  gap: 16px;
}

.page.active {
  display: grid;
}

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
}

.cardHeader {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 12px;
}

.card.collapsible > .cardHeader { cursor: pointer; user-select: none; }
.card.collapsible > .cardHeader h2::before { content: '\25BC '; font-size: 0.7em; vertical-align: middle; opacity: 0.5; }
.card.collapsed > .cardHeader { margin-bottom: 0; }
.card.collapsed > .cardHeader h2::before { content: '\25B6 '; }
.card.collapsed > *:not(.cardHeader) { display: none !important; }

.cardActions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

h2 {
  margin: 0;
  font-size: 16px;
}

label span {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
  font-weight: 600;
}

input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  outline: none;
}

input:focus, select:focus, textarea:focus {
  border-color: rgba(37, 99, 235, 0.5);
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.12);
}

textarea { resize: vertical; }

.btn {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
  font-weight: 700;
}

.btn:hover {
  filter: brightness(0.98);
}

.btn.primary {
  border-color: rgba(37, 99, 235, 0.35);
  background: rgba(37, 99, 235, 0.12);
  color: var(--accent2);
}

.btn.primary:hover {
  background: rgba(37, 99, 235, 0.16);
}

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 12px;
}

.grid2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}

.grid3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}

.full { grid-column: 1 / -1; }

.tableWrap {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius2);
  margin-top: 10px;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

thead th {
  text-align: left;
  padding: 10px;
  border-bottom: 1px solid var(--line);
  color: #334155;
  background: var(--surface2);
  position: sticky;
  top: 0;
}

tbody td {
  padding: 10px;
  border-bottom: 1px solid rgba(226, 232, 240, 0.8);
}

tbody tr:nth-child(odd) {
  background: rgba(248, 250, 252, 0.7);
}


tbody tr.clickable {
  cursor: pointer;
}

tbody tr.clickable:hover {
  background: rgba(37, 99, 235, 0.10);
}

.kv {
  border: 1px solid var(--line);
  border-radius: var(--radius2);
  overflow: hidden;
}

.kvRow {
  display: grid;
  grid-template-columns: 140px 1fr;
  gap: 12px;
  padding: 10px 12px;
  border-bottom: 1px solid rgba(226, 232, 240, 0.8);
  align-items: center;
}

.kvRow:last-child {
  border-bottom: none;
}

.kvKey {
  font-size: 12px;
  color: var(--muted);
  font-weight: 700;
}

.kvVal {
  font-size: 13px;
  font-weight: 700;
  color: #111827;
}


tr.detail-row { display: none; }
tr.detail-row.open { display: table-row; }
tr.detail-row td {
  padding: 8px 12px;
  background: #f8fafc;
  border-bottom: 2px solid var(--line);
}
tr.detail-row pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-all;
  font-size: 11px;
  line-height: 1.5;
  max-height: 400px;
  overflow: auto;
  background: #0f172a;
  color: #e2e8f0;
  padding: 10px 12px;
  border-radius: 8px;
}
tr.detail-toggle { cursor: pointer; }
tr.detail-toggle:hover { background: rgba(37, 99, 235, 0.07); }
tr.detail-toggle td:first-child::before {
  content: '\25B6';
  display: inline-block;
  margin-right: 6px;
  font-size: 9px;
  transition: transform 0.15s;
  color: var(--muted);
}
tr.detail-toggle.expanded td:first-child::before {
  transform: rotate(90deg);
}

.result {
  margin-top: 12px;
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 12px;
  padding: 12px;
  border-radius: var(--radius2);
  border: 1px solid var(--line);
  background: #f1f5f9;
  color: var(--text);
}

.result.error {
  border-color: rgba(220, 38, 38, 0.35);
  background: #fef2f2;
  color: #991b1b;
}

.hint {
  font-size: 12px;
  color: var(--muted);
  margin-top: 6px;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; }

.kpiGrid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
  margin-top: 12px;
}

.kpi {
  border: 1px solid var(--line);
  border-radius: var(--radius2);
  padding: 12px;
  background: var(--surface2);
}

.kpiLabel {
  font-size: 12px;
  color: var(--muted);
  font-weight: 700;
}

.kpiValue {
  font-size: 22px;
  font-weight: 900;
  margin-top: 6px;
}

.list {
  margin: 10px 0 0 0;
  padding-left: 18px;
  color: #334155;
}

.callout {
  margin-top: 12px;
  border: 1px dashed rgba(37, 99, 235, 0.35);
  background: rgba(37, 99, 235, 0.06);
  border-radius: var(--radius2);
  padding: 12px;
  color: #1e40af;
}

.footer {
  padding: 14px 22px;
  border-top: 1px solid var(--line);
}

@media (max-width: 980px) {
  .kpiGrid { grid-template-columns: 1fr 1fr; }
  .grid2 { grid-template-columns: 1fr; }
  .grid3 { grid-template-columns: 1fr; }
  .sidebar {
    width: 240px;
  }
  .donutGrid { grid-template-columns: repeat(2, 1fr) !important; }
}

/* 760px breakpoint removed — replaced by 640px mobile drawer */


/* Tunnel URLs helpers */
.btn.xs{padding:6px 10px;font-size:12px;line-height:1;border-radius:10px}
.h3{margin:0 0 10px 0;font-size:14px;color:#1f2937}


.cmdBlock { min-width: 0; }
.cmdHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}
.cmdPre {
  margin-top: 6px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  overflow-x: auto;
  max-width: 100%;
}

.cmdArea {
  width: 100%;
  box-sizing: border-box;
  margin-top: 6px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  overflow-x: auto;
  resize: vertical;
  min-height: 84px;
  font: inherit;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  color: var(--fg);
}
.jsonBox {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  overflow-x: auto;
  max-width: 100%;
}
.callout .mono, .kvVal.mono, .hint .mono { overflow-wrap: anywhere; }
@media (max-width: 1100px) {
  .content { max-width: 100%; }
}

/* SB Dashboard — tablet */
@media (max-width: 980px) {
  #sbDashKpiGrid { grid-template-columns: repeat(3, 1fr) !important; }
}
/* SB Dashboard — mobile handled in 640px block below */

/* Device capabilities form */
.capsCheck {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  padding: 3px 0;
  cursor: pointer;
}
.capsCheck input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}
.capsRow {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  padding: 3px 0;
}
.capsRow select, .capsRow input {
  font-size: 12px;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
}

/* Modbus Connector */
#mcRegTable input, #mcRegTable select,
#mcGroupTable input, #mcGroupTable select {
  font-size: 11px;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
}
#mcRegTable td, #mcGroupTable td {
  padding: 3px 4px;
}
#mcRegTable input:focus, #mcGroupTable input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

/* Hamburger button (hidden by default, shown on mobile) */
.hamburger {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: none;
  font-size: 22px;
  cursor: pointer;
  border-radius: 8px;
  color: var(--text);
  flex-shrink: 0;
}
.hamburger:hover { background: rgba(0,0,0,0.05); }

/* Bottom nav (hidden by default) */
.bottomNav { display: none; }

/* Mobile overlay (hidden by default) */
.mobileOverlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 250;
}
.mobileOverlay.show { display: block; }

/* Mobile specific */
@media (max-width: 640px) {
  /* Sidebar: hidden by default on mobile, slide-in drawer when open */
  .sidebar,
  .sidebar.collapsed {
    display: none !important;
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 280px !important;
    height: 100vh !important;
    z-index: 300;
    border-right: 1px solid var(--line);
    border-bottom: none;
    overflow-y: auto;
    padding: 18px 14px !important;
    background: var(--surface);
  }
  .sidebar.mobileOpen,
  .sidebar.collapsed.mobileOpen {
    display: flex !important;
    flex-direction: column;
  }
  /* Main takes full width — no sidebar in flow */
  .main { width: 100% !important; margin-left: 0 !important; }
  /* Show all sidebar content when open on mobile */
  .sidebar.collapsed.mobileOpen .brandText,
  .sidebar.collapsed.mobileOpen .navItem span:not(.navIcon),
  .sidebar.collapsed.mobileOpen .sidebarFooter { display: initial; }
  .sidebar.collapsed.mobileOpen .brand { justify-content: flex-start; padding: 8px 10px 16px 10px; }
  .sidebar.collapsed.mobileOpen .navItem { justify-content: flex-start; padding: 10px 10px; }

  /* Hide sidebar toggle button on mobile */
  .sidebarToggle { display: none; }

  /* Hamburger visible */
  .hamburger { display: flex !important; }

  /* Content adjustments */
  .content { padding: 12px; max-width: 100%; padding-bottom: 72px; }
  .topbar { padding: 12px 16px; }

  /* KPI force 2-col */
  .kpiGrid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px !important; }

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

  /* Buttons touch-friendly */
  .btn { min-height: 44px; }
  .btn.xs { min-height: 36px; }

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

  /* Nav in sidebar as vertical list on mobile (undo 760px 2-col grid) */
  .nav { grid-template-columns: 1fr; }

  /* Bottom navigation */
  .bottomNav {
    display: flex !important;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--surface);
    border-top: 1px solid var(--line);
    z-index: 200;
    padding: 4px 0 env(safe-area-inset-bottom, 4px);
    box-shadow: 0 -2px 10px rgba(0,0,0,0.06);
  }
  .bottomNav button {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    padding: 6px 2px;
    font-size: 9px;
    font-weight: 600;
    color: var(--muted);
    cursor: pointer;
    border: none;
    background: none;
  }
  .bottomNav button.active { color: var(--accent); }
  .bottomNav .bnIcon { font-size: 18px; display: block; }

  /* SB Dashboard KPI — override inline repeat(6) */
  #sbDashKpiGrid { grid-template-columns: repeat(2, 1fr) !important; }

  /* Energy flow SVG — force responsive */
  #sbDashFlow { overflow: hidden; }
  #sbFlowSvg { width: 100% !important; height: auto !important; max-width: 100% !important; }

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

  /* Chart controls simplification */
  #sbDualSlider { display: none !important; }
  #sbSliderFromLabel, #sbSliderToLabel, #sbSliderRangeLabel { display: none !important; }

  /* Chart tab controls — compact */
  #sbChartControls { flex-wrap: wrap; gap: 6px !important; }
  #sbChartCtrlDay, #sbChartCtrlWeek, #sbChartCtrlMonth, #sbChartCtrlYear {
    flex-wrap: wrap !important;
    gap: 6px !important;
  }
  /* Slider range div is unusable on touch */
  #sbChartCtrlDay > div[style*="min-width:280px"] { display: none !important; }

  /* Unit toggle + series toggles — wrap */
  #sbChartUnitToggle { font-size: 11px; }
  #sbChartToggles { gap: 3px !important; }
  #sbChartToggles button { padding: 3px 6px !important; font-size: 10px !important; }

  /* Battery management — stack */
  .card > div[style*="display:flex"][style*="gap:16px"] {
    flex-direction: column !important;
  }

  /* Device capabilities — single column */
  #ddCapabilitiesSection div[style*="grid-template-columns:1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }

  /* Tab bar buttons compact */
  [data-chart-tab] { padding: 6px 10px !important; font-size: 12px !important; }

  /* Device selector bar — wrap */
  .card > div[style*="display:flex"][style*="align-items:center"][style*="gap:12px"] {
    flex-wrap: wrap !important;
  }

  /* Callout compact */
  .callout { padding: 10px 12px; font-size: 13px; }

  /* Page title */
  .pageTitle { font-size: 16px; }
}

.entityStatusBar {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  margin: 12px 0;
}

.entityStatusBar > div {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-soft);
  padding: 10px 12px;
  min-width: 0;
}

.entityTree {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
  max-height: 460px;
  overflow: auto;
}

.entityNode {
  border-left: 3px solid #cbd5e1;
  margin: 8px 0;
  padding-left: 10px;
}

.entityNode.device { border-left-color: #2563eb; }
.entityNode.machine { border-left-color: #16a34a; }

.entityChildren {
  margin-left: 12px;
}

.entityPick {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 10px;
  cursor: pointer;
  text-align: left;
}

.entityPick:hover {
  border-color: #93c5fd;
  background: #eff6ff;
}

.entityPick small,
.entityLeaf small {
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
}

.entityLeaf {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  margin: 6px 0;
  padding: 8px 10px;
  background: #fff;
}

.entityLeaf span {
  min-width: 0;
  flex: 1;
  overflow-wrap: anywhere;
}

.entityLeaf.sensor {
  background: #f8fafc;
}

@media (max-width: 760px) {
  .entityStatusBar {
    grid-template-columns: 1fr;
  }

  .entityLeaf {
    flex-wrap: wrap;
  }
}
