:root {
  --bg: #0B0E14;
  --surface: #141821;
  --surface-hover: #1C2230;
  --surface-active: #232B3D;
  --border: #232936;
  --text: #E6E8EC;
  --text-muted: #8A93A6;
  --text-dim: #5A6478;
  --accent: #3B82F6;
  --accent-hover: #2563EB;
  --success: #10B981;
  --danger: #EF4444;
  --bubble-in: #1C2230;
  --bubble-out: #2563EB;
  --unread: #3B82F6;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
}

[data-theme="light"] {
  --bg: #F7F8FA;
  --surface: #FFFFFF;
  --surface-hover: #F1F3F7;
  --surface-active: #E6EBF5;
  --border: #E5E8EF;
  --text: #0F172A;
  --text-muted: #64748B;
  --text-dim: #94A3B8;
  --bubble-in: #F1F3F7;
  --bubble-out: #3B82F6;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

a, button, input, textarea {
  transition: background-color 150ms ease-out, border-color 150ms ease-out;
}

/* ============ LOGIN ============ */
.login-bg {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at top, #1a2332 0%, #0B0E14 60%);
}
[data-theme="light"] .login-bg {
  background: radial-gradient(ellipse at top, #EAF0FB 0%, #F7F8FA 60%);
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 24px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  max-width: 480px;
  width: 100%;
  margin: 16px;
}
.progress-pills { display: flex; gap: 8px; margin-bottom: 24px; }
.progress-pill { flex: 1; height: 4px; border-radius: 2px; background: var(--border); }
.progress-pill.completed { background: var(--accent); }
.progress-pill.active { background: var(--accent); animation: pill-pulse 1.5s ease-in-out infinite; }
@keyframes pill-pulse { 0%,100%{opacity:1} 50%{opacity:0.5} }
.form-label { display: block; font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }
.form-input {
  width: 100%; height: 44px; padding: 0 12px;
  border-radius: var(--radius-md); border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 14px; outline: none;
}
.form-input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(59,130,246,0.4); }
.form-input::placeholder { color: var(--text-dim); }
.code-input {
  height: 56px; text-align: center; letter-spacing: 0.4em; font-size: 24px;
  font-family: "SF Mono","Fira Code","Cascadia Code",monospace;
}
.password-wrapper { position: relative; }
.password-toggle {
  position: absolute; right: 12px; top: 50%; transform: translateY(-50%);
  background: none; border: none; color: var(--text-dim); cursor: pointer; padding: 4px;
}
.btn-primary {
  width: 100%; height: 44px; border: none; border-radius: var(--radius-md);
  background: var(--accent); color: #fff; font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary .spinner {
  display: inline-block; width: 16px; height: 16px;
  border: 2px solid rgba(255,255,255,0.3); border-top-color: #fff;
  border-radius: 50%; animation: spin 0.6s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.form-group { margin-bottom: 16px; }
.heading { font-size: 20px; font-weight: 700; margin-bottom: 4px; }
.subtext { font-size: 13px; color: var(--text-muted); margin-bottom: 20px; line-height: 1.6; }
.link-secondary {
  font-size: 13px; color: var(--text-muted); background: none; border: none;
  cursor: pointer; text-decoration: underline; text-underline-offset: 2px;
}
.link-secondary:hover { color: var(--text); }
.link-back {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 13px; color: var(--text-dim); text-decoration: none; margin-top: 16px;
}
.link-back:hover { color: var(--text-muted); }
.error-msg { color: var(--danger); font-size: 13px; margin-top: 8px; }
.success-state { text-align: center; padding: 32px 0; }
.success-state .check-icon {
  width: 48px; height: 48px; background: var(--success); border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center; margin-bottom: 12px;
}
.note-text { font-size: 12px; font-style: italic; color: var(--text-dim); margin-top: 12px; }
.step-actions { display: flex; justify-content: space-between; margin-top: 12px; }
.hidden { display: none !important; }

/* ============ DASHBOARD LAYOUT ============ */
.dashboard {
  display: flex;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: relative;
}

/* ---- Sidebar ---- */
.sidebar {
  width: 240px;
  min-width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}
.sidebar-header {
  padding: 16px;
  font-size: 16px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
}
.sidebar-list { flex: 1; overflow-y: auto; }
.account-item {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; cursor: pointer;
}
.account-item:hover { background: var(--surface-hover); }
.account-item.active { background: var(--surface-active); }
.account-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.account-name {
  flex: 1; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.account-avatar-img {
  width: 24px; height: 24px; border-radius: 50%; object-fit: cover; flex-shrink: 0;
}
.unread-pill {
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 600;
  padding: 1px 7px; border-radius: 10px; min-width: 18px; text-align: center;
  flex-shrink: 0;
}
.sidebar-footer { padding: 12px 16px; border-top: 1px solid var(--border); }
.btn-add-account {
  width: 100%; height: 36px; border: none; border-radius: 18px;
  background: var(--accent); color: #fff; font-size: 13px; font-weight: 600;
  cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-add-account:hover { background: var(--accent-hover); }

.icon-btn {
  background: none; border: none; color: var(--text-dim); cursor: pointer;
  padding: 6px; border-radius: var(--radius-sm); display: flex; align-items: center;
}
.icon-btn:hover { color: var(--text); background: var(--surface-hover); }

/* ---- Conversation panel ---- */
.conv-panel {
  width: 340px;
  min-width: 340px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}
.conv-search { padding: 12px 12px 8px; }
.search-input-wrapper { position: relative; }
.search-input-wrapper i {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  color: var(--text-dim); width: 16px; height: 16px;
}
.search-input {
  width: 100%; height: 36px; padding: 0 12px 0 34px;
  border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 13px; outline: none;
}
.search-input:focus { border-color: var(--accent); }

.conv-filters {
  display: flex; gap: 6px; padding: 4px 12px 10px;
  border-bottom: 1px solid var(--border);
}
.filter-btn {
  padding: 4px 12px; border-radius: 14px; border: 1px solid var(--border);
  background: transparent; color: var(--text-muted); font-size: 12px; cursor: pointer;
  white-space: nowrap;
}
.filter-btn:hover { background: var(--surface-hover); color: var(--text); }
.filter-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.conv-list { flex: 1; overflow-y: auto; }
.conv-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  border-bottom: 1px solid var(--border); cursor: pointer;
}
.conv-item:hover { background: var(--surface-hover); }
.conv-item.active { background: var(--surface-active); border-left: 3px solid var(--accent); }
.conv-avatar {
  width: 40px; height: 40px; min-width: 40px; min-height: 40px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 600; color: #fff; flex-shrink: 0;
  position: relative; overflow: hidden;
}
.conv-avatar-img {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: cover; border-radius: 50%; z-index: 1;
}
.conv-avatar-letter { z-index: 0; }
.conv-avatar-ring {
  position: absolute; inset: -3px; border-radius: 50%;
  border: 2px solid transparent; z-index: 2; pointer-events: none;
}
.conv-info { flex: 1; min-width: 0; overflow: hidden; }
.conv-name {
  font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-snippet {
  font-size: 13px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-meta { text-align: right; flex-shrink: 0; }
.conv-time { font-size: 12px; color: var(--text-dim); }
.conv-unread { margin-top: 4px; }
.empty-conv { padding: 24px; text-align: center; color: var(--text-dim); font-size: 13px; }

/* ---- Thread panel ---- */
.thread-panel {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}
.thread-header {
  height: 60px; min-height: 60px; padding: 0 16px;
  display: flex; align-items: center; gap: 12px;
  border-bottom: 1px solid var(--border); background: var(--surface);
  flex-shrink: 0;
}
.thread-header-info { flex: 1; min-width: 0; overflow: hidden; }
.thread-header-name { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread-header-via { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.thread-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px;
  display: flex;
  flex-direction: column;
}

.empty-state {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; color: var(--text-muted); gap: 8px;
}
.empty-state i { width: 48px; height: 48px; color: var(--text-dim); }

/* ---- Messages ---- */
.msg-wrapper {
  display: flex;
  flex-direction: column;
  max-width: 100%;
}
.msg-wrapper.gap-small { margin-top: 2px; }
.msg-wrapper.gap-large { margin-top: 12px; }

.msg-bubble {
  max-width: 70%;
  padding: 10px 14px;
  font-size: 15px;
  line-height: 1.5;
  word-wrap: break-word;
  word-break: break-word;
  white-space: pre-wrap;
}
.msg-incoming {
  align-self: flex-start;
  background: var(--bubble-in);
  color: var(--text);
  border-radius: 16px 16px 16px 4px;
}
.msg-outgoing {
  align-self: flex-end;
  background: var(--bubble-out);
  color: #fff;
  border-radius: 16px 16px 4px 16px;
}
.msg-time { font-size: 11px; color: var(--text-dim); margin-top: 2px; }
.msg-time.outgoing { align-self: flex-end; }
.msg-time.incoming { align-self: flex-start; }

/* Media in messages */
.msg-media {
  display: block;
  max-width: 320px;
  max-height: 360px;
  width: auto;
  height: auto;
  border-radius: var(--radius-md);
  object-fit: contain;
  cursor: pointer;
}
.msg-media.incoming { align-self: flex-start; }
.msg-media.outgoing { align-self: flex-end; }

/* Reply quote inside message */
.msg-reply-quote {
  font-size: 12px;
  color: var(--text-muted);
  padding: 4px 10px;
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  background: var(--surface-hover);
  margin-bottom: 4px;
  max-width: 70%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-reply-quote.outgoing { align-self: flex-end; }
.msg-reply-quote.incoming { align-self: flex-start; }

/* Reply bar above composer */
.reply-bar {
  display: flex; align-items: center; padding: 8px 12px;
  background: var(--surface); border-top: 1px solid var(--border);
  border-left: 3px solid var(--accent); flex-shrink: 0;
}
.reply-bar-content { flex: 1; min-width: 0; }
.reply-bar-label { font-size: 11px; color: var(--accent); font-weight: 600; }
.reply-bar-text {
  font-size: 13px; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.reply-bar-close {
  background: none; border: none; color: var(--text-dim); cursor: pointer; padding: 4px;
}
.reply-bar-close:hover { color: var(--text); }

/* Composer */
.composer {
  padding: 12px; border-top: 1px solid var(--border);
  display: flex; align-items: flex-end; gap: 8px; background: var(--surface);
  flex-shrink: 0;
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: var(--radius-md);
  background: var(--bg); color: var(--text); padding: 10px 12px;
  font-size: 14px; font-family: inherit; line-height: 1.5; outline: none;
  min-height: 40px; max-height: 144px; overflow-y: auto;
}
.composer textarea:focus { border-color: var(--accent); }
.composer textarea::placeholder { color: var(--text-dim); }
.btn-send {
  width: 36px; height: 36px; min-width: 36px; border: none; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0;
}
.btn-send:hover { background: var(--accent-hover); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

/* ============ CONTEXT MENU ============ */
.context-menu {
  position: fixed;
  z-index: 1000;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  min-width: 170px;
  padding: 4px 0;
}
.context-item {
  display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 14px;
  background: none; border: none; color: var(--text); font-size: 13px;
  cursor: pointer; text-align: left;
}
.context-item:hover { background: var(--surface-hover); }
.context-item.danger { color: var(--danger); }
.context-item.danger:hover { background: rgba(239,68,68,0.1); }

/* ============ SETTINGS MODAL ============ */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0,0,0,0.6);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  width: 520px;
  max-width: 92vw;
  max-height: 80vh;
  overflow-y: auto;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 24px; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
.modal-header h2 { font-size: 18px; font-weight: 700; }
.modal-body { padding: 24px; }

.settings-section-title {
  font-size: 12px; font-weight: 600; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 12px;
  margin-top: 24px;
}
.settings-section-title:first-child { margin-top: 0; }

.settings-account-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 0; border-bottom: 1px solid var(--border);
}
.settings-account-info { display: flex; align-items: center; gap: 10px; }
.btn-disconnect {
  display: flex; align-items: center; gap: 4px;
  padding: 6px 12px; border-radius: var(--radius-sm); border: 1px solid var(--danger);
  background: transparent; color: var(--danger); font-size: 12px; cursor: pointer;
  white-space: nowrap;
}
.btn-disconnect:hover { background: rgba(239,68,68,0.1); }
.settings-hint { font-size: 12px; color: var(--text-dim); margin-top: 8px; }

.settings-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 0; border-bottom: 1px solid var(--border);
}
.settings-row-label { font-size: 14px; }
.settings-row-desc { font-size: 12px; color: var(--text-dim); margin-top: 2px; }

/* Toggle switch */
.toggle-switch { position: relative; display: inline-block; width: 40px; height: 22px; flex-shrink: 0; }
.toggle-switch input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; cursor: pointer; inset: 0;
  background: var(--border); border-radius: 22px; transition: 0.2s;
}
.toggle-slider:before {
  content: ''; position: absolute; height: 16px; width: 16px;
  left: 3px; bottom: 3px; background: #fff; border-radius: 50%; transition: 0.2s;
}
.toggle-switch input:checked + .toggle-slider { background: var(--accent); }
.toggle-switch input:checked + .toggle-slider:before { transform: translateX(18px); }

/* Select dropdown */
.settings-select {
  padding: 6px 10px; border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: 13px; outline: none;
  cursor: pointer;
}
.settings-select:focus { border-color: var(--accent); }

/* Danger button */
.btn-danger-outline {
  padding: 8px 16px; border-radius: var(--radius-sm); border: 1px solid var(--danger);
  background: transparent; color: var(--danger); font-size: 13px; cursor: pointer;
  display: flex; align-items: center; gap: 6px;
}
.btn-danger-outline:hover { background: rgba(239,68,68,0.1); }

.settings-footer {
  margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--border);
  text-align: center; color: var(--text-dim); font-size: 12px;
}

/* ---- Connection indicator ---- */
.conn-indicator { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-dim); }
.conn-dot { width: 8px; height: 8px; border-radius: 50%; }
.conn-dot.connected { background: var(--success); }
.conn-dot.reconnecting { background: #F59E0B; }
.conn-dot.offline { background: var(--danger); }

.theme-toggle {
  background: none; border: none; color: var(--text-dim); cursor: pointer;
  padding: 6px; border-radius: var(--radius-sm);
}
.theme-toggle:hover { color: var(--text); background: var(--surface-hover); }

.top-bar {
  position: fixed; top: 12px; right: 16px;
  display: flex; align-items: center; gap: 12px; z-index: 10;
}

/* ---- Toast ---- */
.toast-container {
  position: fixed; bottom: 16px; right: 16px;
  display: flex; flex-direction: column; gap: 8px; z-index: 600;
}
.toast {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius-sm); padding: 10px 16px; font-size: 13px;
  box-shadow: var(--shadow-md); animation: toast-in 200ms ease-out;
}
.toast.error { border-left: 3px solid var(--danger); color: var(--danger); }
.toast.success { border-left: 3px solid var(--success); color: var(--success); }
@keyframes toast-in { from{transform:translateY(20px);opacity:0} to{transform:translateY(0);opacity:1} }

@keyframes row-flash { from{background-color:rgba(59,130,246,0.2)} to{background-color:transparent} }
.conv-flash { animation: row-flash 600ms ease-out; }

/* ---- Responsive ---- */
@media (max-width: 900px) {
  .sidebar, .conv-panel {
    width: 100%; min-width: 100%;
    position: absolute; top: 0; left: 0; height: 100%; z-index: 20;
  }
  .thread-panel { min-width: 0; }
  .mobile-hidden { display: none; }
}
