/* chat feature styles — one shared room. Phone: full-height overlay. Desktop: floating card on the right. */

/* ---------- entry buttons ---------- */
#tb-ext { display: flex; align-items: center; gap: 8px; margin-left: auto; }
#panel-ext { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.ch-btn {
  position: relative; display: grid; place-items: center; flex: none;
  width: 44px; height: 44px; padding: 0; border: 0; border-radius: 50%;
  color: var(--ink);
  transition: background-color .15s, transform .1s;
}
.ch-btn:active { transform: scale(.96); }
.ch-btn-tb {
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  box-shadow: var(--shadow);
}
.ch-btn-panel { width: 40px; height: 40px; background: var(--paper-2); }
.ch-btn-panel.on { background: var(--ink); color: var(--card); }
.ch-badge {
  position: absolute; top: -3px; right: -4px;
  min-width: 19px; height: 19px; padding: 0 5px; border-radius: 999px;
  display: grid; place-items: center;
  background: var(--coral); color: #fff; font-size: 11.5px; font-weight: 600; line-height: 1;
  box-shadow: 0 0 0 2px var(--card); font-variant-numeric: tabular-nums;
}
@media (hover: hover) {
  .ch-btn-tb:hover { background: var(--card); }
  .ch-btn-panel:hover:not(.on) { background: var(--fill-hover, var(--paper-2)); }
}

/* ---------- container ---------- */
#chat {
  position: fixed; z-index: 900;
  left: 0; right: 0; top: var(--ch-top, 0px);
  height: 100dvh; height: var(--ch-h, 100dvh);
  display: flex; pointer-events: none;
}
#chat.open { pointer-events: auto; }
/* phone: a solid backdrop down to the physical bottom — the card itself only spans the visual viewport, and iOS
   draws a translucent accessory bar/keyboard over whatever is beneath it */
@media (max-width: 899px) {
  #chat.open::before { content: ""; position: fixed; left: 0; right: 0; top: 0; height: calc(var(--full-h, 100lvh) + 200px); background: var(--card); z-index: -1; }
}
.ch-card {
  flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative;
  background: var(--card); overflow: hidden;
  opacity: 0; transform: translateY(24px);
  transition: opacity .2s var(--spring), transform .22s var(--spring);
}
#chat.open .ch-card { opacity: 1; transform: none; }
html.ch-open, html.ch-open body { overflow: hidden; overscroll-behavior: none; }

/* header */
.ch-head {
  flex: none; display: flex; align-items: center; gap: 12px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) calc(env(safe-area-inset-right, 0px) + 12px) 10px calc(env(safe-area-inset-left, 0px) + 20px);
  border-bottom: 1px solid var(--line);
}
/* header keeps its status-bar inset even with the keyboard up: in home-screen mode iOS always draws (and blurs
   under) the status bar */
.ch-titles { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.ch-title { margin: 0; font-size: 18px; line-height: 1.25; }
.ch-sub { font-size: 13px; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ch-x {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 50%;
  display: grid; place-items: center; background: var(--paper-2); color: var(--ink-2);
}
@media (hover: hover) { .ch-x:hover { color: var(--ink); } }

/* list */
.ch-list {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  /* never scroll sideways: the tap-to-show time sits beside a bubble and may poke past the edge */
  overflow-x: hidden; overflow-x: clip; touch-action: pan-y;
  padding: 8px calc(env(safe-area-inset-right, 0px) + 14px) 12px calc(env(safe-area-inset-left, 0px) + 14px);
  user-select: text; -webkit-user-select: text;
}
.ch-older { display: grid; place-items: center; min-height: 8px; }
.ch-spin {
  width: 18px; height: 18px; margin: 8px; border-radius: 50%;
  border: 2px solid var(--ink-3); border-right-color: transparent; animation: ch-spin .7s linear infinite;
}
@keyframes ch-spin { to { transform: rotate(360deg); } }
.ch-start { text-align: center; color: var(--ink-3); font-size: 12.5px; padding: 10px 0 2px; }
.ch-empty {
  min-height: 50vh; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 6px; text-align: center; color: var(--ink-2); font-size: 14px; padding: 24px;
}
.ch-empty b { color: var(--ink); font-size: 15px; font-weight: 600; }
.ch-empty-ic { width: 52px; height: 52px; border-radius: 50%; display: grid; place-items: center; background: var(--paper-2); color: var(--ink-2); margin-bottom: 6px; }

.ch-day { display: flex; justify-content: center; margin: 18px 0 6px; }
.ch-day span {
  font-size: 12px; font-weight: 500; color: var(--ink-2); letter-spacing: .01em;
  padding: 3px 10px;
}

/* groups */
.ch-grp { display: flex; flex-direction: column; gap: 2px; margin-top: 12px; }
.ch-grp.me { align-items: flex-end; }
.ch-grp.them { align-items: flex-start; padding-left: 38px; }
.ch-who { display: flex; align-items: center; gap: 8px; margin: 0 0 4px -38px; }
.ch-av { padding: 0; border: 0; background: none; border-radius: 50%; display: block; line-height: 0; }
.ch-av .av { --s: 30px; }
.ch-meta { display: flex; align-items: baseline; gap: 6px; min-width: 0; }
.ch-name {
  padding: 0; border: 0; background: none; font-size: 13.5px; font-weight: 600; color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 60vw;
}
button.ch-name, button.ch-av { cursor: pointer; }
@media (hover: hover) { button.ch-name:hover { text-decoration: underline; text-underline-offset: 2px; } }
.ch-rt { font-size: 12px; color: var(--ink-3); white-space: nowrap; font-variant-numeric: tabular-nums; }

/* bubbles */
.ch-m { position: relative; max-width: min(78%, 520px); display: flex; cursor: default; }
.ch-b {
  padding: 8px 13px; border-radius: 18px; font-size: 15.5px; line-height: 1.38;
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: normal;
  background: var(--paper-2); color: var(--ink);
}
.ch-grp.me .ch-b { background: var(--ink); color: var(--card); }
.ch-grp.them .ch-m:not(.first) .ch-b { border-top-left-radius: 6px; }
.ch-grp.them .ch-m:not(.last) .ch-b { border-bottom-left-radius: 6px; }
.ch-grp.me .ch-m:not(.first) .ch-b { border-top-right-radius: 6px; }
.ch-grp.me .ch-m:not(.last) .ch-b { border-bottom-right-radius: 6px; }
.ch-b a { color: inherit; text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; overflow-wrap: anywhere; }
.ch-grp.them .ch-b a { color: var(--sky); }
.ch-m.pending .ch-b { opacity: .55; }
.ch-m.failed .ch-b { background: transparent; color: var(--ink); box-shadow: inset 0 0 0 1.5px var(--coral); }

/* time on hover / tap — sits beside the bubble, no layout shift */
.ch-t {
  position: absolute; top: 50%; transform: translateY(-50%);
  font-size: 11.5px; color: var(--ink-3); white-space: nowrap; font-variant-numeric: tabular-nums;
  opacity: 0; transition: opacity .15s; pointer-events: none;
}
.ch-grp.them .ch-t { left: calc(100% + 8px); }
.ch-grp.me .ch-t { right: calc(100% + 8px); }
.ch-m.show .ch-t { opacity: 1; }
@media (hover: hover) { .ch-m:hover .ch-t { opacity: 1; } }

/* reactions: picker under a tapped bubble, chips under any reacted one */
.ch-rbar {
  display: flex; gap: 2px; padding: 4px; margin: 4px 0 2px; border-radius: 999px;
  background: var(--card); box-shadow: 0 0 0 1px var(--line), var(--shadow);
  animation: ch-rbar-in .16s var(--spring);
}
@keyframes ch-rbar-in { from { opacity: 0; transform: translateY(-4px); } }
.ch-rbar button {
  width: 40px; height: 40px; border: 0; border-radius: 50%; background: none; padding: 0;
  font-family: var(--emoji); font-size: 22px; line-height: 1; cursor: pointer; transition: transform .12s, background .12s;
}
.ch-rbar button.on { background: var(--paper-2); }
.ch-rbar button:active { transform: scale(.88); }
@media (hover: hover) { .ch-rbar button:hover { background: var(--paper-2); } }
.ch-rx { display: flex; flex-wrap: wrap; gap: 4px; margin: -2px 0 2px; max-width: 85%; }
.ch-grp.me .ch-rx { justify-content: flex-end; }
.ch-rx button {
  display: inline-flex; align-items: center; gap: 3px; min-height: 26px; padding: 2px 8px; border: 0; border-radius: 999px;
  background: var(--card); box-shadow: 0 0 0 1px var(--line); font-family: var(--emoji); font-size: 14px; line-height: 1; cursor: pointer;
}
.ch-rx button b { font-family: var(--font-ui); font-size: 12px; font-weight: 600; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.ch-rx button.on { box-shadow: inset 0 0 0 1.5px var(--ink); }
.ch-rx button:disabled { cursor: default; color: inherit; }

.ch-status { font-size: 12px; color: var(--ink-3); padding: 2px 4px 0; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; max-width: 85%; }
.ch-status.fail { color: var(--coral); }
.ch-status.fail span { text-align: right; }
.ch-status button {
  display: inline-flex; align-items: center; gap: 4px; min-height: 32px; padding: 4px 10px;
  border: 0; border-radius: 999px; background: var(--paper-2); color: var(--ink); font-size: 13px; font-weight: 600;
}

/* new-messages pill */
.ch-pill {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(var(--ch-compose-h, 72px) + 10px); z-index: 3;
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 6px 14px;
  border: 0; border-radius: 999px; background: var(--ink); color: var(--card);
  font-size: 13.5px; font-weight: 600; box-shadow: var(--shadow);
  animation: ch-fade .2s var(--spring);
}
@keyframes ch-fade { from { opacity: 0; transform: translate(-50%, 6px); } }

/* composer */
.ch-compose {
  flex: none; display: flex; align-items: flex-end; gap: 8px; margin: 0;
  padding: 8px calc(env(safe-area-inset-right, 0px) + 10px) calc(env(safe-area-inset-bottom, 0px) + 8px) calc(env(safe-area-inset-left, 0px) + 10px);
  border-top: 1px solid var(--line); background: var(--card);
}
#chat.kb .ch-compose { padding-bottom: 8px; }
.ch-compose textarea {
  flex: 1; min-width: 0; resize: none; display: block;
  min-height: 44px; max-height: 140px; margin: 0;
  padding: 11px 16px; border: 0; border-radius: 22px;
  background: var(--paper-2); color: var(--ink);
  font: inherit; font-size: 16px; line-height: 22px; /* ≥16px: no iOS zoom */
  box-shadow: inset 0 0 0 1px transparent; transition: box-shadow .15s;
}
.ch-compose textarea::placeholder { color: var(--ink-3); }
.ch-compose textarea:focus { box-shadow: inset 0 0 0 1px var(--line); }
.ch-compose textarea:focus-visible { box-shadow: inset 0 0 0 1px var(--line) !important; }
.ch-send {
  flex: none; width: 44px; height: 44px; border: 0; border-radius: 50%;
  display: grid; place-items: center; background: var(--ink); color: var(--card);
  transition: opacity .15s, transform .1s;
}
.ch-send:disabled { opacity: .25; }
.ch-send:active:not(:disabled) { transform: scale(.94); }
.ch-join {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 8px; min-height: 44px;
  border-radius: 22px; background: var(--paper-2); color: var(--ink-3); font-size: 15px; font-weight: 500;
}

/* ---------- peek ---------- */
.ch-peek {
  position: fixed; z-index: 950;
  top: calc(env(safe-area-inset-top, 0px) + 64px); left: 12px; right: 12px; margin: 0 auto; max-width: 420px;
  display: flex; align-items: center; gap: 12px; padding: 10px 16px 10px 10px; text-align: left;
  border: 0; border-radius: 18px; background: var(--card); color: var(--ink);
  box-shadow: 0 0 0 1px var(--line), var(--shadow);
  opacity: 0; transform: translateY(-8px); transition: opacity .2s var(--spring), transform .22s var(--spring);
}
.ch-peek.in { opacity: 1; transform: none; }
.ch-peek .av { --s: 40px; }
.ch-pk-txt { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.ch-pk-txt b { font-size: 14px; font-weight: 600; }
.ch-pk-txt span { font-size: 14px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- desktop: floating card on the right ---------- */
@media (min-width: 900px) {
  #chat { left: auto; top: 12px; right: 12px; bottom: 12px; width: 380px; height: auto; }
  .ch-card {
    border-radius: var(--radius-l); box-shadow: 0 0 0 1px var(--line), var(--shadow);
    transform: translateX(16px);
  }
  .ch-head { padding: 16px 12px 12px 20px; }
  .ch-list { padding: 8px 14px 12px; }
  .ch-compose { padding: 10px 12px 12px; }
  .ch-name { max-width: 180px; }
  .ch-peek { top: 20px; left: auto; right: 20px; width: 340px; }
  .ch-peek.in { transform: none; }
}
@media (prefers-reduced-motion: reduce) {
  .ch-card, .ch-peek { transition: none; }
}

/* edit / delete my own messages */
.ch-mact { display: flex; gap: 6px; margin: 4px 0 2px; }
.ch-mact button {
  display: inline-flex; align-items: center; gap: 5px; min-height: 34px; padding: 4px 12px;
  border: 0; border-radius: 999px; background: var(--card); box-shadow: 0 0 0 1px var(--line);
  color: var(--ink); font-size: 13px; font-weight: 600; cursor: pointer; animation: ch-rbar-in .16s var(--spring);
}
.ch-mact button.danger { color: var(--coral); }
.ch-mact button.armed { background: var(--coral); color: #fff; box-shadow: none; }
.ch-ed { font-size: 11.5px; opacity: .6; margin-left: 6px; white-space: nowrap; }
.ch-m.deleted .ch-b { background: transparent !important; color: var(--ink-3) !important; box-shadow: inset 0 0 0 1px var(--line); }
.ch-m.deleted .ch-b i { font-style: italic; }
.ch-m.editing .ch-b { box-shadow: 0 0 0 2px var(--coral); }
.ch-compose { flex-wrap: wrap; }
.ch-editbar {
  flex: 0 0 100%; display: flex; align-items: center; gap: 6px; padding: 0 4px 2px;
  font-size: 13px; font-weight: 600; color: var(--ink-2);
}
.ch-editbar[hidden] { display: none; }
.ch-editbar span { flex: 1; }
.ch-editbar button { border: 0; background: none; color: var(--coral); font: inherit; font-weight: 600; padding: 6px 4px; cursor: pointer; }

/* nothing in the room may be wider than the room */
#ch-msgs, .ch-grp { min-width: 0; max-width: 100%; }
.ch-rbar, .ch-mact { max-width: 100%; flex-wrap: wrap; }
.ch-compose textarea { overflow-x: hidden; }
