/* Live chat (SPEC 25): the chat panel on the live page (_live_chat.html), the chat replay on a replay's watch page
   (_chat_replay.html) and Studio > Live chat. Colours come from style.css's tokens, so light and dark both follow
   the system setting. */

/* Panel ------------------------------------------------------------------- */

.chat {
  position: relative; display: flex; flex-direction: column; min-width: 0;
  /* Beside the player on wide screens: a fixed height, the list scrolls */
  height: clamp(420px, calc(100vh - 120px), 760px);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  overflow: hidden;
}
.chat-head {
  position: relative; display: flex; align-items: center; gap: 8px; flex: none;
  min-height: 50px; padding: 6px 8px 6px 14px; border-bottom: 1px solid var(--line);
}
.chat-title { font-size: 15px; font-weight: 700; white-space: nowrap; }
.chat-state {
  min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); font-size: 12px; font-weight: 600;
}
.chat-tools { display: flex; align-items: center; gap: 2px; margin-left: auto; flex: 0 1 auto; min-width: 0; }
.chat-icon-btn {
  display: grid; place-items: center; width: 36px; height: 36px; padding: 0; border: 0; border-radius: 50%;
  background: none; color: var(--text-2); cursor: pointer;
}
.chat-icon-btn:hover { background: var(--surface-2); color: var(--text); }
.chat-icon-btn[aria-pressed="true"], .chat-icon-btn[aria-expanded="true"] { background: var(--accent-soft); color: var(--accent-text); }
/* Only shown where the chat sits under the player (phones, narrow windows) */
.chat-toggle {
  display: none; align-items: center; gap: 4px; height: 32px; min-width: 0; padding: 0 10px; border: 0; border-radius: 999px;
  background: var(--surface-2); color: var(--text); font-size: 13px; font-weight: 600; cursor: pointer;
}
.chat-toggle [data-chat-toggle-label] { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-toggle:hover { background: var(--hover); }
.chat-toggle svg { flex: none; transition: transform 0.15s; transform: rotate(180deg); }
.chat-toggle[aria-expanded="false"] svg { transform: none; }

.chat-body { position: relative; display: flex; flex-direction: column; flex: 1; min-height: 0; }
/* The list with its empty text and "New messages" pill, which sit on the list whatever is above or below it */
.chat-scroll { position: relative; display: flex; flex-direction: column; flex: 1; min-height: 0; }

.chat-banner {
  flex: none; margin: 8px 10px 0; padding: 7px 10px; border-radius: 8px;
  background: var(--surface-2); color: var(--text); font-size: 13px; font-weight: 600; text-align: center;
}
.chat-banner.is-warn { background: color-mix(in srgb, var(--warn) 18%, transparent); }

/* Pinned message ---------------------------------------------------------- */

.chat-pinned {
  flex: none; display: flex; align-items: flex-start; gap: 8px; margin: 8px 10px 0; padding: 8px 10px;
  border-radius: 10px; background: var(--surface-2); border-left: 3px solid var(--accent);
  font-size: 13.5px; line-height: 1.4;
}
.chat-pinned-icon { flex: none; margin-top: 2px; color: var(--accent-text); }
.chat-pinned-main { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.chat-pinned-label { display: block; font-size: 11px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-2); }
.chat-pinned .chat-name { margin-right: 4px; }
.chat-pinned-unpin {
  flex: none; padding: 2px 8px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface);
  color: var(--text); font-size: 12px; font-weight: 600; cursor: pointer;
}
.chat-pinned-unpin:hover { background: var(--hover); }

/* Messages ---------------------------------------------------------------- */

.chat-list {
  flex: 1; min-height: 0; margin: 0; padding: 8px 0; list-style: none;
  overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
}
.chat-list:focus-visible { outline-offset: -2px; }
.chat-empty {
  position: absolute; left: 16px; right: 16px; top: 45%; transform: translateY(-50%);
  color: var(--text-2); font-size: 14px; text-align: center; pointer-events: none;
}
.chat-msg {
  position: relative; display: flex; align-items: flex-start; gap: 8px;
  padding: 4px 40px 4px 12px; font-size: 14px; line-height: 1.45;
}
.chat-msg:hover, .chat-msg:focus-within { background: color-mix(in srgb, var(--surface-2) 70%, transparent); }
.chat-msg .avatar { margin-top: 1px; }
.chat-line { flex: 1; min-width: 0; margin: 0; overflow-wrap: anywhere; word-break: break-word; }
.chat-name { font-weight: 700; color: var(--text-2); unicode-bidi: isolate; }
.chat-msg.is-mine .chat-name { color: var(--text); }
.chat-line .ubadge { margin-left: 3px; }
.chat-tag {
  display: inline-block; margin-left: 4px; padding: 0 6px; border-radius: 6px; vertical-align: 1px;
  font-size: 10.5px; font-weight: 800; line-height: 17px; letter-spacing: 0.03em; text-transform: uppercase;
}
.chat-tag.tag-creator { background: var(--accent); color: #fff; }
.chat-tag.tag-mod { background: color-mix(in srgb, var(--chart-1) 20%, transparent); color: var(--text); }
.chat-text { margin-left: 6px; color: var(--text); }
.chat-text mark { padding: 0 2px; border-radius: 4px; background: var(--accent-soft); color: var(--accent-text); font-weight: 700; }
/* A message that mentions you: a marked edge as well as a tint, and "Mentions you" for screen readers. The @you in it
   sits on that one tint (a second one under it would bring the red text below 4.5:1 in the light theme). */
.chat-msg.is-mention { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.chat-msg.is-mention .chat-text mark, .chat-pinned .chat-text mark { background: none; } /* the pinned box is tinted too */
.chat-msg.is-pinned-msg .chat-line::after { content: ""; }
.chat-time { margin-left: 6px; color: var(--text-3); font-size: 12px; font-variant-numeric: tabular-nums; }

.chat-msg-more {
  position: absolute; top: 2px; right: 6px; display: grid; place-items: center; width: 30px; height: 30px; padding: 0;
  border: 0; border-radius: 50%; background: none; color: var(--text-2); cursor: pointer; opacity: 0;
}
.chat-msg:hover .chat-msg-more, .chat-msg-more:focus-visible, .chat-msg-more[aria-expanded="true"] { opacity: 1; }
.chat-msg-more:hover, .chat-msg-more[aria-expanded="true"] { background: var(--hover); color: var(--text); }
@media (hover: none) { .chat-msg-more { opacity: 1; } }

.chat-pill {
  position: absolute; left: 50%; bottom: 12px; z-index: 3; transform: translateX(-50%);
  display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 14px; border: 0; border-radius: 999px;
  background: var(--text); color: var(--bg); font-size: 13px; font-weight: 700; box-shadow: var(--shadow); cursor: pointer;
  white-space: nowrap;
}

/* Composer ---------------------------------------------------------------- */

.chat-composer { flex: none; padding: 8px 10px 10px; border-top: 1px solid var(--line); }
.chat-input-row { display: flex; align-items: center; gap: 6px; }
.chat-input {
  flex: 1; min-width: 0; height: 40px; padding: 0 14px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 999px; outline: none;
}
.chat-input:focus { border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 25%, transparent); }
.chat-input:disabled, .chat-input[aria-disabled="true"] { opacity: 0.6; cursor: not-allowed; }
.chat-input[aria-invalid="true"] { border-color: var(--danger-text); }
.chat-send {
  display: grid; place-items: center; flex: none; width: 40px; height: 40px; padding: 0; border: 0; border-radius: 50%;
  background: var(--accent); color: #fff; cursor: pointer;
}
.chat-send:hover { background: var(--accent-hover); }
.chat-send:disabled { background: var(--surface-2); color: var(--text-3); cursor: default; }
.chat-meta { display: flex; justify-content: space-between; gap: 10px; margin-top: 5px; padding: 0 6px; font-size: 12px; color: var(--text-2); }
.chat-hint { min-width: 0; }
.chat-count { flex: none; font-variant-numeric: tabular-nums; color: var(--text-3); }
.chat-count.is-over { color: var(--danger-text); font-weight: 700; }
.chat-error {
  margin-top: 6px; padding: 7px 10px; border-radius: 8px; background: var(--accent-soft); color: var(--danger-text);
  font-size: 13px; font-weight: 600;
}
.chat-signin { flex: none; padding: 10px; border-top: 1px solid var(--line); }

/* Menus (a message's moderator menu, the header's chat settings) ---------- */

.chat-menu {
  position: absolute; z-index: 20; min-width: 190px; max-width: calc(100% - 16px); padding: 6px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.chat-settings-menu { top: calc(100% - 4px); right: 8px; }
.chat-menu button {
  display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 10px; border: 0; border-radius: 8px;
  background: none; color: var(--text); font-size: 14px; text-align: left; cursor: pointer;
}
.chat-menu button:hover, .chat-menu button:focus-visible { background: var(--surface-2); outline: none; }
.chat-menu button:focus-visible { box-shadow: inset 0 0 0 2px var(--focus); }
.chat-menu [role="menuitemradio"]::before { content: ""; width: 16px; flex: none; text-align: center; }
.chat-menu [role="menuitemradio"][aria-checked="true"]::before {
  content: "\2713"; content: "\2713" / ""; /* a mark only: "checked" is already said */
  font-weight: 800; color: var(--accent-text);
}
.chat-menu-label { padding: 6px 10px 2px; font-size: 11px; font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase; color: var(--text-2); }
.chat-menu-sep { height: 1px; margin: 6px 4px; background: var(--line); }
.chat-menu .chat-menu-danger { color: var(--danger-text); }

.chat-confirm .dialog-text { color: var(--text-2); }

/* Replay on the watch page ------------------------------------------------ */

.chat-replay-note { flex: none; padding: 8px 14px 10px; border-top: 1px solid var(--line); }
.watch-page.has-chat-replay { grid-template-areas: "player replay" "info next"; grid-template-rows: auto 1fr; }
.watch-page.has-chat-replay .watch-main { display: contents; }
.watch-page.has-chat-replay .player { grid-area: player; }
.watch-page.has-chat-replay .watch-info { grid-area: info; min-width: 0; }
.watch-page.has-chat-replay .up-next { grid-area: next; }
/* Beside the player: exactly as tall as the player (its content doesn't size the row), the list scrolls */
.watch-page.has-chat-replay .chat-replay { grid-area: replay; align-self: stretch; height: auto; min-height: 320px; contain: size; }

/* Studio > Live chat ------------------------------------------------------ */

.chat-studio { min-width: 0; }
.chat-studio-intro { max-width: 70ch; margin-bottom: 14px; }
.chat-mod-form { max-width: 520px; margin-bottom: 18px; }
.chat-mod-row { display: flex; align-items: center; gap: 8px; }
.chat-mod-row input { flex: 1; min-width: 0; }
.chat-mod-at { font-weight: 700; color: var(--text-2); }
.chat-mod-form .form-error { margin-top: 8px; }
.chat-people { margin: 0; padding: 0; list-style: none; display: grid; }
.chat-person { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-top: 1px solid var(--line); }
.chat-person:first-child { border-top: 0; }
.chat-person-avatar { flex: none; }
.chat-person-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.chat-person-name { font-weight: 700; }
.chat-person form { margin: 0; flex: none; }

/* Narrow: the chat goes under the player and can be collapsed ------------- */

@media (max-width: 1100px) {
  .chat { height: auto; }
  .chat-toggle { display: inline-flex; }
  /* the settings menu can still open below the header; nothing may widen the page */
  .chat.is-collapsed { overflow: visible; overflow-x: clip; }
  .chat.is-collapsed .chat-body { display: none; }
  .chat.is-collapsed .chat-head { border-bottom: 0; }
  .chat-list { flex: none; height: min(52vh, 440px); }
  .chat-replay .chat-list { height: min(40vh, 300px); }
  .watch-page.has-chat-replay { grid-template-areas: "player" "replay" "info" "next"; grid-template-rows: none; }
  .watch-page.has-chat-replay .chat-replay { align-self: start; min-height: 0; contain: none; }
  /* The live page (SPEC 24's live.html): the chat right under the player, then the title and the channel */
  .live-page:has(.chat-live) .watch-main { display: contents; }
  .live-page:has(.chat-live) .watch-info { order: 1; min-width: 0; }
}

@media (max-width: 720px) {
  .watch-page.has-chat-replay .chat-replay { margin: 0 16px; }
  .chat-msg { padding-right: 36px; }
  .chat-mod-row .btn { padding: 0 14px; }
}

@media (max-width: 480px) {
  /* Studio lists: the button goes under the name instead of squeezing it */
  .chat-person { flex-wrap: wrap; row-gap: 4px; }
  .chat-person-text { flex: 1 1 calc(100% - 56px); }
  .chat-person form { margin-left: 52px; }
}

@media (max-width: 380px) {
  .chat-head { padding-left: 10px; gap: 4px; }
  .chat-state { display: none; }
  .chat-msg { padding-left: 8px; gap: 6px; }
}

@media (max-width: 360px) {
  /* "Show (12)" on the button, "Show chat (12 new)" for screen readers */
  .chat-toggle-word { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-toggle svg { transition: none; }
}
