
* {
    box-sizing: border-box;
  }
  
  :root {
    color-scheme: light;
    font-family: Arial, Helvetica, sans-serif;
    background: #e9edef;
    color: #111b21;
  }
  
  html,
  body {
    width: 100%;
    height: 100%;
    min-height: 0;
    margin: 0;
    overflow: hidden;
  }
  
  body {
    display: flex;
  }
  
  button,
  input {
    font: inherit;
  }
  
  #app {
    display: flex;
    width: 100%;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    background: #fff;
  }
  
  .sidebar {
    display: flex;
    flex-direction: column;
    flex: 0 0 340px;
    width: 340px;
    min-width: 250px;
    min-height: 0;
    border-right: 1px solid #d1d7db;
    background: #fff;
  }
  
  .sidebar-header {
    display: flex;
    align-items: center;
    min-height: 64px;
    padding: 16px;
    background: #f0f2f5;
    border-bottom: 1px solid #e4e7e9;
  }
  
  .sidebar-header h1 {
    margin: 0;
    font-size: 20px;
    font-weight: 700;
  }
  
  #chat-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  
  .chat-item {
    display: flex;
    width: 100%;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border: 0;
    border-bottom: 1px solid #f0f2f5;
    background: #fff;
    text-align: left;
    cursor: pointer;
  }
  
  .chat-item:hover {
    background: #f5f6f6;
  }
  
  .chat-item.active {
    background: #e9edef;
  }
  
  .chat-avatar {
    display: flex;
    flex: 0 0 46px;
    width: 46px;
    height: 46px;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    background: #d9fdd3;
    color: #176b4d;
    font-size: 20px;
    font-weight: 700;
  }
  
  .chat-item-info {
    flex: 1;
    min-width: 0;
  }
  
  .chat-item-name {
    overflow: hidden;
    margin-bottom: 5px;
    font-size: 15px;
    font-weight: 600;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  .chat-item-preview {
    color: #667781;
    font-size: 12px;
  }
  
  .main-panel {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: #efeae2;
  }
  
  #chat-view {
    display: flex;
    flex: 1;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
  }
  
  #chat-view[hidden],
  #empty-state[hidden],
  #loading-older[hidden] {
    display: none !important;
  }
  
  .chat-header {
    display: flex;
    flex: 0 0 auto;
    min-height: 64px;
    align-items: center;
    padding: 10px 20px;
    border-bottom: 1px solid #d8dddf;
    background: #f0f2f5;
  }
  
  .chat-header-text {
    min-width: 0;
  }
  
  #chat-title {
    overflow: hidden;
    margin: 0 0 4px;
    font-size: 16px;
    font-weight: 700;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  #chat-subtitle {
    color: #667781;
    font-size: 12px;
  }
  
  #messages {
    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-height: 0;
    padding: 16px clamp(10px, 3vw, 44px);
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    scrollbar-gutter: stable;
    scroll-behavior: auto;
  }
  
  #loading-older {
    align-self: center;
    flex: 0 0 auto;
    margin: 4px auto 14px;
    padding: 8px 12px;
    border-radius: 18px;
    background: #fff;
    color: #667781;
    font-size: 12px;
    box-shadow: 0 1px 2px #00000012;
  }
  
  .date-separator {
    display: flex;
    justify-content: center;
    margin: 12px 0 18px;
  }
  
  .date-separator span {
    padding: 7px 12px;
    border-radius: 8px;
    background: #fff;
    color: #54656f;
    font-size: 12px;
    box-shadow: 0 1px 1px #0000000a;
  }
  
  .message-row {
    display: flex;
    flex: 0 0 auto;
    width: 100%;
    margin-bottom: 8px;
  }
  
  .message-row.from-me {
    justify-content: flex-end;
  }
  
  .message-row.from-them {
    justify-content: flex-start;
  }
  
  .message-bubble {
    width: fit-content;
    max-width: min(76%, 640px);
    min-width: 90px;
    padding: 8px 10px 5px;
    border-radius: 8px;
    background: #fff;
    box-shadow: 0 1px 1px #0000000d;
    overflow-wrap: anywhere;
  }
  
  .from-me .message-bubble {
    background: #d9fdd3;
  }
  
  .message-sender {
    margin-bottom: 5px;
    color: #075e54;
    font-size: 12px;
    font-weight: 700;
  }
  
  .message-text,
  .message-caption {
    font-size: 14px;
    line-height: 1.45;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
  }
  
  .message-caption {
    margin-top: 7px;
  }
  
  .message-time {
    margin-top: 5px;
    color: #667781;
    font-size: 10px;
    text-align: right;
  }
  
  .media {
    display: flex;
    flex-direction: column;
    gap: 7px;
    margin-top: 6px;
  }
  
  .media-label {
    color: #54656f;
    font-size: 12px;
    font-weight: 600;
  }
  
  .media audio {
    display: block;
    width: min(320px, 100%);
    max-width: 100%;
    height: 42px;
  }
  
  .media video {
    display: block;
    width: min(420px, 100%);
    max-width: 100%;
    max-height: 340px;
    border-radius: 6px;
    background: #111;
  }
  
  .image-media img {
    display: block;
    width: auto;
    max-width: min(360px, 100%);
    max-height: 360px;
    border-radius: 6px;
    object-fit: contain;
  }
  
  .media-download {
    align-self: flex-start;
    color: #027eb5;
    font-size: 12px;
    text-decoration: none;
  }
  
  .media-download:hover {
    text-decoration: underline;
  }
  
  #empty-state {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: center;
    padding: 30px;
    color: #54656f;
    text-align: center;
  }
  
  .loading-message,
  .no-messages,
  .error-message {
    margin: auto;
    padding: 20px;
    color: #54656f;
    text-align: center;
    overflow-wrap: anywhere;
  }
  
  .error-message {
    color: #b42318;
  }
  
  @media (max-width: 720px) {
    .sidebar {
      flex-basis: 110px;
      width: 110px;
      min-width: 90px;
    }
  
    .sidebar-header {
      padding: 12px 8px;
    }
  
    .sidebar-header h1 {
      font-size: 14px;
    }
  
    .chat-item {
      flex-direction: column;
      gap: 5px;
      padding: 10px 4px;
      text-align: center;
    }
  
    .chat-item-info {
      width: 100%;
    }
  
    .chat-item-name {
      font-size: 11px;
    }
  
    .chat-item-preview {
      font-size: 10px;
    }
  
    .chat-avatar {
      width: 38px;
      height: 38px;
      flex-basis: 38px;
      font-size: 16px;
    }
  
    .message-bubble {
      max-width: 92%;
    }
  
    #messages {
      padding: 12px 8px;
    }
  
    .media video {
      max-height: 240px;
    }
  }
  .search-box {
    padding: 10px 12px;
    background: #f0f2f5;
  }
  
  .search-box input,
  .discussion-search input {
    width: 100%;
    min-width: 0;
    padding: 10px 13px;
    border: 1px solid #d9dfe2;
    border-radius: 8px;
    outline: none;
    background: white;
    color: #111b21;
    font-size: 13px;
  }
  
  .search-box input:focus,
  .discussion-search input:focus {
    border-color: #00a884;
  }
  
  .discussion-search {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    width: min(520px, 65%);
    margin-top: 8px;
  }
  
  .discussion-search input {
    flex: 1 1 180px;
  }
  
  .discussion-search button {
    width: 32px;
    height: 32px;
    border: 0;
    border-radius: 6px;
    background: #e1e8eb;
    cursor: pointer;
  }
  
  .discussion-search button:hover {
    background: #cfdadf;
  }
  
  #search-count {
    color: #54656f;
    font-size: 11px;
    white-space: nowrap;
  }
  
  .message-row.search-match .message-bubble {
    outline: 2px solid #f5c451;
  }
  
  .message-row.search-current .message-bubble {
    outline: 3px solid #00a884;
  }
  
  .chat-item[hidden] {
    display: none !important;
  }
  
  @media (max-width: 720px) {
    .discussion-search {
      width: 100%;
    }
  }
  
  .message-text,
.message-caption {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.45;
}

.message-text strong,
.message-caption strong {
  font-weight: 700;
}

#scroll-to-bottom {
    position: absolute;
    right: 24px;
    bottom: 90px;
    z-index: 100;
    width: 44px;
    height: 44px;
    border: 1px solid #37474f;
    border-radius: 50%;
    background: #202c33;
    color: #e9edef;
    font-size: 25px;
    line-height: 1;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
    display: none;
    align-items: center;
    justify-content: center;
  }
  
  #scroll-to-bottom:hover {
    background: #2a3942;
  }
  
  #scroll-to-bottom.visible {
    display: flex;
  }

  /* =========================================================
   MOBILE FIRST — WhatsApp Archive
   ========================================================= */

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

/* Empêche les éléments flex de dépasser de l'écran */
#app,
.app,
main,
.sidebar,
.chat-view,
.messages,
#messages {
  min-width: 0;
  min-height: 0;
}

/* =========================================================
   DESKTOP
   ========================================================= */

#scroll-to-bottom {
  position: absolute;
  right: 24px;
  bottom: 90px;
  z-index: 100;

  width: 44px;
  height: 44px;

  border: 1px solid #d9dfe2;
  border-radius: 50%;

  background: #ffffff;
  color: #54656f;

  font-size: 24px;
  line-height: 1;

  cursor: pointer;

  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);

  display: none;
  align-items: center;
  justify-content: center;
}

#scroll-to-bottom.visible {
  display: flex;
}

.search-box {
  padding: 8px 12px;
}

.search-box input,
.discussion-search input {
  min-height: 38px;
}

/* =========================================================
   MOBILE
   ========================================================= */

@media (max-width: 700px) {

  html,
  body {
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  body {
    font-size: 14px;
  }

  /* -------------------------------------------------------
     Structure générale
     ------------------------------------------------------- */

  #app,
  .app,
  main {
    width: 100%;
    height: 100dvh;
    min-height: 100dvh;
  }

  /*
    Par défaut, la liste des discussions occupe tout l'écran.
    La discussion ouverte vient par-dessus.
  */

  .sidebar {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100dvh;
    z-index: 10;

    display: flex;
    flex-direction: column;
  }

  .chat-view {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100dvh;
    z-index: 20;

    display: flex;
    flex-direction: column;
  }

  /*
    Quand la discussion est cachée par le JS, elle ne doit
    absolument pas prendre de place.
  */

  .chat-view[hidden],
  .sidebar[hidden] {
    display: none !important;
  }

  /* -------------------------------------------------------
     Liste des conversations
     ------------------------------------------------------- */

  .sidebar-header {
    flex: 0 0 auto;

    min-height: 58px;
    padding:
      max(10px, env(safe-area-inset-top))
      14px
      10px
      14px;

    display: flex;
    align-items: center;
  }

  .search-box {
    flex: 0 0 auto;
    padding: 8px 12px 10px;

    background: inherit;
  }

  .search-box input {
    width: 100%;
    height: 44px;
    min-height: 44px;

    padding: 0 15px;

    border: 0;
    border-radius: 10px;

    font-size: 16px;
  }

  .chat-list {
    flex: 1 1 auto;

    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;

    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .chat-item {
    width: 100%;
    min-height: 70px;

    padding: 10px 14px;

    display: flex;
    align-items: center;

    border: 0;
    border-bottom: 1px solid rgba(128, 128, 128, 0.12);

    text-align: left;
  }

  .chat-avatar {
    flex: 0 0 50px;

    width: 50px;
    height: 50px;

    margin-right: 13px;

    border-radius: 50%;

    display: flex;
    align-items: center;
    justify-content: center;

    font-size: 20px;
  }

  .chat-item-info {
    flex: 1 1 auto;
    min-width: 0;
  }

  .chat-item-name {
    width: 100%;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    font-size: 16px;
    font-weight: 500;
  }

  .chat-item-preview {
    margin-top: 3px;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    font-size: 13px;
  }

  /* -------------------------------------------------------
     En-tête de discussion
     ------------------------------------------------------- */

  .chat-header {
    flex: 0 0 auto;

    min-height: 60px;

    padding:
      max(7px, env(safe-area-inset-top))
      10px
      8px
      10px;

    display: flex;
    flex-wrap: wrap;
    align-items: center;

    position: relative;
    z-index: 5;
  }

  #chat-title {
    flex: 1 1 auto;
    min-width: 0;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    font-size: 16px;
    font-weight: 600;
  }

  #chat-subtitle {
    flex: 0 0 100%;

    margin-top: 1px;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    font-size: 11px;
  }

  /* -------------------------------------------------------
     Recherche dans la conversation
     ------------------------------------------------------- */

  .discussion-search {
    order: 10;

    width: 100%;
    margin-top: 7px;

    display: flex;
    align-items: center;
    gap: 5px;
  }

  .discussion-search input {
    flex: 1 1 auto;
    min-width: 0;

    width: auto;
    height: 38px;
    min-height: 38px;

    padding: 0 11px;

    border-radius: 9px;

    font-size: 15px;
  }

  #search-count {
    flex: 0 0 auto;

    max-width: 65px;

    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;

    font-size: 10px;
  }

  .discussion-search button {
    flex: 0 0 36px;

    width: 36px;
    height: 36px;

    padding: 0;

    border-radius: 50%;

    font-size: 18px;

    touch-action: manipulation;
  }

  /* -------------------------------------------------------
     Messages
     ------------------------------------------------------- */

  .messages,
  #messages {
    flex: 1 1 auto;

    width: 100%;
    min-height: 0;

    overflow-x: hidden;
    overflow-y: auto;

    padding: 8px 7px 14px;

    -webkit-overflow-scrolling: touch;

    overscroll-behavior-y: contain;

    scroll-behavior: smooth;
  }

  .message-row {
    width: 100%;

    margin: 2px 0;

    display: flex;
  }

  .message-bubble {
    /*
      Important : on ne met PAS une largeur fixe.
      Les messages longs doivent pouvoir utiliser l'écran.
    */
    max-width: min(86%, 520px);

    padding: 7px 9px 5px;

    border-radius: 8px;

    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .from-me {
    justify-content: flex-end;
  }

  .from-them {
    justify-content: flex-start;
  }

  .message-text {
    max-width: 100%;

    font-size: 15px;
    line-height: 1.42;

    overflow-wrap: anywhere;
    word-break: break-word;
  }

  .message-caption {
    font-size: 14px;
    line-height: 1.4;

    overflow-wrap: anywhere;
  }

  .message-time {
    margin-top: 3px;

    font-size: 9px;

    text-align: right;
    white-space: nowrap;
  }

  .message-sender {
    margin-bottom: 2px;

    font-size: 11px;
    font-weight: 600;
  }

  /* -------------------------------------------------------
     Médias
     ------------------------------------------------------- */

  .media {
    max-width: 100%;
  }

  .media img {
    display: block;

    max-width: 100%;
    width: auto;
    height: auto;

    max-height: 55dvh;

    object-fit: contain;

    border-radius: 6px;
  }

  .media video {
    display: block;

    width: min(100%, 420px);
    max-width: 100%;
    max-height: 55dvh;

    border-radius: 7px;

    background: #000;
  }

  .media audio {
    width: min(100%, 320px);
    max-width: 100%;

    height: 42px;
  }

  .media-label {
    margin-bottom: 4px;
    font-size: 11px;
  }

  .media-download {
    display: inline-block;

    margin-top: 4px;

    font-size: 11px;
  }

  /* -------------------------------------------------------
     Séparateurs de dates
     ------------------------------------------------------- */

  .date-separator {
    position: sticky;
    top: 6px;

    z-index: 2;

    width: max-content;
    max-width: calc(100% - 20px);

    margin: 12px auto 8px;
  }

  .date-separator span {
    display: block;

    padding: 5px 9px;

    border-radius: 7px;

    font-size: 11px;

    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
  }

  /* -------------------------------------------------------
     Recherche : résultats
     ------------------------------------------------------- */

  .message-row.search-match .message-bubble {
    outline: 2px solid #f5c451;
  }

  .message-row.search-current .message-bubble {
    outline: 3px solid #00a884;
  }

  /* -------------------------------------------------------
     Bouton "retour en bas"
     ------------------------------------------------------- */

  #scroll-to-bottom {
    position: absolute;

    right: 14px;

    /*
      On le place suffisamment haut pour ne pas être
      derrière la zone de navigation/saisie.
    */
    bottom: max(18px, env(safe-area-inset-bottom));

    width: 44px;
    height: 44px;

    z-index: 50;

    border: 0;
    border-radius: 50%;

    display: none;
    align-items: center;
    justify-content: center;

    font-size: 23px;

    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);

    touch-action: manipulation;
  }

  #scroll-to-bottom.visible {
    display: flex;
  }

  /* -------------------------------------------------------
     États
     ------------------------------------------------------- */

  .loading-message,
  .no-messages,
  .error-message,
  .empty-state {
    padding: 25px 15px;

    text-align: center;

    font-size: 14px;
  }

  /* -------------------------------------------------------
     Safe areas iPhone
     ------------------------------------------------------- */

  @supports (padding: env(safe-area-inset-bottom)) {
    .chat-list {
      padding-bottom: env(safe-area-inset-bottom);
    }

    #messages {
      padding-bottom: calc(14px + env(safe-area-inset-bottom));
    }
  }
}

/* =========================================================
   TRÈS PETITS TÉLÉPHONES
   ========================================================= */

@media (max-width: 380px) {

  .chat-item {
    min-height: 64px;
    padding: 8px 11px;
  }

  .chat-avatar {
    flex-basis: 45px;
    width: 45px;
    height: 45px;
    margin-right: 10px;
  }

  .chat-item-name {
    font-size: 15px;
  }

  .message-bubble {
    max-width: 90%;
  }

  .message-text {
    font-size: 14px;
  }

  .discussion-search {
    gap: 3px;
  }

  .discussion-search button {
    flex-basis: 32px;
    width: 32px;
    height: 32px;
  }
}

/* =========================================================
   MODE SOMBRE — MOBILE
   ========================================================= */

@media (max-width: 700px) and (prefers-color-scheme: dark) {

  body {
    background: #0b141a;
    color: #e9edef;
  }

  .sidebar,
  .chat-header,
  .sidebar-header,
  .chat-list {
    background: #111b21;
    color: #e9edef;
  }

  #messages,
  .messages,
  .chat-view {
    background: #0b141a;
    color: #e9edef;
  }

  .chat-item {
    background: #111b21;
    color: #e9edef;
  }

  .chat-item:hover,
  .chat-item.active {
    background: #202c33;
  }

  .chat-item {
    border-bottom-color: #202c33;
  }

  .chat-item-preview,
  #chat-subtitle,
  .message-time {
    color: #8696a0;
  }

  .message-bubble {
    background: #202c33;
    color: #e9edef;
  }

  .from-me .message-bubble {
    background: #005c4b;
    color: #e9edef;
  }

  .search-box input,
  .discussion-search input {
    background: #202c33;
    color: #e9edef;
    border-color: transparent;
  }

  .search-box input::placeholder,
  .discussion-search input::placeholder {
    color: #8696a0;
  }

  .discussion-search button,
  #scroll-to-bottom {
    background: #202c33;
    color: #e9edef;
    border-color: #374045;
  }

  .date-separator span {
    background: #182229;
    color: #d1d7db;
  }
}
#chat-view {
  position: relative;
}

/* Navigation mobile : contacts OU discussion */
.mobile-back-button {
  display: none;
}

@media (max-width: 700px) {
  #app {
    position: relative;
    width: 100%;
    height: 100dvh;
    overflow: hidden;
  }

  #app:not(.chat-open) .sidebar {
    display: flex;
    position: relative;
    inset: auto;
    width: 100%;
    height: 100%;
    flex: 1 1 100%;
    min-width: 0;
    border-right: 0;
  }

  #app:not(.chat-open) .main-panel {
    display: none;
  }

  #app.chat-open .sidebar {
    display: none;
  }

  #app.chat-open .main-panel {
    display: flex;
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    min-width: 0;
    z-index: 20;
  }

  #app.chat-open #chat-view {
    display: flex;
    width: 100%;
    height: 100%;
    min-height: 0;
  }

  .mobile-back-button {
    display: flex;
    flex: 0 0 38px;
    width: 38px;
    height: 38px;
    align-items: center;
    justify-content: center;
    margin-right: 10px;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: inherit;
    font-size: 28px;
    cursor: pointer;
    touch-action: manipulation;
  }

  .chat-header-text {
    flex: 1 1 0;
    min-width: 0;
  }

  .chat-header {
    flex-wrap: nowrap;
    padding-left: 8px;
  }

  .discussion-search {
    width: 100%;
  }

  #messages {
    flex: 1 1 0;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
}