:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #1a1d23;
  --muted: #646b78;
  --border: #dde0e6;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --mine: #dbe8ff;
  --theirs: #ffffff;
  --danger: #c62828;
  --read: #2563eb;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #181c22;
    --surface-2: #232830;
    --text: #e8eaee;
    --muted: #98a0ad;
    --border: #2d333c;
    --accent: #5b8def;
    --accent-text: #0b0e12;
    --mine: #1e3358;
    --theirs: #20252d;
    --danger: #ef6b6b;
    --read: #7fb0ff;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, "Segoe UI", Roboto, "Noto Sans Hebrew", Arial, sans-serif;
  overflow: hidden;
}

.sprite { display: none; }
.icon { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.error { color: var(--danger); margin: 0; font-size: 13.5px; }

button, input, textarea { font: inherit; color: inherit; }
input, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 0;
}
input:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; font-weight: 600; }
.btn.link { border: 0; background: none; color: var(--accent); }
.btn:disabled { opacity: 0.6; cursor: default; }

.icon-btn {
  width: 40px; height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: 8px;
  background: none; cursor: pointer; flex: none;
  color: var(--muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.active { color: var(--accent); }
.icon-btn:disabled { opacity: 0.5; cursor: default; }
.small-btn { width: 28px; height: 28px; }

/* ---------- auth ---------- */
.auth { height: 100dvh; display: flex; align-items: center; justify-content: center; padding: 16px; overflow: auto; }
.auth-card {
  width: 100%; max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  display: flex; flex-direction: column; gap: 12px;
}
.auth-logo { align-self: center; border-radius: 14px; }
.auth-card h1 { margin: 0; text-align: center; font-size: 22px; }
.auth-card > p.muted { margin: 0 0 4px; text-align: center; }
.auth-card label, .dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 13.5px; }

/* ---------- layout ---------- */
.app { height: 100dvh; display: grid; grid-template-columns: 320px 1fr; }
.sidebar {
  background: var(--surface);
  border-inline-end: 1px solid var(--border);
  display: flex; flex-direction: column;
  min-height: 0;
}
.side-head {
  display: flex; align-items: center; gap: 0;
  padding: 8px 12px;
  padding-top: calc(8px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
}
.side-me { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.side-me strong, .side-me span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-me span { text-align: end; align-self: flex-start; }
.banner { background: #fef3c7; color: #78350f; padding: 6px 12px; font-size: 13px; }

.conversation-list { flex: 1; overflow-y: auto; }
.conversation {
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  border: 0; border-bottom: 1px solid var(--border);
  background: none; text-align: start; cursor: pointer;
}
.conversation:hover { background: var(--surface-2); }
.conversation.active { background: var(--surface-2); box-shadow: inset -3px 0 0 var(--accent); }
.avatar {
  position: relative;
  width: 42px; height: 42px; flex: none;
  border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600;
}
.avatar.online::after {
  content: ""; position: absolute; bottom: 0; inset-inline-end: 0;
  width: 11px; height: 11px; border-radius: 50%;
  background: #22c55e; border: 2px solid var(--surface);
}
.conversation-main { flex: 1; min-width: 0; }
.conversation-top, .conversation-bottom { display: flex; align-items: center; gap: 6px; }
.conversation-name { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conversation-time { color: var(--muted); font-size: 12px; flex: none; }
.conversation-snippet { flex: 1; min-width: 0; color: var(--muted); font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.unread-dots { display: flex; gap: 3px; flex: none; }
.unread-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c, var(--accent)); }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px;
  border-radius: 10px;
  background: var(--accent); color: var(--accent-text);
  font-size: 12px; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
  flex: none;
}
.badge.quiet { background: var(--muted); color: var(--surface); }

.chat { min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.chat-empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); padding: 24px; text-align: center; }
.chat-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-head {
  display: flex; align-items: center; gap: 4px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 6px 12px;
  padding-top: calc(6px + env(safe-area-inset-top));
}
.chat-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back-btn { display: none; }

.search-bar { display: flex; gap: 6px; padding: 6px 12px; background: var(--surface); border-bottom: 1px solid var(--border); }
.search-bar input { flex: 1; }

/* ---------- channels ---------- */
.channel-bar {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: thin;
}
.chip {
  --c: var(--muted);
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 11px;
  min-height: 32px;
  border-radius: 16px;
  border: 1px solid color-mix(in srgb, var(--c) 45%, var(--border));
  background: color-mix(in srgb, var(--c) 10%, var(--surface));
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
  font-size: 13.5px;
  flex: none;
}
.chip .chip-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--c); flex: none; }
.chip[aria-pressed="true"] {
  background: color-mix(in srgb, var(--c) 30%, var(--surface));
  border-color: var(--c);
  font-weight: 600;
  box-shadow: inset 0 0 0 1px var(--c);
}
.chip .icon { width: 13px; height: 13px; }
.chip .badge { min-width: 18px; height: 18px; font-size: 11px; background: var(--c); color: #fff; }
.chip.plain { background: none; border-style: dashed; color: var(--muted); }
.tag {
  --c: var(--muted);
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11.5px;
  padding: 0 7px;
  border-radius: 9px;
  background: color-mix(in srgb, var(--c) 18%, transparent);
  border: 1px solid color-mix(in srgb, var(--c) 50%, transparent);
  white-space: nowrap;
}

/* ---------- messages ---------- */
.messages {
  flex: 1; min-height: 0;
  overflow-y: auto;
  padding: 12px;
  display: flex; flex-direction: column; gap: 4px;
  overscroll-behavior: contain;
}
.day-separator { align-self: center; margin: 8px 0; padding: 2px 10px; border-radius: 10px; background: var(--surface-2); color: var(--muted); font-size: 12px; }
.load-note { align-self: center; color: var(--muted); font-size: 12.5px; padding: 8px; text-align: center; }

.message { display: flex; flex-direction: column; max-width: min(78%, 620px); align-self: flex-start; position: relative; }
.bubble {
  background: var(--theirs);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  overflow-wrap: anywhere;
}
.message.mine .bubble { background: var(--mine); border-color: transparent; }
.message.deleted .bubble { color: var(--muted); font-style: italic; }
.message-body { white-space: pre-wrap; }
.message-body a { color: var(--accent); }
.message-tags { display: flex; flex: 1 1 auto; flex-wrap: wrap; justify-content: center; gap: 4px; min-width: 0; }
.message-tags .tag { font-size: 10px; line-height: 1.4; padding: 0 5px; }
.message-header { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 2px; }
.message-sender { flex-shrink: 0; max-width: 45%; font-size: 12.5px; font-weight: 600; font-style: normal; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.message-meta { display: flex; flex-shrink: 0; align-items: center; gap: 5px; font-size: 11px; font-style: normal; color: var(--muted); }
.ticks { letter-spacing: -2px; font-size: 12px; direction: ltr; }
.ticks.read { color: var(--read); }

.quote {
  width: 100%;
  border: 0;
  border-inline-start: 3px solid var(--accent);
  background: color-mix(in srgb, var(--text) 6%, transparent);
  border-radius: 4px;
  padding: 3px 8px;
  margin-bottom: 4px;
  font-size: 13px;
  color: var(--muted);
  text-align: start;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  display: block;
}
.attachment-image { display: block; max-width: 100%; max-height: 320px; border-radius: 8px; margin-bottom: 4px; }
.attachment-file {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px; margin-bottom: 4px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--text) 6%, transparent);
  color: inherit; text-decoration: none;
}

.message-actions {
  display: none;
  gap: 2px;
  margin-top: 3px;
  padding: 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  align-self: flex-start;
}
.message.show-actions .message-actions { display: flex; }
@media (hover: hover) {
  .message:hover .message-actions, .message:focus-within .message-actions {
    display: flex;
    position: absolute;
    top: -14px;
    inset-inline-end: 4px;
    margin: 0;
    z-index: 1;
  }
}
.message-actions .icon-btn { width: 30px; height: 30px; }
.message-actions .icon { width: 16px; height: 16px; }

.typing { min-height: 18px; padding: 0 14px; }

/* ---------- composer ---------- */
.composer {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 6px 12px;
  padding-bottom: calc(6px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 6px;
}
.composer-context {
  display: flex; align-items: center; gap: 8px;
  padding: 2px 8px;
  border-inline-start: 3px solid var(--accent);
  background: var(--surface-2);
  border-radius: 4px;
  font-size: 13px;
}
.composer-context span { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer-channels { display: flex; gap: 5px; flex-wrap: wrap; max-height: 74px; overflow-y: auto; }
.composer-channels:empty { display: none; }
.composer-row { display: flex; align-items: flex-end; gap: 4px; }
.composer-row textarea { flex: 1; resize: none; min-height: 40px; max-height: 140px; line-height: 1.4; }
.send-btn { background: var(--accent); color: var(--accent-text); border-radius: 50%; }
.send-btn:hover { background: var(--accent); color: var(--accent-text); filter: brightness(1.08); }
.send-btn .icon { transform: scaleX(-1); }

/* ---------- dialogs ---------- */
.dialog {
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  width: min(460px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
}
.dialog::backdrop { background: rgb(0 0 0 / 0.45); }
.dialog h2 { margin: 0 0 10px; font-size: 17px; }
.dialog p { margin: 0 0 10px; }
.dialog-actions { display: flex; gap: 8px; margin-top: 14px; }
.channels-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.channel-row, .channel-add { display: flex; align-items: center; gap: 6px; }
.channel-row input[type="text"], .channel-add input:not([type="color"]) { flex: 1; }
input[type="color"] { width: 36px; height: 36px; padding: 2px; flex: none; cursor: pointer; }

.toast {
  position: fixed;
  bottom: calc(90px + env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%);
  background: #1a1d23; color: #fff;
  padding: 8px 14px; border-radius: 8px;
  font-size: 14px;
  max-width: calc(100vw - 32px);
  z-index: 10;
}

/* ---------- phones: one pane at a time ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border: 0; }
  .chat { display: none; }
  body.in-chat .sidebar { display: none; }
  body.in-chat .chat { display: flex; }
  .back-btn { display: inline-flex; }
  .message { max-width: 88%; }
}
