:root {
  --bg: #faf6f1;
  --surface: #ffffff;
  --surface-2: #f3ece4;
  --text: #2a211b;
  --muted: #8a7b70;
  --border: #eadfd3;
  --accent: #e8590c;
  --accent-soft: #fde7d9;
  --ok: #2f9e44;
  --idle: #1c7ed6;
  --alarm: #e03131;
  --c-air: #e8590c;
  --c-set: #868e96;
  --c-flue: #ae3ec9;
  --c-band: rgba(232, 89, 12, 0.10);
  --shadow: 0 1px 2px rgba(60, 40, 20, .06), 0 4px 16px rgba(60, 40, 20, .05);
  --radius: 18px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1714;
    --surface: #262019;
    --surface-2: #30281f;
    --text: #f3ebe3;
    --muted: #a8998c;
    --border: #3a3027;
    --accent: #ff7a2f;
    --accent-soft: #46291a;
    --c-air: #ff8a3d;
    --c-set: #adb5bd;
    --c-flue: #d47cf0;
    --c-band: rgba(255, 122, 47, 0.13);
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, h3 { margin: 0; }
h2 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.label { font-weight: 600; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 6px; }

#app { max-width: 560px; margin: 0 auto; padding: 0 16px calc(90px + env(safe-area-inset-bottom)); }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(14px + env(safe-area-inset-top)) 0 12px;
  position: sticky; top: 0; background: var(--bg); z-index: 5;
}
.device-name { font-weight: 700; font-size: 1.15rem; }
.updated { font-size: .8rem; color: var(--muted); }
.icon-btn {
  border: 0; background: var(--surface); width: 42px; height: 42px; border-radius: 50%;
  display: grid; place-items: center; box-shadow: var(--shadow); font-size: 1.4rem;
}
.icon-btn.spin svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.card {
  background: var(--surface); border-radius: var(--radius); padding: 16px;
  box-shadow: var(--shadow); margin-bottom: 14px; border: 1px solid var(--border);
}
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.divider { height: 1px; background: var(--border); margin: 14px 0; }

.banner { background: var(--accent-soft); color: var(--text); padding: 10px 14px; border-radius: 12px; margin-bottom: 12px; font-size: .9rem; }

/* Hero */
.hero { position: relative; overflow: hidden; }
.hero-top { display: flex; align-items: center; gap: 10px; }
.pill { padding: 4px 12px; border-radius: 999px; font-weight: 600; font-size: .85rem; background: var(--surface-2); }
.pill.heating, .pill.starting { background: var(--accent); color: #fff; }
.pill.idle { background: var(--idle); color: #fff; }
.pill.alarm { background: var(--alarm); color: #fff; }
.status-text { font-size: .8rem; color: var(--muted); text-transform: lowercase; }
.hero-main { display: flex; align-items: center; gap: 18px; margin: 18px 0 14px; }
.big-temp { font-size: 4rem; font-weight: 700; line-height: 1; letter-spacing: -2px; font-variant-numeric: tabular-nums; }
.big-temp small { font-size: 1.4rem; font-weight: 500; letter-spacing: 0; margin-left: 4px; color: var(--muted); }
.hero-sub { display: flex; gap: 18px; flex-wrap: wrap; font-size: .9rem; }
.warn { color: var(--alarm); font-weight: 600; }

.flame {
  width: 64px; height: 76px; flex: none; border-radius: 50% 50% 46% 46% / 62% 62% 38% 38%;
  background: var(--surface-2); transform: rotate(0); transition: background .4s;
  position: relative;
}
.flame.on {
  background: radial-gradient(circle at 50% 75%, #ffe066 0 18%, #ff922b 40%, #e8590c 70%);
  animation: flicker 1.6s ease-in-out infinite alternate;
  box-shadow: 0 8px 30px rgba(232, 89, 12, .45);
}
.flame.starting { animation-duration: .6s; opacity: .8; }
.flame.idle { background: radial-gradient(circle at 50% 75%, #74c0fc 0 20%, #1c7ed6 75%); }
@keyframes flicker {
  0% { transform: scale(1, 1) rotate(-2deg); }
  50% { transform: scale(.96, 1.04) rotate(2deg); }
  100% { transform: scale(1.03, .97) rotate(-1deg); }
}

/* Stepper */
.stepper { display: flex; align-items: center; gap: 10px; }
.round {
  width: 46px; height: 46px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface-2); font-size: 1.5rem; line-height: 1;
}
.round:active { transform: scale(.94); }
.stepper-val { min-width: 84px; text-align: center; font-size: 1.7rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stepper-val small { font-size: .9rem; color: var(--muted); font-weight: 500; }
.stepper-val.pending { color: var(--accent); }

.chips { display: flex; gap: 8px; margin-top: 10px; }
.chips button {
  flex: 1; border: 1px solid var(--border); background: var(--surface-2); border-radius: 12px;
  padding: 10px 0; font-weight: 600;
}
.chips button.on { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Buttons */
.btn {
  border: 0; border-radius: 14px; padding: 13px 18px; font-weight: 600; background: var(--surface-2);
}
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; border: 1px solid var(--border); width: 100%; }
.btn:disabled { opacity: .5; }
.power-btn {
  width: 100%; margin-bottom: 14px; padding: 18px; font-size: 1.1rem; border-radius: var(--radius);
  background: var(--accent); color: #fff; box-shadow: var(--shadow);
}
.power-btn.off { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.link { border: 0; background: none; color: var(--accent); font-weight: 600; padding: 4px 0; }

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 12px 0; }
.kpi { background: var(--surface-2); border-radius: 12px; padding: 10px 6px; text-align: center; }
.kpi b { display: block; font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.kpi span { font-size: .75rem; color: var(--muted); }

.chart-box { position: relative; height: 180px; }
.chart-box.tall { height: 260px; }
.cycle-now { margin-top: 10px; font-size: .9rem; background: var(--accent-soft); padding: 10px 12px; border-radius: 12px; }

/* Charts tab */
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 4px; margin-bottom: 14px; }
.seg button { flex: 1; border: 0; background: none; padding: 9px 0; border-radius: 9px; font-weight: 600; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.day-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; font-weight: 600; }
.day-nav .icon-btn { width: 36px; height: 36px; box-shadow: none; background: var(--surface-2); }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: .8rem; color: var(--muted); margin-top: 10px; }
.sw { display: inline-block; width: 14px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 6px; }
.sw.air { background: var(--c-air); }
.sw.set { background: var(--c-set); }
.sw.flue { background: var(--c-flue); }
.sw.band { background: var(--c-band); height: 10px; border: 1px solid var(--accent); }

/* Cycles */
.day-group { font-size: .8rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 18px 4px 8px; }
.cycle {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 12px 14px;
  margin-bottom: 8px; display: grid; grid-template-columns: auto 1fr auto; gap: 4px 12px; align-items: center;
}
.cycle.live { border-color: var(--accent); }
.cycle-time { font-weight: 700; font-size: 1.1rem; font-variant-numeric: tabular-nums; }
.cycle-temp { font-weight: 700; font-size: 1.1rem; text-align: right; }
.cycle-temp small { color: var(--muted); font-weight: 500; }
.cycle-detail { grid-column: 1 / -1; font-size: .82rem; color: var(--muted); }
.cycle-arrow { font-size: .9rem; color: var(--muted); }
.gain-up { color: var(--accent); font-weight: 600; }

/* Events / registers */
.events { list-style: none; margin: 10px 0 0; padding: 0; }
.events li { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: .9rem; }
.events li:last-child { border: 0; }
.events time { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 92px; font-size: .8rem; padding-top: 2px; }
.regs { width: 100%; border-collapse: collapse; font-size: .8rem; margin-top: 8px; }
.regs td { padding: 5px 4px; border-bottom: 1px solid var(--border); word-break: break-all; }
.regs td:first-child { font-family: ui-monospace, monospace; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: .9rem; margin: 10px 0 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }

/* Appareils autorisés */
.devices { list-style: none; margin: 10px 0 14px; padding: 0; }
.devices li { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.devices li:last-child { border: 0; }
.devices .dev-main { flex: 1; min-width: 0; }
.devices .dev-name { font-weight: 600; overflow-wrap: anywhere; }
.devices .dev-meta { color: var(--muted); font-size: .8rem; }
.devices .tag { font-size: .75rem; font-weight: 600; color: var(--accent); margin-left: 6px; }
.invite { display: grid; gap: 10px; margin-top: 12px; }
.invite input { font-size: .85rem; width: 100%; box-sizing: border-box; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; display: flex; justify-content: center;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); z-index: 10;
}
.tabbar button {
  flex: 1; max-width: 140px; border: 0; background: none; display: flex; flex-direction: column; align-items: center;
  gap: 2px; font-size: .72rem; color: var(--muted); padding: 6px 0;
}
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabbar button.on { color: var(--accent); }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; text-align: center; }
.login-card h1 { margin: 12px 0 6px; }
.login-card form { display: grid; gap: 10px; margin-top: 18px; }
.login-card > .btn, .login-card > .error { margin-top: 12px; }
.login-card input, .invite input {
  font: inherit; padding: 13px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
.logo { width: 72px; height: 84px; margin: 0 auto; border-radius: 50% 50% 46% 46% / 62% 62% 38% 38%;
  background: radial-gradient(circle at 50% 75%, #ffe066 0 18%, #ff922b 40%, #e8590c 70%); }
.error { color: var(--alarm); margin: 0; font-size: .9rem; }

dialog { border: 0; border-radius: var(--radius); padding: 20px; background: var(--surface); color: var(--text); max-width: 340px; width: calc(100% - 32px); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dialog-actions { display: flex; gap: 10px; margin-top: 16px; }
.dialog-actions .btn { flex: 1; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: .9rem;
  z-index: 20; max-width: calc(100% - 32px); box-shadow: var(--shadow);
}

/* Programmation */
:root { --p1: #e8590c; --p2: #ae3ec9; --p3: #1c7ed6; --p4: #2f9e44; }
@media (prefers-color-scheme: dark) { :root { --p1: #ff8a3d; --p2: #d47cf0; --p3: #4dabf7; --p4: #51cf66; } }
.switch {
  flex: none; width: 52px; height: 30px; border-radius: 15px; border: 0; background: var(--border);
  position: relative; transition: background .2s;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s;
}
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(22px); }
.switch:disabled { opacity: .5; }
.week { display: grid; grid-template-columns: 34px 1fr; gap: 6px 8px; align-items: center; margin-top: 12px; font-size: .78rem; color: var(--muted); }
.week.disabled .bar { opacity: .35; }
.track {
  position: relative; height: 14px; border-radius: 4px; background: var(--surface-2);
  background-image: linear-gradient(90deg, transparent calc(25% - 1px), var(--border) 25%, transparent calc(25% + 1px), transparent calc(50% - 1px), var(--border) 50%, transparent calc(50% + 1px), transparent calc(75% - 1px), var(--border) 75%, transparent calc(75% + 1px));
}
.bar { position: absolute; top: 0; bottom: 0; border-radius: 4px; opacity: .85; }
.bar.p1, .prog.p1 .prog-tag { background: var(--p1); }
.bar.p2, .prog.p2 .prog-tag { background: var(--p2); }
.bar.p3, .prog.p3 .prog-tag { background: var(--p3); }
.bar.p4, .prog.p4 .prog-tag { background: var(--p4); }
.prog { padding: 0; overflow: hidden; }
.prog.dirty { border-color: var(--accent); }
.prog-head {
  width: 100%; display: flex; align-items: center; gap: 12px; border: 0; background: none;
  padding: 14px 16px; text-align: left;
}
.prog-tag { flex: none; color: #fff; font-weight: 700; font-size: .85rem; border-radius: 8px; padding: 4px 8px; }
.prog-sum { flex: 1; display: flex; flex-direction: column; gap: 2px; font-weight: 600; font-variant-numeric: tabular-nums; }
.prog-days { display: flex; gap: 4px; font-size: .72rem; font-weight: 600; color: var(--muted); }
.prog-days b { font-weight: 600; opacity: .35; }
.prog-days b.on { opacity: 1; color: var(--text); }
.chev { color: var(--muted); }
.prog-body { padding: 0 16px 16px; border-top: 1px solid var(--border); }
.times { display: flex; align-items: flex-end; gap: 10px; margin: 14px 0; }
.times label { flex: 1; display: grid; gap: 4px; font-size: .8rem; color: var(--muted); }
.times select {
  font: inherit; font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 12px; padding: 8px 10px; width: 100%;
}
.times .arrow { padding-bottom: 12px; color: var(--muted); }
.day-chips { display: grid; grid-template-columns: repeat(7, 1fr); gap: 6px; }
.day-chips button {
  border: 1px solid var(--border); background: var(--surface-2); border-radius: 10px; padding: 9px 0; font-weight: 600;
}
.day-chips button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.prog-row { margin-top: 14px; }
.small-stepper .round { width: 38px; height: 38px; font-size: 1.2rem; }
.small-stepper .stepper-val { min-width: 70px; font-size: 1.3rem; }
.prog-err { margin-top: 10px; }
.prog-clear { margin-top: 12px; color: var(--muted); }
.sched-note { margin: 4px 4px 0; }
