:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #172033;
  --muted: #5d6b82;
  --border: #dde3ec;
  --accent: #1d4ed8;
  --accent-text: #ffffff;
  --danger: #c2410c;
  --ok: #15803d;
  --heat: #f59e0b;
  --heat-bg: #fef3c7;
  --cold: #3b82f6;
  --cold-bg: #dbeafe;
  --wet: #14b8a6;
  --wet-bg: #ccfbf1;
  --fan: #94a3b8;
  --fan-bg: #e2e8f0;
  --auto: #8b5cf6;
  --auto-bg: #ede9fe;
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1522;
    --surface: #182133;
    --surface-2: #222d42;
    --text: #e8edf6;
    --muted: #9aa7bd;
    --border: #2d3a52;
    --accent: #3b82f6;
    --danger: #fb923c;
    --ok: #4ade80;
    --heat-bg: #4a3510;
    --cold-bg: #1a2e55;
    --wet-bg: #0f3d39;
    --fan-bg: #2c3648;
    --auto-bg: #33275a;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; font-weight: 600; }
button, input, select {
  font: inherit;
  color: inherit;
}
button {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { border-color: var(--muted); }
button:active { transform: scale(0.98); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { color: var(--danger); }
button.link { background: none; border: none; color: var(--accent); padding: 4px 6px; }
button.icon { padding: 8px 11px; font-size: 17px; line-height: 1; }
input, select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  min-height: 38px;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label > input, label > select { color: var(--text); font-size: 15px; }
.error { color: var(--danger); font-size: 13px; min-height: 1em; margin: 6px 0 0; }
.muted { color: var(--muted); }

/* Connexion */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px;
  padding: 28px; width: min(360px, 100%); display: flex; flex-direction: column; gap: 14px; text-align: center;
}
.login-card img { margin: 0 auto; }

/* Barre du haut */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand small { display: block; color: var(--muted); font-size: 12px; }
.top-actions { display: flex; gap: 8px; }
.banner {
  margin: 12px 16px 0; padding: 10px 14px; border-radius: 12px;
  background: var(--cold-bg); display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
}
.banner.warn { background: var(--heat-bg); }

/* Onglets */
.tabs {
  display: flex; gap: 6px; padding: 12px 16px 0; max-width: 1100px; margin: 0 auto;
}
.tabs button { border-radius: 999px; }
.tabs button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
main { max-width: 1100px; margin: 0 auto; padding: 16px; padding-bottom: 90px; }

@media (max-width: 640px) {
  .tabs {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 6; margin: 0; max-width: none;
    background: var(--surface); border-top: 1px solid var(--border);
    padding: 6px 8px; padding-bottom: max(6px, env(safe-area-inset-bottom));
    justify-content: space-around;
  }
  .tabs button { flex: 1; padding: 8px 4px; font-size: 13px; border: none; }
  .top-actions button:not(.icon) { padding: 8px 10px; font-size: 13px; }
  .brand small { max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

/* Cartes clims */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; display: flex; flex-direction: column; gap: 14px;
}
.card.on { border-color: var(--heat); box-shadow: inset 0 3px 0 var(--mode-color, var(--heat)); }
.card.offline { opacity: 0.7; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card-head h3 { font-size: 17px; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.badge.online { color: var(--ok); }
.power {
  width: 46px; height: 46px; border-radius: 50%; font-size: 20px; padding: 0;
  display: grid; place-items: center;
}
.power.on { background: var(--mode-color, var(--heat)); color: #fff; border-color: transparent; }
.setpoint { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.setpoint button { width: 48px; height: 48px; border-radius: 50%; font-size: 22px; padding: 0; }
.setpoint .value { text-align: center; }
.setpoint .value strong { font-size: 40px; font-weight: 600; line-height: 1; }
.setpoint .value small { display: block; color: var(--muted); font-size: 12px; }
.room { display: flex; justify-content: space-between; color: var(--muted); font-size: 14px; }
.room b { color: var(--text); font-weight: 600; }
.seg { display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; }
.seg button { border: none; background: none; padding: 6px 2px; border-radius: 9px; font-size: 12px; display: flex; flex-direction: column; align-items: center; gap: 1px; }
.seg button span { font-size: 17px; }
.seg button.active { background: var(--surface); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 600; }
.card-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.card-foot select { flex: 1; min-width: 120px; }
.toggle { flex-direction: row; align-items: center; gap: 8px; font-size: 14px; color: var(--text); cursor: pointer; }
.toggle input { width: 18px; height: 18px; min-height: 0; accent-color: var(--accent); }
.stale { font-size: 12px; color: var(--danger); }

/* Planning */
.plan-head { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.pill-group { display: flex; gap: 6px; flex-wrap: wrap; }
.pill-group button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.brush {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; margin-bottom: 12px;
}
.brush .title { font-weight: 600; width: 100%; font-size: 14px; }
.brush label { min-width: 110px; }
.grid-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 12px 8px; }
.grid { display: grid; grid-template-columns: 44px 1fr; row-gap: 6px; align-items: center; user-select: none; }
.hours { position: relative; height: 16px; font-size: 11px; color: var(--muted); }
.hours span { position: absolute; transform: translateX(-50%); }
.day-label { font-size: 13px; color: var(--muted); }
.track {
  position: relative; height: 38px; border-radius: 8px; cursor: crosshair; touch-action: pan-y;
  background-color: var(--surface-2);
  background-image: repeating-linear-gradient(to right, var(--border) 0 1px, transparent 1px calc(100% / 24));
}
.track.today { outline: 2px solid var(--accent); outline-offset: 1px; }
.slot {
  position: absolute; top: 3px; bottom: 3px; border-radius: 6px; cursor: pointer;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
  font-size: 12px; font-weight: 600; color: #1b1b1b; border: 1px solid rgba(0,0,0,.15);
}
.slot.preview { opacity: 0.6; pointer-events: none; }
.now-line { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--danger); pointer-events: none; }
.plan-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.hint { color: var(--muted); font-size: 13px; margin: 10px 2px 0; }
.legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }

/* Regles */
.list { display: flex; flex-direction: column; gap: 10px; }
.rule {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
}
.rule .txt { flex: 1; min-width: 220px; }
.rule .txt small { display: block; color: var(--muted); }
.rule.disabled .txt { opacity: 0.55; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }

/* Journal */
.log-table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; font-size: 14px; }
.log-table th, .log-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
.log-table th { font-size: 12px; color: var(--muted); font-weight: 500; }
.log-table td.when { white-space: nowrap; color: var(--muted); }
.src { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); white-space: nowrap; }
.fail { color: var(--danger); }
@media (max-width: 640px) {
  .log-table .col-src { display: none; }
}

/* Dialogues */
dialog {
  border: 1px solid var(--border); border-radius: 18px; padding: 0; width: min(460px, calc(100vw - 24px));
  background: var(--surface); color: var(--text);
}
dialog::backdrop { background: rgba(10, 15, 25, 0.5); }
dialog form { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
#dialog-body { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > label { flex: 1; min-width: 120px; }
.checks { display: flex; flex-wrap: wrap; gap: 8px 14px; }
.checks label { flex-direction: row; align-items: center; gap: 6px; color: var(--text); font-size: 14px; }
.checks input { min-height: 0; width: 17px; height: 17px; accent-color: var(--accent); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.dialog-actions .left { margin-right: auto; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); color: #fff; }
