/* ========================================
   Nova private messaging
   Two panes on desktop, one at a time on mobile.
======================================== */

.nova-chat {
  display: grid;
  grid-template-columns: minmax(260px, 320px) 1fr;
  gap: 0;
  height: calc(100vh - var(--header-height) - 3rem);
  min-height: 480px;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-xl);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}

/* -------------------------------------------------------------- thread list */

.chat-threads {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border-light);
  min-width: 0;
  min-height: 0;
}

.chat-threads-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1.15rem;
  border-bottom: 1px solid var(--border-light);
}

.chat-threads-header h1 {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text-primary);
}

.chat-new-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 1px solid var(--border-light);
  border-radius: 50%;
  background: var(--bg-card);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition-fast);
}

.chat-new-btn:hover {
  background: var(--secondary);
  border-color: var(--secondary);
  color: #fff;
}

.chat-search {
  padding: 0.85rem;
  border-bottom: 1px solid var(--border-light);
  background: var(--bg-hover);
}

.chat-search input {
  width: 100%;
  padding: 0.6rem 0.8rem;
  font: inherit;
  font-size: 0.9rem;
  color: var(--text-primary);
  background: var(--bg-card);
  border: 1px solid var(--border-medium);
  border-radius: var(--radius-md);
}

.chat-search input:focus {
  outline: none;
  border-color: var(--secondary);
}

.chat-search-results {
  margin-top: 0.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.chat-search-results button {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.45rem 0.5rem;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  cursor: pointer;
  text-align: left;
  color: var(--text-primary);
}

.chat-search-results button:hover {
  background: var(--bg-card);
}

.chat-search-results strong {
  display: block;
  font-size: 0.9rem;
}

.chat-search-results em {
  font-style: normal;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.chat-search-empty {
  font-size: 0.85rem;
  color: var(--text-secondary);
  padding: 0.35rem 0.5rem;
}

.chat-thread-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 0.5rem;
}

.chat-list-empty {
  padding: 1.5rem 1rem;
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text-secondary);
  text-align: center;
}

.chat-thread-row {
  position: relative;
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.15rem;
  border-radius: var(--radius-lg);
  background: transparent;
  transition: background var(--transition-fast);
}

.chat-thread-row:hover,
.chat-thread-row:focus-within {
  background: var(--bg-hover);
}

.chat-thread-row.is-active {
  background: var(--bg-hover);
  box-shadow: inset 3px 0 0 var(--secondary);
}

.chat-thread-row-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.7rem;
  border: none;
  border-radius: var(--radius-lg);
  background: transparent;
  cursor: pointer;
  text-align: left;
  color: inherit;
  font: inherit;
}

.chat-thread-row-menu {
  position: absolute;
  top: 50%;
  right: 0.35rem;
  z-index: 3;
  transform: translateY(-50%);
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.chat-thread-row-menu .chat-menu-btn {
  width: 32px;
  height: 32px;
  background: var(--bg-card);
  box-shadow: 0 0 0 1px var(--border-light);
}

.chat-thread-row:hover .chat-thread-row-menu,
.chat-thread-row:focus-within .chat-thread-row-menu,
.chat-thread-row-menu:has(.chat-menu:not([hidden])) {
  opacity: 1;
}

@media (hover: none), (max-width: 900px) {
  .chat-thread-row-menu {
    opacity: 1;
  }
}

.chat-thread-row-body {
  flex: 1;
  min-width: 0;
}

.chat-thread-row-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.5rem;
}

.chat-thread-row-top strong {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-primary);
  display: -webkit-box;
  overflow: hidden;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-clamp: 2;
  line-height: 1.25;
  text-overflow: ellipsis;
}

.chat-thread-row-top time {
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
}

.chat-thread-row-preview {
  display: block;
  margin-top: 0.15rem;
  font-size: 0.85rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-thread-row.has-unread .chat-thread-row-preview {
  color: var(--text-primary);
  font-weight: 500;
}

/* The preview used to carry the unread weight on its own. A row without one,
   like the advocate's, would otherwise read as already opened. */
.chat-thread-row.has-unread .chat-thread-row-top strong {
  font-weight: 700;
}

.chat-thread-row-count {
  flex-shrink: 0;
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  display: grid;
  place-items: center;
  font-size: 0.7rem;
  font-weight: 700;
  color: #fff;
  background: var(--secondary);
  border-radius: 999px;
}

/* ------------------------------------------------------------------- avatars */

.chat-avatar {
  width: 42px;
  height: 42px;
  flex-shrink: 0;
  border-radius: 50%;
  object-fit: cover;
  display: grid;
  place-items: center;
  font-size: 0.85rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
}

.chat-avatar.is-anonymous {
  background: linear-gradient(135deg, #64748b, #334155);
}

/* ------------------------------------------------------------------- thread */

.chat-thread {
  display: flex;
  flex-direction: column;
  min-width: 0;
  /* A grid item defaults to the height of its content, which would push the
     message list past the card instead of letting it scroll inside it. */
  min-height: 0;
  overflow: hidden;
  background: var(--bg-main);
}

.chat-thread-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.85rem 1.15rem;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border-light);
}

.chat-thread-header .chat-avatar {
  width: 38px;
  height: 38px;
}

.chat-back {
  display: none;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
}

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

.chat-thread-who strong {
  display: block;
  font-size: 1rem;
  color: var(--text-primary);
}

.chat-thread-who span,
.chat-thread-who a {
  font-size: 0.82rem;
  color: var(--text-secondary);
  text-decoration: none;
}

.chat-thread-who a:hover {
  color: var(--secondary);
  text-decoration: underline;
}

.chat-thread-menu {
  position: relative;
}

.chat-menu-btn {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
}

.chat-menu-btn:hover {
  background: var(--bg-hover);
}

.chat-menu {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  z-index: 20;
  min-width: 190px;
  padding: 0.35rem;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
}

.chat-menu button {
  display: block;
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-primary);
  font: inherit;
  font-size: 0.88rem;
  text-align: left;
  cursor: pointer;
}

.chat-menu button:hover {
  background: var(--bg-hover);
}

.chat-menu button.is-danger {
  color: var(--danger);
}

/* ----------------------------------------------------------------- messages */

.chat-messages {
  flex: 1;
  /* Same reason as .chat-thread: without this the list refuses to shrink below
     its content and overflow-y never has anything to scroll. */
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.25rem 1.15rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.chat-thread-intro {
  margin: auto;
  max-width: 340px;
  text-align: center;
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text-muted);
}

.chat-thread-intro h2 {
  margin: 0 0 0.35rem;
  font-size: 1rem;
}

.chat-thread-intro.is-error,
.chat-list-empty.is-error {
  color: var(--text-secondary);
}

.chat-thread-intro.is-error .otv-secondary-btn,
.chat-list-empty.is-error .otv-secondary-btn {
  margin-top: 0.75rem;
}

.chat-day {
  align-self: center;
  margin: 0.85rem 0 0.5rem;
  padding: 0.2rem 0.7rem;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: 999px;
}

.chat-system {
  align-self: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 0.3rem 0;
}

.chat-message {
  position: relative;
  align-self: flex-start;
  max-width: min(72%, 560px);
  padding: 0.6rem 0.85rem 0.4rem;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
  border-bottom-left-radius: var(--radius-sm);
}

.chat-message.is-mine {
  align-self: flex-end;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  border-color: transparent;
  border-radius: var(--radius-lg);
  border-bottom-right-radius: var(--radius-sm);
}

.chat-message.is-mine.is-pending {
  opacity: 0.84;
}

.chat-message.is-mine.is-failed {
  background: #fff1f2;
  border-color: #fecaca;
}

.chat-message.is-mine.is-failed p {
  color: var(--text-primary);
}

.chat-message.is-grouped {
  border-radius: var(--radius-lg);
}

.chat-message p {
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--text-primary);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.chat-message.is-mine p {
  color: #fff;
}

.chat-message.is-withdrawn p {
  font-style: italic;
  color: var(--text-muted);
}

.chat-message.is-withdrawn.is-mine {
  background: var(--bg-card);
  border-color: var(--border-light);
}

.chat-message.is-withdrawn.is-mine p {
  color: var(--text-muted);
}

.chat-message footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.4rem;
  margin-top: 0.15rem;
}

.chat-message time {
  font-size: 0.68rem;
  color: var(--text-muted);
}

.chat-message.is-mine time {
  color: rgba(255, 255, 255, 0.75);
}

.chat-delivery {
  border: 0;
  padding: 0;
  background: transparent;
  color: rgba(255, 255, 255, 0.78);
  font: inherit;
  font-size: 0.68rem;
}

button.chat-delivery.is-failed {
  color: #dc2626;
  cursor: pointer;
  font-weight: 700;
}

.chat-message-action {
  border: none;
  background: transparent;
  padding: 0;
  font-size: 0.68rem;
  color: var(--text-muted);
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--transition-fast);
}

.chat-message.is-mine .chat-message-action {
  color: rgba(255, 255, 255, 0.75);
}

.chat-message:hover .chat-message-action,
.chat-message-action:focus-visible {
  opacity: 1;
}

.chat-reported {
  font-size: 0.68rem;
  color: var(--text-muted);
}

/* ----------------------------------------------------------------- composer */

.chat-composer {
  padding: 0.85rem 1.15rem 1rem;
  background: var(--bg-card);
  border-top: 1px solid var(--border-light);
}

.chat-anon-note {
  margin-bottom: 0.5rem;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.chat-composer-row {
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
}

.chat-composer textarea {
  flex: 1;
  resize: none;
  max-height: 160px;
  overflow-y: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding: 0.65rem 0.85rem;
  font: inherit;
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--bg-main);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-lg);
}

.chat-composer textarea::-webkit-scrollbar {
  display: none;
}

.chat-composer textarea:focus {
  outline: none;
  border-color: var(--secondary);
}

.chat-send {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  border: none;
  border-radius: 50%;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  color: #fff;
  cursor: pointer;
  transition: transform var(--transition-fast);
}

.chat-send:hover:not(:disabled) {
  transform: translateY(-1px);
}

.chat-send:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.chat-composer-error {
  margin-top: 0.5rem;
  font-size: 0.82rem;
  color: var(--danger);
}

.chat-composer-closed {
  padding: 1rem 1.15rem;
  font-size: 0.88rem;
  color: var(--text-secondary);
  background: var(--bg-card);
  border-top: 1px solid var(--border-light);
  text-align: center;
}

/* ------------------------------------------------------------ empty states */

.chat-empty {
  margin: auto;
  padding: 2rem;
  max-width: 380px;
  text-align: center;
}

.chat-empty i {
  font-size: 2.25rem;
  color: var(--text-muted);
  margin-bottom: 0.9rem;
}

.chat-empty h2 {
  font-size: 1.1rem;
  color: var(--text-primary);
  margin-bottom: 0.45rem;
}

.chat-empty p {
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text-secondary);
}

.chat-empty-standalone {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-xl);
  padding: 3rem 2rem;
}

.chat-empty-standalone .otv-primary-btn {
  margin-top: 1.25rem;
}

.chat-loading {
  margin: auto;
  padding: 2rem;
  display: grid;
  place-items: center;
}

/* --------------------------------------------------------- sidebar unread */

.sidebar-chat-btn .chat-status.has-unread {
  width: auto;
  min-width: 18px;
  height: 18px;
  animation: none;
  box-shadow: none;
  padding: 0 5px;
  display: grid;
  place-items: center;
  font-size: 0.68rem;
  font-weight: 700;
  color: #1d4ed8;
  background: #fff;
  border-radius: 999px;
}

/* ------------------------------------------------------------------ mobile */

@media (max-width: 900px) {
  .nova-chat {
    grid-template-columns: 1fr;
    height: calc(100vh - var(--header-height) - 1.5rem);
  }

  .nova-chat[data-pane="list"] .chat-thread,
  .nova-chat[data-pane="thread"] .chat-threads {
    display: none;
  }

  .chat-threads {
    border-right: none;
  }

  .chat-back {
    display: grid;
    place-items: center;
  }

  .chat-message {
    max-width: 85%;
  }
}

/* ---------------------------------------------------------------- debates

   A debate thread carries context an ordinary message thread does not: which
   measure is being argued and which way round the two sides are. */

.chat-thread-row-context {
  display: -webkit-box;
  margin-top: 0.15rem;
  font-size: 0.75rem;
  font-weight: 600;
  color: #6366f1;
  overflow: hidden;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-clamp: 2;
  line-height: 1.25;
  text-overflow: ellipsis;
}

.chat-thread-row.is-synthetic {
  background: linear-gradient(90deg, rgba(124, 58, 237, 0.07), transparent);
}

.chat-thread-row.is-synthetic .chat-avatar {
  background: #7c3aed;
  color: #fff;
}

.chat-thread-row.is-synthetic .chat-thread-row-context,
.chat-thread-simulation-badge {
  color: #7c3aed;
}

.chat-thread-simulation-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin-left: auto;
  border: 1px solid #c4b5fd;
  border-radius: 999px;
  background: #f5f3ff;
  padding: 0.35rem 0.55rem;
  font-size: 0.62rem;
  font-weight: 800;
  letter-spacing: 0.05em;
}

.chat-composer-closed.is-synthetic {
  border-top-color: #c4b5fd;
  background: #f5f3ff;
  color: #6d28d9;
}

html.dark .chat-thread-row.is-synthetic,
html.dark .chat-composer-closed.is-synthetic {
  background: rgba(91, 33, 182, 0.16);
}

html.dark .chat-thread-simulation-badge {
  border-color: #6d28d9;
  background: rgba(91, 33, 182, 0.22);
  color: #ddd6fe;
}

.chat-thread-row-context i {
  margin-right: 0.3rem;
}

.chat-debate-bar {
  flex-shrink: 0;
  padding: 0.6rem 1rem 0.7rem;
  border-bottom: 1px solid var(--border-light);
  background: var(--bg-secondary, var(--bg-card));
}

.chat-debate-measure {
  margin: 0;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-primary);
}

.chat-debate-measure i {
  margin-right: 0.4rem;
  color: #6366f1;
}

.chat-debate-measure a {
  color: inherit;
  text-decoration: none;
}

.chat-debate-measure a:hover {
  text-decoration: underline;
}

.chat-debate-sides {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin: 0.5rem 0 0;
}

.chat-stance {
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 600;
}

.chat-stance.is-pro {
  color: #047857;
  background: rgba(16, 185, 129, 0.12);
}

.chat-stance.is-contra {
  color: #b91c1c;
  background: rgba(239, 68, 68, 0.12);
}

html.dark .chat-stance.is-pro {
  color: #6ee7b7;
}

html.dark .chat-stance.is-contra {
  color: #fca5a5;
}

/* ------------------------------------------------------------------ advocate */

/* Visibly not a member: the advocate gets a machine mark and a violet edge, so
   nothing it writes can be mistaken for another citizen's argument. */
.chat-avatar.is-advocate {
  color: #fff;
  background: linear-gradient(135deg, #8b5cf6, #6366f1);
}

.chat-message.is-advocate {
  border-left: 3px solid #8b5cf6;
  background: color-mix(in srgb, #8b5cf6 5%, var(--bg-card));
}

.chat-message-who {
  display: block;
  margin-bottom: 0.3rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: #7c3aed;
}

.chat-message-who i {
  margin-right: 0.3rem;
}

html.dark .chat-message-who {
  color: #c4b5fd;
}

.chat-debate-bar.is-advocate .chat-debate-measure i {
  color: #8b5cf6;
}

.chat-advocate-note {
  margin: 0.35rem 0 0;
  font-size: 0.75rem;
  line-height: 1.45;
  color: var(--text-muted);
}

.chat-prompt-chips {
  display: flex;
  gap: 0.4rem;
  overflow-x: auto;
  padding: 0 0 0.45rem;
  scrollbar-width: none;
}

.chat-prompt-chips::-webkit-scrollbar { display: none; }

.chat-prompt-chips button {
  flex: 0 0 auto;
  padding: 0.4rem 0.58rem;
  color: #6d28d9;
  background: color-mix(in srgb, #8b5cf6 9%, var(--bg-card));
  border: 1px solid color-mix(in srgb, #8b5cf6 24%, var(--border-light));
  border-radius: 999px;
  font: 600 0.7rem/1 'Inter', sans-serif;
  cursor: pointer;
}

html.dark .chat-prompt-chips button { color: #c4b5fd; }

.chat-thinking-state {
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr) auto;
  gap: 0.6rem;
  align-items: center;
  min-width: min(300px, 72vw);
  padding: 0.65rem;
  border: 1px solid color-mix(in srgb, #8b5cf6 20%, var(--border-light));
  border-radius: 10px;
  background: color-mix(in srgb, #8b5cf6 6%, var(--bg-card));
}

.chat-thinking-icon {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  color: #7c3aed;
  background: rgba(139, 92, 246, 0.12);
  border-radius: 9px;
}

.chat-thinking-state > span:nth-child(2) { display: grid; gap: 0.12rem; }
.chat-thinking-state strong { font-size: 0.75rem; color: var(--text-primary); }
.chat-thinking-state small { font-size: 0.68rem; color: var(--text-secondary); }
.chat-thinking-state > i { color: #8b5cf6; font-size: 0.75rem; }

.chat-typing {
  display: inline-flex;
  gap: 0.22rem;
  padding: 0.15rem 0;
}

.chat-typing i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #8b5cf6;
  animation: chat-typing-bounce 1.2s infinite ease-in-out;
}

.chat-typing i:nth-child(2) {
  animation-delay: 0.15s;
}

.chat-typing i:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes chat-typing-bounce {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

@media (prefers-reduced-motion: reduce) {
  .chat-typing i {
    animation: none;
  }
}
