:root {
  --green: #075e54;
  --green-dark: #054d44;
  --green-light: #25d366;
  --chat-bg: #e5ddd5;
  --bubble-in: #fff;
  --bubble-out: #dcf8c6;
  --text: #111b21;
  --muted: #667781;
  --border: #e9edef;
  --header-h: 56px;
  --input-h: 56px;
  --sidebar-w: 380px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: #d1d7db;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}

/* ─── APP CONTAINER ──────────────────────── */

.app-container {
  display: flex;
  height: 100%;
  width: 100%;
  overflow: hidden;
}

/* ─── SCREENS (mobile default) ───────────── */

.screen {
  display: none;
  flex-direction: column;
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0; left: 0;
}

/* ─── LOGIN ───────────────────────────────── */

#screen-login {
  background: var(--green);
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.login-box {
  background: #fff;
  border-radius: 12px;
  padding: 36px 28px;
  width: 100%;
  max-width: 360px;
  text-align: center;
  box-shadow: 0 8px 40px rgba(0,0,0,0.25);
}

.login-box h1 {
  font-size: 22px;
  color: var(--green);
  margin-bottom: 4px;
}

.login-box p {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 24px;
}

.login-box input {
  display: block;
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  margin-bottom: 12px;
  outline: none;
  transition: border-color 0.2s;
}

.login-box input:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 3px rgba(7,94,84,0.1);
}

.login-box button {
  width: 100%;
  padding: 12px;
  background: var(--green);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.login-box button:hover:not(:disabled) {
  background: var(--green-dark);
}

.login-box button:disabled {
  opacity: 0.6;
}

.login-error {
  color: #e53e3e;
  font-size: 13px;
  margin-top: 8px;
  min-height: 18px;
}

/* ─── CONVERSATIONS ───────────────────────── */

#screen-conversations {
  background: #fff;
  border-right: 1px solid var(--border);
}

.top-bar {
  background: var(--green);
  color: #fff;
  padding: 0 16px;
  padding-top: var(--safe-top);
  height: calc(var(--header-h) + var(--safe-top));
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.top-bar h2 {
  font-size: 18px;
  font-weight: 600;
  flex: 1;
}

.refresh-btn {
  background: none;
  border: none;
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 50%;
  transition: background 0.2s;
  line-height: 1;
}
.refresh-btn:hover {
  background: rgba(255,255,255,0.15);
}
.refresh-btn:active {
  background: rgba(255,255,255,0.25);
}
.refresh-btn.spinning {
  animation: spin 0.6s linear;
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

.search-bar {
  padding: 8px 12px;
  background: #f0f2f5;
  flex-shrink: 0;
}

.search-bar input {
  width: 100%;
  padding: 8px 14px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  background: #fff;
  outline: none;
  transition: box-shadow 0.2s;
}

.search-bar input:focus {
  box-shadow: 0 0 0 2px rgba(7,94,84,0.15);
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.conv-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}

.conv-item:hover {
  background: #f5f6f6;
}

.conv-item:active {
  background: #e9edef;
}

.conv-item.active {
  background: #f0f2f5;
}

.conv-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  color: #fff;
  font-size: 20px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--green-light);
}

.conv-body {
  flex: 1;
  min-width: 0;
}

.conv-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.conv-name {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.conv-preview {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.conv-taken {
  font-size: 10px;
  font-weight: 700;
  background: #fff3cd;
  color: #856404;
  padding: 1px 6px;
  border-radius: 4px;
  flex-shrink: 0;
}

.empty-text {
  text-align: center;
  padding: 40px 20px;
  color: var(--muted);
  font-size: 14px;
}

/* ─── CHAT ────────────────────────────────── */

#screen-chat {
  background: var(--chat-bg);
}

.chat-header {
  background: var(--green);
  color: #fff;
  padding: 0 12px;
  padding-top: var(--safe-top);
  height: calc(var(--header-h) + var(--safe-top));
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.chat-back {
  background: none;
  border: none;
  color: #fff;
  font-size: 22px;
  cursor: pointer;
  padding: 4px 8px;
  line-height: 1;
  border-radius: 50%;
  transition: background 0.2s;
}

.chat-back:hover {
  background: rgba(255,255,255,0.15);
}

.chat-contact {
  flex: 1;
  min-width: 0;
}

.chat-contact-name {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-contact-phone {
  font-size: 11px;
  opacity: 0.8;
}

.chat-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.chat-actions button {
  padding: 5px 12px;
  border: 1px solid rgba(255,255,255,0.5);
  border-radius: 6px;
  background: transparent;
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.chat-actions button:hover {
  background: rgba(255,255,255,0.15);
}

.chat-actions button:active {
  background: rgba(255,255,255,0.25);
}

#status-badge {
  display: none;
  font-size: 10px;
  padding: 2px 8px;
  border-radius: 10px;
  background: #fff3cd;
  color: #856404;
}

/* ─── CHAT PLACEHOLDER (desktop, no chat open) */

.chat-placeholder {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #f0f2f5;
  color: var(--muted);
  gap: 12px;
  user-select: none;
}

.chat-placeholder-icon {
  font-size: 72px;
  opacity: 0.3;
}

.chat-placeholder-text {
  font-size: 16px;
  color: var(--muted);
}

/* Message area */
.msg-area {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg {
  max-width: 65%;
  padding: 6px 10px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  position: relative;
  box-shadow: 0 1px 1px rgba(0,0,0,0.06);
}

.msg.in {
  align-self: flex-start;
  background: var(--bubble-in);
  border-top-left-radius: 0;
}

.msg.out {
  align-self: flex-end;
  background: var(--bubble-out);
  border-top-right-radius: 0;
}

.msg-label {
  font-size: 11px;
  font-weight: 700;
  color: #2b6cb0;
  margin-bottom: 2px;
}

.msg-label.bot {
  color: var(--green);
}

.msg-text {
  white-space: pre-wrap;
}

.msg-time {
  font-size: 10px;
  color: var(--muted);
  text-align: right;
  margin-top: 2px;
}

.msg-date {
  align-self: center;
  padding: 4px 12px;
  border-radius: 6px;
  background: rgba(0,0,0,0.06);
  font-size: 11px;
  color: #555;
  margin: 6px 0;
}

/* Input bar */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px;
  padding-bottom: calc(8px + var(--safe-bottom));
  background: #f0f2f5;
  flex-shrink: 0;
}

.input-bar textarea {
  flex: 1;
  border: none;
  border-radius: 20px;
  padding: 10px 16px;
  font-size: 15px;
  font-family: inherit;
  resize: none;
  max-height: 100px;
  outline: none;
  line-height: 1.4;
}

.input-bar button {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--green-light);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.2s;
}

.input-bar button:hover:not(:disabled) {
  background: #20c05c;
}

.input-bar button:disabled {
  opacity: 0.5;
}

/* ─── UNREAD BADGE ───────────────────────── */

.unread-badge {
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 10px;
  background: var(--green-light);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.total-unread {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: #e53e3e;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  display: none;
  align-items: center;
  justify-content: center;
  margin-left: 8px;
}

.total-unread.visible {
  display: flex;
}

.conv-name.unread {
  font-weight: 700;
  color: #000;
}

.conv-time.unread {
  color: var(--green-light);
}

.conv-preview.unread {
  color: var(--text);
  font-weight: 500;
}

/* ─── SSE STATUS DOT ─────────────────────── */

.sse-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #e53e3e;
  margin-left: 8px;
  flex-shrink: 0;
}

.sse-dot.connected {
  background: var(--green-light);
}

.sse-dot.disconnected {
  background: #e53e3e;
}

.sse-dot.reconnecting {
  background: #f6ad55;
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* ─── MEDIA IN MESSAGES ──────────────────── */

.msg-media {
  margin: 4px -4px 4px -4px;
}

.msg-image {
  max-width: 100%;
  max-height: 280px;
  border-radius: 6px;
  cursor: pointer;
  display: block;
  object-fit: cover;
}

.msg-audio {
  width: 100%;
  max-width: 260px;
  height: 36px;
  border-radius: 18px;
}

.msg-audio::-webkit-media-controls-panel {
  background: transparent;
}

/* Image lightbox overlay */
.img-overlay {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background: rgba(0,0,0,0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  cursor: pointer;
}

.img-overlay img {
  max-width: 95%;
  max-height: 90vh;
  border-radius: 4px;
  object-fit: contain;
}

/* ─── DESKTOP LAYOUT (≥768px) ────────────── */

@media (min-width: 768px) {
  body {
    background: #d1d7db;
  }

  .app-container {
    max-width: 1600px;
    margin: 0 auto;
    height: 100vh;
    box-shadow: 0 0 20px rgba(0,0,0,0.15);
  }

  /* Override absolute positioning for desktop */
  .screen {
    position: relative;
    top: auto;
    left: auto;
  }

  #screen-login {
    position: fixed;
    top: 0; left: 0;
    width: 100%;
    height: 100%;
  }

  /* Conversations: fixed-width left panel */
  #screen-conversations {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    max-width: var(--sidebar-w);
    flex-shrink: 0;
  }

  /* Chat: fills remaining space */
  #screen-chat {
    flex: 1;
    min-width: 0;
    border-left: 1px solid var(--border);
  }

  /* Chat placeholder for desktop */
  .chat-placeholder {
    flex: 1;
    min-width: 0;
    border-left: 1px solid var(--border);
  }

  /* Hide back button on desktop */
  .chat-back {
    display: none;
  }

  /* Wider msg bubbles on desktop */
  .msg {
    max-width: 55%;
  }

  .msg-area {
    padding: 12px 24px;
  }

  .input-bar {
    padding: 10px 16px;
  }
}

/* ─── LARGE DESKTOP (≥1200px) ────────────── */

@media (min-width: 1200px) {
  :root {
    --sidebar-w: 420px;
  }

  .msg {
    max-width: 50%;
  }
}

/* ─── MOBILE ONLY (<768px) ───────────────── */

@media (max-width: 767px) {
  body {
    background: #f0f2f5;
  }

  .app-container {
    display: block;
    position: relative;
  }

  .screen {
    position: absolute;
    top: 0; left: 0;
  }

  .chat-placeholder {
    display: none !important;
  }

  .msg {
    max-width: 80%;
  }
}

/* ─── SCROLLBAR STYLING ──────────────────── */

.conv-list::-webkit-scrollbar,
.msg-area::-webkit-scrollbar {
  width: 6px;
}

.conv-list::-webkit-scrollbar-thumb,
.msg-area::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.15);
  border-radius: 3px;
}

.conv-list::-webkit-scrollbar-thumb:hover,
.msg-area::-webkit-scrollbar-thumb:hover {
  background: rgba(0,0,0,0.25);
}
