:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --border: #e3e6ea;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --mine: #dbeafe;
  --theirs: #f1f3f5;
  --error: #b42318;
  --notice: #8a919c;
  --online: #16a34a;
  --away: #d97706;
  --offline: #a3a9b3;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --panel: #1a1d22;
    --text: #e7e9ec;
    --muted: #9aa1ab;
    --border: #2a2e35;
    --accent: #4f8cff;
    --accent-text: #0b0d10;
    --mine: #1e3a6b;
    --theirs: #23272e;
    --error: #ff8a80;
    --notice: #7d848f;
    --online: #22c55e;
    --away: #f59e0b;
    --offline: #5b616b;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.error { color: var(--error); min-height: 1.2em; margin: 0; }

button, input { font: inherit; color: inherit; }
input {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}
input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  background: transparent;
}
button.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
button.ghost { border-color: var(--border); }
button.small { padding: 4px 10px; font-size: 13px; }
button.block { width: calc(100% - 24px); margin: 0 12px 8px; }
button:disabled { opacity: .5; cursor: default; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.card h1, .card h2 { margin: 0; }
.card label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; }
.row { display: flex; gap: 8px; justify-content: flex-end; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 360px; }

/* Layout */
.app {
  height: 100%;
  display: grid;
  grid-template-columns: 280px 1fr;
  grid-template-rows: 1fr auto;
}
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
}
.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 14px 12px;
}
.room-list { display: flex; flex-direction: column; }
.room-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border: 0;
  border-radius: 0;
  text-align: left;
  width: 100%;
}
.room-item:hover { background: var(--bg); }
.room-item.active { background: var(--mine); }
.room-item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot {
  display: inline-block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  margin-right: 7px;
  vertical-align: 1px;
  background: var(--offline);
}
.dot.online { background: var(--online); }
.dot.away { background: var(--away); }
.badge {
  background: var(--accent);
  color: var(--accent-text);
  border-radius: 999px;
  padding: 0 7px;
  font-size: 12px;
  font-weight: 600;
}
.invite-item { padding: 10px 14px; border-bottom: 1px solid var(--border); }
.invite-item .row { justify-content: flex-start; margin-top: 6px; }

.room { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.room-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.room-title { flex: 1; min-width: 0; }
.room-title > * { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-actions { display: flex; gap: 6px; }

.timeline {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.empty { margin: auto; text-align: center; }
.load-older { align-self: center; margin-bottom: 8px; }
.msg { max-width: min(560px, 85%); display: flex; flex-direction: column; }
.msg .meta { font-size: 12px; color: var(--muted); margin: 0 4px 2px; }
.msg .bubble {
  background: var(--theirs);
  padding: 7px 11px;
  border-radius: 12px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.mine .bubble { background: var(--mine); }
.msg.pending .bubble { opacity: .6; }
.msg.failed .bubble { outline: 1px solid var(--error); }
.notice { align-self: center; color: var(--notice); font-size: 12.5px; text-align: center; }

.composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}

.sync-bar {
  grid-column: 1 / -1;
  padding: 5px 12px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

dialog {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  width: min(420px, calc(100% - 32px));
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }

.pad { padding: 0 14px; }
.room-item .sub { display: block; font-size: 12px; }
.typing-text { color: var(--online); font-style: italic; }

/* Message extras: actions, reactions, replies, threads, receipts */
.msg { position: relative; }
.actions {
  display: none;
  gap: 2px;
  flex-wrap: wrap;
  margin-top: 3px;
  padding: 2px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.msg:hover .actions, .msg.show-actions .actions { display: flex; }
.act { padding: 2px 6px; font-size: 14px; border-radius: 6px; }
.act:hover { background: var(--bg); }

.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.chip {
  padding: 1px 8px;
  font-size: 13px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel);
}
.chip.mine { border-color: var(--accent); background: var(--mine); }

.reply-quote {
  display: block;
  text-align: left;
  max-width: 100%;
  margin-bottom: 3px;
  padding: 4px 9px;
  border-left: 3px solid var(--accent);
  border-radius: 6px;
  background: var(--bg);
  font-size: 13px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.reply-quote strong { color: var(--text); }

.thread-link { align-self: flex-start; padding: 2px 4px; font-size: 13px; color: var(--accent); }
.msg.mine .thread-link { align-self: flex-end; }
.seen { font-size: 12px; color: var(--muted); margin: 2px 4px 0; }
.msg.deleted .bubble { font-style: italic; color: var(--muted); }

.thread-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.typing { min-height: 20px; padding: 0 16px; font-style: italic; }
.composer-context {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  color: var(--muted);
}
.composer-context span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Attachments */
.attach-img {
  display: block;
  max-width: 100%;
  max-width: min(320px, 100%);
  height: auto;
  min-height: 40px;
  border-radius: 8px;
  cursor: zoom-in;
  background: var(--bg);
}
.attach-file { padding: 2px 0; text-align: left; color: var(--accent); }
.lightbox { width: auto; max-width: calc(100% - 32px); text-align: center; }
.lightbox img { display: block; max-width: 100%; max-height: 80vh; margin: 0 auto 8px; border-radius: 8px; }

.mobile-only { display: none; }

/* Phone: one pane at a time */
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .app .room { display: none; }
  .app.show-room .sidebar { display: none; }
  .app.show-room .room { display: flex; }
  .mobile-only { display: inline-block; }
  .timeline { padding: 12px 16px; }
}
