/* ======================================================
   Messages modal — Air-Plan v11
   ====================================================== */

/* ── Modal shell ─────────────────────────────────────── */
.modal-message {
  width: min(920px, calc(100vw - 24px));
  height: min(80vh, 720px);
  max-height: calc(var(--app-vh, 1vh) * 100 - 32px);
  overflow: hidden;
}

.modal-message .modal-message-layout {
  display: grid;
  /* Fixed 260px thread list; conversation pane gets the rest */
  grid-template-columns: 260px minmax(0, 1fr);
  height: 100%;
  min-height: 0;
  padding: 0;
  overflow: hidden;
  border-bottom-left-radius: inherit;
  border-bottom-right-radius: inherit;
}

/* ── Thread list pane ─────────────────────────────────── */
.modal-message .modal-threads {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface-2) 45%, transparent);
}

.modal-message .modal-threads-head {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 10px 6px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-message .modal-search {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 8px;
  background: color-mix(in srgb, var(--surface) 80%, white 20%);
  border: 1px solid var(--border);
}
.modal-message .modal-search .icon {
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
}
.modal-message .modal-search .input {
  flex: 1 1 auto;
  width: 100%;
  min-height: 28px;
  height: 28px;
  margin: 0; padding: 0;
  border: none !important;
  background: transparent !important;
  box-shadow: none !important;
  font-size: 12px;
}

/* Filter pills row */
.modal-message .thread-filters {
  display: flex;
  gap: 4px;
}
.modal-message .filter-pill {
  padding: 4px 10px;
  border-radius: 99px;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  background: transparent;
  border: 1px solid transparent;
  cursor: pointer;
  transition: color .13s, background .13s, border-color .13s;
}
.modal-message .filter-pill:hover { color: var(--text); }
.modal-message .filter-pill.active {
  color: var(--color-primary);
  background: color-mix(in srgb, var(--color-primary) 10%, transparent);
  border-color: color-mix(in srgb, var(--color-primary) 35%, transparent);
}

/* Action buttons row */
.modal-message .thread-actions {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}
.modal-message .thread-actions .btn {
  height: 28px;
  font-size: 11px;
  padding-inline: 9px;
}

/* Thread list scroll area */
.modal-message .thread-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 6px;
}

/* ── Thread items ─────────────────────────────────────── */
.modal-message .thread-item {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  cursor: pointer;
  transition: background .15s, border-color .15s;
  margin-bottom: 2px;
}
.modal-message .thread-item:hover {
  background: color-mix(in srgb, var(--surface) 80%, white 20%);
  border-color: color-mix(in srgb, var(--border) 70%, transparent);
}
.modal-message .thread-item.active {
  background: color-mix(in srgb, var(--color-primary) 10%, transparent);
  border-color: color-mix(in srgb, var(--color-primary) 30%, transparent);
}

.modal-message .thread-avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--color-primary) 20%, transparent);
  color: var(--color-primary);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700;
  flex-shrink: 0;
  background-size: cover; background-position: center;
}
.modal-message .thread-avatar.has-image { color: transparent; }

.modal-message .thread-body {
  flex: 1; min-width: 0;
}
.modal-message .thread-title-row {
  display: flex; align-items: center; gap: 5px; margin-bottom: 2px;
}
.modal-message .thread-title {
  font-size: 12px; font-weight: 700; line-height: 1.25;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  flex: 1; min-width: 0;
}
.modal-message .thread-type-chip {
  font-size: 9px; font-weight: 700; letter-spacing: .06em;
  text-transform: uppercase;
  padding: 1px 5px; border-radius: 4px;
  background: color-mix(in srgb, var(--muted) 15%, transparent);
  color: var(--muted); flex-shrink: 0;
}
.modal-message .thread-preview {
  font-size: 11px; color: var(--muted); line-height: 1.35;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.modal-message .thread-meta {
  display: flex; flex-direction: column; align-items: flex-end; gap: 3px;
  flex-shrink: 0; padding-top: 1px;
}
.modal-message .thread-time {
  font-size: 10px; color: var(--muted); white-space: nowrap;
}
.modal-message .thread-badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 18px; height: 18px; padding: 0 4px;
  border-radius: 99px;
  background: var(--color-primary);
  color: #fff;
  font-size: 9px; font-weight: 700;
}

/* Load more button */
.modal-message .thread-load-more {
  display: block; width: 100%;
  padding: 8px; margin-top: 4px;
  border-radius: 8px; border: 1px dashed var(--border);
  background: transparent; color: var(--muted);
  font-size: 12px; font-weight: 600;
  cursor: pointer; text-align: center;
  transition: color .13s, border-color .13s;
}
.modal-message .thread-load-more:hover { color: var(--text); border-color: var(--text); }

/* Empty state */
.modal-message .thread-empty {
  display: flex; flex-direction: column; align-items: center;
  gap: 8px; padding: 32px 16px; color: var(--muted); text-align: center;
}
.modal-message .thread-empty i { font-size: 28px; opacity: .4; }
.modal-message .thread-empty p { font-size: 12px; margin: 0; }

/* ── Conversation pane ───────────────────────────────── */
.modal-message .modal-thread-view {
  display: flex;
  flex-direction: column;
  gap: 0;
  min-width: 0; min-height: 0;
  overflow: hidden;
}

/* Chat header */
.modal-message .chat-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-message .chat-header-left {
  display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1;
}
.modal-message .chat-header-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: color-mix(in srgb, var(--color-primary) 20%, transparent);
  color: var(--color-primary);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; flex-shrink: 0;
  background-size: cover; background-position: center;
}
.modal-message .chat-header-avatar.has-image { color: transparent; }
.modal-message .chat-header h3 {
  font-size: 14px; font-weight: 700; line-height: 1.2;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin: 0;
}

/* Thread menu */
.modal-message .chat-header-actions { position: relative; }
.modal-message .thread-menu-btn {
  width: 32px; height: 32px; border-radius: 8px;
  border: 1px solid transparent;
  background: transparent; cursor: pointer; color: var(--muted);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  transition: color .13s, background .13s, border-color .13s;
}
.modal-message .thread-menu-btn:hover {
  color: var(--text);
  background: color-mix(in srgb, var(--surface-2) 60%, transparent);
  border-color: var(--border);
}
.modal-message .thread-menu-dropdown {
  position: absolute; top: calc(100% + 4px); right: 0; z-index: 50;
  min-width: 160px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-md);
  overflow: hidden;
}
.modal-message .thread-menu-dropdown.open { display: block; }
.modal-message .thread-menu-item {
  display: block; width: 100%;
  padding: 9px 14px;
  text-align: left; background: none; border: none; cursor: pointer;
  font-size: 13px; color: var(--text);
  transition: background .12s;
}
.modal-message .thread-menu-item:hover { background: color-mix(in srgb, var(--surface-2) 70%, transparent); }

/* Chat thread scroll area */
.modal-message .chat-thread {
  flex: 1; width: 100%; min-height: 0;
  overflow-y: auto; overflow-x: hidden;
  padding: 12px 0 16px;
  background-color: var(--chat-thread-bg, var(--surface));
  background-image:
    radial-gradient(var(--chat-dot-light, rgba(255,255,255,.06)) 1px, transparent 1px),
    radial-gradient(var(--chat-dot-dark, rgba(0,0,0,.04)) 1px, transparent 1px);
  background-size: 18px 18px, 28px 28px;
  background-position: 0 0, 9px 9px;
}

/* ── Chat bubbles ─────────────────────────────────────── */
.modal-message .chat-messages { display: flex; flex-direction: column; gap: 2px; }

.modal-message .chat-bubble {
  max-width: 72%;
  margin: 2px 14px 4px;
  padding: 9px 12px;
  border-radius: 14px;
  line-height: 1.45;
}
.modal-message .chat-bubble.incoming {
  align-self: flex-start;
  background: color-mix(in srgb, var(--surface-2) 85%, white 15%);
  border-bottom-left-radius: 4px;
}
.modal-message .chat-bubble.outgoing {
  align-self: flex-end;
  background: var(--color-primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.modal-message .chat-text { font-size: 13px; white-space: pre-wrap; word-break: break-word; }
.modal-message .chat-meta {
  margin-top: 5px; font-size: 10px; font-weight: 500; opacity: .65;
}
.modal-message .chat-bubble.outgoing .chat-meta { color: rgba(255,255,255,.85); opacity: 1; }
.modal-message .chat-bubble.incoming .chat-meta { color: var(--muted); }

/* System messages — centred label, no bubble */
.modal-message .chat-system {
  align-self: center;
  font-size: 11px; color: var(--muted); font-style: italic;
  padding: 2px 12px;
  margin: 6px 0;
  display: flex; align-items: center; gap: 8px;
}
.modal-message .chat-system::before,
.modal-message .chat-system::after {
  content: '';
  flex: 1;
  height: 1px;
  background: color-mix(in srgb, var(--border) 60%, transparent);
}

/* Attachments */
.modal-message .chat-attachments { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 7px; }
.modal-message .attachment-link {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 10px; border-radius: 8px;
  font-size: 11px; font-weight: 600; text-decoration: none;
  max-width: 200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
  background: rgba(255,255,255,.18); color: inherit;
}
.modal-message .chat-bubble.incoming .attachment-link {
  background: color-mix(in srgb, var(--color-primary) 12%, transparent);
  color: var(--color-primary);
}
.modal-message .chat-image {
  max-width: 200px; max-height: 160px;
  border-radius: 8px; display: block;
}

/* ── No-thread-selected placeholder ─────────────────── */
.modal-message .chat-placeholder {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 10px; color: var(--muted); padding: 32px;
}
.modal-message .chat-placeholder i { font-size: 40px; opacity: .3; }
.modal-message .chat-placeholder p { font-size: 13px; margin: 0; text-align: center; }

/* ── Compose bar ─────────────────────────────────────── */
.modal-message .modal-compose { flex-shrink: 0; padding: 10px 12px; border-top: 1px solid var(--border); }

.modal-message .chat-compose-bar {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 88%, white 12%);
  transition: border-color .15s;
}
.modal-message .chat-compose-bar:focus-within {
  border-color: color-mix(in srgb, var(--color-primary) 50%, var(--border));
}

.modal-message .chat-attach-btn {
  width: 30px; height: 30px; border-radius: 8px;
  background: transparent; border: none; cursor: pointer;
  color: var(--muted); font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0; transition: color .13s;
}
.modal-message .chat-attach-btn:hover { color: var(--text); }

.modal-message .chat-input-inline {
  flex: 1 1 auto; min-width: 0;
  min-height: 22px !important; max-height: 66px;
  height: auto !important;
  line-height: 1.45;
  margin: 0; padding: 0;
  border: none !important; background: transparent !important;
  box-shadow: none !important; resize: none;
  font-size: 13px; overflow-y: auto;
  scrollbar-width: thin;
}

.modal-message .compose-right {
  display: flex; align-items: center; gap: 5px; flex-shrink: 0;
}
.modal-message .chat-char-count {
  font-size: 10px; color: var(--muted);
  transition: color .15s;
}
.modal-message .chat-char-count.is-warn { color: var(--color-warning); }
.modal-message .chat-char-count.is-over { color: var(--color-danger); font-weight: 700; }

.modal-message .chat-send-btn {
  width: 32px; height: 32px; border-radius: 10px;
  background: var(--color-primary);
  border: none; cursor: pointer; color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; flex-shrink: 0;
  transition: opacity .13s, transform .1s;
}
.modal-message .chat-send-btn:hover:not(:disabled) { opacity: .88; transform: scale(1.04); }
.modal-message .chat-send-btn:disabled { opacity: .35; cursor: not-allowed; transform: none; }

/* Attachment preview chips below compose */
.modal-message .compose-attachments {
  display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px;
}
.modal-message .compose-attach-chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 8px; border-radius: 99px; font-size: 11px; font-weight: 600;
  border: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface-2) 70%, transparent);
  color: var(--text); max-width: 180px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
.modal-message .compose-attach-chip button {
  background: none; border: none; padding: 0; cursor: pointer;
  font-size: 12px; color: var(--muted); line-height: 1; flex-shrink: 0;
}
.modal-message .compose-attach-chip button:hover { color: var(--color-danger); }

.modal-message .compose-attach-hint {
  font-size: 10px; color: var(--muted);
  align-self: center; white-space: nowrap;
}

/* ── Sub-modals ──────────────────────────────────────── */
.modal-sub { display: none; }
.modal-sub[aria-hidden="false"] { display: flex; }

.modal-sub-card {
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  width: min(520px, calc(100vw - 24px));
  max-height: calc(var(--app-vh, 1vh) * 100 - 40px);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.modal-sub-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface-2) 50%, transparent);
  flex-shrink: 0;
}
.modal-sub-head h4 { font-size: 14px; font-weight: 700; margin: 0; }

.modal-sub-body {
  padding: 12px;
  overflow-y: auto;
  flex: 1; min-height: 0;
  display: flex; flex-direction: column; gap: 10px;
}

/* Forms inside sub-modals use the form standard (forms.css, forms wave 2):
   the old msg-form-card / msg-field rules are retired. The card takes the
   page background so the section cards keep their contrast
   (form-standard.md, "Modals"); the footer holds the .ap-form action row. */
.modal-sub-card:has(> .modal-sub-body.ap-form) { background: var(--color-bg, var(--bg)); }
.modal-sub-head__title { display: flex; align-items: center; gap: 10px; }
.modal-sub-footer {
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-sub-footer.ap-form .ap-form-actions--sticky { padding: 0; border-top: 0; background: none; }

/* Member list inside sub-modals */
.modal-sub-body .group-member-list {
  max-height: 220px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

/* Reset browser defaults on both <button> and <label> member items */
.modal-sub-body .group-member-item {
  appearance: none;
  -webkit-appearance: none;
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
  background: color-mix(in srgb, var(--surface-2) 50%, transparent);
  cursor: pointer;
  text-align: left;
  transition: background .13s, border-color .13s;
  font-family: inherit;
}
.modal-sub-body .group-member-item:hover {
  background: color-mix(in srgb, var(--color-primary) 8%, transparent);
  border-color: color-mix(in srgb, var(--color-primary) 30%, transparent);
}
.modal-sub-body .group-member-item:has(input:checked) {
  background: color-mix(in srgb, var(--color-primary) 8%, transparent);
  border-color: color-mix(in srgb, var(--color-primary) 30%, transparent);
}
.modal-sub-body .group-member-item input[type="checkbox"] {
  flex-shrink: 0;
  cursor: pointer;
}

/* Name + email typography */
.group-member-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
}
.group-member-email {
  font-size: 11px;
  color: var(--muted);
  margin-top: 1px;
  line-height: 1.3;
}

/* ── File input — style the native picker button ─────── */
.modal-sub-body input[type="file"].input {
  padding: 5px 8px;
  cursor: pointer;
  height: auto;
  min-height: 0;
}
.modal-sub-body input[type="file"].input::file-selector-button {
  background: color-mix(in srgb, var(--color-primary) 10%, transparent);
  color: var(--color-primary);
  border: 1px solid color-mix(in srgb, var(--color-primary) 35%, transparent);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .02em;
  cursor: pointer;
  margin-right: 8px;
  transition: background .13s;
}
.modal-sub-body input[type="file"].input::file-selector-button:hover {
  background: color-mix(in srgb, var(--color-primary) 18%, transparent);
}

.modal-sub-actions {
  display: flex; align-items: center; justify-content: flex-end; gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-sub-body .hint { font-size: 11px; }

/* Broadcast warning */
.modal-sub-body .broadcast-warning {
  padding: 8px 10px; border-radius: 7px; font-size: 11px;
  border: 1px solid color-mix(in srgb, var(--color-warning) 40%, transparent);
  background: color-mix(in srgb, var(--color-warning) 8%, transparent);
  color: var(--text);
}

/* ── Result / error line ─────────────────────────────── */
.modal-message #messageModalResult,
.modal-sub .hint[id$="Result"] {
  font-size: 11px; min-height: 0;
}

/* ── Mobile ──────────────────────────────────────────── */
@media (max-width: 640px) {
  .modal-message {
    width: calc(100vw - 12px);
    height: calc(var(--app-vh, 1vh) * 100 - 16px);
    max-height: calc(var(--app-vh, 1vh) * 100 - 16px);
  }
  .modal-message .modal-message-layout {
    grid-template-columns: 1fr;
  }
  .modal-message .modal-thread-view { display: none; }
  .modal-message .modal-message-layout[data-view="thread"] .modal-threads { display: none; }
  .modal-message .modal-message-layout[data-view="thread"] .modal-thread-view { display: flex; }
}

/* ── System messages (3h) ─────────────────────────────── */
.chat-system {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 2px 16px;
  font-size: 11px;
  color: var(--muted);
  margin: 6px 0;
}
.chat-system::before,
.chat-system::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
  opacity: 0.6;
}
