:root {
  --bg: #09090b;
  --surface: rgba(24, 24, 27, 0.72);
  --surface-solid: #141418;
  --surface-hover: rgba(255, 255, 255, 0.06);
  --border: rgba(255, 255, 255, 0.09);
  --text: #f4f4f5;
  --text-muted: #a1a1aa;
  --accent: #6366f1;
  --accent-2: #06b6d4;
  --bubble-me: linear-gradient(135deg, #6366f1, #4f46e5);
  --bubble-other: rgba(39, 39, 42, 0.85);
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #f43f5e;
  --radius: 18px;
  --shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
}

[data-theme="light"] {
  --bg: #f4f5f7;
  --surface: rgba(255, 255, 255, 0.82);
  --surface-solid: #ffffff;
  --surface-hover: rgba(0, 0, 0, 0.05);
  --border: rgba(15, 23, 42, 0.09);
  --text: #0f172a;
  --text-muted: #64748b;
  --bubble-other: #ffffff;
  --shadow: 0 20px 50px rgba(15, 23, 42, 0.08);
}

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

html, body {
  height: 100%;
  height: 100dvh;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

.hidden { display: none !important; }

/* Ambient Background (GPU-friendly radial gradients, zero blur lag) */
.ambient-bg {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}
.orb {
  position: absolute;
  border-radius: 50%;
  opacity: 0.22;
}
.orb-1 {
  width: 640px; height: 640px;
  background: radial-gradient(circle, var(--accent) 0%, transparent 70%);
  top: -180px; left: -160px;
}
.orb-2 {
  width: 580px; height: 580px;
  background: radial-gradient(circle, var(--accent-2) 0%, transparent 70%);
  bottom: -200px; right: -140px;
}

.glass {
  background: var(--surface);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid var(--border);
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); }
.icon-btn:active { transform: scale(0.94); }
.icon-btn.danger:hover { color: var(--danger); background: rgba(244, 63, 94, 0.12); }

/* Login View */
.login-view {
  position: relative;
  z-index: 2;
  height: 100%;
  display: grid;
  place-items: center;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 410px;
  padding: 36px 32px;
  border-radius: 26px;
  box-shadow: none;
  animation: popIn 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes popIn {
  from { opacity: 0; transform: translateY(14px) scale(0.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 18px;
}
.brand-icon {
  width: 34px; height: 34px;
  border-radius: 11px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid;
  place-items: center;
  font-size: 16px;
}
.brand-badge {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: var(--accent-2);
  background: rgba(6, 182, 212, 0.12);
  padding: 4px 10px;
  border-radius: 999px;
}

.login-card h1 {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 8px;
}
.login-sub {
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-muted);
  margin-bottom: 24px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.password-field {
  position: relative;
  display: flex;
  align-items: center;
}
.password-field input {
  width: 100%;
  height: 50px;
  padding: 0 46px 0 16px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}
[data-theme="light"] .password-field input {
  background: rgba(15, 23, 42, 0.03);
}
.password-field input:focus {
  border-color: var(--accent);
  box-shadow: none;
}
.password-field .icon-btn {
  position: absolute;
  right: 8px;
}

.primary-btn {
  height: 50px;
  border: none;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--accent), #4f46e5);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: transform 0.12s, filter 0.15s;
}
.primary-btn:hover { filter: brightness(1.08); }
.primary-btn:active { transform: scale(0.98); }
.primary-btn:disabled { opacity: 0.6; cursor: wait; }

.login-error {
  margin-top: 12px;
  font-size: 13px;
  color: var(--danger);
  min-height: 18px;
}

/* Chat Shell */
.chat-shell {
  position: relative;
  z-index: 2;
  display: flex;
  height: 100%;
  width: 100%;
}

/* Sidebar */
.sidebar {
  width: 280px;
  height: 100%;
  display: flex;
  flex-direction: column;
  border-top: none;
  border-bottom: none;
  border-left: none;
  flex-shrink: 0;
  z-index: 20;
}

.sidebar-top {
  padding: 18px 16px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}

.logo-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.logo-dot {
  width: 28px; height: 28px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid;
  place-items: center;
  font-size: 14px;
}
.logo-text {
  font-weight: 700;
  font-size: 16px;
  letter-spacing: -0.02em;
}

.conn-pill {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(245, 158, 11, 0.15);
  color: var(--warning);
}
.conn-pill.ws {
  background: rgba(16, 185, 129, 0.15);
  color: var(--success);
}
.conn-pill.poll {
  background: rgba(6, 182, 212, 0.15);
  color: var(--accent-2);
}

.sidebar-section {
  padding: 16px 12px 6px;
}
.sidebar-section.flex-1 {
  flex: 1;
  overflow-y: auto;
}

.section-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 0 8px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.count-pill {
  background: var(--surface-hover);
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 11px;
  color: var(--text);
}

.room-list {
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.room-item {
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text-muted);
  padding: 9px 12px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background 0.15s, color 0.15s;
}
.room-item:hover { background: var(--surface-hover); color: var(--text); }
.room-item.active {
  background: rgba(99, 102, 241, 0.16);
  color: var(--text);
  font-weight: 600;
}
.room-item .hash { color: var(--accent); font-weight: 700; }
.unread-dot {
  margin-left: auto;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent-2);
}

.online-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.online-user {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border-radius: 11px;
  font-size: 13.5px;
}
.avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 12px;
  color: #fff;
  flex-shrink: 0;
  position: relative;
}
.avatar.online::after {
  content: '';
  position: absolute;
  bottom: -1px; right: -1px;
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--success);
  border: 2px solid var(--surface-solid);
}

.profile-bar {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
.profile-meta {
  flex: 1;
  min-width: 0;
}
.profile-name {
  background: none;
  border: none;
  color: var(--text);
  font-size: 13.5px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  max-width: 100%;
}
.profile-name span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.profile-status {
  display: block;
  font-size: 11px;
  color: var(--success);
}
.profile-actions {
  display: flex;
  gap: 2px;
}

/* Main Chat Area */
.chat-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}

.chat-header {
  height: 64px;
  padding: 0 20px;
  border-top: none;
  border-left: none;
  border-right: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}
.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}
.room-title-wrap h2 {
  font-size: 16px;
  font-weight: 700;
}
.room-subtitle {
  font-size: 12px;
  color: var(--text-muted);
}
.latency-badge {
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--surface-hover);
  color: var(--text-muted);
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px 22px 10px;
  overscroll-behavior: contain;
}
.messages-list {
  max-width: 900px;
  margin: 0 auto;
  padding-top: 26px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  margin-top: 10px;
  animation: msgIn 0.18s cubic-bezier(0.16, 1, 0.3, 1);
}
.msg-row.grouped { margin-top: 2px; }
.msg-row.me { flex-direction: row-reverse; }

@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg-avatar-slot { width: 32px; flex-shrink: 0; }

.msg-content {
  max-width: min(72%, 560px);
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  position: relative;
}
.msg-row.me .msg-content { align-items: flex-end; }

.msg-meta {
  font-size: 11.5px;
  color: var(--text-muted);
  margin: 0 6px 4px;
  display: flex;
  gap: 8px;
}

.msg-bubble {
  max-width: 100%;
  padding: 10px 15px;
  border-radius: 18px;
  background: var(--bubble-other);
  border: 1px solid var(--border);
  font-size: 14.5px;
  line-height: 1.48;
  overflow-wrap: anywhere;
  word-wrap: break-word;
  word-break: break-word;
  white-space: pre-wrap;
  position: relative;
}
.msg-row.me .msg-bubble {
  background: var(--bubble-me);
  color: #fff;
  border-color: transparent;
}
.msg-row.pending .msg-bubble { opacity: 0.72; }

.msg-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
}
.reaction-chip {
  font-size: 12px;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  cursor: pointer;
}

.quick-react-bar {
  position: absolute;
  bottom: calc(100% - 2px);
  right: 4px;
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 4px 6px;
  border-radius: 999px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  box-shadow: none;
  z-index: 10;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  white-space: nowrap;
  transition: opacity 0.12s ease, visibility 0.12s ease;
}
.quick-react-bar::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 100%;
  height: 8px;
}
.msg-row.me .quick-react-bar { right: 4px; left: auto; }
.msg-row:not(.me) .quick-react-bar { left: 4px; right: auto; }
.msg-content:hover .quick-react-bar,
.msg-content:focus-within .quick-react-bar {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.typing-bar {
  height: 22px;
  padding: 0 24px;
  font-size: 12px;
  color: var(--text-muted);
  max-width: 944px;
  width: 100%;
  margin: 0 auto;
}

.composer-wrap {
  padding: 8px 20px calc(18px + env(safe-area-inset-bottom, 0px));
}
.composer {
  max-width: 900px;
  margin: 0 auto;
  padding: 8px 10px;
  border-radius: 22px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  box-shadow: none;
}
.composer textarea {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: inherit;
  font-size: 14.5px;
  line-height: 1.45;
  resize: none;
  max-height: 140px;
  padding: 7px 4px;
  outline: none;
}

.send-btn {
  width: 38px; height: 38px;
  border-radius: 14px;
  border: none;
  background: linear-gradient(135deg, var(--accent), #4f46e5);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  transition: transform 0.12s, filter 0.15s;
}
.send-btn:hover { filter: brightness(1.1); }
.send-btn:active { transform: scale(0.93); }

.emoji-trigger-wrap { position: relative; }
.emoji-popover {
  position: absolute;
  bottom: 46px;
  left: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 8px;
  border-radius: 16px;
  box-shadow: none;
  z-index: 30;
}
.emoji-popover button {
  background: none; border: none; font-size: 18px; padding: 6px; border-radius: 8px; cursor: pointer;
}
.emoji-popover button:hover { background: var(--surface-hover); }

.scroll-bottom-btn {
  position: absolute;
  bottom: 92px;
  right: 28px;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--accent);
  color: #fff;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: none;
  z-index: 15;
}

.toast {
  position: fixed;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  padding: 10px 18px;
  border-radius: 999px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 500;
  box-shadow: none;
  z-index: 50;
}

/* Rename Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 60;
}
.modal-card {
  width: 100%;
  max-width: 360px;
  padding: 24px;
  border-radius: 22px;
  box-shadow: none;
}
.modal-card h3 {
  font-size: 17px;
  margin-bottom: 14px;
}
.modal-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal-form input {
  height: 44px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
  font-size: 14.5px;
  outline: none;
}
[data-theme="light"] .modal-form input {
  background: rgba(15, 23, 42, 0.04);
}
.modal-form input:focus {
  border-color: var(--accent);
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
.secondary-btn {
  height: 40px;
  padding: 0 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}
.primary-btn.compact {
  height: 40px;
  padding: 0 18px;
}

/* Custom Scrollbar */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(161, 161, 170, 0.25); border-radius: 999px; }
::-webkit-scrollbar-thumb:hover { background: rgba(161, 161, 170, 0.45); }

.mobile-only { display: none; }

/* Tablet / Small Laptop */
@media (max-width: 1024px) {
  .sidebar { width: 245px; }
}

/* Mobile & Compact Screens */
@media (max-width: 820px) {
  .mobile-only { display: inline-flex; }
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    width: min(290px, 84vw);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    transform: translateX(-100%);
    transition: transform 0.24s cubic-bezier(0.16, 1, 0.3, 1);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 15;
  }
  .chat-header {
    height: 58px;
    padding: 0 12px;
  }
  .messages-container {
    padding: 14px 12px 8px;
  }
  .msg-content { max-width: 86%; }
  .composer-wrap {
    padding: 6px 10px calc(12px + env(safe-area-inset-bottom, 0px));
  }
  .typing-bar { padding: 0 14px; }
  .scroll-bottom-btn { right: 14px; bottom: 82px; }
  /* Prevent iOS auto-zoom on input focus */
  input, textarea { font-size: 16px !important; }
}

/* Very Small Phones (< 400px) */
@media (max-width: 400px) {
  .login-card { padding: 26px 20px; border-radius: 20px; }
  .latency-badge { display: none; }
  .msg-content { max-width: 90%; }
  .msg-avatar-slot { width: 28px; }
  .msg-avatar-slot .avatar { width: 28px; height: 28px; font-size: 11px; }
}

/* Ultrawide / 4K Monitors */
@media (min-width: 1440px) {
  .messages-list, .composer { max-width: 1080px; }
}
