/* Chat tab ("Table talk"). Markup comes from js/views/chat.js.
   The chat fills the space between the header and the bottom nav (or the
   keyboard); js sets the exact height, the calc() below is the fallback. */

.app-main[data-route="chat"]:has(> .chat) { display: block; padding: 0; max-width: 860px; }
@media (min-width: 720px) {
  .app-main[data-route="chat"]:has(> .chat) { padding: 16px 16px 0; }
}

.chat {
  position: relative; display: flex; flex-direction: column; overflow: hidden;
  height: calc(100dvh - 62px - var(--nav-h) - env(safe-area-inset-bottom));
  min-height: 300px;
  background: var(--surface);
  --bubble: var(--surface-2);
  --mine: var(--accent);
  --mine-text: var(--accent-text);
}
@media (min-width: 720px) {
  .chat { border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
}

/* ---- Header: title and who is online ---- */
.chat-head {
  flex: none; display: flex; align-items: center; gap: 10px;
  min-height: 56px; padding: 8px 14px;
  border-bottom: 1px solid var(--border);
}
.chat-title { flex: 1; min-width: 0; }
.chat-title h2 { margin: 0; font-size: 1.15rem; }
.chat-sub {
  margin: 0; color: var(--muted); font-size: 0.8125rem; line-height: 1.3;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-sub:empty { display: none; }
.chat-online { display: flex; flex: none; list-style: none; margin: 0; padding: 0 2px 0 8px; }
.chat-online li { position: relative; margin-left: -8px; }
.chat-online li::after {
  content: ""; position: absolute; right: -1px; bottom: -1px; width: 10px; height: 10px;
  border-radius: 50%; background: #2fae6b; border: 2px solid var(--surface);
}
.chat-online .chat-avatar { box-shadow: 0 0 0 2px var(--surface); }
.chat-conn {
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  padding: 3px 10px; border-radius: 999px;
  background: var(--winner); color: var(--text); font-size: 0.8125rem; font-weight: 600;
}

.chat-avatar { width: 36px; height: 36px; font-size: 0.85rem; }
.chat-avatar.small { width: 28px; height: 28px; font-size: 0.7rem; }
.chat-avatar.tiny { width: 22px; height: 22px; font-size: 0.625rem; }
.chat-avatar.everyone { background: var(--accent-soft); color: var(--text); }

.chat-spinner {
  display: inline-block; width: 12px; height: 12px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent;
  animation: chat-spin 700ms linear infinite;
}
@keyframes chat-spin { to { transform: rotate(360deg); } }

/* ---- Message list ---- */
.chat-body { position: relative; flex: 1; min-height: 0; display: flex; }
.chat-list {
  flex: 1; min-width: 0; overflow-y: auto; overflow-x: hidden;
  overscroll-behavior: contain; overflow-anchor: none;
  padding: 22px 10px 10px;
  display: flex; flex-direction: column;
}
.chat-list:focus { outline: none; }
.chat-list > :first-child { margin-top: auto; } /* short conversations sit at the bottom */

.chat-hist {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 6px 0 10px; color: var(--muted); font-size: 0.8125rem; text-align: center;
}
.chat-day, .chat-divider {
  display: flex; align-items: center; gap: 10px; margin: 14px 4px 4px;
  color: var(--muted); font-size: 0.75rem; font-weight: 650; letter-spacing: 0.04em; text-transform: uppercase;
}
.chat-day::before, .chat-day::after, .chat-divider::before, .chat-divider::after {
  content: ""; flex: 1; height: 1px; background: var(--border);
}
.chat-divider { color: var(--danger); }
.chat-divider::before, .chat-divider::after { background: currentColor; opacity: 0.45; }

.msg {
  position: relative; display: grid; grid-template-columns: 36px minmax(0, 1fr); column-gap: 8px;
  align-items: start; padding: 1px 4px; border-radius: 12px; outline-offset: -2px;
}
.msg.first { margin-top: 10px; }
.msg.mine { grid-template-columns: minmax(0, 1fr); }
.msg-gutter { width: 36px; }
.msg > .chat-avatar { margin-top: 20px; }
.msg-main { display: grid; justify-items: start; min-width: 0; max-width: min(84%, 560px); }
.msg.mine .msg-main { justify-self: end; justify-items: end; }

.msg-meta { display: flex; align-items: baseline; gap: 8px; padding: 0 4px 2px; font-size: 0.8125rem; line-height: 1.4; }
.msg-name { font-weight: 700; color: color-mix(in srgb, var(--who, var(--text)) 45%, var(--text)); }
.msg-meta time { color: var(--muted); font-size: 0.75rem; }

.msg-bubble {
  max-width: 100%; padding: 7px 12px; border-radius: 18px;
  background: var(--bubble); color: var(--text);
  line-height: 1.4; white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.theirs:not(.first) .msg-bubble { border-top-left-radius: 6px; }
.msg.theirs:not(.last) .msg-bubble { border-bottom-left-radius: 6px; }
.msg.mine .msg-bubble { background: var(--mine); color: var(--mine-text); }
.msg.mine:not(.first) .msg-bubble { border-top-right-radius: 6px; }
.msg.mine:not(.last) .msg-bubble { border-bottom-right-radius: 6px; }
.msg.mine .msg-bubble a { color: inherit; }
.msg-bubble a { font-weight: 600; }

.msg.mention-me .msg-bubble { background: var(--winner); box-shadow: inset 0 0 0 1.5px var(--gold); }
.mention { font-weight: 650; border-radius: 5px; padding: 0 2px; background: color-mix(in srgb, var(--accent) 16%, transparent); }
.msg.mine .mention { background: color-mix(in srgb, var(--mine-text) 20%, transparent); }
.mention.me { background: color-mix(in srgb, var(--gold) 34%, transparent); }

.msg.deleted .msg-bubble {
  background: none; color: var(--muted); font-style: italic; font-size: 0.875rem;
  border: 1px dashed var(--border-strong); padding: 4px 12px;
}
.msg.jumbo .msg-bubble { background: none; box-shadow: none; padding: 2px 4px; line-height: 1.15; }
.msg.jumbo.xl .msg-text { font-size: 2.75rem; }
.msg.jumbo.lg .msg-text { font-size: 1.9rem; }
.msg.jumbo .msg-quote { font-size: 0.8125rem; }

.msg.sending .msg-bubble { opacity: 0.6; }
.msg.failed .msg-bubble { background: var(--danger-soft); color: var(--text); box-shadow: inset 0 0 0 1.5px var(--danger); }
.msg-status { margin: 2px 4px 0; display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 2px 10px; color: var(--muted); font-size: 0.75rem; }
.msg-status.failed { color: var(--danger); font-weight: 600; font-size: 0.8125rem; }
.chat .link {
  padding: 0; border: 0; background: none; color: inherit; cursor: pointer;
  font-weight: 700; text-decoration: underline; text-underline-offset: 2px; min-height: 24px;
}

.msg-quote {
  display: grid; width: 100%; margin: 0 0 5px; padding: 3px 8px; text-align: left; cursor: pointer;
  border: 0; border-left: 3px solid var(--accent); border-radius: 6px;
  background: color-mix(in srgb, var(--text) 7%, transparent); color: inherit;
  font-size: 0.8125rem; line-height: 1.35; white-space: normal;
}
.msg-quote span { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; opacity: 0.85; }
.msg.mine .msg-quote { border-left-color: currentColor; background: color-mix(in srgb, var(--mine-text) 16%, transparent); }

/* Reactions */
.msg-rx { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 3px; }
.msg.mine .msg-rx { justify-content: flex-end; }
.rx {
  display: inline-flex; align-items: center; gap: 4px; min-height: 28px; padding: 1px 8px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--surface);
  color: var(--text); font-size: 0.8125rem; font-weight: 600; cursor: pointer;
  font-variant-numeric: tabular-nums;
  transition: background-color 150ms var(--ease), border-color 150ms var(--ease), transform 100ms var(--ease);
}
.rx:hover { border-color: var(--border-strong); background: var(--surface-2); }
.rx:active { transform: scale(0.94); }
.rx[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.rx-emoji { font-size: 0.95rem; }
.msg-rx-who { display: none; margin: 3px 4px 0; color: var(--muted); font-size: 0.75rem; }
.msg.is-active .msg-rx-who { display: block; }

/* Actions: quick reactions, reply, delete. Shown on hover, focus or tap. */
.msg-actions {
  position: absolute; top: -22px; left: 44px; z-index: 3;
  display: none; align-items: center; gap: 1px; padding: 3px;
  border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); box-shadow: var(--shadow);
}
.msg.mine .msg-actions { left: auto; right: 6px; }
.msg.first .msg-actions { top: -12px; }
.msg.is-active .msg-actions, .msg:focus-visible .msg-actions, .msg-actions:focus-within { display: flex; }
@media (hover: hover) { .msg:hover .msg-actions { display: flex; } }
.msg-actions button {
  display: inline-grid; place-items: center; width: 32px; height: 32px; padding: 0;
  border: 0; border-radius: 50%; background: none; color: var(--text);
  font-size: 1.05rem; line-height: 1; cursor: pointer;
  transition: background-color 120ms var(--ease), transform 120ms var(--ease);
}
.msg-actions button:hover { background: var(--surface-2); transform: scale(1.15); }
.msg-actions button.icon { color: var(--muted); }
.msg-actions button.icon:hover { color: var(--text); transform: none; }
.msg-actions .confirm-del {
  width: auto; padding: 0 12px; border-radius: 999px;
  background: var(--danger); color: var(--surface); font-size: 0.8125rem; font-weight: 700;
}
.msg-actions .confirm-del:hover { background: var(--danger); transform: none; filter: brightness(1.08); }
@media (pointer: coarse) {
  .msg-actions { left: 4px; }
  .msg-actions button { width: 35px; height: 35px; }
}
.msg.is-active, .msg:focus-visible { background: color-mix(in srgb, var(--accent) 7%, transparent); }

.chat svg {
  width: 20px; height: 20px; fill: none; stroke: currentColor;
  stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.chat svg .fill { fill: currentColor; stroke: none; }

/* Jump to latest, toast */
.chat-jump {
  position: absolute; right: 14px; bottom: 12px; z-index: 4;
  display: inline-grid; place-items: center; width: 44px; height: 44px; padding: 0;
  border: 1px solid var(--border); border-radius: 50%;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow); cursor: pointer;
}
.chat-jump:hover { background: var(--surface-2); }
.chat-jump-count {
  position: absolute; top: -6px; right: -4px; min-width: 22px; padding: 1px 6px;
  border-radius: 999px; background: var(--accent); color: var(--accent-text);
  font-size: 0.75rem; font-weight: 700; line-height: 1.5; text-align: center;
}
.chat-toast {
  position: absolute; left: 50%; bottom: 12px; z-index: 5; transform: translateX(-50%);
  max-width: calc(100% - 96px); margin: 0; padding: 8px 14px; border-radius: 12px;
  background: var(--text); color: var(--surface); font-size: 0.875rem; font-weight: 600;
  box-shadow: var(--shadow); text-align: center;
}

/* Loading, empty and error states */
.chat-state { margin: auto; padding: 24px 12px; }
.chat-state code { font-size: 0.875em; padding: 1px 5px; border-radius: 5px; background: var(--surface-2); }
.chat-ideas, .chat-pick { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-top: 10px; }
.chip-btn {
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 5px 14px;
  border: 1px solid var(--border-strong); border-radius: 999px; background: var(--surface);
  color: var(--text); font-size: 0.9rem; font-weight: 600; text-decoration: none; cursor: pointer;
  transition: background-color 150ms var(--ease), transform 100ms var(--ease);
}
.chip-btn:hover { background: var(--accent-soft); }
.chip-btn:active { transform: scale(0.97); }
.chat-skeleton { display: grid; gap: 10px; padding: 8px 6px; }
.skel {
  height: 38px; border-radius: 18px; background: var(--surface-2);
  background-image: linear-gradient(100deg, transparent 30%, color-mix(in srgb, var(--surface) 70%, transparent) 50%, transparent 70%);
  background-size: 200% 100%; animation: chat-shimmer 1.4s linear infinite;
}
.skel.right { justify-self: end; }
@keyframes chat-shimmer { from { background-position: 150% 0; } to { background-position: -50% 0; } }

/* ---- Composer ---- */
.chat-foot { flex: none; border-top: 1px solid var(--border); background: var(--surface); }
.chat-form { padding: 8px 8px 4px; }
.chat-compose { display: flex; align-items: flex-end; gap: 6px; }
.chat-field { position: relative; flex: 1; min-width: 0; display: flex; }
.chat-input {
  flex: 1; min-width: 0; width: 100%; resize: none; display: block;
  min-height: 44px; max-height: 148px; padding: 10px 44px 10px 16px;
  border-radius: 22px; line-height: 1.4; background: var(--surface-2); border-color: var(--border);
  box-shadow: none;
}
.chat-input[aria-invalid="true"] { border-color: var(--danger); }
.chat-icon-btn {
  flex: none; display: inline-grid; place-items: center; width: 44px; height: 44px; padding: 0;
  border: 0; border-radius: 50%; background: none; color: var(--muted); cursor: pointer;
  transition: background-color 150ms var(--ease), color 150ms var(--ease), transform 100ms var(--ease);
}
.chat-icon-btn:hover { background: var(--surface-2); color: var(--text); }
.chat-icon-btn:active { transform: scale(0.92); }
.chat-icon-btn.small { width: 32px; height: 32px; }
.chat-icon-btn.banter { color: var(--gold); }
.chat-icon-btn.banter svg { width: 24px; height: 24px; }
.chat-icon-btn[aria-expanded="true"] { background: var(--accent-soft); color: var(--text); }
.chat-icon-btn.emoji { position: absolute; right: 2px; bottom: 2px; width: 40px; height: 40px; }
.chat-icon-btn.emoji:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.chat-send {
  flex: none; display: inline-grid; place-items: center; width: 44px; height: 44px; padding: 0;
  border: 0; border-radius: 50%; background: var(--accent); color: var(--accent-text); cursor: pointer;
  box-shadow: 0 1px 2px rgb(20 60 45 / 0.25), 0 4px 12px -4px var(--ring);
  transition: transform 120ms var(--ease), opacity 150ms var(--ease), filter 150ms var(--ease);
}
.chat-send:hover { filter: brightness(1.08); }
.chat-send:active { transform: scale(0.9); }
.chat-send:disabled { opacity: 0.4; cursor: default; box-shadow: none; filter: none; transform: none; }
.chat-hint {
  display: flex; justify-content: space-between; gap: 12px; margin: 3px 56px 0 58px; min-height: 1.1rem;
  color: var(--muted); font-size: 0.6875rem; line-height: 1.5;
}
.chat-counter { margin-left: auto; font-weight: 700; font-variant-numeric: tabular-nums; }
.chat-counter.over { color: var(--danger); }
@media (hover: none) and (pointer: coarse) {
  .chat-hint:not(:has(.chat-counter:not([hidden]))) { display: none; }
  .chat-form { padding-bottom: 8px; }
}

.chat-reply {
  display: flex; align-items: center; gap: 8px; margin: 0 4px 8px; padding: 6px 6px 6px 10px;
  border-left: 3px solid var(--accent); border-radius: 8px; background: var(--surface-2);
  font-size: 0.8125rem; line-height: 1.35;
}
.chat-reply-text { flex: 1; min-width: 0; display: grid; }
.chat-reply-text span { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chat-who {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 14px; padding: 12px 14px;
  background: var(--accent-soft);
}
.chat-who p { flex: 1 1 220px; margin: 0; font-size: 0.9375rem; }
.chat-who .chat-pick { justify-content: flex-start; margin: 0; flex-basis: 100%; }

/* Trays above the composer: trash talk, emoji, mentions */
.chat-tray { max-height: min(46vh, 320px); overflow-y: auto; border-bottom: 1px solid var(--border); background: var(--surface); }
.tray-head { display: flex; align-items: center; gap: 8px; padding: 8px 8px 4px 14px; }
.tray-head strong { font-family: var(--font-display); font-size: 1rem; }
.tray-head .muted { flex: 1; min-width: 0; font-size: 0.8125rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tray-head strong:only-of-type:not(:has(+ .muted)) { flex: 1; }
.banter-list { list-style: none; margin: 0; padding: 0 8px 8px; display: grid; gap: 6px; }
.banter-list li {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 6px 8px;
  padding: 8px 8px 8px 12px; border-radius: 12px; background: var(--surface-2);
}
.banter-list p { flex: 1 1 200px; margin: 0; font-size: 0.9375rem; line-height: 1.4; }
.emoji-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 2px; padding: 0 8px 8px; }
.emoji-grid button {
  height: 42px; padding: 0; border: 0; border-radius: 10px; background: none;
  font-size: 1.4rem; line-height: 1; cursor: pointer; transition: transform 100ms var(--ease);
}
.emoji-grid button:hover { background: var(--surface-2); transform: scale(1.12); }
.mention-list { list-style: none; margin: 0; padding: 6px; }
.mention-list li {
  display: flex; align-items: center; gap: 8px; min-height: 40px; padding: 4px 10px;
  border-radius: 10px; cursor: pointer; font-weight: 600;
}
.mention-list li .muted { font-weight: 400; font-size: 0.8125rem; }
.mention-list li[aria-selected="true"], .mention-list li:hover { background: var(--accent-soft); }

/* ---- Motion ---- */
@media (prefers-reduced-motion: no-preference) {
  .msg.is-new { animation: chat-in 240ms var(--ease) both; transform-origin: 0 100%; }
  .msg.mine.is-new { transform-origin: 100% 100%; }
  .msg.flash .msg-bubble { animation: chat-flash 1.6s var(--ease); }
  .chat-jump:not([hidden]), .chat-toast:not([hidden]), .msg-actions { animation: chat-rise 160ms var(--ease) both; }
  .chat-toast:not([hidden]) { animation-name: chat-rise-center; }
  .chat-tray:not([hidden]) { animation: chat-rise 180ms var(--ease) both; }
  @keyframes chat-in { from { opacity: 0; transform: translateY(8px) scale(0.96); } }
  @keyframes chat-rise { from { opacity: 0; transform: translateY(6px); } }
  @keyframes chat-rise-center { from { opacity: 0; transform: translate(-50%, 6px); } }
  @keyframes chat-flash { 0%, 60% { box-shadow: 0 0 0 3px var(--gold); } 100% { box-shadow: 0 0 0 3px transparent; } }
}
@media (prefers-reduced-motion: reduce) {
  .msg.flash .msg-bubble { box-shadow: 0 0 0 3px var(--gold); }
  .chat-spinner, .skel { animation: none; }
}
