/* Mobile first: the base rules target a phone; wider screens only widen the column. */

:root {
  color-scheme: light;
  --page: #f2f2ef;
  --surface: #fcfcfb;
  --surface-sunken: #ebeae6;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.06);
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 4px 16px rgba(11, 11, 11, 0.04);
  --bar: rgba(252, 252, 251, 0.8);
  --accent: #2a78d6;
  --on-accent: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --status-critical: #d03b3b;
  --radius: 18px;
  --radius-small: 12px;
  --tab-bar-height: 58px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-sunken: #262624;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --baseline: #383835;
    --border: rgba(255, 255, 255, 0.06);
    --shadow: none;
    --bar: rgba(26, 26, 25, 0.8);
    --accent: #3987e5;
    --on-accent: #ffffff;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-7: #9085e9;
  }
}

* { box-sizing: border-box; }
/* No double-tap zoom: taps answer at once, like a native app. */
html { touch-action: manipulation; }
p { margin: 0; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  -webkit-tap-highlight-color: transparent;
}

a { color: inherit; text-decoration: none; }

.page {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(12px + env(safe-area-inset-top)) 14px calc(var(--tab-bar-height) + 20px + env(safe-area-inset-bottom));
}

.page-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin: 6px 2px 12px;
}

h1 { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; margin: 0; }
h2 { font-size: 0.78rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.06em; margin: 16px 4px 6px; }
.subtitle { font-size: 0.8rem; color: var(--text-muted); margin-top: 2px; }

svg { flex: none; }

/* Surfaces */

.card {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
}

/* Home: summary stats */

.summary { padding: 14px 14px 10px; }
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { min-width: 0; }
.stat-icon { display: inline-flex; color: var(--accent); font-size: 1.05rem; margin-bottom: 4px; }
.stat-value { font-size: 1.3rem; font-weight: 650; letter-spacing: -0.02em; white-space: nowrap; }
.stat-value small { font-size: 0.75rem; font-weight: 500; color: var(--text-secondary); margin-left: 2px; }
.stat-label { font-size: 0.72rem; color: var(--text-muted); }

/* Rows inside a list card */

.list { padding: 4px 12px; }
.row { display: flex; align-items: center; gap: 10px; padding: 8px 0; min-height: 52px; }
.list > .row + .row, .radiator + .radiator { border-top: 1px solid var(--grid); }
.row-text { flex: 1; min-width: 0; }
.row-title { font-size: 0.92rem; font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-detail { font-size: 0.75rem; color: var(--text-muted); display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.row-trend { width: 64px; flex: none; }
.row-trend .sparkline { margin: 0; height: 24px; }
.row-value { font-size: 1.25rem; font-weight: 650; letter-spacing: -0.02em; min-width: 52px; text-align: right; font-variant-numeric: tabular-nums; }
.row-value small { font-size: 0.85rem; font-weight: 500; color: var(--text-secondary); }
.humidity { display: inline-flex; align-items: center; gap: 2px; }

.icon-bubble {
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 11px;
  background: color-mix(in srgb, currentColor 14%, transparent);
  font-size: 1.05rem;
  flex: none;
}

/* Home: heating chips */

.chip-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.chip {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  box-shadow: var(--shadow);
  padding: 10px 12px;
}
.chip-icon { display: inline-flex; color: var(--accent); font-size: 1.1rem; }

/* Heating page */

.radiator { padding-bottom: 10px; }
.radiator .row { padding-bottom: 6px; }

/* Controls */

.segmented { display: flex; background: var(--surface-sunken); border-radius: 11px; padding: 3px; gap: 2px; }
.segment {
  flex: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.8rem;
  padding: 6px 10px;
  white-space: nowrap;
  border-radius: 9px;
  min-height: 36px;
  cursor: pointer;
  transition: background 120ms, color 120ms;
}
.segment.selected { background: var(--surface); color: var(--text-primary); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }
.segment.mode { flex-direction: column; gap: 2px; font-size: 0.68rem; padding: 6px 4px; min-height: 46px; }
.segment.mode svg { font-size: 1.05rem; }
.segment.mode.selected svg { color: var(--accent); }

.action-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.action {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font: inherit;
  font-size: 0.72rem;
  padding: 10px 4px;
  border-radius: var(--radius-small);
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--text-secondary);
  cursor: pointer;
}
.action svg { font-size: 1.2rem; color: var(--accent); }
.action:active { background: var(--surface-sunken); }
/* Heating modes are shown, not yet sent: the buttons stay readable but inert. */
.action:disabled { opacity: 0.45; cursor: default; }
.segment:disabled { cursor: default; }
.segment:disabled:not(.selected) { opacity: 0.55; }

/* Charts */

.legend { list-style: none; display: flex; flex-wrap: wrap; gap: 4px 12px; margin: 0 0 6px; padding: 0; font-size: 0.75rem; color: var(--text-secondary); }
.legend li { display: flex; align-items: center; gap: 5px; }
.swatch { display: inline-block; width: 8px; height: 8px; border-radius: 50%; flex: none; }

.hero { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 10px; }
.hero .segmented { flex: none; }
.hero-value { font-size: 1.8rem; font-weight: 650; letter-spacing: -0.02em; }
.hero-value small { font-size: 0.85rem; font-weight: 500; color: var(--text-secondary); margin-left: 3px; }
.hero-detail { font-size: 0.78rem; color: var(--text-muted); }

.chart { position: relative; touch-action: pan-y; }
.chart svg { width: 100%; height: auto; display: block; overflow: visible; }
.grid { stroke: var(--grid); stroke-width: 1; }
.baseline { stroke: var(--baseline); stroke-width: 1; }
.axis-label { fill: var(--text-muted); font-size: 9px; font-variant-numeric: tabular-nums; }
.series-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.series-line.dashed { stroke-dasharray: 4 4; }
.crosshair { stroke: var(--baseline); stroke-width: 1; }
.hover-dot { stroke: var(--surface); stroke-width: 2; }
.hit-area { fill: transparent; cursor: pointer; }
.marker { stroke: var(--surface); stroke-width: 1.5; }
.mean-line { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.line-swatch { display: inline-block; width: 16px; border-top: 2px solid; }
.line-swatch.dashed { border-top-style: dashed; }
.mean-swatch { display: inline-block; width: 14px; border-top: 1.5px dashed var(--text-muted); }
.bar { transition: opacity 120ms; }
.bar.dimmed { opacity: 0.35; }

.sparkline { width: 100%; height: 26px; margin-top: 8px; display: block; }
.sparkline polyline { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; vector-effect: non-scaling-stroke; }



/* Period controls */

.period-controls { display: grid; gap: 8px; margin-bottom: 12px; }
.navigator { display: flex; align-items: center; gap: 8px; }
.navigator-text { flex: 1; text-align: center; min-width: 0; }
.navigator-title { font-size: 1rem; font-weight: 650; }
.navigator-subtitle { font-size: 0.75rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-button {
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--text-primary);
  font-size: 1.15rem;
  cursor: pointer;
}
.nav-button:disabled { opacity: 0.35; cursor: default; }

/* Chart cards */

.chart-card { padding: 14px 12px 8px; }
.readout { font-size: 0.75rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 6px; }
.readout-reset {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 10px;
  border: none;
  border-radius: 18px;
  font-family: inherit;
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, transparent);
  cursor: pointer;
}
.readout-reset svg { font-size: 0.95rem; }

.change-badge { display: inline-flex; align-items: center; gap: 3px; color: var(--text-secondary); white-space: nowrap; }

.chart-card-header { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; }
.chart-card-header .readout { margin: 0; }
.chart-card-header .segment { min-height: 30px; padding: 4px 12px; }

.battery-badge {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 8px;
  font-size: 0.68rem;
  font-weight: 600;
  vertical-align: middle;
  color: var(--status-critical);
  background: color-mix(in srgb, var(--status-critical) 14%, transparent);
}

/* Temperature: one row per room, opening its chart below. */

.room-accordion + .room-accordion { border-top: 1px solid var(--grid); }
.room-accordion-header {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 0;
  border: none;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}
.room-accordion-header .row { flex: 1; min-width: 0; }
.room-accordion-chevron { display: inline-flex; color: var(--text-muted); transition: transform 150ms; }
.room-accordion.open .room-accordion-chevron { transform: rotate(180deg); }
.room-climate { padding: 4px 0 12px; }
.room-climate-header { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 6px; }
.room-climate-header .readout { margin: 0; }
.room-climate-figures { font-size: 0.95rem; font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* Breakdown rows */

.proportion-bar { display: flex; gap: 2px; height: 8px; margin: 10px 0 4px; border-radius: 4px; overflow: hidden; }
.proportion-bar span { display: block; height: 100%; }
.row-figures { text-align: right; }
.row-figure { font-size: 0.9rem; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.row-figures .row-detail { justify-content: flex-end; }
.meter { height: 6px; margin-top: 5px; border-radius: 3px; background: var(--surface-sunken); overflow: hidden; }
.meter span { display: block; height: 100%; border-radius: 3px; }

/* Bottom tab bar */

.tab-bar {
  position: fixed;
  inset: auto 0 0 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tab-bar-height) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bar);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-top: 1px solid var(--border);
}
.tab-bar a {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 0.68rem;
  color: var(--text-muted);
}
.tab-bar a svg { font-size: 1.35rem; }
.tab-bar a[aria-current="page"] { color: var(--accent); font-weight: 600; }

@media (min-width: 640px) {
  .chip-grid { grid-template-columns: repeat(4, 1fr); }
}

/* Status screens: loading, failure and sign-in, one card centred on the phone with no tab bar. */
.status-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: calc(24px + env(safe-area-inset-top)) 20px calc(24px + env(safe-area-inset-bottom));
  background: radial-gradient(120% 60% at 50% 0%, color-mix(in srgb, var(--accent) 12%, transparent), transparent 70%);
}
.status-card { width: 100%; max-width: 360px; display: flex; flex-direction: column; align-items: center; text-align: center; padding: 32px 24px 24px; }
.status-logo {
  display: inline-grid;
  place-items: center;
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
  border-radius: 20px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 1.9rem;
  box-shadow: 0 6px 20px color-mix(in srgb, var(--accent) 35%, transparent);
}
.status-card h1 { font-size: 1.9rem; }
.status-tagline { margin: 6px 0 24px; color: var(--text-secondary); font-size: 0.9rem; line-height: 1.4; }
.status-card .notice { width: 100%; margin-bottom: 16px; border-radius: var(--radius-small); background: color-mix(in srgb, var(--status-critical) 10%, transparent); }

.primary-action {
  flex-direction: row;
  justify-content: center;
  gap: 10px;
  width: 100%;
  min-height: 48px;
  padding: 12px 20px;
  font-size: 0.95rem;
  font-weight: 600;
  border: none;
  background: var(--accent);
  color: var(--on-accent);
}
.primary-action svg { color: inherit; }
.primary-action:active { background: color-mix(in srgb, var(--accent) 85%, var(--text-primary)); }

/* Notices: in place of content that cannot be shown. */
.notice { display: flex; align-items: center; gap: 12px; padding: 12px; text-align: left; }
.notice-icon { color: var(--status-critical); }
.notice-title { font-size: 0.92rem; font-weight: 600; }
.notice-detail { font-size: 0.8rem; color: var(--text-secondary); line-height: 1.4; margin-top: 2px; }
.notice-card { display: grid; gap: 4px; padding: 0 0 12px; }
.notice-card .primary-action { width: auto; margin: 0 12px; }
.notice-card:has(> .notice:only-child) { padding: 0; }
.not-found { display: flex; flex-direction: column; align-items: center; gap: 16px; margin-top: 30vh; }
.not-found .notice-card { width: 100%; }
.not-found-link { flex-direction: row; gap: 8px; min-height: 44px; padding: 10px 16px; font-size: 0.88rem; color: var(--text-primary); }

.loading { display: flex; align-items: center; justify-content: center; gap: 10px; min-height: 52px; color: var(--text-muted); font-size: 0.85rem; }
.spinner {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 2px solid var(--grid);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }
