@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap");

:root {
  --bg: #0d0f12;
  --surface: #14171b;
  --surface-2: #1a1e24;
  --surface-3: #21262e;
  --border: #2c313a;
  --text: #f2efe6;
  --text-muted: #9a968c;
  --accent: #f4c430;
  --accent-hover: #e0b32a;
  --accent-ink: #14120a;
  --bubble-mine: #2a4a3f;
  --bubble-theirs: #1a1e24;
  --error-text: #ff9270;
  --body: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--body);
  background: var(--bg);
  color: var(--text);
  overscroll-behavior: none;
}

button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; }

#statusBanner {
  display: none;
  text-align: center;
  padding: 6px;
  font-size: 13px;
  background: var(--surface-3);
  color: var(--text-muted);
}
#statusBanner.visible { display: block; }

#app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* ---------- sidebar (список бесед) ---------- */

#sidebar {
  width: 340px;
  min-width: 280px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--surface);
}

#sidebarHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
#sidebarHeader h1 { font-size: 18px; margin: 0; flex: 1; }
#menuBtn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 20px;
  padding: 4px 8px;
  border-radius: 8px;
}
#menuBtn:hover { background: var(--surface-2); }
#newChatBtn {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 8px;
  width: 34px; height: 34px;
  font-size: 18px;
  font-weight: 700;
}
#newChatBtn:hover { background: var(--accent-hover); }

#conversationList {
  flex: 1;
  overflow-y: auto;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--surface-3); }
.conv-avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--surface-3);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: var(--accent);
  flex-shrink: 0;
}
.conv-info { flex: 1; min-width: 0; }
.conv-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-preview { font-size: 13px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-badge {
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 12px; font-weight: 700;
  min-width: 20px; height: 20px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  padding: 0 6px;
}

#emptySidebarHint {
  padding: 24px 16px;
  color: var(--text-muted);
  font-size: 14px;
}

/* ---------- thread (переписка) ---------- */

#thread {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#threadHeader {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
#threadBackBtn { display: none; background: none; border: none; color: var(--text); font-size: 20px; }
#threadTitle { font-weight: 600; flex: 1; }
#threadSubtitle { font-size: 12px; color: var(--text-muted); }

#threadActive {
  display: none;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

#threadPlaceholder {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

#messageList {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg-row { display: flex; }
.msg-row.mine { justify-content: flex-end; }

.bubble {
  max-width: 72%;
  padding: 8px 12px;
  border-radius: 14px;
  background: var(--bubble-theirs);
  position: relative;
}
.msg-row.mine .bubble { background: var(--bubble-mine); }
.bubble .sender { font-size: 12px; font-weight: 600; color: var(--accent); margin-bottom: 2px; }
.bubble .body { white-space: pre-wrap; word-break: break-word; }
/* Текст идёт после вложения (см. app.js:renderMessageBubble) — добавляем
   отступ сверху только в этом случае, чтобы не залипал к фото/файлу. */
.attachment-image + .body, .attachment + .body { margin-top: 6px; }
.bubble .meta { font-size: 11px; color: var(--text-muted); margin-top: 4px; text-align: right; }
.bubble .attachment {
  display: flex; align-items: center; gap: 6px;
  margin-top: 6px; padding: 6px 8px;
  background: var(--surface-3); border-radius: 8px;
  text-decoration: none; color: var(--text); font-size: 13px;
}
.bubble.deleted .body { color: var(--text-muted); font-style: italic; }

.attachment-image {
  display: block;
  max-width: 240px;
  max-height: 240px;
  width: auto; height: auto;
  border-radius: 10px;
  margin-top: 6px;
  cursor: zoom-in;
  object-fit: cover;
}

/* ---------- лайтбокс: клик по миниатюре открывает изображение целиком ---------- */

.lightbox {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.88);
  display: flex; align-items: center; justify-content: center;
  z-index: 50;
}
.lightbox.hidden { display: none; }
.lightbox img {
  max-width: 92vw;
  max-height: 92vh;
  object-fit: contain;
  border-radius: 4px;
}
#lightboxClose {
  position: absolute;
  top: 16px; right: 20px;
  background: rgba(255, 255, 255, 0.08);
  border: none;
  color: #fff;
  font-size: 26px;
  line-height: 1;
  width: 40px; height: 40px;
  border-radius: 50%;
}

#typingIndicator {
  padding: 0 16px 4px;
  font-size: 12px;
  color: var(--text-muted);
  min-height: 16px;
}

#composerBar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
#attachBtn {
  background: var(--surface-3);
  border: none; color: var(--text);
  width: 38px; height: 38px; border-radius: 10px; font-size: 18px;
  flex-shrink: 0;
}
/* Настоящий <input type="file"> никогда не показывается сам — единственная
   точка входа это кнопка-скрепка #attachBtn (см. app.js, .click() по её
   нажатию). Скрыто через класс, а не inline style="display:none" в разметке
   — так это не зависит от CSP style-src (см. правку CSP ранее), надёжнее. */
#fileInput { display: none; }
#composerInput {
  flex: 1;
  resize: none;
  max-height: 120px;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
}
#sendBtn {
  background: var(--accent);
  color: var(--accent-ink);
  border: none; border-radius: 10px;
  width: 38px; height: 38px; font-size: 16px; font-weight: 700;
  flex-shrink: 0;
}
#sendBtn:disabled { opacity: 0.5; cursor: default; }

#pendingAttachments { display: flex; gap: 6px; flex-wrap: wrap; padding: 0 16px; }
.pending-att {
  background: var(--surface-3);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
  display: flex; align-items: center; gap: 6px;
}
.pending-att button { background: none; border: none; color: var(--error-text); font-size: 14px; }
.pending-att img { width: 20px; height: 20px; border-radius: 4px; object-fit: cover; }

/* ---------- выдвижное меню слева (как в Telegram) ---------- */

.drawer-backdrop {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 40;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}
.drawer-backdrop.open { opacity: 1; pointer-events: auto; }
.drawer-backdrop.hidden { display: none; }

.side-drawer {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: 280px; max-width: 82vw;
  background: var(--surface);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  display: flex; flex-direction: column;
}
.drawer-backdrop.open .side-drawer { transform: translateX(0); }

.drawer-header {
  display: flex; align-items: center; gap: 12px;
  padding: 22px 16px;
  border-bottom: 1px solid var(--border);
}
.drawer-avatar {
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--surface-3); color: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 18px;
  flex-shrink: 0;
}
.drawer-email { font-weight: 600; word-break: break-all; font-size: 14px; }

.drawer-nav { display: flex; flex-direction: column; padding: 8px 0; }
.drawer-nav a {
  padding: 13px 18px;
  color: var(--text);
  text-decoration: none;
  font-size: 15px;
}
.drawer-nav a:hover { background: var(--surface-2); }

.drawer-logout {
  margin: auto 16px 20px;
  padding: 11px; border-radius: 8px; border: none;
  background: var(--surface-3); color: var(--error-text);
  font-weight: 600; font-size: 14px;
}

/* ---------- модалка нового чата ---------- */

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 20;
}
.modal-backdrop.hidden { display: none; }
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 380px; max-width: 92vw;
  max-height: 80vh;
  display: flex; flex-direction: column;
  padding: 16px;
  gap: 10px;
}
.modal h2 { margin: 0; font-size: 16px; }
.modal input[type="text"] {
  padding: 9px 12px; border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text);
}
.modal .tabs { display: flex; gap: 8px; }
.modal .tabs button {
  flex: 1; padding: 6px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text-muted);
}
.modal .tabs button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
#userResults { max-height: 200px; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; }
.user-result {
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  display: flex; align-items: center; justify-content: space-between;
}
.user-result:hover { background: var(--surface-2); }
.selected-members { display: flex; flex-wrap: wrap; gap: 6px; }
.selected-chip {
  background: var(--surface-3); border-radius: 8px; padding: 3px 8px; font-size: 12px;
  display: flex; align-items: center; gap: 4px;
}
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.btn-primary { background: var(--accent); color: var(--accent-ink); border: none; border-radius: 8px; padding: 8px 14px; font-weight: 600; }
.btn-secondary { background: var(--surface-3); color: var(--text); border: none; border-radius: 8px; padding: 8px 14px; }
.error-text { color: var(--error-text); font-size: 13px; min-height: 16px; }

/* ---------- mobile ---------- */

@media (max-width: 768px) {
  #sidebar { width: 100%; min-width: 0; }
  #thread { display: none; }
  #threadBackBtn { display: block; }

  #app.thread-open #sidebar { display: none; }
  #app.thread-open #thread { display: flex; }

  .bubble { max-width: 84%; }
}
