.message-content {
  position: relative;
  max-width: var(--max-width);

  @media (max-width: 600px) {
    max-width: calc(100vw - 7rem);

    // Workaround for sass function override - we should use CSS min() here
    @supports (max-width: #{"min(29rem, 100vw - 7rem)"}) {
      max-width: #{"min(29rem, 100vw - 7rem)"};
    }

    .MessageList.no-avatars & {
      max-width: calc(100vw - 4.5rem);

      @supports (max-width: #{"min(29rem, 100vw - 4.5rem)"}) {
        max-width: #{"min(29rem, 100vw - 4.5rem)"};
      }
    }

    .Message.own & {
      max-width: calc(100vw - 4.5rem);

      @supports (max-width: #{"min(30rem, 100vw - 4.5rem)"}) {
        max-width: #{"min(30rem, 100vw - 4.5rem)"};
      }
    }
  }

  .text-content {
    margin: 0;
    word-break: break-word;
    line-height: 1.3125;
    text-align: initial;
    display: flow-root;
    unicode-bidi: plaintext;
  }

  .text-entity-link {
    unicode-bidi: plaintext;
  }

  &:not(.custom-shape) .text-content,
  &.document {
    & > .MessageMeta {
      position: relative;
      top: .4375rem;
      bottom: auto !important;
      float: right;
      line-height: 1;
      margin-left: .4375rem;
      margin-right: -.5rem;
    }

    &:dir(rtl) {
      & > .MessageMeta {
        float: left;
        margin-left: -.25rem;
        margin-right: .4375rem;
      }
    }
  }

  &.document:not(.text) {
    &::after {
      content: "";
      display: block;
      clear: both;
    }

    & > .MessageMeta {
      position: relative;
      top: auto;
      bottom: -.5rem !important;
      margin-top: -.25rem;
      &:not([dir=rtl]) {
        margin-top: -1.25rem;
      }
    }
  }

  .theme-dark .Message.own & {
    .text-entity-link {
      text-decoration: underline;

      &:hover {
        text-decoration: none;
      }
    }
  }

  &:not(.custom-shape) {
    font-size: var(--message-text-size, 1rem);

    & > .content-inner {
      min-width: 0;
    }
  }

  .matching-text-highlight {
    background: #CAE3F7;
    border-radius: 0.25rem;
    padding: 0 0.125rem;
  }

  .message-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.25rem;
    color: var(--accent-color);
    unicode-bidi: plaintext;
    display: flex;

    & > .interactive {
      overflow: hidden;
      text-overflow: ellipsis;
    }

    &.interactive, & > .interactive {
      cursor: pointer;

      &:hover {
        opacity: 0.85;
      }
    }

    & > .color-1 {
      color: var(--color-user-1);
    }

    & > .color-2 {
      color: var(--color-user-2);
    }

    & > .color-4 {
      color: var(--color-user-4);
    }

    & > .color-5 {
      color: var(--color-user-5);
    }

    & > .color-6 {
      color: var(--color-user-6);
    }

    & > .color-7 {
      color: var(--color-user-7);
    }

    & > .color-8 {
      color: var(--color-user-8);
    }

    .theme-dark .Message.own & > .color-1 {
      color: var(--accent-color);
    }

    & + .File {
      margin-top: .25rem;
    }

    .via {
      padding: 0 0.2rem;
      font-weight: 400;
    }

    .admin-title {
      flex: 1;
      margin-left: 1rem;
      text-align: right;
      font-weight: 400;
      font-size: 0.75rem;
      margin-top: -0.1rem;
      color: rgba(var(--color-text-meta-rgb), 0.75);

      .Message.own & {
        color: var(--accent-color);
      }
    }
  }

  &.has-shadow {
    box-shadow: 0 1px 2px var(--color-default-shadow);
  }

  &.has-solid-background, .is-album & {
    background: var(--background-color);
  }

  &.has-solid-background {
    padding: .3125rem .5rem .375rem;

    .forwarded-message > .text-content:not(.with-meta):last-child::after {
      content: '';
      display: inline-block;
      width: var(--meta-safe-area-size);
      height: 1rem;
    }
  }

  &:not(.has-appendix) .svg-appendix {
    display: none;
  }

  .svg-appendix {
    overflow: hidden;
    position: absolute;
    bottom: -.0875rem;
    width: .5625rem;
    height: 1.125rem;

    .corner {
      fill: var(--background-color);
    }

    .Message.own & {
      right: -.551rem; // This value is correct. Safari fix
    }

    .Message:not(.own) & {
      left: -.562rem; // This value is correct. Safari fix
    }
  }

  &[data-has-custom-appendix] {
    .svg-appendix .corner {
      fill: var(--appendix-bg);
    }
  }

  &.has-appendix-thumb {
    .Message & {
      .svg-appendix {
        --background-color: #ccc;
        opacity: 1;
        transition: opacity 500ms ease;
      }
    }
  }

  &.document {
    padding: .5rem !important;

    .File {
      .theme-dark & {
        --color-primary: var(--color-selection-highlight);
      }
    }

    .File + .text-content {
      margin-top: .3125rem;
    }
  }

  &.voice,
  &.audio {
    .message-title,
    .Embedded {
      margin-top: -.1875rem;
      margin-bottom: .1875rem;
    }
  }

  &.voice {
    padding: .5rem !important;

    .Voice + .text-content {
      margin-top: .5rem;
    }
  }

  &.audio {
    min-width: 17.1875rem;
    padding: .5rem .5rem .8125rem !important;

    .Audio + .text-content {
      margin-top: .25rem;
    }

    &:not(.has-replies) {
      .Audio + .text-content {
        margin-bottom: -0.4375rem;
      }
    }
  }

  &:not(.custom-shape) .emoji {
    display: inline-block;
    width: 1.25rem;
    background-size: 1.25rem;
    color: transparent;
    margin-inline-end: 1px;
    vertical-align: text-bottom;

    &::selection {
      background-color: var(--color-selection-highlight-emoji);
      color: transparent;
    }
  }

  &:not(.custom-shape) .text-content .emoji {
    width: calc(1.25 * var(--message-text-size, 1rem));
    background-size: calc(1.25 * var(--message-text-size, 1rem));
  }

  .no-media-corners {
    --border-top-left-radius: 0;
    --border-top-right-radius: 0;
  }
}

.message-content.media, .WebPage {
  .media-inner {
    display: flex;
    justify-content: center;
    align-items: center;

    &.interactive {
      cursor: pointer;

      &.dark video {
        background-color: #232323;
      }
    }
  }

  &.round.is-reply {
    width: auto !important;
  }

  &.force-sender-name .media-inner,
  &.force-sender-name .Album,
  &.text .media-inner,
  &.is-via-bot .media-inner,
  &.is-reply .media-inner,
  &.is-reply .Album {
    margin-left: -0.5rem;
    margin-right: -0.5rem;
  }

  &.text .media-inner {
    margin-top: -0.3125rem;
  }

  &.is-reply .media-inner,
  &.force-sender-name .Album,
  &.is-reply .Album,
  .message-title ~ .media-inner {
    margin-top: 0.375rem;
    margin-bottom: -0.375rem;
  }

  // Moved below .is-reply to overwrite its styles
  &.text .media-inner,
  &.text .Album {
    margin-bottom: 0.375rem;
  }

  .media-loading {
    display: flex;
    align-items: center;
    justify-content: center;

    &:not(.open) {
      opacity: 0.5;
      transform: scale(0);
      transition: opacity .3s ease, transform .3s ease;
    }
  }

  .icon-large-play, .icon-download {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    opacity: 0.7;
    transition: opacity 150ms;

    &:hover {
      opacity: 1;
    }

    &:before {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 3.375rem;
      height: 3.375rem;
      border-radius: 50%;
      background: rgba(black, 0.25);
    }
  }

  .icon-download {
    font-size: 1.5rem;
  }

  .icon-large-play {
    font-size: 2.5rem;
  }

  .message-media-duration,
  .message-upload-progress {
    background: rgba(0, 0, 0, .25);
    color: #fff;
    font-size: 0.75rem;
    position: absolute;
    left: .1875rem;
    top: .1875rem;
    z-index: 1;
    padding: 0 .375rem;
    border-radius: .75rem;
    line-height: 1.125rem;
  }

  .message-media-duration .icon-muted-chat {
    vertical-align: -.1875rem;
    margin-left: .375rem;
    font-size: 1.0625rem;
  }
}

.message-content.custom-shape {
  line-height: 1.2;
  font-size: 10rem;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;

  &.has-comments {
    min-height: 5.25rem;
    margin-top: 0 !important;

    &.has-replies {
      min-height: 6.5rem;
    }
  }

  &.is-reply {
    max-width: 100%;
  }

  .reply-message {
    display: flex;
    align-items: flex-start;
    flex-direction: row-reverse;
    direction: ltr;

    > p {
      margin-bottom: 0;
    }

    .EmbeddedMessage {
      margin-left: 0.5rem;
      border-radius: var(--border-radius-messages);

      @media (max-width: 600px) {
        max-width: calc(90vw - 13rem);
      }

      @media (max-width: 340px) {
        margin-left: -1rem;
        z-index: 1;
        max-width: calc(90vw - 12rem);
      }
    }
  }

  .text-content {
    margin-bottom: 1rem;
  }

  .media-inner {
    line-height: 1;
    font-size: 1rem;
    flex-shrink: 0;

    .full-media {
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
    }
  }

  .emoji {
    display: inline-block;
    color: transparent;

    &::selection {
      background-color: var(--color-selection-highlight-emoji);
      color: transparent;
    }
  }

  &.emoji-only {
    .text-content {
      margin-bottom: 0;
    }
  }

  &.emoji-only-1 {
    min-width: 8rem;
    font-size: 4.5rem;

    .content-inner {
      height: 7rem;
    }

    .text-content {
      line-height: 1.5;
      text-align: center;
    }

    .Message.was-edited & {
      min-width: 10rem;
    }

    .emoji {
      width: 5rem;
      height: 5rem;
    }
  }

  &.emoji-only-2 {
    font-size: 4rem;
    margin-top: .5rem;
    min-width: 10rem;

    &.has-comments {
      margin-top: 1.25rem;
    }

    .Message.was-edited & {
      min-width: 12rem;
    }

    .emoji {
      width: 4rem;
      height: 4rem;
    }
  }

  &.emoji-only-3 {
    font-size: 3rem;
    margin-top: 1.75rem;
    min-width: 12rem;

    &.has-comments {
      margin-top: 2.5rem;
    }

    .Message.was-edited & {
      min-width: 14rem;
    }

    .emoji {
      width: 3rem;
      height: 3rem;
    }
  }

  .Message:not(.own) & {
    --hover-color: var(--color-reply-hover);
    --active-color: var(--color-reply-active);
  }

  .Message.own & {
    --hover-color: var(--color-reply-own-hover);
    --active-color: var(--color-reply-own-active);

    .reply-message {
      flex-direction: row;

      .EmbeddedMessage {
        margin-right: 0.5rem;

        @media (max-width: 340px) {
          margin-left: 0;
          z-index: 1;
          max-width: calc(90vw - 11.5rem);
        }
      }
    }
  }
}

.forwarded-message {
  .message-content.poll & {
    // MessageOutgoingStatus's icon needs more space
    margin-bottom: 0.5rem;
  }

  .message-content:not(.custom-shape) & {
    position: relative;
    margin-top: 0.25rem;
    padding-left: 0.625rem;

    &::before {
      content: '';
      display: block;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0.05rem;
      width: 2px;
      background: var(--accent-color);
      border-radius: 2px;
    }

    &:dir(rtl) {
      padding-inline-start: 0.625rem;

      &::before {
        left: auto;
        right: 0.05rem;
      }
    }
  }

  --border-top-left-radius: var(--border-radius-messages-small);
  --border-top-right-radius: var(--border-radius-messages-small);
  --border-bottom-left-radius: var(--border-radius-messages-small);
  --border-bottom-right-radius: var(--border-radius-messages-small);

  > .media-inner {
    margin: 0 !important;
    margin-bottom: 0.25rem !important;
  }
}

.long-word-break-all {
  word-break: break-all;
}

.text-entity-link {
  color: var(--color-links) !important;
  text-decoration: none;
  word-break: break-word;
  cursor: pointer;
  unicode-bidi: initial;

  &:hover, &:active, &:visited {
    color: var(--color-links-hover) !important;
    text-decoration: underline;
  }
}

.text-entity-code, .text-entity-pre {
  color: var(--color-code);
  background: var(--color-code-bg);
  white-space: pre-wrap;
  margin: 0;
  padding: 1px 2px;
  border-radius: 4px;
}
