:root {
  --bg: #eef2ec;
  --panel: #ffffff;
  --panel-2: #f6f8f4;
  --text: #1c261c;
  --muted: #5e6b5d;
  --line: #d7ded4;
  --accent: #23802b;
  --accent-text: #ffffff;
  --accent-soft: #e0f2dd;
  --mine: #d9f2d4;
  --theirs: #ffffff;
  --error: #b3261e;
  --online: #2e9e37;
  --away: #d99a00;
  --busy: #d93a2b;
  --offline: #9aa59a;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #141a15;
    --panel: #1d241d;
    --panel-2: #252d25;
    --text: #e7ede6;
    --muted: #a3afa2;
    --line: #364036;
    --accent: #5cc263;
    --accent-text: #0d1a0e;
    --accent-soft: #24402a;
    --mine: #27482c;
    --theirs: #2a322a;
    --error: #ff8a80;
    --offline: #728071;
  }
}

* { 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, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

input, select, textarea {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 0;
}

button {
  background: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 8px;
  color: var(--accent-text);
  cursor: pointer;
  font-weight: 600;
  padding: 8px 14px;
}

button:disabled { cursor: wait; opacity: .6; }

button.ghost {
  background: transparent;
  border-color: var(--line);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  padding: 4px 8px;
  white-space: nowrap;
}

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

.error { color: var(--error); margin: 0; }

/* Sign-in */
.login { align-items: center; display: flex; justify-content: center; min-height: 100%; padding: 16px; }

.login-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 360px;
  padding: 28px;
  width: 100%;
}

.login-card img { align-self: center; }
.login-card h1 { font-size: 22px; margin: 0 0 8px; text-align: center; }
.login-card button { margin-top: 8px; }
.check { align-items: center; color: var(--muted); display: flex; font-size: 14px; gap: 8px; margin-top: 4px; }

/* Change password */
.password-dialog {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  color: var(--text);
  max-width: 380px;
  padding: 24px;
  width: calc(100% - 32px);
}

.password-dialog::backdrop { background: rgba(0, 0, 0, .45); }
.password-dialog form { display: flex; flex-direction: column; gap: 8px; }
.password-dialog h2 { font-size: 18px; margin: 0 0 8px; }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.dialog-buttons { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
.dialog-buttons .ghost { font-size: 15px; padding: 8px 14px; }

/* App layout */
.app { display: grid; grid-template-columns: 320px minmax(0, 1fr); height: 100%; }

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.me { align-items: center; border-bottom: 1px solid var(--line); display: flex; flex-wrap: wrap; gap: 8px; padding: 12px; }
.me-text { display: flex; flex: 1 1 100px; flex-direction: column; gap: 2px; min-width: 0; }
.me-text select { border: 0; background: transparent; color: var(--muted); font-size: 13px; padding: 0; }

.connection { background: var(--away); color: #1c1600; font-size: 13px; margin: 0; padding: 6px 12px; }

.add { display: flex; gap: 6px; padding: 12px 12px 6px; }
.add input { flex: 1; font-size: 14px; }
.add-message { color: var(--muted); font-size: 13px; margin: 0; padding: 0 12px 6px; }

.contacts { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 0 12px; }

.group-name {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .04em;
  margin: 12px 12px 4px;
  text-transform: uppercase;
}

.contact {
  align-items: center;
  background: transparent;
  border: 0;
  border-radius: 0;
  color: var(--text);
  display: flex;
  font-weight: 400;
  gap: 10px;
  padding: 8px 12px;
  text-align: left;
  width: 100%;
}

.contact:hover { background: var(--panel-2); }
.contact.active { background: var(--accent-soft); }
.contact-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact.offline .contact-name { color: var(--muted); }
.contact-typing { color: var(--accent); flex: none; font-size: 12px; font-style: italic; }

.unread {
  background: var(--busy);
  border-radius: 10px;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  min-width: 20px;
  padding: 0 6px;
  text-align: center;
}

.no-contacts { color: var(--muted); font-size: 14px; margin: 12px; }

.dot { background: var(--offline); border-radius: 50%; flex: none; height: 10px; width: 10px; }
.dot.online { background: var(--online); }
.dot.away, .dot.idle { background: var(--away); }
.dot.occupied, .dot.dnd, .dot.na { background: var(--busy); }

/* Conversation */
.chat { display: flex; min-height: 0; min-width: 0; }

.chat-empty {
  align-items: center;
  color: var(--muted);
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
}

.chat-open { display: flex; flex: 1; flex-direction: column; min-height: 0; min-width: 0; }

.chat-header {
  align-items: center;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 10px;
  padding: 10px 14px;
}

.back { display: none; }
.chat-title { display: flex; flex: 1; flex-direction: column; min-width: 0; }
.chat-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title span { color: var(--muted); font-size: 13px; }

.messages {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 6px;
  list-style: none;
  margin: 0;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
}

.message {
  align-self: flex-start;
  background: var(--theirs);
  border: 1px solid var(--line);
  border-radius: 12px;
  max-width: min(75%, 640px);
  padding: 6px 10px;
}

.message.mine { align-self: flex-end; background: var(--mine); }
.message.failed { border-color: var(--error); }
.message-text { overflow-wrap: anywhere; white-space: pre-wrap; }
.message-meta { color: var(--muted); font-size: 11px; margin-top: 2px; }
.message.failed .message-meta { color: var(--error); }

.typing { color: var(--muted); font-size: 13px; margin: 0; min-height: 20px; padding: 0 16px; }

.live {
  border-style: dashed;
  border-color: var(--accent);
  flex: none;
  margin: 0 16px 6px;
  max-height: 30vh;
  overflow-y: auto;
}

.live .message-text::after {
  animation: caret 1s steps(2) infinite;
  border-left: 2px solid var(--accent);
  content: "";
  margin-left: 1px;
}

@keyframes caret { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .live .message-text::after { animation: none; } }

.composer { background: var(--panel); border-top: 1px solid var(--line); display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 12px; }
.composer textarea { flex: 1 1 200px; resize: none; }
.live-toggle { align-items: center; color: var(--muted); display: flex; flex-basis: 100%; font-size: 12px; gap: 6px; }

@media (max-width: 720px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { border-right: 0; }
  .app .chat { display: none; }
  .app.in-chat .sidebar { display: none; }
  .app.in-chat .chat { display: flex; }
  .back { display: inline-block; }
  .message { max-width: 88%; }
}
