/* Chat and its editor share the same message renderer and visual tokens. */
#pg-chat .chat-hdr {
  padding-top: max(12px, env(safe-area-inset-top));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
#pg-chat .chat-hdr > :nth-child(3) { min-width: 0; flex: 1; }
.chat-signature { font-size: 11px; color: var(--cd); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#pg-chat .chat-st { color: var(--cd); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#pg-chat .chat-av { flex: none; }
#pg-chat .chat-msgs {
  overscroll-behavior: contain;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}

.chat-message,
.msg.chat-message.u {
  display: flex;
  position: relative;
  align-items: flex-start;
  flex-direction: row;
  width: fit-content;
  max-width: max(8ch, var(--chat-max, 80%));
  min-width: 0;
  gap: 7px;
  margin-bottom: var(--chat-gap);
  color: inherit;
}
.chat-message.u { margin-left: auto; }
.chat-message[data-avatar-position="right"] { flex-direction: row-reverse; }
.msg.chat-message.u[data-avatar-position="right"] { flex-direction: row-reverse; }
.chat-message[data-avatar-position="above"],
.msg.chat-message.u[data-avatar-position="above"] { flex-direction: column; gap: 3px; }
.chat-message.u[data-avatar-position="above"] { align-items: flex-end; }
.chat-message .chat-message-avatar,
.msg.u .chat-message-avatar {
  display: flex;
  width: var(--chat-avatar-size);
  height: var(--chat-avatar-size);
  flex: 0 0 auto;
  margin-top: var(--chat-avatar-offset);
  border-radius: var(--chat-avatar-radius);
  background: rgba(255, 255, 255, .05);
}
.chat-message .chat-message-avatar[hidden] { display: none; }
.chat-message[data-avatar-enabled="true"]:not([data-avatar-position="above"]) .chat-message-avatar[hidden] {
  display: flex;
  visibility: hidden;
}
.chat-message-body, .chat-bubble-shell { min-width: 0; max-width: 100%; }
.chat-bubble-line { display: flex; align-items: flex-end; gap: 5px; }
.chat-message.u .chat-bubble-line { flex-direction: row-reverse; }
.chat-meta { display: flex; gap: 6px; font-size: 10px; line-height: 1.5; }
.chat-message.u .chat-meta { justify-content: flex-end; }
.chat-meta[hidden] { display: none; }
.chat-nickname, .chat-time {
  color: var(--cd);
  overflow-wrap: anywhere;
}
.chat-time { font-size: 10px; white-space: nowrap; }
.chat-bubble-shell { position: relative; }
#pg-chat .chat-message .chat-bubble,
#chatAppearancePreview .chat-message .chat-bubble {
  position: relative;
  padding: var(--chat-padding) calc(var(--chat-padding) * 1.44);
  border-radius: var(--chat-radius);
  border: var(--chat-border) solid rgba(255, 255, 255, var(--chat-glint));
  color: var(--c);
  white-space: pre-wrap;
  word-break: normal;
  overflow-wrap: anywhere;
  background:
    linear-gradient(155deg, rgba(255, 255, 255, calc(var(--chat-glint) * .48)), transparent 24%),
    linear-gradient(340deg, rgba(255, 255, 255, calc(var(--chat-glint) * .17)), transparent 16%),
    linear-gradient(135deg, var(--chat-color), var(--chat-color-end));
  -webkit-backdrop-filter: blur(var(--chat-blur)) saturate(var(--chat-saturation));
  backdrop-filter: blur(var(--chat-blur)) saturate(var(--chat-saturation));
  box-shadow: inset 0 .5px 0 rgba(255, 255, 255, var(--chat-glint)),
              inset 0 -.5px 0 rgba(255, 255, 255, calc(var(--chat-glint) * .45)),
              inset 1px 1px 5px -4px rgba(255, 255, 255, var(--chat-glint)),
              0 2px 8px rgba(0, 0, 0, calc(var(--chat-shadow) * .55));
}
:is(#pg-chat, #chatAppearancePreview) .chat-message[data-material="soft"] .chat-bubble {
  background: linear-gradient(135deg, var(--chat-color), var(--chat-color-end));
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
}
:is(#pg-chat, #chatAppearancePreview) .chat-message[data-material="frost"] .chat-bubble {
  -webkit-backdrop-filter: blur(var(--chat-blur));
  backdrop-filter: blur(var(--chat-blur));
}
.chat-message[data-fit="short"] .chat-bubble { max-width: 18ch; }
.chat-message[data-fit="section"] .chat-text-part + .chat-text-part { margin-top: .55em; }
.chat-text-part { display: block; }
.chat-tail {
  position: absolute;
  width: 10px;
  height: 10px;
  bottom: 5px;
  left: -5px;
  background: var(--chat-color);
  -webkit-backdrop-filter: blur(var(--chat-blur));
  backdrop-filter: blur(var(--chat-blur));
  border-right: var(--chat-border) solid rgba(255, 255, 255, var(--chat-glint));
  border-bottom: var(--chat-border) solid rgba(255, 255, 255, var(--chat-glint));
  border-radius: 2px;
  transform: rotate(135deg);
}
.chat-message.u .chat-tail { left: auto; right: -5px; transform: rotate(-45deg); }
.chat-message[data-tail-position="top"] .chat-tail { top: 5px; bottom: auto; }
.chat-message[data-tail-position="middle"] .chat-tail { top: calc(50% - 5px); bottom: auto; }
.chat-message.chat-error .chat-bubble { border-color: #e49aa6 !important; }

#chatForm {
  display: block;
  padding-left: max(14px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
}
.chat-composer {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
}
#chatForm #ti {
  background: transparent;
  box-shadow: none;
  -webkit-backdrop-filter: none;
  backdrop-filter: none;
  border: 0;
  color: inherit;
  min-width: 0;
  font-size: 13.5px;
}
#chatForm #ti::placeholder { color: var(--cd); }
#chatForm button { color: inherit; margin: 0; }
#chatForm button:disabled { opacity: .45; }
#chatForm button:focus-visible { outline: 2px solid var(--ac); outline-offset: 1px; }
#chatForm .chat-composer-tool { background: transparent; border-color: transparent; }
#chatForm .chat-composer-tool[aria-pressed="true"] { background: rgba(255, 255, 255, .15); }
#chatForm .chat-composer-tool svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.5; }
#chatAttachment { display: none; }
@media (prefers-reduced-transparency: reduce) {
  :is(#pg-chat, #chatAppearancePreview) .chat-message .chat-bubble { -webkit-backdrop-filter: none; backdrop-filter: none; }
}

.chat-appearance {
  color: var(--c);
  font: inherit;
  margin-bottom: 12px;
}
.chat-preview-wrap, .chat-editor-tabs {
  border-radius: var(--rs);
}
.chat-preview-wrap {
  padding: 14px;
  margin-bottom: 10px;
  background: rgba(255, 255, 255, .03);
  box-shadow: inset 0 .5px rgba(255, 255, 255, .28), 0 0 0 .5px rgba(255, 255, 255, .18);
  -webkit-backdrop-filter: blur(14px) saturate(1.4);
  backdrop-filter: blur(14px) saturate(1.4);
}
.chat-preview-heading { display: flex; align-items: center; justify-content: space-between; padding: 0 0 8px; font-size: 9px; color: var(--cd); }
.chat-preview-heading button, .chat-editor-tab, .chat-editor-action {
  color: var(--c);
  font: inherit;
  font-size: 12px;
  min-height: 34px;
  padding: 4px 11px;
  border: .5px solid rgba(255, 255, 255, .12);
  border-radius: 12px;
  background: rgba(255, 255, 255, .15);
  cursor: pointer;
}
#chatAppearancePreview {
  max-height: clamp(100px, 27vh, 220px);
  overflow: auto;
  overscroll-behavior: contain;
  border-radius: 12px;
  padding: 3px 5px;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}
#chatAppearanceStatus { font-size: 11px; min-height: 1.4em; color: var(--cd); margin: 4px 2px 0; }
#saveChatAppearance {
  background: rgba(255, 255, 255, .06);
  border-color: rgba(255, 255, 255, .1);
}
.chat-editor-entries {
  display: grid;
  gap: 4px;
  margin-bottom: 8px;
}
.chat-editor-entry {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  text-align: left;
  background: rgba(255, 255, 255, .06);
  border-color: rgba(255, 255, 255, .1);
}
.chat-settings-entry {
  width: 100%;
  color: var(--c);
  font: inherit;
  border: 0;
  text-align: left;
}
.chat-editor-entry-copy {
  display: grid;
  gap: 2px;
  min-width: 0;
}
.chat-editor-entry-description, .chat-editor-entry-arrow {
  color: var(--cd);
}
.chat-editor-entry-description { font-size: 11px; }
.chat-editor-entry-arrow { flex: none; }
#settingsHome[hidden], .chat-appearance[hidden] { display: none; }
.chat-editor-detail-header {
  --editor-nav-size: 16px;
  --editor-nav-weight: 300;
  --editor-nav-line-height: 1.4;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 8px;
}
.chat-editor-detail-header :is(.s-title, .chat-editor-action) {
  font-size: var(--editor-nav-size);
  font-weight: var(--editor-nav-weight);
  line-height: var(--editor-nav-line-height);
}
.chat-editor-detail-header .s-title { margin: 0; }
.chat-editor-section { padding: 0 4px 8px; }
.chat-appearance [hidden] { display: none !important; }
.chat-editor-field {
  display: grid;
  grid-template-columns: minmax(80px, 1fr) minmax(0, 1.35fr) auto;
  align-items: center;
  min-height: 36px;
  gap: 7px;
  font-size: 12px;
}
.chat-editor-field + .chat-editor-field { border-top: .5px solid rgba(255, 255, 255, .07); }
.chat-editor-field input {
  min-width: 0;
  max-width: 100%;
  color: inherit;
  font-size: 12px;
  accent-color: var(--ui-control-accent);
}
.chat-editor-field input[type="text"] {
  min-height: 30px;
  border-radius: 7px;
  padding: 3px 6px;
  border: .5px solid rgba(255, 255, 255, .2);
  background: rgba(255, 255, 255, .06);
}
.chat-editor-field input[type="checkbox"] { justify-self: start; width: 17px; height: 17px; }
.chat-editor-field input[type="color"] {
  box-sizing: border-box;
  width: 28px;
  height: 28px;
  justify-self: start;
  align-self: center;
  margin: 0;
  padding: 0;
  -webkit-appearance: none;
  appearance: none;
  border: 1px solid rgba(255, 255, 255, .2);
  border-radius: 50%;
  background: transparent;
}
.chat-editor-field input[type="color"]::-webkit-color-swatch-wrapper {
  padding: 2px;
  border-radius: inherit;
}
.chat-editor-field input[type="color"]::-webkit-color-swatch {
  border: 0;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .28);
}
.chat-editor-field input[type="color"]::-moz-color-swatch {
  border: 0;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .28);
}
.chat-editor-field input[type="range"] { width: 100%; }
.chat-editor-field input[type="file"] { width: 100%; }
.chat-editor-value { min-width: 2ch; color: var(--cd); text-align: right; font-variant-numeric: tabular-nums; }
.chat-editor-tabs { display: flex; gap: 4px; padding: 4px; margin-bottom: 8px; }
.chat-editor-tab { flex: 1; background: transparent; }
.chat-editor-tab[aria-pressed="true"] { background: rgba(255, 255, 255, .1); }
.chat-appearance :focus-visible { outline: 2px solid var(--ac); outline-offset: 1px; }

.chat-editor-options {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  min-width: 0;
  padding: 3px 0;
}
.chat-editor-option {
  position: relative;
  min-width: 0;
  cursor: pointer;
}
.chat-editor-option input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}
.chat-editor-option span {
  display: block;
  padding: 5px 8px;
  border: 1px solid rgba(255, 255, 255, .12);
  border-radius: 8px;
  background: rgba(255, 255, 255, .04);
  line-height: 1.3;
  overflow-wrap: anywhere;
}
.chat-editor-option input:checked + span {
  background: rgba(255, 255, 255, .15);
  border-color: rgba(255, 255, 255, .35);
  font-weight: 600;
}
.chat-editor-option input:focus-visible + span {
  outline: 2px solid var(--ac);
  outline-offset: 2px;
}
