/* «Кулуар» — оформление.
   Мотив — защитный узор изнутри конфиденциального конверта: мелкая косая сетка,
   сквозь которую не прочитать содержимое. Остальное — тихо и строго. */

:root {
  --ink: #18212c;
  --ink-soft: #3c4857;
  --muted: #5f6c7b;
  --paper: #e8ecf0;
  --sheet: #ffffff;
  --line: #d2d9e1;
  --line-strong: #b9c3ce;
  --tint: #2c4c78;
  --tint-hover: #233e63;
  --tint-soft: #dfe7f1;
  --on-tint: #ffffff;
  --ok: #2b6a4b;
  --alert: #a3341d;
  --alert-soft: #f8e6e1;
  --hatch: rgba(44, 76, 120, 0.09);
  --shadow: 0 10px 40px rgba(24, 33, 44, 0.18);
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: Charter, "Bitstream Charter", "Sitka Text", Cambria, "PT Serif", Georgia, serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --ink: #e2e7ed;
    --ink-soft: #c2cad4;
    --muted: #96a2b0;
    --paper: #131922;
    --sheet: #1a222d;
    --line: #2b3542;
    --line-strong: #3a4656;
    --tint: #8aacd8;
    --tint-hover: #a3bfe3;
    --tint-soft: #233249;
    --on-tint: #0f1722;
    --ok: #7cc4a0;
    --alert: #f0907a;
    --alert-soft: #3a2420;
    --hatch: rgba(138, 172, 216, 0.07);
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font: 15px/1.45 var(--sans);
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--tint); }
h1, h2, h3 { margin: 0; font-weight: 600; }
p { margin: 0; }
[hidden] { display: none !important; }

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

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.noscript { padding: 24px; }

.icon {
  width: 20px; height: 20px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.8;
  stroke-linecap: round; stroke-linejoin: round;
}
.icon.tiny { width: 15px; height: 15px; stroke-width: 2; }
.bad { color: var(--alert); }

/* Защитный узор */
.auth, .empty-pane, .code {
  background-image:
    repeating-linear-gradient(45deg, var(--hatch) 0 1px, transparent 1px 7px),
    repeating-linear-gradient(-45deg, var(--hatch) 0 1px, transparent 1px 7px);
}

/* ───────── Кнопки и поля ───────── */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 38px; padding: 7px 14px;
  border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--sheet); color: var(--ink);
  font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--ink-soft); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.primary { background: var(--tint); border-color: var(--tint); color: var(--on-tint); }
.btn.primary:hover:not(:disabled) { background: var(--tint-hover); border-color: var(--tint-hover); }
.btn.danger { background: var(--alert); border-color: var(--alert); color: #fff; }
.btn.danger-quiet { color: var(--alert); }
.btn.quiet { border-color: transparent; background: transparent; color: var(--tint); }
.btn.small { min-height: 30px; padding: 4px 10px; font-size: 13px; }
.btn.wide { width: 100%; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 38px; height: 38px; flex: none;
  border: 0; border-radius: 8px; background: transparent; color: var(--ink-soft); cursor: pointer;
}
.icon-btn:hover:not(:disabled) { background: var(--tint-soft); color: var(--ink); }
.icon-btn:disabled { opacity: 0.4; cursor: default; }

.link { border: 0; background: none; padding: 0; color: var(--tint); text-decoration: underline; cursor: pointer; font-weight: 500; }

input[type="text"], input[type="password"], select, textarea {
  width: 100%; min-height: 40px; padding: 8px 11px;
  border: 1px solid var(--line-strong); border-radius: 8px;
  background: var(--sheet);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--tint); outline-offset: -1px; border-color: var(--tint); }
input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--tint); margin: 0; flex: none; }

.form { display: grid; gap: 16px; }
.field { display: grid; gap: 6px; }
.field label { font-weight: 600; font-size: 14px; }
.hint { font-size: 13px; color: var(--muted); }
.form-error { color: var(--alert); font-size: 14px; }
.notice { padding: 10px 12px; border-radius: 8px; background: var(--tint-soft); font-size: 14px; }
.check { display: flex; align-items: center; gap: 9px; font-size: 14px; cursor: pointer; }
.actions { display: flex; justify-content: flex-end; gap: 10px; flex-wrap: wrap; margin-top: 4px; }
.actions.spread { justify-content: space-between; margin-top: 22px; padding-top: 16px; border-top: 1px solid var(--line); }
.fieldset { border: 0; padding: 0; margin: 0; }
.fieldset legend { font-weight: 600; font-size: 14px; padding: 0; margin-bottom: 6px; }

/* ───────── Вход ───────── */

.auth {
  min-height: 100dvh; display: grid; place-items: center;
  padding: 32px 16px; background-color: var(--paper);
}
.auth-card {
  width: 100%; max-width: 420px;
  padding: 32px 30px 28px;
  background: var(--sheet); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: var(--shadow);
}
.auth-card h1 { font-family: var(--serif); font-size: 28px; line-height: 1.15; margin: 18px 0 8px; letter-spacing: -0.01em; }
.auth-card .lead { color: var(--ink-soft); margin-bottom: 22px; }
.auth-card .form { margin-top: 18px; }
.auth-card .lead + .form { margin-top: 0; }

.brand {
  display: flex; align-items: center; gap: 8px;
  font-family: var(--serif); font-size: 18px; font-weight: 600; color: var(--tint);
}
.brand-icon { width: 19px; height: 19px; }

/* ───────── Каркас ───────── */

.app { height: 100dvh; display: flex; flex-direction: column; }
.shell { flex: 1; min-height: 0; display: grid; grid-template-columns: 300px 1fr; }

.danger-banner, .offline {
  display: flex; align-items: center; gap: 8px; padding: 9px 16px; font-size: 14px;
}
.danger-banner { background: var(--alert); color: #fff; }
.offline { background: var(--tint-soft); color: var(--ink); }

.sidebar {
  display: flex; flex-direction: column; min-height: 0;
  background: var(--paper); border-right: 1px solid var(--line);
}
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 10px 10px 18px; }
.new-chat { margin: 4px 14px 10px; }

.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 20; min-width: 250px;
  padding: 6px; background: var(--sheet); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow);
}
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 4px; }
.menu-item {
  display: block; width: 100%; text-align: left; padding: 9px 12px;
  border: 0; border-radius: 6px; background: none; cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--tint-soft); outline: none; }

.chat-list { flex: 1; min-height: 0; overflow-y: auto; padding: 2px 8px 8px; }
.list-empty { padding: 12px 10px; }
.chat-item {
  position: relative; display: grid; grid-template-columns: 1fr auto; gap: 1px 10px; align-items: baseline;
  width: 100%; padding: 10px 12px; margin-bottom: 2px;
  border: 0; border-radius: 8px; background: none; text-align: left; cursor: pointer;
}
.chat-item:hover { background: var(--tint-soft); }
.chat-item.active { background: var(--sheet); box-shadow: inset 3px 0 0 var(--tint), 0 1px 0 var(--line); }
.chat-item-name { display: flex; align-items: center; gap: 5px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; }
.chat-item.unread .chat-item-name { font-weight: 700; }
.chat-item-when { font-size: 12px; color: var(--muted); }
.chat-item-sub { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.ttl-mark { display: inline-flex; }
.badge {
  justify-self: end; min-width: 22px; height: 22px; padding: 0 7px;
  display: inline-grid; place-items: center;
  border-radius: 11px; background: var(--tint); color: var(--on-tint); font-size: 12px; font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.side-foot { padding: 12px 18px 14px; border-top: 1px solid var(--line); }
.me { display: grid; }
.me-name { font-weight: 600; }

.pane { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--sheet); }

.empty-pane { flex: 1; display: grid; place-items: center; padding: 24px; background-color: var(--paper); }
.empty-card {
  max-width: 400px; padding: 28px; text-align: left;
  background: var(--sheet); border: 1px solid var(--line); border-radius: 14px;
}
.empty-card h2 { font-family: var(--serif); font-size: 22px; margin-bottom: 8px; }
.empty-card p { color: var(--ink-soft); margin-bottom: 18px; }

/* ───────── Открытый чат ───────── */

.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-head {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 10px 10px 20px; border-bottom: 1px solid var(--line);
}
.chat-head .back { display: none; margin-left: -10px; }
.chat-title {
  flex: 1; min-width: 0; display: grid; gap: 2px; padding: 2px 0;
  border: 0; background: none; text-align: left; cursor: pointer;
}
.chat-title-name {
  font-family: var(--serif); font-size: 19px; font-weight: 600; line-height: 1.25;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chat-title-sub { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 13px; color: var(--muted); }

.state { display: inline-flex; align-items: center; gap: 4px; font-size: 13px; color: var(--muted); }
.state.ok { color: var(--ok); }
.state.bad { color: var(--alert); font-weight: 600; }

.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 8px 24px 20px; overscroll-behavior: contain; }
.loading { padding: 40px 0; }
.older { display: flex; margin: 8px auto; }

.day {
  display: flex; align-items: center; gap: 12px; margin: 22px 0 6px;
  font-size: 12px; color: var(--muted);
}
.day::before, .day::after { content: ""; flex: 1; border-top: 1px solid var(--line); }

.sys { margin: 10px auto; max-width: 60ch; text-align: center; font-size: 13px; color: var(--muted); }

.msg {
  position: relative; max-width: 74ch;
  padding: 1px 40px 1px 14px; margin-top: 2px;
  border-left: 3px solid var(--line);
}
.msg:not(.grouped) { margin-top: 14px; }
.msg.mine { border-left-color: var(--tint); }
.msg.suspect { border-left-color: var(--alert); }
.msg-head { display: flex; align-items: center; gap: 7px; margin-bottom: 1px; font-size: 13px; }
.msg-author { font-weight: 600; color: var(--ink); }
.msg.mine .msg-author { color: var(--tint); }
.msg-head time { color: var(--muted); font-variant-numeric: tabular-nums; }
.msg-ttl { display: inline-flex; color: var(--muted); }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.5; }
.msg-error, .msg-warn { display: flex; align-items: center; gap: 6px; color: var(--alert); font-size: 14px; }
.msg-warn { font-size: 13px; margin-top: 2px; }
.trust { display: inline-flex; }
.trust.ok { color: var(--ok); }
.trust.bad { color: var(--alert); }
.msg-del {
  position: absolute; right: 4px; top: 0; width: 28px; height: 28px;
  display: grid; place-items: center; border: 0; border-radius: 6px;
  background: none; color: var(--muted); cursor: pointer; opacity: 0;
}
.msg:hover .msg-del, .msg-del:focus-visible { opacity: 1; }
.msg-del:hover { background: var(--alert-soft); color: var(--alert); }
@media (hover: none) { .msg-del { opacity: 0.6; } }

.file-card {
  display: flex; align-items: center; gap: 12px; margin-top: 4px; max-width: 420px;
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--paper);
}
.file-meta { flex: 1; min-width: 0; display: grid; }
.file-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.chat-empty { max-width: 420px; margin: 48px auto; text-align: center; display: grid; gap: 6px; }

.composer-notice {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin: 0 16px; padding: 10px 12px; border-radius: 8px;
  background: var(--alert-soft); color: var(--alert); font-size: 14px;
}
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.composer textarea { resize: none; max-height: 200px; line-height: 1.45; min-height: 42px; padding: 10px 12px; }
.composer .icon-btn { height: 42px; }
.composer .send { min-height: 42px; }

/* ───────── Диалоги ───────── */

.dialog {
  width: min(480px, calc(100vw - 24px)); max-height: calc(100dvh - 32px);
  padding: 0; border: 1px solid var(--line); border-radius: 14px;
  background: var(--sheet); color: var(--ink); box-shadow: var(--shadow);
}
.dialog.wide { width: min(720px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(14, 20, 28, 0.5); }
.dialog-head {
  position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 16px 12px 12px 22px; background: var(--sheet);
}
.dialog-head h2 { font-family: var(--serif); font-size: 20px; line-height: 1.25; }
.dialog-body { padding: 4px 22px 22px; }
.dialog-body h3 { font-size: 15px; margin: 22px 0 8px; }

.pick { display: grid; gap: 2px; max-height: 260px; overflow-y: auto; padding: 2px; }
.pick-row, .person {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 9px 10px; border: 0; border-radius: 8px; background: none; text-align: left; cursor: pointer;
}
.pick-row:hover, .person:hover { background: var(--tint-soft); }
.pick-row.disabled { opacity: 0.6; cursor: default; }
.person-name { flex: 1; min-width: 0; font-weight: 500; }
.people { display: grid; gap: 2px; margin-top: 14px; }

.verify { display: grid; gap: 14px; }
.code {
  display: grid; grid-template-columns: repeat(4, auto); justify-content: center; gap: 10px 22px;
  padding: 20px 16px; border-radius: 12px; border: 1px solid var(--line);
  background-color: var(--tint-soft);
  font-size: 23px; font-weight: 600; letter-spacing: 0.05em; font-variant-numeric: tabular-nums lining-nums;
}
.alert-box { display: grid; gap: 6px; padding: 12px 14px; border-radius: 10px; background: var(--alert-soft); color: var(--alert); }
.alert-box p { color: var(--ink); }

.settings .inline-form { display: flex; align-items: flex-end; gap: 10px; margin-bottom: 14px; }
.settings .inline-form .field { flex: 1; }
.members { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.member {
  display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 12px;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.member-actions { display: flex; gap: 6px; justify-content: flex-end; }
.tag {
  display: inline-block; margin-left: 8px; padding: 1px 8px; border-radius: 10px;
  background: var(--tint-soft); color: var(--tint); font-size: 12px; font-weight: 600; vertical-align: 1px;
}
.add-member { display: grid; gap: 10px; margin-top: 16px; padding: 14px; border: 1px solid var(--line); border-radius: 10px; }
.add-member .btn { justify-self: start; }
.keyinfo { margin-top: 16px; }

.tabs { display: flex; gap: 4px; margin: 14px 0 16px; border-bottom: 1px solid var(--line); }
.tab {
  padding: 9px 14px; border: 0; border-bottom: 2px solid transparent; margin-bottom: -1px;
  background: none; color: var(--muted); font-weight: 500; cursor: pointer;
}
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--tint); }

.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 10px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table thead th { font-size: 13px; color: var(--muted); font-weight: 600; }
.table tr.inactive td { color: var(--muted); }
.table.kv th { width: 45%; font-weight: 500; color: var(--ink-soft); }
.row-actions { text-align: right; white-space: nowrap; }
.row-actions .btn + .btn { margin-left: 6px; }
.state-cell.ok { color: var(--ok); font-weight: 600; }
.state-cell.bad { color: var(--alert); font-weight: 700; }

.invite-form { grid-template-columns: 1fr 170px; align-items: start; }
.invite-form .field:first-child { grid-column: 1; }
.invite-form .check, .invite-form .actions { grid-column: 1 / -1; }
.invite-result {
  display: grid; gap: 10px; margin-top: 16px; padding: 14px;
  border: 1px solid var(--tint); border-radius: 10px; background: var(--tint-soft);
}
.invite-result .btn { justify-self: start; }
.link-field { font-variant-numeric: tabular-nums; }

/* ───────── Уведомления ───────── */

.toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 100;
  max-width: min(520px, calc(100vw - 32px)); padding: 11px 16px;
  border-radius: 10px; background: var(--ink); color: var(--sheet); font-size: 14px;
  box-shadow: var(--shadow);
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--alert); color: #fff; }

/* ───────── Телефоны ───────── */

@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .shell .pane { display: none; }
  .shell.show-chat .sidebar { display: none; }
  .shell.show-chat .pane { display: flex; }
  .chat-head { padding-left: 14px; }
  .chat-head .back { display: inline-grid; }
  .messages { padding: 6px 14px 16px; }
  .msg { padding-right: 32px; }
  .composer { padding-left: 10px; padding-right: 10px; }
  .send-label { display: none; }
  .composer .send { width: 42px; padding: 0; }
  .member { grid-template-columns: 1fr; gap: 6px; }
  .member-actions { justify-content: flex-start; }
  .invite-form { grid-template-columns: 1fr; }
  .code { grid-template-columns: repeat(2, auto); font-size: 21px; }
  .auth-card { padding: 26px 20px 22px; }
}

@media (prefers-reduced-motion: reduce) {
  .toast { transition: none; }
}
