:root {
  color-scheme: light;
  --surface-1: #ffffff;
  --surface-2: #f3f4f8;
  --page: #f5f6fa;
  --text-primary: #16181d;
  --text-secondary: #565b66;
  --text-muted: #8a8f9a;
  --gridline: #e7e8ee;
  --border: rgba(15, 18, 30, 0.09);

  --brand: #2f5fe0;
  --brand-dark: #1f45b8;
  --brand-soft: rgba(47, 95, 224, 0.10);

  --series-1: #2f5fe0;
  --series-2: #eb6834;
  --series-3: #1baf7a;

  --good: #16a34a;
  --good-soft: rgba(22, 163, 74, 0.12);
  --warning: #d98a0b;
  --warning-soft: rgba(217, 138, 11, 0.14);
  --critical: #dc2626;
  --critical-soft: rgba(220, 38, 38, 0.12);

  --sidebar-bg: linear-gradient(195deg, #131b31, #0d1424);
  --sidebar-text: rgba(255, 255, 255, 0.68);
  --sidebar-text-active: #ffffff;
  --sidebar-active-bg: rgba(255, 255, 255, 0.08);
  --sidebar-border: rgba(255, 255, 255, 0.08);

  --shadow-sm: 0 1px 2px rgba(16, 20, 33, 0.06);
  --shadow-md: 0 6px 20px rgba(16, 20, 33, 0.08);
  --radius: 10px;
  --radius-sm: 7px;

  --sidebar-w: 248px;
  --sidebar-w-collapsed: 76px;
  --topbar-h: 60px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #181b23;
    --surface-2: #1f232c;
    --page: #101218;
    --text-primary: #f4f5f7;
    --text-secondary: #b6bac4;
    --text-muted: #82868f;
    --gridline: #2a2e38;
    --border: rgba(255, 255, 255, 0.09);

    --brand: #5b83ef;
    --brand-dark: #4066d9;
    --brand-soft: rgba(91, 131, 239, 0.16);

    --series-1: #5b83ef;
    --series-2: #e58a52;
    --series-3: #34c690;

    --good: #34c690;
    --good-soft: rgba(52, 198, 144, 0.14);
    --warning: #e0a52f;
    --warning-soft: rgba(224, 165, 47, 0.16);
    --critical: #ef5a5a;
    --critical-soft: rgba(239, 90, 90, 0.14);

    --sidebar-bg: linear-gradient(195deg, #0c1019, #090d15);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 6px 24px rgba(0, 0, 0, 0.4);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #181b23;
  --surface-2: #1f232c;
  --page: #101218;
  --text-primary: #f4f5f7;
  --text-secondary: #b6bac4;
  --text-muted: #82868f;
  --gridline: #2a2e38;
  --border: rgba(255, 255, 255, 0.09);

  --brand: #5b83ef;
  --brand-dark: #4066d9;
  --brand-soft: rgba(91, 131, 239, 0.16);

  --series-1: #5b83ef;
  --series-2: #e58a52;
  --series-3: #34c690;

  --good: #34c690;
  --good-soft: rgba(52, 198, 144, 0.14);
  --warning: #e0a52f;
  --warning-soft: rgba(224, 165, 47, 0.16);
  --critical: #ef5a5a;
  --critical-soft: rgba(239, 90, 90, 0.14);

  --sidebar-bg: linear-gradient(195deg, #0c1019, #090d15);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 6px 24px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--page);
  color: var(--text-primary);
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; }

/* ---------- App shell ---------- */

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

/* ---------- Sidebar ---------- */

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--sidebar-bg);
  color: var(--sidebar-text);
  position: fixed;
  inset: 0 auto 0 0;
  display: flex;
  flex-direction: column;
  z-index: 40;
  transition: transform 0.2s ease;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--topbar-h);
  padding: 0 20px;
  border-bottom: 1px solid var(--sidebar-border);
  color: #fff;
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.2px;
  flex: 0 0 auto;
}

.sidebar-brand .brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: var(--brand);
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}

.sidebar-nav {
  padding: 16px 12px;
  overflow-y: auto;
  flex: 1 1 auto;
}

.sidebar-section-label {
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: rgba(255, 255, 255, 0.35);
  padding: 12px 12px 6px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  font-weight: 500;
  color: var(--sidebar-text);
  text-decoration: none;
  margin-bottom: 2px;
  border-left: 3px solid transparent;
  transition: background 0.12s, color 0.12s;
}

.nav-item svg { flex: 0 0 auto; opacity: 0.85; }

.nav-item:hover {
  background: var(--sidebar-active-bg);
  color: var(--sidebar-text-active);
}

.nav-item.active {
  background: var(--sidebar-active-bg);
  color: var(--sidebar-text-active);
  border-left-color: var(--brand);
}
.nav-item.active svg { opacity: 1; }

.sidebar-foot {
  padding: 14px 20px;
  font-size: 11.5px;
  color: rgba(255, 255, 255, 0.35);
  border-top: 1px solid var(--sidebar-border);
  flex: 0 0 auto;
}

/* ---------- Topbar ---------- */

.content-area {
  flex: 1 1 auto;
  margin-left: var(--sidebar-w);
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 24px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 30;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.sidebar-toggle {
  display: none;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  width: 34px;
  height: 34px;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  cursor: pointer;
}

.page-title {
  font-size: 16px;
  font-weight: 700;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.page-subtitle {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 1px;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 0 0 auto;
}

.conn-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 999px;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--critical);
  flex: 0 0 auto;
}
.dot.online { background: var(--good); }

/* ---------- Main content ---------- */

main {
  padding: 24px;
  max-width: 1400px;
  width: 100%;
  margin: 0 auto;
  flex: 1 1 auto;
}

/* ---------- KPI / stat cards ---------- */

.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}

.kpi-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: var(--shadow-sm);
}

.kpi-icon {
  width: 42px;
  height: 42px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
}
.kpi-icon.tone-brand { background: var(--brand-soft); color: var(--brand); }
.kpi-icon.tone-good { background: var(--good-soft); color: var(--good); }
.kpi-icon.tone-warning { background: var(--warning-soft); color: var(--warning); }
.kpi-icon.tone-critical { background: var(--critical-soft); color: var(--critical); }

.kpi-value {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.kpi-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ---------- Section / card container ---------- */

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 0 12px;
}
.section-head h2 {
  font-size: 14px;
  margin: 0;
  font-weight: 700;
}
.section-hint {
  font-size: 12px;
  color: var(--text-muted);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}

.ecu-group { margin-bottom: 20px; }
.ecu-group:last-child { margin-bottom: 0; }

.ecu-group-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 4px;
  margin-bottom: 10px;
  cursor: pointer;
  user-select: none;
}
.ecu-group-head .collapse-toggle { pointer-events: none; }

.ecu-group-title {
  font-size: 13.5px;
  font-weight: 700;
  margin: 0;
  letter-spacing: 0.2px;
}

.ecu-group-location {
  font-weight: 500;
  color: var(--text-muted);
}

.ecu-group-count {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-muted);
}

.ecu-group.collapsed .ecu-group-body { display: none; }
.ecu-group.collapsed .collapse-toggle svg { transform: rotate(-90deg); }

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.1s;
  box-shadow: var(--shadow-sm);
}
.card:hover { border-color: var(--brand); box-shadow: var(--shadow-md); transform: translateY(-1px); }
.card.selected { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }

.card-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  row-gap: 6px;
  margin-bottom: 8px;
}

.card-head .code { font-weight: 700; font-size: 14px; }

.card-head-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  padding: 3px 9px;
  border-radius: 999px;
  white-space: nowrap;
}
.status-pill.online { background: var(--good-soft); color: var(--good); }
.status-pill.warning { background: var(--warning-soft); color: var(--warning); }
.status-pill.offline { background: var(--critical-soft); color: var(--critical); }

.card .name { color: var(--text-secondary); font-size: 12px; margin-bottom: 10px; }

.metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; }
.metrics.metrics-wide { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 16px 20px; }
.metric-label { font-size: 11px; color: var(--text-muted); }
.metric-value { font-size: 14.5px; font-weight: 600; font-variant-numeric: tabular-nums; }

.panel {
  margin-top: 20px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  display: none;
  box-shadow: var(--shadow-sm);
}
.panel.open { display: block; }
main .panel + .panel { margin-top: 16px; }

.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  flex-wrap: wrap;
  gap: 10px;
}

.panel-head h2 { font-size: 14.5px; margin: 0; font-weight: 700; }

.collapse-toggle {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  width: 30px;
  height: 30px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  flex: 0 0 auto;
}
.collapse-toggle svg { transition: transform 0.15s; }
.panel.collapsed .collapse-toggle svg { transform: rotate(-90deg); }
.panel.collapsed .panel-body { display: none; }

.panel-controls {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

select, button, input {
  font-family: inherit;
  font-size: 13px;
  padding: 7px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
}

button {
  cursor: pointer;
  font-weight: 600;
  border-color: var(--border);
  transition: background 0.12s, border-color 0.12s;
}
button:hover { border-color: var(--brand); }

.btn-primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}
.btn-primary:hover { background: var(--brand-dark); border-color: var(--brand-dark); }

.chart-wrap { position: relative; height: 320px; }

.empty-state {
  color: var(--text-muted);
  font-size: 13px;
  padding: 40px 0;
  text-align: center;
}

/* ---------- Device management forms/tables ---------- */

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1 1 150px;
  min-width: 150px;
}
.field label {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-secondary);
}
.field input, .field select {
  width: 100%;
}

.field-checkbox {
  flex: 0 0 auto;
  min-width: 0;
  flex-direction: row;
  align-items: center;
  gap: 7px;
  height: 34px;
  align-self: end;
}
.field-checkbox label { order: 2; margin: 0; }
.field-checkbox input { width: auto; order: 1; }

.btn-link {
  background: transparent;
  border: none;
  color: var(--brand);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
  padding: 4px 6px;
  cursor: pointer;
  white-space: nowrap;
}
.btn-link:hover { text-decoration: underline; }
.btn-link.sm { font-size: 11.5px; padding: 3px 8px; border: 1px solid var(--border); border-radius: 999px; }
.btn-link.sm:hover { text-decoration: none; border-color: var(--brand); background: var(--brand-soft); }

.device-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
  align-items: end;
}
.device-form .btn-primary { flex: 0 0 auto; height: 34px; align-self: end; }

.form-msg {
  font-size: 12.5px;
  margin-bottom: 12px;
  min-height: 1.2em;
  font-weight: 600;
}
.form-msg.success { color: var(--good); }
.form-msg.error { color: var(--critical); }

.table-toolbar {
  display: flex;
  align-items: end;
  gap: 10px;
  margin-bottom: 12px;
}
.table-toolbar .field { max-width: 280px; }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }

.device-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
.device-table th,
.device-table td {
  text-align: left;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.device-table tbody tr:last-child td { border-bottom: none; }
.device-table tbody tr:hover { background: var(--surface-2); }
.device-table th {
  color: var(--text-muted);
  font-weight: 600;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  background: var(--surface-2);
}
.device-table .empty-cell {
  text-align: center;
  color: var(--text-muted);
  white-space: normal;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 9px;
  border-radius: 999px;
}
.badge.on { background: var(--good-soft); color: var(--good); }
.badge.off { background: var(--critical-soft); color: var(--critical); }
.badge.warn { background: var(--warning-soft); color: var(--warning); }
.badge.info { background: var(--brand-soft); color: var(--brand); }

.btn-link.danger { color: var(--critical); }

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); box-shadow: var(--shadow-md); }
  .app-shell.sidebar-open .sidebar { transform: translateX(0); }
  .content-area { margin-left: 0; }
  .sidebar-toggle { display: inline-flex; }
  main { padding: 16px; }
}

.sidebar-scrim {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 35;
}
@media (max-width: 900px) {
  .app-shell.sidebar-open .sidebar-scrim { display: block; }
}
