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

:root {
  /* Colors */
  --bg-primary: #0a0a1a;
  --bg-secondary: #12122a;
  --bg-tertiary: #1a1a3e;
  --bg-input: #1e1e40;
  --text-primary: #e8e8f0;
  --text-secondary: #9898b8;
  --text-muted: #6868a0;
  --accent: #7c5cfc;
  --accent-hover: #9b80ff;
  --accent-dim: #4a38a0;
  --danger: #f44;
  --success: #4f8;
  --bubble-mine: #1a1a5e;
  --bubble-theirs: #1e1e3a;
  --border: #2a2a4a;

  /* Layout */
  --sidebar-width: 360px;
  --header-height: 52px;
  --composer-height: 56px;

  /* Typography */
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  --font-size-xs: 11px;
  --font-size-sm: 13px;
  --font-size-md: 15px;
  --font-size-lg: 17px;

  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 12px;
  --space-lg: 16px;
  --space-xl: 20px;

  /* Borders */
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 22px;

  /* Chat bubbles */
  --bubble-max-width: 72%;
  --avatar-size: 40px;
  --msg-font-size: var(--font-size-md);

  /* Shadows */
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);

  /* Transitions */
  --transition-fast: 0.15s ease;
  --transition-normal: 0.25s ease;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  font-size: var(--font-size-md);
  color: var(--text-primary);
  background: var(--bg-primary);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#app {
  height: 100%;
  overflow: hidden;
}

/* ── Scrollbar ────────────────────────────────────── */
::-webkit-scrollbar {
  width: 4px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--bg-tertiary);
  border-radius: 2px;
}

/* ── Screens ──────────────────────────────────────── */
.screen {
  display: none;
  height: 100%;
  overflow: hidden;
  flex-direction: column;
}
.screen.active {
  display: flex;
}

/* ── Auth Screen ──────────────────────────────────── */
.auth-screen {
  align-items: center;
  justify-content: center;
  padding: var(--space-xl);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  padding: var(--space-xl);
  border: 1px solid var(--border);
}
.auth-card h1 {
  font-size: 24px;
  text-align: center;
  margin-bottom: var(--space-xs);
  color: var(--accent);
}
.auth-card .subtitle {
  text-align: center;
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
  margin-bottom: var(--space-xl);
}
.auth-card label {
  display: block;
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  margin-bottom: var(--space-xs);
}
.auth-card input {
  width: 100%;
  padding: var(--space-md);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: var(--font-size-md);
  margin-bottom: var(--space-lg);
}
.auth-card input:focus {
  outline: none;
  border-color: var(--accent);
}
.auth-card .key-display {
  font-family: var(--font-mono);
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  word-break: break-all;
  background: var(--bg-tertiary);
  padding: var(--space-sm);
  border-radius: var(--radius-sm);
  margin-bottom: var(--space-lg);
  max-height: 60px;
  overflow-y: auto;
}
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-sm);
  padding: var(--space-md) var(--space-xl);
  border: none;
  border-radius: var(--radius-sm);
  font-size: var(--font-size-md);
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition-fast);
  white-space: nowrap;
}
.btn-primary {
  background: var(--accent);
  color: #fff;
  width: 100%;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { background: var(--accent-dim); }
.btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  width: 100%;
}
.btn-secondary:hover { background: var(--bg-input); }
.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-small {
  padding: var(--space-xs) var(--space-md);
  font-size: var(--font-size-sm);
}

/* ── Main Layout ──────────────────────────────────── */
.main-layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  height: 100%;
  overflow: hidden;
}

/* ── Chat List Panel ──────────────────────────────── */
.chatlist-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  overflow: hidden;
}
.chatlist-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-md) var(--space-lg);
  height: var(--header-height);
  border-bottom: 1px solid var(--border);
  gap: var(--space-sm);
  flex-shrink: 0;
}
.chatlist-header h2 {
  font-size: var(--font-size-lg);
  color: var(--accent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-buttons {
  display: flex;
  gap: var(--space-xs);
  flex-shrink: 0;
}
.icon-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition-fast);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--bg-input); color: var(--text-primary); }
.icon-btn:active { background: var(--accent-dim); }

.chatlist-search {
  padding: var(--space-sm) var(--space-md);
  flex-shrink: 0;
}
.chatlist-search input {
  width: 100%;
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text-primary);
  font-size: var(--font-size-sm);
}
.chatlist-search input:focus {
  outline: none;
  border-color: var(--accent);
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
}
.conversation-item {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-md) var(--space-lg);
  cursor: pointer;
  transition: background var(--transition-fast);
  border-bottom: 1px solid rgba(255,255,255,0.03);
}
.conversation-item:hover { background: var(--bg-tertiary); }
.conversation-item.active { background: var(--bg-tertiary); border-left: 3px solid var(--accent); }
.conv-avatar {
  width: var(--avatar-size);
  height: var(--avatar-size);
  border-radius: 50%;
  background: var(--accent-dim);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: var(--font-size-sm);
  flex-shrink: 0;
}
.conv-info {
  flex: 1;
  min-width: 0;
}
.conv-name {
  font-weight: 600;
  font-size: var(--font-size-md);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-preview {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-time {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
  flex-shrink: 0;
}
.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  font-size: var(--font-size-sm);
  text-align: center;
  padding: var(--space-xl);
}

/* ── Chat Panel ───────────────────────────────────── */
.chat-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--bg-primary);
  overflow: hidden;
}
.chat-header {
  display: flex;
  align-items: center;
  padding: var(--space-md) var(--space-lg);
  height: var(--header-height);
  border-bottom: 1px solid var(--border);
  gap: var(--space-md);
  flex-shrink: 0;
}
.chat-header .back-btn {
  display: none;
}
.chat-header .peer-info {
  flex: 1;
  min-width: 0;
}
.chat-header .peer-name {
  font-weight: 600;
}
.chat-header .peer-status {
  font-size: var(--font-size-xs);
  color: var(--text-muted);
}

/* ── Message List ─────────────────────────────────── */
.message-list-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  /* Scroll to bottom behavior */
  scroll-behavior: smooth;
}
.loading-indicator {
  text-align: center;
  padding: var(--space-md);
  color: var(--text-muted);
  font-size: var(--font-size-xs);
}
.date-divider {
  text-align: center;
  padding: var(--space-sm) 0;
  font-size: var(--font-size-xs);
  color: var(--text-muted);
}
.date-divider span {
  background: var(--bg-secondary);
  padding: 2px 10px;
  border-radius: var(--radius-lg);
}

.message-row {
  display: flex;
  padding: 2px var(--space-lg);
  gap: var(--space-sm);
  align-items: flex-end;
}
.message-row.mine {
  justify-content: flex-end;
}
.message-row.theirs {
  justify-content: flex-start;
}
.message-row .msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent-dim);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 700;
  flex-shrink: 0;
  order: 0;
}
.message-row.mine .msg-avatar {
  order: 2;
}

.message-bubble {
  max-width: var(--bubble-max-width);
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-lg);
  font-size: var(--msg-font-size);
  line-height: 1.45;
  word-break: break-word;
  position: relative;
  content-visibility: auto;
  contain-intrinsic-size: auto 60px;
}
.message-row.mine .message-bubble {
  background: var(--bubble-mine);
  border-bottom-right-radius: var(--radius-sm);
}
.message-row.theirs .message-bubble {
  background: var(--bubble-theirs);
  border-bottom-left-radius: var(--radius-sm);
}
.bubble-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
  text-align: right;
}
.message-row.mine .bubble-time {
  color: var(--accent-hover);
}

/* Attachment in bubble */
.bubble-attachment {
  margin-top: var(--space-xs);
  border-radius: var(--radius-sm);
  overflow: hidden;
  max-width: 240px;
  cursor: pointer;
}
.bubble-attachment img {
  width: 100%;
  height: auto;
  display: block;
  border-radius: var(--radius-sm);
}
.bubble-attachment video {
  width: 100%;
  max-height: 200px;
  border-radius: var(--radius-sm);
}
.bubble-attachment .file-download {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: var(--space-sm);
  background: var(--bg-tertiary);
  border-radius: var(--radius-sm);
  font-size: var(--font-size-xs);
  color: var(--accent);
  text-decoration: none;
}

/* Decryption error */
.decrypt-error {
  color: var(--danger);
  font-style: italic;
  font-size: var(--font-size-sm);
}

/* ── Message Composer ─────────────────────────────── */
.message-composer {
  display: flex;
  align-items: flex-end;
  gap: var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
  flex-shrink: 0;
  min-height: var(--composer-height);
}
.message-composer textarea {
  flex: 1;
  resize: none;
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  color: var(--text-primary);
  font-size: var(--font-size-md);
  font-family: var(--font);
  line-height: 1.4;
  max-height: 100px;
  min-height: 36px;
  outline: none;
}
.message-composer textarea:focus {
  border-color: var(--accent);
}
.composer-actions {
  display: flex;
  gap: var(--space-xs);
  align-items: center;
}
.send-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:active { background: var(--accent-dim); }
.send-btn:disabled {
  background: var(--bg-tertiary);
  color: var(--text-muted);
  cursor: default;
}
.typing-indicator {
  font-size: var(--font-size-xs);
  color: var(--accent);
  padding: 0 var(--space-lg) var(--space-xs);
  font-style: italic;
}

/* ── Search Screen ────────────────────────────────── */
.search-screen {
  background: var(--bg-primary);
}
.search-header {
  display: flex;
  align-items: center;
  padding: var(--space-md);
  gap: var(--space-sm);
  border-bottom: 1px solid var(--border);
}
.search-header input {
  flex: 1;
  padding: var(--space-sm) var(--space-md);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  color: var(--text-primary);
  font-size: var(--font-size-md);
}
.search-header input:focus {
  outline: none;
  border-color: var(--accent);
}
.search-results {
  flex: 1;
  overflow-y: auto;
}
.search-result-item {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  padding: var(--space-md) var(--space-lg);
  cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,0.03);
}
.search-result-item:hover { background: var(--bg-secondary); }

/* ── Settings Screen ──────────────────────────────── */
.settings-screen {
  background: var(--bg-primary);
  overflow-y: auto;
  padding: var(--space-lg);
}
.settings-screen h2 {
  margin-bottom: var(--space-lg);
  color: var(--accent);
}
.settings-section {
  background: var(--bg-secondary);
  border-radius: var(--radius-md);
  padding: var(--space-lg);
  margin-bottom: var(--space-lg);
  border: 1px solid var(--border);
}
.settings-section h3 {
  font-size: var(--font-size-md);
  margin-bottom: var(--space-md);
  color: var(--text-secondary);
}
.settings-section p {
  font-size: var(--font-size-sm);
  color: var(--text-muted);
  margin-bottom: var(--space-sm);
}
.settings-section .key-code {
  font-family: var(--font-mono);
  font-size: var(--font-size-xs);
  background: var(--bg-tertiary);
  padding: var(--space-sm);
  border-radius: var(--radius-sm);
  word-break: break-all;
  margin-bottom: var(--space-sm);
  max-height: 80px;
  overflow-y: auto;
}
.settings-section button {
  margin-top: var(--space-sm);
}

/* ── Notifications ────────────────────────────────── */
.notification {
  position: fixed;
  top: var(--space-lg);
  right: var(--space-lg);
  background: var(--bg-tertiary);
  border: 1px solid var(--accent);
  border-radius: var(--radius-md);
  padding: var(--space-md) var(--space-lg);
  color: var(--text-primary);
  font-size: var(--font-size-sm);
  z-index: 1000;
  max-width: 320px;
  box-shadow: var(--shadow-md);
  animation: slideIn 0.3s ease;
}
@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
.notification.warning { border-color: var(--danger); }

/* ── Utility ──────────────────────────────────────── */
.hidden { display: none !important; }
.text-muted { color: var(--text-muted); }
.text-accent { color: var(--accent); }
