:root {
  color-scheme: dark;
  --bg: #101317;
  --panel: #181d23;
  --panel-2: #202730;
  --line: #303944;
  --text: #eef2f5;
  --muted: #9aa6b2;
  --green: #45d483;
  --blue: #5ba8ff;
  --amber: #f0b35a;
  --red: #f26d6d;
  --ink: #0d1116;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
input,
select {
  font: inherit;
}

button {
  height: 38px;
  border: 0;
  border-radius: 8px;
  padding: 0 14px;
  color: var(--text);
  cursor: pointer;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.primary {
  background: var(--green);
  color: var(--ink);
  font-weight: 700;
}

.secondary {
  background: var(--panel-2);
  border: 1px solid var(--line);
}

.danger {
  background: rgba(242, 109, 109, 0.15);
  color: var(--red);
  border: 1px solid rgba(242, 109, 109, 0.35);
}

.ghost {
  background: transparent;
  border: 1px solid var(--line);
}

.sidebar {
  position: fixed;
  inset: 0 auto 0 0;
  width: 260px;
  padding: 24px 18px;
  border-right: 1px solid var(--line);
  background: #12171d;
}

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

.brand-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 8px;
  background: var(--green);
  color: var(--ink);
  font-weight: 800;
}

.brand h1,
.topbar h2,
.section h3 {
  margin: 0;
}

.brand p,
.topbar p,
.hint,
.device-line {
  color: var(--muted);
}

.brand p,
.topbar p {
  margin: 4px 0 0;
}

.nav {
  display: grid;
  gap: 8px;
  margin: 30px 0;
}

.nav a {
  color: var(--muted);
  text-decoration: none;
  padding: 10px 12px;
  border-radius: 8px;
}

.nav a:hover {
  color: var(--text);
  background: var(--panel);
}

.side-card {
  display: grid;
  gap: 10px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.status-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--red);
}

.dot.online {
  background: var(--green);
}

.app {
  margin-left: 260px;
  padding: 24px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: flex-end;
}

.section {
  margin-bottom: 18px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

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

.metric-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(130px, 1fr));
  gap: 12px;
}

.device-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(170px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.device-card {
  min-height: 120px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #151b21;
}

.device-card.online {
  border-color: rgba(69, 212, 131, 0.45);
}

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

.device-card-head span,
.device-topic,
.device-powers {
  color: var(--muted);
  font-size: 12px;
}

.device-card.online .device-card-head span {
  color: var(--green);
}

.device-topic {
  margin-top: 8px;
  overflow-wrap: anywhere;
}

.device-powers {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
  margin-top: 10px;
}

.metric {
  min-height: 86px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
}

.metric span {
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  display: block;
  margin-top: 8px;
  font-size: 24px;
  line-height: 1.1;
}

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

.flow-node {
  padding: 16px;
  border-radius: 8px;
  background: #13191f;
  border: 1px solid var(--line);
}

.flow-node span {
  display: block;
  color: var(--muted);
  margin-bottom: 6px;
}

.flow-node strong {
  font-size: 22px;
}

.pv strong {
  color: var(--green);
}

.grid strong {
  color: var(--blue);
}

.battery strong {
  color: var(--amber);
}

.load strong {
  color: var(--red);
}

.scenario-grid,
.settings-grid,
.fault-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(190px, 1fr));
  gap: 12px;
}

label,
.setting-item,
.fault-item {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #151b21;
}

label span,
.setting-title,
.fault-title {
  color: var(--muted);
  font-size: 13px;
}

input,
select {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: #0f141a;
  color: var(--text);
}

input[type="range"] {
  padding: 0;
}

.switch-row {
  display: flex;
  gap: 8px;
}

.switch-row button {
  flex: 1;
}

.switch-row .active {
  background: var(--green);
  color: var(--ink);
  font-weight: 700;
}

.settings-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.settings-tabs button {
  background: #111820;
  border: 1px solid var(--line);
}

.settings-tabs .active {
  color: var(--ink);
  background: var(--green);
}

/* ─── Device selector tabs ─────────────────────────────────────────────────── */

.device-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.device-tab {
  position: relative;
  display: flex;
  align-items: center;
  gap: 7px;
  height: 36px;
  padding: 0 14px 0 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #111820;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
}

.device-tab:hover {
  color: var(--text);
  border-color: #4a5868;
  background: #181f28;
}

.device-tab.active {
  border-color: rgba(69, 212, 131, 0.6);
  background: rgba(69, 212, 131, 0.08);
  color: var(--text);
}

.device-tab .tab-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--red);
  flex-shrink: 0;
}

.device-tab .tab-dot.online {
  background: var(--green);
}

.device-tab .tab-name {
  font-weight: 600;
  color: inherit;
}

.device-tab .tab-idx {
  font-size: 11px;
  color: var(--muted);
  margin-left: 2px;
}

.split {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 0.9fr);
  gap: 18px;
}

pre,
.logs {
  height: 430px;
  overflow: auto;
  margin: 0;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #0d1116;
  color: #d8e0e8;
  font-size: 12px;
  line-height: 1.5;
}

.log-item {
  padding: 8px 0;
  border-bottom: 1px solid #1f2832;
}

.log-item:last-child {
  border-bottom: 0;
}

.log-item .meta {
  color: var(--muted);
  font-size: 12px;
}

.log-item .level-error {
  color: var(--red);
}

.log-item .level-out {
  color: var(--green);
}

.log-item .level-in,
.log-item .level-cmd {
  color: var(--blue);
}

.log-item pre {
  height: auto;
  max-height: 180px;
  margin-top: 8px;
  font-size: 11px;
}

@media (max-width: 1100px) {
  .sidebar {
    position: static;
    width: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .app {
    margin-left: 0;
  }

  .metric-grid,
  .device-grid,
  .scenario-grid,
  .settings-grid,
  .fault-grid,
  .flow-grid,
  .split {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 680px) {
  .topbar,
  .section-head {
    align-items: flex-start;
    flex-direction: column;
  }

  .metric-grid,
  .scenario-grid,
  .settings-grid,
  .fault-grid,
  .flow-grid,
  .split {
    grid-template-columns: 1fr;
  }

  .actions {
    width: 100%;
    justify-content: stretch;
  }

  .actions button {
    flex: 1;
  }
}
