:root {
  --bg: #f7f7f8;
  --card: #ffffff;
  --text: #1c1f22;
  --muted: #6b7378;
  --line: #e6e8ea;
  /* Palette : déclinaisons du rouge du logo (#ca0415) */
  --brand: #ca0415;
  --primary: #c40414;
  --primary-dark: #8e0310;
  --primary-soft: #fdecee;
  --primary-line: #f3c4c9;
  --primary-ink: #ffffff;
  --accent: #f08a93;
  --ok: #8e0310;
  --warn-bg: #fdecee;
  --warn-ink: #8e0310;
  --danger: #c40414;
  --chip: #f4eced;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
a { color: inherit; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 12px 8px;
  background: var(--brand); color: #fff;
}
.topbar .icon-btn { color: #fff; }
.topbar .icon-btn:active { background: rgba(255,255,255,.15); }
.topbar h1 { flex: 1; margin: 0; font-size: 18px; font-weight: 650; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-spacer, .icon-btn { width: 40px; height: 40px; flex: none; }
.icon-btn { display: grid; place-items: center; border: 0; background: none; border-radius: 10px; cursor: pointer; color: var(--primary); }
.icon-btn:active { background: var(--chip); }

main { max-width: 720px; margin: 0 auto; padding: 16px 16px calc(var(--tabbar-h) + var(--safe-b) + 24px); outline: none; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(5, 1fr);
  height: calc(var(--tabbar-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabbar a { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; text-decoration: none; color: var(--muted); font-size: 12px; }
.tabbar a.active { color: var(--primary); font-weight: 600; }
.tabbar a.active svg { stroke-width: 2.2; }

.drawer-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 30; }
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 31; width: min(320px, 86vw);
  display: flex; flex-direction: column; background: var(--card);
  transform: translateX(-102%); transition: transform .2s ease;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 0 calc(var(--safe-b) + 12px);
}
.drawer.open { transform: none; box-shadow: 0 0 30px rgba(0,0,0,.2); }
.drawer-head { display: flex; align-items: center; gap: 10px; padding: 4px 12px 12px 20px; }
.drawer-head strong { flex: 1; font-size: 18px; }
.drawer-nav { display: flex; flex-direction: column; overflow-y: auto; }
.drawer-nav a { display: flex; align-items: center; gap: 14px; padding: 13px 20px; text-decoration: none; font-size: 17px; }
.drawer-nav a svg { color: var(--primary); flex: none; }
.drawer-head img { border-radius: 9px; }
.drawer-nav a:active, .drawer-nav a.active { background: var(--primary-soft); }
.drawer-foot { margin-top: auto; padding: 16px 20px 0; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.drawer-foot small { color: var(--muted); }

.offline { position: sticky; top: 57px; z-index: 19; padding: 8px 16px; background: var(--warn-bg); color: var(--warn-ink); font-size: 14px; text-align: center; }

.card { background: var(--card); border-radius: 14px; padding: 16px; margin-bottom: 16px; border: 1px solid var(--line); }
.card h2 { margin: 0 0 8px; font-size: 18px; }
.card p { margin: 6px 0; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.warn { background: var(--warn-bg); color: var(--warn-ink); border-radius: 12px; padding: 12px 14px; margin: 0 0 16px; }
.error { color: var(--danger); }

.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 14px 0 4px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); font-weight: 650; text-decoration: none; cursor: pointer;
}
.btn svg { width: 20px; height: 20px; }
.btn:not(.primary) svg { color: var(--primary); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; }
.link-btn { border: 0; background: none; color: var(--primary); text-decoration: underline; cursor: pointer; padding: 0; text-align: left; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0 0; }
.stat { background: var(--bg); border-radius: 10px; padding: 8px 10px; }
.stat b { display: block; font-size: 22px; }
.stat span { font-size: 12px; color: var(--muted); }

.field { display: block; margin: 0 0 12px; }
.field > span { display: block; font-size: 14px; font-weight: 600; margin-bottom: 6px; }
.input, textarea.input, select.input {
  width: 100%; min-height: 46px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--card); font-size: 16px;
}
.search { position: relative; margin: 0 0 12px; }
.search input { width: 100%; min-height: 44px; padding: 10px 12px 10px 40px; border: 0; border-radius: 10px; background: var(--chip); font-size: 16px; }
.search svg { position: absolute; left: 11px; top: 10px; color: var(--muted); width: 22px; height: 22px; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; margin: 0 0 12px; }
.chip { padding: 6px 12px; border-radius: 999px; background: var(--chip); border: 0; cursor: pointer; font-weight: 600; font-size: 14px; text-decoration: none; }
.chip.active { background: var(--primary); color: var(--primary-ink); }

.group-head { display: flex; align-items: baseline; gap: 8px; margin: 20px 0 4px; }
.group-head h3 { margin: 0; font-size: 19px; flex: 1; }
.group-head .time { font-weight: 700; color: var(--primary); }
.group-head .count { font-size: 13px; color: var(--muted); }
.group-note { font-size: 13px; color: var(--muted); margin: 0 0 4px; }
.group-note.bad { color: var(--warn-ink); }

.list { background: var(--card); border-radius: 14px; border: 1px solid var(--line); overflow: hidden; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 12px 12px 14px; border-top: 1px solid var(--line); min-height: 60px; }
.row:first-child { border-top: 0; }
.row .main { flex: 1; min-width: 0; text-decoration: none; }
.row .name { font-weight: 650; }
.row .sub { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .chev { color: var(--primary-line); width: 20px; height: 20px; flex: none; }
.row.checked .name, .row.checked .sub { color: #9aa0a5; }
.row a.main { display: block; }

.check {
  flex: none; width: 32px; height: 32px; border-radius: 50%; border: 2px solid #c3c8cc; background: var(--card);
  display: grid; place-items: center; cursor: pointer; padding: 0;
}
.check svg { width: 18px; height: 18px; stroke: #fff; stroke-width: 3; opacity: 0; }
.check.on { background: var(--primary); border-color: var(--primary); }
.check.on svg { opacity: 1; }
.check.pending { box-shadow: 0 0 0 3px var(--accent); }
.check:disabled { cursor: default; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 8px 0; }
.kv dt { color: var(--muted); font-size: 14px; }
.kv dd { margin: 0; }

.map { position: relative; z-index: 0; isolation: isolate; height: 58vh; min-height: 320px; border-radius: 14px; overflow: hidden; border: 1px solid var(--line); margin-bottom: 16px; }
.pin { width: 30px; height: 30px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg); background: var(--primary); border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.pin.old { background: #9aa0a5; }
.pin-label { transform: rotate(45deg); color: #fff; font-size: 10px; font-weight: 700; display: grid; place-items: center; height: 100%; }

.info-item { display: flex; gap: 12px; align-items: flex-start; }
.info-item h3 { margin: 0 0 4px; font-size: 17px; }
.info-body { white-space: pre-wrap; }
.media { width: 100%; border-radius: 10px; margin-top: 10px; background: #000; }
img.media { background: none; }
audio.media { background: none; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); transform: translateX(-50%);
  z-index: 40; max-width: min(92vw, 480px); padding: 12px 16px; border-radius: 12px;
  background: #1f2428; color: #fff; font-size: 15px; box-shadow: 0 6px 20px rgba(0,0,0,.25);
}

.login { max-width: 420px; margin: 8vh auto 0; }
.login .logo { width: 120px; height: 120px; border-radius: 26px; box-shadow: 0 6px 20px rgba(142,3,16,.2); display: block; margin: 0 auto 16px; }
.login h2 { text-align: center; }

.admin-item { display: flex; gap: 8px; align-items: center; }
.admin-item .main { flex: 1; min-width: 0; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 6px; background: var(--chip); font-size: 12px; font-weight: 600; color: var(--muted); }

.spinner { margin: 40px auto; width: 32px; height: 32px; border: 3px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 900px) {
  main { padding-top: 24px; }
}
#menu-btn[hidden] { display: grid !important; visibility: hidden; }

/* Chat */
#menu-btn { position: relative; }
.menu-dot { position: absolute; top: 8px; right: 6px; width: 10px; height: 10px; border-radius: 50%; background: #fff; box-shadow: 0 0 0 2px var(--brand); }
.tab-icon { position: relative; display: inline-flex; }
.badge { position: absolute; top: -4px; right: -10px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; }
.badge.inline { position: static; display: inline-block; margin-left: 6px; vertical-align: middle; }
.chat-list { display: flex; flex-direction: column; gap: 12px; padding-bottom: 84px; }
.chat-msg { max-width: 86%; align-self: flex-start; }
.chat-msg.mine { align-self: flex-end; text-align: right; }
.chat-meta { font-size: 12px; color: var(--muted); margin: 0 4px 3px; }
.chat-meta .tag { margin-left: 6px; }
.chat-bubble { display: inline-block; text-align: left; white-space: pre-wrap; overflow-wrap: anywhere; padding: 9px 13px; border-radius: 16px 16px 16px 4px; background: var(--card); border: 1px solid var(--line); }
.chat-msg.mine .chat-bubble { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); border-radius: 16px 16px 4px 16px; }
.chat-actions { display: flex; gap: 12px; justify-content: flex-end; font-size: 13px; margin: 3px 4px 0; }
.chat-msg:not(.mine) .chat-actions { justify-content: flex-start; }
.chat-form {
  position: fixed; left: 0; right: 0; bottom: calc(var(--tabbar-h) + var(--safe-b)); z-index: 15;
  display: flex; gap: 8px; align-items: flex-end; padding: 8px 12px; background: var(--card); border-top: 1px solid var(--line);
}
.chat-form .chat-input { flex: 1; min-height: 44px; resize: none; }
.chat-form .btn { flex: none; }
@media (min-width: 760px) { .chat-form { left: 50%; right: auto; width: 720px; transform: translateX(-50%); } }

/* À l'aide ! */
.btn.help { color: var(--danger); border-color: var(--danger); }
.aide-group .group-head:first-child { margin-top: 12px; }

/* Bandeau d'accueil (écrans Aller / Retour) */
.hero { margin: -16px -16px 16px; background: var(--card); border-bottom: 1px solid var(--primary-line); }
.hero-band { height: 52px; background: linear-gradient(135deg, var(--brand), var(--primary-dark)); }
.hero-body { display: flex; align-items: flex-end; gap: 14px; padding: 0 16px 14px; }
.hero-logo { width: 88px; height: 88px; margin-top: -40px; flex: none; border-radius: 20px; border: 3px solid #fff; box-shadow: 0 4px 14px rgba(142,3,16,.25); background: #fff; }
.hero-title { font-size: 22px; font-weight: 700; line-height: 1.15; }
.hero-name { color: var(--muted); font-size: 16px; overflow-wrap: anywhere; }

/* Mode « voir comme » (admin) */
.viewas { position: sticky; top: 57px; z-index: 19; display: flex; align-items: center; gap: 10px; padding: 8px 16px; background: var(--primary-dark); color: #fff; font-size: 14px; }
.viewas svg { width: 20px; height: 20px; }
.viewas-text { flex: 1; }
.viewas .btn { min-height: 34px; padding: 4px 12px; color: var(--primary-dark); }
a.row { text-decoration: none; }
.inline-form { display: flex; gap: 8px; margin-top: 12px; }
.inline-form .input { flex: 1; min-width: 0; }
