:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-2: #eef0f3;
  --border: #e0e2e7;
  --text: #1a1d23;
  --muted: #5f6672;
  --accent: #3d6be0;
  --accent-2: #2f56ba;
  --bubble-me: #3d6be0;
  --bubble-me-text: #ffffff;
  --bubble-other: #eef0f3;
  --danger: #d33b3b;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); font-size: 13px; }

.screen { height: 100%; width: 100%; }

/* ---- Auth ---- */
#auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
}
.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 360px;
}
.auth-card h1 { margin: 0 0 4px; font-size: 22px; }
.auth-card h2 { font-size: 16px; margin: 16px 0 12px; }
.auth-card input {
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
}
.auth-card button {
  width: 100%;
  padding: 10px 12px;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  color: white;
  font-weight: 600;
  cursor: pointer;
}
.auth-card button:hover { background: var(--accent-2); }
.switch { font-size: 13px; margin-top: 12px; text-align: center; }
.switch a { color: var(--accent); }

/* ---- App layout ---- */
#app-screen { display: flex; height: 100%; }

/* ---- Nav rail ---- */
#nav-rail {
  width: 84px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 16px 0;
}
.nav-items { display: flex; flex-direction: column; gap: 4px; align-items: center; }
.nav-item {
  width: 68px;
  padding: 10px 4px;
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 11px;
}
.nav-icon-wrap { position: relative; display: inline-flex; }
.nav-item .nav-icon { font-size: 20px; }
.unread-dot {
  position: absolute;
  top: -2px;
  right: -4px;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #3ecf6e;
  border: 2px solid var(--panel);
}
.nav-item:hover { background: var(--panel-2); color: var(--text); }
.nav-item.active { background: var(--panel-2); color: var(--accent); }
.nav-footer { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.nav-footer button {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 16px;
}

.view { flex: 1; display: flex; min-width: 0; }

/* ---- Chat view ---- */
#conversations-list {
  width: 260px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.conv-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
}
.conv-item:hover { background: var(--panel-2); }
.conv-item.active { background: var(--panel-2); border-left: 3px solid var(--accent); }
.conv-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex-shrink: 0;
}
.conv-info { min-width: 0; }
.conv-name { font-weight: 600; font-size: 14px; }
.conv-time { margin-top: 2px; }

#chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  text-align: center;
  padding: 24px;
}
.chat-empty p { margin: 4px 0; }
.header-right { display: flex; align-items: center; gap: 10px; }

#chat-view { display: flex; flex-direction: column; height: 100%; min-height: 0; }

#chat-header {
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
}
#chat-title { font-weight: 700; font-size: 15px; }
#back-to-list-btn {
  display: none; /* shown only in the mobile layout, see media query below */
  background: none;
  border: none;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  padding: 4px 8px 4px 0;
  flex-shrink: 0;
}
#message-list {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg-row { display: flex; flex-direction: column; max-width: 60%; }
.msg-row.me { align-self: flex-end; align-items: flex-end; }
.msg-row.other { align-self: flex-start; align-items: flex-start; }

.msg-sender { font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.msg-bubble {
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.4;
  word-wrap: break-word;
  white-space: pre-wrap;
  position: relative;
  cursor: pointer;
}
.msg-row.me .msg-bubble { background: var(--bubble-me); color: var(--bubble-me-text); border-bottom-right-radius: 4px; }
.msg-row.me .msg-bubble button { color: var(--bubble-me-text); border-color: rgba(255, 255, 255, 0.4); }
.msg-row.other .msg-bubble { background: var(--bubble-other); border-bottom-left-radius: 4px; }
.msg-bubble img { max-width: 260px; max-height: 260px; border-radius: 10px; display: block; }
.msg-bubble audio { max-width: 260px; }
.msg-bubble button {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
}
.msg-meta { font-size: 10px; color: var(--muted); margin-top: 3px; }
.msg-error { color: var(--danger); font-style: italic; }

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

.reaction-picker {
  position: absolute;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 4px 8px;
  display: flex;
  gap: 4px;
  z-index: 20;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
}
.reaction-picker span { cursor: pointer; font-size: 18px; padding: 2px; }
.reaction-picker span:hover { transform: scale(1.2); }

#typing-indicator { padding: 0 20px 4px; min-height: 16px; }

#composer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}
#composer button {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 50%;
  width: 38px;
  height: 38px;
  cursor: pointer;
  font-size: 16px;
  flex-shrink: 0;
}
#voice-btn.recording { background: var(--danger); border-color: var(--danger); color: white; }
#text-input {
  flex: 1;
  padding: 10px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
}
#send-btn { background: var(--accent); border: none; color: white; }
#send-btn:hover { background: var(--accent-2); }

/* ---- Users view ---- */
#view-users { flex-direction: column; padding: 24px 32px; overflow-y: auto; }
#view-users h2 { margin: 0 0 20px; font-size: 18px; }
#users-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 14px;
}
.user-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  text-align: center;
}
.user-avatar-wrap { position: relative; }
.user-avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 22px;
}
.presence-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: #7a7f8a;
  border: 2px solid var(--panel);
}
.presence-dot.online { background: #3ecf6e; }
.user-name { font-weight: 600; font-size: 14px; }
.chat-icon-btn {
  background: var(--accent);
  border: none;
  color: white;
  border-radius: 20px;
  padding: 7px 16px;
  cursor: pointer;
  font-size: 14px;
}
.chat-icon-btn:hover { background: var(--accent-2); }

/* ---- Mobile layout ---- */
@media (max-width: 768px) {
  #app-screen { flex-direction: column; }

  #nav-rail {
    width: 100%;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 8px 14px;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .nav-items { flex-direction: row; gap: 6px; }
  .nav-item {
    width: auto;
    flex-direction: row;
    gap: 6px;
    padding: 8px 12px;
    font-size: 13px;
  }
  .nav-item .nav-icon { font-size: 17px; }
  .nav-footer { flex-direction: row; gap: 10px; }

  /* Single-pane chat on narrow screens: show either the conversation list
     or the open conversation, never both side by side. */
  #conversations-list { width: 100%; }
  #view-chats.chat-open #conversations-list { display: none; }
  #view-chats:not(.chat-open) #chat-area { display: none; }
  #back-to-list-btn { display: inline-block; }

  .msg-row { max-width: 82%; }

  #view-users { padding: 16px; }
  #users-grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 10px; }
}
