:root {
  color-scheme: dark;
  --bg: #0b1118;
  --panel: #121c27;
  --panel-2: #182635;
  --text: #f3f7fb;
  --muted: #8fa2b5;
  --blue: #4cb5ff;
  --green: #57d49b;
  --red: #ff7a7a;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; min-height: 100%; background: var(--bg); }

body {
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--text);
  background:
    radial-gradient(circle at 90% 0%, rgba(76, 181, 255, .16), transparent 42%),
    var(--bg);
}

.dashboard {
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  padding: clamp(18px, 4vw, 32px);
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  gap: clamp(14px, 3vw, 22px);
}

.topbar, .hero, .footer { display: flex; align-items: center; justify-content: space-between; }

.top-actions { display: flex; align-items: center; gap: 8px; }

.view-toggle, .range-buttons button {
  border: 1px solid rgba(76,181,255,.28);
  border-radius: 999px;
  background: rgba(76,181,255,.1);
  color: var(--text);
  font: inherit;
  font-size: .78rem;
  font-weight: 700;
  cursor: pointer;
}

.view-toggle { padding: 8px 11px; }
.view-toggle:focus-visible, .range-buttons button:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

.eyebrow, .label, .footer p {
  margin: 0;
  color: var(--muted);
  font-size: .78rem;
  font-weight: 700;
  letter-spacing: .1em;
  text-transform: uppercase;
}

h1 { margin: 3px 0 0; font-size: clamp(1.7rem, 6vw, 2.6rem); line-height: 1; }

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 999px;
  color: var(--muted);
  font-size: .82rem;
}

.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #f5bd4f; box-shadow: 0 0 12px #f5bd4f; }
.status.live .status-dot { background: var(--green); box-shadow: 0 0 12px var(--green); }
.status.stale .status-dot { background: var(--red); box-shadow: 0 0 12px var(--red); }
.status.version-warning { color: var(--red); border-color: rgba(255,122,122,.28); }
.status.version-warning .status-dot { background: var(--red); box-shadow: 0 0 12px var(--red); }

.hero {
  padding: clamp(18px, 5vw, 30px);
  border: 1px solid rgba(76,181,255,.2);
  border-radius: 22px;
  background: linear-gradient(135deg, rgba(76,181,255,.16), rgba(24,38,53,.92));
}

.hero strong { display: block; margin-top: 7px; font-size: clamp(1.7rem, 7vw, 3rem); }
.hero .mode { color: var(--blue); }
.mode-reason {
  display: block;
  max-width: min(440px, 62vw);
  margin-top: 5px;
  color: var(--muted);
  font-size: clamp(.68rem, 1.8vw, .82rem);
  line-height: 1.25;
}
.outside { text-align: right; }
small { font-size: .48em; color: var(--muted); }

.rooms { display: grid; grid-template-columns: repeat(3, 1fr); gap: clamp(9px, 2vw, 15px); }

.room-card {
  min-width: 0;
  padding: clamp(14px, 3.5vw, 22px);
  border: 1px solid rgba(255,255,255,.07);
  border-radius: 18px;
  background: var(--panel);
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.room-card strong { margin: 8px 0 3px; font-size: clamp(1.55rem, 6vw, 2.5rem); }

.target-state {
  margin-bottom: 14px;
  color: var(--muted);
  font-size: clamp(.65rem, 1.8vw, .78rem);
  font-weight: 600;
  white-space: nowrap;
}

.temperature-delta { margin-left: 4px; font-weight: 800; }
.temperature-delta.warm { color: var(--red); }
.temperature-delta.cool { color: var(--blue); }
.temperature-delta.neutral { color: var(--muted); }

.device-state {
  width: fit-content;
  padding: 5px 8px;
  border-radius: 7px;
  background: rgba(143,162,181,.12);
  color: var(--muted);
  font-size: clamp(.65rem, 2vw, .78rem);
  font-weight: 700;
  white-space: nowrap;
}

.device-state.on { color: var(--red); background: rgba(255,122,122,.12); }

.footer {
  padding: 13px 16px;
  border-radius: 14px;
  background: rgba(18,28,39,.72);
}

.system-states { display: flex; align-items: center; gap: 18px; }
.system-state { display: flex; align-items: baseline; gap: 7px; }
.system-state strong { color: var(--green); }
.system-state strong.off { color: var(--muted); }
.footer p { font-size: .67rem; letter-spacing: .04em; text-align: right; }
.script-version { color: var(--muted); }
.dashboard-version { color: var(--muted); }
.script-version.mismatch { color: var(--red); }

.dashboard.history-mode { grid-template-rows: auto 1fr; }

.history-view {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(280px, 1fr) auto auto;
  gap: 12px;
}

.history-view[hidden] { display: none; }

.history-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.history-toolbar h2 { margin: 0; font-size: clamp(1rem, 4vw, 1.35rem); }
.range-buttons { display: flex; gap: 5px; }
.range-buttons button { min-width: 42px; padding: 6px 8px; color: var(--muted); }
.range-buttons button.active { color: var(--text); background: rgba(76,181,255,.28); border-color: var(--blue); }

.chart-shell {
  position: relative;
  min-height: 0;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 18px;
  background: rgba(18,28,39,.86);
}

#history-chart { display: block; width: 100%; height: 100%; }

.history-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 7px 12px;
  color: var(--muted);
  font-size: .66rem;
  font-weight: 700;
}

.history-legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend-line { width: 16px; height: 0; border-top: 2px solid; }
.legend-line.emil { border-color: #ff8a65; }
.legend-line.staffan { border-color: #57d49b; }
.legend-line.nettan { border-color: #c792ea; }
.legend-line.outside-line { border-color: #8fa2b5; }
.legend-line.target-line { border-color: #f3f7fb; border-top-style: dashed; }
.legend-box { width: 13px; height: 8px; border-radius: 2px; }
.legend-box.ac-box { background: rgba(76,181,255,.3); }
.legend-box.ftx-box { background: #36d6c4; }
.legend-box.heater-box { background: linear-gradient(90deg, #ff8a65 0 33%, #57d49b 33% 66%, #c792ea 66%); }
.history-status { margin: 0; color: var(--muted); font-size: .7rem; }

@media (max-width: 430px) {
  .top-actions { gap: 5px; }
  .status { padding: 7px 9px; font-size: .72rem; }
  .view-toggle { padding: 7px 9px; font-size: .72rem; }
  .history-toolbar { align-items: stretch; flex-direction: column; gap: 8px; }
  .range-buttons { width: 100%; }
  .range-buttons button { flex: 1; }
  .rooms { grid-template-columns: 1fr; gap: 10px; }
  .room-card {
    min-height: 96px;
    padding: 13px 14px;
    display: grid;
    grid-template-columns: 54px minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    grid-template-areas:
      "name temperature device"
      "name target device";
    column-gap: 9px;
    row-gap: 3px;
    align-items: center;
  }
  .room-card > .label { grid-area: name; }
  .room-card strong {
    grid-area: temperature;
    margin: 0;
    font-size: clamp(1.65rem, 8vw, 2rem);
    line-height: 1;
    white-space: nowrap;
  }
  .target-state {
    grid-area: target;
    margin: 0;
    font-size: .64rem;
  }
  .device-state {
    grid-area: device;
    min-width: 84px;
    text-align: center;
  }
}

@media (max-height: 520px) and (min-width: 431px) {
  .dashboard { padding: 8px 14px; gap: 6px; }
  .eyebrow { font-size: .62rem; }
  h1 { margin-top: 1px; font-size: 1.45rem; }
  .status { padding: 5px 9px; font-size: .7rem; }
  .hero { padding: 9px 16px; border-radius: 15px; }
  .hero strong { margin-top: 2px; font-size: 1.65rem; }
  .mode-reason { margin-top: 2px; font-size: .64rem; }
  .room-card { padding: 9px 12px; border-radius: 14px; }
  .room-card strong { margin: 3px 0 1px; font-size: 1.55rem; }
  .target-state { margin-bottom: 6px; font-size: .65rem; }
  .device-state { padding: 4px 7px; font-size: .65rem; }
  .footer { padding: 7px 12px; }
}
