:root {
  --bg: #0b1120;
  --surface: #111a2e;
  --surface-2: #1e293b;
  --border: #334155;
  --text: #f8fafc;
  --muted: #a3b1c6;
  --ok: #22c55e;
  --ok-bg: rgba(34, 197, 94, .14);
  --warn: #f59e0b;
  --danger: #ef4444;
  --danger-bg: rgba(239, 68, 68, .14);
  --accent: #38bdf8;
  --kid: #c084fc;
  --wall: #e2e8f0;
  --floor: #16213a;
  --radius: 16px;
  --space: 16px;
  --tab-h: 64px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 16px; line-height: 1.5; }
body { height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 1.35rem; margin-bottom: 12px; }
h3 { font-size: 1.05rem; margin: 18px 0 8px; }
button, input, select { font: inherit; color: inherit; }
svg.ic { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.num { font-variant-numeric: tabular-nums; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--accent); color: #000; padding: 8px 12px; border-radius: 8px; z-index: 100; }
.skip:focus { left: 8px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- structure ---------- */
.top { flex: none; display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) var(--space) 10px; background: var(--bg); border-bottom: 1px solid var(--surface-2); }
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 1.1rem; }
.logo { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 12px; background: var(--ok-bg); color: var(--ok); }
.live { margin: 0; font-size: .8rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.live.on .dot { background: var(--ok); animation: blink 2s ease-out infinite; }
.live.off .dot { background: var(--danger); }
@keyframes blink { 0% { box-shadow: 0 0 0 0 rgba(34, 197, 94, .6); } 100% { box-shadow: 0 0 0 8px rgba(34, 197, 94, 0); } }

#banners { flex: none; max-height: 45vh; overflow-y: auto; padding: 0 var(--space); }
#banners:not(:empty) > div:not(:empty) { padding-top: 10px; }
main { flex: 1; min-height: 0; overflow-y: auto; outline: none; }
.page { padding: var(--space); max-width: 960px; margin: 0 auto; }
.view { animation: fade .2s ease-out; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.tabs { flex: none; display: grid; grid-template-columns: repeat(3, 1fr); height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom); background: var(--surface); border-top: 1px solid var(--surface-2); }
.tab { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  border: 0; background: transparent; color: var(--muted); font-size: .78rem; cursor: pointer; min-height: 48px; touch-action: manipulation; }
.tab.active { color: var(--ok); }
.badge { position: absolute; top: 6px; left: 56%; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px;
  background: var(--danger); color: #fff; font-size: .7rem; display: grid; place-items: center; }

/* ---------- composants ---------- */
.btn { min-height: 48px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface-2);
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 8px; touch-action: manipulation;
  transition: transform .15s ease-out, background-color .15s ease-out; }
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--ok); border-color: var(--ok); color: #052e14; font-weight: 600; }
.btn.danger { background: transparent; border-color: var(--danger); color: #fca5a5; }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; margin-top: 12px; }
.btn[disabled], .tool[disabled] { opacity: .45; cursor: not-allowed; }
.small-btn { min-height: 40px; padding: 0 12px; font-size: .85rem; }
.icon-btn { width: 48px; height: 48px; display: grid; place-items: center; border-radius: 12px; border: 1px solid var(--surface-2);
  background: var(--surface); cursor: pointer; touch-action: manipulation; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 12px; }
.actions.end { justify-content: flex-end; }
.actions select { flex: 1 1 160px; }
.card { background: var(--surface); border: 1px solid var(--surface-2); border-radius: var(--radius); padding: var(--space); margin-top: 12px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.row-between p { margin: 2px 0 0; }
details.card > summary { cursor: pointer; font-weight: 600; min-height: 32px; list-style: none; display: flex; align-items: center; justify-content: space-between; }
details.card > summary::after { content: "›"; font-size: 1.4rem; color: var(--muted); transition: transform .2s ease-out; }
details.card[open] > summary::after { transform: rotate(90deg); }
details.card > summary::-webkit-details-marker { display: none; }
label { display: block; font-size: .85rem; color: var(--muted); margin: 10px 0 4px; }
input, select { width: 100%; min-height: 48px; padding: 0 12px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); }
.cfg { padding: 12px 0; border-bottom: 1px solid var(--surface-2); }
.cfg.unknown { border-left: 3px solid var(--danger); padding-left: 10px; }
.cfg .title { font-weight: 600; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: .78rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.chip svg.ic { width: 14px; height: 14px; }
.chips { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.chips-choice { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.chip-btn { min-height: 44px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); cursor: pointer; }
.chip-btn:active { background: var(--ok-bg); border-color: var(--ok); }
.exp { font-size: .7rem; font-weight: 700; letter-spacing: .04em; padding: 2px 8px; border-radius: 999px; background: rgba(245, 158, 11, .15); color: var(--warn); }
.switch { width: 56px; height: 32px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); position: relative; cursor: pointer; flex: none; }
.switch span { position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: var(--muted); transition: transform .2s ease-out, background-color .2s; }
.switch[aria-checked="true"] { background: rgba(245, 158, 11, .25); border-color: var(--warn); }
.switch[aria-checked="true"] span { transform: translateX(24px); background: var(--warn); }
.note { color: var(--muted); font-size: .875rem; }
.plain { list-style: none; padding: 0; margin: 0; }
.qr { display: inline-block; background: #fff; padding: 8px; border-radius: 12px; margin-top: 8px; }
.qr svg { width: 220px; height: 220px; display: block; }
.link { color: var(--accent); word-break: break-all; }
.explain { padding-left: 18px; margin: 8px 0; }
.explain li { margin-bottom: 8px; }

/* ---------- bandeaux ---------- */
.alert { display: flex; gap: 12px; align-items: center; padding: 12px 14px; border-radius: var(--radius);
  background: var(--danger-bg); border: 1px solid var(--danger); }
.alert > svg { color: var(--danger); }
.alert > div { flex: 1; display: flex; flex-direction: column; }
.pair { padding: 14px; border-radius: var(--radius); border: 1px solid var(--accent); background: rgba(56, 189, 248, .1); }
.pair .code { font-size: 2rem; font-weight: 700; letter-spacing: .25em; font-variant-numeric: tabular-nums; }

/* ---------- plan ---------- */
#view-plan { height: 100%; display: flex; flex-direction: column; position: relative; }
.plan-bar { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px var(--space) 0; }
.plan-actions { display: flex; gap: 8px; margin-left: auto; }
body.editing .plan-actions { display: none; }
.floors { display: flex; gap: 4px; padding: 4px; background: var(--surface); border-radius: 12px; border: 1px solid var(--surface-2); overflow-x: auto; }
.seg { min-height: 40px; min-width: 64px; padding: 0 14px; border-radius: 9px; border: 0; background: transparent; color: var(--muted); cursor: pointer; white-space: nowrap; }
.seg.on { background: var(--surface-2); color: var(--text); font-weight: 600; }
.seg.add { color: var(--ok); }
.edit-top { flex: none; display: flex; align-items: center; gap: 8px; padding: 10px var(--space) 0; }
.edit-title { font-weight: 700; margin-right: auto; }
.plan-wrap { flex: 1; min-height: 0; position: relative; margin: 8px var(--space); border-radius: var(--radius); background: #0d1628;
  border: 1px solid var(--surface-2); overflow: hidden; }
#plan { width: 100%; height: 100%; display: block; touch-action: manipulation; user-select: none; -webkit-user-select: none; }
body.editing #plan { touch-action: none; }
#plan.drawing { cursor: crosshair; }

.grid { stroke: rgba(148, 163, 184, .1); stroke-width: .02; }
.grid.major { stroke: rgba(148, 163, 184, .2); stroke-width: .03; }
.room-floor { fill: var(--floor); transition: fill .4s ease-out; }
.room-wall { fill: none; stroke: var(--wall); stroke-width: .14; stroke-linejoin: miter; }
.room-name { fill: var(--muted); text-anchor: middle; dominant-baseline: middle; font-family: var(--font); font-weight: 600; pointer-events: none; }
.room.lit .room-floor { fill: rgba(34, 197, 94, .3); animation: lit 1.2s ease-out infinite alternate; }
.room.lit .room-name { fill: #d1fae5; }
@keyframes lit { from { fill: rgba(34, 197, 94, .18); } to { fill: rgba(34, 197, 94, .38); } }
.sensor-missing { fill: none; stroke: rgba(148, 163, 184, .45); stroke-width: .03; stroke-dasharray: .05 .04; }
.sensor-missing.hide { display: none; }
body.editing .room { cursor: move; }
body.editing .room.sel .room-floor { fill: rgba(56, 189, 248, .18); }
body.editing .room.sel .room-wall { stroke: var(--accent); }
.door-gap { stroke: #0d1628; stroke-width: .2; }
.door-leaf { stroke: var(--wall); stroke-width: .04; }
.door-swing { fill: none; stroke: rgba(226, 232, 240, .55); stroke-width: .03; stroke-dasharray: .08 .06; }
.door.sel .door-leaf, .door.sel .door-swing { stroke: var(--accent); }
.door-hit { stroke: transparent; stroke-width: .7; cursor: pointer; }
.handle { fill: var(--accent); stroke: #0b1120; stroke-width: .04; pointer-events: none; }
.handle-hit { fill: transparent; cursor: nwse-resize; }
.draft-room { fill: rgba(34, 197, 94, .15); stroke: var(--ok); stroke-width: .08; stroke-dasharray: .2 .1; }

.obj { color: var(--text); }
.obj use { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.obj-bg { fill: var(--surface-2); stroke: var(--border); stroke-width: .04; }
.obj-box .obj-bg { fill: #0c4a6e; stroke: var(--accent); }
.obj-tv .obj-bg { fill: #312e81; stroke: #818cf8; }
.obj-camera .obj-bg { fill: #3f3f46; stroke: #a1a1aa; }
.obj.sel .obj-bg { stroke: var(--ok); stroke-width: .08; }
body.editing .obj { cursor: grab; }
.obj-dot { fill: var(--muted); stroke: #0b1120; stroke-width: .03; }
.obj-camera.ok .obj-dot { fill: var(--ok); }
.obj-camera.off .obj-dot { fill: var(--danger); }
.obj-pulse { fill: none; stroke: #818cf8; stroke-width: .06; opacity: 0; transform-box: fill-box; transform-origin: center; }
.obj-tv.active .obj-pulse { animation: pulse 1.6s ease-out infinite; }
@keyframes pulse { from { transform: scale(1); opacity: .9; } to { transform: scale(2.6); opacity: 0; } }

.ring-band { fill: none; opacity: .09; }
.ring-edge { fill: none; stroke-width: .05; opacity: .6; }
.avatar-ring { fill: var(--surface); stroke-width: .07; }
.avatar-moving { fill: var(--ok); stroke: #0b1120; stroke-width: .03; }
.intrus circle { fill: var(--danger); stroke: #0b1120; stroke-width: .05; }
.intrus text { fill: #fff; font-weight: 800; text-anchor: middle; font-family: var(--font); }
.intrus { animation: alarm 1s ease-in-out infinite alternate; }
@keyframes alarm { from { opacity: 1; } to { opacity: .55; } }

.demo-badge { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); padding: 6px 14px; border-radius: 999px;
  background: var(--warn); color: #1c1100; font-weight: 800; font-size: .8rem; letter-spacing: .04em; pointer-events: none; }
.plan-hint { position: absolute; left: 12px; right: 12px; bottom: 12px; padding: 12px 14px; border-radius: 12px;
  background: rgba(17, 26, 46, .94); border: 1px solid var(--border); }
.plan-hint.onboard { top: 12px; bottom: auto; max-width: 420px; }
.plan-hint ol { margin: 6px 0 10px; padding-left: 20px; }
.plan-hint p { margin: 4px 0 10px; }

.near { flex: none; list-style: none; margin: 0 var(--space); padding: 0; display: flex; gap: 8px; overflow-x: auto; }
.near-chip { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 6px; border-radius: 999px;
  background: var(--surface); border: 2px solid var(--border); font-size: .85rem; white-space: nowrap; }
.near-chip img, .out-chip img { border-radius: 50%; object-fit: cover; }
.outside { flex: none; display: flex; align-items: center; gap: 8px; margin: 8px var(--space) 10px; padding: 8px 10px;
  border-radius: 12px; background: var(--surface); border: 1px dashed var(--border); overflow-x: auto; }
.out-label { font-weight: 700; color: var(--muted); margin-right: 4px; }
.out-chip { flex: none; display: inline-flex; align-items: center; gap: 6px; font-size: .85rem; white-space: nowrap; }
.out-chip img { filter: grayscale(.6); opacity: .8; }

.edit-tools { flex: none; padding: 0 var(--space) 10px; }
.edit-tip { margin: 0 0 6px; min-height: 1.3em; }
.tool-row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.tool { min-height: 56px; border-radius: 12px; border: 1px solid var(--surface-2); background: var(--surface); color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; font-size: .75rem; cursor: pointer; touch-action: manipulation; }
.tool.on { color: var(--ok); border-color: var(--ok); background: var(--ok-bg); }
.tool.danger:not([disabled]) { color: #fca5a5; }
.palette { flex: none; display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; padding: 0 var(--space) 8px; }
.palette .btn { flex-direction: column; gap: 2px; min-height: 60px; font-size: .8rem; padding: 0 6px; }

/* ---------- famille ---------- */
.people { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
@media (min-width: 768px) { .people { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.person { background: var(--surface); border: 1px solid var(--surface-2); border-radius: var(--radius); padding: 14px;
  display: flex; flex-direction: column; align-items: center; text-align: center; gap: 6px; }
.person.here { border-color: rgba(34, 197, 94, .55); }
.avatar { width: 72px; height: 72px; border-radius: 50%; object-fit: cover; background: var(--surface-2); border: 3px solid var(--surface-2); }
.person.here .avatar { border-color: var(--ok); }
.person.away .avatar { filter: grayscale(.7); opacity: .7; }
.person .name { font-weight: 700; font-size: 1.05rem; }
.role { font-size: .75rem; font-weight: 600; padding: 2px 10px; border-radius: 999px; background: var(--surface-2); color: var(--accent); }
.role.kid { color: var(--kid); }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: .9rem; font-weight: 600; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status.ok { color: var(--ok); }
.status.ok::before { background: var(--ok); }
.status.away { color: var(--muted); }

/* ---------- réglages ---------- */
canvas { width: 100%; display: block; margin-top: 10px; }
.legend { list-style: none; padding: 0; margin: 10px 0 0; display: flex; flex-wrap: wrap; gap: 8px 16px; font-size: .85rem; }
.legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
.devices { list-style: none; padding: 0; margin: 14px 0 0; display: grid; gap: 8px; }
.device { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 10px 12px; background: var(--bg); border: 1px solid var(--surface-2); border-radius: 12px; }
.device.unknown { border-color: var(--danger); background: var(--danger-bg); }
.device.gone { opacity: .6; }
.device .meta { grid-column: 1 / -1; font-size: .8rem; color: var(--muted); }
.sources { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.sources li { display: grid; grid-template-columns: auto 1fr; gap: 0 10px; font-size: .9rem; }
.sources .detail { grid-column: 2; font-size: .8rem; color: var(--muted); }
.state { font-size: .72rem; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); align-self: start; }
.state.ok { color: var(--ok); } .state.erreur { color: var(--danger); } .state.na { color: var(--muted); }
.journal { list-style: none; padding: 0; margin: 10px 0 0; display: grid; gap: 6px; }
.journal li { display: grid; grid-template-columns: 4.2em 1fr; gap: 10px; padding: 10px 12px; background: var(--bg); border-radius: 12px; font-size: .9rem; }
.journal time { color: var(--muted); font-variant-numeric: tabular-nums; }
.journal li.intrus, .journal li.alerte { border-left: 3px solid var(--danger); }
.journal li.arrivee { border-left: 3px solid var(--ok); }
.journal li.depart { border-left: 3px solid var(--muted); }
.journal li.refus { border-left: 3px solid var(--warn); }

/* ---------- accès, feuille, notification ---------- */
.gate { background: var(--surface); border-radius: var(--radius); padding: 20px; margin: var(--space); display: grid; gap: 8px; }
.sheet { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .55); display: flex; flex-direction: column; justify-content: flex-end; }
.sheet-body { background: var(--surface); max-height: 85dvh; overflow: auto; padding: var(--space) var(--space) calc(var(--space) + env(safe-area-inset-bottom));
  border-radius: 20px 20px 0 0; width: 100%; max-width: 640px; margin: 0 auto; animation: up .25s ease-out; }
@keyframes up { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }
.toast { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tab-h) + 20px + env(safe-area-inset-bottom));
  background: var(--surface-2); border: 1px solid var(--border); padding: 12px 18px; border-radius: 12px; z-index: 60; max-width: 90vw; }

/* ---------- grand écran : rail à gauche, panneau à droite ---------- */
@media (min-width: 1024px) {
  body { display: grid; grid-template-columns: 96px 1fr; grid-template-rows: auto auto 1fr; }
  .top { grid-column: 2; }
  #banners { grid-column: 2; }
  main { grid-column: 2; grid-row: 3; }
  .tabs { grid-column: 1; grid-row: 1 / 4; display: flex; flex-direction: column; justify-content: flex-start; gap: 8px;
    height: auto; padding: 16px 8px; border-top: 0; border-right: 1px solid var(--surface-2); }
  .tab { min-height: 72px; border-radius: 14px; }
  .tab.active { background: var(--ok-bg); }
  .toast { bottom: 24px; }
  #view-plan { display: grid; grid-template-columns: 1fr 300px; grid-template-rows: auto auto 1fr auto auto auto; }
  #view-plan > .plan-bar, #view-plan > .edit-top, #view-plan > .plan-wrap, #view-plan > .palette, #view-plan > .edit-tools { grid-column: 1; }
  #view-plan > .plan-wrap { grid-row: 3 / 7; }
  #view-plan > .near { grid-column: 2; grid-row: 3; flex-direction: column; overflow: visible; margin: 8px var(--space) 0 0; }
  #view-plan > .outside { grid-column: 2; grid-row: 4; flex-direction: column; align-items: flex-start; margin: 8px var(--space) 0 0; }
  .near-chip { white-space: normal; border-radius: 14px; }
  body.editing #view-plan { grid-template-columns: 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
