:root {
  --bg: #14100e;
  --panel: #1e1815;
  --panel-2: #271f1a;
  --line: #3a2e26;
  --text: #f4ece4;
  --muted: #a99a8c;
  --accent: #e2762f;
  --accent-2: #f0a35e;
  --danger: #c0392b;
  --online: #57b45c;
  --radius: 12px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

/* Атрибут hidden обязан прятать всегда. У .screen.login, .overlay.center и label
   задан свой display, а он по каскаду перебивает браузерное [hidden]{display:none} —
   из-за этого окно «Входящий звонок» висело поверх страницы сразу после входа. */
[hidden] { display: none !important; }

button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #1a1206; font-weight: 600; }
button.danger { background: transparent; border-color: var(--danger); color: #f0a99e; }
button[disabled] { opacity: .5; cursor: default; }

input[type="tel"], input[type="text"], input[type="password"] {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 12px;
  margin-top: 4px;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

label { display: block; margin-bottom: 12px; font-size: 14px; color: var(--muted); }
label.checkbox { display: flex; align-items: center; gap: 8px; color: var(--text); }
label.checkbox input { width: auto; margin: 0; }

h1 { margin: 0 0 4px; font-size: 28px; }
h2 { margin: 0 0 12px; font-size: 20px; }

.hint { color: var(--muted); font-size: 14px; }
.muted { color: var(--muted); }
.error { color: #f0a99e; font-size: 14px; margin: 10px 0 0; }
.note { color: var(--accent-2); font-size: 14px; margin: 10px 0 0; word-break: break-all; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
}

.row { display: flex; gap: 10px; flex-wrap: wrap; }

/* --- вход --- */
.screen.login { display: grid; place-items: center; min-height: 100dvh; padding: 20px; }
.screen.login .card { width: min(420px, 100%); }

/* --- основной экран --- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 5;
}
.me { display: flex; flex-direction: column; line-height: 1.2; }
.tabs { display: flex; gap: 6px; margin-left: auto; }
.tab.active { background: var(--accent); border-color: var(--accent); color: #1a1206; font-weight: 600; }
.topbar-actions { display: flex; gap: 8px; }

.conn { margin: 8px 16px 0; }

main { padding: 16px; max-width: 900px; margin: 0 auto; }

/* --- контакты --- */
.contacts { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.contact {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.avatar {
  width: 42px; height: 42px; flex: 0 0 42px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--panel-2);
  border: 1px solid var(--line);
  font-weight: 600;
  position: relative;
}
.avatar.online::after {
  content: "";
  position: absolute; right: -1px; bottom: -1px;
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--online);
  border: 2px solid var(--panel);
}
.contact-who { flex: 1; min-width: 0; }
.contact-who strong { display: block; }
.contact-who span { font-size: 13px; }
.contact-actions { display: flex; gap: 8px; }

/* --- чат --- */
.panel.chat { display: flex; flex-direction: column; height: calc(100dvh - 190px); min-height: 340px; }
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding-bottom: 10px; border-bottom: 1px solid var(--line);
}
.chat-who { display: flex; flex-direction: column; line-height: 1.2; flex: 1; min-width: 0; }
.chat-actions { display: flex; gap: 8px; }
.messages {
  list-style: none; margin: 0; padding: 12px 2px;
  flex: 1; overflow-y: auto;
  display: flex; flex-direction: column; gap: 6px;
}
.msg {
  max-width: 78%;
  padding: 8px 12px;
  border-radius: 14px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  align-self: flex-start;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg.mine { align-self: flex-end; background: #38230f; border-color: #55371b; }
.msg .meta { display: block; margin-top: 4px; font-size: 11px; color: var(--muted); text-align: right; }
.msg .ticks { margin-left: 6px; }
.msg .ticks.read { color: var(--accent-2); }
.composer { display: flex; gap: 8px; align-items: flex-end; padding-top: 10px; border-top: 1px solid var(--line); }
.composer textarea {
  flex: 1; resize: none; max-height: 120px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 12px; color: inherit; font: inherit;
}
.badge-unread {
  background: var(--accent); color: #1a1206;
  border-radius: 999px; padding: 1px 8px;
  font-size: 12px; font-weight: 600;
}
.contact { cursor: pointer; }
.preview {
  display: block; font-size: 13px; color: var(--muted);
  max-width: 46vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* --- управление --- */
.table-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 22px;
}
.users { width: 100%; border-collapse: collapse; margin-top: 6px; }
.users th, .users td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.users th { color: var(--muted); font-weight: 500; font-size: 13px; }
.users td.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.badge {
  font-size: 12px;
  color: var(--accent-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 1px 8px;
  margin-left: 6px;
}

/* --- звонок --- */
.overlay {
  position: fixed;
  inset: 0;
  background: #0b0908;
  z-index: 20;
}
.overlay.center { display: grid; place-items: center; padding: 20px; }
.overlay.center .card { width: min(360px, 100%); text-align: center; }

#remote-video { width: 100%; height: 100%; object-fit: contain; background: #000; }
#local-video {
  position: absolute;
  right: 14px; top: 14px;
  width: min(28vw, 180px);
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #000;
  transform: scaleX(-1);
}
.call-info {
  position: absolute;
  left: 16px; bottom: 84px;
  display: flex; flex-direction: column;
  text-shadow: 0 1px 4px #000;
}
.call-actions {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  display: flex;
  gap: 10px;
  justify-content: center;
  padding: 16px;
  background: linear-gradient(transparent, rgba(0,0,0,.7));
}

/* --- диалоги --- */
dialog {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
  width: min(420px, 92vw);
}
dialog::backdrop { background: rgba(0,0,0,.6); }

.toast {
  position: fixed;
  left: 50%;
  /* Сверху, а не снизу: внизу кнопки идущего звонка, и подсказка их перекрывала. */
  top: 16px;
  transform: translateX(-50%);
  background: var(--panel-2);
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 10px 16px;
  z-index: 30;
  max-width: 90vw;
}

@media (max-width: 620px) {
  .tabs { margin-left: 0; order: 3; width: 100%; }
  .topbar-actions { margin-left: auto; }
  .contact-actions button { padding: 8px 10px; }
}
