:root {
  --bg: #f4f5f7; --surface: #ffffff; --surface-2: #f0f1f4; --text: #1d2026; --muted: #6b7280;
  --border: #e2e4e9; --accent: #e8590c; --accent-text: #ffffff; --ok: #2f9e44; --warn: #e67700; --bad: #c92a2a;
  --cold: #4dabf7; --warm: #ff8787; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #14161a; --surface: #1d2025; --surface-2: #262a31; --text: #e8eaed; --muted: #9aa0a6;
    --border: #30343c; --shadow: none; }
}
* { box-sizing: border-box; }
body { margin: 0; font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.spacer { flex: 1; }

.topbar { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px;
  padding: 8px 16px; padding-top: calc(8px + env(safe-area-inset-top)); background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { font-weight: 600; font-size: 17px; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.logo { width: 28px; height: 28px; flex: none; }
nav { display: flex; gap: 4px; margin-left: auto; }
nav button { display: inline-flex; align-items: center; gap: 7px; border: 0; background: none; color: var(--muted); padding: 7px 11px;
  border-radius: 10px; font: inherit; font-size: 14px; cursor: pointer; -webkit-tap-highlight-color: transparent; }
nav button svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
nav button.active { background: var(--surface-2); color: var(--accent); font-weight: 600; }
nav button.active span { color: var(--text); }
#refresh-btn { margin-left: 8px; }
nav button.wx { gap: 4px; font-variant-numeric: tabular-nums; white-space: nowrap; }
nav button.wx i { font-style: normal; font-size: 17px; }
nav button.wx b { font-weight: 600; color: var(--text); }
nav button.wx small { font-size: 12px; color: var(--muted); }
#weather-view a { color: var(--accent); }
#refresh-btn.busy svg { animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (max-width: 899px) {
  /* phone / small tablet: one row of icons, no brand, labels or time */
  .brand, nav button span { display: none; }
  nav { margin: 0; width: 100%; justify-content: space-between; }
  nav button { flex: 1; justify-content: center; padding: 9px 0; }
  nav button.wx { flex: 1.9; font-size: 13px; }
  #refresh-btn { margin-left: 0; }
  .topbar { padding: 6px 8px; padding-top: calc(6px + env(safe-area-inset-top)); }
}

main { max-width: 1200px; margin: 0 auto; padding: 16px; }
.banner { max-width: 1200px; margin: 12px auto 0; padding: 10px 14px; border-radius: 10px; background: #fff4e6; color: #8a4b00; border: 1px solid #ffd8a8; }
.banner.error { background: #fff5f5; color: #a61e1e; border-color: #ffc9c9; }
@media (prefers-color-scheme: dark) { .banner { background: #3a2a12; color: #ffd8a8; border-color: #5c3d10; }
  .banner.error { background: #3b1818; color: #ffc9c9; border-color: #5c2020; } }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 7px 12px; border-radius: 8px; font: inherit; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.btn.small { padding: 4px 9px; font-size: 13px; }
.btn.danger { color: var(--bad); }
.btn:disabled { opacity: .5; cursor: default; }

.system { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.system .faults { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.modes { flex: 1; min-width: 0; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.modes::-webkit-scrollbar { display: none; }
.modes .btn { padding: 6px 10px; font-size: 14px; }
.weather { flex: none; display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--muted); white-space: nowrap; font-variant-numeric: tabular-nums; }
nav button.demo, nav button.demo.active { color: var(--bad); }
input.demo::placeholder { color: var(--bad); font-weight: 700; opacity: 1; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 12px; }
.grid > .card { margin: 0; }
.tile { display: flex; flex-direction: column; gap: 6px; margin: 0; padding: 12px; text-align: center; cursor: pointer; min-width: 0; }
.tile .name { font-size: 13px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; justify-content: center; gap: 5px; }
.tile .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: none; }
.tile .dot.fault { background: var(--bad); }
.tile .temp { font-size: 40px; font-weight: 300; line-height: 1.05; font-variant-numeric: tabular-nums; }
.tile .temp small { font-size: 15px; color: var(--muted); vertical-align: top; margin-left: 1px; }
.tile .temp.heating { color: var(--warm); }
.tile .setpoint { display: flex; align-items: center; justify-content: space-between; gap: 4px; background: var(--surface-2); border-radius: 99px; padding: 3px; }
.tile .setpoint .value { font-size: 17px; font-weight: 600; font-variant-numeric: tabular-nums; }
.tile .setpoint.override .value { color: var(--accent); }
.tile .setpoint .value.text { flex: 1; }
.tile .round { width: 34px; height: 34px; border: 0; background: var(--surface); flex: none; }
.valve { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.valve svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2; flex: none; }
.valve .bar { flex: 1; height: 5px; border-radius: 5px; background: var(--surface-2); overflow: hidden; }
.valve .bar span { display: block; height: 100%; background: linear-gradient(90deg, var(--cold), var(--warm)); border-radius: 5px; }
.valve .pct { min-width: 32px; text-align: right; font-variant-numeric: tabular-nums; }
.tile .meta { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-height: 17px; }
.tile .meta.fault { color: var(--bad); }
.round { width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); font-size: 18px; cursor: pointer; }
.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.badge.override { background: #fff0e6; color: var(--accent); }
.badge.fault { background: #fff5f5; color: var(--bad); }
@media (prefers-color-scheme: dark) { .badge.override { background: #3a2412; } .badge.fault { background: #3b1818; } }

.wx-now { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.wx-now .big { font-size: 52px; line-height: 1; }
.wx-now .t { font-size: 44px; font-weight: 300; line-height: 1; }
.wx-now .desc { font-weight: 600; }
.wx-facts { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px 14px; margin-top: 14px; font-size: 14px; }
.wx-facts div span { display: block; font-size: 12px; color: var(--muted); }
.wx-hours { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.wx-hours::-webkit-scrollbar { display: none; }
.wx-hours > div { flex: none; width: 54px; text-align: center; font-size: 13px; display: flex; flex-direction: column; gap: 3px; }
.wx-hours i { font-style: normal; font-size: 20px; }
.wx-rain { color: var(--cold); font-size: 11px; min-height: 14px; }
.wx-days > div { display: grid; grid-template-columns: 84px 28px 1fr 64px; align-items: center; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.wx-days > div:last-child { border-bottom: 0; }
.wx-days i { font-style: normal; font-size: 20px; }
.wx-days .range { font-variant-numeric: tabular-nums; }
.wx-days .range span { color: var(--muted); }
.card h3 { margin: 0 0 10px; font-size: 15px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; }
select, input[type=text], input[type=email], input[type=password], input[type=number], input[type=time], input[type=datetime-local] {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; }
label { display: inline-flex; gap: 6px; align-items: center; }

.form { display: flex; flex-direction: column; gap: 10px; max-width: 640px; }
.form h2 { margin: 0; font-size: 17px; }
.form label { display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
.form label.row, .row { flex-direction: row; align-items: center; gap: 10px; display: flex; flex-wrap: wrap; }

.week { display: flex; flex-direction: column; gap: 10px; }
.day { display: grid; grid-template-columns: 110px 1fr; gap: 12px; align-items: start; }
.day .label { font-weight: 600; padding-top: 6px; }
.day .bar { display: flex; height: 22px; border-radius: 6px; overflow: hidden; border: 1px solid var(--border); margin-bottom: 8px; }
.day .bar div { height: 100%; font-size: 11px; color: #222; display: flex; align-items: center; justify-content: center; overflow: hidden; white-space: nowrap; }
.sps { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.sp { display: flex; gap: 4px; align-items: center; background: var(--surface-2); border-radius: 8px; padding: 4px 6px; }
.sp input[type=number] { width: 64px; }
.sp select { padding: 4px; }
.sp button { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; }
.hours { display: grid; grid-template-columns: repeat(8, 1fr); font-size: 11px; color: var(--muted); margin: 0 0 4px 122px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 99px; border: 1px solid var(--border); cursor: pointer; background: var(--surface); font-size: 13px; user-select: none; }
.chip.off { opacity: .45; }
.chip i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.chart { min-height: 340px; }
.chart svg { width: 100%; height: 360px; display: block; }
.chart .axis { stroke: var(--border); }
.chart text { fill: var(--muted); font-size: 11px; }

table.log { width: 100%; border-collapse: collapse; font-size: 14px; }
table.log th, table.log td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); }
table.log td:first-child { white-space: nowrap; color: var(--muted); }

dialog { border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--surface); color: var(--text); width: min(440px, 94vw); }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog form { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
dialog h3 { margin: 0; }
.options { display: flex; flex-wrap: wrap; gap: 6px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 50; max-width: 90vw; }

@media (max-width: 640px) {
  .banner { margin: 10px 10px 0; }
  main { padding: 10px; padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .card { padding: 12px; margin-bottom: 10px; }
  .tile { padding: 10px 8px; }
  .tile .temp { font-size: 34px; }
  .day { grid-template-columns: 1fr; }
  .hours { margin-left: 0; }
  .system { margin-bottom: 10px; }
}
/* phone in landscape: three tiles per row */
@media (orientation: landscape) and (max-height: 600px) {
  .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
  main { padding-left: calc(10px + env(safe-area-inset-left)); padding-right: calc(10px + env(safe-area-inset-right)); }
}

.devices { list-style: none; margin: 0; padding: 0; }
.devices li { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-top: 1px solid var(--border); }
.devices li:first-child { border-top: 0; }
.login { max-width: 380px; margin: 12vh auto 0; padding: 0 16px; }
.login .brand { display: flex; flex-direction: column; justify-content: center; gap: 10px; margin-bottom: 16px; font-size: 20px; }
.login .logo { width: 96px; height: 96px; filter: drop-shadow(0 6px 18px rgba(0,0,0,.35)); }
.login .error { color: var(--bad); min-height: 1.4em; margin: 0; }
