:root {
  --bg: #0b0d10;
  --surface: #161a20;
  --surface-2: #1f242c;
  --text: #f2f4f7;
  --muted: #9aa3ae;
  --accent: #4f8cff;
  --live: #ff3b4e;
  --ok: #2ecc71;
  --radius: 18px;
  --ease: cubic-bezier(.2, .8, .2, 1);
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root { --bg: #f5f6f8; --surface: #fff; --surface-2: #eceef2; --text: #12151a; --muted: #5d6673; color-scheme: light; }
}

* { box-sizing: border-box; margin: 0; }
html, body { height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
#app { height: 100%; }
button { font: inherit; color: inherit; cursor: pointer; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.screen { min-height: 100%; padding: max(16px, env(safe-area-inset-top)) 20px max(20px, env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 20px; }
.center { justify-content: center; max-width: 440px; margin: 0 auto; }
h1 { font-size: 1.6rem; letter-spacing: -.01em; }
.topbar { display: flex; justify-content: space-between; align-items: center; min-height: 44px; }
.link { background: none; border: 0; color: var(--muted); min-height: 44px; padding: 0 4px; text-decoration: underline; text-underline-offset: 3px; }

.btn { min-height: 52px; padding: 0 22px; border-radius: 14px; border: 0; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; text-decoration: none; transition: transform .15s var(--ease), opacity .15s; }
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .6; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.ghost { background: var(--surface-2); color: var(--text); }
.btn.danger { background: var(--live); color: #fff; }
.btn.small { min-height: 44px; padding: 0 16px; font-size: .9rem; }
.camera-screen .btn { width: min(100%, 360px); }
.camera-screen .siren-stop { min-height: 72px; font-size: 1.3rem; animation: pulse 1s infinite; }
.mic-off { color: var(--muted); font-size: .9rem; }

/* Onboarding */
.logo { width: 64px; height: 64px; border-radius: 20px; background: var(--surface-2); display: grid; place-items: center; }
.logo span { width: 26px; height: 26px; border-radius: 50%; border: 6px solid var(--accent); }
.choice { display: flex; gap: 16px; align-items: center; text-align: left; padding: 20px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--surface-2); transition: transform .15s var(--ease), border-color .15s; }
.choice:active { transform: scale(.98); border-color: var(--accent); }
.choice strong { display: block; font-size: 1.1rem; }
.choice small { color: var(--muted); font-size: .9rem; }
.choice-icon { flex: none; width: 48px; height: 48px; border-radius: 14px; display: grid; place-items: center; background: var(--surface-2); color: var(--accent); font-size: 1.2rem; }

/* Mode Caméra */
.camera-screen { align-items: center; text-align: center; }
.camera-screen .topbar { width: 100%; }
.cam-name { font-weight: 600; }
.status { display: inline-flex; align-items: center; gap: 12px; font-size: 1.5rem; font-weight: 700; padding: 14px 24px; border-radius: 999px; background: var(--surface); }
.status .dot { width: 14px; height: 14px; border-radius: 50%; background: var(--muted); }
.status[data-state="live"] .dot { background: var(--live); animation: pulse 1.6s infinite; }
.status[data-state="live"] { color: var(--live); }
.pair { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.status[data-state="live"] ~ .pair { display: none; }
.qr { width: min(64vw, 260px); aspect-ratio: 1; background: #fff; border-radius: var(--radius); padding: 12px; }
.qr svg { width: 100%; height: 100%; display: block; }
.pair-help { color: var(--muted); max-width: 280px; }
.pin strong { font-size: 1.4rem; letter-spacing: .12em; font-variant-numeric: tabular-nums; }
.preview { width: min(100%, 360px); aspect-ratio: 16 / 9; object-fit: cover; border-radius: var(--radius); background: #000; }

/* Appairage */
.steps { padding-left: 1.2em; color: var(--muted); display: grid; gap: 8px; }
.steps strong { color: var(--text); }
.pin-form { display: grid; gap: 10px; padding: 20px; border-radius: var(--radius); background: var(--surface); }
.pin-form label { color: var(--muted); font-size: .95rem; }
.pin-form input { width: 100%; min-width: 0; min-height: 60px; text-align: center; font-size: 1.8rem; letter-spacing: .2em; font-variant-numeric: tabular-nums; border-radius: 14px; border: 2px solid var(--surface-2); background: var(--bg); color: var(--text); }
.pin-form input:focus { border-color: var(--accent); outline: none; }
.form-error { color: var(--live); min-height: 1.4em; font-size: .95rem; }

/* Visionneuse : la vidéo d'abord */
.stage { position: fixed; inset: 0; padding: 0; background: #000; overflow: hidden; color: #fff; touch-action: none; user-select: none; }
.feed {
  position: absolute; top: 50%; left: 50%; width: 100%; height: 100%; object-fit: contain;
  transform: translate(-50%, -50%) translate(var(--px, 0px), var(--py, 0px)) rotate(var(--rot, 0deg)) scaleX(var(--mirror, 1)) scale(var(--zoom, 1));
  transition: transform .2s var(--ease), filter .2s;
}
.stage.gesturing .feed { transition: filter .2s; } /* suit le doigt sans retard */
.stage[data-rot="90"] .feed, .stage[data-rot="270"] .feed { width: 100vh; width: 100dvh; height: 100vw; }
.stage.night .feed { filter: grayscale(1) brightness(1.7) contrast(1.4); }
.zoom-chip { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 8px 16px; border-radius: 999px; background: #000a; font-weight: 700; font-size: 1.2rem; font-variant-numeric: tabular-nums; pointer-events: none; }
.toast { position: absolute; left: 50%; bottom: 140px; transform: translateX(-50%); max-width: calc(100% - 32px); padding: 12px 18px; border-radius: 14px; background: var(--surface); color: var(--text); box-shadow: 0 8px 24px #0006; animation: up .2s var(--ease); z-index: 3; }
.focus-ring { position: absolute; width: 72px; height: 72px; margin: -36px 0 0 -36px; border: 2px solid #ffd23f; border-radius: 12px; pointer-events: none; animation: focus 1.2s var(--ease) forwards; }
@keyframes focus { 0% { transform: scale(1.5); opacity: 0; } 15% { transform: scale(1); opacity: 1; } 80% { opacity: 1; } 100% { opacity: 0; } }
.icon-btn.small { width: 44px; height: 44px; }
.icon-btn.small svg { width: 20px; height: 20px; }
.listen[aria-pressed="true"] { background: var(--accent); }
.listen[aria-pressed="true"] .slash, .listen[aria-pressed="false"] .waves { display: none; }
.talk { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.talk.talking { background: var(--live); transform: scale(1.12); box-shadow: 0 0 0 8px #ff3b4e44; }
.talk-chip, .siren-pill { position: absolute; left: 50%; transform: translateX(-50%); padding: 8px 16px; border-radius: 999px; font-weight: 700; white-space: nowrap; z-index: 1; }
.talk-chip { top: max(70px, calc(env(safe-area-inset-top) + 58px)); background: var(--live); color: #fff; animation: pulse 1.2s infinite; }
.siren-pill { top: max(116px, calc(env(safe-area-inset-top) + 104px)); min-height: 44px; border: 0; background: #ffd23f; color: #000; }
.icon-btn:disabled { opacity: .35; }
.icon-btn[aria-pressed="true"] { background: #ffd23f; color: #000; }
.hud, .actionbar { position: absolute; left: 0; right: 0; display: flex; align-items: center; transition: opacity .25s var(--ease), transform .25s var(--ease); }
.hud { top: 0; flex-wrap: wrap; gap: 12px; padding: max(12px, env(safe-area-inset-top)) 16px 24px; background: linear-gradient(#000a, transparent); }
.actionbar { bottom: 0; justify-content: center; gap: 14px; padding: 32px 16px max(24px, env(safe-area-inset-bottom)); background: linear-gradient(transparent, #000a); }
.stage[data-controls="off"][data-state="live"] .hud { opacity: 0; transform: translateY(-8px); pointer-events: none; }
.stage[data-controls="off"][data-state="live"] .actionbar { opacity: 0; transform: translateY(8px); pointer-events: none; }
.badge { flex: none; white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px; background: #0009; font-size: .8rem; font-weight: 700; letter-spacing: .04em; }
.badge .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--live); animation: pulse 1.6s infinite; }
.stage:not([data-state="live"]) .badge { display: none; } /* laisse la place au nom, utile pour changer de caméra */
.rtt { font-weight: 500; opacity: .8; font-variant-numeric: tabular-nums; }
.icon-btn { width: 52px; height: 52px; border-radius: 50%; border: 0; background: #ffffff1f; backdrop-filter: blur(12px); display: grid; place-items: center; color: #fff; font-size: 1.4rem; transition: transform .15s var(--ease), background .15s; }
.icon-btn:active { transform: scale(.92); background: #ffffff33; }
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; }
.icon-btn.big { width: 72px; height: 72px; background: #fff; color: #000; }
.icon-btn.big svg { width: 30px; height: 30px; }
.icon-btn.busy { animation: pulse 1s infinite; }
.stage:not([data-state="live"]) .actionbar { opacity: .35; pointer-events: none; }
.menu { width: 44px; height: 44px; }

.loading { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 16px; color: #ccd; }
.stage:not([data-state="connecting"]) .loading { display: none; }
.spinner { width: 40px; height: 40px; border-radius: 50%; border: 3px solid #fff3; border-top-color: #fff; animation: spin .9s linear infinite; }
.overlay { position: absolute; inset: 0; display: grid; place-items: center; padding: 20px; pointer-events: none; }
.overlay > * { pointer-events: auto; }
.stage.flash::after { content: ""; position: absolute; inset: 0; background: #fff; animation: flash .25s ease-out forwards; pointer-events: none; }

/* Cartes et feuilles */
.card { background: var(--surface); color: var(--text); border-radius: var(--radius); padding: 24px; display: grid; gap: 12px; justify-items: center; text-align: center; max-width: 360px; margin: auto; }
.card p { color: var(--muted); }
.error-icon { width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center; background: #ff3b4e22; color: var(--live); font-weight: 800; font-size: 1.4rem; }
#app > .card { margin-top: 30vh; }
.sheet { position: absolute; left: 0; right: 0; bottom: 0; background: var(--surface); color: var(--text); border-radius: 24px 24px 0 0; padding: 16px 16px max(16px, env(safe-area-inset-bottom)); display: grid; gap: 14px; animation: up .25s var(--ease); }
.sheet img { width: 100%; max-height: 60vh; object-fit: contain; border-radius: 12px; background: #000; }
.sheet-actions { display: flex; gap: 10px; }
.sheet-actions .btn { flex: 1; }
.sheet { z-index: 2; max-height: 85vh; overflow-y: auto; touch-action: pan-y; }
.saved { color: var(--ok); font-weight: 600; text-align: center; }

/* Panneau de réglages */
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet-head h2 { font-size: 1.2rem; }
.sheet-head .icon-btn { width: 44px; height: 44px; background: var(--surface-2); color: var(--text); font-size: 1rem; }
.settings-sheet details { border-top: 1px solid var(--surface-2); padding-top: 10px; }
.settings-sheet summary { min-height: 44px; display: flex; align-items: center; font-weight: 700; cursor: pointer; list-style: none; }
.settings-sheet summary::after { content: "⌄"; margin-left: auto; color: var(--muted); transition: transform .2s var(--ease); }
.settings-sheet details[open] summary::after { transform: rotate(180deg); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 52px; }
.row > span { color: var(--muted); }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; }
.seg button { min-width: 48px; min-height: 40px; padding: 0 10px; border: 0; border-radius: 9px; background: none; color: var(--text); font-size: .9rem; font-weight: 600; transition: background .15s var(--ease); }
.seg button[aria-pressed="true"] { background: var(--accent); color: #fff; }
.seg button:disabled { opacity: .3; cursor: not-allowed; }
.seg button.pending { animation: pulse .8s infinite; }
.actual { color: var(--muted); font-size: .85rem; font-variant-numeric: tabular-nums; }
input[role="switch"] { appearance: none; width: 52px; height: 32px; border-radius: 999px; background: var(--surface-2); position: relative; cursor: pointer; transition: background .15s var(--ease); }
input[role="switch"]::after { content: ""; position: absolute; top: 3px; left: 3px; width: 26px; height: 26px; border-radius: 50%; background: #fff; transition: transform .15s var(--ease); }
input[role="switch"]:checked { background: var(--accent); }
input[role="switch"]:checked::after { transform: translateX(20px); }

@keyframes pulse { 50% { opacity: .35; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes flash { from { opacity: .8; } to { opacity: 0; } }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Détection de mouvement */
.detect { color: var(--ok); font-weight: 600; }
.detect.rec { color: var(--live); animation: pulse 1.2s infinite; }
.rec { padding: 4px 10px; border-radius: 999px; background: var(--live); color: #fff; font-size: .75rem; font-weight: 800; animation: pulse 1.2s infinite; }
.gallery-btn { position: relative; }
.gallery-btn .count { position: absolute; top: -4px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 999px; background: var(--live); color: #fff; font-size: .75rem; font-weight: 800; line-height: 20px; text-align: center; }
.hint { color: var(--muted); font-size: .9rem; }
.empty { color: var(--muted); text-align: center; padding: 32px 8px; }

/* Panneaux plein écran (galerie, fiche) */
.sheet > * { min-width: 0; } /* sans ça, une vidéo large élargit la feuille et coupe les boutons */
.sheet.panel { top: 0; max-height: none; border-radius: 0; padding: max(10px, env(safe-area-inset-top)) 0 0; display: flex; flex-direction: column; gap: 0; background: var(--bg); overflow: hidden; animation: fade .18s var(--ease); }
.panel-head { display: flex; align-items: center; gap: 10px; padding: 4px 12px 10px; }
.panel-head h2 { font-size: 1.2rem; line-height: 1.2; }
.panel-head .sub { color: var(--muted); font-size: .85rem; }
.panel .icon-btn.back { flex: none; width: 44px; height: 44px; background: var(--surface-2); color: var(--text); backdrop-filter: none; }

/* Galerie */
.tabs { display: flex; gap: 8px; padding: 0 12px 12px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button { flex: none; min-height: 38px; padding: 0 16px; border-radius: 999px; border: 1px solid var(--surface-2); background: none; color: var(--text); font-weight: 600; font-size: .9rem; }
.tabs button[aria-selected="true"] { background: var(--text); color: var(--bg); border-color: transparent; }
.tabs .n { margin-left: 6px; opacity: .55; font-variant-numeric: tabular-nums; }
.panel-body { flex: 1; overflow-y: auto; padding: 0 12px max(16px, env(safe-area-inset-bottom)); }
.day h3 { margin: 6px 2px 8px; font-size: .95rem; font-weight: 700; }
.day h3::first-letter { text-transform: uppercase; }
.day + .day { margin-top: 14px; }
.gallery .grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 3px; border-radius: 12px; overflow: hidden; }
.tile { position: relative; padding: 0; border: 0; aspect-ratio: 1; overflow: hidden; background: var(--surface-2); color: #fff; }
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile span { position: absolute; padding: 2px 6px; border-radius: 6px; background: #000000a0; font-size: .72rem; font-weight: 700; line-height: 1.4; }
.t-time { left: 5px; bottom: 5px; }
.t-dur { right: 5px; bottom: 5px; font-variant-numeric: tabular-nums; }
.t-tag { left: 5px; top: 5px; background: var(--live) !important; }

/* Fiche photo / vidéo */
.item-view .media { flex: 1; min-height: 0; display: grid; place-items: center; background: #000; }
.item-view .media > * { max-width: 100%; max-height: 100%; display: block; }
.item-actions { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 8px; padding: 12px 12px max(12px, env(safe-area-inset-bottom)); background: var(--bg); }
.item-actions .btn { flex-direction: column; gap: 4px; min-height: 64px; padding: 0 6px; font-size: .85rem; }
.item-actions svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.item-actions .del { color: var(--live); }

/* Éditeur de zones */
.zone-frame { position: relative; width: min(100%, calc(55vh * var(--ar, 1.33))); margin: 0 auto; border-radius: 12px; overflow: hidden; background: #000; }
.zone-frame canvas { width: 100%; height: 100%; display: block; object-fit: contain; }
.zone-grid { position: absolute; inset: 0; display: grid; touch-action: none; cursor: crosshair; }
.zone-grid span { border: 1px solid #ffffff2e; }
.zone-grid span.off { background: repeating-linear-gradient(45deg, #000b 0 6px, #0008 6px 12px); }

/* Photo | Vidéo */
.actionbar { flex-direction: column; gap: 10px; }
.controls-row { display: flex; align-items: center; justify-content: center; gap: clamp(6px, 3vw, 14px); flex-wrap: nowrap; }
.controls-row .icon-btn:not(.big) { width: 48px; height: 48px; }
.mode-switch { display: flex; justify-content: center; gap: 4px; }
.mode-switch button { min-height: 32px; padding: 0 14px; border: 0; border-radius: 999px; background: none; color: #fffb; font-size: .85rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; }
.mode-switch button[aria-pressed="true"] { background: #fff3; color: #ffd23f; }
.mode-switch button:disabled { opacity: .4; }
.shoot[data-mode="video"] svg { display: none; }
.shoot[data-mode="video"]::after { content: ""; width: 30px; height: 30px; border-radius: 50%; background: var(--live); transition: border-radius .2s var(--ease), transform .2s var(--ease); }
.shoot.recording::after { border-radius: 6px; transform: scale(.8); }
.rec-timer { position: absolute; top: max(70px, calc(env(safe-area-inset-top) + 58px)); left: 50%; transform: translateX(-50%); padding: 6px 14px; border-radius: 999px; background: var(--live); color: #fff; font-weight: 800; font-variant-numeric: tabular-nums; z-index: 1; }
.settings-sheet .row { flex-wrap: wrap; }

/* Planning */
.sched { display: grid; gap: 10px; padding-bottom: 6px; }
.sched[hidden] { display: none; }
.times { justify-content: flex-start; gap: 16px; }
.times label { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.times input { min-height: 44px; padding: 0 10px; border-radius: 10px; border: 1px solid var(--surface-2); background: var(--bg); color: var(--text); font: inherit; font-variant-numeric: tabular-nums; }
.days { display: flex; gap: 6px; }
.days button { flex: 1; min-height: 44px; border-radius: 10px; border: 1px solid var(--surface-2); background: none; color: var(--muted); font-weight: 700; }
.days button[aria-pressed="true"] { background: var(--accent); border-color: transparent; color: #fff; }
.armed-status { color: var(--ok); font-weight: 600; }

/* Écran noir de la caméra : pixels éteints sur AMOLED */
.blackout { position: fixed; inset: 0; background: #000; z-index: 10; cursor: none; }


/* Vignettes des autres caméras (visionneuse) */
.pips { position: absolute; top: calc(max(12px, env(safe-area-inset-top)) + 118px); right: 12px; display: grid; gap: 8px; z-index: 1; }
.pip { position: relative; width: min(34vw, 200px); aspect-ratio: 16 / 9; padding: 0; border: 2px solid #ffffff99; border-radius: 12px; overflow: hidden; background: #000; box-shadow: 0 4px 16px #0008; }
.pip video { width: 100%; height: 100%; object-fit: cover; display: block; }
.pip-name { position: absolute; left: 6px; bottom: 4px; max-width: calc(100% - 12px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: #fff; font-size: .75rem; font-weight: 600; text-shadow: 0 1px 3px #000; }
.pip.off video { opacity: .25; }
.pip.off::after { content: "Hors ligne"; position: absolute; inset: 0; display: grid; place-items: center; color: #fff; font-size: .8rem; }
.invite-sheet { justify-items: center; text-align: center; }
.invite-sheet .sheet-head, .invite-sheet .sheet-actions { width: 100%; }
.invite-qr { display: grid; place-items: center; }
.invite-qr .spinner { border-color: #0002; border-top-color: #333; }

/* Onglets des caméras (visionneuse) : 2e ligne du bandeau du haut, masquée avec les autres contrôles */
.hud-spacer { flex: 1; }
.cam-tabs { flex-basis: 100%; display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; }
.cam-tab { flex: none; display: inline-flex; align-items: center; gap: 8px; min-height: 40px; max-width: 45vw; padding: 0 14px; border-radius: 999px; border: 0; background: #ffffff26; color: #fff; font-weight: 600; backdrop-filter: blur(12px); }
.cam-tab .n { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cam-tab .dot { width: 8px; height: 8px; border-radius: 50%; background: #fff8; flex: none; }
.cam-tab[data-status="live"] .dot { background: var(--ok); }
.cam-tab[data-status="error"] .dot { background: var(--live); }
.cam-tab[aria-current="true"] { background: #fff; color: #12151a; }
.cam-tab.add { background: none; border: 1px dashed #ffffff80; }
.pip[hidden] { display: none; }
