:root {
  --green: #2e7d6b;
  --green-dark: #23604f;
  --green-light: #cdede3;
  --green-pale: #e8f5f0;
  --bg: #f6faf8;
  --card: #ffffff;
  --text: #17302a;
  --muted: #5d716b;
  --line: #dde8e3;
  --red: #ba1a1a;
  --red-pale: #fde8e6;
  --orange: #b85c00;
  --orange-pale: #fff1e0;
  --blue: #1565c0;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(23, 48, 42, .06), 0 4px 16px rgba(23, 48, 42, .06);
  --t: 160ms ease;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-tap-highlight-color: transparent;
}
h1, h2 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1rem; font-weight: 650; }
p { margin: .25rem 0; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.center { text-align: center; }
.error { color: var(--red); }

/* ---------- Transitions de page ---------- */
.view { animation: fade-in 260ms ease both; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.swap { animation: fade-in 220ms ease both; }

/* ---------- Boutons : retour par couleur, jamais par mouvement ---------- */
.btn {
  font: inherit; font-weight: 600;
  border: 1px solid var(--line); border-radius: 10px;
  padding: .6rem 1rem; min-height: 42px;
  background: var(--card); color: var(--green-dark);
  cursor: pointer;
  transition: background-color var(--t), color var(--t), border-color var(--t);
}
.btn:hover { background: var(--green-pale); border-color: var(--green-light); }
.btn:active { background: var(--green-light); }
.btn:disabled { opacity: .5; cursor: default; }
.btn:focus-visible, .chip:focus-visible, .dev:focus-visible { outline: 3px solid var(--green-light); outline-offset: 1px; }
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: var(--green-dark); }
.btn.primary:active { background: #1b4a3d; }
.btn.warn { color: var(--orange); }
.btn.warn:hover { background: var(--orange-pale); border-color: #f3d2ad; }
.btn.danger { color: var(--red); }
.btn.danger:hover { background: var(--red-pale); border-color: #f4c3be; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--green-pale); }
.btn.ghost.light { color: #fff; }
.btn.ghost.light:hover { background: rgba(255, 255, 255, .16); }
.btn.ghost.light:active { background: rgba(255, 255, 255, .26); }
.btn-row { display: flex; flex-wrap: wrap; gap: .5rem; margin-top: .6rem; }
.btn-row .btn { flex: 1 1 auto; }

/* ---------- Connexion ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 1.5rem; }
.login-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: .8rem; text-align: center; }
.logo-big { width: 72px; height: 72px; margin: 0 auto; }
label { display: flex; flex-direction: column; gap: .3rem; text-align: left; font-weight: 600; font-size: .9rem; }
input {
  font: inherit; padding: .65rem .8rem; border-radius: 10px; border: 1px solid var(--line);
  background: #fff; color: var(--text); transition: border-color var(--t), background-color var(--t);
}
input:hover { border-color: var(--green-light); }
input:focus { outline: none; border-color: var(--green); background: #fcfffe; }

/* ---------- Barre du haut ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; gap: .5rem;
  padding: .5rem 1rem; padding-top: max(.5rem, env(safe-area-inset-top));
  background: var(--green); color: #fff;
}
.brand { display: flex; align-items: center; gap: .5rem; font-weight: 700; font-size: 1.1rem; }
.brand img { width: 30px; height: 30px; }
.spacer { flex: 1; }
.live { display: inline-flex; align-items: center; gap: .35rem; font-size: .8rem; opacity: .9; margin-left: .5rem; }
.live i { width: 8px; height: 8px; border-radius: 50%; background: #f3b13c; }
.live.on i { background: #7ef0c6; }
.live.off i { background: #ff8a80; }

/* ---------- Mise en page ---------- */
.layout { display: grid; grid-template-columns: 260px 1fr; gap: 1rem; padding: 1rem; max-width: 1400px; margin: 0 auto; }
.layout:has(> .device-list[hidden]) { grid-template-columns: 1fr; }
.device-list { display: flex; flex-direction: column; gap: .5rem; align-self: start; position: sticky; top: 4.2rem; }
.detail { min-width: 0; }
.foot { text-align: center; padding: 1rem; }

.dev {
  display: grid; grid-template-columns: auto 1fr; gap: .1rem .6rem; align-items: center;
  text-align: left; font: inherit; color: inherit;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--card);
  padding: .7rem .8rem; cursor: pointer;
  transition: background-color var(--t), border-color var(--t);
}
.dev:hover { background: var(--green-pale); }
.dev.active { border-color: var(--green); background: var(--green-pale); }
.dev .avatar-wrap { grid-row: span 2; position: relative; }
.dev .avatar-wrap .dot { position: absolute; right: -1px; bottom: -1px; border: 2px solid var(--card); width: 13px; height: 13px; box-shadow: none; }
.dev b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dev small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #b9c6c1; display: inline-block; }
.dot.on { background: #20b37d; box-shadow: 0 0 0 3px rgba(32, 179, 125, .18); }
.dot.alert { background: var(--red); }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.1rem; }
.card h2 { margin-bottom: .4rem; }
.card-title { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap; margin-bottom: .4rem; }
.card-title h2 { margin: 0; }

.device-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 1rem; }
.device-head .grow { flex: 1; min-width: 0; }
.device-head h1 { margin: 0; }
.device-head p { margin: .1rem 0 0; }

/* Photos : image ronde, ou initiale sur fond vert */
.avatar {
  --s: 40px;
  width: var(--s); height: var(--s); border-radius: 50%; flex: none;
  display: inline-grid; place-items: center; overflow: hidden;
  background: var(--green); color: #fff; font-weight: 700; font-size: calc(var(--s) * .42);
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar.lg { --s: 64px; }
.avatar.adult { background: var(--blue); }
.photo-row { display: flex; align-items: center; gap: 1rem; margin: .5rem 0 1rem; }
.photo-btns { display: flex; flex-wrap: wrap; gap: .5rem; }
.child-hidden .child-only { display: none !important; }

/* Marqueurs de la carte */
.pin { background: none; border: 0; }
/* Leaflet remet les images des marqueurs à leur taille naturelle : on reprend la main. */
.leaflet-marker-icon.pin .avatar img { width: 100%; height: 100%; max-width: none; }
.pin .avatar { --s: 44px; border: 3px solid #fff; box-shadow: 0 2px 8px rgba(0, 0, 0, .35); }
.pin.other .avatar { --s: 36px; opacity: .92; }
.pin.stale .avatar { filter: grayscale(.8); }
.alert {
  background: var(--red-pale); color: #7a1010; border-radius: var(--radius);
  padding: .8rem 1rem; margin-bottom: 1rem; font-weight: 600;
}
.alert ul { margin: .3rem 0 0; padding-left: 1.2rem; font-weight: 400; }

.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.map-card, .timeline-card { grid-column: 1 / -1; }

.now-app { font-size: 1.6rem; font-weight: 700; color: var(--green-dark); overflow-wrap: anywhere; }
.now-app.off { color: var(--muted); }
.big { font-size: 1.6rem; font-weight: 700; }
.big .muted { font-size: 1rem; font-weight: 500; }
.bar { height: 10px; border-radius: 6px; background: var(--green-pale); overflow: hidden; margin: .5rem 0; }
.bar div { height: 100%; width: 0; background: var(--green); transition: width 400ms ease, background-color var(--t); }
.bar div.over { background: var(--red); }
.bar div.near { background: var(--orange); }

#map { height: 320px; border-radius: 10px; background: var(--green-pale); }
.map-empty { display: grid; place-items: center; height: 100%; color: var(--muted); padding: 1rem; text-align: center; }

details summary { display: flex; align-items: center; gap: .6rem; cursor: pointer; list-style: none; }
details summary::-webkit-details-marker { display: none; }
details summary::after { content: ""; margin-left: auto; width: 8px; height: 8px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg); transition: transform var(--t); }
details[open] summary::after { transform: rotate(-135deg); }
details summary h2 { margin: 0; }
.pill { font-size: .78rem; font-weight: 600; padding: .1rem .55rem; border-radius: 999px; background: var(--green-pale); color: var(--green-dark); }
.pill.bad { background: var(--red-pale); color: var(--red); }

.checks, .top { list-style: none; padding: 0; margin: .6rem 0 0; }
.checks li, .top li { display: flex; justify-content: space-between; gap: .5rem; padding: .4rem 0; border-top: 1px solid var(--line); }
.checks .ok { color: var(--green); font-weight: 600; }
.checks .ko { color: var(--red); font-weight: 600; }

.chips { display: flex; gap: .35rem; flex-wrap: wrap; }
.chip {
  font: inherit; font-size: .82rem; border: 1px solid var(--line); background: #fff; color: var(--muted);
  border-radius: 999px; padding: .25rem .7rem; cursor: pointer;
  transition: background-color var(--t), color var(--t), border-color var(--t);
}
.chip:hover { background: var(--green-pale); color: var(--green-dark); }
.chip.active { background: var(--green); border-color: var(--green); color: #fff; }

.timeline { list-style: none; margin: .4rem 0 0; padding: 0; max-height: 460px; overflow: auto; }
.timeline li { display: grid; grid-template-columns: 3.4rem 10px 1fr; gap: .6rem; align-items: baseline; padding: .45rem 0; border-top: 1px solid var(--line); }
.timeline li.new { animation: flash 1.6s ease; }
@keyframes flash { from { background: var(--green-pale); } to { background: transparent; } }
.timeline time { color: var(--muted); font-variant-numeric: tabular-nums; font-size: .85rem; }
.timeline .k { width: 10px; height: 10px; border-radius: 3px; background: #b9c6c1; align-self: center; }
.k.block { background: var(--red); }
.k.site { background: var(--orange); }
.k.app { background: var(--green); }
.k.warn { background: #7a1010; }
.k.cmd { background: var(--blue); }
.k.screen { background: #b9c6c1; }
.timeline .empty-line { display: block; color: var(--muted); padding: .6rem 0; border: 0; }

.empty { text-align: center; padding: 2.5rem 1.5rem; max-width: 560px; margin: 2rem auto; display: flex; flex-direction: column; gap: .8rem; align-items: center; }

/* ---------- Fenêtres ---------- */
.dlg {
  border: 0; border-radius: 18px; padding: 1.4rem; width: min(440px, calc(100vw - 2rem));
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25); color: var(--text);
}
.dlg[open] { animation: fade-in 200ms ease both; }
.dlg::backdrop { background: rgba(10, 30, 25, .45); animation: fade-in 200ms ease both; }
.dlg h2 { font-size: 1.15rem; margin-bottom: .8rem; }
.dlg form { display: flex; flex-direction: column; gap: .8rem; }
.dlg-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1rem; }
.dlg-actions.spread { justify-content: space-between; }
.steps { padding-left: 1.2rem; margin: 0; }
.pair-code { font-size: 2.6rem; letter-spacing: .3em; font-weight: 800; text-align: center; color: var(--green-dark); margin: .8rem 0 .2rem; font-variant-numeric: tabular-nums; }
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; margin-top: .8rem; }

.toasts { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: .5rem; z-index: 2000; width: min(420px, calc(100vw - 2rem)); }
.toast { background: #17302a; color: #fff; padding: .7rem 1rem; border-radius: 10px; box-shadow: var(--shadow); animation: fade-in 200ms ease both; }
.toast.bad { background: var(--red); }

/* ---------- Petits écrans ---------- */
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; padding: .75rem; gap: .75rem; }
  .device-list { position: static; flex-direction: row; overflow-x: auto; padding-bottom: .2rem; }
  .dev { min-width: 190px; }
  .grid { grid-template-columns: 1fr; gap: .75rem; }
  #map { height: 260px; }
}
@media (max-width: 520px) {
  .topbar { padding-left: .6rem; padding-right: .6rem; }
  #btn-pair { font-size: 0; padding: .5rem .8rem; }
  #btn-pair::after { content: "+"; font-size: 1.3rem; line-height: 1; }
  .live span { display: none; }
  h1 { font-size: 1.3rem; }
  .now-app, .big { font-size: 1.35rem; }
}
label.btn { display: inline-flex; flex-direction: row; align-items: center; cursor: pointer; font-size: inherit; }
