:root {
  --bg: #f4f2ee;
  --panel: #ffffff;
  --ink: #1d1d1b;
  --muted: #76736c;
  --line: #e6e2da;
  --brand: #1f6f4a;
  --brand-ink: #ffffff;
  --brand-soft: #e3f1e9;
  --in: #ffffff;
  --out-bot: #dff3e6;
  --out-human: #e4ecfb;
  --warn: #c77a12;
  --warn-soft: #fbf0dc;
  --danger: #b3261e;
  --danger-soft: #fbe5e3;
  --ok: #1f7a4a;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.04);
  font-family: "Inter", system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141513; --panel: #1d1f1c; --ink: #ecebe6; --muted: #9b998f; --line: #2d2f2b;
    --brand: #4fb07f; --brand-ink: #0e1a13; --brand-soft: #1f3529;
    --in: #262825; --out-bot: #1f3529; --out-human: #1f2a3d;
    --warn: #e5a54a; --warn-soft: #3a2e19; --danger: #ef7d74; --danger-soft: #3d201d; --ok: #5cc48f;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink); }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }

/* ---------- barre du haut ---------- */
.top {
  height: 56px; display: flex; align-items: center; gap: 12px; padding: 0 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.top h1 { font-size: 16px; margin: 0; font-weight: 650; letter-spacing: -.01em; }
.top .spacer { flex: 1; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border-radius: 999px;
  font-size: 12.5px; font-weight: 550; border: 1px solid var(--line); background: var(--panel);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot.ok { background: var(--ok); } .dot.warn { background: var(--warn); } .dot.bad { background: var(--danger); }
.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 9px; padding: 7px 12px;
  font-size: 13.5px; font-weight: 550;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ---------- mise en page ---------- */
.layout { display: grid; grid-template-columns: 340px 1fr 300px; height: calc(100% - 56px); }
.col { min-height: 0; min-width: 0; display: flex; flex-direction: column; }
.composer textarea { min-width: 0; }
.list-col { border-right: 1px solid var(--line); background: var(--panel); }
.side-col { border-left: 1px solid var(--line); background: var(--panel); overflow-y: auto; }

.filters { display: flex; gap: 6px; padding: 12px; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.filters button {
  border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 4px 10px; font-size: 12.5px;
}
.filters button.active { background: var(--ink); color: var(--panel); border-color: var(--ink); }
.filters .count { opacity: .7; margin-left: 3px; }
.search { margin: 0 12px 10px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 9px; background: var(--bg); }

.contacts { overflow-y: auto; flex: 1; }
.contact {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; padding: 12px 14px; border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.contact:hover { background: var(--bg); }
.contact.selected { background: var(--brand-soft); }
.contact .name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.contact .preview { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.contact .meta { display: flex; gap: 5px; align-items: center; justify-content: flex-end; }
.badge { font-size: 11px; font-weight: 600; padding: 2px 7px; border-radius: 999px; background: var(--bg); color: var(--muted); white-space: nowrap; }
.badge.unread { background: var(--brand); color: var(--brand-ink); }
.badge.human { background: var(--out-human); color: var(--ink); }
.badge.alert { background: var(--danger-soft); color: var(--danger); }
.badge.rdv { background: var(--brand-soft); color: var(--ok); }
.timer { font-variant-numeric: tabular-nums; font-size: 11.5px; font-weight: 600; }
.timer.ok { color: var(--ok); } .timer.warn { color: var(--warn); } .timer.bad { color: var(--danger); } .timer.closed { color: var(--muted); }

/* ---------- conversation ---------- */
.chat-col { background: var(--bg); }
.chat-head { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); min-height: 56px; }
.chat-head .who { font-weight: 650; }
.chat-head .phone { color: var(--muted); font-size: 13px; }
.back { display: none; }
.window-banner { padding: 8px 16px; font-size: 13px; font-weight: 550; display: flex; gap: 8px; align-items: center; }
.window-banner.ok { background: var(--brand-soft); color: var(--ok); }
.window-banner.warn { background: var(--warn-soft); color: var(--warn); }
.window-banner.bad, .window-banner.closed { background: var(--danger-soft); color: var(--danger); }
.messages { flex: 1; overflow-y: auto; padding: 18px 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: min(72%, 560px); padding: 8px 11px; border-radius: 12px; box-shadow: var(--shadow); white-space: pre-wrap; word-wrap: break-word; font-size: 14.5px; line-height: 1.4; }
.msg.in { align-self: flex-start; background: var(--in); border-top-left-radius: 4px; }
.msg.out { align-self: flex-end; border-top-right-radius: 4px; background: var(--out-human); }
.msg.out.bot { background: var(--out-bot); }
.msg .info { font-size: 11px; color: var(--muted); margin-top: 4px; text-align: right; }
.msg a { color: var(--brand); }
.day { align-self: center; font-size: 11.5px; color: var(--muted); background: var(--panel); padding: 3px 10px; border-radius: 999px; margin: 8px 0; }
.composer { display: flex; gap: 8px; padding: 10px 12px; background: var(--panel); border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; min-height: 42px; max-height: 160px; background: var(--bg); }
.empty { margin: auto; color: var(--muted); text-align: center; padding: 24px; }

/* ---------- panneau de droite ---------- */
.side { padding: 16px; display: flex; flex-direction: column; gap: 18px; }
.side h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 8px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: transparent; padding: 8px; font-size: 13px; font-weight: 600; }
.seg button.active { background: var(--brand); color: var(--brand-ink); }
.side select, .side textarea { width: 100%; border: 1px solid var(--line); border-radius: 9px; padding: 8px; background: var(--bg); }
.side textarea { min-height: 110px; resize: vertical; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 10px; font-size: 13px; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; text-align: right; }
.hint { font-size: 12.5px; color: var(--muted); line-height: 1.45; }

/* ---------- fenêtres modales ---------- */
dialog { border: 0; border-radius: 16px; padding: 0; width: min(720px, calc(100vw - 32px)); background: var(--panel); color: var(--ink); box-shadow: 0 20px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(0,0,0,.35); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head h2 { font-size: 17px; margin: 0; }
.modal-body { padding: 18px 20px; max-height: 70vh; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; }
.field input, .field textarea { width: 100%; border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; background: var(--bg); }
.field textarea { min-height: 90px; resize: vertical; }
.relance { display: grid; grid-template-columns: 90px 1fr auto; gap: 8px; align-items: start; }
.qr { display: grid; place-items: center; min-height: 280px; }
.qr img { width: 264px; height: 264px; image-rendering: pixelated; background: #fff; padding: 8px; border-radius: 12px; }
.error { color: var(--danger); font-size: 13px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--ink); color: var(--panel); padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 50; }

/* ---------- connexion ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login { width: min(380px, 100%); background: var(--panel); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; }
.login h1 { margin: 0; font-size: 20px; }
.login input { width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; background: var(--bg); }

/* ---------- mobile ---------- */
@media (max-width: 1100px) { .layout { grid-template-columns: 320px 1fr; } .side-col { display: none; } .side-col.open { display: block; position: fixed; right: 0; top: 56px; bottom: 0; width: min(320px, 100%); z-index: 20; box-shadow: var(--shadow); } }
@media (max-width: 760px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .chat-col { display: none; }
  body.chat-open .list-col { display: none; }
  body.chat-open .chat-col { display: flex; }
  .back { display: inline-block; }
  .top { gap: 8px; padding: 0 10px; }
  .top h1, .top .hide-sm { display: none; }
  .top .btn { padding: 6px 9px; font-size: 13px; }
  .chat-head { padding: 8px 10px; gap: 6px; }
  .chat-head > div:nth-child(2) { min-width: 0; }
  .chat-head .phone { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .chat-head .btn { padding: 6px 8px; font-size: 12.5px; white-space: nowrap; }
  .msg { max-width: 88%; }
}

/* ================= v2 : étapes CRM, brouillons, pipeline ================= */
.stage { font-size: 11px; font-weight: 650; padding: 2px 8px; border-radius: 999px; white-space: nowrap; background: var(--bg); color: var(--muted); }
.s-a_valider { background: #fde8c8; color: #8a4b00; }
.s-nouveau { background: #e8e4ff; color: #4b3aa8; }
.s-vsl { background: var(--brand-soft); color: var(--ok); }
.s-a_relancer { background: #ffe3d6; color: #a2410f; }
.s-rdv { background: #d8ecff; color: #0f5a9c; }
.s-client { background: #1f6f4a; color: #fff; }
.s-ferme { background: #ecebe7; color: #6f6c64; }
.s-stop { background: var(--danger-soft); color: var(--danger); }
.s-perdu { background: #ecebe7; color: #8b877e; text-decoration: line-through; }
@media (prefers-color-scheme: dark) {
  .s-a_valider { background: #4a3313; color: #f7c27a; } .s-nouveau { background: #2d2752; color: #c1b7ff; }
  .s-a_relancer { background: #4a2617; color: #ffb08a; } .s-rdv { background: #17324d; color: #8ec5ff; }
  .s-ferme, .s-perdu { background: #2d2f2b; color: #9b998f; }
}
.filters button.f-a_valider .count { font-weight: 700; }
.filters button.f-a_valider:not(.active) { border-color: #f0b660; }

.draft-box { margin: 0 12px 8px; background: var(--panel); border: 1.5px dashed #e3a548; border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; box-shadow: var(--shadow); }
.draft-head { display: flex; justify-content: space-between; gap: 8px; font-size: 12.5px; font-weight: 600; color: #8a4b00; }
@media (prefers-color-scheme: dark) { .draft-head { color: #f7c27a; } }
.draft-text { white-space: pre-wrap; font-size: 14.5px; line-height: 1.4; max-height: 30vh; overflow-y: auto; }
.draft-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--line); background: var(--bg); border-radius: 999px; padding: 3px 10px; font-size: 12px; }
.chip:hover { border-color: var(--muted); }
.draft-cta { display: flex; gap: 8px; padding: 0 12px 8px; flex-wrap: wrap; }
.loading { opacity: .55; pointer-events: none; position: relative; }
.loading::after { content: 'L’IA rédige…'; position: absolute; inset: 0; display: grid; place-items: center; font-weight: 600; }

.status-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.status-grid button { border: 1px solid var(--line); background: var(--bg); border-radius: 9px; padding: 7px 6px; font-size: 12.5px; font-weight: 600; }
.status-grid button.active { background: var(--ink); color: var(--panel); border-color: var(--ink); }

.board { display: none; height: calc(100% - 56px); overflow-x: auto; padding: 14px; gap: 12px; }
body.pipeline-on .board { display: flex; }
body.pipeline-on .layout { display: none; }
.board-col { flex: 0 0 280px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; display: flex; flex-direction: column; min-height: 0; }
.board-col header { display: flex; justify-content: space-between; align-items: center; padding: 12px 12px 4px; }
.board-col header .count { font-weight: 700; color: var(--muted); }
.board-col > .hint { padding: 0 12px 8px; margin: 0; border-bottom: 1px solid var(--line); }
.board-items { overflow-y: auto; flex: 1; }
.board-items .contact { grid-template-columns: 1fr auto; }

.examples { display: flex; flex-direction: column; gap: 8px; max-height: 360px; overflow-y: auto; }
.example { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-size: 13px; display: flex; flex-direction: column; gap: 4px; }
.example.off { opacity: .5; }
.example .was { text-decoration: line-through; text-decoration-color: rgba(179,38,30,.5); }
.ex-actions { display: flex; gap: 6px; justify-content: flex-end; }
.ex-actions .btn { padding: 4px 9px; font-size: 12px; }
.modal-body .seg { max-width: 460px; }

/* Application installée : respecter l'encoche et la barre du bas de l'iPhone */
.top { padding-top: env(safe-area-inset-top); height: calc(56px + env(safe-area-inset-top)); }
.layout, .board { height: calc(100% - 56px - env(safe-area-inset-top)); }
.composer { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
@media (max-width: 760px) {
  .draft-box { margin: 0 8px 6px; }
  .draft-actions .btn { flex: 1; padding: 8px 6px; }
  .filters { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .filters button { flex: 0 0 auto; }
  .status-grid { grid-template-columns: 1fr; }
  .composer textarea { font-size: 16px; } /* évite le zoom automatique sur iPhone */
}
.pill { white-space: nowrap; }
@media (max-width: 760px) { .pill { font-size: 12px; padding: 5px 9px; } }
.s-formation { background: #f3e5ff; color: #7a2fa8; }
@media (prefers-color-scheme: dark) { .s-formation { background: #3a2450; color: #dcb3ff; } }
.badge.src { background: transparent; padding: 0 2px; font-size: 13px; }
.settings-section { margin: 10px 0 0; font-size: 14px; border-top: 1px solid var(--line); padding-top: 14px; }
