:root {
  --ink: #14171C;
  --ink-soft: #1D2129;
  --ink-softer: #262B33;
  --paper: #FAF9F6;
  --paper-dim: #F1EFE9;
  --border: #E4E2DC;
  --accent: #2F6F62;
  --accent-hover: #275C51;
  --accent-soft: #E4EFEC;
  --text-main: #1C1E22;
  --text-muted: #6B7280;
  --danger: #B3441E;
  --danger-soft: #FBEAE2;
  --success: #3F8F5F;
  --warn: #C97A2B;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-sm: 0 1px 2px rgba(20,23,28,0.06);
  --shadow-md: 0 8px 24px rgba(20,23,28,0.10);
  --shadow-lg: 0 20px 48px rgba(20,23,28,0.22);
  font-family: 'Manrope', -apple-system, BlinkMacSystemFont, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--paper); color: var(--text-main); }

button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; }

/* thin scrollbars, everywhere */
* { scrollbar-width: thin; scrollbar-color: rgba(0,0,0,0.18) transparent; }
::-webkit-scrollbar { width: 7px; height: 7px; }
::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.18); border-radius: 999px; }
::-webkit-scrollbar-track { background: transparent; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
@keyframes pulseDot { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

/* ---------- Auth page ---------- */
.auth-body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background:
    radial-gradient(circle at 15% 15%, rgba(47,111,98,0.35), transparent 45%),
    radial-gradient(circle at 85% 85%, rgba(47,111,98,0.20), transparent 40%),
    var(--ink);
}
.auth-card {
  background: var(--paper);
  width: 380px;
  max-width: 90vw;
  padding: 40px 32px;
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.35s ease;
}
.auth-mark {
  font-weight: 800; letter-spacing: -0.02em; color: var(--accent); margin-bottom: 20px;
  display: flex; align-items: center; gap: 8px; font-size: 15px;
}
.auth-mark::before {
  content: ''; width: 22px; height: 22px; border-radius: 7px;
  background: linear-gradient(135deg, var(--accent), #4C9484);
  display: inline-block;
}
.auth-card h1 { font-size: 26px; margin: 0 0 6px; font-weight: 700; letter-spacing: -0.01em; }
.auth-sub { color: var(--text-muted); font-size: 14px; margin: 0 0 24px; line-height: 1.5; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; }
.auth-card label { font-size: 13px; font-weight: 600; color: var(--text-muted); display: flex; flex-direction: column; gap: 6px; }
.auth-card input {
  padding: 11px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 15px;
  background: white; transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.auth-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.auth-card button[type="submit"] {
  margin-top: 8px; background: var(--accent); color: white; border: none;
  padding: 12px; border-radius: var(--radius-sm); font-weight: 700; font-size: 15px;
  transition: background 0.15s ease, transform 0.1s ease;
}
.auth-card button[type="submit"]:hover { background: var(--accent-hover); }
.auth-card button[type="submit"]:active { transform: scale(0.99); }
.auth-error {
  color: var(--danger); font-size: 13px; margin: 2px 0 0; background: var(--danger-soft);
  padding: 8px 10px; border-radius: var(--radius-sm); animation: fadeIn 0.15s ease;
}

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

.sidebar {
  width: 320px; flex-shrink: 0; background: var(--ink);
  color: #E7E5DF; display: flex; flex-direction: column;
}
.sidebar-top { padding: 18px 16px 12px; display: flex; align-items: center; justify-content: space-between; }
.brand { font-weight: 800; font-size: 16px; letter-spacing: -0.01em; display: flex; align-items: center; gap: 8px; }
.brand::before {
  content: ''; width: 20px; height: 20px; border-radius: 6px;
  background: linear-gradient(135deg, var(--accent), #4C9484); flex-shrink: 0;
}

.status-pill {
  border: none; border-radius: 999px; font-size: 11px; font-weight: 700;
  padding: 5px 10px 5px 8px; color: white; text-transform: none;
  display: inline-flex; align-items: center; gap: 6px; transition: background 0.2s ease;
}
.status-pill::before { content: ''; width: 6px; height: 6px; border-radius: 999px; background: currentColor; }
.status-connecting, .status-connected-transient { background: var(--warn); }
.status-connecting::before, .status-qr_pending::before { animation: pulseDot 1.4s ease-in-out infinite; }
.status-connected { background: var(--success); }
.status-disconnected, .status-auth_failed { background: var(--danger); }
.status-qr_pending { background: var(--warn); }

.search-row { padding: 0 16px 12px; }
.search-row input {
  width: 100%; padding: 9px 12px 9px 34px; border-radius: var(--radius-sm); border: 1px solid #2B3038;
  background: var(--ink-soft) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='15' height='15' viewBox='0 0 24 24' fill='none' stroke='%237A8089' stroke-width='2' stroke-linecap='round'%3E%3Ccircle cx='11' cy='11' r='7'/%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'/%3E%3C/svg%3E") no-repeat 11px center;
  color: #E7E5DF; font-size: 14px; transition: border-color 0.15s ease;
}
.search-row input:focus { outline: none; border-color: var(--accent); }
.search-row input::placeholder { color: #7A8089; }

.filter-row { display: flex; gap: 8px; padding: 0 16px 12px; }
.filter-row select {
  flex: 1; padding: 7px 8px; border-radius: var(--radius-sm); border: 1px solid #2B3038;
  background: var(--ink-soft); color: #E7E5DF; font-size: 12px; cursor: pointer;
}

.contact-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.contact-list-empty {
  padding: 40px 20px; text-align: center; color: #7A8089; font-size: 13px; line-height: 1.6;
}
.contact-item-status {
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em;
  padding: 2px 7px; border-radius: 999px; display: inline-block; margin-top: 6px;
}
.contact-item-status.lead { background: rgba(201,122,43,0.18); color: #E3A469; }
.contact-item-status.client { background: rgba(47,111,98,0.25); color: #6FBFAE; }
.contact-item-status.completed { background: rgba(107,114,128,0.25); color: #B4B8BF; }
.contact-item {
  padding: 10px 8px; display: flex; gap: 10px; cursor: pointer;
  border-radius: var(--radius-sm); margin: 2px 0; transition: background 0.12s ease;
}
.contact-item:hover { background: rgba(255,255,255,0.05); }
.contact-item.active { background: rgba(47,111,98,0.22); }
.contact-item.kbd-focus { outline: 1px solid rgba(255,255,255,0.25); outline-offset: -1px; }
.contact-item-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.contact-item-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.contact-item-name { font-weight: 700; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-item-time { font-size: 11px; color: #8B9099; flex-shrink: 0; }
.contact-item-preview { font-size: 13px; color: #A8ACB3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.unread-badge {
  background: var(--accent); color: white; font-size: 11px; font-weight: 700;
  border-radius: 999px; min-width: 18px; height: 18px; display: inline-flex;
  align-items: center; justify-content: center; padding: 0 5px; flex-shrink: 0;
}

.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px; color: white; letter-spacing: -0.01em;
}
.avatar-sm { width: 40px; height: 40px; }
.avatar-lg { width: 44px; height: 44px; font-size: 16px; }

.sidebar-bottom {
  padding: 12px 16px; border-top: 1px solid var(--ink-softer); display: flex; align-items: center; gap: 12px; font-size: 13px;
}
#agent-name { color: #A8ACB3; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-btn { background: none; border: none; color: #C7CBD1; font-size: 13px; font-weight: 600; padding: 0; transition: color 0.12s ease; }
.link-btn:hover { color: white; }

/* ---------- Thread ---------- */
.thread { flex: 1; display: flex; flex-direction: column; background: var(--paper); min-width: 0; }
.thread-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--text-muted); font-size: 14px; gap: 14px; text-align: center; padding: 20px;
}
.thread-empty svg { opacity: 0.5; }
.thread-empty p { margin: 0; max-width: 240px; line-height: 1.5; }
.thread-active { flex: 1; display: flex; flex-direction: column; min-height: 0; }

.back-btn {
  display: none; background: none; border: none; padding: 4px; margin-right: 4px;
  color: var(--text-main); align-items: center; justify-content: center; border-radius: 6px;
}
.back-btn:hover { background: var(--paper-dim); }

.thread-header {
  padding: 14px 24px; border-bottom: 1px solid var(--border); display: flex;
  align-items: center; justify-content: space-between; background: white; gap: 12px;
}
.thread-header-left { display: flex; align-items: center; gap: 12px; min-width: 0; }
.thread-name { font-weight: 700; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-phone { font-size: 13px; color: var(--text-muted); }
.thread-header-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.channel-badge {
  font-size: 11px; font-weight: 700; color: var(--accent); background: var(--accent-soft);
  padding: 4px 10px; border-radius: 999px; text-transform: capitalize;
}
.icon-btn {
  background: none; border: none; color: var(--text-muted); padding: 6px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; transition: background 0.12s ease, color 0.12s ease;
}
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); }
.status-badge {
  font-size: 11px; font-weight: 700; padding: 4px 10px; border-radius: 999px; text-transform: capitalize;
}
.status-badge.lead { background: #FCEEDF; color: var(--warn); }
.status-badge.client { background: var(--accent-soft); color: var(--accent); }
.status-badge.completed { background: #ECEDEF; color: var(--text-muted); }

.messages {
  flex: 1; overflow-y: auto; padding: 20px 24px; display: flex; flex-direction: column; gap: 10px;
  background-image: radial-gradient(rgba(20,23,28,0.035) 1px, transparent 1px);
  background-size: 18px 18px;
}
.day-divider {
  align-self: center; font-size: 11px; font-weight: 700; color: var(--text-muted);
  background: var(--paper-dim); padding: 4px 12px; border-radius: 999px; margin: 6px 0;
}
.bubble {
  max-width: 60%; padding: 9px 13px; border-radius: 16px; font-size: 14px; line-height: 1.45;
  box-shadow: var(--shadow-sm); animation: slideUp 0.18s ease; word-wrap: break-word;
}
.bubble.in { align-self: flex-start; background: white; border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble.out { align-self: flex-end; background: var(--accent); color: white; border-bottom-right-radius: 4px; }
.bubble.out.failed { background: var(--danger); }
.bubble-time { display: block; font-size: 10.5px; margin-top: 4px; opacity: 0.65; text-align: right; }
.bubble img, .bubble video { max-width: 100%; border-radius: 8px; margin-bottom: 6px; display: block; }

.composer { display: flex; gap: 10px; padding: 14px 24px; border-top: 1px solid var(--border); background: white; align-items: center; }
.composer input {
  flex: 1; padding: 12px 16px; border: 1px solid var(--border); border-radius: 999px; font-size: 14px;
  background: var(--paper-dim); transition: border-color 0.15s ease, background 0.15s ease;
}
.composer input:focus { outline: none; border-color: var(--accent); background: white; }
.composer button {
  background: var(--accent); color: white; border: none; width: 42px; height: 42px; border-radius: 50%;
  font-weight: 700; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  transition: background 0.15s ease, transform 0.1s ease;
}
.composer button:hover { background: var(--accent-hover); }
.composer button:active { transform: scale(0.95); }

/* ---------- Quick replies ---------- */
.quick-reply-picker {
  margin: 0 24px; background: white; border: 1px solid var(--border); border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md); max-height: 220px; overflow-y: auto; animation: slideUp 0.15s ease;
}
.quick-reply-option { padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--paper-dim); }
.quick-reply-option:last-child { border-bottom: none; }
.quick-reply-option:hover, .quick-reply-option.selected { background: var(--accent-soft); }
.quick-reply-option .qr-shortcut { font-weight: 700; font-size: 13px; color: var(--accent); }
.quick-reply-option .qr-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.quick-reply-empty { padding: 12px 14px; font-size: 13px; color: var(--text-muted); }

.modal-hint { font-size: 13px; color: var(--text-muted); margin: -6px 0 14px; }
.modal-hint code { background: var(--paper-dim); padding: 1px 6px; border-radius: 4px; }
.quick-replies-list { display: flex; flex-direction: column; gap: 8px; max-height: 240px; overflow-y: auto; margin-bottom: 16px; }
.quick-reply-row {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 10px;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.quick-reply-row-text { min-width: 0; }
.quick-reply-row-title { font-weight: 700; font-size: 13px; }
.quick-reply-row-shortcut { font-size: 11px; color: var(--accent); font-weight: 600; }
.quick-reply-row-body { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.quick-reply-row button {
  background: none; border: none; color: var(--danger); font-weight: 700; font-size: 13px; flex-shrink: 0;
}
.quick-reply-form { display: flex; flex-direction: column; gap: 8px; }
.quick-reply-form input, .quick-reply-form textarea {
  padding: 9px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 13px; font-family: inherit; resize: vertical;
}
.quick-reply-form input:focus, .quick-reply-form textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* ---------- Details panel ---------- */
.details-panel {
  width: 300px; flex-shrink: 0; background: white; border-left: 1px solid var(--border);
  padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px;
  animation: slideUp 0.2s ease;
}
.details-head { display: flex; justify-content: space-between; align-items: center; }
.details-head h2 { font-size: 15px; margin: 0; }
.details-head .link-btn { font-size: 20px; line-height: 1; color: var(--text-muted); }

.details-label {
  display: flex; flex-direction: column; gap: 6px; font-size: 12px; font-weight: 700;
  color: var(--text-muted); text-transform: none;
}
.details-label input, .details-label select, .details-label textarea {
  padding: 9px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 14px;
  font-weight: 400; color: var(--text-main); font-family: inherit; resize: vertical;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.details-label input:focus, .details-label select:focus, .details-label textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.details-label input:disabled { background: var(--paper-dim); color: var(--text-muted); }

.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; background: var(--accent-soft); color: var(--accent);
  font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 999px;
}
.chip button {
  background: none; border: none; color: var(--accent); font-weight: 800; padding: 0; font-size: 13px; line-height: 1;
}

.group-add-row { display: flex; gap: 6px; }
.group-add-row select, .group-add-row input { flex: 1; padding: 7px 8px; font-size: 13px; border: 1px solid var(--border); border-radius: var(--radius-sm); }
.group-add-row button {
  background: var(--paper-dim); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 0 12px; font-weight: 700; font-size: 13px;
  transition: background 0.12s ease;
}
.group-add-row button:hover { background: var(--border); }

.save-btn {
  background: var(--accent); color: white; border: none; padding: 11px; border-radius: var(--radius-sm); font-weight: 700; font-size: 14px;
  transition: background 0.15s ease;
}
.save-btn:hover { background: var(--accent-hover); }
.save-confirmation { color: var(--success); font-size: 12px; font-weight: 600; margin: -4px 0 0; text-align: center; animation: fadeIn 0.15s ease; }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(20,23,28,0.55); display: flex;
  align-items: center; justify-content: center; z-index: 50; animation: fadeIn 0.15s ease;
}
.modal { background: white; width: 420px; max-width: 92vw; border-radius: 16px; padding: 24px; box-shadow: var(--shadow-lg); animation: slideUp 0.2s ease; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.modal-head h2 { font-size: 18px; margin: 0; }
#settings-body { font-size: 14px; color: var(--text-main); line-height: 1.5; }
#settings-body img { width: 100%; border-radius: var(--radius-sm); margin: 14px 0; border: 1px solid var(--border); padding: 8px; background: white; }
#settings-body input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); margin: 8px 0; font-size: 14px;
}
#settings-body input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
#settings-body button {
  background: var(--accent); color: white; border: none; padding: 10px 16px; border-radius: var(--radius-sm); font-weight: 700;
  transition: background 0.15s ease;
}
#settings-body button:hover { background: var(--accent-hover); }
.pairing-code { font-size: 26px; font-weight: 800; letter-spacing: 0.15em; text-align: center; padding: 14px; background: var(--paper-dim); border-radius: var(--radius-sm); margin: 10px 0; }

@media (max-width: 720px) {
  .app { position: relative; }
  .sidebar { width: 100%; }
  .thread { display: none; }
  .app.show-thread .sidebar { display: none; }
  .app.show-thread .thread { display: flex; }
  .back-btn { display: flex; }
  .details-panel { position: absolute; inset: 0; z-index: 20; width: 100%; }
  .bubble { max-width: 82%; }
}

/* Ensure the HTML hidden attribute always wins over display rules */
[hidden] { display: none !important; }
