@layer components {
  .chat {
    position: fixed;
    left: calc(var(--safe-left) + var(--rail-w) + 8px);
    bottom: calc(var(--safe-bottom) + var(--dock-h) + 10px);
    z-index: 40;
    pointer-events: none;
  }

  .chat__drawer {
    width: min(340px, calc(100vw - 24px));
    height: min(430px, calc(100dvh - var(--topbar-h) - var(--dock-h) - 40px));
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    border-radius: 22px;
    overflow: hidden;
    background:
      radial-gradient(120% 50% at 50% 0%, rgba(93, 233, 255, 0.14), transparent 60%),
      linear-gradient(180deg, rgba(34, 22, 64, 0.96), rgba(10, 7, 22, 0.97));
    box-shadow:
      0 0 0 1.5px #1a0c02,
      0 0 0 3.5px rgba(93, 233, 255, 0.7),
      0 0 0 5px #0a2230,
      0 30px 60px rgba(0, 0, 0, 0.55);
    transform-origin: 0 100%;
    transform: translateX(-24px) scale(0.9);
    opacity: 0;
    visibility: hidden;
    transition: transform 0.45s var(--ease-spring), opacity 0.2s ease, visibility 0.45s;
  }

  .chat.is-open .chat__drawer {
    transform: none;
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }

  .chat__head {
    display: grid;
    grid-template-columns: auto auto minmax(0, 1fr) auto;
    align-items: center;
    column-gap: 8px;
    padding: 10px 10px 10px 12px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }

  .chat__icon {
    width: 28px;
    height: 28px;
  }

  .chat__title {
    font-size: 16px;
    font-weight: 900;
  }

  .chat__online {
    justify-self: start;
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 11px;
    font-weight: 700;
    color: var(--text-faint);
  }

  .chat__online i {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #5ee06a;
    box-shadow: 0 0 8px #5ee06a;
  }

  .chat__close {
    position: relative;
    width: 32px;
    height: 32px;
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.08);
  }

  .chat__close::before,
  .chat__close::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    width: 14px;
    height: 3px;
    margin: -1.5px 0 0 -7px;
    border-radius: 2px;
    background: var(--text-dim);
    transform: rotate(45deg);
  }

  .chat__close::after {
    transform: rotate(-45deg);
  }

  .chat__list {
    overflow-y: auto;
    padding: 10px 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    overscroll-behavior: contain;
    -webkit-user-select: text;
    user-select: text;
  }

  .chat__msg {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    column-gap: 8px;
    max-width: 88%;
    padding: 7px 10px 8px;
    border-radius: 14px 14px 14px 4px;
    background: rgba(255, 255, 255, 0.07);
    animation: msg-in 0.35s var(--ease-out);
  }

  .chat__msg.is-me {
    align-self: flex-end;
    border-radius: 14px 14px 4px 14px;
    background: linear-gradient(180deg, rgba(255, 212, 74, 0.22), rgba(255, 170, 30, 0.14));
    box-shadow: inset 0 0 0 1px rgba(255, 212, 74, 0.25);
  }

  .chat__from {
    font-size: 11.5px;
    font-weight: 900;
    color: var(--cyan);
  }

  .chat__msg.is-me .chat__from {
    color: var(--gold);
  }

  .chat__from--link {
    justify-self: start;
    text-align: left;
    text-decoration: underline;
    text-decoration-color: rgba(93, 233, 255, 0.35);
    text-underline-offset: 2px;
  }

  .chat__from--link:hover {
    text-decoration-color: currentcolor;
  }

  .chat__time {
    grid-row: 1;
    grid-column: 2;
    font-size: 10px;
    font-weight: 700;
    color: var(--text-faint);
  }

  .chat__text {
    grid-column: 1 / -1;
    font-size: 13.5px;
    font-weight: 600;
    line-height: 1.35;
    word-break: break-word;
  }

  .chat__form {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    padding: 10px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
  }

  .chat__input {
    height: 42px;
    padding: 0 14px;
    border: 0;
    border-radius: 14px;
    font-size: 14px;
    font-weight: 600;
    background: rgba(0, 0, 0, 0.45);
    box-shadow: inset 0 0 0 1.5px rgba(255, 255, 255, 0.12);
    outline: none;
    -webkit-user-select: text;
    user-select: text;
  }

  .chat__input:focus {
    box-shadow: inset 0 0 0 2px rgba(93, 233, 255, 0.7);
  }

  .chat__send {
    position: relative;
    width: 46px;
    height: 42px;
    border-radius: 14px;
    background:
      radial-gradient(120% 70% at 50% 0%, rgba(255, 255, 255, 0.55), rgba(255, 255, 255, 0) 55%),
      linear-gradient(180deg, #8ff3ff, #1fb6e0);
    box-shadow: 0 0 0 2px #06283a, 0 3px 0 0 #0b5a7a, inset 0 2px 0 rgba(255, 255, 255, 0.5);
  }

  .chat__send::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    width: 0;
    height: 0;
    margin: -8px 0 0 -5px;
    border-left: 14px solid #fff;
    border-top: 8px solid transparent;
    border-bottom: 8px solid transparent;
    filter: drop-shadow(0 1px 0 rgba(0, 60, 90, 0.8));
  }

  .chat__peek {
    position: fixed;
    z-index: 45;
    max-width: 240px;
    padding: 8px 12px;
    border-radius: 14px;
    font-size: 12.5px;
    font-weight: 600;
    line-height: 1.35;
    color: var(--text);
    background: linear-gradient(180deg, rgba(34, 22, 64, 0.96), rgba(12, 8, 26, 0.97));
    box-shadow: 0 0 0 1.5px rgba(93, 233, 255, 0.6), 0 10px 24px rgba(0, 0, 0, 0.5);
    opacity: 0;
    transform: translate(-6px, -50%) scale(0.9);
    transition: opacity 0.25s ease, transform 0.35s var(--ease-spring);
    pointer-events: none;
    cursor: var(--cursor-hand, pointer);
  }

  .chat__peek[data-side='below'] {
    transform: translateY(-6px) scale(0.9);
  }

  .chat__peek.is-in {
    opacity: 1;
    transform: translate(0, -50%);
    pointer-events: auto;
  }

  .chat__peek[data-side='below'].is-in {
    transform: none;
  }

  .chat__peek b {
    color: var(--cyan);
    font-weight: 900;
  }

  @media (orientation: portrait) and (max-aspect-ratio: 4/5) {
    .chat {
      left: 8px;
      right: 8px;
      bottom: calc(var(--safe-bottom) + var(--dock-h) + var(--lane-h, 0px) + 6px);
    }

    .chat__drawer {
      width: 100%;
      height: min(420px, 56dvh);
      transform-origin: 50% 100%;
      transform: translateY(24px) scale(0.94);
    }
  }
}

@layer fx {
  @keyframes msg-in {
    from {
      opacity: 0;
      transform: translateY(8px);
    }

    to {
      opacity: 1;
      transform: none;
    }
  }

  .chat__head {
    display: flex;
    align-items: center;
    gap: 5px;
    min-width: 0;
  }

  .chat__head .chat__from {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .chat__badge {
    flex: 0 0 auto;
    padding: 1px 6px;
    border-radius: 99px;
    font-size: 9px;
    font-style: normal;
    font-weight: 900;
    letter-spacing: 0.08em;
    line-height: 1.5;
    color: #150a00;
    background: linear-gradient(180deg, #fff1b8, #f0b534);
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
  }

  .chat__badge[data-role='mod'] {
    color: #f4eaff;
    background: linear-gradient(180deg, #b48cff, #6a36d8);
  }

  .chat__badge[data-role='verified'] {
    display: grid;
    place-items: center;
    width: 14px;
    height: 14px;
    padding: 0;
    font-size: 8.5px;
    color: #fff;
    background: linear-gradient(180deg, #7fd8ff, #1e9be0);
  }
}
