:root {
  color-scheme: dark;
  --bg: #07070a;
  --panel: #12121a;
  --panel-strong: #1c1b29;
  --text: #f8f7ff;
  --muted: #a7a2b8;
  --accent: #8b5cf6;
  --accent-strong: #a78bfa;
  --live: #ff3b5f;
  --border: rgba(255, 255, 255, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow: hidden;
  background:
    radial-gradient(circle at top left, rgba(139, 92, 246, 0.32), transparent 34rem),
    linear-gradient(135deg, #07070a 0%, #11101a 55%, #06060a 100%);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

[hidden] {
  display: none !important;
}

.auth-screen {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 1rem;
}

.auth-card {
  display: grid;
  gap: 1rem;
  width: min(100%, 30rem);
  border: 1px solid var(--border);
  border-radius: 1.25rem;
  padding: 1.25rem;
  background: rgba(18, 18, 26, 0.9);
  box-shadow: 0 1.5rem 4rem rgba(0, 0, 0, 0.4);
}

.auth-card h1 {
  font-size: clamp(2rem, 7vw, 3.5rem);
}

.auth-card p {
  color: var(--muted);
}

.auth-error {
  min-height: 1.3rem;
  color: #fca5a5 !important;
}

.twitch-login {
  display: block;
  border-radius: 0.75rem;
  padding: 0.85rem 1rem;
  color: white;
  background: #9146ff;
  font-weight: 900;
  text-align: center;
  text-decoration: none;
}

.auth-divider {
  text-align: center;
  font-size: 0.8rem;
}

.shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 23rem;
  gap: 0.75rem;
  height: 100vh;
  padding: 0.75rem;
}

.stage,
.chat {
  border: 1px solid var(--border);
  background: rgba(18, 18, 26, 0.84);
  box-shadow: 0 1.5rem 4rem rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(18px);
}

.stage {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  min-height: 0;
  overflow: auto;
  border-radius: 1rem;
  padding: 0.75rem;
}

.brand-row,
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.65rem;
}

.header-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 0.6rem;
}

.account-pill {
  display: grid;
  grid-template-columns: auto auto;
  align-items: center;
  column-gap: 0.55rem;
  row-gap: 0.1rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.35rem 0.75rem 0.35rem 0.4rem;
  background: rgba(255, 255, 255, 0.05);
}

.account-avatar,
.avatar-fallback,
.message-avatar,
.chatter-avatar {
  width: 2rem;
  height: 2rem;
  border-radius: 999px;
  object-fit: cover;
  background: rgba(255, 255, 255, 0.12);
}

.account-avatar,
.account-pill > .avatar-fallback {
  grid-row: 1 / 3;
}

.avatar-fallback {
  display: grid;
  place-items: center;
  color: white;
  font-size: 0.78rem;
  font-weight: 900;
}

.account-pill span {
  color: var(--muted);
  font-size: 0.68rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.account-pill strong {
  color: white;
  font-size: 0.88rem;
}

.eyebrow {
  margin: 0 0 0.25rem;
  color: var(--accent-strong);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(1.45rem, 3vw, 2.35rem);
  letter-spacing: -0.07em;
}

.stream-status {
  color: var(--muted);
  margin-top: 0.1rem;
  font-size: 0.9rem;
}

.stream-info-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr) auto;
  gap: 0.6rem;
}

.stream-info-form button {
  width: auto;
}

h2 {
  font-size: 1rem;
}

.badge {
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.5rem 0.8rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
  font-size: 0.85rem;
  font-weight: 800;
}

.badge.is-live {
  border-color: rgba(255, 59, 95, 0.5);
  color: white;
  background: rgba(255, 59, 95, 0.18);
}

.player-card {
  position: relative;
  overflow: hidden;
  flex: 1 1 auto;
  min-height: min(64vh, 40rem);
  border: 1px solid var(--border);
  border-radius: 1rem;
  background: #000;
  aspect-ratio: 16 / 9;
}

video {
  display: block;
  width: 100%;
  height: 100%;
  background: #000;
}

.player-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 0.35rem;
  padding: 2rem;
  text-align: center;
  background: linear-gradient(135deg, rgba(8, 8, 12, 0.86), rgba(25, 22, 38, 0.88));
}

.player-overlay[hidden] {
  display: none;
}

/*
 * Unmute badge.  Shown ONLY while `video.muted === true`, attached
 * to the top-right of the player-card.  Browsers force autoplay to
 * be muted unless the user has previously interacted with the
 * domain, so without this badge viewers see a silent stream and
 * assume the audio is broken.  Tap to unmute.
 */
.unmute-badge {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.85rem;
  border-radius: 999px;
  background: rgba(191, 148, 255, 0.92);
  color: #0a0a12;
  font-weight: 800;
  font-size: 0.85rem;
  border: none;
  width: auto;
  cursor: pointer;
  box-shadow: 0 0.5rem 1.5rem rgba(0, 0, 0, 0.5);
  z-index: 5;
  animation: unmute-pulse 1.6s ease-in-out infinite;
}

.unmute-badge[hidden] {
  display: none;
}

.unmute-badge:hover,
.unmute-badge:focus-visible {
  background: #d4b6ff;
}

@keyframes unmute-pulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.04); }
}

.player-overlay span,
.setup-card,
.stream-meta,
.chat-header span,
.message-meta,
input,
select,
textarea {
  color: var(--muted);
}

.stream-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.35rem;
  font-size: 0.72rem;
}

.playback-hint {
  border: 1px solid rgba(167, 139, 250, 0.24);
  border-radius: 0.75rem;
  padding: 0.45rem 0.6rem;
  color: #ddd6fe;
  background: rgba(139, 92, 246, 0.1);
  font-size: 0.76rem;
  line-height: 1.45;
}

.playback-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
}

.mode-button {
  width: auto;
  border: 1px solid var(--border);
  border-radius: 0.55rem;
  padding: 0.45rem 0.65rem;
  background: rgba(255, 255, 255, 0.06);
  font-size: 0.78rem;
}

.mode-button.is-active {
  border-color: rgba(167, 139, 250, 0.8);
  background: linear-gradient(135deg, var(--accent), #ec4899);
}

.quality-picker {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  width: auto;
  color: var(--muted);
  font-size: 0.75rem;
}

.quality-picker select {
  width: auto;
  min-width: 7rem;
  border-radius: 0.55rem;
  padding: 0.45rem 0.55rem;
  font-size: 0.78rem;
}

.setup-card {
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 0.65rem;
  background: rgba(255, 255, 255, 0.04);
}

.diagnostics-card {
  border: 1px solid var(--border);
  border-radius: 1rem;
  padding: 0.65rem;
  background: rgba(255, 255, 255, 0.04);
}

.setup-card summary,
.diagnostics-card summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.45rem;
  cursor: pointer;
  color: white;
  font-size: 0.85rem;
  font-weight: 900;
}

.setup-card summary::-webkit-details-marker,
.diagnostics-card summary::-webkit-details-marker {
  display: none;
}

.setup-card summary::after,
.diagnostics-card summary::after {
  content: "Show";
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.35rem 0.65rem;
  color: var(--accent-strong);
  background: rgba(139, 92, 246, 0.12);
  font-size: 0.75rem;
}

.setup-card[open] summary::after,
.diagnostics-card[open] summary::after {
  content: "Hide";
}

.setup-card summary small,
.diagnostics-card summary small {
  color: var(--muted);
  font-weight: 600;
}

.setup-card .setup-grid {
  margin-top: 1rem;
}

.diagnostics-card .diagnostics-header:first-of-type {
  margin-top: 1rem;
}

.diagnostics-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.diagnostics-header span {
  color: var(--muted);
  font-size: 0.85rem;
}

.stats-title {
  margin-top: 1rem;
}

.health-grid,
.health-summary,
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.6rem;
  margin-top: 0.75rem;
}

.health-summary {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.metric {
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 0.7rem;
  background: rgba(0, 0, 0, 0.22);
}

.metric span,
.metric strong {
  display: block;
}

.metric span {
  color: var(--muted);
  font-size: 0.75rem;
}

.metric strong {
  margin-top: 0.25rem;
  color: white;
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}

.metric[data-status="live"] {
  border-color: rgba(34, 197, 94, 0.45);
}

.metric[data-status="live"] strong {
  color: #86efac;
}

.metric[data-status="waiting"] {
  border-color: rgba(251, 191, 36, 0.35);
}

.metric[data-status="waiting"] strong,
.metric[data-status="stale"] strong {
  color: #fde68a;
}

.metric[data-status="stale"] {
  border-color: rgba(251, 146, 60, 0.45);
}

.setup-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
  margin-top: 0.75rem;
}

label {
  display: grid;
  gap: 0.35rem;
  font-size: 0.85rem;
}

code {
  overflow-wrap: anywhere;
  border-radius: 0.55rem;
  padding: 0.55rem;
  color: white;
  background: rgba(0, 0, 0, 0.42);
}

.stream-warning {
  margin-top: 0.9rem;
  border: 1px solid rgba(251, 191, 36, 0.32);
  border-radius: 0.75rem;
  padding: 0.75rem;
  color: #fde68a;
  background: rgba(251, 191, 36, 0.1);
  font-size: 0.9rem;
  line-height: 1.45;
}

.chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
  border-radius: 1.25rem;
  min-height: 0;
}

.chat-header {
  border-bottom: 1px solid var(--border);
  padding: 0.75rem 0.85rem;
}

.viewer-count {
  width: auto;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.38rem 0.6rem;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
  font-size: 0.8rem;
  font-weight: 800;
}

.chatters-panel {
  border-bottom: 1px solid var(--border);
  padding: 0.75rem;
  background: rgba(0, 0, 0, 0.25);
}

.chatters-head,
.chatter-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.chatters-head {
  margin-bottom: 0.65rem;
}

.chatters-list {
  display: grid;
  gap: 0.45rem;
  max-height: 14rem;
  overflow-y: auto;
}

.chatters-group {
  margin-top: 0.35rem;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.chatter-row {
  justify-content: flex-start;
  border-radius: 0.65rem;
  padding: 0.35rem;
  background: rgba(255, 255, 255, 0.035);
}

.chatter-name {
  font-size: 0.9rem;
  font-weight: 800;
}

.messages {
  overflow-y: auto;
  min-height: 0;
  padding: 1rem;
}

.message {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 0.65rem;
  padding: 0.6rem 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  position: relative;       /* anchor the reply left-border bar */
}

.message-body {
  min-width: 0;
}

/*
 * Reply styling — Twitch-flavoured.
 *
 * A chat message is treated as a reply when its text starts with
 * "@<login> " (app.js sets the .is-reply class + populates the
 * .reply-header).  Visual treatment:
 *   1. A 3px purple vertical bar runs the full message row height
 *      on the LEFT edge, separating it visually from non-reply
 *      messages stacked above.
 *   2. The body indents slightly to accommodate the bar.
 *   3. A small "↳ Replying to @X" header above the message-meta
 *      mirrors Twitch's "Replying to @..." line.  Useful even
 *      when the @mention is the first token of the message body
 *      because mobile viewers may not scroll up to find the
 *      original message.
 *
 * The bar is positioned via `border-left` on the .message itself
 * (using the .is-reply modifier).  Tip: this DOESN'T conflict
 * with the parent `.messages { padding: 1rem }` because the
 * border lives INSIDE that padding.
 */
.message.is-reply {
  border-left: 3px solid rgba(191, 148, 255, 0.55);
  padding-left: 0.55rem;
  margin-left: -0.55rem;     /* keep horizontal alignment with non-reply rows */
  background: linear-gradient(
    90deg,
    rgba(191, 148, 255, 0.045) 0%,
    transparent 12rem
  );
}

.reply-header {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-bottom: 0.25rem;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.2;
}

.reply-header[hidden] {
  display: none;
}

.reply-arrow {
  display: inline-block;
  color: rgba(191, 148, 255, 0.7);
  font-size: 0.85rem;
  /* slight nudge so the corner glyph aligns with the text
     baseline of the "Replying to" label */
  transform: translateY(-1px);
}

.reply-target {
  color: #bf94ff;
  font-weight: 700;
}

/*
 * @mention token highlighting.  Renders the leading @<login>
 * AND any inline @<login> tokens in Twitch-purple so they pop
 * against the surrounding text the same way links do.  Subtle
 * background tint reinforces it for skim-readers without making
 * the text harder to read.
 */
.chat-mention {
  color: #bf94ff;
  font-weight: 700;
  background: rgba(191, 148, 255, 0.1);
  border-radius: 0.25rem;
  padding: 0 0.2rem;
}

.message-meta {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.35rem;
  font-size: 0.8rem;
}

.message-name,
.chatter-name {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}

.streamer-badge {
  display: inline-grid;
  place-items: center;
  width: 1rem;
  height: 1rem;
  border-radius: 0.25rem;
  color: #ff3b5f;
  background: rgba(255, 59, 95, 0.14);
  vertical-align: -0.12em;
}

.streamer-badge svg {
  width: 0.72rem;
  height: 0.72rem;
  fill: currentColor;
}

/*
 * Bot badge — rendered next to the displayName of any message
 * with isBot:true (i.e. messages injected by studio_bridge.py).
 * Same general dimensions as .streamer-badge for visual parity,
 * but uses a text label ("BOT") instead of an icon and a calmer
 * purple-on-translucent palette so viewers can distinguish at a
 * glance: red badge = real human streamer, purple badge = bot.
 */
.bot-badge {
  display: inline-block;
  padding: 0 0.32rem;
  border-radius: 0.3rem;
  color: #bf94ff;
  background: rgba(191, 148, 255, 0.16);
  font-size: 0.62rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  vertical-align: 0.12em;
  line-height: 1.4;
}

.message strong {
  color: var(--accent-strong);
}

.message p {
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.chat-emote {
  display: inline-block;
  height: 1.75rem;
  width: auto;
  margin: 0 0.08rem;
  vertical-align: middle;
}

/*
 * Clickable URLs in chat.  Twitch purple (#bf94ff), no underline
 * default, underline on hover.  Matches the established voice of
 * the chat UI (compact, low-noise) while still being unambiguously
 * recognisable as a link.
 *
 * `overflow-wrap: anywhere` is already on .message p (line ~620),
 * so long URLs wrap mid-string rather than overflowing the chat
 * column — important on mobile where the column is narrow.
 */
.chat-link {
  color: #bf94ff;
  text-decoration: none;
  word-break: break-word;
}

.chat-link:hover,
.chat-link:focus-visible {
  text-decoration: underline;
}

/*
 * Reply button on each message row.  Hidden by default; appears on
 * hover (desktop pointer) so the row stays uncluttered.  On mobile
 * (no hover), the long-press handler in app.js is the primary
 * affordance — the button can stay always-hidden on touch devices.
 */
.message-reply-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.4rem;
  height: 1.4rem;
  margin-left: auto;       /* push to far right of message-meta */
  padding: 0;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 0.95rem;
  cursor: pointer;
  opacity: 0;
  transition: opacity 120ms ease-out, color 120ms ease-out;
  border-radius: 0.4rem;
}

.message:hover .message-reply-btn,
.message-reply-btn:focus-visible {
  opacity: 1;
}

.message-reply-btn:hover,
.message-reply-btn:focus-visible {
  color: #bf94ff;
  background: rgba(191, 148, 255, 0.1);
}

/* Touch devices: hide the button entirely; long-press is the
   affordance.  hover:none catches phones/tablets reliably. */
@media (hover: none) {
  .message-reply-btn {
    display: none;
  }
}

/* ── Raid overlay ────────────────────────────────────────────────
 *
 * Full-viewport takeover fired by the `raid:start` socket event.
 * Hidden by default (the [hidden] attribute is set in HTML, removed
 * by app.js when a raid lands).  Centered card with the target's
 * Twitch avatar, countdown, then a button + auto-redirect.
 *
 * Uses `position: fixed` + `inset: 0` so it covers everything
 * including the existing chat / video panes.  z-index above the
 * mobile breakpoint's overlay panel concerns.
 */
.raid-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(10, 10, 18, 0.92);
  backdrop-filter: blur(16px);
  z-index: 9999;
  padding: 1.5rem;
  animation: raid-fade-in 220ms ease-out;
}

@keyframes raid-fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.raid-card {
  display: grid;
  justify-items: center;
  gap: 0.65rem;
  max-width: 28rem;
  width: 100%;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 1.25rem;
  padding: 2rem 1.5rem 1.5rem;
  background: linear-gradient(180deg, rgba(191, 148, 255, 0.12), rgba(18, 18, 26, 0.95));
  box-shadow: 0 2rem 5rem rgba(0, 0, 0, 0.6);
}

.raid-eyebrow {
  font-size: 0.85rem;
  font-weight: 900;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: #bf94ff;
  margin: 0;
}

.raid-avatar {
  width: 7rem;
  height: 7rem;
  border-radius: 50%;
  border: 3px solid #bf94ff;
  box-shadow: 0 0 0 6px rgba(191, 148, 255, 0.18);
  object-fit: cover;
}

.raid-title {
  font-size: 1.85rem;
  font-weight: 900;
  margin: 0.2rem 0 0;
  line-height: 1.15;
}

.raid-detail {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.raid-countdown {
  font-size: 3rem;
  font-weight: 900;
  margin: 0.4rem 0;
  color: #bf94ff;
  animation: raid-pulse 1s ease-in-out infinite;
  font-variant-numeric: tabular-nums;
  min-height: 3.6rem;
  line-height: 1;
}

@keyframes raid-pulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.08); }
}

.raid-button {
  display: inline-block;
  margin: 0.5rem 0 0.25rem;
  padding: 0.85rem 1.6rem;
  border-radius: 999px;
  background: #bf94ff;
  color: #0a0a12;
  font-weight: 900;
  font-size: 1.05rem;
  text-decoration: none;
  border: none;
  width: auto;
  min-width: 14rem;
  cursor: pointer;
  transition: transform 120ms ease-out, background 120ms ease-out;
}

.raid-button:hover,
.raid-button:focus-visible {
  background: #d4b6ff;
  transform: translateY(-1px);
}

.raid-autoredirect {
  margin: 0.1rem 0 0.5rem;
  font-size: 0.85rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-height: 1rem;
}

.raid-dismiss {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 0.45rem 0.9rem;
  border-radius: 999px;
  font-size: 0.85rem;
  width: auto;
  cursor: pointer;
}

.raid-dismiss:hover,
.raid-dismiss:focus-visible {
  color: inherit;
  border-color: rgba(255, 255, 255, 0.25);
}

.message.is-system {
  display: block;
  color: var(--muted);
  font-size: 0.9rem;
}

.message.is-system strong {
  color: #fbbf24;
}

.chat-form {
  display: grid;
  gap: 0.35rem;
  border-top: 1px solid var(--border);
  padding: 0.75rem;
  background: rgba(0, 0, 0, 0.18);
  position: relative;       /* anchor the autocomplete popover */
}

.chat-error {
  min-height: 1rem;
  color: #fca5a5;
  font-size: 0.78rem;
}

/*
 * Textarea + send button layout.  Textarea takes all available
 * width; send button is a square at the right edge, aligned to
 * the bottom of the textarea so it always sits next to the
 * "send" line of text the user is typing.  align-items:end keeps
 * the button at the bottom even when the textarea grows to 2-3
 * lines.
 */
.chat-input-row {
  display: flex;
  gap: 0.4rem;
  align-items: stretch;
}

.chat-form textarea {
  min-height: 3.75rem;
  max-height: 6rem;
  flex: 1 1 0;
  min-width: 0;
}

.chat-send-button {
  flex: 0 0 auto;
  width: 3.25rem;
  padding: 0;
  display: inline-grid;
  place-items: center;
  border: none;
  border-radius: 0.75rem;
  color: white;
  background: linear-gradient(135deg, #bf94ff, #8b5cf6);
  cursor: pointer;
  transition: filter 120ms ease-out, transform 120ms ease-out;
}

.chat-send-button:hover,
.chat-send-button:focus-visible {
  filter: brightness(1.12);
}

.chat-send-button:active {
  transform: translateY(1px);
}

.chat-send-button svg {
  width: 1.3rem;
  height: 1.3rem;
  /* The paper-plane glyph leans slightly; nudge so the
     diagonal feels centred in the button. */
  transform: translateX(-1px) translateY(0);
}

/*
 * Autocomplete popover (positioned above the input row).  Hidden
 * by default; app.js toggles [hidden] and pushes <button> items
 * into it as the caret moves through @mention or emote prefixes.
 */
.chat-autocomplete {
  position: absolute;
  bottom: calc(100% - 0.4rem);  /* sit just above the chat-form, pushed up */
  left: 0.75rem;
  right: 0.75rem;
  max-height: 12rem;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  background: rgba(18, 18, 26, 0.97);
  backdrop-filter: blur(12px);
  box-shadow: 0 0.75rem 2rem rgba(0, 0, 0, 0.45);
  z-index: 50;
  padding: 0.3rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.chat-autocomplete[hidden] {
  display: none;
}

.chat-autocomplete-item {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  width: 100%;
  padding: 0.45rem 0.6rem;
  border: none;
  border-radius: 0.5rem;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  font-size: 0.88rem;
}

.chat-autocomplete-item:hover,
.chat-autocomplete-item.is-selected {
  background: rgba(191, 148, 255, 0.16);
}

.chat-autocomplete-item img {
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
}

.chat-autocomplete-item .ac-avatar-fallback {
  display: inline-grid;
  place-items: center;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 800;
  flex-shrink: 0;
}

.chat-autocomplete-item .ac-emote-img {
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 0;
  background: transparent;
  object-fit: contain;
}

.chat-autocomplete-item .ac-label {
  flex: 1 1 0;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-autocomplete-item .ac-hint {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
}

input,
select,
textarea,
button {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  font: inherit;
}

input,
select,
textarea {
  padding: 0.75rem;
  background: rgba(0, 0, 0, 0.22);
  resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid rgba(167, 139, 250, 0.45);
  border-color: rgba(167, 139, 250, 0.7);
}

button {
  cursor: pointer;
  border: 0;
  padding: 0.8rem 1rem;
  color: white;
  background: linear-gradient(135deg, var(--accent), #ec4899);
  font-weight: 900;
}

.ghost-button {
  width: auto;
  border: 1px solid var(--border);
  color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
}

button:hover {
  filter: brightness(1.08);
}

@media (max-width: 900px) {
  /*
   * Mobile split-pane layout — video on top, chat below.
   *
   * History: an earlier version of this query used
   * `.shell { height: auto }` + `body { overflow: auto }`, which
   * made the whole document scroll instead of the messages list
   * scrolling internally — chat input pushed below 200 messages.
   * The first fix put video + chat in a grid with body locked, but
   * set `.stage { max-height: 45dvh; overflow: auto }` which
   * caused the video (with `min-height: min(64vh, 40rem)` from
   * desktop) to be taller than the stage AND positioned BELOW the
   * brand-row + stream-info-form, so it ended up scrolled
   * off-screen.  Streamer reported: "the mobile CHAT looks good
   * however i can no longer see the video at all."
   *
   * Current shape:
   *   - .shell is a flex column (auto stage, 1fr chat).  Stage
   *     gets just-enough height for the 16:9 video; chat gets the
   *     rest of the viewport.
   *   - Video player-card is reordered to TOP of the stage so it's
   *     immediately visible without scrolling.  Brand-row + info
   *     form + meta + controls + diagnostics all stack below the
   *     video; if any of those exceed the stage's natural height
   *     the stage scrolls internally to expose them.
   *   - Player-card's desktop `min-height: min(64vh, 40rem)` is
   *     dropped; aspect-ratio: 16/9 + flex: 0 0 auto handles
   *     sizing.
   *   - Chat keeps its internal grid (header / messages-1fr-scroll
   *     / form-pinned-bottom) so input is always reachable.
   *
   * `100dvh` (dynamic viewport height) handles iOS Safari address
   * bar + keyboard correctly; falls back to 100vh on older
   * browsers (Safari <16, ~2% of mobile users).
   */
  .shell {
    display: flex;
    flex-direction: column;
    grid-template-columns: 1fr;
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    padding: 0.5rem;
    gap: 0.5rem;
  }

  body {
    overflow: hidden;
    overscroll-behavior: contain;
  }

  .stage {
    /*
     * Video on top, everything else below.  `flex: 0 0 auto` +
     * `max-height: 50dvh` means the stage takes only what its
     * intrinsic content needs (the 16:9 video card at full
     * width, plus the playback-controls row) — usually
     * substantially LESS than 50dvh on portrait phones.  Hard
     * cap of 50dvh prevents extreme tablets / large phones from
     * giving the stage more than half the viewport.
     */
    flex: 0 0 auto;
    max-height: 50dvh;
    overflow: auto;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
  }

  /*
   * Keyboard-open mode (mobile).  When the chat input has focus
   * and the on-screen keyboard is up, the visualViewport shrinks
   * and dvh follows it.  Without further intervention the stage
   * naturally takes 50dvh of the shrunken viewport — which on a
   * small phone in landscape, or a phone with a tall keyboard,
   * can squeeze the video to nothing AND push everything else
   * around so the streamer "loses the video" entirely while
   * typing.  Reported: "when i bring up my keyboard i lose the
   * video (cant see it, obstructed? scrools? idk)".
   *
   * The is-keyboard-open class is added to .shell by app.js
   * when messageInput gains focus.  Under that class we:
   *   - shrink the stage cap further so the video stays in
   *     its 16/9 aspect ratio but doesn't fight chat for space
   *   - hide all non-video stage chrome (brand-row, controls,
   *     diagnostics) so the stage = just the video card
   *   - tighten the chat panel so input + the most-recent few
   *     messages are visible above the keyboard
   */
  .shell.is-keyboard-open .stage {
    max-height: 32dvh;
    gap: 0;
  }
  .shell.is-keyboard-open .stage > :not(.player-card) {
    display: none;
  }

  .stage .player-card {
    /* Promote video to top of stage on mobile. */
    order: -1;
    /* Drop the desktop floor so the card uses its 16/9 ratio. */
    min-height: 0;
    flex: 0 0 auto;
  }

  /*
   * Chat-space reclamation on mobile.  Each of these elements
   * adds 20-60 px of vertical chrome the streamer doesn't need
   * on a phone — collectively they were eating a third of the
   * chat panel.  Power users can still expand the <details>
   * collapsibles for diagnostics if they want them.
   */
  .stage .eyebrow,
  .stage .playback-hint,
  .stage .stream-meta {
    display: none;
  }
  .brand-row #streamTitle {
    font-size: 1.1rem;
    line-height: 1.15;
    margin: 0;
  }
  .brand-row .stream-status {
    font-size: 0.78rem;
    margin: 0.1rem 0 0;
    color: var(--muted);
  }
  .header-actions {
    gap: 0.4rem;
  }
  .account-pill {
    padding: 0.25rem 0.55rem 0.25rem 0.3rem;
    font-size: 0.78rem;
  }
  .account-pill .account-avatar,
  .account-pill .avatar-fallback {
    width: 1.4rem;
    height: 1.4rem;
  }
  .playback-controls {
    gap: 0.3rem;
    flex-wrap: wrap;
  }
  .playback-controls .mode-button {
    padding: 0.45rem 0.7rem;
    font-size: 0.82rem;
  }
  .quality-picker {
    /* The text label "WebRTC quality" eats horizontal space on
       narrow phones.  The dropdown alone is enough affordance. */
    font-size: 0;
  }
  .quality-picker select {
    font-size: 0.85rem;
    padding: 0.4rem 0.6rem;
  }

  .chat {
    /*
     * Chat fills the remaining viewport height.  flex: 1 1 0 lets
     * it both expand to take leftover space AND shrink if the
     * stage scrolls into more height.  min-height: 0 is critical:
     * without it the inner `messages` 1fr track defaults to
     * min-content and never scrolls.
     */
    flex: 1 1 0;
    min-height: 0;
  }

  .messages {
    /*
     * Belt-and-braces: explicitly scroll the messages list and
     * ensure its grid track can shrink.  Slightly tighter
     * padding so each message takes less vertical space.
     */
    overflow-y: auto;
    min-height: 0;
    padding: 0.6rem;
  }

  .message {
    /* Tighter message rows on mobile so more fit in the
       visible area.  Desktop padding (0.6rem top/bottom) wastes
       ~25% of each row on whitespace; 0.4rem is plenty for
       legibility. */
    padding: 0.4rem 0;
    gap: 0.5rem;
  }

  .chatters-panel {
    /* Don't let the chatters panel eat the messages area. */
    max-height: 40%;
    overflow-y: auto;
  }

  .health-grid,
  .health-summary,
  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 620px) {
  .shell {
    padding: 0.5rem;
  }

  .brand-row,
  .setup-grid,
  .stream-info-form {
    grid-template-columns: 1fr;
  }

  .health-grid,
  .health-summary,
  .stats-grid {
    grid-template-columns: 1fr;
  }

  .brand-row {
    align-items: flex-start;
    flex-direction: column;
  }

  .header-actions,
  .ghost-button {
    width: 100%;
  }

  .header-actions {
    justify-content: flex-start;
  }
}
