:root {
  --wa: #25D366;
  --wa-dark: #128C7E;
  --wa-deep: #075E54;
  --wa-light: #DCF8C6;
  --wa-bg: #ECE5DD;
  --wa-blue: #34B7F1;
  --surface: #ffffff;
  --bg: #f0f2f5;
  --text: #111b21;
  --text-soft: #667781;
  --text-mute: #8696a0;
  --border: #e9edef;
  --danger: #ea0038;
  --shadow: 0 4px 24px rgba(17, 27, 33, 0.08);
  --radius: 14px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html, body, #app { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button { font-family: inherit; cursor: pointer; }

.app-loading {
  display: flex; align-items: center; justify-content: center;
  height: 100vh; background: var(--wa-bg);
}
.loading-spinner {
  width: 40px; height: 40px;
  border: 3px solid rgba(0,0,0,0.1);
  border-top-color: var(--wa);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@supports (padding-top: env(safe-area-inset-top)) {
  .safe-top { padding-top: env(safe-area-inset-top); }
  .safe-bottom { padding-bottom: env(safe-area-inset-bottom); }
}

/* ==================== AUTH ==================== */
.auth-screen {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 1.5rem;
  background: linear-gradient(150deg, var(--wa-deep) 0%, #0a3d36 45%, #062924 100%);
}
.auth-card {
  width: 100%; max-width: 420px;
  background: var(--surface);
  border-radius: 22px;
  padding: 2.25rem 1.75rem;
  box-shadow: 0 30px 70px rgba(0,0,0,0.25);
}
.auth-brand {
  display: flex; align-items: center; gap: 0.6rem;
  margin-bottom: 1.75rem;
}
.auth-brand-icon {
  width: 46px; height: 46px;
  background: linear-gradient(135deg, var(--wa), var(--wa-dark));
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  color: white;
}
.auth-brand-title { font-weight: 800; font-size: 1.35rem; letter-spacing: -0.02em; line-height: 1; }
.auth-brand-sub { font-size: 0.78rem; color: var(--text-soft); margin-top: 0.2rem; }
.auth-title { font-size: 1.4rem; font-weight: 700; margin: 0 0 0.4rem; letter-spacing: -0.02em; }
.auth-desc { font-size: 0.9rem; color: var(--text-soft); margin: 0 0 1.25rem; line-height: 1.5; }

.auth-input {
  width: 100%; padding: 0.85rem 1rem;
  border: 1.5px solid var(--border);
  border-radius: 12px;
  font: inherit; font-size: 1rem;
  margin-bottom: 0.6rem;
  background: var(--surface);
  color: var(--text);
}
.auth-input:focus { outline: none; border-color: var(--wa); box-shadow: 0 0 0 3px rgba(37,211,102,0.15); }
.auth-code {
  text-align: center;
  letter-spacing: 0.5em;
  font-weight: 700;
  font-size: 1.4rem;
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.4rem;
  padding: 0.85rem 1.25rem;
  border: none; border-radius: 12px;
  font: inherit; font-weight: 700; font-size: 0.95rem;
  transition: all 0.15s; width: 100%;
}
.btn-wa { background: var(--wa); color: white; }
.btn-wa:active { background: var(--wa-dark); }
.btn-wa:disabled { background: #b9e3c6; cursor: not-allowed; }
.btn-ghost { background: transparent; color: var(--text-soft); }
.auth-error {
  margin: 0.4rem 0;
  font-size: 0.85rem; color: var(--danger);
  text-align: center; font-weight: 500;
}
.auth-foot { margin-top: 1rem; text-align: center; font-size: 0.75rem; color: var(--text-mute); }

/* ==================== SHELL ==================== */
.shell {
  display: flex; flex-direction: column;
  height: 100vh;
  background: var(--bg);
}
.shell-main {
  flex: 1;
  display: flex; flex-direction: column;
  overflow: hidden;
  background: var(--surface);
}

/* TopBar */
.topbar {
  background: var(--wa-deep);
  color: white;
  padding: 0.55rem 0.6rem;
  display: flex; align-items: center; gap: 0.4rem;
  position: sticky; top: 0; z-index: 10;
}
.topbar .safe-extend {
  height: env(safe-area-inset-top, 0px);
  background: var(--wa-deep);
  position: absolute; top: calc(-1 * env(safe-area-inset-top, 0px));
  left: 0; right: 0;
}
.topbar-back {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px;
  background: transparent; border: none;
  color: white; border-radius: 50%;
  flex-shrink: 0;
}
.topbar-back:active { background: rgba(255,255,255,0.15); }
.topbar-avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--wa), var(--wa-dark));
  color: white; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; font-size: 0.95rem;
}
.topbar-title {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column;
  gap: 0.1rem;
}
.topbar-name { font-weight: 700; font-size: 1.05rem; line-height: 1.1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-sub { font-size: 0.72rem; opacity: 0.78; }
.topbar-actions { display: flex; align-items: center; gap: 0.3rem; flex-shrink: 0; }
.topbar-icon-btn {
  width: 38px; height: 38px;
  display: flex; align-items: center; justify-content: center;
  background: transparent; border: none;
  color: white; border-radius: 50%;
}
.topbar-icon-btn:active { background: rgba(255,255,255,0.15); }

/* Line selector dans la topbar */
.topbar-line-btn {
  flex: 1; min-width: 0;
  display: flex; align-items: center; gap: 0.6rem;
  padding: 0.4rem 0.65rem 0.4rem 0.4rem;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  color: white;
  font-family: inherit;
}
.topbar-line-btn:active:not(:disabled) { background: rgba(255,255,255,0.18); }
.topbar-line-btn:disabled { opacity: 1; cursor: default; }
.topbar-line-icon {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--wa), var(--wa-dark));
  display: flex; align-items: center; justify-content: center;
  color: white; flex-shrink: 0;
}
.topbar-line-text {
  flex: 1; min-width: 0;
  text-align: left;
  display: flex; flex-direction: column;
  gap: 0.05rem;
}
.topbar-line-num {
  font-weight: 700; font-size: 0.92rem; line-height: 1.15;
  font-family: ui-monospace, monospace;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-line-sub {
  font-size: 0.7rem; opacity: 0.78; line-height: 1.2;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.topbar-line-chevron { flex-shrink: 0; opacity: 0.8; transition: transform 0.2s; }
.topbar-line-chevron.open { transform: rotate(180deg); }

/* Dropdown lignes */
.line-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0.6rem; right: 0.6rem;
  background: white;
  color: var(--text);
  border-radius: 14px;
  box-shadow: 0 14px 40px rgba(0,0,0,0.25);
  z-index: 20;
  max-height: 60vh;
  overflow-y: auto;
  padding: 0.4rem;
  border: 1px solid var(--border);
  animation: slideDown 0.18s ease;
}
@keyframes slideDown { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: translateY(0); } }
.line-dropdown-item {
  width: 100%;
  display: flex; align-items: center; gap: 0.7rem;
  padding: 0.6rem 0.55rem;
  background: transparent;
  border: none;
  border-radius: 10px;
  text-align: left;
  font-family: inherit;
  color: var(--text);
}
.line-dropdown-item:active { background: var(--bg); }
.line-dropdown-item.active { background: rgba(37, 211, 102, 0.1); }
.line-dropdown-icon {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--wa), var(--wa-dark));
  color: white;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.line-dropdown-text { flex: 1; min-width: 0; }
.line-dropdown-num { font-weight: 700; font-size: 0.92rem; font-family: ui-monospace, monospace; }
.line-dropdown-sub { font-size: 0.75rem; color: var(--text-soft); }
.line-dropdown-check { color: var(--wa-dark); flex-shrink: 0; }

/* ==================== INBOX LIST ==================== */
.inbox-list {
  flex: 1;
  overflow-y: auto;
  background: var(--surface);
}
.search-bar {
  padding: 0.6rem 0.85rem;
  background: var(--bg);
}
.search-input {
  width: 100%;
  background: var(--surface);
  border: none;
  padding: 0.6rem 0.85rem;
  border-radius: 999px;
  font: inherit; font-size: 0.9rem;
}
.search-input:focus { outline: none; box-shadow: 0 0 0 2px rgba(37,211,102,0.25); }

.conv-row {
  display: flex; align-items: center; gap: 0.75rem;
  padding: 0.7rem 0.95rem;
  border-bottom: 1px solid var(--border);
  background: transparent;
  border-left: none; border-right: none; border-top: none;
  width: 100%; text-align: left;
}
.conv-row:active { background: #f5f6f6; }
.conv-avatar {
  width: 48px; height: 48px; border-radius: 50%;
  background: linear-gradient(135deg, #6da4d5, #4a7fb0);
  color: white; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; font-size: 1rem;
}
.conv-avatar.v1 { background: linear-gradient(135deg, #ef9a9a, #c97676); }
.conv-avatar.v2 { background: linear-gradient(135deg, #a5d6a7, #6ab06d); }
.conv-avatar.v3 { background: linear-gradient(135deg, #ffcc80, #cc9b56); }
.conv-avatar.v4 { background: linear-gradient(135deg, #ce93d8, #8e63a5); }
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; justify-content: space-between; gap: 0.5rem; align-items: baseline; }
.conv-name { font-weight: 600; font-size: 0.95rem; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: 0.7rem; color: var(--text-soft); flex-shrink: 0; }
.conv-time.unread { color: var(--wa); font-weight: 600; }
.conv-bot { font-size: 0.62rem; color: var(--wa-deep); background: var(--wa-light); padding: 0.05rem 0.4rem; border-radius: 4px; font-weight: 700; margin-right: 0.3rem; }
.conv-bottom { display: flex; justify-content: space-between; gap: 0.5rem; align-items: center; margin-top: 0.2rem; }
.conv-preview { font-size: 0.82rem; color: var(--text-soft); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.unread-badge {
  background: var(--wa); color: white;
  font-size: 0.68rem; font-weight: 700;
  padding: 0.1rem 0.45rem; border-radius: 999px;
  min-width: 20px; text-align: center; flex-shrink: 0;
}

/* ==================== THREAD ==================== */
.thread-screen {
  display: flex; flex-direction: column;
  height: 100%;
  background: var(--wa-bg);
}
.thread-bg {
  flex: 1; overflow-y: auto;
  padding: 0.75rem 0.5rem;
  background-color: var(--wa-bg);
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='%23d1d7db' fill-opacity='0.25'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/svg%3E");
}
.day-sep {
  text-align: center;
  margin: 0.5rem 0;
}
.day-sep span {
  background: rgba(225, 245, 254, 0.85);
  color: var(--text-soft);
  font-size: 0.72rem; font-weight: 600;
  padding: 0.25rem 0.75rem;
  border-radius: 8px;
  box-shadow: 0 1px 1px rgba(0,0,0,0.05);
}
.msg-row { display: flex; padding: 0.1rem 0.4rem; }
.msg-row.out { justify-content: flex-end; }
.msg-bubble {
  max-width: 78%;
  padding: 0.45rem 0.6rem 0.3rem;
  border-radius: 8px;
  font-size: 0.92rem;
  line-height: 1.4;
  box-shadow: 0 1px 1px rgba(0,0,0,0.08);
  position: relative;
  word-wrap: break-word;
}
.msg-row.in .msg-bubble { background: white; border-top-left-radius: 0; }
.msg-row.out .msg-bubble { background: var(--wa-light); border-top-right-radius: 0; }
.msg-text { white-space: pre-wrap; }
.msg-media-link {
  display: inline-block;
  margin-top: 0.25rem;
  font-size: 0.78rem;
  color: var(--wa-dark);
  font-weight: 600;
}
.msg-foot {
  display: inline-flex; align-items: center; gap: 0.2rem;
  float: right;
  margin-left: 0.65rem; margin-top: 0.2rem;
  font-size: 0.66rem; color: var(--text-soft);
}
.msg-by-bot { font-style: italic; color: var(--wa-dark); }
.msg-status { color: var(--wa-dark); }
.msg-status.read { color: var(--wa-blue); }

/* Compose */
.compose-wrap {
  padding: 0.5rem 0.5rem env(safe-area-inset-bottom, 0.5rem);
  background: var(--bg);
  display: flex; gap: 0.4rem; align-items: flex-end;
}
.compose-input-wrap {
  flex: 1;
  background: var(--surface);
  border-radius: 22px;
  padding: 0.5rem 0.85rem;
  display: flex; align-items: center;
  box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.compose-input {
  flex: 1;
  border: none;
  background: transparent;
  font: inherit;
  font-size: 0.95rem;
  resize: none;
  min-height: 22px;
  max-height: 120px;
  line-height: 1.4;
  padding: 0;
  outline: none;
}
.compose-send {
  width: 46px; height: 46px;
  border-radius: 50%;
  background: var(--wa);
  color: white;
  border: none;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.compose-send:active { background: var(--wa-dark); }
.compose-send:disabled { background: #b9e3c6; }

.thread-banner {
  background: rgba(255, 248, 225, 0.95);
  color: #6b5005;
  font-size: 0.75rem;
  padding: 0.5rem 0.85rem;
  text-align: center;
}

/* ==================== STATES ==================== */
.empty-state {
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 3.5rem 1.5rem;
  text-align: center;
  color: var(--text-soft);
  flex: 1;
}
.empty-icon {
  width: 84px; height: 84px;
  border-radius: 50%;
  background: var(--wa-light);
  color: var(--wa-deep);
  display: flex; align-items: center; justify-content: center;
  margin-bottom: 1rem;
}
.empty-title { font-weight: 700; color: var(--text); margin-bottom: 0.3rem; font-size: 1.05rem; }
.empty-desc { font-size: 0.88rem; max-width: 280px; line-height: 1.5; }

/* ==================== TOAST ==================== */
.toast {
  position: fixed; bottom: 1.5rem; left: 50%;
  transform: translateX(-50%);
  background: rgba(17, 27, 33, 0.95);
  color: white;
  padding: 0.7rem 1.1rem;
  border-radius: 999px;
  font-size: 0.85rem; font-weight: 600;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  z-index: 9999;
  animation: toastIn 0.25s ease;
  max-width: 90vw;
}
.toast.error { background: var(--danger); }
.toast.success { background: var(--wa-dark); }
@keyframes toastIn {
  from { transform: translate(-50%, 20px); opacity: 0; }
  to { transform: translate(-50%, 0); opacity: 1; }
}

/* ==================== SETTINGS DRAWER (basic) ==================== */
.drawer-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 100;
  animation: fade 0.15s ease;
}
.drawer {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  z-index: 101;
  padding-bottom: env(safe-area-inset-bottom, 0);
  animation: slideUp 0.2s ease;
  max-height: 80vh;
  overflow-y: auto;
}
.drawer-handle {
  width: 36px; height: 4px;
  background: #ccd0d5;
  border-radius: 2px;
  margin: 0.6rem auto 0.4rem;
}
.drawer h2 { font-size: 1.1rem; margin: 0.5rem 1rem 0.8rem; }
.drawer-row {
  display: flex; align-items: center; gap: 0.85rem;
  padding: 0.9rem 1rem;
  border-top: 1px solid var(--border);
  width: 100%;
  background: transparent; border-left: 0; border-right: 0; border-bottom: 0;
  text-align: left; font: inherit;
}
.drawer-row:active { background: var(--bg); }
.drawer-row-icon {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--wa-light);
  color: var(--wa-deep);
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.drawer-row-text { flex: 1; }
.drawer-row-label { font-weight: 600; font-size: 0.93rem; }
.drawer-row-desc { font-size: 0.78rem; color: var(--text-soft); margin-top: 0.1rem; }
.drawer-row.danger .drawer-row-icon { background: #fee2e2; color: var(--danger); }
.drawer-row.danger .drawer-row-label { color: var(--danger); }

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }

/* ==================== UTILS ==================== */
.spin { animation: spin 0.8s linear infinite; }
.scroll-y { flex: 1; overflow-y: auto; }
.app-root { display: flex; flex-direction: column; height: 100%; }
.app-root.has-bottom-nav .shell { padding-bottom: 0; }
.app-root.has-bottom-nav .shell-main {
  padding-bottom: calc(68px + env(safe-area-inset-bottom, 0px));
}

/* Inbox foot */
.inbox-foot {
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-soft);
  padding: 1.25rem 1rem 1.5rem;
  display: flex; align-items: center; justify-content: center; gap: 0.4rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.conv-row-wrap { background: var(--surface); }

/* ==================== BOTTOM NAV ==================== */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: 1fr 1fr 84px 1fr 1fr;
  align-items: center;
  padding: 0 0.25rem 0;
  z-index: 40;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  box-shadow: 0 -4px 20px rgba(0,0,0,0.06);
  min-height: 64px;
  overflow: visible;
}
.nav-tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.15rem;
  padding: 0.45rem 0.25rem 0.5rem;
  background: transparent;
  border: none;
  color: var(--text-mute);
  font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}
.nav-tab span { font-size: 0.66rem; font-weight: 600; letter-spacing: 0.01em; }
.nav-tab svg { width: 24px; height: 24px; }
.nav-tab:active { opacity: 0.6; }
.nav-tab.active { color: var(--wa-dark); }
.nav-tab.active svg { stroke-width: 2.2; }

.nav-fab {
  width: 62px; height: 62px;
  margin: 0 auto;
  position: relative;
  top: -22px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--wa), var(--wa-dark));
  color: white;
  border: 4px solid var(--surface);
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 20px rgba(37, 211, 102, 0.5);
  transition: transform 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.nav-fab:active { transform: scale(0.92); }
.nav-fab svg { width: 26px; height: 26px; }

/* ==================== STATUT ==================== */
.status-wrap { padding: 0.85rem 0.85rem 1.25rem; }
.status-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.6rem;
  margin-bottom: 1.25rem;
}
.status-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem 0.85rem;
  border: 1px solid var(--border);
}
.status-card-val { font-size: 1.7rem; font-weight: 800; color: var(--wa-deep); line-height: 1; }
.status-card-lbl { font-size: 0.74rem; color: var(--text-soft); margin-top: 0.4rem; line-height: 1.3; }

.status-section-title {
  font-size: 0.72rem;
  text-transform: uppercase;
  color: var(--text-mute);
  font-weight: 700;
  letter-spacing: 0.06em;
  padding: 0.5rem 0.25rem;
}
.status-row {
  display: flex; align-items: center; gap: 0.7rem;
  background: var(--surface);
  padding: 0.6rem 0.75rem;
  border-radius: 12px;
  margin-bottom: 0.35rem;
  border: 1px solid var(--border);
}
.status-row-main { flex: 1; min-width: 0; }
.status-row-name { font-weight: 600; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-row-prev { font-size: 0.78rem; color: var(--text-soft); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-row-time { font-size: 0.72rem; color: var(--text-soft); }
.status-empty {
  background: var(--surface);
  padding: 1.5rem 1rem;
  text-align: center;
  border-radius: var(--radius);
  color: var(--text-soft);
  font-size: 0.88rem;
  border: 1px solid var(--border);
}
.status-line-dot {
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--wa);
  box-shadow: 0 0 0 4px rgba(37,211,102,0.15);
  flex-shrink: 0;
  margin: 0 0.35rem;
}

/* ==================== SETTINGS ==================== */
.settings-profile {
  display: flex; align-items: center; gap: 1rem;
  padding: 1.25rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.settings-avatar {
  width: 60px; height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--wa), var(--wa-dark));
  color: white;
  font-size: 1.4rem;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.settings-profile-info { flex: 1; min-width: 0; }
.settings-name { font-weight: 700; font-size: 1.05rem; }
.settings-mail { font-size: 0.85rem; color: var(--text-soft); }
.settings-group {
  background: var(--surface);
  margin-top: 0.65rem;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}
.settings-foot {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-mute);
  padding: 2rem 1rem;
}
