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

:root {
  --bg: #0f0f0f;
  --surface: #1a1a2e;
  --surface2: #16213e;
  --surface3: #1e2a4a;
  --primary: #0f3460;
  --accent: #e94560;
  --text: #eee;
  --text-dim: #888;
  --text-dark: #555;
  --online: #00c853;
  --offline: #666;
  --sent: #888;
  --delivered: #4fc3f7;
  --seen: #00c853;
  --border: #222;
  --sidebar-width: 320px;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

.screen { height: 100vh; }
.hidden { display: none !important; }

/* ---- LOGIN ---- */
#login-screen {
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #0f0f0f 0%, #1a1a2e 50%, #0f3460 100%);
}

.login-card {
  background: var(--surface);
  padding: 48px 40px;
  border-radius: 16px;
  width: 360px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  text-align: center;
}

.login-card h1 { font-size: 28px; margin-bottom: 4px; }

.subtitle {
  color: var(--text-dim);
  margin-bottom: 32px;
  font-size: 14px;
}

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

.login-card input {
  padding: 14px 16px;
  border: 1px solid #333;
  border-radius: 10px;
  background: #111;
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.2s;
}

.login-card input:focus { border-color: var(--accent); }

.login-card button {
  padding: 14px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
  margin-top: 6px;
}

.login-card button:hover { opacity: 0.85; }

.error {
  color: var(--accent);
  margin-top: 14px;
  font-size: 14px;
  min-height: 20px;
}

.msg-feedback {
  margin-top: 10px;
  font-size: 13px;
  min-height: 18px;
}

.msg-feedback.success { color: var(--online); }
.msg-feedback.error { color: var(--accent); }

/* ---- APP LAYOUT ---- */
#app-screen {
  display: flex;
  height: 100vh;
}

/* ---- SIDEBAR ---- */
#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.sidebar-user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
}

.role-badge {
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--accent);
  color: white;
  letter-spacing: 0.5px;
}

.sidebar-actions {
  display: flex;
  gap: 4px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  border: 1px solid #333;
  border-radius: 8px;
  background: transparent;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ---- SEARCH ---- */
.sidebar-search {
  padding: 12px 16px 8px;
  position: relative;
}

.sidebar-search input {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid #333;
  border-radius: 10px;
  background: #111;
  color: var(--text);
  font-size: 13px;
  outline: none;
  transition: border-color 0.2s;
}

.sidebar-search input:focus { border-color: var(--accent); }

#search-results {
  position: absolute;
  top: 100%;
  left: 16px;
  right: 16px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  max-height: 240px;
  overflow-y: auto;
  z-index: 50;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

.search-result-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  cursor: pointer;
  transition: background 0.15s;
  font-size: 13px;
}

.search-result-item:hover { background: var(--surface3); }

.search-result-item .username { font-weight: 600; }

.search-result-item .add-btn {
  padding: 4px 12px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: transparent;
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.search-result-item .add-btn:hover {
  background: var(--accent);
  color: white;
}

.search-result-item .add-btn.added {
  border-color: var(--online);
  color: var(--online);
  pointer-events: none;
}

/* ---- SIDEBAR TABS ---- */
.sidebar-tabs {
  display: flex;
  padding: 0 16px;
  gap: 4px;
  margin-bottom: 4px;
}

.sidebar-tab {
  flex: 1;
  padding: 8px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sidebar-tab.active {
  background: var(--surface2);
  color: var(--text);
}

/* ---- SIDEBAR LISTS ---- */
.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.sidebar-list::-webkit-scrollbar { width: 4px; }
.sidebar-list::-webkit-scrollbar-track { background: transparent; }
.sidebar-list::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; }

.conv-item, .contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.15s;
  position: relative;
}

.conv-item:hover, .contact-item:hover { background: var(--surface2); }
.conv-item.active { background: var(--primary); }

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--surface2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  color: var(--accent);
  flex-shrink: 0;
  position: relative;
}

.avatar .online-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--surface);
  background: var(--online);
}

.conv-info, .contact-info {
  flex: 1;
  min-width: 0;
}

.conv-name, .contact-name {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 2px;
}

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

.conv-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex-shrink: 0;
}

.conv-time {
  font-size: 10px;
  color: var(--text-dim);
}

.unread-badge {
  background: var(--accent);
  color: white;
  font-size: 10px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
}

.contact-presence {
  font-size: 11px;
  color: var(--text-dim);
}

.contact-presence.online { color: var(--online); }

.contact-chat-btn {
  padding: 6px 14px;
  border: 1px solid #444;
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  font-size: 11px;
  cursor: pointer;
  transition: all 0.2s;
}

.contact-chat-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ---- MAIN AREA ---- */
#main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.center-message {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 15px;
}

/* ---- CHAT AREA ---- */
#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#chat-header {
  padding: 16px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
}

.chat-header-info {
  display: flex;
  flex-direction: column;
}

#chat-partner-name {
  font-weight: 700;
  font-size: 16px;
}

.presence-text {
  font-size: 12px;
  color: var(--text-dim);
}

.presence-text.online { color: var(--online); }

/* ---- MESSAGES ---- */
#chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

#chat-messages::-webkit-scrollbar { width: 6px; }
#chat-messages::-webkit-scrollbar-track { background: transparent; }
#chat-messages::-webkit-scrollbar-thumb { background: #333; border-radius: 3px; }

.msg {
  max-width: 65%;
  padding: 10px 16px;
  border-radius: 16px;
  font-size: 15px;
  line-height: 1.45;
  word-wrap: break-word;
  animation: fadeIn 0.2s ease;
}

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

.msg.mine {
  align-self: flex-end;
  background: var(--primary);
  border-bottom-right-radius: 4px;
}

.msg.theirs {
  align-self: flex-start;
  background: var(--surface2);
  border-bottom-left-radius: 4px;
}

.msg-text { word-break: break-word; }

.msg-image {
  max-width: 300px;
  max-height: 300px;
  border-radius: 10px;
  cursor: pointer;
  display: block;
}

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

.msg-voice audio {
  height: 36px;
  max-width: 250px;
}

.msg-voice .voice-duration {
  font-size: 11px;
  color: var(--text-dim);
}

.msg-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 4px;
}

.msg-time {
  font-size: 10px;
  color: var(--text-dim);
}

.msg-status {
  font-size: 10px;
  font-weight: 600;
}

.msg-status.sent { color: var(--sent); }
.msg-status.delivered { color: var(--delivered); }
.msg-status.seen { color: var(--seen); }

.date-separator {
  align-self: center;
  color: var(--text-dim);
  font-size: 11px;
  padding: 6px 16px;
  background: var(--surface);
  border-radius: 12px;
  margin: 8px 0;
}

/* ---- TYPING INDICATOR ---- */
#typing-indicator {
  padding: 6px 24px;
  font-size: 12px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 6px;
}

.typing-dots {
  display: flex;
  gap: 3px;
}

.typing-dots span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: typingBounce 1.4s infinite;
}

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

@keyframes typingBounce {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-4px); }
}

/* ---- COMPOSER ---- */
#composer {
  padding: 12px 24px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}

.composer-btn {
  width: 38px;
  height: 38px;
  border: 1px solid #333;
  border-radius: 50%;
  background: transparent;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.composer-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.composer-btn.accent {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

.composer-btn.recording {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
  animation: pulse 1.5s infinite;
}

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

#msg-form {
  flex: 1;
  display: flex;
  gap: 10px;
}

#msg-input {
  flex: 1;
  padding: 10px 18px;
  border: 1px solid #333;
  border-radius: 24px;
  background: #111;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}

#msg-input:focus { border-color: var(--accent); }

#btn-send {
  padding: 10px 24px;
  border: none;
  border-radius: 24px;
  background: var(--accent);
  color: white;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

#btn-send:hover { opacity: 0.85; }

/* ---- VOICE RECORDING BAR ---- */
#voice-recording {
  padding: 12px 24px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 16px;
}

.recording-indicator {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.rec-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1s infinite;
}

#rec-timer {
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}

/* ---- MODALS ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 90;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  animation: fadeIn 0.2s ease;
}

.modal-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 36px 40px;
  width: 400px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.modal-wide { width: 540px; }

.modal-card h3 {
  font-size: 20px;
  margin-bottom: 24px;
  text-align: center;
}

.modal-card h4 {
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.modal-card label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  display: block;
  margin-bottom: 6px;
  margin-top: 14px;
}

.modal-card label:first-of-type { margin-top: 0; }

.modal-card input, .modal-card select {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid #333;
  border-radius: 10px;
  background: #111;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}

.modal-card input:focus, .modal-card select:focus { border-color: var(--accent); }

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 24px;
}

.btn-secondary {
  flex: 1;
  padding: 10px;
  border: 1px solid #444;
  border-radius: 10px;
  background: transparent;
  color: var(--text-dim);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-secondary:hover {
  border-color: var(--text);
  color: var(--text);
}

.btn-primary {
  flex: 1;
  padding: 10px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: white;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}

.btn-primary:hover { opacity: 0.85; }

/* ---- ADMIN ---- */
.admin-section {
  margin-bottom: 24px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

.admin-section:last-of-type { border-bottom: none; }

.form-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.form-row input, .form-row select {
  flex: 1;
  min-width: 0;
}

.form-row .btn-primary {
  flex: 0 0 auto;
  padding: 10px 16px;
  white-space: nowrap;
}

.form-separator {
  color: var(--text-dim);
  font-size: 16px;
  flex-shrink: 0;
}

#admin-user-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 200px;
  overflow-y: auto;
}

.admin-user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: var(--surface2);
  border-radius: 8px;
  font-size: 13px;
}

.admin-user-row .user-role {
  font-size: 10px;
  color: var(--text-dim);
  text-transform: uppercase;
}

.admin-user-row .user-status {
  font-size: 10px;
}

.admin-user-row .user-status.online { color: var(--online); }
.admin-user-row .user-status.offline { color: var(--offline); }

/* ---- IMAGE PREVIEW ---- */
.image-preview-container {
  position: relative;
  max-width: 90vw;
  max-height: 90vh;
}

.image-preview-container img {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: 8px;
}

.close-preview {
  position: absolute;
  top: -40px;
  right: 0;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  font-size: 24px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---- EMPTY STATE ---- */
.empty-state {
  padding: 24px;
  text-align: center;
  color: var(--text-dim);
  font-size: 13px;
}

/* ---- BACK BUTTON (hidden on desktop) ---- */
.back-btn {
  display: none;
  width: 36px;
  height: 36px;
  border: 1px solid #333;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: 20px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-right: 12px;
  transition: all 0.2s;
}

.back-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ==================================================
   MOBILE RESPONSIVE
   ================================================== */

@media (max-width: 768px) {
  :root {
    --sidebar-width: 100%;
  }

  /* App layout: stack instead of side-by-side */
  #app-screen {
    flex-direction: column;
    position: relative;
  }

  /* Sidebar takes full screen on mobile */
  #sidebar {
    width: 100%;
    min-width: 100%;
    height: 100vh;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 10;
    transition: transform 0.25s ease;
  }

  /* When chat is open, hide sidebar */
  #app-screen.chat-open #sidebar {
    transform: translateX(-100%);
    pointer-events: none;
  }

  /* Main area takes full screen */
  #main-area {
    width: 100%;
    height: 100vh;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 5;
  }

  /* Show back button on mobile */
  .back-btn {
    display: flex;
  }

  /* Login card */
  .login-card {
    width: 90%;
    max-width: 360px;
    padding: 36px 28px;
  }

  /* Messages take more width on mobile */
  .msg {
    max-width: 85%;
  }

  /* Composer padding */
  #composer {
    padding: 10px 12px;
    gap: 6px;
  }

  #msg-input {
    padding: 10px 14px;
    font-size: 16px; /* prevents iOS zoom on focus */
  }

  #btn-send {
    padding: 10px 16px;
    font-size: 14px;
  }

  /* Chat messages padding */
  #chat-messages {
    padding: 12px 12px;
  }

  /* Chat header */
  #chat-header {
    padding: 12px 16px;
  }

  /* Voice recording */
  #voice-recording {
    padding: 10px 12px;
    gap: 10px;
  }

  /* Typing indicator */
  #typing-indicator {
    padding: 6px 12px;
  }

  /* Sidebar header */
  .sidebar-header {
    padding: 14px 12px;
  }

  .sidebar-search {
    padding: 10px 12px 6px;
  }

  .sidebar-tabs {
    padding: 0 12px;
  }

  .sidebar-list {
    padding: 4px 6px;
  }

  /* Conversation items - bigger tap targets */
  .conv-item, .contact-item {
    padding: 14px 12px;
  }

  /* Search results */
  #search-results {
    left: 12px;
    right: 12px;
  }

  /* Modals */
  .modal-card {
    width: 92%;
    max-width: 400px;
    padding: 28px 24px;
  }

  .modal-wide {
    width: 94%;
    max-width: 540px;
  }

  /* Admin form rows stack on small screens */
  .form-row {
    flex-wrap: wrap;
  }

  .form-row input, .form-row select {
    flex: 1 1 100%;
    min-width: 0;
  }

  .form-row .btn-primary {
    flex: 1 1 100%;
  }

  .form-separator {
    display: none;
  }

  /* Image in messages */
  .msg-image {
    max-width: 220px;
    max-height: 220px;
  }

  /* Voice audio player */
  .msg-voice audio {
    max-width: 180px;
  }

  /* Image preview */
  .image-preview-container img {
    max-width: 95vw;
    max-height: 85vh;
  }

  .close-preview {
    top: -36px;
  }

  /* Center message / no-chat placeholder */
  .center-message {
    padding: 24px;
    text-align: center;
    font-size: 14px;
  }
}

/* Extra small screens (under 380px) */
@media (max-width: 380px) {
  .sidebar-user-info {
    font-size: 13px;
  }

  .icon-btn {
    width: 30px;
    height: 30px;
    font-size: 14px;
  }

  .conv-name, .contact-name {
    font-size: 13px;
  }

  .avatar {
    width: 38px;
    height: 38px;
    font-size: 14px;
  }

  .composer-btn {
    width: 34px;
    height: 34px;
    font-size: 14px;
  }

  #btn-send {
    padding: 8px 12px;
    font-size: 13px;
  }
}

/* Safe area insets for notched phones */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  #composer {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  #voice-recording {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  .sidebar-list {
    padding-bottom: env(safe-area-inset-bottom);
  }
}
