:root {
  --bg: #F3F5F8;
  --surface: #FFFFFF;
  --ink: #17212B;
  --muted: #5F6B78;
  --line: #DCE1E7;
  --mine: #1D3B53;
  --mine-ink: #FFFFFF;
  --agent-bubble: #E6ECF2;
  --signal: #B8741A;
  --danger: #A33A2F;
  --radius-bubble: 18px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10161D;
    --surface: #18212B;
    --ink: #E6EBF0;
    --muted: #93A0AD;
    --line: #263241;
    --mine: #3E7CA8;
    --mine-ink: #FFFFFF;
    --agent-bubble: #1F2B37;
    --signal: #E0A54A;
    --danger: #E07A6E;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
}

#app {
  height: 100%;
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: var(--bg);
}

a { color: inherit; }

button, input, textarea { font: inherit; color: inherit; }

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

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

/* ---------- buttons ---------- */

.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 0.6rem 1.1rem;
  background: var(--mine);
  color: var(--mine-ink);
  font-weight: 600;
  cursor: pointer;
}
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.quiet { background: transparent; color: var(--mine); padding: 0.5rem 0.75rem; }
.btn.small { padding: 0.4rem 0.85rem; font-size: 0.9rem; }

@media (prefers-color-scheme: dark) {
  .btn.quiet { color: #8DBDE0; }
}

/* ---------- top bar ---------- */

.bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: calc(env(safe-area-inset-top) + 0.75rem) 1rem 0.75rem;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 2;
}
.bar h1 { font-size: 1.35rem; margin: 0; flex: 1; letter-spacing: -0.01em; }
.bar .who { flex: 1; min-width: 0; }
.bar .who strong { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar .who small { color: var(--muted); font-size: 0.8rem; }
.bar.wrap { flex-wrap: wrap; row-gap: 0.15rem; }
.bar .account {
  flex-basis: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  min-width: 0;
  color: var(--muted);
  font-size: 0.85rem;
}
.bar .account span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar .account .btn { flex: none; margin-right: -0.85rem; }
.back { text-decoration: none; font-size: 1.6rem; line-height: 1; padding: 0 0.4rem 0.15rem 0; color: var(--mine); }

/* ---------- avatars: round for people, notched square for agents ---------- */

.avatar {
  flex: none;
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.95rem;
  border-radius: 50%;
  background: var(--line);
  color: var(--ink);
}
.avatar.agent {
  border-radius: 12px 12px 12px 3px;
  background: var(--mine);
  color: var(--mine-ink);
}
.bar .avatar { width: 36px; height: 36px; font-size: 0.85rem; }

/* ---------- notices ---------- */

.notice {
  margin: 0.75rem 1rem 0;
  padding: 0.85rem 1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  display: flex;
  gap: 0.75rem;
  align-items: center;
  justify-content: space-between;
  font-size: 0.95rem;
}
.notice p { margin: 0; }
.notice.error { border-color: var(--danger); color: var(--danger); }

/* ---------- invite panel ---------- */

.invite-panel {
  margin: 0.75rem 1rem 0;
  padding: 1rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
}
.invite-panel p { margin: 0 0 0.6rem; }
.invite-panel input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  font-size: 0.9rem;
}
.invite-panel .row { display: flex; gap: 0.5rem; margin-top: 0.6rem; flex-wrap: wrap; }
.hint { color: var(--muted); font-size: 0.85rem; }

/* ---------- chat list ---------- */

.chats { list-style: none; margin: 0.5rem 0 0; padding: 0; }
.chats a {
  display: flex;
  gap: 0.85rem;
  align-items: center;
  padding: 0.75rem 1rem;
  text-decoration: none;
}
.chats a:hover { background: var(--surface); }
.chats .text { flex: 1; min-width: 0; }
.chats .top { display: flex; gap: 0.5rem; align-items: baseline; }
.chats .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chats .kind { color: var(--muted); font-size: 0.8rem; }
.chats time { margin-left: auto; color: var(--muted); font-size: 0.8rem; flex: none; }
.chats .preview {
  color: var(--muted);
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chats .unread .preview { color: var(--ink); font-weight: 600; }
.badge {
  flex: none;
  min-width: 1.4rem;
  height: 1.4rem;
  padding: 0 0.4rem;
  border-radius: 999px;
  background: var(--signal);
  color: #fff;
  font-size: 0.78rem;
  font-weight: 700;
  display: grid;
  place-items: center;
}

.empty {
  padding: 3rem 1.5rem;
  text-align: center;
  color: var(--muted);
  max-width: 34ch;
  margin: 0 auto;
}

/* ---------- thread ---------- */

.thread {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  overscroll-behavior: contain;
}
.msg {
  max-width: 80%;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}
.msg .bubble {
  padding: 0.55rem 0.85rem;
  border-radius: var(--radius-bubble);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.theirs { align-self: flex-start; }
.msg.theirs .bubble { background: var(--surface); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg.theirs.from-agent .bubble { background: var(--agent-bubble); border-color: transparent; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.mine .bubble { background: var(--mine); color: var(--mine-ink); border-bottom-right-radius: 6px; }
.msg img {
  display: block;
  max-width: min(260px, 100%);
  max-height: 340px;
  border-radius: 14px;
  object-fit: cover;
  background: var(--line);
}
.msg time, .msg .seen { font-size: 0.72rem; color: var(--muted); padding: 0 0.3rem; }
.msg + .msg.gap { margin-top: 0.6rem; }

.load-older { align-self: center; margin-bottom: 0.5rem; }

/* ---------- composer ---------- */

.composer {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.6rem 0.75rem calc(env(safe-area-inset-bottom) + 0.6rem);
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 9rem;
  padding: 0.6rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--surface);
  line-height: 1.35;
}
.icon-btn {
  flex: none;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 0;
  display: grid;
  place-items: center;
  background: transparent;
  color: var(--mine);
  cursor: pointer;
}
.icon-btn.send { background: var(--mine); color: var(--mine-ink); }
.icon-btn:disabled { opacity: 0.45; cursor: default; }
.icon-btn svg { width: 22px; height: 22px; }

.attachment {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.5rem 0.75rem 0;
  font-size: 0.85rem;
  color: var(--muted);
}
.attachment img { width: 48px; height: 48px; object-fit: cover; border-radius: 8px; }

/* ---------- forms (join) ---------- */

.card {
  margin: auto 1rem;
  padding: 1.5rem;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 18px;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}
.card h1 { font-size: 1.4rem; margin: 0; letter-spacing: -0.01em; }
.card p { margin: 0; }
.card .note {
  padding: 0.7rem 0.9rem;
  border-left: 3px solid var(--mine);
  background: var(--bg);
  border-radius: 0 10px 10px 0;
}
.card label { font-weight: 600; font-size: 0.95rem; }
.card input {
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.7rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
}
.card .who { display: flex; gap: 0.75rem; align-items: center; }
.error-text { color: var(--danger); font-size: 0.9rem; }
.card .new-person .hint { margin-top: 0.6rem; }
.card .as-me .btn { margin: 0.2rem 0 0 -0.85rem; }
