:root {
  --bg: #1e1f22;
  --sidebar: #1a1b1e;
  --surface: #2b2d31;
  --surface-2: #232428;
  --surface-hover: #35373c;
  --border: rgba(255, 255, 255, .07);
  --text: #f2f3f5;
  --muted: #a6a9b3;
  --accent: #7469f6;
  --accent-hover: #867cff;
  --accent-text: #ffffff;
  --green: #3ba55c;
  --red: #e04b52;
  --shadow: 0 18px 50px rgba(0, 0, 0, .35);
  --radius: 12px;
}

body[data-theme="light"] {
  --bg: #f4f5f8;
  --sidebar: #e9ebf0;
  --surface: #ffffff;
  --surface-2: #f0f1f5;
  --surface-hover: #e5e7ee;
  --border: rgba(0, 0, 0, .08);
  --text: #22242b;
  --muted: #666b78;
  --accent: #5b4fe0;
  --accent-hover: #4c40cc;
  --accent-text: #ffffff;
  --shadow: 0 18px 45px rgba(35, 40, 70, .16);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
}

button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- Авторизация ---------- */

.auth-screen {
  height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(800px 500px at 15% 15%, rgba(116, 105, 246, .20), transparent 60%),
    radial-gradient(700px 500px at 85% 85%, rgba(59, 165, 92, .12), transparent 60%),
    var(--bg);
}

.auth-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 30px;
  box-shadow: var(--shadow);
}

.auth-mark, .brand-mark, .call-avatar {
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, var(--accent), #4f46c8);
  color: #fff;
  font-weight: 800;
}

.auth-mark { width: 54px; height: 54px; border-radius: 16px; font-size: 26px; margin-bottom: 14px; }
.auth-card h1 { margin: 0; font-size: 28px; letter-spacing: -.5px; }
.auth-subtitle { margin: 4px 0 22px; color: var(--muted); }

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 4px;
  background: var(--surface-2);
  border-radius: 10px;
  margin-bottom: 20px;
}

.tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 9px 4px;
  border-radius: 7px;
  font-weight: 600;
}
.tab.active { background: var(--surface); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.12); }

.auth-form { display: flex; flex-direction: column; gap: 13px; }
.auth-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.auth-form input, .settings-field input, .settings-field select {
  width: 100%;
  padding: 11px 13px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}
.auth-form input:focus, .settings-field input:focus, .settings-field select:focus,
textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(116, 105, 246, .16); }

.solid-btn, .outline-btn {
  border: 0;
  border-radius: 9px;
  padding: 11px 16px;
  font-weight: 650;
  transition: background .15s, transform .1s;
}
.solid-btn { background: var(--accent); color: var(--accent-text); }
.solid-btn:hover { background: var(--accent-hover); }
.outline-btn { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.outline-btn:hover { background: var(--surface-hover); }
button:active { transform: translateY(1px); }
button:disabled { opacity: .5; cursor: not-allowed; }

.auth-error { color: #ff8c92; min-height: 18px; margin: 12px 0 0; font-size: 13px; }
.auth-note { color: var(--muted); font-size: 12px; margin: 14px 0 0; border-top: 1px solid var(--border); padding-top: 14px; }

/* ---------- Основной интерфейс ---------- */

.app { display: grid; grid-template-columns: 258px 1fr; height: 100%; }

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

.brand { display: flex; align-items: center; gap: 11px; padding: 18px 16px; border-bottom: 1px solid var(--border); }
.brand-mark { width: 36px; height: 36px; border-radius: 11px; font-size: 18px; }
.brand-text { display: flex; flex-direction: column; }
.brand-text strong { font-size: 17px; letter-spacing: -.3px; }
.brand-text small { color: var(--muted); font-size: 11px; }

.sidebar-block { padding: 16px 10px 4px; }
.members-block { flex: 1; min-height: 0; overflow-y: auto; }
.section-label { text-transform: uppercase; font-size: 11px; font-weight: 700; letter-spacing: .8px; color: var(--muted); padding: 0 8px 8px; }

.room-button {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 10px;
  text-align: left;
  font-weight: 600;
}
.room-button:hover { background: var(--surface-hover); }
.hash { color: var(--muted); font-size: 18px; line-height: 1; }

.member-list { display: flex; flex-direction: column; gap: 2px; }
.member {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border-radius: 8px;
}
.member:hover { background: var(--surface-hover); }
.avatar {
  position: relative;
  width: 32px; height: 32px;
  flex: 0 0 32px;
  border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
}
.avatar::after {
  content: '';
  position: absolute;
  right: -2px; bottom: -2px;
  width: 11px; height: 11px;
  border-radius: 50%;
  background: #6b6f79;
  border: 2px solid var(--sidebar);
}
.avatar.online::after { background: var(--green); }
.member-name { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-tag { font-size: 11px; color: var(--muted); }

.sidebar-actions { padding: 10px; display: flex; flex-direction: column; gap: 4px; }
.side-btn { background: transparent; color: var(--text); border: 0; text-align: left; padding: 9px 10px; border-radius: 8px; }
.side-btn:hover { background: var(--surface-hover); }
.side-btn.subtle { color: var(--muted); }

.self-card {
  display: flex; align-items: center; gap: 9px;
  margin: 0 10px 12px; padding: 9px;
  background: var(--surface-2); border-radius: 9px;
}
.self-card .avatar { width: 30px; height: 30px; flex-basis: 30px; }
.self-info { display: flex; flex-direction: column; min-width: 0; }
.self-info strong { font-size: 13px; overflow: hidden; text-overflow: ellipsis; }
.self-info small { color: var(--muted); font-size: 11px; }

.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.chat-header {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.chat-title h1 { margin: 0; font-size: 17px; }
.chat-title p { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.header-actions { display: flex; gap: 8px; align-items: center; }
.header-actions .solid-btn, .header-actions .outline-btn { padding: 9px 13px; font-size: 13px; }
.danger { color: #ff9a9f !important; }

.messages { flex: 1; overflow-y: auto; padding: 22px 20px 10px; scroll-behavior: smooth; }
.messages::-webkit-scrollbar, .sidebar::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb, .sidebar::-webkit-scrollbar-thumb { background: var(--surface-hover); border-radius: 8px; }

.day-divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 11px; margin: 14px 0; }
.day-divider::before, .day-divider::after { content: ''; height: 1px; background: var(--border); flex: 1; }

.message { display: flex; gap: 11px; padding: 5px 0; position: relative; }
.message:hover { background: rgba(127, 127, 127, .05); border-radius: 8px; }
.message.grouped { padding-top: 1px; }
.message .avatar { margin-top: 2px; }
.message.grouped .avatar { visibility: hidden; }
.message-body { min-width: 0; flex: 1; }
.message-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 2px; }
.message-author { font-weight: 700; font-size: 14px; }
.message-time { color: var(--muted); font-size: 11px; }
.message own .message-author { color: var(--accent-hover); }
.message-text { white-space: pre-wrap; overflow-wrap: anywhere; }
.message-replay {
  display: flex; gap: 8px; align-items: center;
  border-left: 2px solid var(--accent);
  background: var(--surface-2);
  padding: 5px 9px; border-radius: 0 6px 6px 0;
  margin-bottom: 6px; font-size: 12px;
}
.message-replay strong { color: var(--accent-hover); }
.message-replay span { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 400px; }
.read-mark { margin-left: 6px; font-size: 11px; color: var(--green); }

.file-card {
  display: flex; align-items: center; gap: 11px;
  max-width: min(420px, 100%);
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px; margin-top: 5px;
  text-decoration: none; color: var(--text);
}
.file-card:hover { border-color: var(--accent); }
.file-card img { width: 54px; height: 54px; object-fit: cover; border-radius: 7px; background: var(--surface-2); }
.file-icon { width: 54px; height: 54px; display: grid; place-items: center; background: var(--surface-2); border-radius: 7px; font-size: 22px; }
.file-meta { min-width: 0; }
.file-meta strong { display: block; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 290px; }
.file-meta small { color: var(--muted); font-size: 11px; }
.file-caption { margin-top: 6px; }

.typing { height: 18px; padding: 0 22px; color: var(--muted); font-size: 12px; font-style: italic; }

.reply-bar {
  margin: 0 20px;
  display: flex; align-items: center; justify-content: space-between;
  background: var(--surface-2); border-left: 2px solid var(--accent);
  border-radius: 6px 6px 0 0; padding: 7px 10px; font-size: 12px;
}
.reply-bar span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reply-bar button { background: transparent; border: 0; color: var(--muted); }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  margin: 8px 20px 18px; padding: 6px;
  background: var(--surface); border-radius: 12px; border: 1px solid var(--border);
}
.composer textarea {
  flex: 1; resize: none; max-height: 160px; min-height: 36px;
  background: transparent; color: var(--text); border: 0; outline: none;
  padding: 8px 6px; line-height: 1.4;
}
.attach-btn, .send-btn {
  width: 36px; height: 36px; flex: 0 0 36px;
  border: 0; border-radius: 9px; font-size: 19px;
  background: var(--surface-2); color: var(--muted);
}
.send-btn { background: var(--accent); color: #fff; font-size: 15px; }
.attach-btn:hover { background: var(--surface-hover); color: var(--text); }
.send-btn:hover { background: var(--accent-hover); }

/* ---------- Звонок ---------- */

.call-panel {
  position: fixed; top: 16px; right: 16px; z-index: 30;
  width: 320px; padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: 14px;
  box-shadow: var(--shadow);
  display: grid; grid-template-columns: 42px 1fr auto; gap: 10px; align-items: center;
}
.call-avatar { width: 42px; height: 42px; border-radius: 50%; font-size: 17px; }
.call-info { display: flex; flex-direction: column; min-width: 0; }
.call-info strong { font-size: 14px; }
.call-info span { color: var(--muted); font-size: 11px; }
.call-timer { font-variant-numeric: tabular-nums; font-size: 12px; color: var(--muted); }
.call-controls { grid-column: 1 / -1; display: flex; gap: 8px; justify-content: center; padding-top: 4px; }
.round-btn {
  width: 42px; height: 42px; border: 0; border-radius: 50%;
  background: var(--surface-2); color: var(--text); font-size: 17px;
}
.round-btn:hover { background: var(--surface-hover); }
.round-btn.active { background: var(--accent); color: #fff; }
.round-btn.red { background: var(--red); color: #fff; }
.round-btn.red:hover { filter: brightness(1.1); }
.screen-preview {
  grid-column: 1 / -1;
  width: 100%; max-height: 160px; object-fit: contain;
  background: #000; border-radius: 8px; margin-top: 6px;
}
#remoteAudio { display: none; }

.incoming-card { text-align: center; }
.incoming-pulse {
  width: 56px; height: 56px; border-radius: 50%; margin: 0 auto 12px;
  background: rgba(116, 105, 246, .2);
  box-shadow: 0 0 0 0 rgba(116, 105, 246, .5);
  animation: pulse 1.8s infinite;
}
@keyframes pulse {
  70% { box-shadow: 0 0 0 22px rgba(116, 105, 246, 0); }
  100% { box-shadow: 0 0 0 0 rgba(116, 105, 246, 0); }
}

/* ---------- Модальные окна ---------- */

.modal {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center; padding: 20px;
  background: rgba(0, 0, 0, .55);
  backdrop-filter: blur(3px);
}
.modal-card {
  position: relative;
  width: min(420px, 100%);
  background: var(--surface); border-radius: 16px;
  padding: 24px; box-shadow: var(--shadow);
}
.modal-card h2 { margin: 0 0 8px; font-size: 20px; }
.modal-card p { margin: 0 0 16px; font-size: 13px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.modal-actions .solid-btn, .modal-actions .outline-btn { flex: 1; }
.modal-close { position: absolute; top: 12px; right: 12px; background: transparent; border: 0; color: var(--muted); font-size: 16px; }

.invite-code {
  font: 700 24px/1.4 ui-monospace, "SF Mono", Consolas, monospace;
  letter-spacing: 3px; text-align: center;
  padding: 15px; border-radius: 10px;
  background: var(--surface-2); border: 1px dashed var(--accent);
  user-select: all;
}
.settings-field { display: flex; flex-direction: column; gap: 6px; margin: 12px 0; font-size: 13px; font-weight: 600; }
.settings-check { display: flex; align-items: center; gap: 9px; margin: 12px 0; font-size: 13px; }
.settings-check input { width: 17px; height: 17px; accent-color: var(--accent); }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  z-index: 100; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 16px; box-shadow: var(--shadow); font-size: 13px;
}

.empty-room {
  max-width: 460px; margin: 15% auto; padding: 24px;
  text-align: center; color: var(--muted);
  background: var(--surface); border: 1px dashed var(--border); border-radius: 14px;
}

/* ---------- Комнаты и групповой звонок ---------- */

.section-label.with-action { display: flex; align-items: center; justify-content: space-between; }
.mini-add {
  width: 22px; height: 22px; border: 0; border-radius: 6px;
  background: var(--surface-2); color: var(--muted); font-size: 15px; line-height: 1;
}
.mini-add:hover { background: var(--accent); color: #fff; }

.rooms-block { padding-bottom: 8px; }
.room-list { display: flex; flex-direction: column; gap: 2px; max-height: 230px; overflow-y: auto; }
.room-item {
  width: 100%; display: flex; align-items: center; gap: 7px;
  padding: 8px; border: 0; border-radius: 8px;
  background: transparent; color: var(--text); text-align: left;
}
.room-item:hover { background: var(--surface-hover); }
.room-item.active { background: var(--surface); box-shadow: inset 0 0 0 1px var(--border); }
.room-item-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-count { color: var(--muted); font-size: 11px; }
.room-badge {
  background: var(--accent); color: #fff; border-radius: 10px;
  padding: 1px 7px; font-size: 11px; font-weight: 700;
}
.side-btn.compact { font-size: 12px; color: var(--muted); padding: 6px 10px; }

.local-screen {
  grid-column: 1 / -1; width: 100%; max-height: 120px;
  object-fit: contain; background: #000; border-radius: 8px; margin-top: 6px;
}
.remote-screens {
  grid-column: 1 / -1;
  display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 6px; max-height: 190px; overflow-y: auto;
}
.remote-tile { width: 100%; height: 90px; object-fit: contain; background: #000; border-radius: 7px; }

.manage-label { padding: 14px 0 6px; }
.manage-members { display: flex; flex-direction: column; gap: 6px; max-height: 190px; overflow-y: auto; }
.manage-member {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 9px; background: var(--surface-2); border-radius: 8px; font-size: 13px;
}
.manage-member span { flex: 1; }
.manage-member button { background: transparent; border: 0; color: #ff9a9f; font-size: 12px; }
.modal-actions.wrap { flex-wrap: wrap; }
.invite-code-note { text-align: center; margin-top: 8px; }

@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .chat-header { padding: 12px; }
  .header-actions { flex-wrap: wrap; justify-content: flex-end; }
  .messages { padding: 16px 12px 6px; }
  .composer { margin: 8px 12px 12px; }
  .call-panel { width: calc(100% - 24px); top: 8px; right: 12px; }
}
