/* WA Desk — palette propre, pas de dégradé violet générique */
:root {
  --ink: #0c0f12;
  --ink-soft: #1a2129;
  --paper: #f6f4ef;
  --paper-elevated: #ffffff;
  --line: #e4dfd4;
  --muted: #6b7280;
  --wa: #128c7e;
  --wa-bright: #25d366;
  --wa-dim: rgba(18, 140, 126, 0.12);
  --coral: #e85d4c;
  --amber: #d97706;
  --sky: #0ea5e9;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(12, 15, 18, 0.06), 0 12px 40px rgba(12, 15, 18, 0.08);
  --font: 'Figtree', system-ui, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
  --sidebar-w: 248px;
}

[data-theme='dark'] {
  --paper: #141a21;
  --paper-elevated: #1c242d;
  --line: #2a3540;
  --muted: #94a3b8;
  --ink: #f1f5f9;
  --ink-soft: #cbd5e1;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
}

*, *::before, *::after { box-sizing: border-box; }

html {
  font-size: 15px;
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--paper);
  color: var(--ink);
  height: 100%;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: minmax(0, 1fr);
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 100; transform: translateX(-100%); transition: transform 0.25s ease; }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 99; }
  .sidebar-backdrop.show { display: block; }
}

/* Sidebar */
.sidebar {
  background: var(--ink);
  color: #e8edf2;
  padding: 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  border-right: 1px solid rgba(255,255,255,.06);
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0 0.5rem;
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  background: linear-gradient(145deg, var(--wa-bright), var(--wa));
  display: grid;
  place-items: center;
  font-size: 1.1rem;
  color: #fff;
  box-shadow: 0 4px 14px rgba(37, 211, 102, 0.35);
}

.brand h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

.brand span {
  font-size: 0.7rem;
  font-weight: 500;
  color: #8b9aab;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }

.nav-btn {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: #a8b4c0;
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, color 0.15s;
}

.nav-btn:hover { background: rgba(255,255,255,.06); color: #fff; }

.nav-btn.active {
  background: rgba(37, 211, 102, 0.15);
  color: var(--wa-bright);
}

.nav-btn .count {
  margin-left: auto;
  font-size: 0.7rem;
  font-weight: 700;
  background: var(--coral);
  color: #fff;
  padding: 2px 7px;
  border-radius: 999px;
  min-width: 20px;
  text-align: center;
}

.nav-btn .count.hidden { display: none; }

.sidebar-foot {
  padding: 0.75rem 0.5rem 0;
  border-top: 1px solid rgba(255,255,255,.08);
  font-size: 0.75rem;
  color: #6b7a8a;
}

.live-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(255,255,255,.06);
  margin-bottom: 0.5rem;
}

.live-pill .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #64748b;
}

.live-pill.online .dot {
  background: var(--wa-bright);
  box-shadow: 0 0 0 3px rgba(37, 211, 102, 0.25);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* Main */
.main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  height: 100%;
  max-height: 100dvh;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.5rem;
  background: var(--paper-elevated);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 100;
}

.topbar h2 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}

.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.content {
  flex: 1 1 0;
  min-height: 0;
  min-width: 0;
  padding: 1rem 1.5rem;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.panel {
  display: none;
  animation: fadeIn 0.2s ease;
  min-height: 0;
}

.panel.active {
  display: flex;
  flex-direction: column;
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
}

#panel-lines.panel.active,
#panel-send.panel.active {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#panel-inbox.panel.active {
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

/* Stats strip */
.stats-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 1rem;
  flex-shrink: 0;
}

/* Boîte de réception : stats masquées, toute la hauteur pour le chat */
.content:has(#panel-inbox.active) .stats-strip {
  display: none;
}

.stat-card {
  background: var(--paper-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
}

.stat-card .label { font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; }
.stat-card .value { font-size: 1.5rem; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; }
.stat-card .value.ok { color: var(--wa); }

/* Toolbar */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 1rem;
}

.search-wrap {
  flex: 1;
  min-width: 200px;
  position: relative;
}

.search-wrap i {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 0.85rem;
}

.search-wrap input {
  width: 100%;
  padding: 0.6rem 0.85rem 0.6rem 2.25rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--paper-elevated);
  font-family: var(--font);
  font-size: 0.9rem;
  color: var(--ink);
}

.search-wrap input:focus {
  outline: none;
  border-color: var(--wa);
  box-shadow: 0 0 0 3px var(--wa-dim);
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0.55rem 1rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s, box-shadow 0.12s, background 0.12s;
}

.btn:active { transform: scale(0.98); }

.btn-primary {
  background: var(--wa);
  color: #fff;
  box-shadow: 0 2px 8px rgba(18, 140, 126, 0.35);
}

.btn-primary:hover { background: #0f7a6e; }

.btn-ghost {
  background: var(--paper-elevated);
  border-color: var(--line);
  color: var(--ink);
}

.btn-ghost:hover { border-color: var(--wa); color: var(--wa); }

.btn-danger { background: transparent; border-color: #fecaca; color: var(--coral); }
.btn-danger:hover { background: #fef2f2; }

.btn-icon {
  width: 38px;
  height: 38px;
  padding: 0;
  justify-content: center;
  border-radius: var(--radius-sm);
}

select.btn-select {
  padding: 0.55rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--paper-elevated);
  font-family: var(--font);
  font-size: 0.85rem;
  color: var(--ink);
}

/* Line cards */
.lines-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}

.line-card {
  background: var(--paper-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  transition: border-color 0.2s, transform 0.2s;
}

.line-card:hover { transform: translateY(-2px); }

.line-card.connected { border-color: var(--wa); border-width: 2px; }
.line-card.qr_ready { border-color: var(--amber); }
.line-card.error { border-color: var(--coral); }

.line-card-top {
  padding: 1.1rem 1.15rem;
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.line-avatar {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: var(--wa-dim);
  color: var(--wa);
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 1rem;
  flex-shrink: 0;
}

.line-card h3 { margin: 0 0 4px; font-size: 1rem; font-weight: 700; }
.line-card .meta { font-size: 0.78rem; color: var(--muted); font-family: var(--mono); }

.status-tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 3px 8px;
  border-radius: 6px;
  margin-top: 6px;
}

.status-tag.connected { background: var(--wa-dim); color: var(--wa); }
.status-tag.qr_ready { background: #fef3c7; color: var(--amber); }
.status-tag.disconnected { background: #f1f5f9; color: var(--muted); }
.status-tag.initializing { background: #e0f2fe; color: var(--sky); }
.status-tag.error { background: #fee2e2; color: var(--coral); }

.line-card-actions {
  padding: 0 1.15rem 1.15rem;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

/* Inbox — barre filtres fixe, seuls les messages défilent */
.inbox-sticky-header {
  flex-shrink: 0;
  z-index: 50;
  background: var(--paper);
  padding-bottom: 0.5rem;
  margin-bottom: 0.5rem;
}

#panel-inbox .toolbar {
  margin-bottom: 0.5rem;
}

#panel-inbox #inboxCount {
  margin: 0 0 0.5rem 4px !important;
}

/* Inbox split — hauteur = écran restant, scroll uniquement dans les colonnes */
.inbox-layout {
  display: grid;
  grid-template-columns: minmax(260px, 320px) 1fr;
  grid-template-rows: minmax(0, 1fr);
  align-items: stretch;
  gap: 0;
  background: var(--paper-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  flex: 1 1 0;
  min-height: 0;
  height: 0;
  box-shadow: var(--shadow);
}

.inbox-layout > .inbox-list,
.inbox-layout > .inbox-thread {
  min-height: 0;
  max-height: 100%;
  overflow: hidden;
}

@media (max-width: 768px) {
  .inbox-layout { grid-template-columns: 1fr; }
  .inbox-thread { display: none; }
  .inbox-layout.thread-open .inbox-list { display: none; }
  .inbox-layout.thread-open .inbox-thread { display: flex; }
}

.inbox-list {
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.inbox-list-head {
  padding: 12px;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  flex-shrink: 0;
  background: var(--paper-elevated);
}

#convList {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.conv-item {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  transition: background 0.12s;
}

.conv-item:hover, .conv-item.active { background: var(--wa-dim); }

.conv-item.unread { border-left: 3px solid var(--wa-bright); }

.conv-item .row1 {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}

.conv-item .name { font-weight: 600; font-size: 0.9rem; }
.conv-item .time { font-size: 0.7rem; color: var(--muted); }
.conv-item .preview {
  font-size: 0.82rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.inbox-thread {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.thread-head {
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-shrink: 0;
  background: var(--paper-elevated);
  z-index: 10;
}

.thread-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 24px,
    rgba(18, 140, 126, 0.03) 24px,
    rgba(18, 140, 126, 0.03) 25px
  );
}

.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 0.9rem;
  line-height: 1.45;
  position: relative;
}

.bubble.in { align-self: flex-start; background: var(--paper-elevated); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.bubble.out { align-self: flex-end; background: var(--wa-dim); border: 1px solid rgba(18, 140, 126, 0.2); border-bottom-right-radius: 4px; }

.bubble .time { font-size: 0.68rem; color: var(--muted); margin-top: 6px; }

.bubble .msg-media {
  max-width: 100%;
  max-height: 280px;
  border-radius: 10px;
  margin-bottom: 8px;
  display: block;
}

.bubble .msg-audio { width: 100%; margin-bottom: 8px; }

.bubble .msg-sender {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--wa);
  margin-bottom: 4px;
}

.bubble.status { border-style: dashed; opacity: 0.95; }

.inbox-banner {
  background: #fff8e6;
  border: 1px solid #f0d78c;
  color: #744210;
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  font-size: 0.88rem;
  line-height: 1.45;
}

[data-theme='dark'] .inbox-banner {
  background: #2a2418;
  border-color: #5c4a1a;
  color: #fbd38d;
}

.thread-empty {
  flex: 1;
  display: grid;
  place-items: center;
  color: var(--muted);
  padding: 2rem;
  text-align: center;
}

.thread-actions { display: flex; gap: 6px; }

/* Send panel */
.send-layout {
  display: grid;
  grid-template-columns: 1fr 280px;
  gap: 20px;
  align-items: start;
}

@media (max-width: 800px) { .send-layout { grid-template-columns: 1fr; } }

.compose-card {
  background: var(--paper-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.form-group { margin-bottom: 1rem; }

.form-group label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 6px;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.95rem;
  background: var(--paper);
  color: var(--ink);
}

.form-group textarea { resize: vertical; min-height: 120px; }

.char-count { text-align: right; font-size: 0.72rem; color: var(--muted); margin-top: 4px; }

.templates-panel {
  background: var(--paper-elevated);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
}

.templates-panel h4 { margin: 0 0 10px; font-size: 0.85rem; }

.template-chip {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  margin-bottom: 6px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: transparent;
  font-family: var(--font);
  font-size: 0.82rem;
  color: var(--ink-soft);
  cursor: pointer;
}

.template-chip:hover { border-color: var(--wa); background: var(--wa-dim); }

.recent-contact {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--line);
  font-size: 0.85rem;
  cursor: pointer;
}

.recent-contact:hover { color: var(--wa); }

/* Modal QR */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(12, 15, 18, 0.55);
  backdrop-filter: blur(4px);
  z-index: 200;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-backdrop.show { display: flex; }

.modal {
  background: var(--paper-elevated);
  border-radius: var(--radius);
  padding: 1.5rem;
  max-width: 360px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow);
}

.modal img { width: 100%; max-width: 260px; border-radius: 12px; margin: 1rem 0; }

.modal h3 { margin: 0; font-size: 1.1rem; }

.modal p { color: var(--muted); font-size: 0.85rem; line-height: 1.5; }

/* Toast stack */
.toast-stack {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 360px;
}

.toast-item {
  background: var(--ink);
  color: #fff;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  box-shadow: var(--shadow);
  animation: slideIn 0.25s ease;
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.toast-item.success { border-left: 4px solid var(--wa-bright); }
.toast-item.error { border-left: 4px solid var(--coral); }

@keyframes slideIn {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: none; }
}

.empty-state {
  text-align: center;
  padding: 3rem 1.5rem;
  color: var(--muted);
}

.empty-state i { font-size: 2.5rem; margin-bottom: 1rem; opacity: 0.4; }

.menu-toggle { display: none; }

@media (max-width: 900px) {
  .menu-toggle { display: inline-flex; }
}

.kbd {
  font-family: var(--mono);
  font-size: 0.68rem;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--line);
  color: var(--muted);
}
