/* ==========================================================================
   VoiceIQ — /chat page layout (messenger). Loaded LAST, after voiceiq-minimal.css.
   Two columns filling the viewport: the conversation (header · messages · pinned
   composer) and a 280px rail (identity · members online · house rules).
   Tokens only — dark mode is handled by voiceiq-ui.css tokens.
   ========================================================================== */

body[data-page="chat"] {
  --chat-rail-w: 280px;
  --chat-gap: 16px;
  --chat-w: min(1240px, calc(100% - 64px));
}

/* --------------------------------------------------------------------------
   Shell — grid, viewport-high inside the sidebar shell
   -------------------------------------------------------------------------- */
/* Specificity note: voiceiq-minimal.css styles the shell as
   `html[data-private-page][data-authenticated] .chat-shell` (0,3,1) outside any media query,
   so the shell rules here carry the same weight and win by source order. */
html[data-private-page] body[data-page="chat"] .chat-shell,
body[data-page="chat"] .chat-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--chat-rail-w);
  gap: var(--chat-gap);
  align-items: stretch;
  width: var(--chat-w);
  height: auto;
  min-height: 0;
  margin: 24px auto 0;
  padding: 0;
}
@media (min-width: 1001px) {
  html[data-private-page][data-authenticated] body[data-page="chat"] .chat-shell {
    height: calc(100vh - 88px);
    min-height: 520px;
  }
}
body[data-page="chat"] .site-legal-footer {
  width: var(--chat-w);
  margin: 16px auto 0;
  padding: 12px 0 16px;
}

/* --------------------------------------------------------------------------
   Conversation column
   -------------------------------------------------------------------------- */
body[data-page="chat"] .chat-thread {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius-l) !important;
  background: var(--surface);
}
body[data-page="chat"] .chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex: 0 0 auto;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
body[data-page="chat"] .chat-head-title { display: flex; align-items: center; gap: 12px; min-width: 0; }
body[data-page="chat"] .chat-head-icon {
  flex: 0 0 auto; display: grid; place-items: center;
  width: 36px; height: 36px; border-radius: 10px;
  background: var(--ink); color: var(--ink-text);
}
body[data-page="chat"] .chat-head-icon svg { width: 18px; height: 18px; }
body[data-page="chat"] .chat-head-copy { min-width: 0; }
body[data-page="chat"] .chat-head h1 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: -.02em; line-height: 1.2; }
body[data-page="chat"] .chat-head small { display: block; margin-top: 2px; font-size: 12px; color: var(--muted); }
body[data-page="chat"] .live-pill {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 24px; padding: 0 9px 0 8px;
  font-size: 12px; font-weight: 500; letter-spacing: 0; text-transform: none;
  background: var(--signal-soft); color: var(--signal-text);
}
body[data-page="chat"] .live-pill::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--signal); box-shadow: none;
  animation: chat-pulse 2s var(--ease) infinite;
}

/* Messages surface */
body[data-page="chat"] .messages, body[data-page="chat"] #messages {
  flex: 1 1 auto;
  height: auto;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 20px 8px;
  background: var(--surface);
  scroll-behavior: smooth;
}
body[data-page="chat"] .message-row { display: flex; align-items: flex-start; gap: 10px; margin: 0 0 14px; }
body[data-page="chat"] .message-row:last-child { margin-bottom: 4px; }
body[data-page="chat"] .message-avatar {
  flex: 0 0 auto; width: 32px; height: 32px; margin-top: 1px; padding: 0; border: 0; border-radius: 999px; overflow: hidden;
  display: grid; place-items: center; background: var(--surface-2); color: var(--text);
  font-family: var(--font-body); font-size: 12.5px; font-weight: 600; cursor: pointer;
  transition: transform var(--t) var(--ease), box-shadow var(--t) var(--ease);
}
body[data-page="chat"] .message-avatar:hover { transform: translateY(-1px); box-shadow: var(--shadow-1); }
body[data-page="chat"] .message-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
body[data-page="chat"] .message-row.owner .message-avatar { background: var(--surface-3); color: var(--text); }
body[data-page="chat"] .message-row.admin .message-avatar { background: var(--ink); color: var(--ink-text); }
body[data-page="chat"] .message-main { min-width: 0; max-width: min(680px, 90%); }
body[data-page="chat"] .message-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; min-height: 18px; }
body[data-page="chat"] .message-name {
  padding: 0; border: 0; background-color: transparent; color: var(--text); font: inherit; font-size: 13.5px; font-weight: 600; letter-spacing: -.01em; cursor: pointer;
}
body[data-page="chat"] .message-name:hover { text-decoration: underline; text-underline-offset: 3px; }
body[data-page="chat"] .message-time { font-size: 12px; color: var(--faint); font-variant-numeric: tabular-nums; }
body[data-page="chat"] .message-delete {
  margin-left: 2px; padding: 0 6px; min-height: 20px; border: 0; border-radius: 6px; background: transparent;
  color: var(--faint); font-family: var(--font-body); font-size: 12px; font-weight: 500; letter-spacing: 0; text-transform: none; cursor: pointer;
  opacity: 0; transition: opacity var(--t) var(--ease), color var(--t) var(--ease), background var(--t) var(--ease);
}
body[data-page="chat"] .message-row:hover .message-delete, body[data-page="chat"] .message-delete:focus-visible { opacity: 1; }
body[data-page="chat"] .message-delete:hover { color: var(--danger); background: var(--danger-soft); }
body[data-page="chat"] .role-tag, body[data-page="chat"] .custom-chat-tag {
  min-height: 20px; padding: 0 7px; font-family: var(--font-body); font-size: 11.5px; font-weight: 500; letter-spacing: 0; text-transform: none;
}
body[data-page="chat"] .bubble {
  margin-top: 5px; padding: 9px 12px;
  border: 1px solid transparent; border-radius: 4px 12px 12px 12px;
  background: var(--surface-2); color: var(--text);
  font-size: 14px; line-height: 1.5; white-space: pre-wrap; overflow-wrap: anywhere;
}
body[data-page="chat"] .message-row.plus .bubble { background: var(--surface-2); border-color: var(--line); box-shadow: none; }
body[data-page="chat"] .message-row.owner .bubble { background: var(--surface-3); border-color: transparent; }
body[data-page="chat"] .message-row.admin .bubble { background: var(--surface); border-color: var(--line-strong); }

/* Empty + loading states */
body[data-page="chat"] .messages:has(> .chat-empty) { display: grid; place-items: center; }
body[data-page="chat"] .messages .chat-empty {
  position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  width: 100%; max-width: 360px; margin: 0; padding: 72px 20px 32px;
  border: 1px dashed var(--line-strong); border-radius: 12px;
  text-align: center; color: var(--muted); font-size: 13.5px; line-height: 1.5; background: transparent;
}
body[data-page="chat"] .messages .chat-empty::before {
  content: ""; position: absolute; top: 24px; left: 50%; width: 40px; height: 40px; margin-left: -20px;
  border-radius: 12px; background: var(--surface-2);
}
body[data-page="chat"] .messages .chat-empty::after {
  content: ""; position: absolute; top: 34px; left: 50%; width: 20px; height: 20px; margin-left: -10px;
  background: var(--text);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 12a8 8 0 0 1-11.6 7.1L4 21l1.9-5.4A8 8 0 1 1 21 12z'/%3E%3Cpath d='M8.5 11h7M8.5 14h4'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M21 12a8 8 0 0 1-11.6 7.1L4 21l1.9-5.4A8 8 0 1 1 21 12z'/%3E%3Cpath d='M8.5 11h7M8.5 14h4'/%3E%3C/svg%3E") center / contain no-repeat;
}
body[data-page="chat"] .messages .chat-empty-loading::after {
  width: 18px; height: 18px; margin-left: -9px; top: 35px;
  background: transparent; -webkit-mask: none; mask: none;
  border: 2px solid var(--line-strong); border-top-color: var(--text); border-radius: 50%;
  animation: chat-spin .9s linear infinite;
}

/* Composer — pinned to the bottom of the conversation column */
body[data-page="chat"] .composer {
  flex: 0 0 auto;
  padding: 12px 16px 14px;
  border-top: 1px solid var(--line);
  background: var(--surface);
}
body[data-page="chat"] .composer-box {
  padding: 6px 6px 6px 8px;
  border: 1px solid var(--line-strong);
  border-radius: 12px;
  background: var(--surface);
  transition: border-color var(--t) var(--ease), box-shadow var(--t) var(--ease);
}
body[data-page="chat"] .composer-box:focus-within { border-color: var(--text); box-shadow: var(--ring); }
body[data-page="chat"] .composer-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 8px; align-items: end; }
body[data-page="chat"] .composer textarea {
  display: block; width: 100%; height: 40px; min-height: 40px; max-height: 132px; resize: none;
  padding: 9px 6px; border: 0; border-radius: 6px !important; background: transparent; color: var(--text); outline: none;
  font: inherit; font-size: 14.5px; line-height: 1.5; box-shadow: none;
}
body[data-page="chat"] .composer textarea:focus { border: 0; box-shadow: none; background: transparent; }
body[data-page="chat"] .composer textarea::placeholder { color: var(--faint); }
body[data-page="chat"] .composer textarea:disabled { color: var(--faint); cursor: not-allowed; }
body[data-page="chat"] .composer .send-button {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-width: 0; min-height: 38px !important; padding: 0 12px 0 14px; border: 0;
  font-size: 13px !important; font-weight: 500; cursor: pointer;
}
body[data-page="chat"] .composer .send-button svg { width: 15px; height: 15px; }
body[data-page="chat"] .composer .send-button:disabled { opacity: .45; cursor: not-allowed; transform: none; }
body[data-page="chat"] .composer-foot {
  display: flex; align-items: center; gap: 12px;
  min-height: 20px; margin: 6px 0 0; padding: 0 2px 0 6px;
  font-size: 12px; color: var(--muted);
}
body[data-page="chat"] .chat-status { flex: 1 1 auto; min-width: 0; min-height: 0; font-size: 12.5px; color: var(--muted); }
body[data-page="chat"] .chat-status.error { color: var(--danger); }
body[data-page="chat"] .composer-hint { flex: 1 1 auto; color: var(--faint); white-space: nowrap; }
body[data-page="chat"] .chat-status:not(:empty) ~ .composer-hint { display: none; }
body[data-page="chat"] .composer-hint kbd {
  display: inline-block; min-width: 18px; padding: 0 5px; border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px;
  background: var(--surface-2); color: var(--muted); font: inherit; font-size: 11px; line-height: 16px; text-align: center;
}
body[data-page="chat"] .char-count { flex: 0 0 auto; margin-left: auto; font-size: 12px; color: var(--faint); font-variant-numeric: tabular-nums; }

/* --------------------------------------------------------------------------
   Rail — identity, members online, house rules
   -------------------------------------------------------------------------- */
body[data-page="chat"] .chat-rail { display: flex; flex-direction: column; gap: var(--chat-gap); min-width: 0; min-height: 0; }
body[data-page="chat"] .chat-rail .card { padding: 16px; }

body[data-page="chat"] .chat-me { flex: 0 0 auto; }
body[data-page="chat"] .chat-me-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
body[data-page="chat"] .chat-me-avatar { flex: 0 0 auto; width: 36px; height: 36px; background: var(--ink); color: var(--ink-text); }
body[data-page="chat"] .chat-me-avatar svg { width: 18px; height: 18px; }
body[data-page="chat"] .chat-me-name {
  display: block; min-width: 0; margin: 0;
  font-size: 15px; font-weight: 600; letter-spacing: -.02em; line-height: 1.25;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
body[data-page="chat"] #chatIdentityRole { color: var(--muted); font-size: 12px; font-weight: 500; letter-spacing: 0; }
body[data-page="chat"] .chat-me-hint { margin: 12px 0 0; font-size: 12.5px; line-height: 1.5; color: var(--muted); }
body[data-page="chat"] .chat-me-link {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 34px; margin-top: 12px; padding: 0 12px;
  font-size: 13px; font-weight: 500; text-decoration: none;
}
body[data-page="chat"] .chat-me-link svg { width: 15px; height: 15px; color: var(--muted); }

/* Members online — hosts the panel site-session.js injects (docked by page-chat.js) */
body[data-page="chat"] .chat-online-dock { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 200px; padding-bottom: 8px; }
body[data-page="chat"] .chat-online-head { margin-bottom: 8px; }
body[data-page="chat"] .chat-online-slot { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }
body[data-page="chat"] .chat-online-slot:empty { flex: 0 0 auto; align-items: center; justify-content: center; margin-bottom: 8px; padding: 28px 12px; border: 1px dashed var(--line-strong); border-radius: 10px; }
body[data-page="chat"] .chat-online-slot:empty::before {
  content: ""; width: 20px; height: 20px; margin-bottom: 8px; border: 2px solid var(--line-strong); border-top-color: var(--text); border-radius: 50%;
  animation: chat-spin .9s linear infinite;
}
body[data-page="chat"] .chat-online-slot:empty::after { content: "Checking who is online…"; font-size: 12.5px; color: var(--muted); text-align: center; }
body[data-page="chat"] .chat-online-slot .voiceiq-online-panel {
  position: static; inset: auto; width: auto; max-height: none; flex: 1 1 auto; min-height: 0;
  display: flex; flex-direction: column; overflow: hidden;
  border: 0; border-radius: 0; background: transparent; box-shadow: none;
  opacity: 1; pointer-events: auto; transform: none; transition: none;
}
body[data-page="chat"] .chat-online-slot .voiceiq-online-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; flex: 0 0 auto;
  padding: 0 0 10px; border-bottom: 1px solid var(--line);
}
body[data-page="chat"] .chat-online-slot .voiceiq-online-head strong { display: none; }
body[data-page="chat"] .chat-online-slot .voiceiq-online-head::before { content: "Active now"; font-size: 12.5px; font-weight: 450; color: var(--muted); }
body[data-page="chat"] .chat-online-slot .voiceiq-online-head span {
  display: inline-flex; align-items: center; justify-content: center; min-width: 24px; height: 20px; padding: 0 7px;
  border-radius: 6px !important; background: var(--signal-soft); color: var(--signal-text);
  font-family: var(--font-body); font-size: 12px; font-weight: 500; letter-spacing: 0;
}
body[data-page="chat"] .chat-online-slot .voiceiq-online-list { flex: 1 1 auto; min-height: 0; max-height: none; overflow-y: auto; margin: 0 -6px; padding: 8px 6px 0; }
body[data-page="chat"] .chat-online-slot .voiceiq-online-user {
  width: 100%; display: grid; grid-template-columns: 30px minmax(0, 1fr); gap: 10px; align-items: center;
  min-height: 0; padding: 6px; border: 0; border-radius: 8px !important; background: transparent; color: var(--text);
  font: inherit; text-align: left; cursor: pointer; transition: background var(--t) var(--ease);
}
body[data-page="chat"] .chat-online-slot .voiceiq-online-user:hover { background: var(--surface-2); }
body[data-page="chat"] .chat-online-slot .voiceiq-online-avatar {
  display: grid; place-items: center; width: 30px; height: 30px; border-radius: 999px; overflow: hidden;
  background: var(--surface-2); color: var(--text); font-family: var(--font-body); font-size: 12px; font-weight: 600; line-height: 1;
}
body[data-page="chat"] .chat-online-slot .voiceiq-online-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
body[data-page="chat"] .chat-online-slot .voiceiq-online-user.owner .voiceiq-online-avatar { background: var(--surface-3); color: var(--text); }
body[data-page="chat"] .chat-online-slot .voiceiq-online-user.admin .voiceiq-online-avatar { background: var(--ink); color: var(--ink-text); }
body[data-page="chat"] .chat-online-slot .voiceiq-online-copy { min-width: 0; display: block; }
body[data-page="chat"] .chat-online-slot .voiceiq-online-name { display: flex; align-items: center; gap: 6px; min-width: 0; }
body[data-page="chat"] .chat-online-slot .voiceiq-online-name strong { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; font-weight: 500; line-height: 1.25; color: var(--text); }
body[data-page="chat"] .chat-online-slot .voiceiq-online-role {
  flex: 0 0 auto; display: inline-flex; align-items: center; min-height: 18px; padding: 0 6px; border-radius: 5px !important;
  font-family: var(--font-body); font-size: 10.5px; font-weight: 500; letter-spacing: 0; text-transform: none; background: var(--surface-2); color: var(--muted);
}
body[data-page="chat"] .chat-online-slot .voiceiq-online-role.owner { background: var(--surface-3); color: var(--text); }
body[data-page="chat"] .chat-online-slot .voiceiq-online-role.admin { background: var(--ink); color: var(--ink-text); }
body[data-page="chat"] .chat-online-slot .voiceiq-online-tag {
  display: inline-flex; align-items: center; max-width: 150px; min-height: 18px; margin-top: 3px; padding: 0 6px; border-radius: 5px !important;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: var(--font-body); font-size: 11px; font-weight: 500; letter-spacing: 0; text-transform: none;
}
body[data-page="chat"] .chat-online-slot .voiceiq-online-empty {
  margin-top: 8px; padding: 28px 12px; border: 1px dashed var(--line-strong); border-radius: 10px;
  font-family: var(--font-body); font-size: 12.5px; font-weight: 450; color: var(--muted);
}
body[data-page="chat"] #voiceiqOnlineToggle[hidden],
body[data-page="chat"]:has(.chat-online-slot > #voiceiqOnlinePanel) #voiceiqOnlineToggle { display: none !important; }

/* House rules */
body[data-page="chat"] .chat-guidelines { flex: 0 0 auto; }
body[data-page="chat"] .chat-guidelines ul { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 8px; }
body[data-page="chat"] .chat-guidelines li { display: flex; align-items: flex-start; gap: 8px; font-size: 12.5px; line-height: 1.45; color: var(--muted); }
body[data-page="chat"] .chat-guidelines svg { flex: 0 0 auto; width: 15px; height: 15px; margin-top: 2px; color: var(--text); }

/* --------------------------------------------------------------------------
   Profile dialog polish
   -------------------------------------------------------------------------- */
body[data-page="chat"] .profile-modal { position: relative; padding: 24px; }
body[data-page="chat"] .profile-close { position: absolute; top: 16px; right: 16px; float: none; width: 32px; height: 32px; font-size: 18px; }
body[data-page="chat"] .profile-top { padding-right: 40px; }
body[data-page="chat"] .profile-avatar { width: 64px; height: 64px; font-size: 24px; }
body[data-page="chat"] .profile-copy h2 { margin: 4px 0; font-size: 20px; }
body[data-page="chat"] .profile-stats { gap: 8px; margin-top: 18px; grid-template-columns: repeat(3, minmax(0, 1fr)); }
body[data-page="chat"] #viewProfileId { font-family: var(--font-mono); font-variant-numeric: tabular-nums; letter-spacing: .02em; white-space: nowrap; }
@media (max-width: 480px) { body[data-page="chat"] .profile-stats { grid-template-columns: 1fr 1fr; } body[data-page="chat"] .profile-stats .profile-stat:last-child { grid-column: 1 / -1; } }
body[data-page="chat"] .profile-actions { gap: 8px; margin-top: 16px; padding-top: 16px; }

/* --------------------------------------------------------------------------
   Motion
   -------------------------------------------------------------------------- */
@keyframes chat-pulse { 0%, 100% { box-shadow: 0 0 0 0 var(--signal-soft); } 50% { box-shadow: 0 0 0 4px var(--signal-soft); } }
@keyframes chat-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  body[data-page="chat"] .live-pill::before, body[data-page="chat"] .chat-empty-loading::after, body[data-page="chat"] .chat-online-slot:empty::before { animation: none; }
  body[data-page="chat"] .message-avatar, body[data-page="chat"] .composer-box, body[data-page="chat"] .message-delete { transition: none; }
  body[data-page="chat"] .messages { scroll-behavior: auto; }
}

/* --------------------------------------------------------------------------
   Responsive — pill nav (≤ 1000px) and phones
   -------------------------------------------------------------------------- */
@media (max-width: 1000px) {
  body[data-page="chat"] { --chat-w: calc(100% - 32px); }
  html[data-private-page] body[data-page="chat"] .chat-shell, body[data-page="chat"] .chat-shell { width: var(--chat-w); height: auto; min-height: 0; margin-top: 20px; }
  html[data-private-page] body[data-page="chat"] .chat-thread, body[data-page="chat"] .chat-thread { flex: 0 0 auto; height: min(72vh, 720px); min-height: 460px; }
}
@media (max-width: 860px) {
  html[data-private-page] body[data-page="chat"] .chat-shell, body[data-page="chat"] .chat-shell { grid-template-columns: 1fr; }
  body[data-page="chat"] .chat-rail { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: stretch; }
  body[data-page="chat"] .chat-online-dock { grid-column: 1 / -1; min-height: 0; max-height: 380px; }
}
@media (max-width: 640px) {
  html[data-private-page] body[data-page="chat"] .chat-thread, body[data-page="chat"] .chat-thread { height: min(68vh, 640px); min-height: 420px; }
  body[data-page="chat"] .chat-rail { grid-template-columns: 1fr; }
  body[data-page="chat"] .chat-head { padding: 12px 14px; }
  body[data-page="chat"] .messages { padding: 14px 14px 8px; }
  body[data-page="chat"] .message-main { max-width: calc(100% - 42px); }
  body[data-page="chat"] .message-delete { opacity: 1; }
  body[data-page="chat"] .composer { padding: 10px 12px 12px; }
  body[data-page="chat"] .composer-row { grid-template-columns: minmax(0, 1fr) auto; flex-direction: row; align-items: end; }
  body[data-page="chat"] .composer .send-button span { display: none; }
  body[data-page="chat"] .composer .send-button { width: 38px; padding: 0; }
  body[data-page="chat"] .composer .send-button svg { width: 16px; height: 16px; }
  body[data-page="chat"] .composer-hint { display: none; }
  body[data-page="chat"] .site-legal-footer { padding: 12px 0 20px; }
}
