/* ============================================================
   MessageFunnel — unified inbox
   Plain CSS, no framework. Mobile-covered per website law:
   16px inputs, >=44px touch targets, verified layout @390px.
   ============================================================ */

:root {
  --bg: #eef1f7;
  --panel: #ffffff;
  --panel-2: #f6f8fc;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --danger: #ef4444;
  --bubble-in: #eef2f7;
  --bubble-in-text: #0f172a;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px rgba(15, 23, 42, .07);
  --radius: 14px;
}

[data-theme="dark"] {
  --bg: #0b1220;
  --panel: #121a2b;
  --panel-2: #0f1626;
  --text: #e2e8f0;
  --muted: #8ea0b5;
  --line: #22304a;
  --accent: #818cf8;
  --accent-soft: #1c2440;
  --bubble-in: #1b2537;
  --bubble-in-text: #e2e8f0;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px; /* prevents iOS focus zoom */
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; color: inherit; }

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  background: linear-gradient(90deg, var(--accent), #7c3aed 60%, #2563eb);
  color: #fff;
  flex: 0 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  letter-spacing: .2px;
}

.brand-glyph {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: rgba(255, 255, 255, .18);
}

.brand-name b { font-weight: 800; }
.brand-name { font-weight: 400; opacity: .95; }

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  padding: 5px 11px;
  border-radius: 999px;
  background: rgba(255, 255, 255, .16);
  border: 1px solid rgba(255, 255, 255, .28);
  white-space: nowrap;
}

.status-pill .dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #fbbf24;
}

.icon-btn {
  width: 44px;           /* touch-target law */
  height: 44px;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, .14);
  cursor: pointer;
  font-size: 17px;
}
.icon-btn:hover { background: rgba(255, 255, 255, .26); }

.demo-note {
  flex: 0 0 auto;
  text-align: center;
  font-size: 12.5px;
  color: var(--muted);
  background: var(--panel-2);
  border-bottom: 1px solid var(--line);
  padding: 6px 12px;
}

/* ---------- Layout ---------- */

.layout {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 84px 340px 1fr;
  gap: 0;
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
}

.pane {
  min-height: 0;
  background: var(--panel);
}

.list-pane {
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}

.thread-pane {
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

/* ---------- Platform rail ---------- */

.rail {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 14px 8px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
}

.rail-item {
  position: relative;
  width: 56px;
  border: none;
  background: transparent;
  border-radius: 14px;
  padding: 9px 4px 7px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  transition: background .15s ease, color .15s ease;
}

.rail-item:hover { background: var(--panel-2); }

.rail-item.active {
  background: var(--accent-soft);
  color: var(--text);
}

.rail-item .rail-label {
  font-size: 10.5px;
  line-height: 1;
  max-width: 60px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.rail-item .p-icon {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
}

.rail-item.all .p-icon { background: var(--text); }

.rail-count {
  position: absolute;
  top: 2px;
  right: 4px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--danger);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: grid;
  place-items: center;
  border: 2px solid var(--panel);
}

/* ---------- Conversation list ---------- */

.list-head {
  padding: 12px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

#searchBox {
  width: 100%;
  min-height: 44px;      /* touch target */
  font-size: 16px;       /* no-zoom law */
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  outline: none;
}
#searchBox:focus { border-color: var(--accent); }

.convo-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  overflow-y: auto;
  flex: 1 1 auto;
}

.convo-item {
  display: flex;
  gap: 12px;
  align-items: center;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  border-radius: var(--radius);
  padding: 11px 10px;
  cursor: pointer;
}
.convo-item:hover { background: var(--panel-2); }
.convo-item.active { background: var(--accent-soft); }

.avatar {
  position: relative;
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 16px;
  color: #fff;
}

.avatar .p-badge {
  position: absolute;
  right: -3px;
  bottom: -3px;
  width: 19px;
  height: 19px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  border: 2px solid var(--panel);
}
.avatar .p-badge svg { width: 11px; height: 11px; }

.convo-main {
  flex: 1 1 auto;
  min-width: 0;
}

.convo-row1 {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.convo-name {
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.convo-time {
  flex: 0 0 auto;
  font-size: 12px;
  color: var(--muted);
}

.convo-preview {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 3px;
}

.convo-snippet {
  color: var(--muted);
  font-size: 13.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.convo-item.unread .convo-snippet { color: var(--text); font-weight: 550; }

.unread-dot {
  flex: 0 0 auto;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 11.5px;
  font-weight: 700;
  display: grid;
  place-items: center;
}

.empty-results {
  text-align: center;
  color: var(--muted);
  padding: 32px 12px;
  font-size: 14px;
}

/* ---------- Thread pane ---------- */

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
  padding: 20px;
}
.empty-art { font-size: 44px; margin-bottom: 8px; }
.empty-sub { font-size: 13px; opacity: .8; }

.thread {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  background: var(--bg);
}

.thread-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.back-btn {
  display: none; /* mobile only */
  width: 44px;
  height: 44px;
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 10px;
  cursor: pointer;
  font-size: 18px;
}

.thread-who { display: flex; align-items: center; gap: 10px; min-width: 0; }
.thread-who .avatar { width: 40px; height: 40px; font-size: 14px; }
.thread-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-platform { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 5px; }
.thread-platform svg { width: 13px; height: 13px; }

.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 18px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.day-sep {
  text-align: center;
  color: var(--muted);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .6px;
  margin: 14px 0 10px;
}

.msg {
  max-width: min(72%, 520px);
  padding: 9px 13px;
  border-radius: 16px;
  line-height: 1.45;
  font-size: 15px;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.msg.them {
  align-self: flex-start;
  background: var(--bubble-in);
  color: var(--bubble-in-text);
  border-bottom-left-radius: 5px;
}

.msg.me {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  color: #fff;
  border-bottom-right-radius: 5px;
}

.msg-time {
  align-self: center;
  font-size: 10.5px;
  color: var(--muted);
  margin: 6px 0 2px;
  display: none;
}

.typing {
  align-self: flex-start;
  background: var(--bubble-in);
  border-radius: 16px;
  border-bottom-left-radius: 5px;
  padding: 12px 16px;
  display: flex;
  gap: 4px;
}
.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: blink 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.composer {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
}

.composer input {
  flex: 1 1 auto;
  min-height: 46px;
  font-size: 16px;       /* no-zoom law */
  padding: 10px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  outline: none;
}
.composer input:focus { border-color: var(--accent); }

.send-btn {
  flex: 0 0 auto;
  width: 46px;
  height: 46px;
  border: none;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), #7c3aed);
  color: #fff;
  font-size: 17px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.send-btn:hover { filter: brightness(1.08); }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Tablet: rail collapses to a horizontal strip ---------- */

@media (max-width: 960px) {
  .layout { grid-template-columns: 300px 1fr; }
  .rail {
    grid-column: 1 / 3;
    grid-row: 1;
    flex-direction: row;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
    padding: 8px 10px;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }
  .list-pane { grid-column: 1; grid-row: 2; }
  .thread-pane { grid-column: 2; grid-row: 2; }
  .rail-item { flex: 0 0 auto; width: 64px; }
}

/* ---------- Phone (@390px verified): one pane at a time ---------- */

@media (max-width: 640px) {
  .topbar { padding: 8px 10px; }
  .brand { font-size: 16px; }
  .status-pill { display: none; }   /* save space on phones */

  .layout { display: block; position: relative; }

  .rail {
    display: flex;
    border-bottom: 1px solid var(--line);
    padding: 6px 8px;
    gap: 6px;
  }
  .rail-item { width: 58px; padding: 7px 4px 6px; }

  .list-pane { border-right: none; height: auto; }

  .thread-pane {
    position: fixed;
    inset: 0;
    z-index: 30;
    display: none;            /* hidden until a thread opens */
    background: var(--bg);
  }
  body.thread-open .thread-pane { display: flex; }
  .back-btn { display: grid; place-items: center; }

  .messages { padding-bottom: 12px; }
  .msg { max-width: 82%; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* ============================================================
   Auth gate + connected-accounts panel (live mode)
   ============================================================ */
#mf-auth {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  background: rgba(10, 12, 18, .55); backdrop-filter: blur(6px);
  padding: 16px;
}
.mf-auth-card {
  width: min(360px, 100%); background: var(--bg-panel, #fff);
  border-radius: 14px; padding: 22px 20px;
  box-shadow: 0 18px 50px rgba(0,0,0,.35);
}
.mf-auth-sub { margin: 2px 0 12px; opacity: .65; font-size: .9rem; }
.mf-auth-msg {
  background: rgba(220, 60, 60, .12); color: #c0392b;
  border-radius: 8px; padding: 8px 10px; margin: 8px 0; font-size: .85rem;
}
.mf-tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.mf-tabs button {
  flex: 1; padding: 8px; border-radius: 8px; cursor: pointer; font-weight: 600;
  border: none; background: transparent; color: inherit; opacity: .6;
}
.mf-tabs button.active { background: var(--bg-rail, #eee); opacity: 1; }
.mf-auth-card form { display: flex; flex-direction: column; gap: 10px; }
.mf-auth-card input {
  padding: 11px 12px; border-radius: 9px; font-size: 16px;
  border: 1px solid var(--border, #ccc); background: transparent; color: inherit;
}
.mf-primary {
  padding: 11px; border-radius: 9px; border: none; cursor: pointer;
  font-weight: 700; color: #fff; background: linear-gradient(135deg, #4f7cff, #7a5cff);
}
button.mf-primary { width: 100%; }
.mf-danger {
  padding: 8px 12px; border-radius: 8px; cursor: pointer; font-weight: 600;
  border: 1px solid rgba(200, 60, 60, .4); color: #c0392b; background: transparent;
}

#mf-settings {
  position: fixed; inset: 0; z-index: 900;
  display: flex; align-items: center; justify-content: center;
  background: rgba(10, 12, 18, .55); backdrop-filter: blur(4px);
  padding: 16px;
}
.mf-settings-card {
  width: min(560px, 100%); max-height: 88vh; overflow-y: auto;
  background: var(--bg-panel, #fff); border-radius: 14px; padding: 18px 20px;
  box-shadow: 0 18px 50px rgba(0,0,0,.35);
}
.mf-settings-card header { display: flex; align-items: center; gap: 10px; }
.mf-settings-card h2 { font-size: 1.05rem; margin: 0; flex: 1; }
.mf-settings-user { opacity: .55; font-size: .82rem; margin-right: 4px; }
.mf-settings-sub { opacity: .6; font-size: .84rem; margin: 6px 0 14px; }
.mf-conn-list { display: flex; flex-direction: column; gap: 12px; }
.mf-conn {
  border: 1px solid var(--border, #ddd); border-radius: 12px; padding: 12px 14px;
}
.mf-conn.connected { border-color: rgba(40, 167, 90, .45); }
.mf-conn header { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.mf-status { margin-left: auto; font-size: .78rem; opacity: .65; }
.mf-conn.connected .mf-status { color: #28a75a; opacity: 1; }
.mf-conn-form { display: flex; flex-direction: column; gap: 8px; }
.mf-conn-form label {
  display: flex; flex-direction: column; gap: 3px; font-size: .78rem; opacity: .85;
}
.mf-conn-form input {
  padding: 9px 10px; border-radius: 8px; font-size: 16px;
  border: 1px solid var(--border, #ccc); background: transparent; color: inherit;
}
.mf-conn-actions { display: flex; gap: 8px; margin-top: 4px; }
.mf-conn-actions .mf-primary { width: auto; padding: 9px 16px; }
.mf-test {
  padding: 9px 14px; border-radius: 8px; cursor: pointer; font-weight: 600;
  border: 1px solid var(--border, #bbb); background: transparent; color: inherit;
}
.mf-result { font-size: .82rem; margin: 6px 0 0; }
.mf-masked { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 6px 0; font-size: .78rem; }
.mf-masked code { opacity: .6; }
.mf-error, .mf-loading { opacity: .6; padding: 8px 0; }
