:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1c1e21;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --mine: #0f766e;
  --mine-text: #ffffff;
  --theirs: #e9ebee;
  --theirs-text: #1c1e21;
  --error: #b91c1c;
  --unread: #dc2626;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111315;
    --panel: #1b1e21;
    --text: #e8eaed;
    --muted: #9aa0a6;
    --line: #2c3035;
    --accent: #14b8a6;
    --accent-text: #04201d;
    --mine: #0f766e;
    --mine-text: #ffffff;
    --theirs: #2a2e33;
    --theirs-text: #e8eaed;
    --error: #f87171;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--error); min-height: 1.2em; }
.center { text-align: center; margin-top: 30vh; }
.pad { padding: 16px; }
.spacer { flex: 1; }

button, input, textarea { font: inherit; color: inherit; }
button {
  background: var(--accent); color: var(--accent-text);
  border: 0; border-radius: 8px; padding: 8px 14px; cursor: pointer;
}
button.secondary { background: var(--theirs); color: var(--text); }
button.danger { background: transparent; color: var(--error); border: 1px solid var(--error); }
button.small { padding: 4px 10px; font-size: 14px; }
button.icon, label.icon {
  background: transparent; color: var(--text);
  padding: 6px 8px; font-size: 20px; line-height: 1; cursor: pointer; border-radius: 8px;
}
button.icon:hover, label.icon:hover { background: var(--theirs); }
input, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line);
  border-radius: 10px; background: var(--bg);
}

.card { background: var(--panel); border-radius: 14px; box-shadow: 0 2px 16px rgb(0 0 0 / .08); }

/* auth */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.auth form { width: min(360px, 100%); padding: 24px; display: grid; gap: 12px; }
.auth h1 { margin: 0; color: var(--accent); }
.auth label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }

/* layout */
.app { display: flex; height: 100dvh; }
.sidebar { width: 340px; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; }
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.bar {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--panel); min-height: 56px;
}
.bar strong { font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: none; }

/* chat list */
.chat-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.chat-list li { display: flex; gap: 10px; align-items: center; padding: 10px 14px; cursor: pointer; }
.chat-list li:hover { background: var(--bg); }
.chat-list li.active { background: var(--theirs); }
.avatar {
  width: 44px; height: 44px; border-radius: 50%; flex: none;
  display: grid; place-items: center; font-weight: 600; color: #fff;
}
.chat-list .info { flex: 1; min-width: 0; }
.chat-list .name { font-weight: 600; display: flex; gap: 6px; }
.chat-list .name span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list .last { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-list li.has-unread .last { color: var(--text); font-weight: 600; }
.badge { background: var(--unread); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; font-weight: 600; line-height: 20px; }
.badge.req { background: #d97706; }

/* messages */
.messages { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 2px; }
.day { align-self: center; color: var(--muted); font-size: 12px; margin: 12px 0 6px; }
.msg { max-width: min(75%, 560px); display: flex; flex-direction: column; align-items: flex-start; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg .who { font-size: 12px; color: var(--muted); margin: 8px 10px 2px; }
.bubble {
  background: var(--theirs); color: var(--theirs-text);
  padding: 8px 12px; border-radius: 18px; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.mine .bubble { background: var(--mine); color: var(--mine-text); }
.bubble a { color: inherit; text-decoration: underline; }
.bubble.img { padding: 4px; background: transparent; }
.bubble.img img { display: block; max-width: 100%; max-height: 320px; border-radius: 14px; cursor: zoom-in; }
.msg .time { font-size: 11px; color: var(--muted); margin: 1px 10px 0; display: none; }
.msg:hover .time, .msg.show-time .time { display: block; }
.msg .del { display: none; font-size: 11px; color: var(--muted); background: none; padding: 0 10px; }
.msg:hover .del, .msg.show-time .del { display: inline; }
.load-more { align-self: center; margin-bottom: 8px; }

/* composer */
.composer { border-top: 1px solid var(--line); background: var(--panel); padding: 8px; padding-bottom: max(8px, env(safe-area-inset-bottom)); }
.composer .row { display: flex; gap: 6px; align-items: flex-end; }
.composer textarea { resize: none; max-height: 140px; border-radius: 20px; }
.send { border-radius: 20px; }
.preview { position: relative; display: inline-block; margin: 0 0 8px 44px; }
.preview img { max-height: 120px; max-width: 200px; border-radius: 10px; display: block; }
.preview button { position: absolute; top: 2px; right: 2px; background: rgb(0 0 0 / .6); color: #fff; font-size: 14px; }

/* modal */
.modal { position: fixed; inset: 0; background: rgb(0 0 0 / .45); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal-card { width: min(480px, 100%); max-height: 90dvh; display: flex; flex-direction: column; overflow: hidden; }
.modal-body { padding: 16px; overflow-y: auto; display: grid; gap: 12px; }
.modal-body h3 { margin: 8px 0 0; font-size: 15px; color: var(--muted); }
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.list li { display: flex; align-items: center; gap: 8px; padding: 8px; border-radius: 10px; background: var(--bg); flex-wrap: wrap; }
.list li .grow { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.inline { display: flex; gap: 6px; }
.tag { font-size: 12px; color: var(--muted); }
.copy-box { font-family: ui-monospace, monospace; font-size: 13px; }

.viewer { position: fixed; inset: 0; background: rgb(0 0 0 / .9); display: grid; place-items: center; z-index: 20; cursor: zoom-out; }
.viewer img { max-width: 100%; max-height: 100%; }

.toast {
  position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%);
  background: #333; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 30; max-width: 90vw;
}

/* phone: one pane at a time */
@media (max-width: 720px) {
  .sidebar { width: 100%; border-right: 0; }
  .chat { display: none; }
  .app.in-chat .sidebar { display: none; }
  .app.in-chat .chat { display: flex; }
  .back { display: inline-block; }
  .msg { max-width: 85%; }
}

/* add a device / install */
.auth form.link-mode label { display: none; }
.qr { display: block; width: min(260px, 100%); margin: 8px auto; background: #fff; border-radius: 8px; image-rendering: pixelated; }
.steps { margin: 4px 0; padding-left: 20px; display: grid; gap: 6px; }
.center { text-align: center; }
