:root {
  color-scheme: dark;
  --bg: #0b0f12;
  --panel: #151a1e;
  --panel-2: #1b2126;
  --line: #2b333a;
  --line-soft: #222a30;
  --text: #f4f7f8;
  --muted: #95a2ad;
  --muted-2: #6f7d88;
  --accent: #8ab4f8;
  --good: #6ee7a8;
  --warn: #ffd166;
  --bad: #ff7b7b;
  --chart-1: #f4f7f8;
  --chart-2: #8ab4f8;
  --chart-3: #6ee7a8;
  --chart-4: #ffd166;
}

* { box-sizing: border-box; }

html { background: var(--bg); }

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 85% 0%, rgba(138,180,248,.07), transparent 28rem),
    linear-gradient(180deg, #0b0f12 0%, #101419 100%);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, select { font: inherit; }

.shell {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 32px 0 56px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  margin-bottom: 22px;
}

.eyebrow,
.card-kicker {
  margin: 0 0 8px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .14em;
}

h1 {
  margin: 0;
  font-size: clamp(36px, 5vw, 56px);
  line-height: .95;
  letter-spacing: -.045em;
}

.subtitle {
  margin: 10px 0 0;
  color: var(--muted);
}

.status-wrap {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255,255,255,.035);
  color: var(--muted);
  font-size: 14px;
}

.live-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--warn);
  box-shadow: 0 0 14px rgba(255,209,102,.45);
}

.live-dot.online {
  background: var(--good);
  box-shadow: 0 0 14px rgba(110,231,168,.5);
}

.live-dot.offline {
  background: var(--bad);
  box-shadow: 0 0 14px rgba(255,123,123,.5);
}

.tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 18px;
  overflow-x: auto;
  padding-bottom: 2px;
}

.tab,
.range-button {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.025);
  color: var(--muted);
  border-radius: 999px;
  padding: 9px 14px;
  cursor: pointer;
  transition: .18s ease;
  white-space: nowrap;
}

.tab:hover,
.range-button:hover {
  color: var(--text);
  border-color: #3c4852;
}

.tab.active,
.range-button.active {
  color: var(--text);
  background: var(--panel-2);
  border-color: #46535e;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.overview-grid {
  display: grid;
  grid-template-columns: 1.6fr 1fr;
  gap: 18px;
}

.card {
  background:
    linear-gradient(180deg, rgba(255,255,255,.025), transparent),
    var(--panel);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 22px;
  box-shadow: 0 18px 60px rgba(0,0,0,.18);
}

.card h2 {
  margin: 0;
  font-size: 25px;
  letter-spacing: -.025em;
}

.card-head,
.chart-heading {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: flex-start;
}

.badge {
  min-width: 86px;
  text-align: center;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: .05em;
}

.badge.running {
  color: var(--good);
  border-color: rgba(110,231,168,.35);
  background: rgba(110,231,168,.08);
}

.hvac-readings {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-top: 28px;
}

.metric-tile,
.mini-stats > div,
.sensor-row {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.metric-tile { padding: 16px; }

.label,
.mini-stats span,
.system-meta span,
.stat-card span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 8px;
}

.metric-tile strong {
  display: block;
  font-size: clamp(26px, 4vw, 38px);
  letter-spacing: -.035em;
}

.mini-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}

.mini-stats > div { padding: 14px 16px; }
.mini-stats strong { font-size: 18px; }

.sensor-stack {
  display: grid;
  gap: 10px;
  margin-top: 22px;
}

.sensor-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 18px;
  padding: 16px;
}

.sensor-row span { color: var(--muted); }

.sensor-row strong {
  font-size: 24px;
  white-space: nowrap;
}

.sensor-row small {
  display: block;
  margin-top: 4px;
  color: var(--muted-2);
  font-size: 11px;
}

.reference-row {
  margin-top: 10px;
  border-style: dashed;
}

.reference-row .sensor-name {
  color: var(--text);
  font-weight: 650;
}

.chart-card { margin-top: 18px; }

.chart-wrap {
  position: relative;
  height: 300px;
  margin-top: 20px;
}

.chart-wrap canvas {
  width: 100%;
  height: 100%;
  display: block;
}

.chart-legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px 14px;
  max-width: 62%;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 12px;
}

.legend-swatch {
  width: 18px;
  height: 3px;
  border-radius: 99px;
  background: currentColor;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  text-align: center;
}

.hidden { display: none !important; }

.system-card {
  margin-top: 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 24px;
}

.system-meta {
  min-width: 180px;
  text-align: right;
}

.system-meta strong { font-size: 16px; }

.section-heading {
  margin: 12px 0 18px;
}

.section-heading h2 {
  margin: 0;
  font-size: 32px;
  letter-spacing: -.03em;
}

.section-heading > p:last-child,
.history-heading p {
  color: var(--muted);
  margin: 8px 0 0;
}

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

.stat-card {
  min-height: 130px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}

.stat-card strong {
  font-size: 28px;
  letter-spacing: -.025em;
}

.sensor-detail-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
}

.sensor-detail-card {
  min-width: 0;
}

.sensor-detail-top {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
}

.sensor-detail-card h3 {
  margin: 0;
  font-size: 22px;
}

.sensor-detail-temp {
  font-size: 30px;
  font-weight: 800;
  white-space: nowrap;
}

.sensor-role {
  display: inline-block;
  margin-top: 7px;
  padding: 5px 8px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
}

.sensor-meta {
  display: grid;
  gap: 7px;
  margin-top: 18px;
  color: var(--muted);
  font-size: 12px;
}

.sensor-meta code {
  color: #c8d2db;
  word-break: break-all;
}

.history-heading {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: flex-end;
}

.range-buttons {
  display: flex;
  gap: 8px;
}

.large-chart { margin-top: 0; }
.history-chart-wrap { height: 440px; }

.raw-card { margin-top: 18px; }

.registry-wrap {
  overflow-x: auto;
  margin-top: 18px;
}

.registry-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 760px;
}

.registry-table th,
.registry-table td {
  padding: 12px 10px;
  text-align: left;
  border-bottom: 1px solid var(--line-soft);
  font-size: 13px;
}

.registry-table th {
  color: var(--muted);
  font-size: 11px;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.registry-table code {
  color: #c8d2db;
  font-size: 12px;
}

@media (max-width: 960px) {
  .detail-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .sensor-detail-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 780px) {
  .shell {
    width: min(100% - 22px, 1180px);
    padding-top: 22px;
  }

  .topbar,
  .system-card,
  .history-heading {
    flex-direction: column;
    align-items: stretch;
  }

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

  .hvac-readings {
    grid-template-columns: 1fr;
  }

  .system-meta {
    text-align: left;
  }

  .chart-heading {
    flex-direction: column;
  }

  .chart-legend {
    justify-content: flex-start;
    max-width: none;
  }

  .chart-wrap {
    height: 260px;
  }

  .history-chart-wrap {
    height: 360px;
  }
}

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

  .mini-stats {
    grid-template-columns: 1fr;
  }

  .card {
    padding: 18px;
    border-radius: 17px;
  }
}


/* Ray V2: color-independent state communication */
.badge {
  color: var(--text);
  border-width: 2px;
}
.badge.running {
  color: var(--text);
  border-color: var(--text);
  background: repeating-linear-gradient(
    135deg,
    rgba(255,255,255,.09) 0,
    rgba(255,255,255,.09) 5px,
    rgba(255,255,255,.025) 5px,
    rgba(255,255,255,.025) 10px
  );
}
.status-wrap {
  color: var(--text);
}
.live-dot.online,
.live-dot.offline {
  box-shadow: none;
}
.live-dot.online {
  background: transparent;
  border: 2px solid var(--text);
}
.live-dot.offline {
  background: var(--text);
  border: 0;
}
.measurement-note {
  display: flex;
  gap: 12px;
  align-items: baseline;
  margin: -2px 0 18px;
  padding: 12px 14px;
  border-left: 3px solid var(--text);
  background: rgba(255,255,255,.035);
  color: var(--muted);
  font-size: 13px;
}
.measurement-note strong {
  color: var(--text);
  white-space: nowrap;
}
.chart-legend .legend-item:nth-child(1) .legend-swatch { height: 4px; }
.chart-legend .legend-item:nth-child(2) .legend-swatch {
  height: 4px;
  background: repeating-linear-gradient(90deg, currentColor 0 7px, transparent 7px 11px);
}
.chart-legend .legend-item:nth-child(3) .legend-swatch {
  height: 4px;
  background: repeating-linear-gradient(90deg, currentColor 0 2px, transparent 2px 6px);
}
.chart-legend .legend-item:nth-child(4) .legend-swatch {
  height: 6px;
  background: repeating-linear-gradient(135deg, currentColor 0 2px, transparent 2px 5px);
}


/* Toolkit */
.toolkit-card {
  margin-top: 4px;
}

.toolkit-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
}

.toolkit-copy {
  max-width: 720px;
  margin: 9px 0 0;
  color: var(--muted);
  line-height: 1.55;
}

.copy-button {
  appearance: none;
  flex: 0 0 auto;
  border: 2px solid var(--text);
  background: var(--text);
  color: var(--bg);
  border-radius: 10px;
  padding: 10px 15px;
  font-weight: 800;
  cursor: pointer;
}

.copy-button:hover {
  opacity: .88;
}

.copy-button:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
}

.toolkit-status {
  margin: 18px 0 10px;
  color: var(--muted);
  font-size: 12px;
}

.code-viewer {
  max-height: 620px;
  overflow: auto;
  margin: 0;
  padding: 18px;
  background: #090c0e;
  border: 1px solid var(--line);
  border-radius: 14px;
  color: #e8edf1;
  font: 13px/1.55 "Cascadia Code", "Consolas", ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre;
  tab-size: 2;
}

.toolkit-note {
  margin-top: 14px;
}

@media (max-width: 680px) {
  .toolkit-head {
    flex-direction: column;
  }

  .copy-button {
    width: 100%;
  }

  .code-viewer {
    max-height: 520px;
    font-size: 12px;
  }
}


/* Sensor editing */
.sensor-edit-actions {
  margin-top: 16px;
  display: flex;
  justify-content: flex-end;
}

.sensor-edit-button,
.sensor-save-button,
.sensor-cancel-button {
  appearance: none;
  border-radius: 9px;
  padding: 8px 12px;
  font-weight: 750;
  cursor: pointer;
  border: 1px solid var(--line);
}

.sensor-edit-button,
.sensor-cancel-button {
  background: var(--panel-2);
  color: var(--text);
}

.sensor-save-button {
  background: var(--text);
  color: var(--bg);
  border-color: var(--text);
}

.sensor-edit-form {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  display: grid;
  gap: 12px;
}

.sensor-edit-field {
  display: grid;
  gap: 6px;
}

.sensor-edit-field label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.sensor-edit-field input,
.sensor-edit-field select {
  width: 100%;
  box-sizing: border-box;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  padding: 10px 11px;
  font: inherit;
}

.sensor-edit-form-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.sensor-save-message {
  min-height: 20px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.sensor-save-message.error {
  color: #f0b8b8;
}
