:root {
  --ink: #131820;
  --ink-soft: #5d6874;
  --line: #dde2e8;
  --line-soft: #eaeef2;
  --bg: #e9ecf0;
  --panel: #ffffff;
  --navy: #16233a;
  --navy-soft: #223354;
  --focus: #2563eb;
  --ok: #0d9488;
  --danger: #b42318;
  --radius: 10px;
  --font: "Segoe UI", Roboto, -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--ink-soft); }

/* ------------------------------------------------------------------ вход */

.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background: radial-gradient(120% 90% at 50% -10%, #24344f 0%, var(--navy) 55%, #0d1420 100%);
}

.login__card {
  width: min(360px, 100%);
  background: var(--panel);
  border-radius: 14px;
  padding: 32px 28px;
  box-shadow: 0 24px 60px rgba(6, 12, 24, .45);
}

.login__mark { display: flex; gap: 6px; margin-bottom: 20px; }
.login__mark span { width: 26px; height: 6px; border-radius: 3px; }
.login__mark span:nth-child(1) { background: #2563eb; }
.login__mark span:nth-child(2) { background: #0d9488; }
.login__mark span:nth-child(3) { background: #c2410c; }

.login h1 { margin: 0 0 6px; font-size: 22px; letter-spacing: -.01em; }
.login__hint { margin: 0 0 22px; color: var(--ink-soft); font-size: 14px; }
.login label { display: block; font-size: 13px; color: var(--ink-soft); margin-bottom: 6px; }

.login input {
  width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfd;
}
.login input:focus { outline: 2px solid var(--focus); outline-offset: 1px; border-color: transparent; }

.login button {
  width: 100%;
  margin-top: 16px;
  padding: 11px;
  border: 0;
  border-radius: var(--radius);
  background: var(--navy);
  color: #fff;
  cursor: pointer;
}
.login button:hover { background: var(--navy-soft); }
.login__error { margin: 14px 0 0; color: var(--danger); font-size: 13px; }

/* --------------------------------------------------------------- каркас */

.app { height: 100%; display: flex; flex-direction: column; }

.topbar {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 16px;
  height: 56px;
  background: var(--navy);
  color: #e8edf5;
  flex-shrink: 0;
}

.topbar__brand { display: flex; align-items: center; gap: 9px; flex-shrink: 0; }
.brand-mark {
  width: 20px; height: 20px; border-radius: 6px;
  background: linear-gradient(135deg, #2563eb 0%, #0d9488 100%);
}
.brand-name { font-weight: 600; letter-spacing: .02em; }

.accounts { display: flex; gap: 8px; overflow-x: auto; flex: 1; min-width: 0; }

.acc {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, .16);
  background: transparent;
  color: #cbd6e6;
  cursor: pointer;
  white-space: nowrap;
  font-size: 13px;
}
.acc:hover { background: rgba(255, 255, 255, .07); }
.acc.is-active { background: #fff; color: var(--navy); border-color: #fff; }
.acc__dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.acc__count {
  min-width: 20px; padding: 0 6px; border-radius: 999px;
  background: #ef4444; color: #fff; font-size: 11px; line-height: 17px;
  text-align: center; font-variant-numeric: tabular-nums;
}
.acc.is-broken { border-color: #ef4444; color: #ffb4ab; }

.topbar__tools { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }

#search {
  width: 240px; padding: 7px 11px;
  border-radius: 8px; border: 1px solid rgba(255, 255, 255, .18);
  background: rgba(255, 255, 255, .08); color: #fff;
}
#search::placeholder { color: #93a2b8; }
#search:focus { outline: 2px solid #5b8bf0; outline-offset: 0; }

.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: #cbd6e6; cursor: pointer; }

.btn-ghost {
  border: 1px solid rgba(255, 255, 255, .18);
  background: transparent; color: #cbd6e6;
  padding: 6px 11px; border-radius: 8px; cursor: pointer; font-size: 13px;
}
.btn-ghost:hover { background: rgba(255, 255, 255, .08); }
.btn-ghost.is-on { background: #fff; color: var(--navy); border-color: #fff; }

.sync { font-size: 12px; color: #93a2b8; font-variant-numeric: tabular-nums; white-space: nowrap; }
.sync.is-error { color: #ffb4ab; }

.workspace { flex: 1; display: grid; grid-template-columns: minmax(300px, 380px) 1fr; min-height: 0; }

/* ------------------------------------------------------------ список чатов */

.list {
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  min-height: 0;
}

.list__empty { padding: 28px 20px; color: var(--ink-soft); font-size: 14px; }

.chat {
  display: grid;
  grid-template-columns: 4px 40px 1fr;
  gap: 0 11px;
  padding: 11px 14px 11px 0;
  border-bottom: 1px solid var(--line-soft);
  cursor: pointer;
  align-items: start;
}
.chat:hover { background: #f6f8fa; }
.chat.is-active { background: #eef3fb; }

.chat__stripe { height: 100%; min-height: 42px; border-radius: 0 3px 3px 0; }

.chat__avatar {
  width: 40px; height: 40px; border-radius: 9px;
  background: #e6eaef; object-fit: cover;
  display: grid; place-items: center;
  font-size: 15px; font-weight: 600; color: #64748b;
  overflow: hidden;
}
.chat__avatar img { width: 100%; height: 100%; object-fit: cover; }

.chat__body { min-width: 0; }
.chat__top { display: flex; align-items: baseline; gap: 8px; }
.chat__name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat__time { margin-left: auto; font-size: 11px; color: var(--ink-soft); white-space: nowrap; font-variant-numeric: tabular-nums; }

.tag {
  font-size: 11px; padding: 1px 7px; border-radius: 5px;
  color: #fff; white-space: nowrap; flex-shrink: 0;
}

.chat__item { font-size: 12px; color: var(--ink-soft); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat__last { font-size: 13px; color: #46505c; margin-top: 3px; display: flex; gap: 6px; align-items: center; }
.chat__last span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat__unread {
  margin-left: auto; flex-shrink: 0;
  min-width: 19px; height: 19px; padding: 0 6px; border-radius: 999px;
  background: #ef4444; color: #fff; font-size: 11px; line-height: 19px; text-align: center;
}
.chat.is-unread .chat__name { font-weight: 700; }
.chat.is-unread .chat__last { color: var(--ink); font-weight: 500; }

/* ------------------------------------------------------------------ диалог */

.thread { display: flex; flex-direction: column; min-height: 0; background: #f2f4f7; }
.thread__placeholder { margin: auto; text-align: center; color: var(--ink-soft); padding: 24px; }
.thread__inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }

.thread__head {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 12px 20px;
  display: flex; align-items: center; gap: 12px;
  flex-shrink: 0;
}
.thread__head h2 { margin: 0; font-size: 16px; }
.thread__head .item { font-size: 13px; color: var(--ink-soft); }
.thread__head a { color: var(--focus); text-decoration: none; }
.thread__head a:hover { text-decoration: underline; }
.thread__head-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }

.thread__messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 8px; min-height: 0; }

.day { align-self: center; font-size: 11px; color: var(--ink-soft); background: #e2e6eb; padding: 3px 10px; border-radius: 999px; margin: 8px 0; }

.msg {
  max-width: min(62%, 620px);
  padding: 9px 13px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
  align-self: flex-start;
}
.msg--out { align-self: flex-end; background: #dbeafe; border-color: #c3d9fb; }
.msg--system { align-self: center; background: transparent; border: 0; color: var(--ink-soft); font-size: 12px; max-width: 80%; text-align: center; }
.msg__meta { margin-top: 4px; font-size: 11px; color: var(--ink-soft); text-align: right; font-variant-numeric: tabular-nums; }

.composer {
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding: 12px 20px;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "text btn" "from from";
  gap: 8px 12px;
  flex-shrink: 0;
}
.composer textarea {
  grid-area: text;
  resize: none;
  max-height: 160px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfd;
}
.composer textarea:focus { outline: 2px solid var(--focus); outline-offset: 1px; border-color: transparent; }
.composer button {
  grid-area: btn;
  padding: 0 20px;
  border: 0;
  border-radius: var(--radius);
  background: var(--navy);
  color: #fff;
  cursor: pointer;
  height: 42px;
  align-self: end;
}
.composer button:hover { background: var(--navy-soft); }
.composer button:disabled { opacity: .5; cursor: default; }
.composer__from { grid-area: from; font-size: 12px; color: var(--ink-soft); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px;
  font-size: 13px; box-shadow: 0 10px 30px rgba(0, 0, 0, .25); z-index: 50;
}
.toast.is-error { background: var(--danger); }

/* ---------------------------------------------------------------- мобильный */

@media (max-width: 860px) {
  html, body { height: 100dvh; overflow: hidden; }
  .workspace { grid-template-columns: 1fr; }
  .thread { display: none; }
  .app.is-thread .list { display: none; }
  .app.is-thread .thread { display: flex; }
  .app.is-thread .topbar { display: none; }

  .topbar {
    height: auto;
    flex-wrap: wrap;
    padding: max(8px, env(safe-area-inset-top)) 12px 10px;
    gap: 8px;
  }
  .topbar__brand { display: none; }
  .accounts { order: 2; width: 100%; padding-bottom: 2px; }
  .acc { padding: 8px 14px; font-size: 14px; }
  .topbar__tools { order: 1; width: 100%; gap: 8px; }
  #search { flex: 1; min-width: 0; width: auto; padding: 9px 12px; }
  .toggle, .sync { font-size: 12px; }
  #logout-btn { display: none; }

  .chat { padding: 13px 14px 13px 0; grid-template-columns: 4px 44px 1fr; }
  .chat__avatar { width: 44px; height: 44px; }

  .thread__head {
    padding: max(10px, env(safe-area-inset-top)) 14px 10px;
    position: sticky; top: 0; z-index: 5;
  }
  .thread__messages { padding: 14px 12px; }
  .msg { max-width: 88%; font-size: 15px; }

  .composer {
    padding: 10px 12px max(10px, env(safe-area-inset-bottom));
    grid-template-areas: "from from" "text btn";
  }
  .composer textarea { font-size: 16px; }  /* меньше 16px — iOS/Android зумят страницу */
  .composer button { height: 44px; padding: 0 18px; }
  .composer__from { font-size: 11px; }

  .media--image img, .media--video video, .media--video iframe { width: 100%; }
  .viewer { padding: 12px; }
}

/* Установленное приложение: своя строка состояния */
@media (display-mode: standalone) {
  .topbar { padding-top: max(10px, env(safe-area-inset-top)); }
}

@media (prefers-reduced-motion: no-preference) {
  .chat, .btn-ghost, .acc { transition: background-color .12s ease; }
}

/* ------------------------------------------------------------- вложения */

.msg:has(.media) { padding: 6px 6px 8px; }
.msg__text { padding: 3px 7px 0; }

.media { display: block; margin-bottom: 4px; }

.media--image img,
.media--video video,
.media--video img {
  display: block;
  max-width: 100%;
  width: 320px;
  max-height: 340px;
  border-radius: 8px;
  background: #dfe4ea;
  object-fit: cover;
}
.media--image img { cursor: zoom-in; }
.media--video video { object-fit: contain; background: #000; }
.media--video iframe {
  display: block;
  width: 340px;
  max-width: 100%;
  aspect-ratio: 16 / 9;
  border: 0;
  border-radius: 8px;
  background: #000;
}

.media--voice { display: flex; align-items: center; gap: 8px; padding: 2px 4px; }
.media--voice audio { height: 34px; max-width: 260px; }

.media--card {
  display: flex; gap: 10px; align-items: center;
  padding: 7px; border-radius: 8px;
  background: rgba(15, 23, 42, .05);
  text-decoration: none; color: inherit;
  max-width: 320px;
}
.media--card:hover { background: rgba(15, 23, 42, .09); }
.media--card img { width: 46px; height: 46px; border-radius: 6px; object-fit: cover; flex-shrink: 0; }
.media__card-title { font-size: 13px; font-weight: 600; line-height: 1.3; }
.media__note { font-size: 11px; color: var(--ink-soft); padding: 3px 4px 0; }
.media__link { display: inline-block; padding: 3px 4px; color: var(--focus); font-size: 13px; }

.viewer {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(8, 12, 20, .88);
  display: grid; place-items: center;
  padding: 32px; cursor: zoom-out;
}
.viewer img { max-width: 100%; max-height: 100%; border-radius: 6px; }
