:root {
  --bg: #f4f4f6;
  --panel: #ffffff;
  --text: #121317;
  --muted: #6e717b;
  --line: rgba(18, 19, 23, 0.09);
  --accent: #e5402a;          /* rouge « à l'antenne » */
  --accent-text: #ffffff;
  --mine: #e5402a;
  --mine-text: #ffffff;
  --theirs: #ffffff;
  --theirs-text: #121317;
  --ok: #1f9d55;
  --danger: #d0302f;
  --radius: 18px;
  --top: env(safe-area-inset-top);
  --bottom: env(safe-area-inset-bottom);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #181b21;
    --text: #eceef2;
    --muted: #8c909b;
    --line: rgba(255, 255, 255, 0.08);
    --theirs: #22262e;
    --theirs-text: #eceef2;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--danger); font-size: 14px; min-height: 1em; margin: 0; }

.view {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  height: 100dvh;
  background: var(--bg);
}
.scroll { overflow-y: auto; -webkit-overflow-scrolling: touch; flex: 1; }

/* Barre du haut */
.bar {
  display: grid; grid-template-columns: 44px 1fr 44px; align-items: center;
  padding: calc(var(--top) + 6px) 8px 6px;
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(1.8) blur(18px);
  -webkit-backdrop-filter: saturate(1.8) blur(18px);
  border-bottom: 0.5px solid var(--line);
  z-index: 2;
}
.bar h1 { font-size: 17px; font-weight: 600; text-align: center; margin: 0; }
.icon-btn {
  width: 44px; height: 44px; display: grid; place-items: center;
  border: 0; background: none; color: var(--accent); border-radius: 12px;
}
.icon-btn:active { opacity: .5; }
.title-btn { border: 0; background: none; display: flex; flex-direction: column; align-items: center; min-width: 0; padding: 0; }
.title-btn span { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.title-btn small { font-size: 12px; }

.search { padding: 8px 16px; }
.search input {
  width: 100%; border: 0; border-radius: 10px; padding: 8px 12px;
  background: color-mix(in srgb, var(--muted) 14%, transparent); font-size: 16px; outline: none;
}

/* Listes */
.list { list-style: none; margin: 0; padding: 0 0 calc(var(--bottom) + 12px); }
.list li { display: flex; align-items: center; gap: 12px; padding: 10px 16px; }
.list li + li .row-main { border-top: 0.5px solid var(--line); }
.list li:active { background: color-mix(in srgb, var(--muted) 12%, transparent); }
.row-main { flex: 1; min-width: 0; padding: 4px 0; }
.list li + li .row-main { margin-top: -10px; padding-top: 14px; }
.row-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.row-top b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.row-top time { font-size: 13px; color: var(--muted); flex-shrink: 0; }
.row-sub { display: flex; justify-content: space-between; gap: 8px; align-items: center; color: var(--muted); font-size: 14px; }
.row-sub span { overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.badge {
  min-width: 22px; height: 22px; padding: 0 7px; border-radius: 11px;
  background: var(--accent); color: var(--accent-text); font-size: 13px; font-weight: 600;
  display: grid; place-items: center; flex-shrink: 0;
}
.unread .row-top time { color: var(--accent); }
.empty { text-align: center; color: var(--muted); padding: 40px 24px; }

.avatar {
  width: 48px; height: 48px; border-radius: 50%; flex-shrink: 0;
  display: grid; place-items: center; font-weight: 600; font-size: 17px; color: #fff;
  position: relative;
}
.avatar.sm { width: 36px; height: 36px; font-size: 14px; }
.avatar.online::after {
  content: ""; position: absolute; right: 1px; bottom: 1px; width: 12px; height: 12px;
  border-radius: 50%; background: var(--ok); border: 2px solid var(--bg);
}
.avatar.group svg { width: 24px; height: 24px; }

/* Messages */
.msgs { padding: 12px 10px 8px; display: flex; flex-direction: column; gap: 2px; }
.day { align-self: center; font-size: 12px; color: var(--muted); margin: 14px 0 8px; font-weight: 500; }
.msg { max-width: 78%; display: flex; flex-direction: column; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; }
.msg + .msg.gap { margin-top: 8px; }
.author { font-size: 12px; color: var(--muted); margin: 0 12px 2px; }
.bubble {
  padding: 8px 12px; border-radius: var(--radius);
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word;
}
.mine .bubble { background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 6px; }
.theirs .bubble { background: var(--theirs); color: var(--theirs-text); border-bottom-left-radius: 6px; box-shadow: 0 0.5px 0 var(--line); }
.bubble a { color: inherit; }
.bubble.media { padding: 3px; overflow: hidden; }
.bubble.media img, .bubble.media video { display: block; max-width: 100%; max-height: 340px; border-radius: calc(var(--radius) - 3px); }
.bubble.media .cap { padding: 6px 9px 5px; }
.file-link { display: flex; gap: 10px; align-items: center; text-decoration: none; padding: 2px 0; }
.file-link svg { flex-shrink: 0; }
.file-link small { display: block; opacity: .75; }
.meta { font-size: 11px; color: var(--muted); margin: 2px 8px 0; }
.msg.sending { opacity: .55; }
.msg.failed .meta { color: var(--danger); }

/* Saisie */
.composer {
  display: flex; align-items: flex-end; gap: 6px;
  padding: 6px 8px calc(var(--bottom) + 6px);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(1.8) blur(18px);
  -webkit-backdrop-filter: saturate(1.8) blur(18px);
  border-top: 0.5px solid var(--line);
}
.attach { flex-shrink: 0; }
.field {
  flex: 1; background: var(--panel); border: 0.5px solid var(--line); border-radius: 20px;
  display: flex; flex-direction: column; min-width: 0;
}
.field textarea {
  border: 0; background: none; resize: none; outline: none;
  padding: 9px 14px; max-height: 140px; font-size: 16px; line-height: 1.35;
}
.pending { display: flex; gap: 8px; align-items: center; padding: 8px 10px 0; font-size: 14px; }
.pending img { width: 56px; height: 56px; object-fit: cover; border-radius: 10px; }
.pending button { margin-left: auto; border: 0; background: none; color: var(--muted); font-size: 20px; }
.send {
  width: 38px; height: 38px; border-radius: 50%; border: 0; flex-shrink: 0; margin-bottom: 2px;
  background: var(--accent); color: var(--accent-text); display: grid; place-items: center;
}
.send svg { width: 20px; height: 20px; stroke-width: 2.6; }
.send:disabled { opacity: .35; }

/* Connexion */
.auth { justify-content: center; align-items: center; padding: 24px; }
.auth-box { width: 100%; max-width: 360px; text-align: center; }
.auth-logo { width: 76px; height: 76px; border-radius: 18px; }
.auth h1 { margin: 12px 0 2px; font-size: 28px; }
.auth form { display: flex; flex-direction: column; gap: 12px; margin-top: 24px; text-align: left; }
.auth label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
input:not([type=checkbox]):not([type=search]) {
  border: 0.5px solid var(--line); background: var(--panel); border-radius: 12px;
  padding: 12px 14px; font-size: 16px; outline: none; color: var(--text);
}
input:focus { border-color: var(--accent); }
.btn {
  border: 0; border-radius: 12px; padding: 13px 16px; font-weight: 600;
  background: var(--panel); color: var(--accent); border: 0.5px solid var(--line);
}
.btn.primary { background: var(--accent); color: var(--accent-text); border: 0; }
.btn.danger { color: var(--danger); width: calc(100% - 32px); margin: 28px 16px; }
.btn:disabled { opacity: .5; }
.link { border: 0; background: none; color: var(--accent); padding: 8px; font-size: 17px; }
.link.strong { font-weight: 600; }
.link:disabled { opacity: .35; }

/* Feuilles */
dialog { border: 0; padding: 0; color: var(--text); }
.sheet {
  width: 100%; max-width: 640px; height: calc(100dvh - var(--top) - 12px); max-height: none;
  margin: auto auto 0; border-radius: 14px 14px 0 0; background: var(--bg);
  display: none; flex-direction: column;
}
.sheet[open] { display: flex; animation: up .28s cubic-bezier(.2, .8, .2, 1); }
.sheet::backdrop { background: rgba(0, 0, 0, .35); }
@keyframes up { from { transform: translateY(100%); } }
.sheet-bar { display: grid; grid-template-columns: 90px 1fr 90px; align-items: center; padding: 10px 6px; }
.sheet-bar h2 { font-size: 17px; text-align: center; margin: 0; }
.sheet-bar > :last-child { justify-self: end; }
.sheet-body { padding-bottom: calc(var(--bottom) + 20px); }
.sheet h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 500; margin: 24px 16px 6px; }
.sheet-field { display: flex; flex-direction: column; gap: 4px; padding: 0 16px 10px; font-size: 13px; color: var(--muted); }
.seg { display: flex; margin: 4px 16px 12px; padding: 2px; border-radius: 10px; background: color-mix(in srgb, var(--muted) 16%, transparent); }
.seg button { flex: 1; border: 0; background: none; padding: 7px; border-radius: 8px; font-size: 14px; font-weight: 500; }
.seg button.on { background: var(--panel); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.list .check-mark { width: 24px; height: 24px; border-radius: 50%; border: 1.5px solid var(--muted); flex-shrink: 0; display: grid; place-items: center; }
.list li.sel .check-mark { background: var(--accent); border-color: var(--accent); }
.list li.sel .check-mark::after { content: "✓"; color: #fff; font-size: 14px; font-weight: 700; }
.list.inset { background: var(--panel); margin: 0 16px; border-radius: 12px; padding: 0; }
.list.inset li { padding: 8px 12px; }
.list.inset li .row-main { padding: 2px 0; }
.tag { font-size: 11px; color: var(--muted); border: 0.5px solid var(--line); padding: 1px 6px; border-radius: 6px; margin-left: 6px; font-weight: 500; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.inset-form { padding: 0 16px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 15px; }
.me-card { display: flex; flex-direction: column; align-items: center; margin: 8px 0 0; font-size: 18px; }
.me-card span { font-size: 14px; }

/* Visionneuse */
.viewer { background: #000; width: 100vw; height: 100dvh; max-width: none; max-height: none; margin: 0; }
.viewer[open] { display: grid; place-items: center; }
.viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.viewer .link { position: absolute; top: calc(var(--top) + 8px); right: 12px; color: #fff; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--bottom) + 80px); transform: translate(-50%, 20px);
  background: rgba(20, 20, 24, .92); color: #fff; padding: 10px 16px; border-radius: 12px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; max-width: 86vw; text-align: center; z-index: 99;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.install-tip {
  margin: 0 12px calc(var(--bottom) + 10px); padding: 12px 14px; border-radius: 12px;
  background: var(--panel); border: 0.5px solid var(--line); font-size: 14px;
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
}
.install-tip .link { margin-left: auto; padding: 0 4px; font-size: 15px; }

/* Grand écran (Mac, iPad) */
@media (min-width: 900px) {
  #v-list { right: auto; width: 360px; border-right: 0.5px solid var(--line); }
  #v-chat { left: 360px; }
  #v-chat .back { visibility: hidden; }
}
