:root {
  --cream: #F8F6F3;
  --surface: #FFFFFF;
  --ink: #211D1B;
  --muted: #857C76;
  --hairline: #E5DFD8;
  --crimson: #C0392B;
  --accent: #C0392B;          /* overwritten per active account */
  --accent-wash: #FBEDEB;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--cream);
  color: var(--ink);
  font-family: 'DM Sans', system-ui, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}

button { font: inherit; cursor: pointer; }

.wordmark {
  font-family: 'Playfair Display', Georgia, serif;
  font-weight: 600;
  font-size: 1.5rem;
  letter-spacing: -0.01em;
  margin: 0;
}

/* ---------- Sign in ---------- */
.signin {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.signin-card { width: 100%; max-width: 340px; }
.signin-note { color: var(--muted); margin: 6px 0 28px; }
.field { display: block; margin-bottom: 16px; }
.field span { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%;
  padding: 13px 14px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  background: var(--surface);
  font: inherit;
  color: var(--ink);
}
.field input:focus-visible { outline: 2px solid var(--crimson); outline-offset: 1px; }
.btn-primary {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: var(--radius);
  background: var(--crimson);
  color: #fff;
  font-weight: 500;
}
.error { color: var(--crimson); font-size: 0.9rem; margin-top: 12px; }

/* ---------- Inbox shell ---------- */
.app { padding-bottom: 32px; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: env(safe-area-inset-top) 16px 10px;
  padding-top: calc(env(safe-area-inset-top) + 14px);
  background: var(--cream);
}
.icon-btn {
  border: none;
  background: none;
  color: var(--muted);
  font-size: 1.5rem;
  line-height: 1;
  padding: 6px 10px;
  border-radius: 10px;
}
.icon-btn:focus-visible { outline: 2px solid var(--accent); }

/* Account strip — the thing that tells you who you are replying as. */
.accounts {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 4px 16px 14px;
  scrollbar-width: none;
}
.accounts::-webkit-scrollbar { display: none; }

.account-chip {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 14px;
  border: 1px solid var(--hairline);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  white-space: nowrap;
}
.account-chip .dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  background: currentColor;
}
.account-chip[aria-selected="true"] {
  background: var(--chip);
  border-color: var(--chip);
  color: #fff;
  font-weight: 500;
}
.account-chip .count {
  min-width: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--crimson);
  color: #fff;
  font-size: 0.75rem;
  line-height: 20px;
  text-align: center;
}
.account-chip[aria-selected="true"] .count { background: rgba(255,255,255,.28); }
.account-chip.offboarded { opacity: .55; text-decoration: line-through; }

.warning {
  margin: 0 16px 12px;
  padding: 12px 14px;
  border-left: 3px solid var(--crimson);
  background: var(--accent-wash);
  border-radius: 0 10px 10px 0;
  font-size: 0.9rem;
}

/* ---------- Conversation list ---------- */
.threads { list-style: none; margin: 0; padding: 0 12px; }

.thread-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  width: 100%;
  text-align: left;
  padding: 14px;
  margin-bottom: 6px;
  border: none;
  border-radius: var(--radius);
  background: var(--surface);
  border-left: 3px solid var(--accent);
}
.thread-row:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.thread-row .body { flex: 1; min-width: 0; }
.thread-row .name { display: block; font-weight: 500; margin-bottom: 3px; }
.thread-row .preview {
  display: block;
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.thread-row .meta { text-align: right; flex: 0 0 auto; }
.thread-row time { display: block; font-size: 0.75rem; color: var(--muted); }
.thread-row .count {
  display: inline-block;
  margin-top: 6px;
  min-width: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  line-height: 20px;
  text-align: center;
}
.thread-row.unread .name { font-weight: 700; }

.empty { color: var(--muted); padding: 40px 28px; text-align: center; line-height: 1.6; }

/* ---------- Thread view ---------- */
.thread {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  background: var(--cream);
}
.thread-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: calc(env(safe-area-inset-top) + 12px) 14px 12px;
  border-bottom: 1px solid var(--hairline);
  background: var(--surface);
}
.thread-who { display: flex; flex-direction: column; line-height: 1.3; min-width: 0; }
.thread-who strong { font-weight: 500; }
.thread-who span { font-size: 0.8rem; color: var(--accent); font-weight: 500; }

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

.bubble {
  max-width: 78%;
  padding: 10px 13px;
  border-radius: 16px;
  line-height: 1.45;
  word-wrap: break-word;
  white-space: pre-wrap;
}
.bubble.in {
  align-self: flex-start;
  background: var(--surface);
  border-bottom-left-radius: 5px;
}
.bubble.out {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 5px;
}
.bubble .stamp {
  display: block;
  margin-top: 5px;
  font-size: 0.7rem;
  opacity: .7;
}
/* Where a reply came from matters when phone and platform share a number. */
.bubble.from-app .stamp::before { content: '📱 phone · '; }
.bubble.from-history { opacity: .82; }

.composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 10px 12px calc(env(safe-area-inset-bottom) + 10px);
  border-top: 1px solid var(--hairline);
  background: var(--surface);
}
.attach {
  flex: 0 0 auto;
  width: 40px; height: 40px;
  font-size: 1.6rem;
  color: var(--accent);
  border: 1px solid var(--hairline);
  border-radius: 50%;
  background: var(--cream);
  padding: 0;
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 120px;
  padding: 11px 14px;
  border: 1px solid var(--hairline);
  border-radius: 20px;
  font: inherit;
  color: var(--ink);
  background: var(--cream);
}
.bubble img, .bubble video {
  display: block;
  max-width: 100%;
  border-radius: 10px;
  margin-bottom: 6px;
  background: var(--hairline);
}
.bubble audio { width: 100%; margin-bottom: 6px; }
.bubble .file-link {
  display: inline-block;
  padding: 8px 12px;
  margin-bottom: 6px;
  border-radius: 10px;
  background: rgba(0,0,0,.06);
  color: inherit;
  text-decoration: none;
  font-size: 0.9rem;
}
.bubble.out .file-link { background: rgba(255,255,255,.2); }
.bubble .media-pending { font-size: 0.85rem; opacity: .7; }

.upload-bar {
  padding: 10px 14px;
  background: var(--accent-wash);
  border-top: 1px solid var(--hairline);
  font-size: 0.85rem;
}

.composer textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
.send-btn {
  flex: 0 0 auto;
  width: 44px; height: 44px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 1.1rem;
}
.send-btn:disabled { opacity: .45; }

.window-closed {
  padding: 16px;
  background: var(--accent-wash);
  border-top: 1px solid var(--hairline);
  color: var(--ink);
  font-size: 0.9rem;
  line-height: 1.5;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 22px);
  transform: translateX(-50%);
  z-index: 40;
  max-width: 88vw;
  padding: 12px 18px;
  border-radius: 12px;
  background: var(--ink);
  color: var(--cream);
  font-size: 0.9rem;
}

@media (min-width: 720px) {
  .app { max-width: 640px; margin: 0 auto; }
  .thread { max-width: 640px; margin: 0 auto; border-left: 1px solid var(--hairline); border-right: 1px solid var(--hairline); }
}

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