/* Writing together: partners' avatars, their carets on the page, and the chat in the right margin. */

/* A partner's two letters in their own ink (Person#ink). */
.avatar {
  --size: 28px;
  position: relative;
  display: inline-grid;
  flex: none;
  place-items: center;
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
  background: var(--avatar-ink, #61574a);
  color: #fff;
  font: 700 calc(var(--size) * 0.38)/1 var(--font-ui);
  letter-spacing: 0.02em;
  user-select: none;
}
.avatar--small { --size: 24px; }
.avatar--tiny { --size: 18px; }
/* The one writing in a scene wears a pencil. */
.avatar--writing::after {
  content: "✎";
  position: absolute;
  right: -5px;
  bottom: -4px;
  display: grid;
  place-items: center;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background: var(--brass);
  color: var(--brass-ink);
  font-size: 9px;
  box-shadow: 0 0 0 2px var(--mahogany-deep);
}

/* Who's here, on the bar, and the chat's button. */
.here { display: flex; align-items: center; gap: 0.6rem; }
.here__people { display: flex; align-items: center; gap: 4px; }
.here__people[hidden] { display: none; }
.here__person { position: relative; padding: 0; border: 0; border-radius: 50%; background: none; cursor: pointer; }
.here__person .avatar { box-shadow: 0 0 0 2px var(--mahogany-deep); }
.here__person:focus-visible { outline: 2px solid var(--focus-ring); outline-offset: 2px; }
.here__tip {
  position: absolute;
  top: calc(100% + 10px);
  left: 50%;
  z-index: 50;
  display: none;
  translate: -50% 0;
  padding: 0.45rem 0.7rem;
  border-radius: 8px;
  background: var(--sheet);
  color: var(--sheet-soft);
  font: 0.82rem/1.35 var(--font-ui);
  text-align: left;
  white-space: nowrap;
  box-shadow: var(--shadow-menu, 0 6px 18px rgb(0 0 0 / 0.35));
  pointer-events: none;
}
.here__tip b { display: block; color: var(--sheet-ink); }
.here__person:is(:hover, :focus-visible) .here__tip { display: block; }
.here__more { color: var(--chrome-muted); font: 600 0.8rem var(--font-ui); }

.here__chat {
  position: relative;
  display: grid;
  place-items: center;
  width: 34px;
  height: 30px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--chrome);
  cursor: pointer;
}
.here__chat svg { width: 21px; height: 21px; }
.here__chat:hover, .here__chat[aria-pressed="true"] { background: var(--desk-light); }
.here__badge {
  position: absolute;
  top: -3px;
  right: -4px;
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: 9px;
  background: #e5372c;
  color: #fff;
  font: 700 0.66rem/17px var(--font-ui);
  text-align: center;
  box-shadow: 0 0 0 2px var(--mahogany-deep);
}
.here__badge[hidden], .here__badge--people { display: none; }

.focus-mode .here { display: none; }

/* A phone's bar keeps to one line: two avatars at most, overlapping, and a narrower chat button. */
@media (max-width: 40rem) {
  .here { gap: 0.15rem; }
  .here__people { gap: 0; }
  .here__person + .here__person { margin-left: -8px; }
  .here__person:nth-child(n+3), .here__more { display: none; }
  .here__person .avatar { --size: 22px; }
  .here__tip { display: none !important; }
  .here__person .avatar--writing::after { right: -3px; bottom: -3px; width: 11px; height: 11px; font-size: 7px; }
  .here { position: relative; }
  .here:has(.here__people:not([hidden])) .here__chat { display: none; }
  .here:has(.here__people:not([hidden])) .here__badge--people:not([hidden]) { display: block; }
}

/* A partner's caret: a thin line in their ink, their name on a flag only while hovered. Faded once it's
   been still for half a minute. */
.caret-layer { position: absolute; inset: 0; pointer-events: none; }
.partner-caret {
  position: absolute;
  z-index: 3;
  width: 2px;
  border-radius: 1px;
  background: var(--avatar-ink);
  pointer-events: auto;
}
/* A little more to hover than the line itself. */
.partner-caret::before { content: ""; position: absolute; inset: 0 -3px; }
.partner-caret__flag {
  position: absolute;
  bottom: 100%;
  left: -1px;
  display: none;
  margin-bottom: 1px;
  padding: 1px 6px 2px;
  border-radius: 4px 4px 4px 0;
  background: var(--avatar-ink);
  color: #fff;
  font: 700 0.68rem/1.3 var(--font-ui);
  white-space: nowrap;
}
.partner-caret:hover .partner-caret__flag { display: block; }
.partner-caret--still { opacity: 0.4; }
@media (prefers-reduced-motion: no-preference) {
  .partner-caret { transition: opacity 600ms ease; }
}
.focus-mode .caret-layer { display: none; }

/* The chat: a rounded card floating in the right margin. The page stays where it is; on screens under
   1400px it covers the page's blank right edge, never the words. Under 1200px it's a sheet from the bottom. */
.chat {
  --chat-paper: #fff;
  --chat-ink: #111;
  --chat-soft: #6e6e73;
  --chat-faint: #8e8e93;
  --chat-rule: #e5e5ea;
  --chat-head: #f9f9fb;
  --chat-mine: #0a7cff;
  --chat-theirs: #e9e9eb;
  --chat-theirs-ink: #000;
  position: fixed;
  z-index: 30;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--chat-paper);
  color: var(--chat-ink);
  font: 0.92rem/1.32 -apple-system, "SF Pro Text", var(--font-ui);
}
.chat[hidden] { display: none; }
html[data-theme="night"] .chat {
  --chat-paper: #1c1c1e;
  --chat-ink: #f2f2f7;
  --chat-soft: #a1a1a6;
  --chat-faint: #8e8e93;
  --chat-rule: #38383a;
  --chat-head: #232325;
  --chat-theirs: #3a3a3c;
  --chat-theirs-ink: #fff;
}
.focus-mode .chat { display: none; }

@media (min-width: 75rem) {
  .chat {
    top: 4.75rem;
    right: 18px;
    width: 300px;
    height: min(640px, calc(100dvh - 6.5rem));
    border-radius: 18px;
    box-shadow: 0 18px 40px rgb(0 0 0 / 0.5);
  }
}
@media (max-width: 74.99rem) {
  .chat {
    right: 0;
    bottom: 0;
    left: 0;
    height: min(70dvh, 560px);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -12px 36px rgb(0 0 0 / 0.45);
  }
}
@media (min-width: 40.01rem) and (max-width: 74.99rem) {
  .chat { left: auto; right: 16px; width: 360px; }
}

.chat__head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.6rem 0.6rem 0.6rem 0.9rem;
  border-bottom: 1px solid var(--chat-rule);
  background: var(--chat-head);
}
.chat__title { flex: 1; min-width: 0; }
.chat__title h2 { margin: 0; font: 700 0.95rem var(--font-ui); }
.chat__title p { margin: 0; overflow: hidden; color: var(--chat-soft); font-size: 0.75rem; text-overflow: ellipsis; white-space: nowrap; }
.chat__close, .chat__menu > summary {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--chat-faint);
  font-size: 1.3rem;
  line-height: 1;
  list-style: none;
  cursor: pointer;
}
.chat__menu > summary::-webkit-details-marker { display: none; }
.chat__close:hover, .chat__menu > summary:hover { background: var(--chat-rule); color: var(--chat-ink); }
.chat__menu { position: relative; }
.chat__menu .menu__items { right: 0; left: auto; min-width: 12rem; }

.chat__log {
  display: flex;
  flex: 1;
  flex-direction: column;
  padding: 0.6rem 0.75rem 0.4rem;
  overflow-y: auto;
  overscroll-behavior: contain;
}
/* Short logs sit at the bottom, by the box, as a phone's do. */
.chat__log > turbo-frame { display: flex; flex-direction: column; margin-top: auto; }
.chat__log :is(#chat_messages, turbo-frame[id^="chat_earlier"]) { display: flex; flex-direction: column; }
.chat__loading, .chat__empty, .chat__cleared {
  align-self: center;
  margin: 0.8rem 0.5rem;
  color: var(--chat-faint);
  font-size: 0.75rem;
  text-align: center;
}
.chat__earlier { align-self: center; margin: 0.3rem 0 0.6rem; color: var(--chat-mine); font-size: 0.78rem; }
.chat__time { align-self: center; margin: 0.8rem 0 0.35rem; color: var(--chat-faint); font-size: 0.7rem; font-weight: 600; }
.chat__time b { color: var(--chat-soft); }
.chat__read { align-self: flex-end; margin: 0.15rem 0.2rem 0.25rem; color: var(--chat-faint); font-size: 0.68rem; font-weight: 600; }

/* A message: grey on the left with the writer's avatar, or blue on the right if it's yours. Runs of
   messages from one person share a name at the top and an avatar and tail at the bottom. */
.message { display: flex; flex-direction: column; margin-top: 2px; }
.message__who { display: none; margin: 0.45rem 0 0.1rem 2.25rem; color: var(--chat-faint); font-size: 0.68rem; }
.message--starts:not(.message--mine) .message__who { display: block; }
.message__row { display: flex; align-items: flex-end; gap: 10px; }
.message__row > .avatar { visibility: hidden; }
.message--ends .message__row > .avatar, .chat__typing .avatar { visibility: visible; }
.message--mine .message__row { flex-direction: row-reverse; }
.message--mine .message__row > .avatar { display: none; }
.message__bubble {
  position: relative;
  max-width: 76%;
  margin: 0;
  padding: 0.42rem 0.75rem;
  border-radius: 18px;
  background: var(--chat-theirs);
  color: var(--chat-theirs-ink);
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.message--mine .message__bubble { background: var(--chat-mine); color: #fff; }
/* The tail on the last bubble of a run. */
.message--ends .message__bubble::before, .message--ends .message__bubble::after { content: ""; position: absolute; bottom: 0; height: 18px; }
.message--ends:not(.message--mine) .message__bubble::before { left: -7px; width: 20px; background: var(--chat-theirs); border-bottom-right-radius: 15px; }
.message--ends:not(.message--mine) .message__bubble::after { left: -10px; width: 10px; background: var(--chat-paper); border-bottom-right-radius: 10px; }
.message--ends.message--mine .message__bubble::before { right: -7px; width: 20px; background: var(--chat-mine); border-bottom-left-radius: 15px; }
.message--ends.message--mine .message__bubble::after { right: -10px; width: 10px; background: var(--chat-paper); border-bottom-left-radius: 10px; }
.message--mine .message__bubble { margin-right: 4px; }
.message:not(.message--mine) .message__bubble { margin-left: 2px; }

/* Unsend, on your own messages while you're over them. */
.message__unsend-form { display: none; align-self: center; }
.message--mine:is(:hover, :focus-within) .message__unsend-form:not([hidden]) { display: block; }
.message__unsend { padding: 0.1rem 0.35rem; border: 0; background: none; color: var(--chat-faint); font: 600 0.7rem var(--font-ui); cursor: pointer; }
.message__unsend:hover { color: var(--danger-on-paper, #b3261e); }

/* Someone writing a message: three still dots, nothing looping. */
.chat__typing { padding: 0 0.75rem 0.3rem; }
.chat__typing[hidden] { display: none; }
.message__bubble--dots { display: inline-flex; gap: 4px; padding: 0.62rem 0.8rem; }
.message__bubble--dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--chat-faint); }
.message__bubble--dots i:nth-child(2) { opacity: 0.7; }
.message__bubble--dots i:nth-child(3) { opacity: 0.45; }

.chat__compose { display: flex; align-items: flex-end; gap: 0.5rem; padding: 0.55rem 0.7rem 0.7rem; border-top: 1px solid var(--chat-rule); }
.chat__field {
  flex: 1;
  min-height: 34px;
  max-height: 120px;
  padding: 0.42rem 0.8rem;
  border: 1px solid var(--chat-rule);
  border-radius: 18px;
  background: var(--chat-paper);
  color: var(--chat-ink);
  font: inherit;
  resize: none;
}
.chat__field:focus { outline: none; border-color: var(--chat-mine); }
.chat__send {
  display: grid;
  flex: none;
  place-items: center;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: var(--chat-mine);
  color: #fff;
  cursor: pointer;
}
.chat__send svg { width: 18px; height: 18px; }
.chat__compose:has(.chat__field:placeholder-shown) .chat__send { opacity: 0.4; }
