/* ============================================
   ChatFlow — Main Stylesheet
   ============================================ */

/* ── CSS Reset & Variables ── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  /* Colors */
  --bg-primary:       #0f172a;
  --bg-secondary:     #1e293b;
  --bg-tertiary:      #334155;
  --bg-input:         #1e293b;
  --surface:          #1e293b;
  --surface-hover:    #273449;
  --border:           #334155;

  --accent:           #6366f1;
  --accent-light:     #818cf8;
  --accent-dark:      #4f46e5;
  --accent-glow:      rgba(99,102,241,0.35);

  --msg-out-bg:       #6366f1;
  --msg-out-text:     #ffffff;
  --msg-in-bg:        #273449;
  --msg-in-text:      #e2e8f0;

  --text-primary:     #f1f5f9;
  --text-secondary:   #94a3b8;
  --text-muted:       #64748b;

  --success:          #10b981;
  --warning:          #f59e0b;
  --danger:           #ef4444;

  /* Sizing */
  --sidebar-w:        280px;
  --header-h:         64px;
  --input-area-h:     76px;
  --radius-sm:        8px;
  --radius-md:        12px;
  --radius-lg:        18px;
  --radius-xl:        24px;
  --radius-full:      9999px;

  /* Transitions */
  --transition:       0.2s ease;
  --transition-slow:  0.35s ease;

  /* Shadows */
  --shadow-sm:        0 1px 3px rgba(0,0,0,0.3);
  --shadow-md:        0 4px 16px rgba(0,0,0,0.4);
  --shadow-accent:    0 4px 20px var(--accent-glow);
}

html { height: 100%; }

body {
  height: 100%;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: var(--radius-full); }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ── Utilities ── */
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }

/* ============================================
   APP SHELL
   ============================================ */
.app-shell {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

/* ============================================
   SIDEBAR
   ============================================ */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  height: 100%;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform var(--transition-slow);
  z-index: 100;
  flex-shrink: 0;
}

.sidebar-header {
  height: var(--header-h);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: #fff;
  box-shadow: var(--shadow-accent);
}

.brand-name {
  font-size: 17px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.3px;
}

.sidebar-close { display: none; }

/* ── New Chat Button ── */
.btn-new-chat {
  margin: 14px 12px;
  padding: 10px 16px;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff;
  border: none;
  border-radius: var(--radius-md);
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: opacity var(--transition), box-shadow var(--transition), transform var(--transition);
  box-shadow: var(--shadow-accent);
}

.btn-new-chat:hover {
  opacity: 0.9;
  transform: translateY(-1px);
  box-shadow: 0 6px 24px var(--accent-glow);
}

.btn-new-chat:active { transform: translateY(0); }

/* ── Conversations ── */
.conversations {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 8px;
}

.conversations-label {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 8px 8px 6px;
}

.conversation-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background var(--transition);
  position: relative;
}

.conversation-item:hover { background: var(--surface-hover); }
.conversation-item.active {
  background: rgba(99,102,241,0.15);
  border: 1px solid rgba(99,102,241,0.3);
}

.conv-avatar {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

.conv-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conv-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-preview {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-time {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* ── Sidebar Footer ── */
.sidebar-footer {
  border-top: 1px solid var(--border);
  padding: 12px 12px;
  flex-shrink: 0;
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, #7c3aed, #6366f1);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

.user-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.user-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
}

.user-status {
  font-size: 12px;
  color: var(--success);
  display: flex;
  align-items: center;
  gap: 4px;
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: var(--radius-full);
  background: var(--success);
  display: inline-block;
  animation: pulse-dot 2s infinite;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ── Sidebar Overlay ── */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 99;
  backdrop-filter: blur(2px);
}

/* ============================================
   CHAT AREA
   ============================================ */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-width: 0;
  background: var(--bg-primary);
}

/* ── Chat Header ── */
.chat-header {
  height: var(--header-h);
  padding: 0 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  flex-shrink: 0;
  z-index: 10;
}

.menu-toggle { display: none; }

.chat-peer {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
}

.peer-avatar {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

.peer-info { display: flex; flex-direction: column; }
.peer-name { font-size: 15px; font-weight: 600; color: var(--text-primary); }
.peer-status { font-size: 12px; color: var(--success); display: flex; align-items: center; gap: 4px; }

.chat-actions { display: flex; align-items: center; gap: 4px; }

/* ── Icon Buttons ── */
.btn-icon {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  transition: background var(--transition), color var(--transition);
}

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

/* ============================================
   MESSAGES CONTAINER
   ============================================ */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  position: relative;
  scroll-behavior: smooth;
}

.messages-inner {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* ── Date Divider ── */
.date-divider {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 20px 0 12px;
}

.date-divider::before,
.date-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

.date-divider span {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ── Message Rows ── */
.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 100%;
  animation: msg-in 0.28s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes msg-in {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.msg-row.outgoing {
  flex-direction: row-reverse;
  align-self: flex-end;
  max-width: 72%;
}

.msg-row.incoming {
  align-self: flex-start;
  max-width: 72%;
}

/* Group spacing */
.msg-row.same-sender { margin-top: 2px; }
.msg-row:not(.same-sender) { margin-top: 12px; }

/* ── Avatar ── */
.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
  align-self: flex-end;
  margin-bottom: 2px;
}

.msg-row.outgoing .msg-avatar { display: none; }
.msg-row.same-sender.incoming .msg-avatar { visibility: hidden; }

/* ── Bubble ── */
.msg-bubble {
  max-width: 100%;
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
  position: relative;
}

.incoming .msg-bubble {
  background: var(--msg-in-bg);
  color: var(--msg-in-text);
  border-bottom-left-radius: 4px;
}

.outgoing .msg-bubble {
  background: var(--msg-out-bg);
  color: var(--msg-out-text);
  border-bottom-right-radius: 4px;
  box-shadow: 0 2px 12px rgba(99,102,241,0.3);
}

/* Grouped bubble adjustments */
.msg-row.same-sender.incoming .msg-bubble { border-top-left-radius: var(--radius-sm); }
.msg-row.same-sender.outgoing .msg-bubble { border-top-right-radius: var(--radius-sm); }

/* ── Message Meta ── */
.msg-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-muted);
}

.msg-row.outgoing .msg-meta { flex-direction: row-reverse; }

.msg-status i { font-size: 11px; }
.msg-status .fa-check-double { color: var(--accent-light); }

/* ── Reactions ── */
.msg-reactions {
  position: absolute;
  bottom: -14px;
  right: 8px;
  display: flex;
  gap: 2px;
  font-size: 13px;
}

.msg-row.incoming .msg-reactions { right: auto; left: 8px; }

/* ── Typing Indicator ── */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 20px;
  max-width: 760px;
  margin: 8px auto 0;
  animation: msg-in 0.3s ease both;
}

.typing-avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
}

.typing-bubble {
  background: var(--msg-in-bg);
  border-radius: var(--radius-lg);
  border-bottom-left-radius: 4px;
  padding: 12px 16px;
  display: flex;
  gap: 5px;
  align-items: center;
}

.typing-bubble .dot {
  width: 7px;
  height: 7px;
  border-radius: var(--radius-full);
  background: var(--text-muted);
  animation: typing-bounce 1.2s infinite;
}

.typing-bubble .dot:nth-child(2) { animation-delay: 0.2s; }
.typing-bubble .dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ── Scroll to Bottom Button ── */
.scroll-bottom {
  position: absolute;
  bottom: 16px;
  right: 20px;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  box-shadow: 0 4px 16px rgba(99,102,241,0.5);
  transition: opacity var(--transition), transform var(--transition);
  z-index: 10;
  animation: fade-in 0.2s ease;
}

.scroll-bottom:hover { transform: translateY(-2px); }

@keyframes fade-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ============================================
   INPUT AREA
   ============================================ */
.input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  flex-shrink: 0;
}

.input-wrapper {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 4px;
  background: var(--bg-tertiary);
  border-radius: var(--radius-xl);
  padding: 8px 8px 8px 14px;
  border: 1.5px solid transparent;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.message-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  resize: none;
  max-height: 120px;
  overflow-y: auto;
  align-self: center;
}

.message-input::placeholder { color: var(--text-muted); }

.emoji-btn {
  width: 32px;
  height: 32px;
  font-size: 18px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.emoji-btn:hover { color: var(--warning); background: transparent; }

.attach-btn {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  font-size: 15px;
  color: var(--text-secondary);
}

/* ── Send Button ── */
.btn-send {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  flex-shrink: 0;
  transition: opacity var(--transition), transform var(--transition), box-shadow var(--transition);
  box-shadow: var(--shadow-accent);
}

.btn-send:not(:disabled):hover {
  transform: scale(1.06);
  box-shadow: 0 6px 24px var(--accent-glow);
}

.btn-send:not(:disabled):active { transform: scale(0.96); }

.btn-send:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  box-shadow: none;
}

/* ============================================
   EMPTY STATE
   ============================================ */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 16px;
  color: var(--text-muted);
  text-align: center;
  padding: 40px;
  animation: fade-in 0.4s ease;
}

.empty-state i {
  font-size: 56px;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.empty-state h2 { font-size: 22px; font-weight: 700; color: var(--text-primary); }
.empty-state p { font-size: 14px; max-width: 280px; }

/* ============================================
   RESPONSIVE — Mobile
   ============================================ */
@media (max-width: 680px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    transform: translateX(-100%);
  }

  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay.open { display: block; }
  .sidebar-close { display: flex; }
  .menu-toggle { display: flex; }

  .msg-row.outgoing,
  .msg-row.incoming {
    max-width: 88%;
  }

  .messages-inner { padding: 0 12px; }

  .input-area { padding: 10px 12px; }
}

@media (max-width: 400px) {
  .msg-row.outgoing,
  .msg-row.incoming {
    max-width: 95%;
  }
}
