:root {
  --bg: #0d0e13;
  --surface: #16181f;
  --surface-2: #1e212b;
  --border: #262a36;
  --text: #e6e9f0;
  --text-dim: #8a90a3;
  --accent: #4fc3f7;
  --accent-soft: rgba(79, 195, 247, 0.15);
  --own: #2b5f81;
  --danger: #ff6b6b;
  --radius: 12px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Roboto, -apple-system, system-ui, sans-serif;
  font-size: 15px;
}

#app { height: 100vh; }

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input {
  font-family: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 15px;
  outline: none;
  width: 100%;
}

input:focus { border-color: var(--accent); }

/* ---------- Auth ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px;
}

.auth-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

.auth-logo .logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--bg);
  font-weight: 700;
  font-size: 20px;
}

.auth-logo h1 { font-size: 20px; }

.auth-tabs {
  display: flex;
  background: var(--surface-2);
  border-radius: 9px;
  padding: 4px;
  margin-bottom: 18px;
}

.auth-tabs button {
  flex: 1;
  padding: 8px;
  border-radius: 7px;
  color: var(--text-dim);
  font-weight: 600;
}

.auth-tabs button.active { background: var(--accent); color: var(--bg); }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; color: var(--text-dim); margin-bottom: 5px; }

.btn-primary {
  width: 100%;
  background: var(--accent);
  color: var(--bg);
  font-weight: 700;
  padding: 11px;
  border-radius: 9px;
  margin-top: 6px;
  font-size: 15px;
}

.auth-error {
  background: rgba(255, 107, 107, 0.12);
  color: var(--danger);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 13px;
  margin-bottom: 12px;
  display: none;
}

/* ---------- App layout ---------- */
.app { display: flex; height: 100vh; overflow: hidden; }

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

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
  overflow: hidden;
}

.avatar img { width: 100%; height: 100%; object-fit: cover; }

.me-info { flex: 1; min-width: 0; }
.me-info .name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me-info .sub { color: var(--text-dim); font-size: 12px; }

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 18px;
  flex-shrink: 0;
}

.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.chat-list { flex: 1; overflow-y: auto; }

.chat-item {
  display: flex;
  gap: 11px;
  padding: 11px 14px;
  cursor: pointer;
  align-items: center;
  border-bottom: 1px solid rgba(38, 42, 54, 0.5);
}

.chat-item:hover { background: var(--surface-2); }
.chat-item.active { background: var(--accent-soft); }

.chat-item .body { flex: 1; min-width: 0; }
.chat-item .row1 { display: flex; align-items: center; gap: 8px; }
.chat-item .cname { font-weight: 600; font-size: 14px; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .time { color: var(--text-dim); font-size: 12px; flex-shrink: 0; }
.chat-item .last { color: var(--text-dim); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-item .row2 { display: flex; align-items: center; gap: 8px; margin-top: 2px; }
.chat-item .unread {
  margin-left: auto;
  background: var(--accent);
  color: var(--bg);
  border-radius: 10px;
  padding: 1px 8px;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}

.empty-list {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-dim);
  font-size: 14px;
}

/* ---------- Main pane ---------- */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  min-width: 0;
}

.main-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 15px;
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.chat-head .cname { font-weight: 600; }
.chat-head .csub { color: var(--text-dim); font-size: 12px; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
}

.msg-row { display: flex; margin-bottom: 4px; position: relative; align-items: center; }
.msg-row.own { justify-content: flex-end; }
.msg-row.not-own { justify-content: flex-start; }

.msg-row.sel { background: rgba(76, 175, 80, 0.08); border-radius: 10px; }
.msg-row.sel .bubble { box-shadow: 0 0 0 2px var(--accent); }

.msg-check {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  margin: 0 4px;
  padding: 0;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
  border-radius: 50%;
  opacity: 0;
  transition: opacity 0.15s;
}
.msg-row:hover .msg-check { opacity: 1; }
.msg-row .msg-check { opacity: 0; }
.msg-row.sel .msg-check { opacity: 1; color: var(--accent); font-weight: 700; }
.own .msg-check { order: 2; }
.not-own .msg-check { order: 0; }

.bubble {
  max-width: 60%;
  padding: 7px 11px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.4;
  user-select: text;
  -webkit-user-select: text;
}

.bubble .fwd {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 2px;
}
.bubble .fwd .fwd-icon { font-size: 12px; }

.bubble .reply-preview {
  background: rgba(127, 127, 127, 0.18);
  border-left: 2px solid var(--accent);
  border-radius: 6px;
  padding: 4px 8px;
  margin-bottom: 4px;
  cursor: pointer;
  max-width: 240px;
}
.own .bubble .reply-preview { border-left-color: #b3e5fc; background: rgba(255,255,255,0.12); }
.bubble .reply-preview .reply-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.own .bubble .reply-preview .reply-title { color: #b3e5fc; }
.bubble .reply-preview .reply-text {
  font-size: 12px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.own .bubble .reply-preview .reply-text { color: rgba(230,233,240,0.8); }

.reply-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 13px;
}
.reply-bar .reply-info { flex: 1; min-width: 0; }
.reply-bar .reply-title {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.reply-bar .reply-text {
  font-size: 12px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.reply-close {
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
}
.reply-close:hover { color: var(--text); }

.msg-row.flash .bubble { animation: flashbg 0.8s ease; }
@keyframes flashbg { 0% { background: var(--accent-soft); } 100% { background: transparent; } }

.selection-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
  font-size: 13px;
}
.selection-bar .mini-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  padding: 4px 10px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
.selection-bar .mini-btn:hover { background: var(--accent-soft); }

.own .bubble { background: var(--own); border-color: var(--own); }

.bubble .sname {
  font-size: 12px;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 2px;
}

.bubble .meta {
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: flex-end;
  margin-top: 3px;
  font-size: 11px;
  color: var(--text-dim);
}

.own .bubble .meta { color: rgba(230, 233, 240, 0.75); }
.own .bubble .meta .ticks.read { color: #4fc3f7; font-weight: 700; }
.own .bubble .meta .ticks { letter-spacing: -2px; }

.bubble img {
  display: block;
  max-width: 300px;
  max-height: 300px;
  border-radius: 8px;
  margin: 4px 0;
  cursor: pointer;
}

.bubble .file-card {
  display: flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  padding: 3px 0;
}

.bubble .file-card .ficon {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  background: var(--accent-soft);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  flex-shrink: 0;
}

.bubble .file-card .fname { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.bubble .file-card .fsize { font-size: 12px; color: var(--text-dim); }

.typing-hint { color: var(--accent); font-style: italic; font-size: 12px; padding: 6px 18px 0; min-height: 20px; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 14px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.composer textarea {
  flex: 1;
  resize: none;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 10px 12px;
  font-family: inherit;
  font-size: 15px;
  outline: none;
  max-height: 140px;
  min-height: 40px;
}

.composer textarea:focus { border-color: var(--accent); }

.send-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--bg);
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.attach-btn { font-size: 20px; color: var(--text-dim); width: 40px; height: 40px; border-radius: 50%; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.attach-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---------- Modal (new chat / search) ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.modal {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px;
}

.modal h3 { margin-bottom: 14px; font-size: 16px; }

.user-result {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: 9px;
  cursor: pointer;
}

.user-result:hover { background: var(--surface-2); }
.user-result .uname { font-weight: 600; font-size: 14px; }
.user-result .uemail { color: var(--text-dim); font-size: 12px; }
.user-result .uinfo { flex: 1; min-width: 0; }

.modal-close { float: right; color: var(--text-dim); font-size: 18px; }

/* ---------- Image lightbox ---------- */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.lightbox img {
  max-width: 96vw;
  max-height: 96vh;
  object-fit: contain;
  border-radius: 6px;
}
.lightbox-close {
  position: fixed;
  top: 12px;
  right: 18px;
  color: #fff;
  font-size: 28px;
  line-height: 1;
  z-index: 101;
  cursor: pointer;
}

/* ---------- Profile modal ---------- */
.profile-modal { text-align: center; }
.profile-head {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}
.profile-name { font-size: 18px; font-weight: 700; }
.profile-username { color: var(--text-dim); font-size: 13px; }
.profile-rows { text-align: left; }
.profile-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 4px;
  border-top: 1px solid var(--border);
}
.profile-label { color: var(--text-dim); font-size: 13px; }
.profile-value { font-size: 13px; word-break: break-word; }

.sname { cursor: pointer; }

/* ---------- Settings ---------- */
.setting-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 4px;
}

.setting-info { flex: 1; min-width: 0; }
.setting-title { font-weight: 600; font-size: 14px; }
.setting-sub { color: var(--text-dim); font-size: 12px; margin-top: 2px; }

.switch {
  position: relative;
  width: 46px;
  height: 26px;
  flex-shrink: 0;
  cursor: pointer;
}

.switch input { opacity: 0; width: 0; height: 0; }

.switch .slider {
  position: absolute;
  inset: 0;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 13px;
  transition: 0.2s;
}

.switch .slider::before {
  content: "";
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  top: 3px;
  background: var(--text-dim);
  border-radius: 50%;
  transition: 0.2s;
}

.switch input:checked + .slider { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(20px); background: var(--bg); }

/* ---------- Back button (mobile) ---------- */
.back-btn { display: none; }

.load-more { text-align: center; padding: 6px; }
.load-more button { color: var(--accent); font-size: 13px; padding: 4px 10px; }

.notif {
  position: fixed;
  top: 16px;
  right: 16px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 16px;
  font-size: 14px;
  z-index: 60;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  display: none;
  max-width: 320px;
}

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .sidebar { width: 100%; min-width: 100%; }
  .main { display: none; }
  .app.show-chat .sidebar { display: none; }
  .app.show-chat .main { display: flex; width: 100%; }
  .back-btn { display: flex; }
  .bubble { max-width: 82%; }
}
