.chat-markdown {
  max-width: 100%;
  word-wrap: break-word;
  overflow-wrap: break-word;
  font-size: 0.9375rem;
  line-height: 1.5;
  color: hsl(var(--foreground));

  ::ng-deep {
    remark {
      display: contents;
    }

    p {
      margin: 0.25em 0;
    }

    strong {
      font-weight: 700;
      color: hsl(var(--foreground));
    }

    em {
      font-style: italic;
    }

    del {
      text-decoration: line-through;
      opacity: 0.7;
    }

    a {
      color: hsl(var(--primary));
      text-decoration: underline;
      text-decoration-thickness: 1px;
      text-underline-offset: 2px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    h1,
    h2,
    h3,
    h4,
    h5,
    h6 {
      margin: 0.5em 0 0.25em;
      color: hsl(var(--foreground));
      font-weight: 700;
    }

    h1 {
      font-size: 1.5em;
    }
    h2 {
      font-size: 1.3em;
    }
    h3 {
      font-size: 1.15em;
    }

    ul,
    ol {
      margin: 0.25em 0;
      padding-left: 1.5em;
    }

    ul {
      list-style-type: disc;
    }

    ol {
      list-style-type: decimal;
    }

    li {
      margin: 0.125em 0;
    }

    blockquote {
      margin: 0.5em 0;
      border-left: 3px solid hsl(var(--primary) / 0.5);
      border-radius: 0 var(--radius) var(--radius) 0;
      background: hsl(var(--secondary) / 0.3);
      padding: 0.25em 0.75em;
      color: hsl(var(--muted-foreground));
    }

    code:not([class*='language-']) {
      white-space: pre-wrap;
      word-break: break-word;
      border-radius: 4px;
      background: hsl(var(--secondary));
      padding: 0.15em 0.35em;
      font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', monospace;
      font-size: 0.875em;
    }

    pre {
      margin: 0.5em 0;
      max-width: 100%;
      overflow-x: auto;
      border: 1px solid hsl(var(--border));
      border-radius: var(--radius);
      background: hsl(var(--secondary));
      padding: 0.75em 1em;

      code:not([class*='language-']) {
        border-radius: 0;
        background: transparent;
        padding: 0;
        white-space: pre;
        word-break: normal;
      }
    }

    pre[class*='language-'],
    code[class*='language-'] {
      text-shadow: none;
      font-family: 'Fira Code', 'Cascadia Code', 'JetBrains Mono', monospace;
      font-size: 0.875em;
    }

    pre[class*='language-'] {
      border: 1px solid hsl(var(--border));
      border-radius: var(--radius);
      padding: 0.875em 1rem;
    }

    pre[class*='language-'] > code[class*='language-'] {
      display: block;
      border-radius: 0;
      background: transparent;
      padding: 0;
      white-space: pre;
      word-break: normal;
    }

    hr {
      margin: 0.75em 0;
      border: none;
      border-top: 1px solid hsl(var(--border));
    }

    table {
      display: block;
      margin: 0.5em 0;
      width: auto;
      max-width: 100%;
      overflow-x: auto;
      border-collapse: collapse;
      font-size: 0.875em;
    }

    th,
    td {
      border: 1px solid hsl(var(--border));
      padding: 0.35em 0.75em;
      text-align: left;
    }

    th {
      background: hsl(var(--secondary));
      font-weight: 600;
    }

    img {
      display: block;
      max-height: 320px;
      max-width: 100%;
      height: auto;
      border-radius: var(--radius);
    }

    img.chat-custom-emoji-image {
      border-radius: 0;
    }

    .chat-inline-emoji-large {
      display: inline-block;
      font-size: 46px;
      line-height: 1;
      vertical-align: text-bottom;
    }

    p + p {
      margin-top: 0.25em;
    }

    remark-mermaid {
      display: block;
      max-width: 100%;
      overflow-x: auto;

      svg {
        pointer-events: none;
        max-width: 100%;
        height: auto;
      }
    }
  }
}

.chat-image-grid {
  display: grid;
  gap: 4px;
  width: min(100%, 22rem);
  overflow: hidden;
  border-radius: var(--radius);
}

.chat-image-grid--pair {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.chat-image-grid--triple {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.chat-image-grid--quad {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.chat-image-grid--triple .chat-image-grid-cell:last-child {
  grid-column: 1 / -1;
}

.chat-image-grid-cell {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: calc(var(--radius) - 2px);
  background: hsl(var(--secondary) / 0.45);
}

.chat-image-grid-image {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
  object-fit: cover;
}

.chat-image-grid-overflow {
  display: grid;
  place-items: center;
  border: none;
  background: hsl(var(--secondary) / 0.8);
  color: hsl(var(--foreground));
  cursor: pointer;
  transition: background-color 0.15s ease;

  &:hover {
    background: hsl(var(--secondary));
  }
}

.chat-image-grid-overflow-label {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.chat-image-grid-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.5rem;
  background: hsl(var(--secondary) / 0.45);
}

.chat-image-grid-loading-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: hsl(var(--primary));
}

.chat-image-grid-retry {
  border: none;
  border-radius: calc(var(--radius) - 4px);
  background: hsl(var(--secondary));
  padding: 0.25rem 0.5rem;
  font-size: 0.6875rem;
  color: hsl(var(--foreground));
  cursor: pointer;

  &:hover {
    background: hsl(var(--secondary) / 0.8);
  }
}

.edit-textarea {
  min-height: 42px;
  max-height: 520px;
  overflow-y: hidden;
  resize: none;
  transition: height 0.12s ease;
}
