/* ── Design tokens ───────────────────────────────────────────────────── */
:root {
  --primary: #2563EB;
  --primary-hover: #1D4ED8;
  --primary-light: #EFF6FF;
  --primary-text: #ffffff;

  --bg: #F1F5F9;
  --surface: #ffffff;
  --surface-2: #F8FAFC;
  --border: #E2E8F0;
  --border-focus: #93C5FD;

  --text: #0F172A;
  --text-secondary: #64748B;
  --text-tertiary: #94A3B8;

  --bubble-in-bg: #F1F5F9;
  --bubble-in-text: #0F172A;
  --bubble-out-bg: #2563EB;
  --bubble-out-text: #ffffff;

  --success: #10B981;
  --warning: #F59E0B;
  --danger: #EF4444;
  --danger-light: #FEF2F2;

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-xl: 20px;

  --shadow-sm: 0 1px 2px rgba(0,0,0,.06);
  --shadow-md: 0 4px 12px rgba(0,0,0,.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,.12);

  --sidebar-width: 320px;
  --header-height: 64px;
  --compose-height: auto;

  --font: 'Inter', system-ui, -apple-system, sans-serif;
}

/* ── Reset ───────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body {
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button { cursor: pointer; border: none; background: none; font: inherit; }
input, textarea {
  font: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  outline: none;
  background: var(--surface);
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(37,99,235,.15);
}
a { color: var(--primary); text-decoration: none; }

/* ── Utilities ───────────────────────────────────────────────────────── */
.hidden { display: none !important; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Buttons ─────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 14px;
  transition: background .15s, transform .1s, box-shadow .15s;
  white-space: nowrap;
}
.btn:active { transform: scale(0.98); }
.btn-primary {
  background: var(--primary);
  color: var(--primary-text);
}
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--surface-2); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-full { width: 100%; justify-content: center; }
.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  transition: background .15s, color .15s;
  flex-shrink: 0;
}
.btn-icon:hover { background: var(--surface-2); color: var(--text); }

/* ── Login screen ────────────────────────────────────────────────────── */
.login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 100;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: var(--shadow-lg);
}
.login-logo {
  display: flex;
  justify-content: center;
  margin-bottom: 16px;
}
.login-card h1 {
  text-align: center;
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 6px;
}
.login-subtitle {
  text-align: center;
  color: var(--text-secondary);
  margin-bottom: 28px;
}
.login-error {
  background: var(--danger-light);
  color: var(--danger);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 12px;
}

/* ── Form fields ─────────────────────────────────────────────────────── */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field label { font-weight: 500; font-size: 13px; color: var(--text); }
.field input, .field textarea {
  padding: 10px 12px;
  width: 100%;
}
.field textarea { resize: vertical; min-height: 72px; }
.field-hint { font-size: 12px; color: var(--text-tertiary); }

/* ── App shell ───────────────────────────────────────────────────────── */
.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── Sidebar ─────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: hidden;
}
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid var(--border);
  height: var(--header-height);
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  font-weight: 600;
}
.sidebar-search {
  position: relative;
  padding: 12px;
  border-bottom: 1px solid var(--border);
}
.sidebar-search input {
  width: 100%;
  padding: 8px 12px 8px 36px;
  border-radius: var(--radius-xl);
  background: var(--surface-2);
  font-size: 13px;
}
.search-icon {
  position: absolute;
  left: 24px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-tertiary);
  pointer-events: none;
}
.conversation-list {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.empty-state-sidebar {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-tertiary);
  font-size: 13px;
}

/* ── Conversation items ──────────────────────────────────────────────── */
.conv-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: background .12s;
  border-bottom: 1px solid var(--border);
  position: relative;
  user-select: none;
}
.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--primary-light); }
.conv-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  flex-shrink: 0;
}
.conv-body { flex: 1; min-width: 0; }
.conv-name {
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 2px;
}
.conv-preview {
  color: var(--text-secondary);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
  flex-shrink: 0;
}
.conv-time {
  font-size: 11px;
  color: var(--text-tertiary);
}
.conv-badge {
  background: var(--primary);
  color: white;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
  min-width: 18px;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
}

/* ── Main panel ──────────────────────────────────────────────────────── */
.main-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  background: var(--bg);
}

/* ── Welcome state ───────────────────────────────────────────────────── */
.welcome-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 40px;
  text-align: center;
}
.welcome-state h2 { font-size: 22px; font-weight: 600; }
.welcome-state p { color: var(--text-secondary); margin-bottom: 8px; }

/* ── Conversation view ───────────────────────────────────────────────── */
.conversation-view {
  display: flex;
  flex-direction: column;
  height: 100%;
}
.conversation-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  height: var(--header-height);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.back-btn { display: none; }
.conversation-header-info { flex: 1; min-width: 0; }
.conversation-header-name {
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conversation-header-number {
  font-size: 12px;
  color: var(--text-secondary);
}

/* ── Messages ────────────────────────────────────────────────────────── */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.messages-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: auto;
}
.load-more-sentinel {
  display: flex;
  justify-content: center;
  padding: 8px 0 16px;
}
.message-group {
  display: flex;
  flex-direction: column;
  margin-bottom: 12px;
}
.message-group.outbound { align-items: flex-end; }
.message-group.inbound  { align-items: flex-start; }
.message-bubble {
  max-width: 68%;
  padding: 10px 14px;
  border-radius: var(--radius-xl);
  font-size: 14px;
  line-height: 1.5;
  word-break: break-word;
  position: relative;
}
.message-group.outbound .message-bubble {
  background: var(--bubble-out-bg);
  color: var(--bubble-out-text);
  border-bottom-right-radius: 4px;
}
.message-group.inbound .message-bubble {
  background: var(--bubble-in-bg);
  color: var(--bubble-in-text);
  border-bottom-left-radius: 4px;
  border: 1px solid var(--border);
}
.message-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  font-size: 11px;
  color: var(--text-tertiary);
}
.message-group.outbound .message-meta { flex-direction: row-reverse; }
.status-icon { display: inline-flex; align-items: center; }
.status-delivered .status-icon { color: var(--success); }
.status-failed .status-icon { color: var(--danger); }
.day-separator {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 16px 0 8px;
  color: var(--text-tertiary);
  font-size: 12px;
  font-weight: 500;
}
.day-separator::before, .day-separator::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ── Compose area ────────────────────────────────────────────────────── */
.compose-area {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 12px 16px;
  flex-shrink: 0;
}
.compose-input {
  width: 100%;
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  resize: none;
  font-size: 14px;
  max-height: 140px;
  overflow-y: auto;
  scrollbar-width: thin;
  line-height: 1.5;
}
.compose-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}
.char-count {
  font-size: 12px;
  color: var(--text-tertiary);
}
.char-count.warn { color: var(--warning); }
.char-count.over { color: var(--danger); }
.send-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  justify-content: center;
}

/* ── Modals ──────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
  backdrop-filter: blur(2px);
  animation: fadeIn .15s ease;
}
.modal-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 460px;
  padding: 28px;
  animation: slideUp .2s ease;
}
.modal-card-sm { max-width: 360px; }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}
.modal-header h2 { font-size: 18px; font-weight: 600; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

/* ── Toast notifications ─────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  background: var(--text);
  color: var(--surface);
  padding: 12px 18px;
  border-radius: var(--radius-md);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: slideInRight .2s ease;
  pointer-events: auto;
  max-width: 320px;
}
.toast.success { background: #064E3B; }
.toast.error   { background: #7F1D1D; }
.toast.warning { background: #78350F; }
.toast.fade-out { animation: fadeOut .3s ease forwards; }

/* ── Animations ──────────────────────────────────────────────────────── */
@keyframes fadeIn    { from { opacity: 0; }          to { opacity: 1; } }
@keyframes slideUp   { from { transform: translateY(12px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes slideInRight { from { transform: translateX(24px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
@keyframes fadeOut   { from { opacity: 1; }          to { opacity: 0; } }

/* ── Scrollbars ──────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Responsive (mobile) ─────────────────────────────────────────────── */
@media (max-width: 640px) {
  :root { --sidebar-width: 100vw; }
  .app { position: relative; }
  .sidebar {
    position: absolute;
    inset: 0;
    z-index: 10;
    transition: transform .25s ease;
  }
  .main-panel {
    position: absolute;
    inset: 0;
    z-index: 5;
  }
  .back-btn { display: inline-flex !important; }
  .app.show-thread .sidebar { transform: translateX(-100%); }
}

/* ── Passkey button ──────────────────────────────────────────────────── */
.passkey-btn {
  gap: 10px;
  padding: 12px 20px;
  font-size: 15px;
  border-radius: var(--radius-md);
  justify-content: center;
}
.setup-hint {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 20px;
  line-height: 1.6;
}
.passkey-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.passkey-item-name { font-weight: 500; font-size: 14px; }
.passkey-item-meta { font-size: 12px; color: var(--text-tertiary); margin-top: 2px; }

/* ── Loading shimmer ─────────────────────────────────────────────────── */
.shimmer {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--border) 50%, var(--surface-2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
  border-radius: var(--radius-sm);
}
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ── Connection status indicator ─────────────────────────────────────── */
.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-tertiary);
  display: inline-block;
  margin-right: 6px;
  transition: background .3s;
}
.conn-dot.connected { background: var(--success); }
.conn-dot.error     { background: var(--danger); }
