/* ============================================================
   CHAT — styles for shared/chat.js

   Its own file rather than a block in base.css, because the hub
   (www/index.html) is a self-contained page that does not load base.css.
   One <link> on all four pages beats a second copy of these rules living
   inline in the hub and drifting out of step with this one.

   Every page that links this one links shared/tokens.css first, the hub
   included, so the whole palette is available here. That was not true before
   #128, when the hub carried its own :root and a token it did not happen to
   define rendered as a black bubble rather than as an error.
   ============================================================ */

/* ---- launcher ---- */

.chat-fab {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 120;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 6px 24px rgba(42, 37, 32, 0.16);
  transition: transform 0.15s ease, background 0.15s ease;
  touch-action: manipulation;
}

.chat-fab:active { transform: scale(0.94); background: var(--bg-soft); }

.chat-fab-dot {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent-teal);
  border: 2px solid var(--card);
}

/* ---- docked launcher: the pill (#246) ----

   The other way in. Round chat lives on a screen that is being played, so
   the control cannot be the round button above: that shape already means
   "talk to the developer" on these pages, and two of them in one corner is
   one control as far as a player is concerned. This one is wide, dark, and
   says what it is. */

.chat-pill {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  /* It floats over the board, which on the lobby and the ballot means
     floating over the primary button. word/app.js measures that bar and puts
     its height here, so the pill rides above it rather than on it. */
  bottom: calc(max(16px, env(safe-area-inset-bottom)) + var(--chat-pill-lift, 0px));
  /* Under .countdown (100) on purpose. The countdown owns the screen for the
     three seconds it is up, and a chat pill on top of it is a tap target over
     a thing nobody can act on. */
  z-index: 95;
  display: flex;
  align-items: center;
  gap: 8px;
  border: none;
  border-radius: 999px;
  /* 44px: the smallest control a thumb should have to find, and this one is
     found mid-round with a clock running. Pinned rather than left to the
     padding, because the unread count is the tallest thing in here and it
     comes and goes, which would otherwise make the pill change size under
     the finger reaching for it. */
  min-height: 48px;
  padding: 0 18px;
  background: var(--card-active);
  color: var(--ink-on-dark);
  font-family: inherit;
  font-size: 16px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
  box-shadow: 0 6px 20px rgba(26, 37, 48, 0.3);
  /* Transform only. `bottom` is driven by a measurement that follows the
     action bar as the screen scrolls, and easing it would leave the pill
     lagging behind the thing it is supposed to be sitting above. */
  transition: transform 0.15s ease;
}

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

.chat-pill:active { transform: scale(0.95); }

.chat-pill-count {
  min-width: 20px;
  height: 20px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent-teal);
  color: #fff;
  font-size: 11.5px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

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

/* ---- panel ---- */

.chat-backdrop {
  --chat-inset-top: max(20px, env(safe-area-inset-top));
  --chat-inset-bottom: max(20px, env(safe-area-inset-bottom));
  position: fixed;
  inset: 0;
  z-index: 150;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgb(222 208 191 / 40%);
  padding: var(--chat-inset-top) 20px var(--chat-inset-bottom);
}

.chat-backdrop.open { display: flex; }

.chat-panel {
  position: relative;
  background: var(--card);
  border-radius: 26px;
  width: 100%;
  max-width: 460px;
  height: 560px;
  /* vh first as a fallback; dvh wins where supported so mobile browser chrome
     sliding in and out doesn't clip the sheet. Matches .cat-modal. */
  max-height: calc(100vh - var(--chat-inset-top) - var(--chat-inset-bottom));
  max-height: calc(100dvh - var(--chat-inset-top) - var(--chat-inset-bottom));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.22);
  animation: chatIn 0.22s cubic-bezier(0.2, 0.7, 0.3, 1);
}

@keyframes chatIn {
  from { opacity: 0; transform: translateY(12px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}

/* ---- docked panel (#246) ----

   Same panel, different presentation. It rises over the board rather than
   pushing it: the board keeps its height and its scroll position, so closing
   the sheet puts the reader back exactly where they were, and the turn pill
   and the secret word stay above it the whole time. */

.chat-backdrop.is-dock {
  z-index: 110;
  background: none;
  align-items: flex-end;
  padding: 0;
  /* The round underneath is still being played, so every tap that is not on
     the sheet belongs to the game. That also removes click-outside-to-close:
     this one is closed by its own button or by Escape. */
  pointer-events: none;
}

.chat-panel.is-dock {
  pointer-events: auto;
  width: 100%;
  max-width: 520px;
  /* Percentages here are of the VISUAL viewport, which chat.js pins the
     container to, so the sheet shrinks with the keyboard instead of being
     pushed off the bottom of it. */
  height: 56%;
  min-height: min(240px, 100%);
  max-height: 100%;
  border-radius: 22px 22px 0 0;
  box-shadow: 0 -10px 34px rgba(42, 37, 32, 0.18);
  /* Drops the modal's chatIn, which would otherwise still be attached here.
     Its first frame is opacity 0, and a frozen animation clock holds frame
     one, so a sheet opened in a tab that is not on screen would be invisible
     rather than merely un-animated. */
  animation: none;
}

/* Added by chat.js only when the tab is actually on screen. See the note by
   openPanel: a frozen animation clock holds frame one, and frame one of this
   is a sheet nobody can see. */
.chat-panel.is-dock.is-arriving {
  animation: chatDockIn 0.22s cubic-bezier(0.2, 0.7, 0.3, 1);
}

@keyframes chatDockIn {
  from { transform: translateY(100%); }
  to   { transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-panel, .chat-panel.is-dock.is-arriving { animation: none; }
  .chat-fab { transition: none; }
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 14px 12px 22px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-title {
  font-family: 'Literata', Georgia, serif;
  font-weight: 400;
  font-size: 19px;
  color: var(--ink);
  margin: 0;
}

.chat-close {
  background: none;
  border: none;
  cursor: pointer;
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--ink);
  padding: 0;
  border-radius: 10px;
  transition: background 0.15s;
}

.chat-close:active { background: var(--bg-soft); }

/* ---- thread ---- */

.chat-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 16px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.chat-day {
  align-self: center;
  font-size: 12px;
  color: var(--ink-faint);
  margin: 12px 0 6px;
}

.chat-row {
  display: flex;
  flex-direction: column;
  max-width: 82%;
  margin-top: 7px;
}

.chat-row.is-me { align-self: flex-end; align-items: flex-end; }
.chat-row.is-them { align-self: flex-start; align-items: flex-start; }

/* A row carrying the sender's picture. Only incoming messages have one: your
   own are already on your side of the thread in your own colour, and a face
   there would say something the position has said already.

   The picture itself comes from the caller, so nothing here knows what one
   looks like. This is the slot it drops into, sized for the thread rather
   than for the lobby it was drawn for. */
.chat-row.has-av {
  flex-direction: row;
  align-items: flex-end;
  gap: 8px;
  /* 8px of picture plus its gap has to come from somewhere, and taking it out
     of the bubble would reflow every message in a room that has faces. */
  max-width: 88%;
}

.chat-av {
  flex: none;
  width: 30px;
  height: 30px;
}

.chat-av > * {
  width: 100%;
  height: 100%;
  font-size: 11px;
}

.chat-stack {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-width: 0;
}

.chat-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 16px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  /* base.css turns selection off across the games so dragging on a card never
     highlights it. Message text is the one thing here people will want to copy. */
  user-select: text;
  -webkit-user-select: text;
}

.chat-row.is-them .chat-bubble {
  background: var(--bg-soft);
  color: var(--ink);
  border-bottom-left-radius: 6px;
}

.chat-row.is-me .chat-bubble {
  background: var(--accent-teal);
  color: #fff;
  border-bottom-right-radius: 6px;
}

/* A message landing in the thread. Rises and settles rather than appearing,
   which is what makes it read as arriving rather than as having been there. */
.chat-arrive { animation: chatArrive 0.32s cubic-bezier(0.2, 0.8, 0.3, 1.05); }

@keyframes chatArrive {
  from { opacity: 0; transform: translateY(10px) scale(0.96); }
  to   { opacity: 1; transform: none; }
}

/* Typing dots, shown while the greeting is on its way. */
.chat-typing {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 13px 16px;
  animation: chatArrive 0.28s cubic-bezier(0.2, 0.8, 0.3, 1.05);
}

.chat-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--ink-faint);
  animation: chatDot 1.25s ease-in-out infinite;
}

.chat-dot:nth-child(2) { animation-delay: 0.16s; }
.chat-dot:nth-child(3) { animation-delay: 0.32s; }

@keyframes chatDot {
  0%, 60%, 100% { opacity: 0.35; transform: translateY(0); }
  30%           { opacity: 1;    transform: translateY(-3px); }
}

/* The whole point of this animation is movement, so there is nothing to
   soften for someone who asked for less of it: it does not run at all, and
   chat.js skips straight to the settled greeting. */
@media (prefers-reduced-motion: reduce) {
  .chat-arrive, .chat-typing, .chat-dot { animation: none; }
}

.chat-who {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-soft);
  margin-bottom: 2px;
}

.chat-time {
  font-size: 11px;
  color: var(--ink-faint);
  margin: 3px 6px 0;
}

/* ---- composer ---- */

.chat-foot {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 10px 14px max(10px, env(safe-area-inset-bottom));
  background: var(--card);
}

.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.chat-field {
  flex: 1;
  font-family: inherit;
  font-size: 16px; /* below 16px iOS Safari zooms the page on focus */
  line-height: 1.4;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 10px 14px;
  resize: none;
  outline: none;
  max-height: 120px;
  overflow-y: auto;
  user-select: text;
  -webkit-user-select: text;
}

.chat-field:focus { border-color: var(--accent-teal); }

/* The product's send button, not the chat panel's. Four things wear it: the
   chat composer (built in shared/chat.js), the feedback popup on all three
   game pages, and the clue board's field (#257). It lives here rather than
   in base.css because admin.html loads this file and not that one. Its icon
   comes from the plane macro in src/components/icons.njk. */
.send-btn {
  flex-shrink: 0;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--accent-teal);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
  transition: opacity 0.15s ease, transform 0.15s ease;
}

.send-btn:active:not(:disabled) { transform: scale(0.94); }
.send-btn:disabled { opacity: 0.35; cursor: default; }

.chat-err {
  font-size: 13px;
  color: var(--accent-red, #d04a2f);
  padding: 8px 6px 0;
}

/* Phone: fill the screen rather than floating a small card in the middle.
   The docked sheet is exempt from every line of this. Filling the screen is
   the one thing it exists not to do. */
@media (max-width: 520px) {
  .chat-backdrop { padding: 0; }
  .chat-panel:not(.is-dock) {
    height: 100%;
    max-height: none;
    border-radius: 0;
    max-width: none;
  }
  .chat-panel:not(.is-dock) .chat-header { padding-top: max(16px, env(safe-area-inset-top)); }
}

/* ---- docked panel, on the side (#279) ----

   On a wide screen the room chat is a column on the right, open the whole
   time chat is on, and the game moves over to make room for it. chat.js adds
   .is-side while the caller's `side` media query matches, and body.chat-side
   while the column is up. No close button: there is no pill to bring it back. */

.chat-backdrop.is-dock.is-side {
  top: 0;
  right: 0;
  bottom: 0;
  left: auto;
  width: 376px;
  padding: 16px 16px 16px 0;
  align-items: stretch;
}

.chat-backdrop.is-side .chat-panel.is-dock {
  height: auto;
  min-height: 0;
  max-height: none;
  max-width: none;
  border-radius: 22px;
  box-shadow: 0 10px 34px rgba(42, 37, 32, 0.14);
}

.chat-backdrop.is-side .chat-close { display: none; }

body.chat-side { padding-right: 376px; }

/* The full-screen covers (the countdown, and the word game's "Time to find
   the impostor") are fixed to the window, so the padding above does not move
   them, and their text centred on the whole screen, half a column right of
   the game. Stopped at the column, they centre on the game (#286). */
body.chat-side .countdown { right: 376px; }
