/* ==========================================================================
   Chat — a panel that lives in the corner
   ========================================================================== */
#chat { position: fixed; right: 20px; bottom: 20px; z-index: 90; transition: right .25s cubic-bezier(.4,0,.2,1); }
body.wb-open #chat, body.page-open #chat { display: none; }

.chat-bubble {
  position: relative; width: 54px; height: 54px; border-radius: 50%; border: 0;
  background: var(--accent); color: #fff; cursor: pointer; display: grid; place-items: center;
  box-shadow: 0 6px 22px rgba(0,0,0,.22);
}
.chat-bubble:hover { filter: brightness(1.06); }
.chat-badge {
  position: absolute; top: -3px; right: -3px; min-width: 21px; height: 21px; padding: 0 6px;
  border-radius: 11px; background: #d64545; color: #fff; font-size: 11px; font-weight: 700;
  display: grid; place-items: center; border: 2px solid var(--bg);
}

.chat-panel {
  width: 360px; max-width: calc(100vw - 32px); height: 520px; max-height: calc(100vh - 110px);
  background: var(--bg); border: 1px solid var(--border); border-radius: 16px;
  box-shadow: 0 14px 46px rgba(0,0,0,.22); display: flex; flex-direction: column; overflow: hidden;
}
.chat-head {
  display: flex; align-items: center; gap: 8px; padding: 12px 14px;
  border-bottom: 1px solid var(--border); flex: none;
}
.chat-head .spacer { flex: 1; }
.chat-title { font-weight: 700; font-size: 14.5px; }
.chat-sub { font-size: 12px; color: var(--text-faint); }
.chat-back, .chat-ico {
  border: 0; background: none; color: var(--text-soft); cursor: pointer; padding: 4px;
  border-radius: 7px; display: grid; place-items: center; font-size: 15px;
}
.chat-back:hover, .chat-ico:hover { background: var(--bg-hover); color: var(--text); }

.chat-body { flex: 1; overflow-y: auto; padding: 8px; }
.chat-empty { color: var(--text-faint); font-size: 13.5px; text-align: center; padding: 30px 16px; }

/* The colour is stated, not inherited: a <button> with none of its own is drawn
   in the system's link blue on iOS, so the whole list read as a wall of links
   rather than as names. It should look like anything else you can tap in a
   list — a task, say. */
.chat-row {
  display: flex; align-items: center; gap: 11px; width: 100%; text-align: left;
  border: 0; background: none; padding: 9px 8px; border-radius: 11px; cursor: pointer; font: inherit;
  color: var(--text);
}
.chat-row:hover { background: var(--bg-hover); }
.chat-av {
  width: 36px; height: 36px; border-radius: 50%; flex: none; overflow: hidden;
  background: var(--accent); color: #fff; display: grid; place-items: center;
  font-size: 12px; font-weight: 700;
}
.chat-av.ws { background: var(--bg-hover); color: var(--text-soft); }
.chat-av img { width: 100%; height: 100%; object-fit: cover; }
.chat-row-body { flex: 1; min-width: 0; }
.chat-row-top { display: flex; align-items: baseline; gap: 8px; }
.chat-row-name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-row-when { margin-left: auto; font-size: 11px; color: var(--text-faint); flex: none; }
.chat-row-last {
  display: block; font-size: 12.5px; color: var(--text-faint); margin-top: 1px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
/* Unread is said once, by the dot after the name (further down). A conversation
   you have read is set the way everything else in the app is — no colour of its
   own, no badge, nothing asking to be looked at. */
.chat-row.unread .chat-row-name { font-weight: 700; }
.chat-row.unread .chat-row-last { color: var(--text-soft); }

.chat-msg { margin: 0 4px 10px; max-width: 84%; }
/* a message that is mostly a preview card needs the room */
.chat-msg:has(.yt-card), .chat-msg:has(.gif-in-text) { max-width: 96%; }
.chat-msg.run-on { margin-top: -6px; }
.chat-msg.mine { margin-left: auto; }
/* Baseline alignment drops a 20px picture onto the text baseline, which sits it
   far too low next to the name. The row is centred; only the time is nudged
   back onto the name's own line. */
.chat-msg-who {
  display: flex; align-items: center; gap: 7px; font-size: 11.5px;
  font-weight: 700; color: var(--text-soft); margin: 0 2px 4px;
  line-height: 1.2;
}
.chat-msg-who span { font-weight: 400; color: var(--text-faint); }
.chat-msg.mine .chat-msg-who { justify-content: flex-end; }
.chat-bub {
  background: var(--bg-hover); border-radius: 13px; padding: 8px 12px;
  font-size: 14px; line-height: 1.45; word-break: break-word; white-space: normal;
}
.chat-msg.mine .chat-bub { background: var(--accent-soft); color: var(--text); }
.chat-bub a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.chat-at { color: var(--accent); font-weight: 600; }

/* margin:0 because this is a <form>, and a form is given a bottom margin by the
   browser itself — which left the input floating fifteen pixels above the
   bottom edge of the panel instead of sitting on it. */
.chat-composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px; margin: 0;
  border-top: 1px solid var(--border); flex: none;
}
.chat-composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border-strong); border-radius: 12px;
  padding: 9px 12px; font: inherit; font-size: 14px; outline: none; max-height: 110px;
  background: var(--bg); color: var(--text); line-height: 1.4;
}
.chat-composer textarea:focus { border-color: var(--accent); }
.chat-send {
  flex: none; width: 36px; height: 36px; border-radius: 50%; border: 0;
  background: var(--accent); color: #fff; cursor: pointer; display: grid; place-items: center;
}
.chat-send:hover { filter: brightness(1.06); }

.chat-at-pop {
  position: fixed; right: 30px; bottom: 96px; z-index: 95; background: var(--bg);
  border: 1px solid var(--border); border-radius: 11px; box-shadow: 0 8px 26px rgba(0,0,0,.18);
  padding: 4px; min-width: 170px;
}
.chat-at-pop button {
  display: block; width: 100%; text-align: left; border: 0; background: none; font: inherit;
  font-size: 13.5px; padding: 7px 10px; border-radius: 7px; cursor: pointer; color: var(--text);
}
.chat-at-pop button:hover { background: var(--bg-hover); }

.chat-sel { position: fixed; z-index: 96; }
.chat-sel button {
  display: inline-flex; align-items: center; gap: 7px; border: 0; background: #1d1d20; color: #fff;
  border-radius: 9px; padding: 8px 12px; font: inherit; font-size: 13px; cursor: pointer;
  box-shadow: 0 6px 20px rgba(0,0,0,.28);
}
.chat-sel button:hover { background: #000; }

@media (max-width: 640px) {
  /* Above the composer's own buttons rather than on top of them. The send
     button's top edge sits 91px off the bottom of the screen — 20 of the
     composer's padding, 23 for the hint line under it, 8 more padding and the
     40 of the button itself — and the bubble used to cover its corner and take
     the tap meant for it. */
  #chat { right: 12px; bottom: 108px; }
  .chat-panel { width: calc(100vw - 24px); height: min(70vh, 520px); }

  /* Open on a phone, it takes the whole screen. A conversation in a card the
     size of a postcard, with the app showing around its edges, reads as a
     preview of a chat rather than as the place you are having one — and the
     part of the screen it did use was the part the keyboard wanted. */
  /* One box is sized and the panel fills it, so the composer — pinned to the
     bottom of the panel by the flex column — lands on the bottom edge of
     whatever is actually visible. The height starts as the screen and is
     narrowed by the script when the keyboard takes part of it. */
  body.chat-open #chat {
    top: 0; left: 0; right: 0; bottom: auto; z-index: 120;
    height: 100dvh;
  }
  body.chat-open .chat-panel {
    width: 100%; max-width: none; height: 100%; max-height: none;
    border: 0; border-radius: 0; box-shadow: none;
  }
  /* the notch and the home bar are not places to put a button */
  body.chat-open .chat-head { padding-top: calc(12px + env(safe-area-inset-top)); }
  body.chat-open .chat-composer { padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  /* nothing scrolls behind it — but only while it is the thing you can see: a
     full page or the whiteboard hides the chat and would be left unscrollable */
  body.chat-open:not(.wb-open):not(.page-open) { overflow: hidden; }
  /* both of these float above the panel and would otherwise be under it */
  body.chat-open .chat-at-pop, body.chat-open .chat-sel { z-index: 130; }
}

/* the second half of the list: people you haven't written to yet */
.chat-sec {
  font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase;
  color: var(--text-faint); padding: 14px 10px 5px;
}

/* who said it — a face makes a wall of names readable */
.chat-msg-who .chat-av { width: 22px; height: 22px; font-size: 9px; flex: none; }
.chat-msg-who b { font-weight: 700; line-height: 22px; }
.chat-msg.mine .chat-msg-who { flex-direction: row-reverse; }

/* answering with one character */
.chat-react { display: flex; flex-wrap: wrap; gap: 4px; margin: 3px 2px 0; }
.chat-msg.mine .chat-react { justify-content: flex-end; }
.chat-chip {
  display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--border);
  background: var(--bg); border-radius: 999px; padding: 1px 7px; font: inherit; font-size: 12px;
  cursor: pointer; color: var(--text-soft); line-height: 1.6;
}
.chat-chip i { font-style: normal; font-size: 10.5px; font-weight: 700; color: var(--text-faint); }
.chat-chip:hover { border-color: var(--border-strong); }
.chat-chip.mine { border-color: var(--accent); background: var(--accent-soft); color: var(--text); }
.chat-chip.add { color: var(--text-faint); padding: 2px 7px; opacity: 0; transition: opacity .12s; }
.chat-msg:hover .chat-chip.add, .chat-chip.add:focus { opacity: 1; }

/* An open task sits on the right and puts its send button exactly where the
   chat bubble is. The chat steps aside rather than fighting it for the corner. */
body.drawer-open #chat { right: calc(min(560px, 100%) + 20px); }
@media (max-width: 700px) {
  /* on a narrow screen the task fills the width — there is no "beside" */
  body.drawer-open #chat { display: none; }
}

/* what has not been read yet */
.chat-new-line {
  display: flex; align-items: center; gap: 9px; margin: 12px 2px 8px;
  font-size: 10.5px; font-weight: 700; letter-spacing: .07em; text-transform: uppercase;
  color: var(--accent);
}
.chat-new-line::before, .chat-new-line::after {
  content: ""; flex: 1; height: 1px; background: var(--accent); opacity: .35;
}
.chat-msg.fresh .chat-bub { box-shadow: inset 3px 0 0 var(--accent); }
.chat-msg.fresh .chat-msg-who b { color: var(--text); }
/* a conversation with something unread carries it in the list too */
.chat-row.unread .chat-row-name::after {
  content: ""; display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent); margin-left: 6px; vertical-align: middle;
}

/* three dashes on their own line */
.chat-hr { border: 0; border-top: 1px solid currentColor; opacity: .25; margin: 7px 0; }
