:root {
  --bg: #ffffff;
  --chat-bg: #ECE5DD;
  --surface: #f6f6f6;
  --header: #075E54;
  --header-text: #ffffff;
  --primary: #25D366;
  --primary-dark: #128C7E;
  --text: #111b21;
  --text-secondary: #667781;
  --border: #e9edef;
  --bubble-mine: #D9FDD3;
  --bubble-theirs: #ffffff;
  --danger: #e23b3b;

  --header-h: calc(56px + env(safe-area-inset-top));
  --bottom-pad: env(safe-area-inset-bottom);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body, #root { height: 100%; overflow: hidden; -webkit-tap-highlight-color: transparent; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  overscroll-behavior: none;
}

input, button, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
img { display: block; max-width: 100%; }

/* ---- Loader ---- */
.loader-screen {
  height: 100%; display: flex; align-items: center; justify-content: center;
  background: var(--header);
}
.loader-dot {
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--primary);
  animation: pulse 1s ease-in-out infinite;
}
@keyframes pulse { 0%,100% { transform: scale(.6); opacity: .5 } 50% { transform: scale(1.1); opacity: 1 } }

/* ---- Header ---- */
.header {
  height: var(--header-h);
  padding-top: env(safe-area-inset-top);
  background: var(--header);
  color: var(--header-text);
  display: flex; align-items: center; padding-left: 12px; padding-right: 12px;
  gap: 10px;
  flex-shrink: 0;
}
.header-back {
  width: 36px; height: 36px; display: flex; align-items: center; justify-content: center;
  font-size: 24px; color: var(--header-text);
}
.header-title { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 0; }
.header-title h1 {
  font-size: 18px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.header-actions { display: flex; gap: 4px; }
.header-actions button { color: var(--header-text); padding: 8px 12px; font-size: 14px; }

/* ---- Avatar ---- */
.avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--surface);
  display: flex; align-items: center; justify-content: center;
  overflow: hidden; flex-shrink: 0;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-emoji { font-size: 22px; }
.avatar.s32 { width: 32px; height: 32px; } .avatar.s32 .avatar-emoji { font-size: 18px; }
.avatar.s52 { width: 52px; height: 52px; } .avatar.s52 .avatar-emoji { font-size: 26px; }
.avatar.s130 { width: 130px; height: 130px; } .avatar.s130 .avatar-emoji { font-size: 56px; }

/* ---- Screen container ---- */
.screen { height: 100%; display: flex; flex-direction: column; background: var(--bg); }

/* ---- Login ---- */
.login {
  height: 100%; display: flex; flex-direction: column; justify-content: center; padding: 24px;
  background: var(--bg);
}
.login h1 { font-size: 28px; margin-bottom: 8px; }
.login p.sub { color: var(--text-secondary); margin-bottom: 24px; }
.login label { display: block; font-size: 13px; color: var(--primary-dark); font-weight: 500; margin: 16px 0 6px; }
.login input {
  width: 100%; border: none; border-bottom: 1px solid var(--border);
  padding: 10px 2px; font-size: 17px; background: transparent; outline: none;
}
.login input:focus { border-bottom-color: var(--primary); }
.login button.primary {
  background: var(--primary); color: #fff; padding: 14px; border-radius: 28px;
  width: 100%; margin-top: 32px; font-size: 16px; font-weight: 600;
}
.login button.primary:disabled { opacity: .4 }
.login .logo { font-size: 12px; letter-spacing: 4px; color: var(--primary); text-align: center; text-transform: uppercase; margin-bottom: 48px; }
.login .err { color: var(--danger); font-size: 13px; margin-top: 12px; }

/* ---- Chat list ---- */
.list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.list-item {
  display: flex; align-items: center; padding: 10px 16px; gap: 14px; cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.list-item:active { background: var(--surface); }
.list-item .body { flex: 1; min-width: 0; }
.list-item .row1 { display: flex; justify-content: space-between; align-items: center; }
.list-item .name { font-size: 17px; font-weight: 500; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .time { font-size: 12px; color: var(--text-secondary); margin-left: 8px; flex-shrink: 0; }
.list-item .preview { font-size: 14px; color: var(--text-secondary); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-empty { padding: 80px 32px 0; text-align: center; color: var(--text-secondary); }

/* FAB */
.fab {
  position: absolute; right: 20px; bottom: calc(20px + env(safe-area-inset-bottom));
  width: 56px; height: 56px; border-radius: 28px; background: var(--primary);
  color: #fff; font-size: 28px; line-height: 1; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 8px rgba(0,0,0,.25);
}

/* ---- Chat ---- */
.chat { background: var(--chat-bg); flex: 1; display: flex; flex-direction: column; min-height: 0; }
.messages { flex: 1; overflow-y: auto; padding: 8px 10px; -webkit-overflow-scrolling: touch; }
.bubble-wrap { display: flex; margin-bottom: 4px; }
.bubble-wrap.mine { justify-content: flex-end; }
.bubble {
  max-width: 78%; padding: 6px 9px; border-radius: 8px;
  box-shadow: 0 1px 1px rgba(0,0,0,.08);
  word-wrap: break-word; overflow-wrap: anywhere;
}
.bubble.mine { background: var(--bubble-mine); border-top-right-radius: 0; }
.bubble.theirs { background: var(--bubble-theirs); border-top-left-radius: 0; }
.bubble .text { font-size: 15.5px; line-height: 1.4; }
.bubble .time { font-size: 10.5px; color: var(--text-secondary); margin-left: 6px; }
.bubble .text-row { display: flex; align-items: flex-end; flex-wrap: wrap; gap: 0 4px; }
.bubble .media { display: block; max-width: 260px; max-height: 320px; border-radius: 6px; margin-bottom: 4px; }
.bubble video.media { max-width: 260px; max-height: 320px; background: #000; }

.input-bar {
  display: flex; align-items: flex-end; gap: 4px; padding: 6px 6px calc(6px + var(--bottom-pad));
  background: var(--chat-bg);
  flex-shrink: 0;
}
.input-bar .attach, .input-bar .send {
  width: 44px; height: 44px; display: flex; align-items: center; justify-content: center;
  border-radius: 22px;
}
.input-bar .attach { font-size: 20px; color: var(--text-secondary); }
.input-bar .send { background: var(--primary); color: #fff; font-size: 18px; }
.input-bar .send:disabled { opacity: .4 }
.input-bar .field {
  flex: 1; background: #fff; border-radius: 22px; padding: 8px 14px; min-height: 40px;
  display: flex; align-items: center;
}
.input-bar textarea {
  width: 100%; resize: none; border: none; outline: none; background: transparent;
  font-size: 16px; line-height: 1.3; max-height: 100px;
}

/* ---- Settings / Profile ---- */
.settings { padding: 24px; overflow-y: auto; }
.settings .avatar-edit {
  position: relative; width: 130px; height: 130px; margin: 0 auto;
  border-radius: 65px; background: var(--surface);
  display: flex; align-items: center; justify-content: center; overflow: visible;
}
.settings .avatar-edit img { width: 130px; height: 130px; border-radius: 65px; object-fit: cover; }
.settings .avatar-edit .pencil {
  position: absolute; right: 6px; bottom: 6px;
  width: 36px; height: 36px; border-radius: 18px; background: var(--primary);
  color: #fff; font-size: 16px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  border: 3px solid #fff;
}
.settings .hint { text-align: center; color: var(--text-secondary); font-size: 12px; margin: 12px 0 24px; }
.settings label { display: block; font-size: 13px; color: var(--primary-dark); font-weight: 500; margin: 16px 0 6px; }
.settings input {
  width: 100%; border: none; border-bottom: 1px solid var(--border);
  padding: 10px 2px; font-size: 17px; background: transparent; outline: none;
}
.settings input:focus { border-bottom-color: var(--primary); }
.settings input[readonly] { color: var(--text-secondary); }
.settings .save {
  background: var(--primary); color: #fff; padding: 14px; border-radius: 28px;
  width: 100%; margin-top: 32px; font-size: 16px; font-weight: 600;
}
.settings .save:disabled { opacity: .4 }
.settings .logout {
  color: var(--danger); font-size: 16px; font-weight: 500;
  width: 100%; padding: 16px; margin-top: 16px; text-align: center;
}
.settings .push-row {
  margin-top: 24px; padding: 14px; background: var(--surface); border-radius: 12px;
  display: flex; justify-content: space-between; align-items: center;
}
.settings .push-row .desc { flex: 1; padding-right: 10px; }
.settings .push-row .title { font-weight: 500; }
.settings .push-row .sub { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
.settings .push-row .btn {
  background: var(--primary); color: #fff; padding: 8px 16px; border-radius: 16px;
  font-size: 13px; font-weight: 500;
}
.settings .push-row .btn.enabled { background: var(--surface); color: var(--text-secondary); }

/* ---- New chat search ---- */
.search { padding: 16px; }
.search input {
  width: 100%; border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; font-size: 16px; outline: none;
}
.search input:focus { border-color: var(--primary); }
.search-results { padding: 0 16px; }
.search-results .item { display: flex; align-items: center; gap: 12px; padding: 10px 0; cursor: pointer; }
.search-results .item:active { opacity: .6; }
.search-results .item .body { flex: 1; }
.search-results .item .name { font-size: 16px; font-weight: 500; }
.search-results .item .handle { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }
.search-results .empty { padding: 40px 0; text-align: center; color: var(--text-secondary); }
.search-results .section-title {
  padding: 14px 0 6px; font-size: 12px; text-transform: uppercase;
  color: var(--primary-dark); letter-spacing: .5px; font-weight: 500;
}
.search .contacts-btn {
  margin-top: 10px; width: 100%; padding: 10px;
  background: var(--surface); color: var(--text); border-radius: 12px;
  font-size: 14px;
}
