
:root {
  color-scheme: dark;
  --bg: #101114;
  --surface: #191b20;
  --surface-2: #22252c;
  --border: #2c2f37;
  --text: #f4f5f7;
  --muted: #9297a3;
  --green: #28d7a0;
  --green-dark: #153e34;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input {
  font: inherit;
}

button {
  color: inherit;
  cursor: pointer;
}

.app {
  width: 100%;
  max-width: 760px;
  min-height: 100dvh;
  margin: 0 auto;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 18px 16px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 43px;
  height: 43px;
  display: grid;
  place-items: center;
  border-radius: 15px;
  background: var(--green);
  color: #08271e;
  font-size: 24px;
  font-weight: 900;
}

.brand h1, .brand p, .conversation-info h2,
.conversation-info p, .section-heading h2 {
  margin: 0;
}

.brand h1 {
  font-size: 17px;
  letter-spacing: -.3px;
}

.brand p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.icon-button {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 13px;
  background: var(--surface);
  font-size: 22px;
}

.search-box {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px 16px 13px;
  padding: 0 14px;
  height: 47px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  color: var(--muted);
  font-size: 23px;
}

.search-box input {
  width: 100%;
  border: 0;
  outline: 0;
  color: var(--text);
  background: transparent;
  font-size: 14px;
}

.filter-row {
  display: flex;
  gap: 8px;
  padding: 0 16px 18px;
  overflow-x: auto;
  scrollbar-width: none;
}

.filter {
  padding: 9px 13px;
  white-space: nowrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--muted);
  font-size: 12px;
}

.filter.active {
  background: var(--green-dark);
  border-color: #286c57;
  color: var(--green);
}

.section-heading {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 18px 12px;
}

.section-heading h2 {
  font-size: 15px;
}

.section-heading span {
  color: var(--muted);
  font-size: 12px;
}

.chat-list {
  padding: 0 10px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 13px 8px;
  border-bottom: 1px solid #202228;
  border-radius: 12px;
  cursor: pointer;
}

.chat-item:active {
  background: var(--surface);
}

.avatar {
  display: grid;
  place-items: center;
  width: 49px;
  height: 49px;
  flex-shrink: 0;
  border-radius: 50%;
  background: #283e38;
  color: var(--green);
  font-size: 17px;
  font-weight: 700;
}

.chat-details {
  min-width: 0;
  flex: 1;
}

.chat-top, .chat-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.chat-name {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 14px;
  font-weight: 650;
}

.chat-time {
  flex-shrink: 0;
  color: var(--muted);
  font-size: 10px;
}

.chat-preview {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: var(--muted);
  font-size: 12px;
}

.chat-bottom {
  margin-top: 7px;
}

.chat-tags {
  display: flex;
  align-items: center;
  gap: 5px;
}

.tag {
  color: var(--muted);
  font-size: 10px;
}

.unread-dot {
  display: grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 20px;
  background: var(--green);
  color: #08271e;
  font-size: 10px;
  font-weight: 800;
}

.conversation-page {
  display: flex;
  flex-direction: column;
  height: calc(100dvh - 150px - env(safe-area-inset-bottom));
  min-height: 300px;
}

.conversation-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.conversation-info {
  flex: 1;
  min-width: 0;
}

.conversation-info h2 {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}

.conversation-info p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
}

.message-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  padding: 16px 12px;
}

.message {
  max-width: 84%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 14px 14px 14px 4px;
  background: var(--surface-2);
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  font-size: 13px;
  line-height: 1.5;
}

.message.outgoing {
  align-self: flex-end;
  border-color: #205644;
  border-radius: 14px 14px 4px 14px;
  background: var(--green-dark);
}

.message-meta {
  display: flex;
  justify-content: flex-end;
  margin-top: 5px;
  color: #aab5b1;
  font-size: 10px;
}

.read-note {
  padding: 10px;
  text-align: center;
  color: var(--muted);
  font-size: 10px;
  border-top: 1px solid var(--border);
}

.bottom-nav {
  position: fixed;
  z-index: 10;
  right: 0;
  bottom: 0;
  left: 0;
  display: flex;
  justify-content: center;
  gap: 30px;
  padding: 10px 20px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: rgba(16, 17, 20, .96);
  backdrop-filter: blur(15px);
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-width: 65px;
  padding: 4px;
  border: 0;
  background: transparent;
  color: var(--muted);
}

.nav-item span {
  font-size: 21px;
}

.nav-item small {
  font-size: 10px;
}

.nav-item.selected {
  color: var(--green);
}

.loading, .empty-state, .error-state {
  padding: 30px 12px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

.error-state button {
  margin-top: 12px;
  padding: 9px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

.toast {
  position: fixed;
  z-index: 20;
  right: 16px;
  bottom: calc(85px + env(safe-area-inset-bottom));
  left: 16px;
  padding: 12px 15px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: #24272d;
  font-size: 13px;
  text-align: center;
}

.hidden {
  display: none !important;
}

@media (min-width: 600px) {
  .app {
    border-right: 1px solid var(--border);
    border-left: 1px solid var(--border);
  }

  .conversation-page {
    height: calc(100dvh - 160px);
  }
}

/* WhatsApp-inspired mobile redesign */
:root {
  color-scheme: dark;
  --bg: #0b141a;
  --surface: #111b21;
  --surface-2: #202c33;
  --border: #26343d;
  --text: #e9edef;
  --muted: #8696a0;
  --green: #00a884;
  --green-dark: #005c4b;
}

html, body {
  min-height: 100%;
  background: var(--bg);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",
    Roboto, Helvetica, Arial, sans-serif;
}

.app {
  max-width: 760px;
  min-height: 100dvh;
  background: var(--bg);
}

.topbar {
  padding: 14px 16px;
  background: #202c33;
  border-bottom: 1px solid var(--border);
}

.brand-icon {
  border-radius: 50%;
  background: var(--green);
  color: #062e27;
}

.brand h1 {
  font-size: 18px;
  letter-spacing: 0;
}

.brand p,
.conversation-info p {
  color: #aebac1;
}

.icon-button {
  border: 0;
  border-radius: 50%;
  background: transparent;
}

.search-box {
  height: 42px;
  margin: 12px;
  border: 0;
  border-radius: 10px;
  background: #202c33;
}

.filter-row {
  padding: 0 12px 14px;
}

.filter {
  border: 1px solid #2a3942;
  border-radius: 20px;
  background: transparent;
  color: #b1bdc4;
}

.filter.active {
  border-color: #005c4b;
  background: #005c4b;
  color: #e9edef;
}

.section-heading h2 {
  color: #aebac1;
  font-weight: 500;
}

.chat-list {
  padding: 0;
}

.chat-item {
  gap: 13px;
  padding: 12px 16px;
  border-radius: 0;
  border-bottom: 1px solid rgba(134, 150, 160, .12);
}

.chat-item:active {
  background: #202c33;
}

.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #334b56;
  color: #e9edef;
}

.chat-name {
  font-size: 15px;
  font-weight: 500;
}

.chat-preview {
  color: #aebac1;
  font-size: 13px;
}

.chat-time {
  color: #8696a0;
}

.unread-dot {
  background: #00a884;
  color: #062e27;
}

.conversation-page {
  height: calc(100dvh - 74px - env(safe-area-inset-bottom));
  min-height: 360px;
  background: var(--bg);
}

.conversation-header {
  flex-shrink: 0;
  padding: 9px 10px;
  background: #202c33;
  border-bottom: 1px solid var(--border);
}

.conversation-header .avatar {
  width: 40px;
  height: 40px;
}

.conversation-info h2 {
  font-size: 15px;
  font-weight: 550;
}

.message-list {
  gap: 5px;
  padding: 16px 10px;
  background-color: #0b141a;
  background-image:
    radial-gradient(circle at 18px 20px,
      rgba(134,150,160,.055) 1px, transparent 1.5px),
    radial-gradient(circle at 70px 64px,
      rgba(134,150,160,.04) 1px, transparent 1.5px);
  background-size: 96px 96px, 120px 120px;
  overscroll-behavior: contain;
}

.message {
  max-width: 88%;
  padding: 7px 9px 5px;
  border: 0;
  border-radius: 0 9px 9px 9px;
  background: #202c33;
  color: #e9edef;
  font-size: 14px;
  line-height: 1.42;
  box-shadow: 0 1px 0 rgba(0,0,0,.15);
}

.message.outgoing {
  border: 0;
  border-radius: 9px 0 9px 9px;
  background: #005c4b;
}

.message-meta {
  margin-top: 3px;
  color: #aebac1;
  font-size: 10px;
}

.read-note {
  flex-shrink: 0;
  background: #111b21;
  border-top: 1px solid var(--border);
  color: #8696a0;
}

.bottom-nav {
  justify-content: space-around;
  gap: 0;
  background: rgba(17,27,33,.98);
  border-top: 1px solid var(--border);
}

.nav-item {
  color: #8696a0;
}

.nav-item.selected {
  color: #00a884;
}

.error-state button {
  border-color: #2a3942;
  border-radius: 20px;
  background: #202c33;
}

.toast {
  border-color: #2a3942;
  background: #23323a;
}

@media (min-width: 600px) {
  .conversation-page {
    height: calc(100dvh - 74px);
  }
}

/* WhatsApp-inspired Light Mode */
:root {
  color-scheme: light;
  --bg: #f7f9f5;
  --surface: #ffffff;
  --surface-2: #ffffff;
  --border: #e8ece8;
  --text: #243630;
  --muted: #73817b;
  --green: #075e54;
  --green-dark: #d9f2df;
}

html, body, .app {
  background: #fff;
  color: #243630;
}

.topbar {
  background: #075e54;
  color: white;
  border: 0;
}

.brand p { color: #c4e3dc; }
.brand-icon { background: #d9f2df; color: #075e54; }
.icon-button { color: inherit; }

.search-box {
  background: #f4f7f4;
  border: 1px solid #e0e7e1;
  border-radius: 28px;
}
.search-box input { color: #243630; }

.filter {
  background: #f0f4f0;
  color: #52665d;
  border-color: transparent;
}
.filter.active {
  background: #075e54;
  color: white;
  border-color: #075e54;
}

.section-heading h2 { color: #075e54; }
.chat-item { border-bottom: 1px solid #edf0ed; }
.chat-item:active { background: #f4f7f4; }

.avatar {
  background: #d9eee1;
  color: #176451;
}

.chat-name { color: #243630; }
.chat-preview, .chat-time { color: #73817b; }

.conversation-page { background: #f4f7f0; }
.conversation-header {
  background: #fff;
  color: #075e54;
  border-bottom: 1px solid #e8ece8;
}
.conversation-info h2 { color: #243630; }
.conversation-info p { color: #73817b; }

.message-list {
  background-color: #f4f7f0;
  background-image:
    radial-gradient(circle at 18px 20px,
      rgba(7,94,84,.07) 1px, transparent 1.5px),
    radial-gradient(circle at 70px 64px,
      rgba(7,94,84,.045) 1px, transparent 1.5px);
  background-size: 96px 96px, 120px 120px;
}

.message {
  background: #fff;
  color: #243630;
  border: 0;
  border-radius: 0 12px 12px 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,.08);
}
.message.outgoing {
  background: #d9f2df;
  color: #243630;
  border: 0;
  border-radius: 12px 0 12px 12px;
}
.message-meta { color: #73817b; }

.read-note {
  background: #fff;
  border-color: #e8ece8;
  color: #73817b;
}
.bottom-nav {
  background: rgba(255,255,255,.97);
  border-color: #e8ece8;
}
.nav-item { color: #73817b; }
.nav-item.selected { color: #075e54; }

.loading, .empty-state, .error-state { color: #73817b; }
.error-state button, .toast {
  background: #fff;
  color: #243630;
  border-color: #e0e7e1;
}

.save-contact-button {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border-radius: 50%;
  background: #f0f6f1;
  color: #075e54;
  font-size: 20px;
}
.save-contact-button[hidden] {
  display: none !important;
}

/* FIX: WhatsApp-like compact message bubbles */
.message-list {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  padding: 14px 10px 20px;
  overflow-y: auto;
  min-width: 0;
}

.message {
  display: block;
  flex: 0 1 auto;
  width: fit-content;
  max-width: min(82%, 420px);
  min-width: 0;
  padding: 7px 9px 5px;
  margin: 0;
  border: 0;
  border-radius: 0 9px 9px 9px;
  background: #ffffff;
  color: #243630;
  box-shadow: 0 1px 1px rgba(0, 0, 0, .08);
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  font-size: 14px;
  line-height: 1.4;
}

.message.outgoing {
  align-self: flex-end;
  width: fit-content;
  max-width: min(82%, 420px);
  min-width: 0;
  margin-left: auto;
  border: 0;
  border-radius: 9px 0 9px 9px;
  background: #d9f2df;
  color: #243630;
}

.message-meta {
  display: flex;
  justify-content: flex-end;
  margin-top: 3px;
  color: #73817b;
  font-size: 10px;
  line-height: 1.2;
}

.message > div:first-child {
  min-width: 0;
  overflow-wrap: anywhere;
}

.message > div[style*="font-size:10px"] {
  margin-bottom: 3px !important;
}

.conversation-page {
  min-width: 0;
}

@media (max-width: 480px) {
  .message {
    max-width: 86%;
  }

  .message.outgoing {
    max-width: 86%;
  }
}

/* FINAL FIX: compact WhatsApp message bubbles */
#messageList {
  display: flex !important;
  flex-direction: column !important;
  align-items: flex-start !important;
  gap: 8px !important;
  padding: 14px 10px 20px !important;
  box-sizing: border-box !important;
  overflow-y: auto !important;
}

#messageList > .message {
  display: block !important;
  position: static !important;
  flex: 0 1 auto !important;
  width: auto !important;
  min-width: 0 !important;
  max-width: 82% !important;
  height: auto !important;
  min-height: 0 !important;
  max-height: none !important;
  padding: 8px 9px 5px !important;
  margin: 0 !important;
  box-sizing: border-box !important;
  background: #fff !important;
  color: #243630 !important;
  border: 0 !important;
  border-radius: 0 9px 9px 9px !important;
  box-shadow: 0 1px 2px rgba(0,0,0,.08) !important;
  font-size: 14px !important;
  line-height: 1.4 !important;
  white-space: normal !important;
  overflow-wrap: anywhere !important;
  align-self: flex-start !important;
}

#messageList > .message.outgoing {
  align-self: flex-end !important;
  width: auto !important;
  max-width: 82% !important;
  min-width: 0 !important;
  margin-left: 0 !important;
  background: #d9f2df !important;
  color: #243630 !important;
  border-radius: 9px 0 9px 9px !important;
}

#messageList > .message > div {
  width: auto !important;
  height: auto !important;
  min-height: 0 !important;
  max-height: none !important;
  white-space: pre-wrap !important;
  overflow-wrap: anywhere !important;
}

#messageList > .message > .message-meta {
  display: flex !important;
  justify-content: flex-end !important;
  width: auto !important;
  height: auto !important;
  margin-top: 3px !important;
  color: #73817b !important;
  font-size: 10px !important;
  line-height: 1.2 !important;
}

#messageList > .message > div[style*="font-size:10px"] {
  margin-bottom: 4px !important;
}

@media (max-width: 480px) {
  #messageList > .message,
  #messageList > .message.outgoing {
    max-width: 86% !important;
  }
}
