:root {
  color-scheme: light;
  --bg: #f7f8f6;
  --surface: #ffffff;
  --text: #18201c;
  --muted: #69716d;
  --line: #dfe3df;
  --green: #177245;
  --green-soft: #e7f3ec;
  --red: #c23b33;
  --red-soft: #fbe8e5;
  --amber: #a9660e;
  --amber-soft: #fff3dc;
  --blue: #276b82;
  --shadow: 0 6px 24px rgba(24, 32, 28, 0.06);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-width: 320px;
}

button, select, input, a { font: inherit; }

.topbar {
  min-height: 64px;
  padding: 10px max(20px, calc((100vw - 1240px) / 2));
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.96);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand, .topbar-meta, .section-heading, .primary-actions, .service-main, .service-meta {
  display: flex;
  align-items: center;
}

.brand { gap: 11px; }
.brand-link { color: inherit; text-decoration: none; }
.brand-logo {
  width: 42px;
  height: 42px;
  object-fit: contain;
  flex: 0 0 auto;
}
.brand-mark {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  color: #fff;
  background: #1f5b3d;
  border-radius: 6px;
  font-size: 13px;
  font-weight: 800;
}

.brand strong, .brand span { display: block; }
.brand strong { font-size: 15px; }
.brand div > span { margin-top: 2px; color: var(--muted); font-size: 12px; }
.topbar-meta { gap: 20px; color: var(--muted); font-size: 13px; }
.topbar-link { color: #235d40; font-weight: 750; text-decoration: none; }
.topbar-link:hover { text-decoration: underline; }
.connection { display: flex; align-items: center; gap: 7px; }
.connection i, .service-state i {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #9da4a0;
}
.connection.online i, .service-state.online i { background: #27a05d; box-shadow: 0 0 0 3px #dff3e7; }
.connection.live i { background: #27a05d; box-shadow: 0 0 0 3px #dff3e7; animation: live-pulse 1.8s ease-out infinite; }
.connection.connecting i, .connection.fallback i { background: #cc8a1b; box-shadow: 0 0 0 3px #fff0cf; }
.connection.offline i, .service-state.offline i { background: var(--red); box-shadow: 0 0 0 3px var(--red-soft); }

@keyframes live-pulse {
  0%, 45% { box-shadow: 0 0 0 3px #dff3e7; }
  75%, 100% { box-shadow: 0 0 0 7px transparent; }
}

main { max-width: 1240px; margin: 0 auto; padding: 38px 20px 64px; }
section { padding: 0 0 42px; margin: 0 0 42px; border-bottom: 1px solid var(--line); }
section:last-child { border-bottom: 0; margin-bottom: 0; }

.section-heading { justify-content: space-between; gap: 24px; margin-bottom: 22px; }
.section-heading.compact { margin-bottom: 18px; }
.eyebrow {
  margin: 0 0 5px;
  color: var(--green);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}
h1, h2 { margin: 0; letter-spacing: 0; }
h1 { font-size: 34px; line-height: 1.08; }
h2 { font-size: 23px; line-height: 1.2; }

.room-picker { display: grid; gap: 6px; min-width: min(330px, 100%); }
.room-picker span { color: var(--muted); font-size: 12px; font-weight: 700; }
select {
  width: 100%;
  min-height: 42px;
  padding: 0 38px 0 12px;
  border: 1px solid #cbd1cc;
  border-radius: 6px;
  color: var(--text);
  background: var(--surface);
}

.status-band {
  min-height: 132px;
  padding: 24px 28px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 20px;
  border: 1px solid #b9ddc8;
  border-radius: 8px;
  background: var(--green-soft);
  box-shadow: var(--shadow);
}
.status-band.alarm { background: var(--red-soft); border-color: #efb1aa; }
.status-band.warning { background: var(--amber-soft); border-color: #edce94; }
.status-band.waiting { background: #eef0ee; border-color: #d4d8d5; }
.status-symbol {
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  border: 2px solid var(--green);
  border-radius: 50%;
}
.status-symbol span { width: 16px; height: 16px; border-radius: 50%; background: var(--green); }
.alarm .status-symbol { border-color: var(--red); }
.alarm .status-symbol span { background: var(--red); animation: pulse 1.2s infinite; }
.warning .status-symbol { border-color: var(--amber); }
.warning .status-symbol span { background: var(--amber); }
.waiting .status-symbol { border-color: #858d88; }
.waiting .status-symbol span { background: #858d88; }
@keyframes pulse { 50% { transform: scale(.62); opacity: .58; } }
.status-copy { display: grid; gap: 4px; min-width: 0; }
.status-label, .score-block span, .summary-strip span, .metric-card span { color: var(--muted); font-size: 12px; font-weight: 700; }
.status-copy strong { font-size: 27px; line-height: 1.15; }
.status-copy > span:last-child { color: var(--muted); font-size: 13px; }
.score-block { min-width: 145px; text-align: right; border-left: 1px solid rgba(24,32,28,.16); padding-left: 24px; }
.score-block span { display: block; }
.score-block strong { font-size: 48px; line-height: 1; }
.score-block small { color: var(--muted); }

.summary-strip {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  margin: 16px 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}
.summary-strip div { padding: 14px 18px; border-right: 1px solid var(--line); }
.summary-strip div:last-child { border-right: 0; }
.summary-strip span, .summary-strip strong { display: block; }
.summary-strip strong { margin-top: 4px; font-size: 18px; }

.sensor-health-panel { margin: 18px 0; padding: 14px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); }
.sensor-health-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.sensor-health-heading strong { font-size: 14px; }
.sensor-health-heading span { color: var(--muted); font-size: 11px; }
.sensor-health-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 8px; }
.sensor-health-grid > div { min-height: 56px; padding: 9px; border: 1px solid var(--line); border-radius: 6px; background: #f6f8f6; }
.sensor-health-grid > div.ok { border-color: #8fc7a3; background: var(--green-soft); }
.sensor-health-grid > div.error { border-color: #e1aaa5; background: var(--red-soft); }
.sensor-health-grid strong, .sensor-health-grid span { display: block; }
.sensor-health-grid strong { font-size: 11px; }
.sensor-health-grid span { margin-top: 4px; color: var(--muted); font-size: 10px; line-height: 1.25; }

.metric-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.metric-card {
  min-height: 116px;
  padding: 17px;
  border: 1px solid var(--line);
  border-top: 4px solid #7d8580;
  border-radius: 6px;
  background: var(--surface);
}
.metric-card strong { display: block; margin: 12px 0 2px; font-size: 29px; line-height: 1; }
.metric-card small { color: var(--muted); }
.accent-red { border-top-color: #cf4e45; }
.accent-orange { border-top-color: #d9852b; }
.accent-teal { border-top-color: #258c84; }
.accent-violet { border-top-color: #7a62a8; }
.accent-yellow { border-top-color: #c99b26; }
.accent-green { border-top-color: #3b8d5c; }
.accent-blue { border-top-color: #3f80a5; }

.primary-actions { gap: 10px; margin-top: 18px; }
.button {
  min-height: 42px;
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid #235d40;
  border-radius: 6px;
  text-decoration: none;
  font-size: 14px;
  font-weight: 750;
}
.button.primary { background: #235d40; color: #fff; }
.button.secondary { background: var(--surface); color: #235d40; }
.button:hover { filter: brightness(.96); }

.chart-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.chart-panel {
  margin: 0;
  min-width: 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}
.chart-panel figcaption { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; margin-bottom: 10px; }
.chart-panel figcaption span { font-size: 13px; font-weight: 750; }
.chart-panel figcaption strong { font-size: 18px; }
canvas { display: block; width: 100%; height: 230px; }
.muted { color: var(--muted); font-size: 12px; }

.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 22px;
}
.heading-actions { display: flex; align-items: center; gap: 8px; }
.compact-button { min-height: 38px; padding: 0 13px; }
.service-list { border-top: 1px solid var(--line); }
.service-row {
  min-height: 76px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 22px;
  padding: 13px 4px;
  border-bottom: 1px solid var(--line);
}
.service-main { gap: 13px; min-width: 0; }
.service-badge, .service-logo {
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid #ccd2ce;
  border-radius: 6px;
  background: var(--surface);
  color: #315b46;
  font-size: 11px;
  font-weight: 850;
}
.service-logo { object-fit: contain; padding: 4px; }
.service-main strong, .service-main span { display: block; }
.service-main strong { font-size: 14px; }
.service-main span { margin-top: 3px; color: var(--muted); font-size: 12px; }
.service-main > .service-badge { margin-top: 0; color: #315b46; }
.service-meta { gap: 18px; }
.service-state { min-width: 100px; display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.service-link { color: #1f5b3d; font-size: 13px; font-weight: 750; text-decoration: none; }
.service-link:hover { text-decoration: underline; }
.service-link.disabled { color: var(--muted); pointer-events: none; }

.config-dialog {
  width: min(760px, calc(100vw - 28px));
  max-height: min(820px, calc(100vh - 28px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 22px 70px rgba(20, 29, 24, .22);
}
.config-dialog::backdrop { background: rgba(18, 27, 22, .46); }
.config-dialog form { padding: 24px; }
.dialog-heading, .link-editor-heading, .dialog-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.dialog-heading { margin-bottom: 22px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid label, .link-row label { display: grid; gap: 6px; }
.form-grid label > span, .link-row label > span { color: var(--muted); font-size: 12px; font-weight: 700; }
.form-grid .full { grid-column: 1 / -1; }
input {
  width: 100%;
  min-height: 42px;
  padding: 9px 11px;
  border: 1px solid #cbd1cc;
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}
input:focus, select:focus { outline: 2px solid rgba(35, 93, 64, .2); border-color: #397b59; }
.link-editor { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line); }
.link-editor-heading { margin-bottom: 10px; }
.text-button { padding: 4px 0; border: 0; background: transparent; color: #235d40; font-size: 13px; font-weight: 750; cursor: pointer; }
.link-row { display: grid; grid-template-columns: minmax(150px, .8fr) minmax(230px, 1.6fr) auto; align-items: end; gap: 10px; margin-top: 10px; }
.remove-link { width: 42px; min-height: 42px; border: 1px solid #e0bbb6; border-radius: 6px; background: #fff7f6; color: #9f312b; cursor: pointer; font-size: 20px; }
.empty-links { margin: 10px 0 0; color: var(--muted); font-size: 13px; }
.form-status { min-height: 20px; margin: 18px 0 0; color: var(--muted); font-size: 12px; }
.form-status.error { color: var(--red); }
.form-status.success { color: var(--green); }
.dialog-actions { justify-content: flex-end; margin-top: 16px; }

footer {
  min-height: 62px;
  padding: 18px max(20px, calc((100vw - 1240px) / 2));
  display: flex;
  justify-content: space-between;
  gap: 20px;
  border-top: 1px solid var(--line);
  background: #edf0ed;
  color: var(--muted);
  font-size: 12px;
}

@media (max-width: 860px) {
  .metric-grid { grid-template-columns: repeat(2, 1fr); }
  .sensor-health-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .chart-grid { grid-template-columns: 1fr; }
  .status-band { grid-template-columns: auto 1fr; }
  .score-block { grid-column: 1 / -1; border-left: 0; border-top: 1px solid rgba(24,32,28,.16); padding: 16px 0 0; text-align: left; }
}

@media (max-width: 620px) {
  .topbar { position: static; }
  .topbar-meta #clock { display: none; }
  .topbar-link { display: none; }
  main { padding: 26px 14px 42px; }
  .section-heading { align-items: stretch; flex-direction: column; }
  .section-heading.compact { flex-direction: row; align-items: center; }
  h1 { font-size: 29px; }
  .status-band { padding: 19px; gap: 14px; }
  .status-symbol { width: 44px; height: 44px; }
  .status-copy strong { font-size: 22px; }
  .summary-strip { grid-template-columns: 1fr 1fr; }
  .summary-strip div:nth-child(2) { border-right: 0; }
  .summary-strip div:nth-child(-n+2) { border-bottom: 1px solid var(--line); }
  .sensor-health-heading { align-items: flex-start; flex-direction: column; }
  .sensor-health-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .metric-grid { gap: 8px; }
  .metric-card { min-height: 104px; padding: 14px; }
  .metric-card strong { font-size: 25px; }
  .primary-actions { align-items: stretch; flex-direction: column; }
  .service-row { grid-template-columns: minmax(0, 1fr) auto; gap: 10px; }
  .service-meta { grid-column: 1 / -1; padding-left: 53px; justify-content: space-between; }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .full { grid-column: auto; }
  .link-row { grid-template-columns: 1fr auto; }
  .link-row label:nth-child(2) { grid-column: 1 / -1; grid-row: 2; }
  .link-row .remove-link { grid-column: 2; grid-row: 1; }
  .config-dialog form { padding: 18px; }
  footer { flex-direction: column; }
}
