/* ========================================================
   RESET & BASE
   ======================================================== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg-deepest:    #111b21;
  --bg-darker:     #1a2631;
  --bg-dark:       #202c33;
  --bg-panel:      #242f37;
  --bg-hover:      #2a3942;
  --bg-selected:   #2a3942;
  --bg-input:      #2a3942;
  --bg-bubble-in:  #202c33;
  --bg-bubble-out: #005c4b;
  --bg-modal:      #233138;

  --green:         #00a884;
  --green-light:   #00cf9d;
  --green-dark:    #005c4b;
  --teal:          #008069;

  --text-primary:  #e9edef;
  --text-secondary:#8696a0;
  --text-subtle:   #667781;
  --text-time:     #8696a0;

  --border:        #2a3942;
  --separator:     #1f2c33;

  --status-sent:      #8696a0;
  --status-delivered: #53bdeb;
  --status-read:      #53bdeb;

  --radius-sm:   8px;
  --radius-md:   12px;
  --radius-lg:   18px;
  --radius-full: 50%;

  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --shadow-md: 0 4px 20px rgba(0,0,0,.4);
  --shadow-lg: 0 8px 40px rgba(0,0,0,.6);
}

html, body {
  height: 100%;
  background: var(--bg-deepest);
  font-family: var(--font);
  color: var(--text-primary);
  font-size: 14px;
  overflow: hidden;
}

.hidden { display: none !important; }

/* ========================================================
   SETUP OVERLAY
   ======================================================== */
.setup-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,.85);
  backdrop-filter: blur(12px);
  display: flex; align-items: center; justify-content: center;
  animation: fadeIn .3s ease;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.setup-card {
  background: var(--bg-dark);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 40px;
  width: 440px;
  max-width: 95vw;
  box-shadow: var(--shadow-lg);
  animation: slideUp .35s cubic-bezier(.34,1.56,.64,1);
}

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

.setup-logo {
  display: flex; justify-content: center;
  margin-bottom: 16px;
  color: var(--green);
}

.setup-title {
  font-size: 22px; font-weight: 600;
  text-align: center; margin-bottom: 6px;
}

.setup-subtitle {
  text-align: center; color: var(--text-secondary);
  font-size: 13px; margin-bottom: 28px;
}

.form-group {
  margin-bottom: 18px;
}

.form-group label {
  display: flex; align-items: center; gap: 8px;
  font-size: 12px; font-weight: 600;
  color: var(--text-secondary); text-transform: uppercase;
  letter-spacing: .5px; margin-bottom: 6px;
}

.form-group input {
  width: 100%; background: var(--bg-input);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 10px 14px; color: var(--text-primary); font-size: 14px;
  outline: none; transition: border-color .2s;
  font-family: var(--font);
}

.form-group input:focus { border-color: var(--green); }

.input-with-eye { position: relative; }
.input-with-eye input { padding-right: 42px; }
.eye-btn {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; font-size: 16px; opacity: .7;
}
.eye-btn:hover { opacity: 1; }

.form-hint {
  display: block; font-size: 11px; color: var(--text-subtle);
  margin-top: 5px; line-height: 1.4;
}

.webhook-url-box {
  background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 10px 14px;
  font-size: 12px; color: var(--green); font-family: monospace;
  word-break: break-all; cursor: pointer;
  transition: border-color .2s;
}
.webhook-url-box:hover { border-color: var(--green); }

.badge {
  background: var(--green-dark); color: var(--green);
  font-size: 10px; padding: 2px 7px; border-radius: var(--radius-full);
  font-weight: 600; letter-spacing: .3px;
}

.btn-primary {
  width: 100%; padding: 12px; background: var(--green);
  border: none; border-radius: var(--radius-sm);
  color: #fff; font-size: 15px; font-weight: 600;
  cursor: pointer; margin-top: 10px; transition: background .2s, transform .1s;
  font-family: var(--font);
}
.btn-primary:hover  { background: var(--green-light); }
.btn-primary:active { transform: scale(.98); }

.setup-error {
  margin-top: 12px; color: #ff6b6b; font-size: 13px;
  text-align: center; background: rgba(255,107,107,.1);
  border-radius: var(--radius-sm); padding: 8px;
}

/* ========================================================
   MAIN APP LAYOUT
   ======================================================== */
.app {
  display: flex; height: 100vh; width: 100vw;
  background: var(--bg-deepest);
}

/* ========================================================
   SIDEBAR
   ======================================================== */
.sidebar {
  width: 360px; min-width: 280px; max-width: 420px;
  border-right: 1px solid var(--separator);
  display: flex; flex-direction: column;
  background: var(--bg-dark);
  overflow: hidden;
  flex-shrink: 0;
}

.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
  background: var(--bg-panel);
  height: 60px; flex-shrink: 0;
}

.my-avatar {
  width: 40px; height: 40px; border-radius: var(--radius-full);
  background: var(--bg-hover); display: flex; align-items: center;
  justify-content: center; overflow: hidden;
  color: var(--text-secondary); cursor: pointer;
  transition: opacity .2s;
}
.my-avatar:hover { opacity: .8; }

.sidebar-header-actions {
  display: flex; gap: 6px; align-items: center;
}

.icon-btn {
  background: none; border: none; cursor: pointer;
  color: var(--text-secondary); padding: 8px; border-radius: var(--radius-full);
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text-primary); }

.search-container { padding: 8px 12px; flex-shrink: 0; }

.search-box {
  display: flex; align-items: center; gap: 10px;
  background: var(--bg-input); border-radius: 8px;
  padding: 7px 14px;
}
.search-icon { color: var(--text-secondary); flex-shrink: 0; }
.search-box input {
  background: none; border: none; outline: none;
  color: var(--text-primary); font-size: 14px; width: 100%;
  font-family: var(--font);
}
.search-box input::placeholder { color: var(--text-subtle); }

.new-chat-bar {
  display: flex; gap: 8px; padding: 6px 12px;
  border-bottom: 1px solid var(--separator);
}
.new-chat-bar input {
  flex: 1; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 8px 12px;
  color: var(--text-primary); font-size: 13px; outline: none;
  font-family: var(--font);
}
.new-chat-bar input:focus { border-color: var(--green); }

.btn-pill {
  background: var(--green); border: none; border-radius: 20px;
  padding: 8px 16px; color: #fff; font-size: 13px; font-weight: 600;
  cursor: pointer; transition: background .2s; white-space: nowrap;
  font-family: var(--font);
}
.btn-pill:hover { background: var(--green-light); }

.conversations-list {
  flex: 1; overflow-y: auto;
  scrollbar-width: thin; scrollbar-color: var(--bg-hover) transparent;
}
.conversations-list::-webkit-scrollbar { width: 4px; }
.conversations-list::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 2px; }

.convo-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px;
  cursor: pointer; border-bottom: 1px solid var(--separator);
  transition: background .15s;
  position: relative;
}
.convo-item:hover   { background: var(--bg-hover); }
.convo-item.active  { background: var(--bg-selected); }

.convo-avatar {
  width: 48px; height: 48px; border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--teal), var(--green-dark));
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 700; color: #fff;
  flex-shrink: 0; overflow: hidden;
}
.convo-avatar img { width: 100%; height: 100%; object-fit: cover; }

.convo-info { flex: 1; min-width: 0; }

.convo-top {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 3px;
}
.convo-name {
  font-weight: 600; font-size: 15px; color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.convo-time { font-size: 11px; color: var(--text-time); white-space: nowrap; }

.convo-bottom {
  display: flex; justify-content: space-between; align-items: center;
}
.convo-preview {
  font-size: 13px; color: var(--text-secondary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  flex: 1;
}
.unread-badge {
  background: var(--green); color: #fff; font-size: 11px; font-weight: 700;
  border-radius: var(--radius-full); min-width: 20px; height: 20px;
  display: flex; align-items: center; justify-content: center;
  padding: 0 5px; flex-shrink: 0; margin-left: 8px;
}

.empty-state-sidebar {
  display: flex; flex-direction: column; align-items: center;
  padding: 60px 24px; color: var(--text-subtle); text-align: center;
  gap: 6px;
}
.empty-state-sidebar .sub { font-size: 12px; }

/* ========================================================
   CHAT PANEL
   ======================================================== */
.chat-panel {
  flex: 1; display: flex; flex-direction: column; overflow: hidden;
  background: var(--bg-deepest);
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23182229' fill-opacity='0.3'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

/* Welcome Screen */
.chat-welcome {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 40px; text-align: center; gap: 14px;
}
.welcome-icon { margin-bottom: 10px; }
.chat-welcome h2 { font-size: 24px; font-weight: 300; color: var(--text-primary); }
.chat-welcome p { color: var(--text-secondary); max-width: 380px; line-height: 1.6; font-size: 14px; }
.welcome-features { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin-top: 10px; }
.feature-pill {
  background: var(--bg-dark); border: 1px solid var(--border);
  border-radius: 20px; padding: 6px 14px; font-size: 12px; color: var(--text-secondary);
}

.chat-view { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

/* Chat Header */
.chat-header {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 16px; background: var(--bg-panel);
  border-bottom: 1px solid var(--separator);
  min-height: 60px; flex-shrink: 0;
}
.chat-header-avatar {
  width: 40px; height: 40px; border-radius: var(--radius-full);
  background: linear-gradient(135deg, var(--teal), var(--green-dark));
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 700; color: #fff;
  overflow: hidden; flex-shrink: 0;
}
.chat-header-info { flex: 1; }
.chat-header-name  { font-weight: 600; font-size: 16px; }
.chat-header-status{ font-size: 12px; color: var(--text-secondary); }
.chat-header-actions{ display: flex; gap: 4px; }

/* Messages */
.messages-area {
  flex: 1; overflow-y: auto; padding: 16px;
  display: flex; flex-direction: column; gap: 4px;
  scrollbar-width: thin; scrollbar-color: var(--bg-hover) transparent;
}
.messages-area::-webkit-scrollbar { width: 4px; }
.messages-area::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 2px; }

.date-separator {
  display: flex; align-items: center; justify-content: center;
  margin: 12px 0;
}
.date-separator span {
  background: var(--bg-panel); padding: 4px 12px;
  border-radius: 8px; font-size: 12px; color: var(--text-secondary);
}

.message-wrapper {
  display: flex; margin-bottom: 2px;
}
.message-wrapper.inbound  { justify-content: flex-start; }
.message-wrapper.outbound { justify-content: flex-end; }

.bubble {
  max-width: 65%; border-radius: var(--radius-lg);
  padding: 8px 12px; position: relative;
  word-break: break-word; line-height: 1.5;
  box-shadow: 0 1px 2px rgba(0,0,0,.3);
  animation: popIn .15s ease;
}
@keyframes popIn { from { transform: scale(.95); opacity: .5; } to { transform: scale(1); opacity: 1; } }

.bubble.inbound  {
  background: var(--bg-bubble-in);
  border-top-left-radius: 4px; color: var(--text-primary);
}
.bubble.outbound {
  background: var(--bg-bubble-out);
  border-top-right-radius: 4px; color: #fff;
}

.bubble-text { font-size: 14px; }
.bubble-meta {
  display: flex; align-items: center; justify-content: flex-end; gap: 4px;
  margin-top: 3px;
}
.bubble-time { font-size: 11px; opacity: .7; }
.bubble-status { font-size: 12px; }
.bubble-status.sent      { color: var(--status-sent); }
.bubble-status.delivered { color: var(--status-delivered); }
.bubble-status.read      { color: var(--status-read); }

/* Media bubbles */
.bubble-media {
  cursor: pointer; border-radius: var(--radius-sm);
  overflow: hidden; max-width: 280px; margin-bottom: 4px;
  transition: opacity .2s;
}
.bubble-media:hover { opacity: .9; }
.bubble-media img, .bubble-media video {
  width: 100%; display: block; border-radius: var(--radius-sm);
  max-height: 300px; object-fit: cover;
}
.bubble-media.audio-bubble {
  background: rgba(255,255,255,.08); padding: 8px 12px;
  display: flex; align-items: center; gap: 8px;
}
.audio-icon { font-size: 22px; }
.bubble-media.doc-bubble {
  background: rgba(255,255,255,.08); padding: 10px 14px;
  display: flex; align-items: center; gap: 10px;
}
.doc-icon { font-size: 28px; }
.doc-info { flex: 1; }
.doc-name { font-size: 13px; font-weight: 600; }
.doc-size { font-size: 11px; opacity: .7; }

/* Reaction badge */
.bubble-reaction {
  position: absolute; bottom: -12px; right: 8px;
  background: var(--bg-darker); border-radius: 12px;
  padding: 2px 6px; font-size: 14px; box-shadow: var(--shadow-md);
}

/* Typing indicator */
.typing-indicator {
  display: flex; align-items: center; gap: 4px;
  padding: 8px 16px;
}
.typing-indicator span {
  display: block; width: 8px; height: 8px;
  border-radius: 50%; background: var(--text-secondary);
  animation: bounce .8s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: .1s; }
.typing-indicator span:nth-child(3) { animation-delay: .2s; }
@keyframes bounce { 0%,80%,100% { transform: translateY(0); } 40% { transform: translateY(-6px); } }

/* Input bar */
.input-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 16px; background: var(--bg-panel);
  border-top: 1px solid var(--separator); flex-shrink: 0;
}

#message-input {
  flex: 1; background: var(--bg-input);
  border: none; border-radius: 8px;
  padding: 10px 16px; color: var(--text-primary);
  font-size: 14px; outline: none;
  font-family: var(--font); line-height: 1.5;
  transition: background .2s;
}
#message-input:focus { background: var(--bg-hover); }
#message-input::placeholder { color: var(--text-subtle); }

.send-btn {
  width: 42px; height: 42px; border-radius: var(--radius-full);
  background: var(--green); border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: #fff; transition: background .2s, transform .1s;
  flex-shrink: 0;
}
.send-btn:hover  { background: var(--green-light); }
.send-btn:active { transform: scale(.9); }

/* ========================================================
   ATTACHMENT PREVIEW
   ======================================================== */
.attachment-preview {
  position: fixed; inset: 0; z-index: 500;
  background: rgba(0,0,0,.8); backdrop-filter: blur(8px);
  display: flex; align-items: center; justify-content: center;
}
.preview-inner {
  background: var(--bg-modal); border-radius: 16px;
  padding: 24px; max-width: 500px; width: 90%;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  position: relative;
}
.preview-close {
  position: absolute; top: 12px; right: 12px;
  background: var(--bg-hover); border: none; color: var(--text-primary);
  border-radius: 50%; width: 28px; height: 28px; cursor: pointer; font-size: 14px;
}
#preview-img { max-width: 100%; max-height: 320px; border-radius: 8px; object-fit: contain; }
#preview-file-info { color: var(--text-secondary); font-size: 13px; text-align: center; }
.preview-caption-input {
  width: 100%; background: var(--bg-input); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 14px; color: var(--text-primary);
  font-size: 13px; outline: none; font-family: var(--font);
}
.preview-caption-input:focus { border-color: var(--green); }
.preview-send-btn { width: 100%; }

/* ========================================================
   EMOJI PICKER
   ======================================================== */
.emoji-picker {
  position: fixed; bottom: 72px; left: 368px;
  background: var(--bg-panel); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px; box-shadow: var(--shadow-lg);
  z-index: 400; width: 280px;
}
.emoji-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 4px; }
.emoji-grid button {
  background: none; border: none; font-size: 20px; cursor: pointer;
  border-radius: 6px; padding: 4px; transition: background .15s;
  display: flex; align-items: center; justify-content: center;
}
.emoji-grid button:hover { background: var(--bg-hover); }

/* ========================================================
   MEDIA MODAL
   ======================================================== */
.media-modal {
  position: fixed; inset: 0; z-index: 800;
  display: flex; align-items: center; justify-content: center;
}
.media-modal-backdrop {
  position: absolute; inset: 0;
  background: rgba(0,0,0,.88); backdrop-filter: blur(12px);
}
.media-modal-content {
  position: relative; z-index: 1;
  max-width: 90vw; max-height: 90vh;
  display: flex; flex-direction: column; align-items: center;
}
.media-modal-close {
  position: absolute; top: -40px; right: 0;
  background: rgba(255,255,255,.1); border: none; color: #fff;
  font-size: 18px; border-radius: 50%; width: 34px; height: 34px;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
}
#modal-img { max-width: 90vw; max-height: 80vh; border-radius: 8px; object-fit: contain; }
#modal-video { max-width: 90vw; max-height: 80vh; border-radius: 8px; }
#modal-audio { width: 320px; }
.doc-download {
  color: var(--green); font-size: 16px; font-weight: 600;
  text-decoration: none; padding: 16px 32px;
  background: var(--bg-panel); border-radius: 12px;
  border: 1px solid var(--border);
}
.doc-download:hover { border-color: var(--green); }

/* ========================================================
   SCROLLBAR GLOBAL
   ======================================================== */
* { scrollbar-width: thin; scrollbar-color: var(--bg-hover) transparent; }
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-thumb { background: var(--bg-hover); border-radius: 2px; }

/* ========================================================
   RESPONSIVE
   ======================================================== */
@media (max-width: 768px) {
  .sidebar { width: 100%; max-width: 100%; }
  .chat-panel { display: none; }
  .chat-panel.mobile-active { display: flex; }
  .sidebar.mobile-hidden { display: none; }
  .emoji-picker { left: 8px; }
}
