/* ============================================================
   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.

   Uses only tokens that base.css and the hub's inline :root BOTH define
   (--bg, --bg-soft, --card, --ink, --ink-soft, --ink-faint, --accent-teal,
   --border, --shadow). Reach for anything else and the hub renders it as an
   invalid value, which shows up as a black bubble rather than 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);
}

/* ---- 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; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-panel { 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; }

.chat-bubble {
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 15px;
  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); }

.chat-send {
  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;
}

.chat-send:active:not(:disabled) { transform: scale(0.94); }
.chat-send: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. */
@media (max-width: 520px) {
  .chat-backdrop { padding: 0; }
  .chat-panel {
    height: 100%;
    max-height: none;
    border-radius: 0;
    max-width: none;
  }
  .chat-header { padding-top: max(16px, env(safe-area-inset-top)); }
}
