:root {
  --bg: #0a0d15;
  --bg-elevated: #10141f;
  --surface: #151b28;
  --surface-2: #1b2233;
  --border: #262f42;
  --text: #edf0f7;
  --text-muted: #8b93a7;
  --accent: #d4a24e;
  --accent-bright: #f0c274;
  --accent-soft: rgba(212, 162, 78, 0.12);
  --accent-2: #4fb8a8;
  --success: #3ddc84;
  --danger: #ef5b5b;
  --font-display: "Avenir Next", "Century Gothic", ui-sans-serif, system-ui, sans-serif;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
}
* { box-sizing: border-box; }
html, body {
  height: 100%; margin: 0;
  background: var(--bg); color: var(--text);
  font-family: var(--font-body);
  overflow: hidden;
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

#app { display: flex; height: 100vh; }

/* ---------- Sidebar ---------- */
#sidebar {
  width: 320px; flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
}
.side-top {
  padding: 14px 16px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
}
.side-top .logo { width: 34px; height: 34px; border-radius: 50%; box-shadow: 0 4px 14px rgba(212,162,78,0.3); flex-shrink: 0; }
.side-top .brandname { font-family: var(--font-display); font-weight: 700; font-size: 16px; flex: 1; }
.side-top button { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 17px; padding: 6px; border-radius: 8px; }
.side-top button:hover { background: var(--surface-2); color: var(--text); }

.side-search { padding: 10px 14px; }
.side-search input {
  width: 100%; padding: 9px 12px; border-radius: 9px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-size: 13.5px;
}
.side-search input:focus { outline: 1px solid var(--accent); }

.side-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.side-section-label { font-size: 11px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase; color: var(--text-muted); padding: 14px 10px 6px; }

.side-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 10px;
  cursor: pointer; transition: background .15s ease;
}
.side-item:hover { background: var(--surface-2); }
.side-item.active { background: var(--accent-soft); }
.side-avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; position: relative;
  background: var(--surface-2); display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 15px; color: var(--accent);
}
.side-avatar.room { background: linear-gradient(135deg, var(--accent-2), #2c7d72); color: white; }
.presence-dot {
  position: absolute; bottom: -1px; right: -1px; width: 12px; height: 12px; border-radius: 50%;
  background: #555; border: 2px solid var(--bg-elevated);
}
.presence-dot.online { background: var(--success); animation: pulseDot 1.8s ease-in-out infinite; }
@keyframes pulseDot { 0%,100% { box-shadow: 0 0 0 0 rgba(61,220,132,.5);} 50% { box-shadow: 0 0 0 4px rgba(61,220,132,0);} }

.side-item-body { flex: 1; min-width: 0; }
.side-item-top { display: flex; justify-content: space-between; gap: 6px; }
.side-item-name { font-size: 14.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-item-time { font-size: 11px; color: var(--text-muted); flex-shrink: 0; }
.side-item-preview { font-size: 13px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.unread-badge {
  background: var(--accent); color: #17120a; font-size: 11px; font-weight: 800;
  min-width: 19px; height: 19px; border-radius: 10px; display: flex; align-items: center; justify-content: center;
  padding: 0 5px; flex-shrink: 0; animation: pop .2s ease;
}
@keyframes pop { from { transform: scale(0); } to { transform: scale(1); } }

.side-empty { padding: 16px 12px; font-size: 13px; color: var(--text-muted); }

/* ---------- Main panel ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.chat-header {
  padding: 13px 20px; border-bottom: 1px solid var(--border); background: var(--bg-elevated);
  display: flex; align-items: center; gap: 12px; flex-shrink: 0;
}
.chat-header .back-btn { display: none; background: none; border: none; color: var(--text); font-size: 20px; cursor: pointer; }
.chat-header .title { font-weight: 700; font-size: 15.5px; }
.chat-header .subtitle { font-size: 12px; color: var(--text-muted); }

#emptyState { flex: 1; display: flex; align-items: center; justify-content: center; flex-direction: column; gap: 10px; color: var(--text-muted); text-align: center; padding: 20px; }
#emptyState img { width: 84px; height: 84px; border-radius: 50%; margin-bottom: 6px; box-shadow: 0 12px 34px rgba(212,162,78,.22); opacity: .9; }
#emptyState .big { font-size: 18px; color: var(--text); font-weight: 600; font-family: var(--font-display); }
#emptyState div:last-child { max-width: 320px; font-size: 13.5px; }

#messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 4px; }
.msg-row { display: flex; gap: 9px; max-width: 72%; animation: msgIn .28s cubic-bezier(.2,.8,.3,1); margin-top: 10px; }
@keyframes msgIn { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: none; } }
.msg-row.own { align-self: flex-end; flex-direction: row-reverse; }
.msg-avatar { width: 28px; height: 28px; border-radius: 50%; background: var(--surface-2); display:flex;align-items:center;justify-content:center;font-size:11px;font-weight:700;color:var(--accent);flex-shrink:0; }
.msg-body { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.msg-sender { font-size: 12px; font-weight: 700; color: var(--accent-2); margin-left: 3px; }
.msg-bubble-wrap { position: relative; }
.msg-bubble {
  padding: 9px 13px; border-radius: 15px; font-size: 14.5px; line-height: 1.45; word-wrap: break-word;
  background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px;
}
.msg-row.own .msg-bubble { background: linear-gradient(135deg, var(--accent-bright), var(--accent)); color: #17120a; border: none; border-bottom-right-radius: 4px; border-bottom-left-radius: 15px; }
.msg-bubble img { max-width: 260px; border-radius: 10px; display: block; margin-top: 4px; cursor: zoom-in; }
.msg-meta { display: flex; align-items: center; gap: 5px; font-size: 10.5px; color: var(--text-muted); margin-top: 2px; }
.msg-row.own .msg-meta { justify-content: flex-end; }
.msg-meta .edited { font-style: italic; }
.msg-ticks { color: var(--text-muted); }
.msg-ticks.read { color: var(--accent-2); }

.msg-reactions { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.reaction-pill {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 100px;
  padding: 1px 7px; font-size: 12px; cursor: pointer; display: flex; align-items: center; gap: 3px;
  transition: transform .12s ease;
}
.reaction-pill:hover { transform: scale(1.08); border-color: var(--accent); }

.msg-actions {
  position: absolute; top: -14px; right: 4px; display: none; gap: 2px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 100px; padding: 3px; box-shadow: 0 4px 14px rgba(0,0,0,.3);
}
.msg-row.own .msg-actions { right: auto; left: 4px; }
.msg-bubble-wrap:hover .msg-actions { display: flex; }
.msg-actions button { background: none; border: none; cursor: pointer; font-size: 13px; padding: 4px 6px; border-radius: 50%; }
.msg-actions button:hover { background: var(--surface); }

.reaction-picker {
  position: absolute; bottom: 100%; margin-bottom: 6px; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 100px; padding: 5px 7px; display: flex; gap: 3px; box-shadow: 0 6px 20px rgba(0,0,0,.35);
  z-index: 5; animation: pop .15s ease;
}
.reaction-picker button { background: none; border: none; font-size: 18px; cursor: pointer; padding: 3px; border-radius: 50%; transition: transform .1s ease; }
.reaction-picker button:hover { transform: scale(1.25); }

.typing-row { display: flex; gap: 9px; margin-top: 6px; align-items: center; min-height: 30px; }
.typing-bubble { background: var(--surface); border: 1px solid var(--border); border-radius: 15px; border-bottom-left-radius: 4px; padding: 10px 14px; display: flex; gap: 4px; }
.typing-bubble span { width: 6px; height: 6px; border-radius: 50%; background: var(--text-muted); animation: bounce 1.2s infinite; }
.typing-bubble span:nth-child(2) { animation-delay: .15s; }
.typing-bubble span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); opacity: .5; } 30% { transform: translateY(-4px); opacity: 1; } }

/* ---------- Composer ---------- */
#composer { display: flex; gap: 9px; padding: 12px 16px; border-top: 1px solid var(--border); background: var(--bg-elevated); align-items: flex-end; flex-shrink: 0; }
#composer textarea {
  flex: 1; resize: none; max-height: 120px; padding: 10px 14px; border-radius: 14px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 14.5px; font-family: var(--font-body);
}
#composer textarea:focus { outline: 1px solid var(--accent); }
.composer-btn {
  width: 40px; height: 40px; border-radius: 50%; border: none; background: var(--surface); color: var(--text-muted);
  cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 17px; flex-shrink: 0;
  transition: background .15s ease, color .15s ease;
}
.composer-btn:hover { background: var(--surface-2); color: var(--text); }
#sendBtn { background: linear-gradient(135deg, var(--accent-bright), var(--accent)); color: #17120a; }
#sendBtn:hover { filter: brightness(1.08); }
#sendBtn:disabled { opacity: .4; }
#imagePreview { display: none; padding: 10px 16px 0; }
#imagePreview img { max-height: 90px; border-radius: 10px; }
#imagePreview button { margin-left: 8px; background: var(--danger); color: white; border: none; border-radius: 50%; width: 22px; height: 22px; cursor: pointer; }

/* ---------- Lightbox ---------- */
#lightbox { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.85); z-index: 100; align-items: center; justify-content: center; cursor: zoom-out; }
#lightbox img { max-width: 90vw; max-height: 90vh; border-radius: 8px; }

/* ---------- Toast ---------- */
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--surface-2); border: 1px solid var(--border); padding: 10px 18px; border-radius: 10px; font-size: 13.5px; opacity: 0; transition: all .25s ease; pointer-events: none; z-index: 200; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Mobile ---------- */
@media (max-width: 820px) {
  #sidebar { position: absolute; inset: 0; z-index: 10; width: 100%; transition: transform .25s ease; }
  #sidebar.hide { transform: translateX(-100%); }
  #main { width: 100%; }
  .chat-header .back-btn { display: block; }
  .msg-row { max-width: 85%; }
}

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: var(--surface-2); border-radius: 8px; }
