:root {
  --brand: #008069;
  --brand-dark: #006e5a;
  --accent: #25d366;
  --bg: #f0f2f5;
  --panel: #ffffff;
  --chat-bg: #efeae2;
  --bubble-in: #ffffff;
  --bubble-out: #d9fdd3;
  --text: #111b21;
  --muted: #667781;
  --line: #e9edef;
  --hover: #f5f6f6;
  --active: #f0f2f5;
  --tick-read: #53bdeb;
  --danger: #d93025;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b141a;
    --panel: #111b21;
    --chat-bg: #0b141a;
    --bubble-in: #202c33;
    --bubble-out: #005c4b;
    --text: #e9edef;
    --muted: #8696a0;
    --line: #222d34;
    --hover: #202c33;
    --active: #2a3942;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--brand); }
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; color: var(--text); }

/* ---------- buttons & forms ---------- */
.btn {
  display: block; width: 100%;
  background: var(--brand); color: #fff;
  border: 0; border-radius: 24px;
  padding: 12px 18px; font-weight: 600;
}
.btn:hover { background: var(--brand-dark); }
.btn.small { display: inline-block; width: auto; padding: 8px 16px; font-size: 14px; }
.btn.ghost { background: transparent; color: var(--brand); border: 1px solid var(--brand); }
.btn.ghost:hover { background: rgba(0,128,105,.08); }
.link-btn { background: none; border: 0; color: var(--brand); padding: 0; }
.icon-btn {
  display: grid; place-items: center;
  width: 40px; height: 40px; border-radius: 50%;
  background: none; border: 0; color: var(--muted);
  text-decoration: none; flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }

.muted { color: var(--muted); }
.tiny { font-size: 12px; }
.alert { padding: 10px 12px; border-radius: 8px; margin: 0 0 12px; font-size: 14px; }
.alert-err { background: #fde8e7; color: var(--danger); }
.alert-ok { background: #e2f7ed; color: var(--brand-dark); }

/* ---------- auth pages ---------- */
.auth-body {
  min-height: 100%;
  background: linear-gradient(var(--brand) 0 220px, var(--bg) 220px);
  padding: 32px 16px;
}
.auth-card {
  max-width: 420px; margin: 0 auto;
  background: var(--panel); border-radius: 12px;
  padding: 28px 24px; box-shadow: 0 6px 24px rgba(0,0,0,.12);
}
.auth-logo { display: flex; align-items: center; gap: 10px; color: var(--brand); margin-bottom: 16px; }
.auth-logo h1 { font-size: 22px; margin: 0; }
.auth-card h2 { margin: 0 0 6px; font-size: 20px; }
.auth-card p.muted { margin: 0 0 18px; font-size: 14px; }
.auth-card label { display: block; font-size: 14px; color: var(--muted); margin-bottom: 14px; }
.auth-card label small { font-size: 12px; }
.auth-card input:not([type=hidden]) {
  display: block; width: 100%; margin-top: 6px;
  padding: 11px 12px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel); outline: none;
}
.auth-card input:focus { border-color: var(--brand); }
.otp-input {
  text-align: center; font-size: 28px !important;
  letter-spacing: 12px; margin-bottom: 16px !important;
}
.switch { text-align: center; font-size: 14px; margin: 18px 0 0; }
form.inline { text-align: center; margin-top: 14px; font-size: 14px; color: var(--muted); }

/* ---------- app layout ---------- */
.app-body { overflow: hidden; }
.app {
  display: grid; grid-template-columns: minmax(300px, 30%) 1fr;
  height: 100vh; height: 100dvh;
  max-width: 1600px; margin: 0 auto;
  background: var(--panel);
}
.bar {
  display: flex; align-items: center; gap: 10px;
  height: 60px; padding: 0 12px;
  background: var(--active); flex-shrink: 0;
}
.bar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.bar-title strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar-title small { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.avatar {
  position: relative; flex-shrink: 0;
  width: 42px; height: 42px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--brand); color: #fff; font-weight: 700; font-size: 17px;
}
.avatar .dot {
  position: absolute; right: 0; bottom: 0;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--panel);
}

/* sidebar */
.sidebar { display: flex; flex-direction: column; border-right: 1px solid var(--line); min-height: 0; }
.filter { padding: 8px 12px; border-bottom: 1px solid var(--line); }
.filter input {
  width: 100%; padding: 8px 14px; border: 0; border-radius: 8px;
  background: var(--active); outline: none;
}
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-item { display: flex; align-items: center; gap: 12px; padding: 10px 14px; cursor: pointer; }
.chat-item:hover { background: var(--hover); }
.chat-item.active { background: var(--active); }
.chat-mid { flex: 1; min-width: 0; border-bottom: 1px solid var(--line); padding-bottom: 10px; margin-bottom: -10px; }
.chat-item:last-child .chat-mid { border-bottom: 0; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.preview { color: var(--muted); font-size: 14px; }
.time { color: var(--muted); font-size: 12px; flex-shrink: 0; }
.time.unread { color: var(--accent); }
.badge {
  background: var(--accent); color: #fff; font-size: 12px; font-weight: 700;
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  display: grid; place-items: center; flex-shrink: 0;
}
.empty-list { padding: 32px 20px; text-align: center; color: var(--muted); }

/* conversation */
.convo { display: flex; flex-direction: column; min-height: 0; background: var(--chat-bg); }
.convo-empty {
  margin: auto; text-align: center; color: var(--muted); padding: 24px; max-width: 420px;
}
.convo-empty svg { color: var(--brand); opacity: .5; }
.convo-empty h2 { color: var(--text); font-weight: 400; }
.convo-inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.convo-inner[hidden], .convo-empty[hidden] { display: none; }
.back { display: none; }

.messages {
  flex: 1; overflow-y: auto; padding: 16px 6%;
  display: flex; flex-direction: column; gap: 3px;
}
.day-divider {
  align-self: center; margin: 10px 0;
  background: var(--panel); color: var(--muted);
  font-size: 12.5px; padding: 5px 12px; border-radius: 8px;
  box-shadow: 0 1px .5px rgba(0,0,0,.13);
}
.bubble {
  max-width: min(65%, 560px); padding: 6px 8px 8px 9px;
  border-radius: 8px; box-shadow: 0 1px .5px rgba(0,0,0,.13);
  font-size: 14.5px; line-height: 1.4;
  word-wrap: break-word; white-space: pre-wrap;
}
.bubble.in { align-self: flex-start; background: var(--bubble-in); border-top-left-radius: 0; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); border-top-right-radius: 0; }
.bubble .meta {
  float: right; margin: 6px 0 -4px 10px;
  font-size: 11px; color: var(--muted);
  display: inline-flex; gap: 3px; align-items: center;
}
.bubble .tick { letter-spacing: -3px; padding-right: 3px; }
.bubble.read .tick { color: var(--tick-read); }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 12px; background: var(--active);
  padding-bottom: max(8px, env(safe-area-inset-bottom));
}
.composer textarea {
  flex: 1; resize: none; border: 0; outline: none;
  border-radius: 8px; padding: 10px 14px; max-height: 120px;
  background: var(--panel); line-height: 1.35;
}
.send-btn {
  width: 44px; height: 44px; border-radius: 50%; border: 0;
  background: var(--brand); color: #fff; display: grid; place-items: center; flex-shrink: 0;
}
.send-btn svg { width: 22px; height: 22px; fill: currentColor; }

/* dialog */
dialog {
  width: min(420px, calc(100% - 32px)); border: 0; border-radius: 12px;
  padding: 16px; background: var(--panel); color: var(--text);
  box-shadow: 0 10px 40px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0,0,0,.4); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.dlg-search { display: flex; gap: 8px; }
.dlg-search input {
  flex: 1; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 8px; outline: none; background: var(--panel); min-width: 0;
}
.dlg-search input:focus { border-color: var(--brand); }
.result-card {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  padding: 12px; border: 1px solid var(--line); border-radius: 10px; margin-top: 8px;
}
.result-card .chat-mid { border: 0; margin: 0; padding: 0; }
.result-actions { display: flex; gap: 8px; width: 100%; justify-content: flex-end; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: #323232; color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 14px; z-index: 50;
}

/* ---------- mobile: one pane at a time ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; }
  .sidebar > .bar { background: var(--brand); color: #fff; }
  .sidebar > .bar .icon-btn, .sidebar > .bar small { color: rgba(255,255,255,.85); }
  .sidebar > .bar .avatar { background: rgba(255,255,255,.2) !important; }
  .convo { display: none; }
  .app.show-convo .sidebar { display: none; }
  .app.show-convo .convo { display: flex; }
  .convo .bar { background: var(--brand); color: #fff; padding-left: 4px; }
  .convo .bar small, .convo .bar .icon-btn { color: rgba(255,255,255,.85); }
  .convo .bar .btn.ghost { color: #fff; border-color: rgba(255,255,255,.7); }
  .back { display: grid; }
  .messages { padding: 12px 10px; }
  .bubble { max-width: 82%; }
}
