:root {
  --bg: #eef3f5;
  --panel: #ffffff;
  --text: #0e1b2a;
  --muted: #5f6f7a;
  --line: #d8e1e7;
  --ok: #0c8a5b;
  --bad: #d52626;
  --teal: #0c7f78;
  --blue: #2368b3;
  --amber: #b66b07;
  --tension: #b12b47;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Microsoft YaHei", Arial, sans-serif;
  letter-spacing: 0;
  color: var(--text);
  background: var(--bg);
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: center;
  padding: 22px 28px;
  background: #f8fbfc;
  border-bottom: 1px solid var(--line);
}

h1, h2, p { margin: 0; }
h1 { font-size: 30px; }
h2 { font-size: 22px; }
#health { margin-top: 6px; color: var(--muted); }
.panel-subtitle { margin-top: 5px; color: var(--muted); font-size: 14px; }

main { padding: 20px; }
.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 16px;
  margin-bottom: 18px;
}

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

.card { padding: 18px; }
.card h2 { margin-bottom: 4px; }
.status { font-weight: 700; }
.status.online { color: var(--ok); }
.status.offline { color: var(--bad); }
.metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 14px;
  margin-top: 14px;
}
.metric {
  border-left: 3px solid var(--line);
  padding: 6px 10px;
  min-width: 0;
}
.metric strong {
  display: block;
  font-size: 19px;
  overflow-wrap: anywhere;
}
.metric span { color: var(--muted); font-size: 13px; }

.panel { padding: 18px; margin-bottom: 18px; }
.panel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}
.chart-controls {
  display: flex;
  align-items: end;
  flex-wrap: wrap;
  gap: 10px;
}
.field-control {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 13px;
}
.field-control select { min-width: 120px; color: var(--text); }
.field-control input { min-width: 148px; color: var(--text); }
select, input, button, .button {
  font: inherit;
  border: 1px solid #b8c7d0;
  border-radius: 6px;
  padding: 9px 12px;
  background: white;
}
button, .button {
  background: var(--teal);
  color: white;
  border-color: #075f5a;
  cursor: pointer;
  text-decoration: none;
}
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
button:disabled {
  cursor: wait;
  opacity: 0.65;
}
button:focus-visible, select:focus-visible, .button:focus-visible {
  outline: 3px solid rgba(35, 104, 179, 0.28);
  outline-offset: 2px;
}

.metric-switcher {
  display: inline-flex;
  max-width: 100%;
  margin-top: 8px;
  border: 1px solid #b8c7d0;
  border-radius: 6px;
  overflow: hidden;
}
.metric-switcher button {
  min-width: 86px;
  border: 0;
  border-right: 1px solid #b8c7d0;
  border-radius: 0;
  background: #ffffff;
  color: #273746;
}
.metric-switcher button:last-child { border-right: 0; }
.metric-switcher button.active {
  background: #273746;
  color: #ffffff;
}
.trend-filter-row {
  display: flex;
  align-items: end;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 8px;
}
.trend-filter-row .metric-switcher { margin-top: 0; }
.period-controls {
  display: flex;
  align-items: end;
  flex-wrap: wrap;
  gap: 10px;
}
.range-switcher {
  display: inline-flex;
  border: 1px solid #b8c7d0;
  border-radius: 6px;
  overflow: hidden;
}
.range-switcher button {
  min-width: 58px;
  border: 0;
  border-right: 1px solid #b8c7d0;
  border-radius: 0;
  background: #ffffff;
  color: #273746;
}
.range-switcher button:last-child { border-right: 0; }
.range-switcher button.active {
  background: #273746;
  color: #ffffff;
}
.period-field { min-width: 148px; }
.period-field input[hidden] { display: none; }
.command-status {
  min-height: 22px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 14px;
}
.command-status.pending { color: var(--blue); }
.command-status.success { color: var(--ok); }
.command-status.error { color: var(--bad); }
.chart-shell {
  width: 100%;
  margin-top: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: #ffffff;
}

canvas {
  display: block;
  width: 100%;
  height: 420px;
  background: white;
}
.chart-legend {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin-top: 10px;
  color: var(--muted);
}
.chart-legend::before {
  content: "";
  width: 20px;
  height: 3px;
}
.chart-legend.distance::before { background: var(--teal); }
.chart-legend.temp::before { background: var(--amber); }
.chart-legend.humidity::before { background: var(--blue); }
.chart-legend.tension::before { background: var(--tension); }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 820px; }
th, td { border-bottom: 1px solid var(--line); padding: 10px 8px; text-align: left; }
th { color: #273746; }

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
}
.login-box {
  width: min(420px, calc(100vw - 28px));
  padding: 28px;
}
.login-box h1 { margin-bottom: 24px; }
.login-box label { display: block; margin-bottom: 14px; }
.login-box input { display: block; width: 100%; margin-top: 6px; }
.login-box button { width: 100%; }
.error { margin-top: 12px; color: var(--bad); }

@media (max-width: 700px) {
  .topbar { align-items: flex-start; flex-direction: column; padding: 18px; }
  h1 { font-size: 25px; }
  main { padding: 12px; }
  .panel { padding: 14px; }
  .panel-head { align-items: flex-start; flex-wrap: wrap; }
  .chart-controls { width: 100%; align-items: end; }
  .field-control { flex: 1 1 130px; }
  .field-control select { width: 100%; }
  #measureNowBtn { flex: 1 1 130px; }
  .metric-switcher { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); width: 100%; }
  .metric-switcher button { min-width: 0; border-bottom: 1px solid #b8c7d0; }
  .metric-switcher button:nth-child(2) { border-right: 0; }
  .metric-switcher button:nth-child(n+3) { border-bottom: 0; }
  .trend-filter-row, .period-controls { width: 100%; }
  .period-controls { display: grid; grid-template-columns: auto minmax(0, 1fr); }
  .period-field, .period-field input { width: 100%; min-width: 0; }
  .actions { width: 100%; }
  .actions > * { flex: 1 1 120px; text-align: center; }
  .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  canvas { height: 300px; }
}
