:root {
  /* Light theme (Geist) */
  --foreground: #171717;
  --background: #ffffff;
  --background-secondary: #fafafa;
  --secondary-text: #4d4d4d;
  --border: #eaeaea;
  --border-hover: #c9c9c9;
  --surface: #ffffff;
  --surface-hover: #f2f2f2;
  --primary: #006bff;
  --primary-hover: #0059ec;
  --primary-bg: #f0f7ff;
  --primary-bg-hover: #e0f0ff;
  --primary-text: #0059ec;
  --danger: #ea001d;
  --danger-hover: #fc0035;
  --success: #28a948;
  --warning: #ffae00;
  --ring: #cae7ff;

  --radius-sm: 6px;
  --radius-md: 12px;
  --shadow-card: 0 2px 2px rgba(0, 0, 0, 0.04);
  --shadow-popover: 0 1px 1px rgba(0, 0, 0, 0.02), 0 4px 8px -4px rgba(0, 0, 0, 0.04), 0 16px 24px -8px rgba(0, 0, 0, 0.06);

  --sidebar-width: 320px;
  --chat-item-height: 84px;
  --header-height: 72px;
}

:root[data-theme="dark"] {
  /* Dark theme (Geist) */
  --foreground: #ededed;
  --background: #000000;
  --background-secondary: #000000;
  --secondary-text: #a0a0a0;
  --border: #2e2e2e;
  --border-hover: #454545;
  --surface: #000000;
  --surface-hover: #1a1a1a;
  --primary: #006efe;
  --primary-hover: #005be7;
  --primary-bg: #06193a;
  --primary-bg-hover: #003674;
  --primary-text: #47a8ff;
  --danger: #e2162a;
  --danger-hover: #f13242;
  --success: #00ac3a;
  --warning: #ffae00;
  --ring: #003674;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  font-family: 'Geist Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--background-secondary);
  color: var(--foreground);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* Login screen */
.login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--background-secondary);
  z-index: 50;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 32px;
  box-shadow: var(--shadow-popover);
  text-align: center;
}

.login-card .brand h1 {
  margin: 0 0 4px 0;
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--foreground);
}

.login-text {
  color: var(--secondary-text);
  font-size: 0.95rem;
  line-height: 1.5;
  margin: 0 0 20px 0;
}

.qr-large {
  min-height: 320px;
  padding: 24px;
}

.qr-large img {
  width: 280px;
  height: 280px;
}

.login-status {
  margin-top: 18px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
}

.login-status.connecting { background: var(--warning); color: #000; }
.login-status.qr { background: var(--primary); color: #fff; }
.login-status.error { background: var(--danger); color: #fff; }

/* Dashboard */
.dashboard {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand h1 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.subtitle {
  color: var(--secondary-text);
  font-size: 0.8rem;
}

.status-bar {
  display: flex;
  align-items: center;
  gap: 16px;
}

.status-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-label {
  color: var(--secondary-text);
  font-size: 0.85rem;
}

.status-badge {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.status-badge.connecting { background: rgba(255, 174, 0, 0.15); color: var(--warning); }
.status-badge.qr { background: var(--primary-bg); color: var(--primary-text); }
.status-badge.connected { background: rgba(0, 172, 58, 0.15); color: var(--success); }
.status-badge.disconnected,
.status-badge.error { background: rgba(226, 22, 42, 0.15); color: var(--danger); }

.phone {
  font-weight: 600;
  font-size: 0.9rem;
}

.layout {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: calc(100vh - var(--header-height));
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--header-height));
  position: sticky;
  top: var(--header-height);
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h2 {
  margin: 0 0 12px 0;
  font-size: 0.95rem;
  font-weight: 600;
}

.sidebar-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}

.sidebar-tab {
  flex: 1;
  min-width: 0;
  height: 36px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--secondary-text);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
  white-space: nowrap;
}

.sidebar-tab:hover {
  background: var(--surface-hover);
  color: var(--foreground);
}

.sidebar-tab.active {
  background: var(--foreground);
  color: var(--background);
  border-color: var(--foreground);
}

.content {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  min-width: 0;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  box-shadow: var(--shadow-card);
}

.card h2 {
  margin: 0 0 14px 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--foreground);
}

.qr-section p {
  color: var(--secondary-text);
  font-size: 0.9rem;
  margin: 0 0 12px 0;
}

.qr-wrap {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 220px;
}

.qr-wrap img {
  max-width: 100%;
  height: auto;
}

#qr-countdown {
  text-align: center;
  font-size: 13px;
  color: var(--secondary-text);
  margin-top: 8px;
  font-variant-numeric: tabular-nums;
}

.qr-placeholder {
  color: var(--secondary-text);
  font-size: 0.95rem;
}

.hint {
  font-size: 0.8rem;
  color: var(--secondary-text);
  margin-top: 10px;
  margin-bottom: 0;
}

.chats-list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.chat-item {
  height: var(--chat-item-height);
  min-height: var(--chat-item-height);
  max-height: var(--chat-item-height);
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s ease, border-left-color 0.15s ease;
  border-left: 4px solid transparent;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  position: relative;
}

.chat-item.excluded {
  opacity: 0.5;
  border-left-color: var(--danger);
}

.chat-item.excluded:hover {
  opacity: 0.8;
}

.btn-exclude {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--secondary-text);
  font-size: 0.7rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.btn-exclude:hover {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
}

.chat-item.excluded .btn-exclude {
  background: var(--danger);
  color: #fff;
  border-color: var(--danger);
}

.chat-item.excluded .btn-exclude:hover {
  background: var(--surface);
  color: var(--secondary-text);
  border-color: var(--border);
}

.blocked-label {
  font-size: 0.75rem;
  color: var(--danger);
  font-weight: 500;
}

.excluded-badge {
  font-size: 0.65rem;
  color: var(--danger);
  background: rgba(226, 22, 42, 0.1);
  padding: 1px 6px;
  border-radius: 999px;
  margin-left: 6px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.chat-item:hover {
  background: var(--primary-bg);
}

.chat-item.active {
  background: linear-gradient(90deg, var(--primary-bg) 0%, var(--primary-bg-hover) 100%);
  border-left-color: var(--primary);
}

.chat-item.active .chat-name {
  color: var(--primary-text);
}

.chat-name {
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--foreground);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--secondary-text);
}

.chat-last {
  font-size: 0.8rem;
  color: var(--secondary-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.input {
  height: 40px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--foreground);
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px var(--background), 0 0 0 4px var(--ring);
}

.btn {
  height: 40px;
  padding: 0 14px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.05s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--foreground);
  color: var(--background);
}

.btn-primary:hover {
  background: var(--secondary-text);
}

.btn-secondary {
  background: var(--surface);
  color: var(--foreground);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  border-color: var(--border-hover);
  background: var(--surface-hover);
}

.btn-small {
  height: 28px;
  padding: 0 10px;
  font-size: 0.75rem;
  margin-left: 8px;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

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

.btn-ghost {
  background: transparent;
  color: var(--secondary-text);
  border: 1px solid var(--border);
}

.btn-ghost:hover {
  color: var(--foreground);
  background: var(--surface-hover);
  border-color: var(--border-hover);
}

.phone-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.phone-actions.hidden {
  display: none;
}

.messages-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.messages-header h2 {
  margin: 0;
  display: flex;
  align-items: center;
  font-size: 1rem;
}

.count {
  background: var(--surface-hover);
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--secondary-text);
}

.messages-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: calc(100vh - var(--header-height) - 120px);
  overflow-y: auto;
}

.message {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px;
  border-left: 3px solid var(--border);
}

.message.has-media {
  border-left-color: var(--primary);
}

.message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
  flex-wrap: wrap;
  gap: 8px;
}

.message-sender {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--foreground);
}

.message-chat {
  font-size: 0.75rem;
  color: var(--secondary-text);
  background: var(--surface-hover);
  padding: 2px 8px;
  border-radius: 999px;
}

.message-date {
  font-size: 0.75rem;
  color: var(--secondary-text);
}

.message-body {
  font-size: 0.9rem;
  line-height: 1.5;
  word-break: break-word;
  color: var(--foreground);
}

.message-media {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding: 6px 10px;
  background: var(--primary-bg);
  color: var(--primary-text);
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  font-weight: 500;
}

.message-media-preview {
  display: block;
  max-width: 100%;
  max-height: 320px;
  margin-top: 10px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
}

.message-media-audio {
  display: block;
  width: 100%;
  margin-top: 10px;
}

.message-media-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  padding: 8px 12px;
  background: var(--primary-bg);
  color: var(--primary-text);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 500;
  text-decoration: none;
}

.message-media-link:hover {
  text-decoration: underline;
}

.empty {
  color: var(--secondary-text);
  text-align: center;
  padding: 30px 0;
  font-size: 0.9rem;
}

.toast {
  position: fixed;
  bottom: 20px;
  right: 20px;
  padding: 12px 18px;
  background: var(--surface);
  color: var(--foreground);
  border: 1px solid var(--border);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-popover);
  transform: translateY(120px);
  transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.1);
  z-index: 100;
}

.toast.show {
  transform: translateY(0);
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- App --- */
.app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* --- Phone bar --- */
.phone-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  justify-content: center;
  overflow: hidden;
}

.phone-tabs {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  max-width: 100%;
}

.phone-tab {
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--secondary-text);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.phone-tab:hover {
  background: var(--surface-hover);
  color: var(--foreground);
}

.phone-tab.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

.btn-sm {
  height: 32px;
  padding: 0 10px;
  font-size: 0.8rem;
}

/* --- QR view (inline, not fullscreen) --- */
.qr-view {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 32px 16px;
}

.qr-view.hidden {
  display: none;
}

.qr-card {
  width: 100%;
  max-width: 420px;
  text-align: center;
}

.messages-view {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.messages-view.hidden {
  display: none;
}

/* --- Config button --- */
.btn-config {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--secondary-text);
  font-size: 0.8rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.btn-config:hover {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

/* --- Importance badges --- */
.importance-badge {
  font-size: 0.65rem;
  padding: 1px 6px;
  border-radius: 999px;
  margin-left: 4px;
  font-weight: 600;
  white-space: nowrap;
}

.importance-high {
  background: rgba(226, 22, 42, 0.12);
  color: var(--danger);
}

.importance-medium {
  background: rgba(255, 174, 0, 0.15);
  color: var(--warning);
}

.importance-low {
  background: rgba(160, 160, 160, 0.15);
  color: var(--secondary-text);
}

.chat-item .chat-name {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
}

/* --- Modal --- */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}

.modal-content {
  position: relative;
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-popover);
  padding: 24px;
}

.modal-header {
  margin-bottom: 20px;
}

.modal-header h3 {
  margin: 0 0 4px 0;
  font-size: 1.1rem;
  font-weight: 600;
}

.modal-contact-name {
  color: var(--secondary-text);
  font-size: 0.9rem;
}

.modal-body {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.config-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.config-label {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--foreground);
}

/* Toggle switch */
.toggle {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--border);
  border-radius: 24px;
  transition: background 0.2s ease;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s ease;
}

.toggle input:checked + .toggle-slider {
  background: var(--danger);
}

.toggle input:checked + .toggle-slider::before {
  transform: translateX(20px);
}

/* Importance selector */
.importance-selector {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.importance-option {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: var(--radius-sm);
  transition: background 0.15s ease;
}

.importance-option:hover {
  background: var(--surface-hover);
}

.importance-option input[type="radio"] {
  accent-color: var(--primary);
}

.importance-option .importance-badge {
  margin-left: 0;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
  }
  .sidebar {
    height: auto;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }
  .chats-list {
    max-height: 320px;
  }
  .header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
    height: auto;
    padding: 12px 16px;
  }
}
