:root {
  color-scheme: dark;
  --bg: #0d1110;
  --surface: #151b19;
  --surface-2: #1b2320;
  --surface-3: #202b27;
  --line: #2e3a36;
  --text: #e9eeeb;
  --muted: #9aa7a1;
  --subtle: #6f7d77;
  --green: #51b36c;
  --green-soft: rgba(81, 179, 108, 0.14);
  --amber: #f0ad4e;
  --orange: #df8500;
  --red: #ea5a4f;
  --blue: #5a9bd6;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  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;
}

.app-shell {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 22px 14px;
  background: #111715;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 28px;
}

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

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #3b8f5c, #df8500);
  color: #101412;
  font-weight: 900;
}

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

.brand h1 {
  font-size: 18px;
}

.brand p,
.topbar p,
.panel p {
  color: var(--muted);
}

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

.nav-item {
  width: 100%;
  border: 0;
  border-radius: 8px;
  color: #cdd6d1;
  background: transparent;
  padding: 11px 10px;
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: 10px;
  align-items: center;
  text-align: left;
  cursor: pointer;
}

.nav-item.active {
  background: #1d332d;
  color: #8bf0ad;
}

.nav-item.disabled,
.nav-item:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.nav-item.join-open {
  background: #1d332d;
  color: #9ff0b5;
}

.nav-item.join-open .nav-icon,
.nav-item.join-open .nav-count {
  background: rgba(81, 179, 108, 0.22);
  color: #baf4c7;
}

.nav-icon {
  width: 24px;
  height: 24px;
  border-radius: 7px;
  display: grid;
  place-items: center;
  background: #24302c;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.active .nav-icon {
  background: #26533f;
  color: #a8f7bd;
}

.nav-count,
.pill {
  min-width: 24px;
  height: 24px;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
  background: #26302d;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.nav-count.danger {
  background: rgba(234, 90, 79, 0.16);
  color: #ff948d;
}

.side-health {
  margin-top: auto;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.label {
  display: block;
  color: var(--subtle);
  font-size: 11px;
  text-transform: uppercase;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--amber);
  display: inline-block;
}

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

.status-dot.bad {
  background: var(--red);
}

.main {
  padding: 20px;
  min-width: 0;
}

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

.topbar > div {
  display: flex;
  gap: 12px;
  align-items: center;
}

.topbar h2 {
  font-size: 24px;
  font-weight: 750;
}

.top-status {
  display: flex;
  align-items: center;
  gap: 14px;
  color: var(--muted);
  font-size: 14px;
}

.connection {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.alerts-control {
  position: relative;
}

.alerts-bell {
  position: relative;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--muted);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: border-color 160ms ease, color 160ms ease, background 160ms ease;
}

.alerts-bell:hover {
  color: var(--text);
  border-color: var(--subtle);
}

.alerts-bell.has-critical {
  color: #ff948d;
  border-color: rgba(234, 90, 79, 0.55);
}

.alerts-bell.has-warning {
  color: #ffd18d;
  border-color: rgba(240, 173, 78, 0.5);
}

.alerts-bell.open {
  background: var(--surface-2);
  color: var(--text);
  border-color: var(--green);
}

.alerts-badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--red);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  display: inline-grid;
  place-items: center;
  border: 2px solid var(--bg);
}

.alerts-badge.warning {
  background: var(--orange);
}

.alerts-menu {
  position: absolute;
  top: calc(100% + 10px);
  right: 0;
  width: 340px;
  max-width: calc(100vw - 28px);
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 14px;
  z-index: 30;
}

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

.alerts-menu-header h3 {
  font-size: 16px;
  margin: 0;
}

.alerts-menu-header p {
  margin: 3px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.alerts-menu .alert-list {
  max-height: 320px;
}

.alerts-menu .empty-state {
  min-height: 90px;
}

.menu-button {
  display: none;
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
}

.menu-button span {
  display: block;
  width: 16px;
  height: 2px;
  margin: 3px auto;
  background: var(--text);
}

.summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(142px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}

.summary-card,
.panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.summary-card {
  min-height: 112px;
  padding: 16px;
}

.summary-card .metric {
  margin-top: 14px;
  font-size: 30px;
  font-weight: 780;
  line-height: 1.06;
  overflow: hidden;
  text-overflow: ellipsis;
}

.summary-card .metric-time {
  white-space: nowrap;
}

.metric-sub {
  display: block;
  margin-top: 8px;
  color: var(--subtle);
  font-size: 12px;
  font-weight: 700;
}

.summary-card small,
.sensor-card small,
.detail-row span:first-child {
  color: var(--muted);
}

.summary-card.critical {
  border-color: rgba(234, 90, 79, 0.48);
  background: linear-gradient(180deg, rgba(234, 90, 79, 0.12), var(--surface));
}

.summary-card.warning {
  border-color: rgba(240, 173, 78, 0.42);
}

.summary-card.good {
  border-color: rgba(81, 179, 108, 0.42);
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.55fr) minmax(320px, 0.68fr);
  gap: 12px;
  margin-bottom: 12px;
}

.lower-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.network-section {
  margin-top: 12px;
}

.panel {
  min-width: 0;
  padding: 16px;
}

.panel-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: start;
  margin-bottom: 14px;
}

.panel-header h3 {
  font-size: 17px;
}

.panel-header p {
  margin-top: 3px;
  font-size: 13px;
}

.detail-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-start;
  margin-top: 16px;
}

.detail-actions .action-button {
  width: auto;
}

.remove-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.remove-button svg {
  flex: none;
}

.management-note {
  margin-top: 12px;
}

.switch {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}

.switch input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.switch-track {
  width: 68px;
  height: 38px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #202724;
  position: relative;
  transition: background 160ms ease, border-color 160ms ease;
}

.switch-thumb {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #cfd7d2;
  position: absolute;
  top: 4px;
  left: 4px;
  transition: transform 160ms ease, background 160ms ease;
}

.switch input:checked + .switch-track {
  background: rgba(81, 179, 108, 0.18);
  border-color: rgba(81, 179, 108, 0.52);
}

.switch input:checked + .switch-track .switch-thumb {
  transform: translateX(30px);
  background: #9ff0b5;
}

.switch input:disabled + .switch-track {
  opacity: 0.55;
}

.action-button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  padding: 0 14px;
  cursor: pointer;
}

.action-button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.action-button.primary {
  border-color: rgba(81, 179, 108, 0.5);
  background: rgba(81, 179, 108, 0.16);
  color: #baf4c7;
}

.action-button.danger {
  border-color: rgba(234, 90, 79, 0.5);
  background: rgba(234, 90, 79, 0.12);
  color: #ffaea8;
}

.ok-pill {
  background: var(--green-soft);
  color: #92e8aa;
}

.control-feedback {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
}

.success-feedback {
  background: rgba(81, 179, 108, 0.14);
  color: #9ff0b5;
}

.error-feedback {
  background: rgba(234, 90, 79, 0.14);
  color: #ffaea8;
}

.network-map {
  display: grid;
  gap: 0;
  margin-top: 12px;
}

.network-svg-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #101513;
  overflow: auto;
}

.network-svg {
  width: 100%;
  min-height: 520px;
  display: block;
}

.network-raw {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #101513;
  color: var(--muted);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.settings-section {
  margin-top: 12px;
}

.settings-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 24px;
  padding: 18px 0;
  border-top: 1px solid var(--line);
}

.settings-row:first-of-type {
  border-top: 0;
  padding-top: 4px;
}

.settings-label {
  max-width: 520px;
}

.settings-label strong {
  font-size: 14px;
}

.settings-label p {
  margin-top: 4px;
  font-size: 13px;
  color: var(--muted);
}

.settings-control {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: none;
}

.offset-control {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.offset-control input[type="range"] {
  width: 160px;
  min-height: auto;
  border: 0;
  background: transparent;
  padding: 0;
  accent-color: var(--green);
}

.offset-value {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.offset-value input[type="number"] {
  width: 66px;
  text-align: right;
}

.offset-unit {
  color: var(--muted);
}

.pair-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.pair-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--subtle);
}

.pair-button.join-open {
  border-color: rgba(81, 179, 108, 0.5);
  background: rgba(81, 179, 108, 0.16);
  color: #baf4c7;
}

.pair-button.join-open .pair-dot {
  background: var(--green);
}

select,
input {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #101513;
  color: var(--text);
  padding: 0 10px;
}

canvas {
  width: 100%;
  height: 310px;
  display: block;
}

.chart-wrap {
  position: relative;
  touch-action: none;
}

.chart-tooltip {
  position: absolute;
  z-index: 3;
  min-width: 150px;
  border: 1px solid rgba(81, 179, 108, 0.5);
  border-radius: 8px;
  background: rgba(13, 17, 16, 0.94);
  box-shadow: var(--shadow);
  color: var(--text);
  padding: 8px 10px;
  pointer-events: none;
  transform: translate(-50%, -112%);
}

.chart-tooltip strong,
.chart-tooltip span {
  display: block;
}

.chart-tooltip strong {
  font-size: 18px;
}

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

.alert-list {
  display: grid;
  gap: 10px;
  max-height: 360px;
  overflow: auto;
  padding-right: 4px;
}

.alert-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: border-color 160ms ease, background 160ms ease;
}

.alert-item.sev-critical {
  border-color: rgba(234, 90, 79, 0.45);
  background: linear-gradient(180deg, rgba(234, 90, 79, 0.1), var(--surface-2));
}

.alert-item.sev-warning {
  border-color: rgba(240, 173, 78, 0.42);
  background: linear-gradient(180deg, rgba(240, 173, 78, 0.09), var(--surface-2));
}

.alert-item:hover {
  background: #222a26;
}

.alert-item.sev-critical:hover {
  border-color: var(--red);
}

.alert-item.sev-warning:hover {
  border-color: var(--orange);
}

.alert-name {
  display: block;
  font-size: 14px;
  margin-bottom: 9px;
}

.alert-issues {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 7px;
}

.alert-issues li {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}

.alert-tag {
  flex: none;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.alert-tag.sev-critical {
  background: rgba(234, 90, 79, 0.18);
  color: #ff948d;
}

.alert-tag.sev-warning {
  background: rgba(240, 173, 78, 0.18);
  color: #ffd18d;
}

.alert-detail {
  color: var(--muted);
}

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

.search-field {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 200px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #101513;
}

.search-field svg {
  width: 16px;
  height: 16px;
  flex: none;
  color: var(--subtle);
}

.search-field input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  padding: 0;
  min-height: 38px;
}

.search-field input:focus {
  outline: none;
}

.sort-bar {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface-2);
  max-width: 100%;
  overflow-x: auto;
}

.sort-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 7px 11px;
  border-radius: 7px;
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  transition: background 140ms ease, color 140ms ease;
}

.sort-chip:hover {
  color: var(--text);
}

.sort-chip.active {
  background: var(--green-soft);
  color: #baf4c7;
}

.sort-arrow {
  display: none;
  width: 0;
  height: 0;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-bottom: 5px solid currentColor;
  transition: transform 140ms ease;
}

.sort-chip.active .sort-arrow {
  display: inline-block;
}

.sort-chip.active.desc .sort-arrow {
  transform: rotate(180deg);
}

.sensor-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 10px;
}

.sensor-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  padding: 13px;
  cursor: pointer;
  transition: border-color 160ms ease, background 160ms ease;
}

.sensor-card:hover,
.sensor-card.active {
  border-color: var(--green);
  background: #1d2924;
}

.sensor-top {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.sensor-name {
  font-weight: 750;
}

.moisture-row {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 10px;
}

.moisture-value {
  font-size: 38px;
  line-height: 1;
  font-weight: 820;
}

.moisture-bar {
  height: 8px;
  margin-top: 10px;
  border-radius: 999px;
  background: #2a322f;
  overflow: hidden;
}

.moisture-bar span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--green);
}

.moisture-bar.unknown span {
  display: none;
}

.bad .moisture-bar span {
  background: var(--red);
}

.warning .moisture-bar span {
  background: var(--amber);
}

.sensor-meta,
.detail-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 12px;
  font-size: 13px;
}

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 12px;
}

.badge {
  border-radius: 999px;
  padding: 4px 8px;
  background: #26302d;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.badge.ok {
  background: var(--green-soft);
  color: #92e8aa;
}

.badge.warning {
  background: rgba(240, 173, 78, 0.14);
  color: #ffd18d;
}

.badge.bad {
  background: rgba(234, 90, 79, 0.16);
  color: #ff9b93;
}

.detail-panel h3 {
  margin: 0 0 8px;
}

.detail-metric {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin: 16px 0;
}

.detail-metric strong {
  font-size: 56px;
  line-height: 1;
}

.detail-metric.unknown strong,
.detail-metric.unknown span {
  color: var(--muted);
}

.detail-list {
  display: grid;
  gap: 10px;
}

.detail-row {
  margin: 0;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--line);
}

.empty-detail,
.empty-state {
  min-height: 180px;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-align: center;
}

.hidden {
  display: none;
}

@media (max-width: 1120px) {
  .summary-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .content-grid,
  .lower-grid {
    grid-template-columns: 1fr;
  }
}

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

  .sidebar {
    position: fixed;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 180ms ease;
    width: 260px;
  }

  body.nav-open .sidebar {
    transform: translateX(0);
  }

  .main {
    padding: 14px;
  }

  .menu-button {
    display: block;
  }

  .topbar,
  .topbar > div,
  .panel-header.tools,
  .detail-actions {
    align-items: start;
  }

  .topbar,
  .panel-header,
  .panel-header.tools,
  .toolbar,
  .detail-actions {
    flex-direction: column;
  }

  .topbar {
    height: auto;
    margin-bottom: 22px;
  }

  .top-status {
    width: 100%;
    align-items: flex-start;
  }

  .alerts-menu {
    position: fixed;
    top: 158px;
    left: 14px;
    right: 14px;
    width: auto;
    max-width: none;
    max-height: calc(100vh - 188px);
    overflow: hidden;
    z-index: 60;
  }

  .alerts-menu .alert-list {
    max-height: calc(100vh - 280px);
    overflow: auto;
  }

  .settings-row {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .settings-control,
  .offset-control {
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .summary-grid {
    grid-template-columns: 1fr;
  }

  .toolbar,
  .detail-actions,
  .toolbar input,
  .toolbar select,
  .search-field,
  .sort-bar,
  .action-button {
    width: 100%;
  }

  .sort-chip {
    flex: 1;
    justify-content: center;
  }

  .switch-track {
    width: 64px;
  }

}

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