:root {
  --bg: #0f1419;
  --panel: #1a222c;
  --panel-2: #222c38;
  --border: #2d3a48;
  --text: #e7eef6;
  --muted: #8ea0b3;
  --accent: #3d8bfd;
  --accent-2: #2f6fcd;
  --danger: #e35d6a;
  --ok: #3ecf8e;
  --unread: #ff6b4a;
  --msg-in: #243041;
  --msg-out: #1e3a5f;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  font-family: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}
body {
  background: radial-gradient(1200px 600px at 10% -10%, #1b2a3d, transparent),
              radial-gradient(900px 500px at 100% 0%, #1a2430, transparent),
              var(--bg);
  color: var(--text);
}

a { color: var(--accent); text-decoration: none; }
button, input, textarea {
  font: inherit;
  color: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: 220px 1fr;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

.side-nav {
  background: rgba(18, 24, 31, 0.95);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  overflow: auto;
}

.brand {
  font-weight: 700;
  letter-spacing: 0.02em;
  margin: 0 0 18px;
  padding: 0 8px;
}

.nav-link {
  display: block;
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--text);
}
.nav-link:hover, .nav-link.active {
  background: var(--panel-2);
}
.nav-spacer { flex: 1; }
.nav-user {
  color: var(--muted);
  font-size: 13px;
  padding: 8px;
}

.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.card {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.card h1 { margin: 0 0 8px; font-size: 24px; }
.card p { margin: 0 0 20px; color: var(--muted); }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--muted); }
.field input, .field textarea, .composer input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 10px;
  padding: 11px 12px;
  outline: none;
}
.field input:focus, .composer input:focus, .field textarea:focus {
  border-color: var(--accent);
}
.btn {
  border: 0;
  border-radius: 10px;
  padding: 11px 14px;
  background: var(--accent);
  color: white;
  cursor: pointer;
  font-weight: 600;
}
.btn:hover { background: var(--accent-2); }
.btn.secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}
.btn.danger { background: var(--danger); }
.btn:disabled { opacity: 0.55; cursor: default; }
.error {
  color: #ffb4bc;
  background: rgba(227, 93, 106, 0.12);
  border: 1px solid rgba(227, 93, 106, 0.35);
  padding: 10px 12px;
  border-radius: 10px;
  margin-bottom: 12px;
  display: none;
}
.error.show { display: block; }

.chats-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.chat-list {
  border-right: 1px solid var(--border);
  background: rgba(20, 27, 35, 0.9);
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}
.chat-list-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.chat-list-header h2 { margin: 0; font-size: 16px; }
.chat-items { overflow: auto; flex: 1; }
.chat-item {
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 14px 16px;
  border-bottom: 1px solid rgba(45, 58, 72, 0.55);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 10px;
}
.chat-item:hover, .chat-item.active { background: var(--panel-2); }
.chat-item .title { font-weight: 600; }
.chat-item .meta { color: var(--muted); font-size: 12px; margin-top: 4px; }
.badge {
  min-width: 22px;
  height: 22px;
  border-radius: 999px;
  background: var(--unread);
  color: white;
  font-size: 12px;
  font-weight: 700;
  display: inline-grid;
  place-items: center;
  padding: 0 6px;
}

.thread {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  background: rgba(15, 20, 25, 0.55);
}
.thread-empty {
  flex: 1;
  display: grid;
  place-items: center;
  color: var(--muted);
  min-height: 0;
}
.thread-active {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  overflow: hidden;
}
.thread-header {
  flex: 0 0 auto;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: rgba(20, 27, 35, 0.85);
}
.thread-header h2 { margin: 0; font-size: 17px; }
.thread-header .sub { color: var(--muted); font-size: 12px; margin-top: 4px; }

.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 8px;
}
#messageList {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: auto;
}
.msg {
  max-width: min(720px, 88%);
  padding: 10px 12px;
  border-radius: 14px;
  background: var(--msg-in);
  border: 1px solid rgba(255,255,255,0.04);
}
.msg.out { align-self: flex-end; background: var(--msg-out); }
.msg .who { font-size: 12px; color: #9ec1ff; margin-bottom: 4px; font-weight: 600; }
.msg .body { white-space: pre-wrap; word-break: break-word; line-height: 1.4; }
.msg .time { margin-top: 6px; font-size: 11px; color: var(--muted); }
.msg .att {
  margin-top: 8px;
  display: grid;
  gap: 8px;
}
.msg .att img, .msg .att video {
  max-width: 100%;
  border-radius: 10px;
  display: block;
}
.load-more {
  align-self: center;
  margin-bottom: 8px;
}

.composer {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  padding: 12px 14px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  background: rgba(20, 27, 35, 0.92);
}

.toast-stack {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: grid;
  gap: 10px;
  z-index: 50;
  width: min(360px, calc(100vw - 24px));
}
.toast {
  background: var(--panel);
  border: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  border-radius: 12px;
  padding: 12px 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.toast .t-title { font-weight: 700; margin-bottom: 4px; }
.toast .t-body { color: var(--muted); font-size: 13px; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: none;
  place-items: center;
  z-index: 60;
  padding: 16px;
}
.modal-backdrop.show { display: grid; }
.modal {
  width: min(460px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  box-shadow: var(--shadow);
}
.modal h3 { margin: 0 0 8px; }
.modal p { margin: 0 0 16px; color: var(--muted); white-space: pre-wrap; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

.users-page { padding: 24px; }
.users-page h1 { margin-top: 0; }
.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--panel);
  border-radius: 12px;
  overflow: hidden;
}
.table th, .table td {
  text-align: left;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr auto auto;
  gap: 10px;
  margin: 16px 0 20px;
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
  }
  .side-nav { flex-direction: row; flex-wrap: wrap; align-items: center; }
  .nav-spacer { display: none; }
  .chats-layout {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(160px, 34vh) 1fr;
  }
  .chat-list { max-height: none; }
  .form-row { grid-template-columns: 1fr; }
}
