:root {
  color-scheme: light;
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --border: #e1e5ea;
  --text: #191919;
  --text-2: #5f6670;
  --text-3: #8a9099;
  --brand: #0f766e;
  --brand-soft: #d9f2ee;
  --on-brand: #ffffff;
  --temp: #f47230;     /* série température (orange) */
  --hum: #257de0;      /* série humidité (bleu) */
  --grid: #e8ebef;
  --ok: #0a7d2c;
  --warn: #b45309;
  --crit: #c62828;
  --crit-soft: #fde7e7;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #111315; --surface: #1b1e21; --surface-2: #24282c; --border: #2d3237;
    --text: #efefef; --text-2: #a5abb2; --text-3: #7c838b;
    --brand: #2dd4bf; --brand-soft: #123a36; --on-brand: #06201d;
    --temp: #e76624; --hum: #338ced; --grid: #2a2f34;
    --ok: #34c759; --warn: #f5a524; --crit: #ff6b6b; --crit-soft: #3a1d1d;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111315; --surface: #1b1e21; --surface-2: #24282c; --border: #2d3237;
  --text: #efefef; --text-2: #a5abb2; --text-3: #7c838b;
  --brand: #2dd4bf; --brand-soft: #123a36; --on-brand: #06201d;
  --temp: #e76624; --hum: #338ced; --grid: #2a2f34;
  --ok: #34c759; --warn: #f5a524; --crit: #ff6b6b; --crit-soft: #3a1d1d;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Barre du haut ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
}
.topbar h1 { font-size: 22px; font-weight: 700; margin: 0; flex: 1; letter-spacing: -.01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  width: 40px; height: 40px; border: 0; border-radius: 12px; background: transparent;
  display: grid; place-items: center; cursor: pointer; color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn.spin svg { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.status { font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.status.ok { background: var(--brand-soft); color: var(--brand); }
.status.err { background: var(--crit-soft); color: var(--crit); }
.status::before { content: "●"; margin-right: 5px; font-size: 9px; vertical-align: 2px; }

.banner { margin: 0 16px 8px; padding: 12px 14px; border-radius: 12px; background: var(--crit-soft); color: var(--crit); font-size: 14px; }
.banner.info { background: var(--brand-soft); color: var(--brand); }

main { padding: 4px 16px 16px; max-width: 980px; margin: 0 auto; outline: none; }

/* ---------- Cartes capteurs ---------- */
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  border: 1px solid var(--border); padding: 16px;
}
a.card { display: block; color: inherit; text-decoration: none; transition: transform .12s; }
a.card:active { transform: scale(.985); }
.sensor-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.sensor-head h2 { font-size: 16px; margin: 0; flex: 1; font-weight: 650; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex: none; }
.dot.off { background: var(--text-3); }
.meta { font-size: 12.5px; color: var(--text-2); display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.meta svg { width: 14px; height: 14px; vertical-align: -2px; margin-right: 3px; }

.values { display: flex; gap: 4px; align-items: flex-end; margin: 4px 0 12px; }
.value { flex: 1; }
.value .num { font-size: 44px; font-weight: 300; letter-spacing: -.03em; line-height: 1; font-variant-numeric: tabular-nums; }
.value .num small { font-size: 20px; font-weight: 400; margin-left: 2px; color: var(--text-2); }
.value .lbl { font-size: 12px; color: var(--text-2); margin-top: 4px; display: flex; align-items: center; gap: 6px; }
.value .lbl i { width: 10px; height: 3px; border-radius: 2px; display: inline-block; }
.value + .value { border-left: 1px solid var(--border); padding-left: 16px; }
.spark { height: 36px; margin: -4px 0 8px; }
.spark svg { width: 100%; height: 100%; display: block; }

.pill { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 600; padding: 3px 8px; border-radius: 99px; }
.pill.crit { background: var(--crit-soft); color: var(--crit); }
.comfort { font-size: 12.5px; color: var(--text-2); }

/* ---------- Détail ---------- */
.hero { display: flex; gap: 12px; margin-bottom: 12px; }
.hero .card { flex: 1; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 2px; margin: 4px 0 12px; }
.seg button { border: 0; background: transparent; padding: 7px 14px; border-radius: 9px; cursor: pointer; font-weight: 600; font-size: 13.5px; color: var(--text-2); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.chart-card { margin-bottom: 12px; }
.chart-card h3 { font-size: 14px; margin: 0 0 2px; font-weight: 650; display: flex; align-items: center; gap: 8px; }
.chart-card h3 i { width: 12px; height: 3px; border-radius: 2px; display: inline-block; }
.chart-sub { font-size: 12.5px; color: var(--text-2); margin-bottom: 8px; font-variant-numeric: tabular-nums; }
.chart { position: relative; height: 210px; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .axis text { fill: var(--text-3); stroke: none; font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .threshold { stroke: var(--crit); stroke-width: 1; stroke-dasharray: 4 4; opacity: .7; }
.chart .threshold-lbl { fill: var(--crit); stroke: none; font-size: 10.5px; }
.chart .cross { stroke: var(--text-3); stroke-width: 1; }
.chart .hoverdot { stroke: var(--surface); stroke-width: 2; }
.tip {
  position: absolute; top: 0; pointer-events: none; background: var(--text); color: var(--bg);
  font-size: 12px; padding: 6px 9px; border-radius: 8px; white-space: nowrap; transform: translateX(-50%);
  font-variant-numeric: tabular-nums; opacity: 0; transition: opacity .1s;
}
.tip b { font-size: 14px; }
.empty-chart { height: 100%; display: grid; place-items: center; color: var(--text-3); font-size: 13px; }

details.table { margin: 4px 0 12px; }
details.table summary { cursor: pointer; color: var(--text-2); font-size: 13.5px; padding: 6px 0; }
.tbl-wrap { max-height: 300px; overflow: auto; border-radius: 12px; border: 1px solid var(--border); }
table { border-collapse: collapse; width: 100%; font-size: 13px; font-variant-numeric: tabular-nums; }
th, td { text-align: left; padding: 7px 12px; border-bottom: 1px solid var(--border); }
th { position: sticky; top: 0; background: var(--surface-2); font-weight: 600; }
td.r, th.r { text-align: right; }

/* ---------- Formulaires ---------- */
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-2); margin: 22px 4px 8px; font-weight: 650; }
.form { display: grid; gap: 14px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
label.field { display: grid; gap: 5px; font-size: 13px; color: var(--text-2); font-weight: 550; }
input[type=text], input[type=number], input[type=url], input[type=password], select {
  width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); font-size: 15px;
}
input:focus, select:focus { outline: 2px solid var(--brand); outline-offset: 0; border-color: transparent; }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 15px; }
.switch input { width: 46px; height: 28px; appearance: none; background: var(--border); border-radius: 99px; position: relative; cursor: pointer; transition: background .15s; flex: none; }
.switch input::after { content: ""; position: absolute; width: 22px; height: 22px; border-radius: 50%; background: #fff; top: 3px; left: 3px; transition: left .15s; box-shadow: 0 1px 2px rgba(0,0,0,.3); }
.switch input:checked { background: var(--brand); }
.switch input:checked::after { left: 21px; }
.btn { border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 650; cursor: pointer; background: var(--brand); color: var(--on-brand); font-size: 15px; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.hint { font-size: 12.5px; color: var(--text-3); margin: 0; }
.hint code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; color: var(--text-2); }

/* ---------- Alertes ---------- */
.events { display: grid; gap: 8px; }
.event { display: flex; gap: 12px; align-items: flex-start; }
.event .ic { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; flex: none; background: var(--crit-soft); color: var(--crit); }
.event.ok .ic { background: var(--brand-soft); color: var(--brand); }
.event .ic svg { width: 18px; height: 18px; }
.event p { margin: 0; font-size: 14.5px; }
.event time { font-size: 12px; color: var(--text-3); }

.empty { text-align: center; color: var(--text-2); padding: 48px 16px; }
.empty svg { width: 44px; height: 44px; color: var(--text-3); margin-bottom: 8px; }

/* ---------- Barre d'onglets ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px 0 calc(6px + env(safe-area-inset-bottom));
}
.tabbar a { position: relative; display: grid; justify-items: center; gap: 2px; color: var(--text-3); text-decoration: none; font-size: 11.5px; font-weight: 600; padding: 4px 18px; border-radius: 12px; }
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--brand); }
.badge { position: absolute; top: 0; right: 14px; background: var(--crit); color: #fff; font-size: 10.5px; min-width: 18px; height: 18px; border-radius: 9px; display: grid; place-items: center; padding: 0 5px; font-style: normal; }

.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100% - 32px); z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.skeleton { background: linear-gradient(90deg, var(--surface-2), var(--surface), var(--surface-2)); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: var(--radius); height: 170px; }
@keyframes sk { to { background-position: -200% 0; } }

@media (max-width: 420px) {
  .value .num { font-size: 38px; }
  .hero .card { padding: 14px; }
  .value + .value { padding-left: 12px; }
}
