:root {
  color-scheme: light dark;
  --bg: #0f1115;
  --card: #171a21;
  --fg: #e7e9ee;
  --muted: #9aa1ae;
  --line: #262b35;
  --accent: #4f8cff;
  --danger: #ff6b6b;
}
@media (prefers-color-scheme: light) {
  :root { --bg:#f5f6f8; --card:#fff; --fg:#14171d; --muted:#5c6470; --line:#e3e6eb; }
}
* { box-sizing: border-box; }
/*
 * Any rule that sets `display` beats the user-agent's [hidden] rule, which
 * means an element with the hidden attribute stays laid out and keeps
 * intercepting clicks while being invisible. That is a nasty failure: the
 * sign-in overlay sat on top of the whole app, transparent and unclickable
 * through. Declaring it here once, with !important, makes `hidden` mean
 * hidden everywhere no matter what a later rule says.
 */
[hidden] { display: none !important; }
body {
  margin: 0; min-height: 100dvh; display: grid; place-items: center;
  background: var(--bg); color: var(--fg);
  font: 15px/1.5 ui-sans-serif, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding: max(16px, env(safe-area-inset-top)) 16px;
}
.card {
  width: min(440px, 100%); background: var(--card); border: 1px solid var(--line);
  border-radius: 14px; padding: 28px;
}
h1 { margin: 0 0 4px; font-size: 22px; letter-spacing: -0.01em; }
.sub { margin: 0 0 20px; color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; margin: 12px 0 0; }
.error { color: var(--danger); font-size: 14px; }
label { display: block; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
input[type=text], input:not([type]) , input[type=password] { width: 100%; }
input { margin-top: 5px; padding: 10px 12px; font: inherit; width: 100%;
  background: var(--bg); color: var(--fg); border: 1px solid var(--line); border-radius: 8px; }
button { padding: 10px 16px; font: inherit; font-weight: 500; border-radius: 8px;
  border: 1px solid var(--line); background: var(--bg); color: var(--fg); cursor: pointer; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.ghost { background: none; }
button:disabled { opacity: .55; cursor: default; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.row input { flex: 1 1 140px; margin: 0; width: auto; }
.check { display: flex; gap: 5px; align-items: center; margin: 0; }
.check input { width: auto; margin: 0; }
.badge { background: var(--accent); color: #fff; font-size: 11px; padding: 2px 7px; border-radius: 99px; }
details { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 16px; }
summary { cursor: pointer; font-weight: 500; margin-bottom: 10px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
td { padding: 6px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
/* Every button in the panel is the same height, whatever its variant. */
.dialog button { min-height: 34px; }

/* ==================================================================
   Chat shell
   ================================================================== */
.app-body { display: block; place-items: unset; padding: 0; height: 100dvh; overflow: hidden; }
.gate { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px;
  background: var(--bg); z-index: 20; }
.wide { width: 100%; }

.shell { display: grid; grid-template-columns: 320px 1fr; height: 100dvh; }

/* ---- sidebar ---- */
.sidebar { border-right: 1px solid var(--line); display: flex; flex-direction: column;
  min-height: 0; background: var(--card); }
.sidebar-head { display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: max(12px, env(safe-area-inset-top)) 14px 12px; border-bottom: 1px solid var(--line); }
.me { display: flex; align-items: center; gap: 8px; min-width: 0; }
.me-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sidebar-actions { display: flex; gap: 4px; flex-shrink: 0; }
button.icon { padding: 6px 10px; line-height: 1; font-size: 16px; border: 1px solid var(--line);
  background: var(--bg); }
.conn-status { padding: 6px 14px; font-size: 12px; color: var(--muted);
  background: var(--bg); border-bottom: 1px solid var(--line); }

.conv-list { overflow-y: auto; flex: 1; min-height: 0; }
/* An empty list is a blank white void otherwise, which is what a brand
   new member sees first. */
.conv-empty { padding: 48px 24px; text-align: center; color: var(--muted);
  font-size: 14px; line-height: 1.6; }
.conv-empty strong { display: block; color: var(--fg); font-weight: 600;
  margin-bottom: 4px; }
.conv { display: flex; gap: 10px; align-items: center; width: 100%; text-align: left;
  padding: 10px 14px; background: none; border: none; border-bottom: 1px solid var(--line);
  border-radius: 0; cursor: pointer; }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--bg); box-shadow: inset 3px 0 0 var(--accent); }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--line);
  display: grid; place-items: center; font-size: 12px; font-weight: 600;
  flex-shrink: 0; position: relative; }
.avatar.online::after { content: ''; position: absolute; right: -1px; bottom: -1px;
  width: 10px; height: 10px; border-radius: 50%; background: #3ecf7e;
  border: 2px solid var(--card); }
.conv-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.conv-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.conv-name { font-weight: 550; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 11px; color: var(--muted); flex-shrink: 0; }
.conv-preview { font-size: 13px; color: var(--muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }
.unread { background: var(--accent); color: #fff; font-size: 11px; font-weight: 600;
  min-width: 20px; height: 20px; border-radius: 10px; display: grid; place-items: center;
  padding: 0 6px; flex-shrink: 0; }

/* ---- thread ---- */
.thread-pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.thread-head { display: flex; align-items: center; gap: 10px; padding: max(12px, env(safe-area-inset-top)) 18px 12px;
  border-bottom: 1px solid var(--line); flex-shrink: 0; }
.thread-head h2 { margin: 0; font-size: 16px; }
.thread-sub { margin: 0; font-size: 12px; color: var(--muted); }
button.back { display: none; }

.empty-state { flex: 1; display: grid; place-items: center; color: var(--muted); }
.messages { flex: 1; overflow-y: auto; padding: 16px 18px; min-height: 0;
  overscroll-behavior: contain; }

.day-divider { text-align: center; font-size: 11px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .06em; margin: 18px 0 10px; }
.msg { margin-bottom: 12px; max-width: min(680px, 88%); position: relative; }
.msg.grouped { margin-top: -8px; }
.msg.mine { margin-left: auto; }
.msg-head { display: flex; gap: 8px; align-items: baseline; margin-bottom: 3px; }
.msg.mine .msg-head { justify-content: flex-end; }
.msg-author { font-size: 13px; font-weight: 600; }
.msg-time { font-size: 11px; color: var(--muted); }
.bubble {
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  padding: 9px 13px; overflow-wrap: anywhere;
  /* Hug the text. A block-level bubble fills the row's max-width, so a
     three-word message renders as a 680px slab, which is the single
     thing that makes a chat app look wrong. */
  width: fit-content; max-width: 100%;
}
.msg.mine .bubble { margin-left: auto; }
.msg.mine .bubble { background: var(--accent); border-color: var(--accent); color: #fff; }
.msg.mine .bubble a { color: #fff; }
.bubble.deleted { opacity: .6; font-style: italic; }
.edited { font-size: 11px; opacity: .7; }
.quote { display: flex; flex-direction: column; gap: 1px; border-left: 3px solid var(--accent);
  padding: 3px 9px; margin-bottom: 4px; font-size: 12px; background: var(--bg);
  border-radius: 0 6px 6px 0; }
.quote-author { font-weight: 600; }
.quote-body { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.receipt { font-size: 11px; color: var(--muted); margin-top: 3px; text-align: right; }

.reactions { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.msg.mine .reactions { justify-content: flex-end; }
.reaction { display: flex; gap: 4px; align-items: center; padding: 1px 7px; font-size: 12px;
  border-radius: 99px; background: var(--card); border: 1px solid var(--line); }
.reaction.mine { border-color: var(--accent); }
.reaction .count { color: var(--muted); }

.msg-tools { display: none; gap: 2px; position: absolute; top: -10px; right: 0;
  background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.msg:hover .msg-tools, .msg:focus-within .msg-tools { display: flex; }
button.tool { font-size: 11px; padding: 3px 7px; border: none; background: none; color: var(--muted); }
button.tool:hover { color: var(--fg); background: var(--bg); }

.typing { padding: 0 18px 6px; font-size: 12px; color: var(--muted); min-height: 18px; }

.composer { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap;
  padding: 12px 18px max(12px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); flex-shrink: 0; }
.composer textarea { flex: 1; resize: none; min-height: 40px; max-height: 200px;
  margin: 0; font: inherit; line-height: 1.45; }
button.send { flex-shrink: 0; }
.reply-chip { flex-basis: 100%; display: flex; justify-content: space-between; gap: 8px;
  align-items: center; font-size: 12px; color: var(--muted); background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; padding: 5px 9px; }
.composer-error { padding: 0 18px 10px; margin: 0; }

/* ---- markdown ---- */
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 8px; }
.md ul, .md ol { margin: 0 0 8px; padding-left: 22px; }
.md li { margin: 2px 0; }
.md blockquote { margin: 0 0 8px; padding: 2px 0 2px 10px;
  border-left: 3px solid var(--line); color: var(--muted); }
.md .md-heading { margin: 0 0 6px; font-size: 1.05em; }
.md code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .88em;
  background: rgba(127, 127, 127, .18); padding: 1px 5px; border-radius: 4px; }
/* The language sits on its own line rather than floating over the code.
   Absolutely positioned it looked tidy on a wide screen and sat directly
   on top of the first line on a phone, which is where most of this will
   be read. */
.md pre.md-code { margin: 6px 0 8px; padding: 0; background: #0b0d12;
  color: #e7e9ee; border: 1px solid var(--line); border-radius: 8px;
  overflow: hidden; }
.md pre.md-code code { display: block; background: none; padding: 10px 12px;
  font-size: 12.5px; line-height: 1.5; white-space: pre; color: inherit;
  overflow-x: auto; }
.md-code-lang { display: block; font-size: 10px; text-transform: uppercase;
  letter-spacing: .06em; color: #7d879c; padding: 5px 12px 0; }
.md a { color: var(--accent); }

/* ---- dialogs ----
   One horizontal rhythm for the whole panel. Every row, field, button
   group and table cell starts and ends on the same two edges, so the
   eye has a single left margin and a single right margin to follow
   rather than four slightly different ones. */
.dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--card);
  color: var(--fg); padding: 22px; width: min(460px, 92vw); max-height: 85dvh; }
.dialog::backdrop { background: rgba(0, 0, 0, .55); }
.dialog h3 { margin: 0 0 14px; font-size: 17px; }
.dialog h4 { margin: 0 0 10px; font-size: 12px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .06em; }
.dialog section { padding: 18px 0; border-top: 1px solid var(--line); }
.dialog section:first-of-type { padding-top: 0; border-top: none; }

/* Every row in the panel shares these edges. */
.rows { width: 100%; border-collapse: collapse; font-size: 13px; margin: 0; }
.rows td { padding: 8px 0; border-bottom: 1px solid var(--line); vertical-align: middle; }
.rows tr:last-child td { border-bottom: none; }
/* First cell takes the space, the rest hug the right edge. */
.rows td:first-child { width: 100%; }
.rows td:not(:first-child) { white-space: nowrap; padding-left: 12px;
  text-align: right; color: var(--muted); }
.rows td button { padding: 4px 10px; font-size: 12px; }

/* Buttons under a section, aligned to the same right edge as the rows. */
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.actions-split { justify-content: space-between; align-items: center; }
.rows + .actions { margin-top: 12px; }
/* Two buttons in one cell need to stay on one line and not touch. */
.rows td.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

.field { display: block; margin: 0 0 12px; font-size: 13px; color: var(--muted); }
.field input, .field select { margin-top: 6px; }
.hint-block { margin: 0 0 12px; }

/* A one-off value the person has to read or copy. */
.callout { margin-top: 12px; padding: 12px; background: var(--bg);
  border: 1px solid var(--line); border-radius: 8px; }
.callout-label { margin: 0 0 4px; font-size: 12px; color: var(--muted); }
.callout-strong { margin: 0 0 10px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px; word-break: break-all; }
.callout-strong:last-child, .callout p:last-child { margin-bottom: 0; }
.code { margin: 0; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 26px; font-weight: 600; letter-spacing: .08em; text-align: center;
  padding: 10px 0; }
.dialog menu { display: flex; justify-content: flex-end; gap: 8px; padding: 18px 0 0;
  margin: 0; border-top: 1px solid var(--line); }
.tabs { display: flex; gap: 4px; margin-bottom: 14px; }
.tab { flex: 1; }
.tab.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.member-list { max-height: 260px; overflow-y: auto; border: 1px solid var(--line);
  border-radius: 8px; }
.member { display: flex; align-items: center; gap: 9px; padding: 8px 11px; margin: 0;
  border-bottom: 1px solid var(--line); cursor: pointer; color: var(--fg); }
.member:last-child { border-bottom: none; }
.member input { width: auto; margin: 0; }
.member-name { font-weight: 500; }
.member-handle { color: var(--muted); font-size: 12px; margin-left: auto; }

/* ---- phone ---- */
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .thread-pane { display: none; }
  button.back { display: block; }
  body.thread-open .sidebar { display: none; }
  body.thread-open .thread-pane { display: flex; }
  .msg { max-width: 92%; }
}

/* ---- attachments ---- */
/* Capped as well as the image itself. Without this the bubble's
   fit-content sizing still resolves against the image's INTRINSIC width,
   so a 1400px photo widened the bubble by 170px of empty space even
   though the image rendered at 340. */
.attachments { display: flex; flex-direction: column; gap: 6px; margin-top: 6px;
  align-items: flex-start; width: min(340px, 100%); }
.attachments:first-child { margin-top: 0; }
/* An explicit width, not just a max-width. The bubble is sized with
   fit-content, and fit-content resolves against the image's INTRINSIC
   width, so a max-width alone let a 2560px photo push the bubble wide
   before the image itself was constrained. */
.attach-image { display: block; width: min(340px, 100%); max-width: 100%;
  border-radius: 8px; overflow: hidden; background: var(--line);
  position: relative; }
.attach-image img { display: block; width: 100%; height: 100%; object-fit: cover; }
.attach-failed { outline: 1px solid var(--danger); min-height: 40px; }
.attach-file { display: flex; gap: 9px; align-items: center; padding: 8px 11px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
  text-decoration: none; color: var(--fg); }
.msg.mine .attach-file { background: rgba(255, 255, 255, .14); border-color: transparent;
  color: #fff; }
.attach-icon { font-size: 10px; font-weight: 700; letter-spacing: .06em;
  padding: 3px 5px; border: 1px solid currentColor; border-radius: 4px; opacity: .7; }
.attach-meta { display: flex; flex-direction: column; min-width: 0; }
.attach-name { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-size { font-size: 11px; opacity: .7; }

.attach-tray { flex-basis: 100%; display: flex; gap: 6px; flex-wrap: wrap; }
.attach-chip { display: flex; gap: 6px; align-items: center; font-size: 12px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 99px;
  padding: 3px 5px 3px 11px; max-width: 220px; }
.attach-chip.uploading { opacity: .65; }
.attach-chip-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attach-chip-pct { color: var(--muted); font-variant-numeric: tabular-nums; }
.attach-chip .icon { padding: 1px 6px; border: none; background: none; font-size: 14px; }

.thread-pane.dropping { outline: 2px dashed var(--accent); outline-offset: -8px; }
select { width: 100%; margin-top: 5px; padding: 9px 11px; font: inherit;
  background: var(--bg); color: var(--fg); border: 1px solid var(--line);
  border-radius: 8px; }

/* ---- networks ---- */
.actions-split input { flex: 1 1 auto; margin: 0; }
.net-name { font-weight: 550; }
.net-meta { color: var(--muted); font-size: 12px; }
.badge-guest { background: var(--line); color: var(--muted); font-size: 10px;
  padding: 1px 6px; border-radius: 99px; margin-left: 6px; text-transform: uppercase;
  letter-spacing: .04em; }
.net-chip { display: inline-block; font-size: 11px; color: var(--muted);
  border: 1px solid var(--line); border-radius: 99px; padding: 0 7px; margin-left: 6px; }
.thread-sub .net-chip { margin-left: 0; }
.linkish { background: none; border: none; color: var(--accent); padding: 0 0 0 4px;
  font: inherit; font-size: 13px; cursor: pointer; text-decoration: underline; }
.callout-line { margin: 2px 0 0; font-size: 12px; }
