/* ============================================================================
   PAPOTE — Style principal (mobile-first, rendu proche d'une app native)
   ============================================================================ */

* { box-sizing: border-box; }

html, body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg-app);
  color: var(--text-primary);
  transition: background-color .25s ease, color .25s ease;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
img { max-width: 100%; display: block; }

/* Empêche le zoom iOS sur les champs de formulaire */
input, textarea, select { font-size: 16px; }

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-thumb { background: rgba(128,128,128,.35); border-radius: 10px; }
::-webkit-scrollbar-track { background: transparent; }

/* ============================================================================
   ÉCRANS D'AUTHENTIFICATION
   ============================================================================ */
.auth-body {
  background: var(--brand-gradient);
  min-height: 100vh;
  display: flex;
}

.auth-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: max(24px, env(safe-area-inset-top));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
}

.auth-card {
  background: var(--bg-panel);
  border-radius: var(--radius-lg);
  padding: 36px 28px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
  animation: fadeSlideUp .4s ease;
}

@keyframes fadeSlideUp {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

.auth-logo { display: flex; justify-content: center; margin-bottom: 16px; }
.auth-logo img { width: 76px; height: 76px; border-radius: 20px; box-shadow: var(--shadow-md); }

.auth-title { text-align: center; font-size: 26px; font-weight: 700; margin: 0 0 4px; color: var(--text-primary); }
.auth-subtitle { text-align: center; color: var(--text-secondary); margin: 0 0 24px; font-size: 14px; }

.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-label { font-size: 13px; color: var(--text-secondary); display: flex; flex-direction: column; gap: 6px; font-weight: 500; }

.auth-input {
  border: 1.5px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  background: var(--bg-app);
  color: var(--text-primary);
  outline: none;
  transition: border-color .2s ease, box-shadow .2s ease;
}
.auth-input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(237,108,26,0.15); }

.auth-btn {
  margin-top: 8px;
  background: var(--brand-gradient);
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  padding: 13px;
  font-weight: 700;
  font-size: 15px;
  box-shadow: var(--shadow-sm);
  transition: transform .15s ease, box-shadow .15s ease;
}
.auth-btn:active { transform: scale(0.97); }

.auth-switch { text-align: center; margin-top: 20px; font-size: 14px; color: var(--text-secondary); }
.auth-switch a { color: var(--brand); font-weight: 600; }

.auth-alert { padding: 10px 14px; border-radius: var(--radius-sm); font-size: 13.5px; margin-bottom: 16px; }
.auth-alert-error { background: rgba(229,72,77,0.12); color: var(--danger); }
.auth-alert-success { background: rgba(76,217,100,0.12); color: #2E9E43; }

/* ============================================================================
   COQUILLE DE L'APPLICATION (layout général)
   ============================================================================ */
.app-shell {
  height: 100vh;
  height: 100dvh;
  width: 100%;
  display: flex;
  overflow: hidden;
  background: var(--bg-app);
}

/* --- Colonne de gauche : liste des discussions --- */
.sidebar {
  width: 100%;
  max-width: 100%;
  background: var(--bg-panel);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--brand);
  color: #fff;
}

.sidebar-header h1 { font-size: 20px; margin: 0; font-weight: 700; }
.sidebar-header .header-actions { display: flex; gap: 6px; }

.icon-btn {
  background: transparent;
  border: none;
  color: inherit;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color .15s ease;
}
.icon-btn:hover, .icon-btn:active { background: rgba(255,255,255,0.15); }
.icon-btn svg { width: 22px; height: 22px; }

.dark-icon-btn:hover, .dark-icon-btn:active { background: var(--bg-hover); }

.search-bar {
  margin: 10px 12px;
  background: var(--bg-app);
  border-radius: 22px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 14px;
}
.search-bar input {
  border: none;
  background: transparent;
  outline: none;
  flex: 1;
  color: var(--text-primary);
  font-size: 14.5px;
}
.search-bar svg { width: 18px; height: 18px; color: var(--text-secondary); flex-shrink: 0; }

.nav-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  background: var(--bg-panel);
}
.nav-tab {
  flex: 1;
  text-align: center;
  padding: 12px 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  transition: color .2s ease, border-color .2s ease;
}
.nav-tab.active { color: var(--brand); border-bottom-color: var(--brand); }

.conversation-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }

.conversation-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 16px;
  cursor: pointer;
  transition: background-color .12s ease;
  animation: fadeIn .3s ease;
}
.conversation-item:hover { background: var(--bg-hover); }
.conversation-item.active { background: var(--bg-active); }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.avatar {
  width: 50px; height: 50px; border-radius: 50%;
  object-fit: cover; flex-shrink: 0; background: var(--bg-hover);
}
.avatar-sm { width: 36px; height: 36px; }
.avatar-wrap { position: relative; flex-shrink: 0; }
.online-dot {
  position: absolute; bottom: 1px; right: 1px;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--success); border: 2px solid var(--bg-panel);
}

.conv-info { flex: 1; min-width: 0; }
.conv-top-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.conv-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 11.5px; color: var(--text-muted); white-space: nowrap; }
.conv-bottom-row { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-top: 2px; }
.conv-preview { font-size: 13.5px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.conv-preview.typing { color: var(--brand); font-style: italic; }

.unread-badge {
  background: var(--unread-badge);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  border-radius: 999px;
  min-width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  gap: 10px;
  padding: 40px 20px;
  text-align: center;
}

/* --- Colonne de droite : fenêtre de discussion --- */
.chat-window {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg-app);
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform .28s cubic-bezier(.4,0,.2,1);
  z-index: 30;
}
.chat-window.open { transform: translateX(0); }

.chat-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--brand);
  color: #fff;
  box-shadow: var(--shadow-sm);
  z-index: 2;
}
.chat-header .chat-user-info { flex: 1; min-width: 0; cursor: pointer; }
.chat-header .chat-user-name { font-weight: 600; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-header .chat-user-status { font-size: 12px; opacity: .85; }

.messages-area {
  flex: 1;
  overflow-y: auto;
  padding: 14px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background-image:
    radial-gradient(circle at 20% 10%, rgba(237,108,26,0.05), transparent 40%),
    radial-gradient(circle at 80% 90%, rgba(237,108,26,0.05), transparent 40%);
}

.date-separator {
  align-self: center;
  background: var(--bg-elevated);
  color: var(--text-secondary);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 10px;
  margin: 10px 0;
  box-shadow: var(--shadow-sm);
}

.msg-row { display: flex; align-items: flex-start; margin: 2px 0; animation: bubbleIn .22s ease; max-width: 100%; }
.msg-row.out { justify-content: flex-end; }
.msg-row.in { justify-content: flex-start; }

@keyframes bubbleIn {
  from { opacity: 0; transform: translateY(6px) scale(.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.bubble {
  display: block;
  width: max-content;
  max-width: 65%;
  padding: 6px 7px 8px 9px;
  border-radius: 7.5px;
  position: relative;
  box-shadow: 0 1px 0.5px rgba(0,0,0,0.13);
  font-size: 14.2px;
  line-height: 19px;
  word-wrap: break-word;
  overflow-wrap: break-word;
}
.msg-row.out .bubble { background: var(--bubble-out); color: var(--bubble-out-text); border-top-right-radius: 7.5px; border-bottom-right-radius: 2px; }
.msg-row.in .bubble { background: var(--bubble-in); color: var(--bubble-in-text); border-top-left-radius: 7.5px; border-bottom-left-radius: 2px; }

.bubble .sender-name { font-size: 12.5px; font-weight: 700; color: var(--brand); margin-bottom: 2px; }

.bubble .msg-text { margin: 0; word-wrap: break-word; overflow-wrap: break-word; }

.bubble .quoted {
  background: rgba(0,0,0,0.06);
  border-left: 3px solid var(--brand);
  border-radius: 6px;
  padding: 5px 8px;
  margin-bottom: 5px;
  font-size: 12.5px;
  opacity: .85;
  cursor: pointer;
}
[data-theme="dark"] .bubble .quoted { background: rgba(255,255,255,0.06); }

.bubble .meta-row { display: flex; justify-content: flex-end; align-items: center; gap: 3px; margin-top: 2px; font-size: 11px; color: var(--text-muted); opacity: .85; }
.bubble .edited-tag { font-style: italic; opacity: .8; }

.checkmarks { display: inline-flex; }
.checkmarks svg { width: 15px; height: 15px; }
.checkmarks.sent svg { color: var(--check-sent); }
.checkmarks.read svg { color: var(--check-read); }

.bubble-media img, .bubble-media video { border-radius: 10px; max-width: 260px; max-height: 320px; margin-bottom: 4px; }
.bubble-file { display: flex; align-items: center; gap: 8px; background: rgba(0,0,0,0.05); padding: 8px; border-radius: 10px; margin-bottom: 4px; }
[data-theme="dark"] .bubble-file { background: rgba(255,255,255,0.06); }

.voice-player { display: flex; align-items: center; gap: 8px; min-width: 200px; }
.voice-player button { background: var(--brand); border: none; color: #fff; width: 34px; height: 34px; border-radius: 50%; flex-shrink: 0; display:flex; align-items:center; justify-content:center; }
.voice-wave { flex: 1; height: 24px; background: repeating-linear-gradient(90deg, currentColor 0 2px, transparent 2px 5px); opacity: .5; border-radius: 4px; }

.reactions-row { display: flex; gap: 3px; margin-top: -8px; margin-bottom: 4px; flex-wrap: wrap; }
.reaction-chip {
  background: var(--bg-elevated);
  border-radius: 999px;
  padding: 1px 7px;
  font-size: 12px;
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  gap: 3px;
  cursor: pointer;
}
.reaction-chip.mine { border: 1.5px solid var(--brand); }

.typing-indicator-bubble {
  display: inline-flex;
  gap: 4px;
  padding: 10px 14px;
  background: var(--bubble-in);
  border-radius: var(--radius-bubble);
  border-bottom-left-radius: 4px;
  width: fit-content;
  box-shadow: var(--shadow-sm);
}
.typing-indicator-bubble span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--text-muted);
  animation: typingBounce 1.2s infinite ease-in-out;
}
.typing-indicator-bubble span:nth-child(2) { animation-delay: .15s; }
.typing-indicator-bubble span:nth-child(3) { animation-delay: .3s; }
@keyframes typingBounce { 0%, 60%, 100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-4px); opacity: 1; } }

/* --- Zone de saisie --- */
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 10px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
}
.composer-input-wrap {
  flex: 1;
  background: var(--bg-app);
  border-radius: 22px;
  display: flex;
  align-items: flex-end;
  padding: 6px 6px 6px 14px;
  max-height: 120px;
}
.composer-input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  resize: none;
  max-height: 100px;
  color: var(--text-primary);
  font-size: 15px;
  padding: 6px 0;
  line-height: 1.35;
}
.composer .send-btn, .composer .mic-btn {
  width: 44px; height: 44px; border-radius: 50%;
  background: var(--brand); border: none; color: #fff;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; box-shadow: var(--shadow-sm);
  transition: transform .12s ease;
}
.composer .send-btn:active, .composer .mic-btn:active { transform: scale(.92); }
.composer .mic-btn.recording { background: var(--danger); animation: pulseRec 1s infinite; }
@keyframes pulseRec { 0%,100% { box-shadow: 0 0 0 0 rgba(229,72,77,.5);} 50% { box-shadow: 0 0 0 8px rgba(229,72,77,0);} }

.reply-preview, .edit-preview {
  background: var(--bg-hover);
  border-left: 3px solid var(--brand);
  border-radius: 8px;
  padding: 6px 10px;
  margin: 0 10px 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  animation: fadeIn .2s ease;
}

/* --- Bottom navigation mobile (Discussions / Contacts / Paramètres) --- */
.bottom-nav {
  display: flex;
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottom-nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 0 6px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 11px;
  position: relative;
}
.bottom-nav-item.active { color: var(--brand); }
.bottom-nav-item svg { width: 22px; height: 22px; }
.bottom-nav-item .badge-dot {
  position: absolute; top: 4px; right: calc(50% - 18px);
  background: var(--danger); color: #fff; font-size: 9px; font-weight: 700;
  min-width: 15px; height: 15px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
}

/* --- Modals --- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 20px;
  animation: fadeIn .2s ease;
}
.modal-box {
  background: var(--bg-elevated);
  border-radius: var(--radius-lg);
  width: 100%; max-width: 420px;
  max-height: 85vh;
  overflow-y: auto;
  padding: 20px;
  box-shadow: var(--shadow-lg);
  animation: fadeSlideUp .25s ease;
}
.modal-title { font-size: 18px; font-weight: 700; margin: 0 0 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }
.btn { padding: 9px 18px; border-radius: var(--radius-md); border: none; font-weight: 600; font-size: 14px; }
.btn-primary { background: var(--brand); color: #fff; }
.btn-ghost { background: transparent; color: var(--text-secondary); }
.btn-danger { background: var(--danger); color: #fff; }

/* --- Emoji / réaction picker --- */
.emoji-picker {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
  max-height: 240px;
  overflow-y: auto;
  padding: 8px;
}
.emoji-picker button { background: none; border: none; font-size: 22px; padding: 4px; border-radius: 8px; }
.emoji-picker button:hover { background: var(--bg-hover); }

.reaction-quickbar { display: flex; gap: 6px; background: var(--bg-elevated); padding: 6px 10px; border-radius: 999px; box-shadow: var(--shadow-md); }
.reaction-quickbar button { background: none; border: none; font-size: 20px; transition: transform .12s; }
.reaction-quickbar button:hover { transform: scale(1.25); }

/* --- Toast notifications in-app --- */
.toast {
  position: fixed; bottom: 90px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elevated); color: var(--text-primary);
  padding: 10px 18px; border-radius: 999px; box-shadow: var(--shadow-lg);
  font-size: 13.5px; z-index: 200; animation: fadeSlideUp .3s ease;
}

/* --- Loader --- */
.spinner { width: 22px; height: 22px; border: 3px solid var(--border); border-top-color: var(--brand); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ============================================================================
   RESPONSIVE — Desktop / tablette : vue à deux colonnes façon WhatsApp Web
   ============================================================================ */
@media (min-width: 900px) {
  .sidebar { width: clamp(320px, 30vw, 460px); max-width: clamp(320px, 30vw, 460px); }
  .chat-window {
    position: relative;
    transform: none !important;
    flex: 1;
  }
  .chat-window:not(.open) { display: flex; }
  .empty-chat-desktop {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    flex: 1; color: var(--text-muted); gap: 14px; background: var(--bg-panel);
  }
}
@media (max-width: 899px) {
  .empty-chat-desktop { display: none; }
  .bubble { max-width: 82%; }
}
