/* =====================================================================
   CERM Nexus Messenger — Modern Post-Quantum Web Client Design System
   ===================================================================== */

:root {
  --bg-main: #090d16;
  --bg-surface: #101726;
  --bg-surface-elevated: #162034;
  --bg-bubble-self: #0f4c81;
  --bg-bubble-self-gradient: linear-gradient(135deg, #0284c7 0%, #0369a1 100%);
  --bg-bubble-peer: #1c273e;
  --border-subtle: rgba(255, 255, 255, 0.08);
  --border-active: rgba(56, 189, 248, 0.4);

  --accent-cyan: #38bdf8;
  --accent-blue: #0284c7;
  --accent-emerald: #10b981;
  --accent-amber: #f59e0b;
  --accent-rose: #f43f5e;

  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-dim: #64748b;

  --font-sans: 'Outfit', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', monospace;

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-full: 9999px;

  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
  --shadow-glow: 0 0 20px rgba(56, 189, 248, 0.25);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-sans);
  background-color: var(--bg-main);
  color: var(--text-main);
  min-height: 100vh;
  display: flex;
  overflow: hidden;
}

/* --- Common Utilities --- */
.hidden {
  display: none !important;
}

/* --- Modals / Login --- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 50% 20%, rgba(14, 165, 233, 0.15) 0%, rgba(9, 13, 22, 0.95) 75%);
  backdrop-filter: blur(16px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.login-card {
  background: rgba(16, 23, 38, 0.85);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  padding: 40px;
  width: 100%;
  max-width: 440px;
  box-shadow: var(--shadow-md), 0 0 40px rgba(2, 132, 199, 0.15);
  display: flex;
  flex-direction: column;
  gap: 20px;
  animation: fadeInScale 0.35s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes fadeInScale {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

.brand-badge {
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: rgba(56, 189, 248, 0.1);
  border: 1px solid rgba(56, 189, 248, 0.25);
  color: var(--accent-cyan);
  padding: 4px 12px;
  border-radius: var(--radius-full);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.05em;
}

.pulse-icon {
  display: inline-block;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.15); opacity: 0.7; }
}

.login-title {
  font-size: 1.85rem;
  font-weight: 700;
  background: linear-gradient(135deg, #ffffff 40%, var(--accent-cyan) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.login-subtitle {
  color: var(--text-muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.input-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-group label {
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--text-muted);
}

.input-wrapper {
  display: flex;
  align-items: center;
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 0 14px;
  transition: all 0.2s ease;
}

.input-wrapper:focus-within {
  border-color: var(--accent-cyan);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.2);
}

.input-prefix {
  color: var(--accent-cyan);
  font-weight: 600;
  margin-right: 8px;
  user-select: none;
}

.input-wrapper input {
  background: transparent;
  border: none;
  color: var(--text-main);
  font-family: inherit;
  font-size: 0.95rem;
  padding: 12px 0;
  width: 100%;
  outline: none;
}

.btn-primary {
  background: var(--bg-bubble-self-gradient);
  color: #ffffff;
  border: none;
  border-radius: var(--radius-md);
  padding: 14px 20px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  transition: all 0.2s ease;
  box-shadow: var(--shadow-sm), 0 4px 14px rgba(2, 132, 199, 0.35);
  margin-top: 10px;
}

.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-glow);
}

.login-features {
  display: flex;
  justify-content: space-between;
  border-top: 1px solid var(--border-subtle);
  padding-top: 18px;
  font-size: 0.75rem;
  color: var(--text-dim);
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.check-dot {
  color: var(--accent-emerald);
  font-weight: 700;
}

/* --- Main Layout --- */
.app-layout {
  display: flex;
  width: 100vw;
  height: 100vh;
  position: relative;
  background-color: var(--bg-main);
}

/* --- Sidebar --- */
.sidebar {
  width: 340px;
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 10;
}

.profile-header {
  padding: 18px 20px;
  display: flex;
  align-items: center;
  gap: 14px;
  border-bottom: 1px solid var(--border-subtle);
  background: rgba(16, 23, 38, 0.6);
}

.avatar-ring {
  position: relative;
  width: 44px;
  height: 44px;
}

.avatar-inner {
  width: 100%;
  height: 100%;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, #0284c7, #38bdf8);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.1rem;
  text-transform: uppercase;
}

.my-status-indicator {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 12px;
  height: 12px;
  border-radius: var(--radius-full);
  background-color: var(--accent-emerald);
  border: 2px solid var(--bg-surface);
}

.profile-details {
  flex: 1;
  min-width: 0;
}

.profile-name {
  font-weight: 600;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.profile-id {
  font-size: 0.75rem;
  color: var(--text-dim);
  font-family: var(--font-mono);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

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

.icon-btn-small {
  background: transparent;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  transition: all 0.2s ease;
}

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

/* Connection Status Bar */
.conn-status-bar {
  padding: 8px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(2, 132, 199, 0.04);
  border-bottom: 1px solid var(--border-subtle);
  font-size: 0.75rem;
}

.status-indicator-pill {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-full);
}

.status-dot.green {
  background-color: var(--accent-emerald);
  box-shadow: 0 0 8px var(--accent-emerald);
}

.status-dot.yellow {
  background-color: var(--accent-amber);
  box-shadow: 0 0 8px var(--accent-amber);
}

.status-dot.red {
  background-color: var(--accent-rose);
  box-shadow: 0 0 8px var(--accent-rose);
}

.ping-latency {
  color: var(--text-dim);
  font-family: var(--font-mono);
}

/* Search */
.sidebar-search {
  padding: 14px 20px;
}

.search-input-wrapper {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.search-input-wrapper svg {
  color: var(--text-dim);
}

.search-input-wrapper input {
  background: transparent;
  border: none;
  color: var(--text-main);
  outline: none;
  width: 100%;
  font-size: 0.88rem;
  font-family: inherit;
}

/* Contacts List */
.contacts-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: 10px 0;
}

.section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px 8px 20px;
}

.section-title {
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}

.contacts-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  cursor: pointer;
  transition: all 0.15s ease;
  border-left: 3px solid transparent;
}

.contact-item:hover {
  background: rgba(255, 255, 255, 0.03);
}

.contact-item.active {
  background: rgba(56, 189, 248, 0.08);
  border-left-color: var(--accent-cyan);
}

.contact-avatar {
  position: relative;
  width: 40px;
  height: 40px;
  border-radius: var(--radius-full);
  background: var(--bg-surface-elevated);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  color: var(--text-main);
  border: 1px solid var(--border-subtle);
}

.contact-status-dot {
  position: absolute;
  bottom: -1px;
  right: -1px;
  width: 10px;
  height: 10px;
  border-radius: var(--radius-full);
  border: 2px solid var(--bg-surface);
}

.contact-status-dot.online {
  background-color: var(--accent-emerald);
}

.contact-status-dot.offline {
  background-color: var(--text-dim);
}

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

.contact-name-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2px;
}

.contact-name {
  font-size: 0.92rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-time {
  font-size: 0.7rem;
  color: var(--text-dim);
}

.contact-preview-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.contact-preview {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 180px;
}

.unread-badge {
  background-color: var(--accent-cyan);
  color: #090d16;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: var(--radius-full);
}

.loading-contacts {
  padding: 20px;
  color: var(--text-dim);
  font-size: 0.85rem;
  text-align: center;
}

/* Sidebar Footer */
.sidebar-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border-subtle);
  background: rgba(16, 23, 38, 0.4);
}

.crypto-badge {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(56, 189, 248, 0.05);
  border: 1px solid rgba(56, 189, 248, 0.15);
  border-radius: var(--radius-md);
  padding: 8px 12px;
}

.crypto-meta {
  display: flex;
  flex-direction: column;
}

.crypto-title {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--accent-cyan);
}

.crypto-desc {
  font-size: 0.68rem;
  color: var(--text-dim);
  font-family: var(--font-mono);
}

/* --- Main Chat Pane --- */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-main);
  position: relative;
  overflow: hidden;
}

/* Empty State */
.empty-chat-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px;
  gap: 16px;
  color: var(--text-muted);
}

.empty-icon-card {
  position: relative;
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.5rem;
}

.empty-chat-state h2 {
  color: var(--text-main);
  font-size: 1.4rem;
}

.empty-chat-state p {
  max-width: 420px;
  font-size: 0.9rem;
  line-height: 1.5;
}

.btn-secondary {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  color: var(--accent-cyan);
  padding: 10px 16px;
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 500;
  transition: all 0.2s ease;
}

.btn-secondary:hover {
  background: rgba(56, 189, 248, 0.12);
  border-color: var(--accent-cyan);
}

/* Active Chat View */
.active-chat-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.chat-header {
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 5;
}

.header-avatar {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-full);
  background: linear-gradient(135deg, #0284c7, #06b6d4);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.1rem;
}

.header-contact-info {
  flex: 1;
}

.header-contact-name {
  font-size: 1rem;
  font-weight: 600;
}

.header-contact-status {
  font-size: 0.75rem;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 6px;
}

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

.security-chip {
  background: rgba(16, 185, 129, 0.1);
  border: 1px solid rgba(16, 185, 129, 0.3);
  color: var(--accent-emerald);
  padding: 4px 10px;
  border-radius: var(--radius-full);
  font-size: 0.75rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 4px;
}

/* Messages Scroll Container */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.message-row {
  display: flex;
  flex-direction: column;
  max-width: 70%;
  animation: fadeIn 0.2s ease;
}

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

.message-row.outgoing {
  align-self: flex-end;
  align-items: flex-end;
}

.message-row.incoming {
  align-self: flex-start;
  align-items: flex-start;
}

.message-bubble {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  font-size: 0.95rem;
  line-height: 1.45;
  word-break: break-word;
  position: relative;
  box-shadow: var(--shadow-sm);
}

.message-row.outgoing .message-bubble {
  background: var(--bg-bubble-self-gradient);
  color: white;
  border-bottom-right-radius: 4px;
}

.message-row.incoming .message-bubble {
  background: var(--bg-bubble-peer);
  color: var(--text-main);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--border-subtle);
}

.message-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  font-size: 0.7rem;
  color: var(--text-dim);
}

.message-ack {
  font-weight: 600;
  letter-spacing: -1px;
}

.message-ack.delivered {
  color: var(--accent-cyan);
}

.message-ack.read {
  color: var(--accent-emerald);
}

.raw-envelope-badge {
  cursor: pointer;
  opacity: 0.6;
  transition: opacity 0.2s ease;
  font-family: var(--font-mono);
  font-size: 0.65rem;
  padding: 1px 4px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.05);
}

.raw-envelope-badge:hover {
  opacity: 1;
  background: rgba(56, 189, 248, 0.2);
  color: var(--accent-cyan);
}

/* Chat Input Footer */
.chat-input-area {
  padding: 16px 24px;
  background: var(--bg-surface);
  border-top: 1px solid var(--border-subtle);
}

.chat-form {
  display: flex;
  align-items: flex-end;
  gap: 12px;
}

.input-box-wrapper {
  flex: 1;
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 10px 16px;
  transition: all 0.2s ease;
}

.input-box-wrapper:focus-within {
  border-color: var(--accent-cyan);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.15);
}

.input-box-wrapper textarea {
  width: 100%;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-main);
  font-family: inherit;
  font-size: 0.95rem;
  resize: none;
  max-height: 120px;
  display: block;
}

.btn-send {
  background: var(--accent-blue);
  color: white;
  border: none;
  border-radius: var(--radius-md);
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s ease;
  flex-shrink: 0;
  box-shadow: 0 4px 12px rgba(2, 132, 199, 0.35);
}

.btn-send:hover {
  background: var(--accent-cyan);
  color: #090d16;
  transform: translateY(-1px);
}

/* --- WireEnvelope Inspector Drawer --- */
.envelope-inspector {
  width: 380px;
  background: var(--bg-surface);
  border-left: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 20;
  box-shadow: var(--shadow-md);
  animation: slideInRight 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes slideInRight {
  from { transform: translateX(100%); }
  to { transform: translateX(0); }
}

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

.inspector-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--accent-cyan);
}

.inspector-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.inspector-hint {
  font-size: 0.8rem;
  color: var(--text-dim);
  line-height: 1.4;
}

.inspector-content pre {
  background: var(--bg-main);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 14px;
  overflow-x: auto;
}

.inspector-content code {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: #a5f3fc;
  line-height: 1.5;
}

/* Toast Notifications */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 9999;
}

.toast {
  background: var(--bg-surface-elevated);
  border: 1px solid var(--border-subtle);
  border-left: 4px solid var(--accent-cyan);
  padding: 12px 18px;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  font-size: 0.85rem;
  color: var(--text-main);
  animation: fadeIn 0.3s ease;
}

/* Responsive / Mobile */
.mobile-only {
  display: none;
}

@media (max-width: 768px) {
  .mobile-only {
    display: flex;
  }

  .sidebar {
    width: 100vw;
    position: absolute;
    inset: 0;
  }

  .sidebar.mobile-hide {
    display: none;
  }

  .envelope-inspector {
    width: 100vw;
  }
}
