/* Messages page (renderer/js/messages.js) — LMS tokens only, so both themes
   and the density setting apply. Two panes on wide screens; one at a time
   (list, or the open conversation) on phones. */

/* `hidden` must win over .btn / .tab-btn display rules (role-gated buttons). */
#page-messages [hidden], #modal-msg [hidden] { display: none !important; }

.msg-page-title { flex: 1; min-width: 0; }
.msg-page-actions { display: flex; gap: var(--space-2); flex-wrap: wrap; }

/* Sidebar unread count */
.nav-count {
  margin-left: auto; min-width: 20px; height: 20px; padding: 0 6px;
  border-radius: 10px; background: var(--color-brand); color: var(--color-on-brand);
  font-size: 11px; font-weight: 600; line-height: 20px; text-align: center;
  font-variant-numeric: tabular-nums;
}
.nav-count[hidden] { display: none; }
.nav-item[data-page="messages"] { position: relative; }
#app.sidebar-collapsed .nav-count { position: absolute; top: 2px; right: 4px; margin: 0; }

.msg-layout {
  display: grid; grid-template-columns: minmax(260px, 340px) minmax(0, 1fr);
  height: calc(100vh - var(--topbar-h) - 210px); height: calc(100dvh - var(--topbar-h) - 210px);
  min-height: 460px;
  border: 1px solid var(--color-border); border-radius: var(--radius-ui);
  background: var(--color-surface); overflow: hidden;
}
.msg-layout[hidden], #msg-manage[hidden] { display: none; }

/* ── Conversation list ── */
.msg-list-pane { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--color-border); }
.msg-filter { margin: var(--space-3); width: auto; }
.msg-conv-list { list-style: none; margin: 0; padding: 0 0 var(--space-2); overflow-y: auto; flex: 1; }
.msg-conv {
  display: flex; gap: var(--space-3); align-items: flex-start; width: 100%; text-align: left;
  padding: 10px var(--space-3); background: none; border: 0; border-left: 3px solid transparent;
  cursor: pointer; color: var(--color-text); font: inherit;
}
.msg-conv:hover { background: var(--color-surface-hover); }
.msg-conv.active { background: var(--color-brand-subtle); border-left-color: var(--color-brand); }
.msg-conv:focus-visible { outline: 2px solid var(--color-focus); outline-offset: -2px; }
.msg-conv-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.msg-conv-top, .msg-conv-bottom { display: flex; align-items: center; gap: 6px; min-width: 0; }
.msg-conv-title { flex: 1; min-width: 0; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-conv.unread .msg-conv-title { font-weight: 700; }
.msg-conv-top .material-symbols-outlined { font-size: 16px; color: var(--color-text-secondary); }
.msg-conv-time { flex-shrink: 0; font-size: var(--text-xs); color: var(--color-text-secondary); }
.msg-conv-preview { flex: 1; min-width: 0; font-size: var(--text-sm); color: var(--color-text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-conv.unread .msg-conv-preview { color: var(--color-text); }
.msg-unread {
  flex-shrink: 0; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--color-brand); color: var(--color-on-brand);
  font-size: 11px; font-weight: 600; line-height: 20px; text-align: center;
}
.msg-role { font-size: var(--text-xs); color: var(--color-text-secondary); }

.msg-avatar {
  flex-shrink: 0; width: 38px; height: 38px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center; overflow: hidden;
  background: var(--color-surface-muted); color: var(--color-text); font-weight: 600;
  border: 1px solid var(--color-border);
}
.msg-avatar-group { border-radius: var(--radius-ui); color: var(--color-brand); }
.msg-avatar .material-symbols-outlined { font-size: 20px; }

.msg-empty { list-style: none; padding: var(--space-5) var(--space-3); text-align: center; color: var(--color-text-secondary); font-size: var(--text-sm); }

/* ── Thread ── */
.msg-thread-pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.msg-placeholder {
  margin: auto; display: flex; flex-direction: column; align-items: center; gap: var(--space-2);
  padding: var(--space-5); text-align: center; color: var(--color-text-secondary);
}
.msg-placeholder .material-symbols-outlined { font-size: 40px; }
.msg-thread-head {
  display: flex; align-items: center; gap: var(--space-3);
  padding: 10px var(--space-4); border-bottom: 1px solid var(--color-border);
}
.msg-thread-title { flex: 1; min-width: 0; }
.msg-thread-title h3 { margin: 0; font-size: var(--text-base); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg-thread-sub { font-size: var(--text-xs); color: var(--color-text-secondary); }
.msg-back { display: none; }
.msg-viewing { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.msg-viewing .material-symbols-outlined { font-size: 15px; }

.msg-messages {
  list-style: none; margin: 0; padding: var(--space-4); flex: 1; overflow-y: auto;
  display: flex; flex-direction: column; gap: 2px; background: var(--color-canvas);
}
.msg-older { text-align: center; margin-bottom: var(--space-2); }
.msg-day { display: flex; justify-content: center; margin: var(--space-3) 0 var(--space-2); }
.msg-day span {
  font-size: var(--text-xs); color: var(--color-text-secondary); background: var(--color-surface);
  border: 1px solid var(--color-border); border-radius: 12px; padding: 2px 10px;
}
.msg-bubble-row { display: flex; margin-top: 8px; }
.msg-bubble-row.grouped { margin-top: 2px; }
.msg-bubble-row.mine { justify-content: flex-end; }
.msg-bubble {
  max-width: min(78%, 620px); padding: 8px 12px 6px; border-radius: 14px 14px 14px 4px;
  background: var(--color-surface); border: 1px solid var(--color-border); color: var(--color-text);
  display: flex; flex-direction: column; gap: 2px;
}
.msg-bubble-row.mine .msg-bubble {
  border-radius: 14px 14px 4px 14px; background: var(--color-brand); border-color: var(--color-brand); color: var(--color-on-brand);
}
.msg-sender { font-size: var(--text-xs); font-weight: 600; color: var(--color-brand); }
.msg-sender-role { font-weight: 500; color: var(--color-text-secondary); }
.msg-text { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.45; }
.msg-meta { display: flex; align-items: center; justify-content: flex-end; gap: 6px; font-size: 11px; opacity: 0.75; }
.msg-remove {
  background: none; border: 0; padding: 2px; color: inherit; cursor: pointer; opacity: 0;
  display: inline-flex; border-radius: var(--radius-sm);
}
.msg-remove .material-symbols-outlined { font-size: 15px; }
.msg-bubble:hover .msg-remove, .msg-remove:focus-visible { opacity: 1; }
@media (hover: none) { .msg-remove { opacity: 1; } }
.msg-bubble.is-removed { background: transparent; border-style: dashed; color: var(--color-text-secondary); }
.msg-bubble-row.mine .msg-bubble.is-removed { background: transparent; border-color: var(--color-border); color: var(--color-text-secondary); }
.msg-removed { display: inline-flex; align-items: center; gap: 4px; font-style: italic; font-size: var(--text-sm); }
.msg-removed .material-symbols-outlined { font-size: 15px; }
.msg-removed-text { font-style: normal; text-decoration: line-through; }

.msg-composer { display: flex; align-items: flex-end; gap: var(--space-2); padding: var(--space-3) var(--space-4) 4px; border-top: 1px solid var(--color-border); }
.msg-composer textarea { flex: 1; resize: none; min-height: var(--control-h); max-height: 160px; line-height: 1.4; }
.msg-composer .btn { flex-shrink: 0; height: var(--control-h); }
.msg-safeguard, .msg-cannot-post {
  display: flex; align-items: center; gap: 6px; margin: 0; padding: 4px var(--space-4) var(--space-3);
  font-size: var(--text-xs); color: var(--color-text-secondary);
}
.msg-safeguard .material-symbols-outlined, .msg-cannot-post .material-symbols-outlined { font-size: 15px; }
.msg-cannot-post { border-top: 1px solid var(--color-border); padding-top: var(--space-3); font-size: var(--text-sm); }

/* ── Dialog contents ── */
.msg-people, .msg-members { list-style: none; margin: 0; padding: 0; max-height: 320px; overflow-y: auto; }
.msg-person {
  display: flex; align-items: center; gap: var(--space-3); width: 100%; padding: 8px 4px;
  background: none; border: 0; border-bottom: 1px solid var(--color-border); text-align: left;
  color: var(--color-text); font: inherit; cursor: pointer;
}
.msg-person:hover { background: var(--color-surface-hover); }
.msg-person.chosen { background: var(--color-brand-subtle); }
.msg-person-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.msg-check { color: var(--color-brand); display: inline-flex; }
.msg-member { display: flex; align-items: center; gap: var(--space-3); padding: 8px 0; border-bottom: 1px solid var(--color-border); flex-wrap: wrap; }
.msg-member-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.msg-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: var(--space-2); }
.msg-chips .chip {
  display: inline-flex; align-items: center; gap: 4px; min-height: 28px; padding: 0 6px 0 10px;
  border-radius: 14px; border: 1px solid var(--color-border); background: var(--color-brand-subtle);
  color: var(--color-text); font: inherit; font-size: var(--text-sm); cursor: pointer;
}
.msg-chips .chip .material-symbols-outlined { font-size: 16px; }
.msg-fieldset { border: 0; padding: 0; margin: 0 0 var(--space-4); }
.msg-fieldset legend { font-weight: 500; margin-bottom: 6px; }
.msg-radio { display: flex; align-items: center; gap: 8px; font-weight: 400; margin: 4px 0; }
.msg-radio input { width: auto; }
.msg-section-head { display: flex; align-items: center; justify-content: space-between; margin: var(--space-4) 0 var(--space-2); }
.msg-section-head h4 { margin: 0; }
.btn-danger-text { color: var(--color-danger); }
.link-btn { background: none; border: 0; padding: 0; color: var(--color-brand); font: inherit; font-weight: 500; text-align: left; cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }

/* ── Admin manage table ── */
#msg-manage .toolbar { display: flex; gap: var(--space-2); flex-wrap: wrap; align-items: center; margin-bottom: var(--space-3); }
#msg-manage .toolbar input[type="search"] { flex: 1; min-width: 200px; }
.msg-manage-actions { white-space: nowrap; text-align: right; }

/* ── Phones and tablets: one pane at a time ── */
@media (max-width: 768px) {
  .msg-layout {
    grid-template-columns: minmax(0, 1fr);
    height: calc(100dvh - var(--topbar-h) - 190px);
    min-height: 420px;
  }
  .msg-thread-pane { display: none; }
  .msg-layout.show-thread .msg-list-pane { display: none; }
  .msg-layout.show-thread .msg-thread-pane { display: flex; }
  .msg-list-pane { border-right: 0; }
  .msg-back { display: inline-flex; }
  .msg-bubble { max-width: 88%; }
  .msg-page-actions { width: 100%; }
  .msg-page-actions .btn { flex: 1 1 auto; justify-content: center; }
  .msg-viewing { display: none; }
  .msg-manage-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .msg-manage-table, .msg-manage-table tbody, .msg-manage-table tr, .msg-manage-table td { display: block; width: 100%; }
  .msg-manage-table tr { padding: var(--space-3) 0; border-bottom: 1px solid var(--color-border); }
  .msg-manage-table td { border: 0; padding: 2px 0; }
  .msg-manage-table td[data-label]::before { content: attr(data-label) ": "; font-weight: 600; color: var(--color-text-secondary); font-size: var(--text-xs); }
  .msg-manage-actions { text-align: left; white-space: normal; }
}
