/* ── Theme variables ───────────────────────────────────────────────────────── */

:root {
  --bg: #1a1a2e;
  --bg-surface: #16213e;
  --bg-card: #1c2a4a;
  --text: #e0e0e0;
  --text-muted: #8892a4;
  --accent: #2196f3;
  --accent-hover: #42a5f5;
  --border: #2a3a5c;
  --positive: #4caf50;
  --negative: #ef5350;
  --sent-bubble: #2196f3;
  --sent-text: #fff;
  --received-bubble: #2a3a5c;
  --received-text: #e0e0e0;
}

:root[data-theme="light"] {
  --bg: #f5f5f5;
  --bg-surface: #ffffff;
  --bg-card: #ffffff;
  --text: #212121;
  --text-muted: #757575;
  --accent: #1976d2;
  --accent-hover: #1565c0;
  --border: #e0e0e0;
  --positive: #2e7d32;
  --negative: #c62828;
  --sent-bubble: #1976d2;
  --sent-text: #fff;
  --received-bubble: #e0e0e0;
  --received-text: #212121;
}

/* ── Reset ────────────────────────────────────────────────────────────────── */

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  font-size: 14px;
}

#app { min-height: 100vh; display: flex; flex-direction: column; }

/* ── App layout ───────────────────────────────────────────────────────────── */

.app { display: flex; flex-direction: column; height: 100vh; }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  height: 48px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.app-title { font-size: 16px; font-weight: 600; }

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

.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Sidebar (thread list) ────────────────────────────────────────────────── */

.sidebar {
  width: 320px;
  min-width: 280px;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.thread-list { display: flex; flex-direction: column; height: 100%; }

.thread-toolbar {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.toolbar-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}

.toolbar-btn:hover { background: var(--accent); color: var(--sent-text); }

.thread-items { overflow-y: auto; flex: 1; }

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

.thread-item:hover { background: var(--bg-card); }
.thread-active { background: var(--bg-card); }
.thread-pinned { border-left: 3px solid var(--accent); }

.thread-color {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

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

.thread-name {
  font-weight: 500;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-phone {
  font-size: 11px;
  color: var(--text-muted);
  margin-left: 6px;
  font-weight: 400;
}

.thread-header-phone {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}

.thread-preview {
  color: var(--text-muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.unread-badge {
  background: var(--accent);
  color: var(--sent-text);
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 10px;
  flex-shrink: 0;
}

/* ── Main content ─────────────────────────────────────────────────────────── */

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  color: var(--text-muted);
  font-size: 16px;
}

/* ── Thread view ──────────────────────────────────────────────────────────── */

.thread-view {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.thread-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.thread-header h2 { font-size: 16px; font-weight: 600; }

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ── Message bubbles ──────────────────────────────────────────────────────── */

.message-bubble {
  max-width: 70%;
  padding: 10px 14px;
  border-radius: 16px;
  word-wrap: break-word;
}

.bubble-sent {
  align-self: flex-end;
  background: var(--sent-bubble);
  color: var(--sent-text);
  border-bottom-right-radius: 4px;
}

.bubble-received {
  align-self: flex-start;
  background: var(--received-bubble);
  color: var(--received-text);
  border-bottom-left-radius: 4px;
}

.bubble-text { font-size: 14px; line-height: 1.4; }

.bubble-time {
  display: block;
  font-size: 11px;
  opacity: 0.7;
  margin-top: 4px;
  text-align: right;
}

/* ── Compose bar ──────────────────────────────────────────────────────────── */

/* ── Attachments in bubbles ───────────────────────────────────────────────── */

.bubble-image {
  max-width: 100%;
  max-height: 300px;
  border-radius: 8px;
  display: block;
  margin-bottom: 4px;
}

.bubble-attachment {
  color: inherit;
  text-decoration: underline;
  font-size: 13px;
  display: block;
  margin-bottom: 4px;
}

/* ── Pending attachments + attach button ─────────────────────────────────── */

.compose-area {
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.pending-attachments {
  display: flex;
  gap: 8px;
  padding: 8px 16px;
  overflow-x: auto;
}

.pending-att {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
}

.pending-att-thumb {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 4px;
}

.attach-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.attach-btn:hover { background: var(--accent); color: var(--sent-text); }

.compose-bar {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  align-items: center;
}

.compose-input {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 20px;
  font-size: 14px;
  outline: none;
}

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

.compose-send {
  background: var(--accent);
  color: var(--sent-text);
  border: none;
  padding: 10px 20px;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
}

.compose-send:hover { background: var(--accent-hover); }

/* ── Connection badge ─────────────────────────────────────────────────────── */

.connection-badge {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 10px;
}

.badge-connected { background: var(--positive); color: #fff; }
.badge-reconnecting { background: #ff9800; color: #fff; }
.badge-disconnected { background: var(--negative); color: #fff; }

/* ── Theme toggle ─────────────────────────────────────────────────────────── */

.theme-toggle {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}

.theme-toggle:hover { background: var(--accent); color: var(--sent-text); }

/* ── Login ────────────────────────────────────────────────────────────────── */

.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
}

.login-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px;
  width: 360px;
  text-align: center;
}

.login-card h1 { font-size: 24px; margin-bottom: 8px; }

.login-subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 24px;
}

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

.login-input {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  text-align: center;
}

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

.login-button {
  background: var(--accent);
  color: var(--sent-text);
  border: none;
  padding: 12px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
}

.login-button:hover { background: var(--accent-hover); }

.login-waiting { color: var(--text-muted); }

.text-muted { color: var(--text-muted); }

/* ── Compose page ─────────────────────────────────────────────────────────── */

.compose-page {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.compose-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.compose-header h2 { font-size: 16px; font-weight: 600; }

.back-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}

.back-btn:hover { background: var(--accent); color: var(--sent-text); }

.recipients-bar {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  position: relative;
}

.recipient-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.recipient-chip {
  background: var(--accent);
  color: var(--sent-text);
  padding: 4px 10px;
  border-radius: 16px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.chip-remove {
  background: none;
  border: none;
  color: var(--sent-text);
  cursor: pointer;
  font-size: 12px;
  opacity: 0.7;
}

.chip-remove:hover { opacity: 1; }

.recipient-input {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
}

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

.suggestions-dropdown {
  position: absolute;
  left: 16px;
  right: 16px;
  top: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  max-height: 200px;
  overflow-y: auto;
  z-index: 10;
}

.suggestion-item {
  padding: 10px 14px;
  cursor: pointer;
  font-size: 14px;
  border-bottom: 1px solid var(--border);
}

.suggestion-item:hover { background: var(--bg-surface); }
.suggestion-item:last-child { border-bottom: none; }

.compose-body-area {
  flex: 1;
  padding: 16px;
}

.compose-textarea {
  width: 100%;
  height: 100%;
  min-height: 120px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 12px;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  resize: none;
  outline: none;
}

.compose-textarea:focus { border-color: var(--accent); }

.compose-actions {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
}

/* ── Search page ──────────────────────────────────────────────────────────── */

.search-page {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.search-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.search-input {
  flex: 1;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 14px;
  outline: none;
}

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

.search-results {
  flex: 1;
  overflow-y: auto;
}

.search-result-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
}

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

.result-thread { font-size: 12px; color: var(--text-muted); margin-bottom: 4px; }
.result-snippet { font-size: 14px; line-height: 1.4; }
.result-snippet mark { background: var(--accent); color: var(--sent-text); border-radius: 2px; padding: 0 2px; }
.result-time { font-size: 12px; margin-top: 4px; }

/* ── Blocklist page ───────────────────────────────────────────────────────── */

.blocklist-page {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.blocklist-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.blocklist-header h2 { font-size: 16px; font-weight: 600; }

.blocklist-items {
  flex: 1;
  overflow-y: auto;
}

.blocklist-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.blocklist-info { flex: 1; min-width: 0; }
.blocklist-status { font-size: 12px; margin-top: 2px; }

.blocklist-actions {
  display: flex;
  gap: 8px;
}

.blocklist-btn {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 4px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}

.blocklist-btn:hover { background: var(--accent); color: var(--sent-text); }

/* ── Settings button ──────────────────────────────────────────────────────── */

.settings-btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.settings-btn:hover { background: var(--bg-card); }

/* ── Settings page ───────────────────────────────────────────────────────── */

.settings-page {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.settings-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.settings-header h2 { font-size: 16px; font-weight: 600; }

.settings-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

.settings-section {
  margin-bottom: 24px;
}

.settings-section h3 {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.settings-input {
  width: 100%;
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  margin-bottom: 8px;
}

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

.settings-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 4px;
}

/* ── Delete buttons ───────────────────────────────────────────────────────── */

.delete-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.thread-item:hover .delete-btn,
.message-bubble:hover .delete-btn { opacity: 0.7; }
.delete-btn:hover { opacity: 1; }
.delete-btn.armed { opacity: 1; color: var(--negative); }
.bubble-sent .delete-btn { color: rgba(255,255,255,0.9); }
.bubble-sent .delete-btn.armed { color: var(--negative); }

.bubble-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
}

/* ── Undo toast ──────────────────────────────────────────────────────────── */

.undo-toast {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
}

.undo-btn {
  background: var(--accent);
  color: var(--sent-text);
  border: none;
  padding: 6px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
}

.undo-btn:hover { background: var(--accent-hover); }

.mobile-only { display: none; }

/* ── Responsive ───────────────────────────────────────────────────────────── */

@media (max-width: 640px) {
  .sidebar { width: 100%; min-width: 0; }
  .main-content { width: 100%; }

  /* On mobile, show sidebar OR main panel, not both */
  .app-body.has-panel > .sidebar { display: none; }
  .app-body:not(.has-panel) > .main-content { display: none; }

  .mobile-only { display: inline-block; }
  .thread-header { display: flex; align-items: center; gap: 8px; }
}
